Skip to content

Commit 68679d6

Browse files
committed
test(time): add test for Time and ClockSequence method
1 parent 7016e79 commit 68679d6

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

time_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,77 @@ func TestGetTime(t *testing.T) {
4242
})
4343
}
4444
}
45+
46+
func TestClockSequenceInitialization(t *testing.T) {
47+
// Backup and reset global state
48+
timeMu.Lock()
49+
origSeq := clockSeq
50+
clockSeq = 0 // Force initialization
51+
timeMu.Unlock()
52+
defer func() {
53+
timeMu.Lock()
54+
clockSeq = origSeq
55+
timeMu.Unlock()
56+
}()
57+
58+
// First call initializes the sequence
59+
seq := ClockSequence()
60+
if seq == 0 {
61+
t.Error("ClockSequence() should not return 0 after initialization")
62+
}
63+
64+
// Ensure the sequence is within 14-bit mask
65+
if seq&0xc000 != 0 {
66+
t.Errorf("ClockSequence() = %x, expected 14-bit value (mask 0x3fff)", seq)
67+
}
68+
69+
// Subsequent call should return the same sequence
70+
seq2 := ClockSequence()
71+
if seq2 != seq {
72+
t.Errorf("Expected sequence %x, got %x", seq, seq2)
73+
}
74+
}
75+
76+
func TestTime(t *testing.T) {
77+
tests := []struct {
78+
name string
79+
uuid UUID
80+
version int
81+
want Time
82+
}{
83+
{
84+
name: "Version 6",
85+
uuid: UUID{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0x6D, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
86+
version: 6,
87+
want: Time((0x12345678 << 28) | (0x9ABC << 12) | 0xDEF),
88+
},
89+
{
90+
name: "Version 7",
91+
uuid: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
92+
version: 7,
93+
want: Time(g1582ns100),
94+
},
95+
{
96+
name: "Default Version",
97+
uuid: UUID{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0x1D, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
98+
version: 1,
99+
want: Time(int64(0x12345678) | (int64(0x9ABC) << 32) | (int64(0xDEF) << 48)),
100+
},
101+
}
102+
103+
for _, tt := range tests {
104+
t.Run(tt.name, func(t *testing.T) {
105+
if got := tt.uuid.Time(); got != tt.want {
106+
t.Errorf("UUID.Time() = %v, want %v", got, tt.want)
107+
}
108+
})
109+
}
110+
}
111+
112+
func TestClockSequence(t *testing.T) {
113+
uuid := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
114+
expected := 0x1234 & 0x3fff
115+
if got := uuid.ClockSequence(); got != expected {
116+
t.Errorf("ClockSequence() = %x, want %x", got, expected)
117+
}
118+
}

0 commit comments

Comments
 (0)