diff --git a/library/bits/src/commonMain/kotlin/org/kotlincrypto/bitops/bits/Counter.kt b/library/bits/src/commonMain/kotlin/org/kotlincrypto/bitops/bits/Counter.kt index 77c83bb..c53c359 100644 --- a/library/bits/src/commonMain/kotlin/org/kotlincrypto/bitops/bits/Counter.kt +++ b/library/bits/src/commonMain/kotlin/org/kotlincrypto/bitops/bits/Counter.kt @@ -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 @@ -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) @@ -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 @@ -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) diff --git a/library/bits/src/commonTest/kotlin/org/kotlincrypto/bitops/bits/CounterUnitTest.kt b/library/bits/src/commonTest/kotlin/org/kotlincrypto/bitops/bits/CounterUnitTest.kt index 9850e44..82b4d0a 100644 --- a/library/bits/src/commonTest/kotlin/org/kotlincrypto/bitops/bits/CounterUnitTest.kt +++ b/library/bits/src/commonTest/kotlin/org/kotlincrypto/bitops/bits/CounterUnitTest.kt @@ -52,17 +52,27 @@ class CounterUnitTest { } } + @Test + fun givenIncrementBy_whenNumberMinNotDivisibleByIt_thenThrowsException() { + assertFailsWith { + Counter.Bit32(0, 0, 24) + } + assertFailsWith { + 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 { - Counter.Bit32(8, 0, 24) + Counter.Bit32(8, 0, 32) } assertFailsWith { - Counter.Bit64(16, 0, 24) + Counter.Bit64(16, 0, 32) } }