-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
183 lines (159 loc) · 5.19 KB
/
encode_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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package uuid7
import (
"fmt"
"testing"
"time"
)
func TestOutput(t *testing.T) {
var uuids [10]string
for i := 0; i < 10; i++ {
uuids[i] = NewString()
//time.Sleep(1 * time.Millisecond)
}
for i := 0; i < 10; i++ {
fmt.Println(uuids[i])
}
}
func TestZero(t *testing.T) {
uuid := UUID{}
encoded := EncodeBase58(uuid)
if encoded != "111111111_1111111111111" {
t.Errorf("encoded string should be 111111111_1111111111111, got %s", encoded)
}
}
func TestOne(t *testing.T) {
testVecs := []string{
"18AQGAut7_N92awznwCnjuR",
"112d7dWtQ_Mvj9WttA3mMnX",
"1111NKioe_UVktgzXLJ1B3u",
"111115qCH_TcgbQwpvYZQ9d",
"11111126U_w2Vvq8EnJ7hRH",
"11111111F_PBt6CHo3fovdM",
"111111111_4FzkJ37568tQw",
"111111111_11jpXCZedGfVR",
"111111111_111Ahg1opVcGX",
"111111111_11113CUsUpv9u",
"111111111_111111VtB5VXd",
"111111111_11111117YXq9H",
"111111111_111111112UzHM",
"111111111_1111111111LUw",
"111111111_111111111115R",
"111111111_1111111111112",
}
for i := 0; i < 16; i++ {
uuid := UUID{}
uuid[i] = byte(1)
encoded := EncodeBase58(uuid)
exp := testVecs[i]
if encoded != exp {
t.Errorf("%v expected %s, got %s", uuid, exp, encoded)
}
dec, err := DecodeBase58(encoded)
if err != nil {
t.Error(err)
}
if dec != uuid {
t.Errorf("decoded UUID should be %v, got %v", uuid, dec)
}
}
}
func TestOrder(t *testing.T) {
uuid1 := NewString()
// sleep for 1 microsecond to make sure the next uuid is different
time.Sleep(1 * time.Microsecond)
uuid2 := NewString()
if uuid1 >= uuid2 {
t.Errorf("uuid1 should be smaller than uuid2, got %s and %s", uuid1, uuid2)
}
}
func TestEncodeDecode(t *testing.T) {
var prev string
for u0 := 0; u0 < 256; u0++ {
for u1 := 0; u1 < 256; u1++ {
for u2 := 0; u2 < 256; u2++ {
for u3 := 0; u3 < 2; u3++ {
uuid := UUID{byte(u0), byte(u1), byte(u2), byte(u3), 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, byte(u3 ^ u1)}
encoded := EncodeBase58(uuid)
decoded, err := DecodeBase58(encoded)
if err != nil {
t.Error(err)
}
if decoded != uuid {
t.Errorf("decoded UUID should be %v, got %v", uuid, decoded)
}
if prev != "" && prev >= encoded {
t.Errorf("prev should be smaller than uuid, got %v and %v", prev, encoded)
}
prev = encoded
}
}
}
}
}
func TestLengthChange22to21(t *testing.T) {
for lastByte := 0; lastByte < 256; lastByte++ {
// encodes to 22 characters
smallerUUID := UUID{0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, byte(lastByte)}
smallerEncoded := encodeBase58Raw(smallerUUID)
if len(smallerEncoded) != 22 {
t.Errorf("encoded string should be 22 characters long, got %d", len(smallerEncoded))
}
// encodes to 21 characters
largerUUID := UUID{0, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, byte(lastByte)}
largerEncoded := encodeBase58Raw(largerUUID)
if len(largerEncoded) != 21 {
t.Errorf("encoded string should be 21 characters long, got %d", len(largerEncoded))
}
// compare without padding - works as expected for all lastByte values
if smallerEncoded > largerEncoded {
t.Errorf("smallerEncoded should be smaller than largerEncoded, got %s and %s", smallerEncoded, largerEncoded)
}
// compare with padding
smallerEncodedPad := EncodeBase58(smallerUUID)
largerEncodedPad := EncodeBase58(largerUUID)
if smallerEncodedPad > largerEncodedPad {
t.Errorf("smallerEncodedPad should be smaller than largerEncodedPad, got %s and %s", smallerEncodedPad, largerEncodedPad)
}
}
}
func TestLengthChange21to22(t *testing.T) {
for lastByte := 0; lastByte < 256; lastByte++ {
// encodes to 21 characters
smallerUUID := UUID{0, 34, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, byte(lastByte)}
smallerEncoded := encodeBase58Raw(smallerUUID)
if len(smallerEncoded) != 21 {
t.Errorf("encoded string should be 21 characters long, got %d", len(smallerEncoded))
}
// encodes to 22 characters
largerUUID := UUID{0, 35, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, byte(lastByte)}
largerEncoded := encodeBase58Raw(largerUUID)
if len(largerEncoded) != 22 {
t.Errorf("encoded string should be 21 characters long, got %d", len(largerEncoded))
}
// compare without padding
happens := false
if smallerEncoded > largerEncoded {
// this happens indeed for all lastByte values
happens = true
}
if !happens {
t.Errorf("we found counter-example: %v %s and %v %s", smallerUUID, smallerEncoded, largerUUID, largerEncoded)
}
// compare with padding
smallerEncodedPad := EncodeBase58(smallerUUID)
largerEncodedPad := EncodeBase58(largerUUID)
if smallerEncodedPad > largerEncodedPad {
// this never happens for all lastByte values
t.Errorf("smallerEncodedPad should be smaller than largerEncodedPad, got %s and %s", smallerEncodedPad, largerEncodedPad)
}
// last sanity checks
decSmallerUUID, _ := DecodeBase58(smallerEncodedPad)
if decSmallerUUID != smallerUUID {
t.Errorf("decSmallerUUID should be %v, got %v", smallerUUID, decSmallerUUID)
}
decLargerUUID, _ := DecodeBase58(largerEncodedPad)
if decLargerUUID != largerUUID {
t.Errorf("decLargerUUID should be %v, got %v", largerUUID, decLargerUUID)
}
}
}