regular maintenance

This commit is contained in:
Fabio Lima 2021-12-11 13:57:46 -03:00
parent 44bab60674
commit 8a5fc3d093
2 changed files with 22 additions and 21 deletions

View File

@ -149,8 +149,8 @@ public final class Ulid implements Serializable, Comparable<Ulid> {
* @param ulid a ULID * @param ulid a ULID
*/ */
public Ulid(Ulid ulid) { public Ulid(Ulid ulid) {
this.msb = ulid.getMostSignificantBits(); this.msb = ulid.msb;
this.lsb = ulid.getLeastSignificantBits(); this.lsb = ulid.lsb;
} }
/** /**

View File

@ -313,29 +313,30 @@ public class UlidTest {
@Test @Test
public void testIncrement() { public void testIncrement() {
long msb; final long milliseconds = System.currentTimeMillis();
long lsb; final BigInteger increment = BigInteger.valueOf(DEFAULT_LOOP_MAX);
Ulid ulid;
final int loopMax = 100; // Test 1
byte[] random1 = { //
msb = 0x0123456789abcdefL; (byte) 0x00, (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, //
lsb = 0x0123456789abcdefL; (byte) 0x55, (byte) 0x66, (byte) 0x77, (byte) 0x88, (byte) 0x99 };
ulid = new Ulid(msb, lsb); Ulid ulid1 = new Ulid(milliseconds, random1);
for (int i = 0; i < loopMax; i++) { for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
ulid = ulid.increment(); ulid1 = ulid1.increment();
} }
assertEquals(msb, ulid.getMostSignificantBits()); assertEquals(milliseconds, ulid1.getTime());
assertEquals(msb + loopMax, ulid.getLeastSignificantBits()); assertEquals(new BigInteger(random1).add(increment), new BigInteger(ulid1.getRandom()));
msb = 0x0123456789abcdefL; // Test 2
lsb = 0xffffffffffffffffL - (loopMax / 2); byte[] random2 = { //
ulid = new Ulid(msb, lsb); (byte) 0x00, (byte) 0x11, (byte) 0xff, (byte) 0xff, (byte) 0xff, //
for (int i = 0; i < loopMax; i++) { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
ulid = ulid.increment(); Ulid ulid2 = new Ulid(milliseconds, random2);
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
ulid2 = ulid2.increment();
} }
assertEquals(msb + 1, ulid.getMostSignificantBits()); assertEquals(milliseconds, ulid2.getTime());
assertEquals((loopMax / 2) - 1, ulid.getLeastSignificantBits()); assertEquals(new BigInteger(random2).add(increment), new BigInteger(ulid2.getRandom()));
} }
@Test @Test