-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathnode_test.go
More file actions
97 lines (83 loc) · 2.71 KB
/
Copy pathnode_test.go
File metadata and controls
97 lines (83 loc) · 2.71 KB
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 mmdbwriter
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/maxmind/mmdbwriter/v2/mmdbtype"
)
func TestNewNodeIndexRejectsSentinel(t *testing.T) {
// On 32-bit platforms the sentinel wraps to a negative int, which the
// negative-index guard rejects instead of the sentinel comparison.
sentinel := uint64(noNodeIndex)
require.Panics(t, func() {
newNodeIndex(int(sentinel)) //nolint:gosec // intentional boundary conversion
})
}
func TestRecordValueRejectsCompressedPath(t *testing.T) {
tree := &Tree{}
_, err := tree.recordValue(&record{recordType: recordTypePath}, nil)
require.EqualError(t, err, "compressed path record cannot be written before finalization")
}
func TestFinalizeNodeRejectsCompressedPath(t *testing.T) {
tree := &Tree{
nodeBlocks: [][]node{make([]node, nodeBlockSize)},
nodeCountAllocated: 1,
nodeNumbers: make([]int, 1),
}
tree.nodeAt(rootNodeIndex).children[0] = record{recordType: recordTypePath}
require.PanicsWithValue(t, "compressed path found after expandPaths", func() {
tree.finalizeNode(rootNodeIndex, 0)
})
}
// TestMaybeMergeChildren covers the reference-equality merge check. Sibling
// data records merge only when they hold the same canonical reference;
// colliding but distinct values must be left alone.
func TestMaybeMergeChildren(t *testing.T) {
tests := []struct {
name string
sameValue bool
want recordType
}{
{
name: "colliding distinct values are not merged",
sameValue: false,
want: recordTypeNode,
},
{
name: "identical values are merged",
sameValue: true,
want: recordTypeData,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tree := newTestTree(t, "mmdbwriter-merge")
// Both values share a bucket, so only the exact comparison can
// tell them apart.
tree.valueStore = newValueStoreWithHash(func([]byte) uint64 { return 1 })
first, err := tree.valueStore.intern(mmdbtype.String("first"))
require.NoError(t, err)
second := first
if test.sameValue {
tree.valueStore.retain(first)
} else {
second, err = tree.valueStore.intern(mmdbtype.String("second"))
require.NoError(t, err)
require.NotEqual(t, first, second)
}
parent := record{
nodeIndex: tree.newNode([2]record{
{value: first, recordType: recordTypeData},
{value: second, recordType: recordTypeData},
}),
recordType: recordTypeNode,
}
iRec := insertRecord{store: tree.valueStore, tree: tree}
require.NoError(t, iRec.maybeMergeChildren(&parent))
assert.Equal(t, test.want, parent.recordType)
if test.want == recordTypeData {
assert.Equal(t, first, parent.value)
}
})
}
}