-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator_fuzz_test.go
More file actions
106 lines (88 loc) · 2.53 KB
/
Copy pathiterator_fuzz_test.go
File metadata and controls
106 lines (88 loc) · 2.53 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
package k2tree
import (
"math/rand"
"sort"
"testing"
)
func FuzzIteratorEdges(f *testing.F) {
f.Fuzz(func(t *testing.T, seed uint64) {
// Seed a reproducible random source from the fuzz input.
rng := rand.New(rand.NewSource(int64(seed)))
maxID := 2048 // node indices in range [0, 2047]
nEdges := 200 + rng.Intn(300) // 200–499 edges per fuzz run
// Build a K2Tree using the simple slice-backed bitarray.
k2, err := newK2Tree(func() bitarray { return &sliceArray{} }, DefaultConfig)
if err != nil {
t.Fatal(err)
}
// Track ground truth: which edges (row, col) were inserted.
// rowToCols[row] is a map of all unique columns for that row.
rowToCols := make(map[int]map[int]bool)
colToRows := make(map[int]map[int]bool)
for i := 0; i < nEdges; i++ {
row := rng.Intn(maxID)
col := rng.Intn(maxID)
k2.Add(row, col)
if rowToCols[row] == nil {
rowToCols[row] = make(map[int]bool)
}
rowToCols[row][col] = true
if colToRows[col] == nil {
colToRows[col] = make(map[int]bool)
}
colToRows[col][row] = true
}
// Verify row iterators: for each row with outgoing edges,
// the iterator must return exactly the expected columns.
rows := collectKeys(rowToCols)
for _, row := range rows {
expectedCols := mapToSortedSlice(rowToCols[row])
it := k2.From(row)
actualCols := it.ExtractAll()
sort.Ints(actualCols)
if !intSlicesEqual(actualCols, expectedCols) {
t.Fatalf("row iterator mismatch for row %d (seed=%d):\n actual = %v\n expected = %v",
row, seed, actualCols, expectedCols)
}
}
// Verify column iterators: for each column with incoming edges,
// the iterator must return exactly the expected rows.
cols := collectKeys(colToRows)
for _, col := range cols {
expectedRows := mapToSortedSlice(colToRows[col])
it := k2.To(col)
actualRows := it.ExtractAll()
sort.Ints(actualRows)
if !intSlicesEqual(actualRows, expectedRows) {
t.Fatalf("col iterator mismatch for col %d (seed=%d):\n actual = %v\n expected = %v",
col, seed, actualRows, expectedRows)
}
}
})
}
func intSlicesEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func collectKeys(m map[int]map[int]bool) []int {
keys := make([]int, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func mapToSortedSlice(m map[int]bool) []int {
slice := make([]int, 0, len(m))
for k := range m {
slice = append(slice, k)
}
sort.Ints(slice)
return slice
}