Skip to content

Add check for Counter that {Int/Long}.MIN_VALUE be perfectly divisible by incrementBy #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ public sealed class Counter private constructor() {
* - [incrementBy] is less than or equal to 0
* - [incrementBy] is greater than [MAX_INCREMENT]
* - [incrementBy] is not a factor of 8
* - [incrementBy] does not go into [Int.MIN_VALUE] (i.e. when `Int.MIN_VALUE % incrementBy != 0`)
* - [lo] is not a factor of [incrementBy]
* */
public constructor(lo: Int, hi: Int, incrementBy: Int): super() {
require(incrementBy > 0) { "incrementBy[$incrementBy] must be greater than 0" }
require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" }
require(incrementBy % 8 == 0) { "incrementBy[$incrementBy] must be a factor of 8" }
require(Int.MIN_VALUE % incrementBy == 0) { "Int.MIN_VALUE % incrementBy[$incrementBy] != 0" }
require(lo % incrementBy == 0) { "lo must be a factor of incrementBy[$incrementBy]" }

this.incrementBy = incrementBy
Expand All @@ -113,6 +115,7 @@ public sealed class Counter private constructor() {
* - Less than or equal to 0
* - Greater than [MAX_INCREMENT]
* - Not a factor of 8
* - Does not go into [Int.MIN_VALUE] (i.e. when `Int.MIN_VALUE % incrementBy != 0`)
* */
public constructor(incrementBy: Int): this(0, 0, incrementBy)

Expand Down Expand Up @@ -210,12 +213,14 @@ public sealed class Counter private constructor() {
* - [incrementBy] is less than or equal to 0
* - [incrementBy] is greater than [MAX_INCREMENT]
* - [incrementBy] is not a factor of 8
* - [incrementBy] does not go into [Long.MIN_VALUE] (i.e. when `Long.MIN_VALUE % incrementBy != 0`)
* - [lo] is not a factor of [incrementBy]
* */
public constructor(lo: Long, hi: Long, incrementBy: Long): super() {
require(incrementBy > 0L) { "incrementBy[$incrementBy] must be greater than 0" }
require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" }
require(incrementBy % 8 == 0L) { "incrementBy[$incrementBy] must be a factor of 8" }
require(Long.MIN_VALUE % incrementBy == 0L) { "Long.MIN_VALUE % incrementBy[$incrementBy] != 0" }
require(lo % incrementBy == 0L) { "lo must be a factor of incrementBy[$incrementBy]" }

this.incrementBy = incrementBy
Expand All @@ -230,6 +235,7 @@ public sealed class Counter private constructor() {
* - Less than or equal to 0
* - Greater than [MAX_INCREMENT]
* - Not a factor of 8
* - Does not go into [Long.MIN_VALUE] (i.e. when `Long.MIN_VALUE % incrementBy != 0`)
* */
public constructor(incrementBy: Long): this(0, 0, incrementBy)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,27 @@ class CounterUnitTest {
}
}

@Test
fun givenIncrementBy_whenNumberMinNotDivisibleByIt_thenThrowsException() {
assertFailsWith<IllegalArgumentException> {
Counter.Bit32(0, 0, 24)
}
assertFailsWith<IllegalArgumentException> {
Counter.Bit64(0, 0, 24)
}
}

@Test
fun givenLo_whenNotFactoryIncrementBy_thenThrowsException() {
// Would throw if 24 was not a factor of 8...
Counter.Bit32(24)
Counter.Bit64(24)
// Would throw if 32 was not a factor of 8...
Counter.Bit32(32)
Counter.Bit64(32)

assertFailsWith<IllegalArgumentException> {
Counter.Bit32(8, 0, 24)
Counter.Bit32(8, 0, 32)
}
assertFailsWith<IllegalArgumentException> {
Counter.Bit64(16, 0, 24)
Counter.Bit64(16, 0, 32)
}
}

Expand Down
Loading