-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathoperations_test.go
More file actions
98 lines (89 loc) · 2.87 KB
/
operations_test.go
File metadata and controls
98 lines (89 loc) · 2.87 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
98
package storage_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/onflow/flow-go/storage"
)
func TestPrefixInclusiveEnd(t *testing.T) {
tests := []struct {
name string
prefix []byte
start []byte
expected []byte
}{
{
name: "pads remaining bytes with 0xff",
prefix: []byte{0x01},
start: []byte{0x01, 0x02, 0x03},
expected: []byte{0x01, 0xff, 0xff},
},
{
name: "multi-byte prefix pads remaining bytes",
prefix: []byte{0x01, 0x02},
start: []byte{0x01, 0x02, 0x03, 0x04},
expected: []byte{0x01, 0x02, 0xff, 0xff},
},
{
name: "start same length as prefix - no padding added",
prefix: []byte{0x01, 0x02},
start: []byte{0x01, 0x02},
expected: []byte{0x01, 0x02},
},
{
name: "start bytes after prefix are replaced regardless of value",
prefix: []byte{0x01},
start: []byte{0x01, 0x00},
expected: []byte{0x01, 0xff},
},
{
// A shorter start is lexicographically less than prefix, so prefix is returned directly.
name: "start shorter than prefix - prefix returned directly",
prefix: []byte{0x01, 0x02, 0x03},
start: []byte{0x01, 0x02},
expected: []byte{0x01, 0x02, 0x03},
},
{
// start is lexicographically before prefix, so prefix is already beyond start - no padding needed.
name: "start lexicographically before prefix - prefix returned directly",
prefix: []byte{0x05},
start: []byte{0x03, 0x04},
expected: []byte{0x05},
},
{
// start content is entirely replaced by prefix + 0xff padding.
name: "start lexicographically after prefix - content ignored",
prefix: []byte{0x01},
start: []byte{0x08, 0x09},
expected: []byte{0x01, 0xff},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := storage.PrefixInclusiveEnd(tt.prefix, tt.start)
assert.Equal(t, tt.expected, result)
// When start is within or past the prefix namespace, result must start with prefix
// and be padded to len(start).
if len(tt.start) >= len(tt.prefix) && string(tt.start) >= string(tt.prefix) {
assert.Len(t, result, len(tt.start))
assert.Equal(t, tt.prefix, result[:len(tt.prefix)])
}
})
}
}
// TestPrefixInclusiveEnd_GreaterThanAnyKeyWithPrefix verifies that the result is
// always >= any key of the same length that starts with basePrefix.
func TestPrefixInclusiveEnd_GreaterThanAnyKeyWithPrefix(t *testing.T) {
basePrefix := []byte{0x01, 0x02}
start := []byte{0x01, 0x02, 0x10, 0x20} // a mid-range start key
end := storage.PrefixInclusiveEnd(basePrefix, start)
// Any key of len(start) starting with basePrefix must sort <= end.
candidates := [][]byte{
{0x01, 0x02, 0x00, 0x00},
{0x01, 0x02, 0x10, 0x20},
{0x01, 0x02, 0xff, 0xfe},
}
for _, key := range candidates {
assert.True(t, string(key) <= string(end),
"expected key %x <= end %x", key, end)
}
}