Debian Codegraph @

exp/visitor_uint32_test.go

raw
1 package sieve
2
3 import (
4 "math/rand"
5 "sync"
6 "sync/atomic"
7 "testing"
8 )
9
10 // --- visitedBool (k=1) tests ---
11
12 func TestVisitedBool_SetClearTest(t *testing.T) {
13 v := newVisitedBool(256)
14
15 for i := int32(0); i < 256; i++ {
16 if v.IsVisited(i) {
17 t.Fatalf("slot %d should be clear initially", i)
18 }
19 }
20
21 for i := int32(0); i < 256; i += 2 {
22 v.Mark(i)
23 }
24
25 for i := int32(0); i < 256; i++ {
26 expected := i%2 == 0
27 if v.IsVisited(i) != expected {
28 t.Fatalf("slot %d: expected %v, got %v", i, expected, v.IsVisited(i))
29 }
30 }
31
32 for i := int32(0); i < 256; i += 2 {
33 v.Clear(i)
34 }
35
36 for i := int32(0); i < 256; i++ {
37 if v.IsVisited(i) {
38 t.Fatalf("slot %d should be clear after Clear", i)
39 }
40 }
41
42 for i := int32(0); i < 256; i++ {
43 v.Mark(i)
44 }
45 v.ResetAll()
46 for i := int32(0); i < 256; i++ {
47 if v.IsVisited(i) {
48 t.Fatalf("slot %d should be clear after ResetAll", i)
49 }
50 }
51 }
52
53 func TestVisitedBool_MarkIdempotent(t *testing.T) {
54 v := newVisitedBool(128)
55
56 for i := 0; i < 100; i++ {
57 v.Mark(42)
58 }
59 if !v.IsVisited(42) {
60 t.Fatal("slot 42 should be set")
61 }
62
63 for i := 0; i < 100; i++ {
64 v.Clear(42)
65 }
66 if v.IsVisited(42) {
67 t.Fatal("slot 42 should be clear")
68 }
69 }
70
71 func TestVisitedBool_Concurrent(t *testing.T) {
72 const (
73 capacity = 1024
74 goroutines = 64
75 opsPerG = 10000
76 )
77
78 v := newVisitedBool(capacity)
79
80 var wg sync.WaitGroup
81 wg.Add(goroutines)
82 for g := 0; g < goroutines; g++ {
83 go func(seed int64) {
84 defer wg.Done()
85 r := rand.New(rand.NewSource(seed))
86 for i := 0; i < opsPerG; i++ {
87 idx := int32(r.Intn(capacity))
88 switch r.Intn(3) {
89 case 0:
90 v.Mark(idx)
91 case 1:
92 v.Clear(idx)
93 case 2:
94 v.IsVisited(idx)
95 }
96 }
97 }(int64(g))
98 }
99 wg.Wait()
100
101 v.ResetAll()
102 for i := int32(0); i < capacity; i++ {
103 if v.IsVisited(i) {
104 t.Fatalf("slot %d should be clear after ResetAll", i)
105 }
106 }
107 }
108
109 // --- visitedCounter (k>1) tests ---
110
111 func TestVisitedCounter_SaturatingK3(t *testing.T) {
112 v := newVisitedCounter(64, 3)
113
114 // Mark 5 times — should saturate at 3
115 for i := 0; i < 5; i++ {
116 v.Mark(0)
117 }
118 if atomic.LoadUint32(&v.slots[0]) != 3 {
119 t.Fatalf("expected 3, got %d", atomic.LoadUint32(&v.slots[0]))
120 }
121 if !v.IsVisited(0) {
122 t.Fatal("should be visited")
123 }
124
125 // Clear 3 times — should reach 0
126 for i := 0; i < 3; i++ {
127 v.Clear(0)
128 if i < 2 && !v.IsVisited(0) {
129 t.Fatalf("should still be visited after %d clears", i+1)
130 }
131 }
132 if v.IsVisited(0) {
133 t.Fatal("should not be visited after 3 clears")
134 }
135
136 // Clear again — idempotent at 0
137 v.Clear(0)
138 if atomic.LoadUint32(&v.slots[0]) != 0 {
139 t.Fatalf("expected 0, got %d", atomic.LoadUint32(&v.slots[0]))
140 }
141 }
142
143 func TestVisitedCounter_SetClear(t *testing.T) {
144 v := newVisitedCounter(256, 3)
145
146 for i := int32(0); i < 256; i++ {
147 if v.IsVisited(i) {
148 t.Fatalf("slot %d should be clear initially", i)
149 }
150 }
151
152 // Mark each slot once
153 for i := int32(0); i < 256; i++ {
154 v.Mark(i)
155 }
156 for i := int32(0); i < 256; i++ {
157 if !v.IsVisited(i) {
158 t.Fatalf("slot %d should be visited after Mark", i)
159 }
160 }
161
162 // Clear each slot once (counter goes from 1 to 0)
163 for i := int32(0); i < 256; i++ {
164 v.Clear(i)
165 }
166 for i := int32(0); i < 256; i++ {
167 if v.IsVisited(i) {
168 t.Fatalf("slot %d should be clear after Clear", i)
169 }
170 }
171 }
172
173 func TestVisitedCounter_Concurrent(t *testing.T) {
174 const (
175 capacity = 1024
176 goroutines = 64
177 opsPerG = 10000
178 )
179
180 v := newVisitedCounter(capacity, 3)
181
182 var wg sync.WaitGroup
183 wg.Add(goroutines)
184 for g := 0; g < goroutines; g++ {
185 go func(seed int64) {
186 defer wg.Done()
187 r := rand.New(rand.NewSource(seed))
188 for i := 0; i < opsPerG; i++ {
189 idx := int32(r.Intn(capacity))
190 switch r.Intn(3) {
191 case 0:
192 v.Mark(idx)
193 case 1:
194 v.Clear(idx)
195 case 2:
196 v.IsVisited(idx)
197 }
198 }
199 }(int64(g))
200 }
201 wg.Wait()
202
203 for i := int32(0); i < capacity; i++ {
204 if atomic.LoadUint32(&v.slots[i]) > 3 {
205 t.Fatalf("slot %d overflowed: %d", i, atomic.LoadUint32(&v.slots[i]))
206 }
207 }
208 }
209
210 // --- Benchmarks ---
211
212 func BenchmarkVisitedBool_Mark(b *testing.B) {
213 v := newVisitedBool(1 << 20)
214 b.ResetTimer()
215 for i := 0; i < b.N; i++ {
216 v.Mark(int32(i % (1 << 20)))
217 }
218 }
219
220 func BenchmarkVisitedBool_Mark_Contended(b *testing.B) {
221 v := newVisitedBool(64)
222 b.RunParallel(func(pb *testing.PB) {
223 r := rand.New(rand.NewSource(rand.Int63()))
224 for pb.Next() {
225 v.Mark(int32(r.Intn(64)))
226 }
227 })
228 }
229
230 func BenchmarkVisitedBool_IsVisited(b *testing.B) {
231 v := newVisitedBool(1 << 20)
232 for i := int32(0); i < 1<<20; i += 2 {
233 v.Mark(i)
234 }
235 b.ResetTimer()
236 for i := 0; i < b.N; i++ {
237 v.IsVisited(int32(i % (1 << 20)))
238 }
239 }
240
241 func BenchmarkVisitedBool_Clear(b *testing.B) {
242 v := newVisitedBool(1 << 20)
243 for i := int32(0); i < 1<<20; i++ {
244 v.Mark(i)
245 }
246 b.ResetTimer()
247 for i := 0; i < b.N; i++ {
248 v.Clear(int32(i % (1 << 20)))
249 }
250 }
251
252 func BenchmarkVisitedCounter_Mark(b *testing.B) {
253 v := newVisitedCounter(1<<20, 3)
254 b.ResetTimer()
255 for i := 0; i < b.N; i++ {
256 v.Mark(int32(i % (1 << 20)))
257 }
258 }
259
260 func BenchmarkVisitedCounter_Mark_Contended(b *testing.B) {
261 v := newVisitedCounter(64, 3)
262 b.RunParallel(func(pb *testing.PB) {
263 r := rand.New(rand.NewSource(rand.Int63()))
264 for pb.Next() {
265 v.Mark(int32(r.Intn(64)))
266 }
267 })
268 }