update experiment
This commit is contained in:
parent
ae5bf3ddb1
commit
ae80217d4f
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.uuid;
|
||||
|
||||
import com.fasterxml.uuid.impl.UUIDUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
@ -9,27 +8,16 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
|
||||
public class UUIDPositiveLongGeneratorUnitTest {
|
||||
|
||||
private void verifyUUID(UUID uuid) {
|
||||
byte[] bytes = toByteArray(uuid);
|
||||
|
||||
// assert that byte at index 6 is 0x40 (version 4)
|
||||
byte byte6 = bytes[6];
|
||||
assertThat(byte6 & 0xF0).isEqualTo(0x40);
|
||||
|
||||
// assert that the byte at index 8 is 0x80 (IETF type variant)
|
||||
byte byte8 = bytes[8];
|
||||
assertThat(byte8 & 0xC0).isEqualTo(0x80);
|
||||
@Test
|
||||
public void whengetMostSignificantBits_thenAssertPositive() {
|
||||
long randomPositiveLong = Math.abs(UUID.randomUUID().getMostSignificantBits());
|
||||
assertThat(randomPositiveLong).isPositive();
|
||||
}
|
||||
|
||||
private boolean[] getFirst122Bits(UUID uuid) {
|
||||
long msb = uuid.getMostSignificantBits();
|
||||
boolean[] bits = new boolean[122]; // Untuk menyimpan 122 bit pertama dari UUID
|
||||
|
||||
// Konversi 64 bit pertama (Most Significant Bits) menjadi bit
|
||||
for (int i = 0; i < 64; i++) {
|
||||
bits[i] = ((msb >> (63 - i)) & 1) == 1; // Mendapatkan nilai bit ke-i
|
||||
}
|
||||
return bits;
|
||||
@Test
|
||||
public void whengetLeastSignificantBits_thenAssertPositive() {
|
||||
long randomPositiveLong = Math.abs(UUID.randomUUID().getLeastSignificantBits());
|
||||
assertThat(randomPositiveLong).isPositive();
|
||||
}
|
||||
|
||||
private byte[] toByteArray(UUID uuid) {
|
||||
|
@ -49,10 +37,24 @@ public class UUIDPositiveLongGeneratorUnitTest {
|
|||
public void whenGivenUUID_thenVerified() {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
verifyUUID(uuid);
|
||||
byte[] bytes = toByteArray(uuid);
|
||||
|
||||
long msbfirst = uuid.getMostSignificantBits() >>> 6;
|
||||
System.out.println(msbfirst);
|
||||
// assert that byte at index 6 is 0x40 (version 4)
|
||||
byte byte6 = bytes[6];
|
||||
assertThat(byte6 & 0xF0).isEqualTo(0x40);
|
||||
|
||||
// assert that the byte at index 8 is 0x80 (IETF type variant)
|
||||
byte byte8 = bytes[8];
|
||||
assertThat(byte8 & 0xC0).isEqualTo(0x80);
|
||||
|
||||
// 1 byte = 8 bites
|
||||
int totalBites = bytes.length * 8;
|
||||
|
||||
// totalBites - 6 (4 bits for version and 2 bits for variant).
|
||||
int randomBitsCount = totalBites - 6;
|
||||
|
||||
// assert that number of random bits is 122
|
||||
assertThat(randomBitsCount).isEqualTo(122);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue