This repository was archived by the owner on Apr 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloor_ceil_test.go
More file actions
136 lines (113 loc) · 3 KB
/
floor_ceil_test.go
File metadata and controls
136 lines (113 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package typeid
import (
"testing"
"time"
"github.com/google/uuid"
)
type testUserPrefix struct{}
func (testUserPrefix) Prefix() string { return "user" }
type testOrgPrefix struct{}
func (testOrgPrefix) Prefix() string { return "org" }
func TestFloorUUID(t *testing.T) {
now := time.Now()
floor := floorUUID[testUserPrefix](now)
u := floor.UUID()
if u.Version() != 7 {
t.Fatalf("version = %d, want 7", u.Version())
}
if u.Variant() != uuid.RFC4122 {
t.Fatalf("variant = %d, want RFC4122", u.Variant())
}
// Any UUIDv7 generated after now must be >= floor.
for range 100 {
id, err := NewUUID[testUserPrefix]()
if err != nil {
t.Fatal(err)
}
if id.UUID().String() < u.String() {
t.Fatalf("NewUUID %s < floor %s", id.UUID(), u)
}
}
}
func TestCeilUUID(t *testing.T) {
now := time.Now()
ceil := ceilUUID[testUserPrefix](now)
floor := floorUUID[testUserPrefix](now)
u := ceil.UUID()
if u.Version() != 7 {
t.Fatalf("version = %d, want 7", u.Version())
}
if u.Variant() != uuid.RFC4122 {
t.Fatalf("variant = %d, want RFC4122", u.Variant())
}
if ceil.UUID().String() < floor.UUID().String() {
t.Fatalf("ceil %s < floor %s", ceil.UUID(), floor.UUID())
}
}
func TestFloorCeilUUID_Bracket(t *testing.T) {
id, err := NewUUID[testUserPrefix]()
if err != nil {
t.Fatal(err)
}
u := id.UUID()
ts := id.GetTime()
floor := floorUUID[testUserPrefix](ts)
ceil := ceilUUID[testUserPrefix](ts)
if floor.UUID().String() > u.String() {
t.Fatalf("floor %s > id %s", floor.UUID(), u)
}
if ceil.UUID().String() < u.String() {
t.Fatalf("ceil %s < id %s", ceil.UUID(), u)
}
}
func TestFloorUUID_TimestampRoundTrip(t *testing.T) {
ts := time.Date(2026, 3, 27, 12, 0, 0, 0, time.UTC)
floor := floorUUID[testUserPrefix](ts)
if got := floor.GetTime(); got.UnixMilli() != ts.UnixMilli() {
t.Fatalf("GetTime() = %v, want %v", got, ts)
}
}
func TestFloorInt64(t *testing.T) {
now := time.Now()
floor := floorInt64[testOrgPrefix](now)
for range 100 {
id, err := NewInt64[testOrgPrefix]()
if err != nil {
t.Fatal(err)
}
if id.Int64() < floor.Int64() {
t.Fatalf("NewInt64 %d < floor %d", id.Int64(), floor.Int64())
}
}
}
func TestCeilInt64(t *testing.T) {
now := time.Now()
ceil := ceilInt64[testOrgPrefix](now)
floor := floorInt64[testOrgPrefix](now)
if ceil.Int64() < floor.Int64() {
t.Fatalf("ceil %d < floor %d", ceil.Int64(), floor.Int64())
}
}
func TestFloorCeilInt64_Bracket(t *testing.T) {
id, err := NewInt64[testOrgPrefix]()
if err != nil {
t.Fatal(err)
}
v := id.Int64()
ts := id.GetTime()
floor := floorInt64[testOrgPrefix](ts)
ceil := ceilInt64[testOrgPrefix](ts)
if floor.Int64() > v {
t.Fatalf("floor %d > id %d", floor.Int64(), v)
}
if ceil.Int64() < v {
t.Fatalf("ceil %d < id %d", ceil.Int64(), v)
}
}
func TestInt64_GetTime(t *testing.T) {
ts := time.Date(2026, 3, 27, 12, 0, 0, 0, time.UTC)
floor := floorInt64[testOrgPrefix](ts)
if got := floor.GetTime(); got.UnixMilli() != ts.UnixMilli() {
t.Fatalf("GetTime() = %v, want %v", got, ts)
}
}