This repository has been archived by the owner on Nov 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
indexnode_test.go
89 lines (73 loc) · 2.59 KB
/
indexnode_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
package simian
import (
"encoding/json"
"testing"
)
func TestIndexNode(t *testing.T) {
t.Run("JSON serialisation", func(t *testing.T) {
t.Run("should roundtrip all fields", func(t *testing.T) {
n := &IndexNode{
childFingerprintsByString: make(map[string]*Fingerprint),
}
n.registerChild(Fingerprint{samples: []uint8{0x10, 0x20, 0x30, 0x40}})
n.registerChild(Fingerprint{samples: []uint8{0x50, 0x60, 0x70, 0x80}})
entry1 := &IndexEntry{
MaxFingerprint: Fingerprint{samples: []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9}},
Attributes: make(map[string]interface{}),
}
n.registerEntry(entry1)
entry2 := &IndexEntry{
MaxFingerprint: Fingerprint{samples: []uint8{10, 11, 12, 13, 14, 15, 16, 17, 18}},
Attributes: make(map[string]interface{}),
}
n.registerEntry(entry2)
jsonBytes, err := json.Marshal(n)
if err != nil {
t.Fatalf("Error marshalling JSON: %v", err)
}
var result *IndexNode
err = json.Unmarshal(jsonBytes, &result)
if err != nil {
t.Fatalf("Error unmarshalling JSON: %v", err)
}
if actual, expected := len(result.childFingerprints), len(n.childFingerprints); actual != expected {
t.Fatalf("Expected %d child fingerprints but got %d", expected, actual)
}
for i := 0; i < len(result.childFingerprints); i++ {
actual := result.childFingerprints[i].String()
expected := n.childFingerprints[i].String()
if actual != expected {
t.Errorf("Expected fingerprint '%s' but got '%s'", expected, actual)
}
}
if actual, expected := len(result.childFingerprintsByString), len(n.childFingerprintsByString); actual != expected {
t.Fatalf("Expected %d child fingerprints mapped by string but got %d", expected, actual)
}
for k, v := range n.childFingerprintsByString {
actual := result.childFingerprintsByString[k].String()
expected := v.String()
if actual != expected {
t.Errorf("Expected fingerprint '%s' but got '%s'", expected, actual)
}
}
if actual, expected := len(result.entries), len(n.entries); actual != expected {
t.Fatalf("Expected %d entries but got %d", expected, actual)
}
for i := 0; i < len(result.entries); i++ {
actualBytes, err := json.Marshal(result.entries[i])
if err != nil {
t.Fatalf("Error marshalling entry: %v", err)
}
actual := string(actualBytes)
expectedBytes, err := json.Marshal(n.entries[i])
if err != nil {
t.Fatalf("Error marshalling entry: %v", err)
}
expected := string(expectedBytes)
if actual != expected {
t.Errorf("Expected entry '%s' but got '%s'", expected, actual)
}
}
})
})
}