-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_range_test.go
More file actions
169 lines (158 loc) · 3.65 KB
/
reader_range_test.go
File metadata and controls
169 lines (158 loc) · 3.65 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
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
package tinykvs
import (
"fmt"
"testing"
)
func TestRangeOverlaps(t *testing.T) {
tests := []struct {
name string
start []byte
end []byte
minKey []byte
maxKey []byte
want bool
}{
{
name: "range fully within table",
start: []byte("key5"),
end: []byte("key8"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: true,
},
{
name: "range equals table bounds",
start: []byte("key0"),
end: []byte("key9"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: true,
},
{
name: "range overlaps start of table",
start: []byte("aaa"),
end: []byte("key5"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: true,
},
{
name: "range overlaps end of table",
start: []byte("key5"),
end: []byte("zzz"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: true,
},
{
name: "range before table",
start: []byte("aaa"),
end: []byte("bbb"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: false,
},
{
name: "range after table",
start: []byte("zzz"),
end: []byte("zzzz"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: false,
},
{
name: "range end equals table min",
start: []byte("aaa"),
end: []byte("key0"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: false, // [start, end) is exclusive of end
},
{
name: "range start equals table max",
start: []byte("key9"),
end: []byte("zzz"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: true, // table max is inclusive
},
{
name: "range contains entire table",
start: []byte("aaa"),
end: []byte("zzz"),
minKey: []byte("key0"),
maxKey: []byte("key9"),
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := rangeOverlaps(tt.start, tt.end, tt.minKey, tt.maxKey)
if got != tt.want {
t.Errorf("rangeOverlaps(%q, %q, %q, %q) = %v, want %v",
tt.start, tt.end, tt.minKey, tt.maxKey, got, tt.want)
}
})
}
}
func TestScanRangeEdgeCases(t *testing.T) {
dir := t.TempDir()
opts := DefaultOptions(dir)
opts.MemtableSize = 64 * 1024
store, err := Open(dir, opts)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer store.Close()
// Add some keys
for i := 0; i < 100; i++ {
key := fmt.Sprintf("key_%03d", i)
store.PutInt64([]byte(key), int64(i))
}
store.Flush()
// Test: range with no overlap
count := 0
store.ScanRange([]byte("aaa"), []byte("bbb"), func(key []byte, val Value) bool {
count++
return true
})
if count != 0 {
t.Errorf("ScanRange(aaa, bbb) got %d keys, want 0", count)
}
// Test: range at exact boundaries
count = 0
store.ScanRange([]byte("key_050"), []byte("key_055"), func(key []byte, val Value) bool {
count++
return true
})
if count != 5 {
t.Errorf("ScanRange(key_050, key_055) got %d keys, want 5", count)
}
// Test: range beyond data
count = 0
store.ScanRange([]byte("zzz"), []byte("zzzz"), func(key []byte, val Value) bool {
count++
return true
})
if count != 0 {
t.Errorf("ScanRange(zzz, zzzz) got %d keys, want 0", count)
}
// Test: range covering all keys
count = 0
store.ScanRange([]byte("key_000"), []byte("key_999"), func(key []byte, val Value) bool {
count++
return true
})
if count != 100 {
t.Errorf("ScanRange(key_000, key_999) got %d keys, want 100", count)
}
// Test: early termination
count = 0
store.ScanRange([]byte("key_000"), []byte("key_999"), func(key []byte, val Value) bool {
count++
return count < 10 // Stop after 10
})
if count != 10 {
t.Errorf("ScanRange with early stop got %d keys, want 10", count)
}
}