-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucketby_test.go
More file actions
181 lines (163 loc) · 4.02 KB
/
bucketby_test.go
File metadata and controls
181 lines (163 loc) · 4.02 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
170
171
172
173
174
175
176
177
178
179
180
181
package table
import (
"fmt"
"testing"
)
// sampleData generates a synthetic bucket data set with `rows` rows and `cols` columns.
func sampleData(rows, cols int) [][]string {
data := make([][]string, rows)
for i := 0; i < rows; i++ {
row := make([]string, cols)
for j := 0; j < cols; j++ {
// simple pattern: "C{col}-R{row}"
row[j] = fmt.Sprintf("C%d-R%d", j, i)
}
data[i] = row
}
return data
}
func TestGetBy_SingleClause(t *testing.T) {
rows := [][]string{
{"apple", "red"},
{"banana", "yellow"},
{"apple", "green"},
{"cherry", "red"},
}
b := newBucket(rows)
result := b.getBy(map[int]string{0: "apple"})
if len(result) != 2 {
t.Fatalf("expected 2 rows, got %d", len(result))
}
for _, r := range result {
if r[0] != "apple" {
t.Errorf("unexpected row %v", r)
}
}
}
func TestGetByMultiClause(t *testing.T) {
rows := [][]string{
{"user1", "admin", "active"},
{"user2", "member", "inactive"},
{"user3", "admin", "inactive"},
{"user4", "member", "active"},
}
b := newBucket(rows)
result := b.getBy(map[int]string{1: "admin", 2: "inactive"})
if len(result) != 1 {
t.Fatalf("expected 1 row, got %d", len(result))
}
expected := []string{"user3", "admin", "inactive"}
if fmt.Sprint(result[0]) != fmt.Sprint(expected) {
t.Errorf("got %v, want %v", result[0], expected)
}
}
func TestGetByNoMatch(t *testing.T) {
rows := [][]string{{"a", "b"}, {"c", "d"}}
b := newBucket(rows)
result := b.getBy(map[int]string{0: "x"})
if result != nil {
t.Errorf("expected nil, got %v", result)
}
}
func TestRemoveByBasic(t *testing.T) {
rows := [][]string{
{"1", "keep"},
{"2", "remove"},
{"3", "remove"},
{"4", "keep"},
}
b := newBucket(rows)
b.removeBy(map[int]string{1: "remove"})
// After removal, only 2 rows should remain non-nil
count := 0
for _, r := range b.all() {
if r != nil {
count++ // non-nil rows
if r[1] == "remove" {
t.Errorf("found removed row %v", r)
}
}
}
if count != 2 {
t.Errorf("expected 2 remaining rows, got %d", count)
}
// After removal, only 2 rows should remain non-nil
count = 0
for _, r := range b.getBy(make(map[int]string)) {
if r != nil {
count++ // non-nil rows
if r[1] == "remove" {
t.Errorf("found removed row %v", r)
}
}
}
if count != 0 {
t.Errorf("expected no remaining rows when querying empty map, got %d", count)
}
// After removal, only 2 rows should remain non-nil
count = 0
for _, r := range b.getBy(nil) {
if r != nil {
count++ // non-nil rows
if r[1] == "remove" {
t.Errorf("found removed row %v", r)
}
}
}
if count != 0 {
t.Errorf("expected no remaining rows when querying nil, got %d", count)
}
}
func TestRemoveBy_MultiClause(t *testing.T) {
rows := [][]string{
{"a", "x", "1"},
{"a", "y", "2"},
{"b", "x", "2"},
{"b", "y", "1"},
}
b := newBucket(rows)
b.removeBy(map[int]string{0: "a", 2: "2"})
remaining := b.getBy(make(map[int]string))
for _, r := range remaining {
if r != nil && r[0] == "a" && r[2] == "2" {
t.Errorf("row %v should have been removed", r)
}
}
}
// BenchmarkgetBy compares getBy against a manual filter + GetAll for varying sizes
func BenchmarkGetBy(b *testing.B) {
const rows = 10000
const cols = 5
data := sampleData(rows, cols)
bucket := newBucket(data)
b.Run("getBy", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = bucket.getBy(map[int]string{1: "C1-R5000", 3: "C3-R5000"})
}
})
b.Run("FilterAfterGetAll", func(b *testing.B) {
for i := 0; i < b.N; i++ {
all := bucket.getBy(make(map[int]string))
var filtered [][]string
for _, r := range all {
if r[1] == "C1-R5000" && r[3] == "C3-R5000" {
filtered = append(filtered, r)
}
}
_ = filtered
}
})
}
// BenchmarkremoveBy measures the cost of a multi-clause removeBy
func BenchmarkRemoveBy(b *testing.B) {
const rows = 5000
const cols = 4
data := sampleData(rows, cols)
bucket := newBucket(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// re-seed for each iteration
bucket = newBucket(data)
bucket.removeBy(map[int]string{2: "C2-R2500", 0: "C0-R2500"})
}
}