Skip to content

Commit a91307b

Browse files
committed
Add requirement that incrementBy be perfectly divisible into Number.MIN_VALUE
1 parent ce5ceec commit a91307b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

library/bits/src/commonMain/kotlin/org/kotlincrypto/bitops/bits/Counter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ public sealed class Counter private constructor() {
9393
* - [incrementBy] is less than or equal to 0
9494
* - [incrementBy] is greater than [MAX_INCREMENT]
9595
* - [incrementBy] is not a factor of 8
96+
* - [incrementBy] does not go into [Int.MIN_VALUE] (i.e. when `Int.MIN_VALUE % incrementBy != 0`)
9697
* - [lo] is not a factor of [incrementBy]
9798
* */
9899
public constructor(lo: Int, hi: Int, incrementBy: Int): super() {
99100
require(incrementBy > 0) { "incrementBy[$incrementBy] must be greater than 0" }
100101
require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" }
101102
require(incrementBy % 8 == 0) { "incrementBy[$incrementBy] must be a factor of 8" }
103+
require(Int.MIN_VALUE % incrementBy == 0) { "Long.MIN_VALUE % incrementBy[$incrementBy] != 0" }
102104
require(lo % incrementBy == 0) { "lo must be a factor of incrementBy[$incrementBy]" }
103105

104106
this.incrementBy = incrementBy
@@ -210,12 +212,14 @@ public sealed class Counter private constructor() {
210212
* - [incrementBy] is less than or equal to 0
211213
* - [incrementBy] is greater than [MAX_INCREMENT]
212214
* - [incrementBy] is not a factor of 8
215+
* - [incrementBy] does not go into [Long.MIN_VALUE] (i.e. when `Long.MIN_VALUE % incrementBy != 0`)
213216
* - [lo] is not a factor of [incrementBy]
214217
* */
215218
public constructor(lo: Long, hi: Long, incrementBy: Long): super() {
216219
require(incrementBy > 0L) { "incrementBy[$incrementBy] must be greater than 0" }
217220
require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" }
218221
require(incrementBy % 8 == 0L) { "incrementBy[$incrementBy] must be a factor of 8" }
222+
require(Long.MIN_VALUE % incrementBy == 0L) { "Long.MIN_VALUE % incrementBy[$incrementBy] != 0" }
219223
require(lo % incrementBy == 0L) { "lo must be a factor of incrementBy[$incrementBy]" }
220224

221225
this.incrementBy = incrementBy

library/bits/src/commonTest/kotlin/org/kotlincrypto/bitops/bits/CounterUnitTest.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,27 @@ class CounterUnitTest {
5252
}
5353
}
5454

55+
@Test
56+
fun givenIncrementBy_whenNumberMinNotDivisibleByIt_thenThrowsException() {
57+
assertFailsWith<IllegalArgumentException> {
58+
Counter.Bit32(0, 0, 24)
59+
}
60+
assertFailsWith<IllegalArgumentException> {
61+
Counter.Bit64(0, 0, 24)
62+
}
63+
}
64+
5565
@Test
5666
fun givenLo_whenNotFactoryIncrementBy_thenThrowsException() {
57-
// Would throw if 24 was not a factor of 8...
58-
Counter.Bit32(24)
59-
Counter.Bit64(24)
67+
// Would throw if 32 was not a factor of 8...
68+
Counter.Bit32(32)
69+
Counter.Bit64(32)
6070

6171
assertFailsWith<IllegalArgumentException> {
62-
Counter.Bit32(8, 0, 24)
72+
Counter.Bit32(8, 0, 32)
6373
}
6474
assertFailsWith<IllegalArgumentException> {
65-
Counter.Bit64(16, 0, 24)
75+
Counter.Bit64(16, 0, 32)
6676
}
6777
}
6878

0 commit comments

Comments
 (0)