-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflake_test.go
83 lines (74 loc) · 2.33 KB
/
snowflake_test.go
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
package dgoflake
import (
"testing"
"time"
)
func TestSnowflake_Increment(t *testing.T) {
var id uint64 = 577645285396840449
snowflake := ParseInt(id)
expected := id & 0xFFF
actual := snowflake.Increment()
if expected != actual {
t.Errorf("unexpected increment value for snowflake %d: expected %d, got %d", id, expected, actual)
}
}
func TestSnowflake_Int64(t *testing.T) {
var id uint64 = 577645285396840449
snowflake := ParseInt(id)
expected := int64(id)
actual := snowflake.Int64()
if expected != actual {
t.Errorf("unexpected int64 value for snowflake %d: expected %d, got %d", id, expected, actual)
}
}
func TestSnowflake_InternalProcessID(t *testing.T) {
var id uint64 = 577645285396840449
snowflake := ParseInt(id)
expected := (id & 0x1F000) >> 12
actual := snowflake.InternalProcessID()
if expected != actual {
t.Errorf("unexpected internal process ID value for snowflake %d: expected %d, got %d", id, expected, actual)
}
}
func TestSnowflake_InternalWorkerID(t *testing.T) {
var id uint64 = 577645285396840449
snowflake := ParseInt(id)
expected := (id & 0x3E0000) >> 17
actual := snowflake.InternalWorkerID()
if expected != actual {
t.Errorf("unexpected internal worker ID value for snowflake %d: expected %d, got %d", id, expected, actual)
}
}
func TestSnowflake_String(t *testing.T) {
var id uint64 = 577645285396840449
snowflake := ParseInt(id)
expected := "577645285396840449"
actual := snowflake.String()
if expected != actual {
t.Errorf("unexpected string value for snowflake %d: expected %s, got %s", id, expected, actual)
}
}
func TestSnowflake_Timestamp(t *testing.T) {
var id uint64 = 577645285396840449
snowflake := ParseInt(id)
expected := time.Unix(0, ((int64(id)>>22)+int64(DiscordEpoch))*1000000).String()
actual := snowflake.Timestamp().String()
if expected != actual {
t.Errorf("unexpected timestamp value for snowflake %d: expected %s, got %s", id, expected, actual)
}
}
func TestSnowflake_UInt64(t *testing.T) {
var id uint64 = 577645285396840449
snowflake := ParseInt(id)
expected := id
actual := snowflake.UInt64()
if expected != actual {
t.Errorf("unexpected uint64 value for snowflake %d: expected %d, got %d", id, expected, actual)
}
}
func BenchmarkSnowflake_Timestamp(b *testing.B) {
snowflake := ParseInt(577654653257383975)
for i := 0; i < b.N; i++ {
snowflake.Timestamp()
}
}