forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_test.go
210 lines (196 loc) · 4.56 KB
/
id_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ids
import (
"bytes"
"reflect"
"testing"
)
func TestID(t *testing.T) {
id := ID{24}
idCopy := ID{24}
prefixed := id.Prefix(0)
if id != idCopy {
t.Fatalf("ID.Prefix mutated the ID")
}
if nextPrefix := id.Prefix(0); prefixed != nextPrefix {
t.Fatalf("ID.Prefix not consistent")
}
}
func TestIDBit(t *testing.T) {
id0 := ID{1 << 0}
id1 := ID{1 << 1}
id2 := ID{1 << 2}
id3 := ID{1 << 3}
id4 := ID{1 << 4}
id5 := ID{1 << 5}
id6 := ID{1 << 6}
id7 := ID{1 << 7}
id8 := ID{0, 1 << 0}
switch {
case id0.Bit(0) != 1:
t.Fatalf("Wrong bit")
case id1.Bit(1) != 1:
t.Fatalf("Wrong bit")
case id2.Bit(2) != 1:
t.Fatalf("Wrong bit")
case id3.Bit(3) != 1:
t.Fatalf("Wrong bit")
case id4.Bit(4) != 1:
t.Fatalf("Wrong bit")
case id5.Bit(5) != 1:
t.Fatalf("Wrong bit")
case id6.Bit(6) != 1:
t.Fatalf("Wrong bit")
case id7.Bit(7) != 1:
t.Fatalf("Wrong bit")
case id8.Bit(8) != 1:
t.Fatalf("Wrong bit")
}
}
func TestFromString(t *testing.T) {
id := ID{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'}
idStr := id.String()
id2, err := FromString(idStr)
if err != nil {
t.Fatal(err)
}
if id != id2 {
t.Fatal("Expected FromString to be inverse of String but it wasn't")
}
}
func TestIDFromStringError(t *testing.T) {
tests := []struct {
in string
}{
{""},
{"foo"},
{"foobar"},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
_, err := FromString(tt.in)
if err == nil {
t.Error("Unexpected success")
}
})
}
}
func TestIDMarshalJSON(t *testing.T) {
tests := []struct {
label string
in ID
out []byte
err error
}{
{"ID{}", ID{}, []byte("\"11111111111111111111111111111111LpoYY\""), nil},
{
"ID(\"ava labs\")",
ID{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
[]byte("\"jvYi6Tn9idMi7BaymUVi9zWjg5tpmW7trfKG1AYJLKZJ2fsU7\""),
nil,
},
}
for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
out, err := tt.in.MarshalJSON()
if err != tt.err {
t.Errorf("Expected err %s, got error %v", tt.err, err)
} else if !bytes.Equal(out, tt.out) {
t.Errorf("got %q, expected %q", out, tt.out)
}
})
}
}
func TestIDUnmarshalJSON(t *testing.T) {
tests := []struct {
label string
in []byte
out ID
err error
}{
{"ID{}", []byte("null"), ID{}, nil},
{
"ID(\"ava labs\")",
[]byte("\"jvYi6Tn9idMi7BaymUVi9zWjg5tpmW7trfKG1AYJLKZJ2fsU7\""),
ID{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
nil,
},
}
for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
foo := ID{}
err := foo.UnmarshalJSON(tt.in)
if err != tt.err {
t.Errorf("Expected err %s, got error %v", tt.err, err)
} else if foo != tt.out {
t.Errorf("got %q, expected %q", foo, tt.out)
}
})
}
}
func TestIDHex(t *testing.T) {
id := ID{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'}
expected := "617661206c616273000000000000000000000000000000000000000000000000"
actual := id.Hex()
if actual != expected {
t.Fatalf("got %s, expected %s", actual, expected)
}
}
func TestIDString(t *testing.T) {
tests := []struct {
label string
id ID
expected string
}{
{"ID{}", ID{}, "11111111111111111111111111111111LpoYY"},
{"ID{24}", ID{24}, "Ba3mm8Ra8JYYebeZ9p7zw1ayorDbeD1euwxhgzSLsncKqGoNt"},
}
for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
result := tt.id.String()
if result != tt.expected {
t.Errorf("got %q, expected %q", result, tt.expected)
}
})
}
}
func TestSortIDs(t *testing.T) {
ids := []ID{
{'e', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
{'W', 'a', 'l', 'l', 'e', ' ', 'l', 'a', 'b', 's'},
{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
}
SortIDs(ids)
expected := []ID{
{'W', 'a', 'l', 'l', 'e', ' ', 'l', 'a', 'b', 's'},
{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
{'e', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
}
if !reflect.DeepEqual(ids, expected) {
t.Fatal("[]ID was not sorted lexographically")
}
}
func TestIsSortedAndUnique(t *testing.T) {
unsorted := []ID{
{'e', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
}
if IsSortedAndUniqueIDs(unsorted) {
t.Fatal("Wrongly accepted unsorted IDs")
}
duplicated := []ID{
{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
}
if IsSortedAndUniqueIDs(duplicated) {
t.Fatal("Wrongly accepted duplicated IDs")
}
sorted := []ID{
{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
{'e', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
}
if !IsSortedAndUniqueIDs(sorted) {
t.Fatal("Wrongly rejected sorted, unique IDs")
}
}