@@ -121,6 +121,8 @@ func (s *HashSet) check(t *testing.T) {
121
121
if float64 (collisions ) > expected + SLOP * (3 * stddev + 1 ) {
122
122
t .Errorf ("unexpected number of collisions: got=%d mean=%f stddev=%f threshold=%f" , collisions , expected , stddev , expected + SLOP * (3 * stddev + 1 ))
123
123
}
124
+ // Reset for reuse
125
+ s .list = s .list [:0 ]
124
126
}
125
127
126
128
// a string plus adding zeros must make distinct hashes
@@ -230,8 +232,8 @@ func TestSmhasherCyclic(t *testing.T) {
230
232
r := rand .New (rand .NewSource (1234 ))
231
233
const REPEAT = 8
232
234
const N = 1000000
235
+ h := newHashSet ()
233
236
for n := 4 ; n <= 12 ; n ++ {
234
- h := newHashSet ()
235
237
b := make ([]byte , REPEAT * n )
236
238
for i := 0 ; i < N ; i ++ {
237
239
b [0 ] = byte (i * 79 % 97 )
@@ -256,18 +258,18 @@ func TestSmhasherSparse(t *testing.T) {
256
258
if testing .Short () {
257
259
t .Skip ("Skipping in short mode" )
258
260
}
259
- sparse (t , 32 , 6 )
260
- sparse (t , 40 , 6 )
261
- sparse (t , 48 , 5 )
262
- sparse (t , 56 , 5 )
263
- sparse (t , 64 , 5 )
264
- sparse (t , 96 , 4 )
265
- sparse (t , 256 , 3 )
266
- sparse (t , 2048 , 2 )
267
- }
268
- func sparse (t * testing.T , n int , k int ) {
269
- b := make ([]byte , n / 8 )
270
261
h := newHashSet ()
262
+ sparse (t , h , 32 , 6 )
263
+ sparse (t , h , 40 , 6 )
264
+ sparse (t , h , 48 , 5 )
265
+ sparse (t , h , 56 , 5 )
266
+ sparse (t , h , 64 , 5 )
267
+ sparse (t , h , 96 , 4 )
268
+ sparse (t , h , 256 , 3 )
269
+ sparse (t , h , 2048 , 2 )
270
+ }
271
+ func sparse (t * testing.T , h * HashSet , n int , k int ) {
272
+ b := make ([]byte , n / 8 )
271
273
setbits (h , b , 0 , k )
272
274
h .check (t )
273
275
}
@@ -297,15 +299,15 @@ func TestSmhasherPermutation(t *testing.T) {
297
299
if race .Enabled {
298
300
t .Skip ("Too long for race mode" )
299
301
}
300
- permutation (t , []uint32 {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 }, 8 )
301
- permutation (t , []uint32 {0 , 1 << 29 , 2 << 29 , 3 << 29 , 4 << 29 , 5 << 29 , 6 << 29 , 7 << 29 }, 8 )
302
- permutation (t , []uint32 {0 , 1 }, 20 )
303
- permutation (t , []uint32 {0 , 1 << 31 }, 20 )
304
- permutation (t , []uint32 {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 1 << 29 , 2 << 29 , 3 << 29 , 4 << 29 , 5 << 29 , 6 << 29 , 7 << 29 }, 6 )
302
+ h := newHashSet ()
303
+ permutation (t , h , []uint32 {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 }, 8 )
304
+ permutation (t , h , []uint32 {0 , 1 << 29 , 2 << 29 , 3 << 29 , 4 << 29 , 5 << 29 , 6 << 29 , 7 << 29 }, 8 )
305
+ permutation (t , h , []uint32 {0 , 1 }, 20 )
306
+ permutation (t , h , []uint32 {0 , 1 << 31 }, 20 )
307
+ permutation (t , h , []uint32 {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 1 << 29 , 2 << 29 , 3 << 29 , 4 << 29 , 5 << 29 , 6 << 29 , 7 << 29 }, 6 )
305
308
}
306
- func permutation (t * testing.T , s []uint32 , n int ) {
309
+ func permutation (t * testing.T , h * HashSet , s []uint32 , n int ) {
307
310
b := make ([]byte , n * 4 )
308
- h := newHashSet ()
309
311
genPerm (h , b , s , 0 )
310
312
h .check (t )
311
313
}
@@ -542,14 +544,15 @@ func TestSmhasherWindowed(t *testing.T) {
542
544
if race .Enabled {
543
545
t .Skip ("Too long for race mode" )
544
546
}
547
+ h := newHashSet ()
545
548
t .Logf ("32 bit keys" )
546
- windowed (t , & Int32Key {})
549
+ windowed (t , h , & Int32Key {})
547
550
t .Logf ("64 bit keys" )
548
- windowed (t , & Int64Key {})
551
+ windowed (t , h , & Int64Key {})
549
552
t .Logf ("string keys" )
550
- windowed (t , & BytesKey {make ([]byte , 128 )})
553
+ windowed (t , h , & BytesKey {make ([]byte , 128 )})
551
554
}
552
- func windowed (t * testing.T , k Key ) {
555
+ func windowed (t * testing.T , h * HashSet , k Key ) {
553
556
if GOARCH == "wasm" {
554
557
t .Skip ("Too slow on wasm" )
555
558
}
@@ -566,7 +569,6 @@ func windowed(t *testing.T, k Key) {
566
569
const BITS = 16
567
570
568
571
for r := 0 ; r < k .bits (); r ++ {
569
- h := newHashSet ()
570
572
for i := 0 ; i < 1 << BITS ; i ++ {
571
573
k .clear ()
572
574
for j := 0 ; j < BITS ; j ++ {
@@ -585,18 +587,18 @@ func TestSmhasherText(t *testing.T) {
585
587
if testing .Short () {
586
588
t .Skip ("Skipping in short mode" )
587
589
}
588
- text (t , "Foo" , "Bar" )
589
- text (t , "FooBar" , "" )
590
- text (t , "" , "FooBar" )
590
+ h := newHashSet ()
591
+ text (t , h , "Foo" , "Bar" )
592
+ text (t , h , "FooBar" , "" )
593
+ text (t , h , "" , "FooBar" )
591
594
}
592
- func text (t * testing.T , prefix , suffix string ) {
595
+ func text (t * testing.T , h * HashSet , prefix , suffix string ) {
593
596
const N = 4
594
597
const S = "ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrst0123456789"
595
598
const L = len (S )
596
599
b := make ([]byte , len (prefix )+ N + len (suffix ))
597
600
copy (b , prefix )
598
601
copy (b [len (prefix )+ N :], suffix )
599
- h := newHashSet ()
600
602
c := b [len (prefix ):]
601
603
for i := 0 ; i < L ; i ++ {
602
604
c [0 ] = S [i ]
0 commit comments