Skip to content

Commit e7b62e4

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

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 6 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) { "Int.MIN_VALUE % incrementBy[$incrementBy] != 0" }
102104
require(lo % incrementBy == 0) { "lo must be a factor of incrementBy[$incrementBy]" }
103105

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

@@ -210,12 +213,14 @@ public sealed class Counter private constructor() {
210213
* - [incrementBy] is less than or equal to 0
211214
* - [incrementBy] is greater than [MAX_INCREMENT]
212215
* - [incrementBy] is not a factor of 8
216+
* - [incrementBy] does not go into [Long.MIN_VALUE] (i.e. when `Long.MIN_VALUE % incrementBy != 0`)
213217
* - [lo] is not a factor of [incrementBy]
214218
* */
215219
public constructor(lo: Long, hi: Long, incrementBy: Long): super() {
216220
require(incrementBy > 0L) { "incrementBy[$incrementBy] must be greater than 0" }
217221
require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" }
218222
require(incrementBy % 8 == 0L) { "incrementBy[$incrementBy] must be a factor of 8" }
223+
require(Long.MIN_VALUE % incrementBy == 0L) { "Long.MIN_VALUE % incrementBy[$incrementBy] != 0" }
219224
require(lo % incrementBy == 0L) { "lo must be a factor of incrementBy[$incrementBy]" }
220225

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

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)