| 1 |
// visited_bits_test.go - unit tests and benchmarks for packed visited bitfield |
| 2 |
package sieve |
| 3 |
|
| 4 |
import ( |
| 5 |
"math/rand" |
| 6 |
"sync" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestVisitedBits_SetClearTest(t *testing.T) { |
| 11 |
vb := newAtomicBitfield(256) |
| 12 |
|
| 13 |
// All bits should start clear |
| 14 |
for i := int32(0); i < 256; i++ { |
| 15 |
if vb.IsVisited(i) { |
| 16 |
t.Fatalf("bit %d should be clear initially", i) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
// Set every other bit |
| 21 |
for i := int32(0); i < 256; i += 2 { |
| 22 |
vb.Mark(i) |
| 23 |
} |
| 24 |
|
| 25 |
// Verify pattern |
| 26 |
for i := int32(0); i < 256; i++ { |
| 27 |
expected := i%2 == 0 |
| 28 |
if vb.IsVisited(i) != expected { |
| 29 |
t.Fatalf("bit %d: expected %v, got %v", i, expected, vb.IsVisited(i)) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// Clear the even bits |
| 34 |
for i := int32(0); i < 256; i += 2 { |
| 35 |
vb.Clear(i) |
| 36 |
} |
| 37 |
|
| 38 |
// All should be clear again |
| 39 |
for i := int32(0); i < 256; i++ { |
| 40 |
if vb.IsVisited(i) { |
| 41 |
t.Fatalf("bit %d should be clear after Clear", i) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// Set all, then Reset |
| 46 |
for i := int32(0); i < 256; i++ { |
| 47 |
vb.Mark(i) |
| 48 |
} |
| 49 |
vb.ResetAll() |
| 50 |
for i := int32(0); i < 256; i++ { |
| 51 |
if vb.IsVisited(i) { |
| 52 |
t.Fatalf("bit %d should be clear after Reset", i) |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestVisitedBits_SetIdempotent(t *testing.T) { |
| 58 |
vb := newAtomicBitfield(128) |
| 59 |
|
| 60 |
// Setting the same bit multiple times should be fine |
| 61 |
for i := 0; i < 100; i++ { |
| 62 |
vb.Mark(42) |
| 63 |
} |
| 64 |
if !vb.IsVisited(42) { |
| 65 |
t.Fatal("bit 42 should be set") |
| 66 |
} |
| 67 |
|
| 68 |
// Clearing the same bit multiple times should be fine |
| 69 |
for i := 0; i < 100; i++ { |
| 70 |
vb.Clear(42) |
| 71 |
} |
| 72 |
if vb.IsVisited(42) { |
| 73 |
t.Fatal("bit 42 should be clear") |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func TestVisitedBits_WordBoundaries(t *testing.T) { |
| 78 |
vb := newAtomicBitfield(256) |
| 79 |
|
| 80 |
// Test bits at word boundaries (63, 64, 127, 128) |
| 81 |
boundaries := []int32{0, 1, 62, 63, 64, 65, 126, 127, 128, 129, 255} |
| 82 |
for _, idx := range boundaries { |
| 83 |
vb.Mark(idx) |
| 84 |
if !vb.IsVisited(idx) { |
| 85 |
t.Fatalf("bit %d should be set", idx) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// Verify only boundary bits are set |
| 90 |
for i := int32(0); i < 256; i++ { |
| 91 |
isBoundary := false |
| 92 |
for _, b := range boundaries { |
| 93 |
if i == b { |
| 94 |
isBoundary = true |
| 95 |
break |
| 96 |
} |
| 97 |
} |
| 98 |
if vb.IsVisited(i) != isBoundary { |
| 99 |
t.Fatalf("bit %d: expected %v, got %v", i, isBoundary, vb.IsVisited(i)) |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestVisitedBits_Concurrent(t *testing.T) { |
| 105 |
const ( |
| 106 |
capacity = 1024 |
| 107 |
goroutines = 64 |
| 108 |
opsPerG = 10000 |
| 109 |
) |
| 110 |
|
| 111 |
vb := newAtomicBitfield(capacity) |
| 112 |
|
| 113 |
var wg sync.WaitGroup |
| 114 |
wg.Add(goroutines) |
| 115 |
for g := 0; g < goroutines; g++ { |
| 116 |
go func(seed int64) { |
| 117 |
defer wg.Done() |
| 118 |
r := rand.New(rand.NewSource(seed)) |
| 119 |
for i := 0; i < opsPerG; i++ { |
| 120 |
idx := int32(r.Intn(capacity)) |
| 121 |
switch r.Intn(3) { |
| 122 |
case 0: |
| 123 |
vb.Mark(idx) |
| 124 |
case 1: |
| 125 |
vb.Clear(idx) |
| 126 |
case 2: |
| 127 |
vb.IsVisited(idx) |
| 128 |
} |
| 129 |
} |
| 130 |
}(int64(g)) |
| 131 |
} |
| 132 |
wg.Wait() |
| 133 |
|
| 134 |
// No crash or race detector complaint = pass |
| 135 |
// Verify Reset works after concurrent abuse |
| 136 |
vb.ResetAll() |
| 137 |
for i := int32(0); i < capacity; i++ { |
| 138 |
if vb.IsVisited(i) { |
| 139 |
t.Fatalf("bit %d should be clear after Reset", i) |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func BenchmarkVisitedBits_Set(b *testing.B) { |
| 145 |
vb := newAtomicBitfield(1 << 20) // 1M bits |
| 146 |
b.ResetTimer() |
| 147 |
for i := 0; i < b.N; i++ { |
| 148 |
vb.Mark(int32(i % (1 << 20))) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func BenchmarkVisitedBits_Set_Contended(b *testing.B) { |
| 153 |
vb := newAtomicBitfield(64) // single word — maximum contention |
| 154 |
b.RunParallel(func(pb *testing.PB) { |
| 155 |
r := rand.New(rand.NewSource(rand.Int63())) |
| 156 |
for pb.Next() { |
| 157 |
idx := int32(r.Intn(64)) |
| 158 |
vb.Mark(idx) |
| 159 |
} |
| 160 |
}) |
| 161 |
} |
| 162 |
|
| 163 |
func BenchmarkVisitedBits_Test(b *testing.B) { |
| 164 |
vb := newAtomicBitfield(1 << 20) |
| 165 |
// Set half the bits |
| 166 |
for i := int32(0); i < 1<<20; i += 2 { |
| 167 |
vb.Mark(i) |
| 168 |
} |
| 169 |
b.ResetTimer() |
| 170 |
for i := 0; i < b.N; i++ { |
| 171 |
vb.IsVisited(int32(i % (1 << 20))) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func BenchmarkVisitedBits_Clear(b *testing.B) { |
| 176 |
vb := newAtomicBitfield(1 << 20) |
| 177 |
// Set all bits first |
| 178 |
for i := int32(0); i < 1<<20; i++ { |
| 179 |
vb.Mark(i) |
| 180 |
} |
| 181 |
b.ResetTimer() |
| 182 |
for i := 0; i < b.N; i++ { |
| 183 |
vb.Clear(int32(i % (1 << 20))) |
| 184 |
} |
| 185 |
} |