forked from sourcegraph/appdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_test.go
97 lines (87 loc) · 1.77 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
package appdash
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
func TestIDMarshalJSON(t *testing.T) {
id := ID(10018820)
buf := bytes.NewBuffer(nil)
json.NewEncoder(buf).Encode(id)
want := `"000000000098e004"`
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestIDUnmarshalJSONHexString(t *testing.T) {
j := []byte(`"000000000098e004"`)
var got ID
if err := json.Unmarshal(j, &got); err != nil {
t.Fatal(err)
}
want := ID(10018820)
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestIDUnmarshalJSONInt(t *testing.T) {
j := []byte(`10018820`)
var got ID
if err := json.Unmarshal(j, &got); err != nil {
t.Fatal(err)
}
want := ID(10018820)
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestIDUnmarshalJSONNonInt(t *testing.T) {
j := []byte(`[]`)
var got ID
err := json.Unmarshal(j, &got)
if err == nil {
t.Fatalf("unexpectedly unmarshalled %v", got)
}
}
func TestIDUnmarshalJSONNonHexString(t *testing.T) {
j := []byte(`"woo"`)
var got ID
err := json.Unmarshal(j, &got)
if err == nil {
t.Fatalf("unexpectedly unmarshalled %v", got)
}
}
func TestIDGeneration(t *testing.T) {
n := 10000
ids := make(map[ID]bool, n)
for i := 0; i < n; i++ {
id := generateID()
if ids[id] {
t.Errorf("duplicate ID: %v", id)
}
ids[id] = true
}
}
func TestParseID(t *testing.T) {
want := ID(10018181901)
got, err := ParseID(want.String())
if err != nil {
t.Error(err)
}
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestParseIDError(t *testing.T) {
id, err := ParseID("woo")
if err == nil {
t.Errorf("unexpectedly parsed value: %v", id)
}
}
func BenchmarkIDGeneration(b *testing.B) {
for i := 0; i < b.N; i++ {
generateID()
}
}