|
| 1 | +package com.fasterxml.uuid.impl; |
| 2 | + |
| 3 | +import com.fasterxml.uuid.UUIDClock; |
| 4 | +import junit.framework.TestCase; |
| 5 | + |
| 6 | +import java.math.BigInteger; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.UUID; |
| 9 | +import java.util.function.Consumer; |
| 10 | + |
| 11 | +public class TimeBasedEpochGeneratorTest extends TestCase { |
| 12 | + |
| 13 | + public void testFormat() { |
| 14 | + BigInteger minEntropy = BigInteger.ZERO; |
| 15 | + long minTimestamp = 0; |
| 16 | + TimeBasedEpochGenerator generatorEmpty = new TimeBasedEpochGenerator(staticEntropy(minEntropy), staticClock(minTimestamp)); |
| 17 | + UUID uuidEmpty = generatorEmpty.generate(); |
| 18 | + assertEquals(0x07, uuidEmpty.version()); |
| 19 | + assertEquals(0x02, uuidEmpty.variant()); |
| 20 | + assertEquals(minTimestamp, getTimestamp(uuidEmpty)); |
| 21 | + assertEquals(minEntropy, getEntropy(uuidEmpty)); |
| 22 | + |
| 23 | + Consumer<byte[]> entropyFull = bytes -> Arrays.fill(bytes, (byte) 0xFF); |
| 24 | + long maxTimestamp = rightBitmask(48); |
| 25 | + TimeBasedEpochGenerator generatorFull = new TimeBasedEpochGenerator(entropyFull, staticClock(maxTimestamp)); |
| 26 | + UUID uuidFull = generatorFull.generate(); |
| 27 | + assertEquals(0x07, uuidFull.version()); |
| 28 | + assertEquals(0x02, uuidFull.variant()); |
| 29 | + assertEquals(maxTimestamp, getTimestamp(uuidFull)); |
| 30 | + assertEquals(BigInteger.ONE.shiftLeft(73).subtract(BigInteger.ONE), getEntropy(uuidFull)); |
| 31 | + } |
| 32 | + |
| 33 | + public void testIncrement() { |
| 34 | + TimeBasedEpochGenerator generator = new TimeBasedEpochGenerator(staticEntropy(BigInteger.ZERO), staticClock(0)); |
| 35 | + assertEquals(BigInteger.valueOf(0), getEntropy(generator.generate())); |
| 36 | + assertEquals(BigInteger.valueOf(1), getEntropy(generator.generate())); |
| 37 | + assertEquals(BigInteger.valueOf(2), getEntropy(generator.generate())); |
| 38 | + assertEquals(BigInteger.valueOf(3), getEntropy(generator.generate())); |
| 39 | + } |
| 40 | + |
| 41 | + public void testCarryOnce() { |
| 42 | + TimeBasedEpochGenerator generator = new TimeBasedEpochGenerator(staticEntropy(BigInteger.valueOf(0xFF)), staticClock(0)); |
| 43 | + assertEquals(BigInteger.valueOf(0xFF), getEntropy(generator.generate())); |
| 44 | + assertEquals(BigInteger.valueOf(0x100), getEntropy(generator.generate())); |
| 45 | + } |
| 46 | + |
| 47 | + public void testCarryAll() { |
| 48 | + BigInteger largeEntropy = BigInteger.ONE.shiftLeft(73).subtract(BigInteger.ONE); |
| 49 | + TimeBasedEpochGenerator generator = new TimeBasedEpochGenerator(staticEntropy(largeEntropy), staticClock(0)); |
| 50 | + assertEquals(largeEntropy, getEntropy(generator.generate())); |
| 51 | + assertEquals(BigInteger.ONE.shiftLeft(73), getEntropy(generator.generate())); |
| 52 | + } |
| 53 | + |
| 54 | + private long getTimestamp(UUID uuid) { |
| 55 | + return uuid.getMostSignificantBits() >>> 16; |
| 56 | + } |
| 57 | + |
| 58 | + private BigInteger getEntropy(UUID uuid) { |
| 59 | + return BigInteger.valueOf(uuid.getMostSignificantBits() & rightBitmask(12)).shiftLeft(62).or( |
| 60 | + BigInteger.valueOf(uuid.getLeastSignificantBits() & rightBitmask(62))); |
| 61 | + } |
| 62 | + |
| 63 | + private Consumer<byte[]> staticEntropy(BigInteger entropy) { |
| 64 | + byte[] entropyBytes = entropy.toByteArray(); |
| 65 | + return bytes -> { |
| 66 | + int offset = bytes.length - entropyBytes.length; |
| 67 | + Arrays.fill(bytes, 0, offset, (byte) 0x00); |
| 68 | + System.arraycopy(entropyBytes, 0, bytes, offset, entropyBytes.length); |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + private UUIDClock staticClock(long timestamp) { |
| 73 | + return new UUIDClock() { |
| 74 | + @Override |
| 75 | + public long currentTimeMillis() { |
| 76 | + return timestamp; |
| 77 | + } |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + private long rightBitmask(int bits) { |
| 82 | + return (1L << bits) - 1; |
| 83 | + } |
| 84 | +} |
0 commit comments