|
| 1 | +package table |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "reflect" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// Utility to sort slice-of-rows for comparison. |
| 12 | +func sortRows(rows [][]string) { |
| 13 | + sort.Slice(rows, func(i, j int) bool { |
| 14 | + return strings.Join(rows[i], "|") < strings.Join(rows[j], "|") |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +// Merges two sorted slices of rows (with possible duplicates). |
| 19 | +func unionRows(a, b [][]string) [][]string { |
| 20 | + out := append(append([][]string(nil), a...), b...) |
| 21 | + sortRows(out) |
| 22 | + return out |
| 23 | +} |
| 24 | + |
| 25 | +func TestFuzzUnion(t *testing.T) { |
| 26 | + const ( |
| 27 | + maxCols = 5 |
| 28 | + totalIters = 5000 |
| 29 | + lookupIters = 50 |
| 30 | + maxValLen = 8 |
| 31 | + seed = 42 |
| 32 | + ) |
| 33 | + |
| 34 | + r := rand.New(rand.NewSource(seed)) |
| 35 | + A := &Table{} |
| 36 | + B := &Table{} |
| 37 | + var C *Table |
| 38 | + |
| 39 | + for iter := 0; iter < totalIters; iter++ { |
| 40 | + // Randomly choose insert vs delete (80% inserts) |
| 41 | + if r.Float64() < 0.8 { |
| 42 | + row := randomRow(r, maxCols, maxValLen) |
| 43 | + if r.Float64() < 0.5 { |
| 44 | + A.Insert([][]string{row}) |
| 45 | + } else { |
| 46 | + B.Insert([][]string{row}) |
| 47 | + } |
| 48 | + } else { |
| 49 | + // Random delete: pick non-empty table A or B |
| 50 | + var T *Table |
| 51 | + if r.Float64() < 0.5 { |
| 52 | + T = A |
| 53 | + } else { |
| 54 | + T = B |
| 55 | + } |
| 56 | + // Pick a random existing row and delete by one of its columns |
| 57 | + all := T.All() |
| 58 | + if len(all) > 0 { |
| 59 | + row := all[r.Intn(len(all))] |
| 60 | + col := r.Intn(len(row)) |
| 61 | + val := row[col] |
| 62 | + T.DeleteBy(map[int]string{col: val}) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Rebuild C = A ∪ B |
| 67 | + C = &Table{} |
| 68 | + C.Insert(A.All()) |
| 69 | + C.Insert(B.All()) |
| 70 | + C.Compact() |
| 71 | + |
| 72 | + // Now test random single-column lookups |
| 73 | + for li := 0; li < lookupIters; li++ { |
| 74 | + col := r.Intn(maxCols) |
| 75 | + // choose a test value: either random or drawn from C |
| 76 | + var val string |
| 77 | + if r.Float64() < 0.5 { |
| 78 | + val = randString(r, 3) |
| 79 | + } else { |
| 80 | + h := C.All() |
| 81 | + if len(h) > 0 { |
| 82 | + row := h[r.Intn(len(h))] |
| 83 | + if col < len(row) { |
| 84 | + val = row[col] |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + aRows := A.GetAll(col, val) |
| 90 | + bRows := B.GetAll(col, val) |
| 91 | + exp := unionRows(aRows, bRows) |
| 92 | + |
| 93 | + cRows := C.GetAll(col, val) |
| 94 | + sortRows(cRows) |
| 95 | + |
| 96 | + if !equalRows(cRows, exp) { |
| 97 | + t.Fatalf("Invariant broken at iter %d lookup %d:\n"+ |
| 98 | + "col=%d val=%q\nA rows = %v\nB rows = %v\n"+ |
| 99 | + "C rows = %v\nexpected union = %v", |
| 100 | + iter, li, col, val, aRows, bRows, cRows, exp) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Occasional compaction to reclaim holes |
| 105 | + if iter%1000 == 0 { |
| 106 | + A.Compact() |
| 107 | + B.Compact() |
| 108 | + C.Compact() |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// equalRows assumes both are sorted by sortRows(). |
| 114 | +func equalRows(a, b [][]string) bool { |
| 115 | + if len(a) != len(b) { |
| 116 | + return false |
| 117 | + } |
| 118 | + for i := range a { |
| 119 | + if !reflect.DeepEqual(a[i], b[i]) { |
| 120 | + return false |
| 121 | + } |
| 122 | + } |
| 123 | + return true |
| 124 | +} |
| 125 | + |
| 126 | +func randomRow(rnd *rand.Rand, maxCols, maxValLen int) []string { |
| 127 | + n := rnd.Intn(maxCols) + 1 |
| 128 | + row := make([]string, n) |
| 129 | + for i := range row { |
| 130 | + if rnd.Float64() < 0.05 { |
| 131 | + continue // randomly keep some cols empty |
| 132 | + } |
| 133 | + row[i] = randString(rnd, maxValLen) |
| 134 | + } |
| 135 | + return row |
| 136 | +} |
| 137 | + |
| 138 | +func randString(rnd *rand.Rand, length int) string { |
| 139 | + chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 140 | + s := make([]byte, length) |
| 141 | + for i := range s { |
| 142 | + s[i] = chars[rnd.Intn(len(chars))] |
| 143 | + } |
| 144 | + return string(s) |
| 145 | +} |
0 commit comments