Skip to content

Commit d923856

Browse files
carldenigmathibaultmeyer
authored andcommitted
fix: force usage of safe absolute value when generating v1 (#11)
1 parent 5a0b7fb commit d923856

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/main/java/io/github/thibaultmeyer/cuid/CUID.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private static synchronized int nextCounterValue() {
225225
*/
226226
private static String getRandomBlock() {
227227

228-
return Common.padWithZero(Integer.toString(Common.nextIntValue() * DISCRETE_VALUE, NUMBER_BASE), BLOCK_SIZE);
228+
return Common.padWithZero(Integer.toString(safeAbs(Common.nextIntValue() * DISCRETE_VALUE), NUMBER_BASE), BLOCK_SIZE);
229229
}
230230
}
231231

src/test/java/io/github/thibaultmeyer/cuid/CUIDv1Test.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,13 @@ void unicityOver500000() {
132132
// Assert
133133
Assertions.assertEquals(500000, cuidSet.size());
134134
}
135+
136+
@Test
137+
void validCreation500000() {
138+
// Act & Assert
139+
for (int i = 0; i < 500000; i += 1) {
140+
final CUID cuid = CUID.randomCUID1();
141+
Assertions.assertTrue(CUID.isValid(cuid.toString()));
142+
}
143+
}
135144
}

0 commit comments

Comments
 (0)