1
1
package bitvec
2
2
3
3
import (
4
- // "fmt"
5
4
"math/rand"
6
5
"testing"
7
6
)
@@ -12,106 +11,132 @@ var BadSets = []uint64{100, 101, 200, 1000}
12
11
13
12
func TestBV (t * testing.T ) {
14
13
bv := New (100 )
15
- for _ , k := range GoodSets {
16
- if ! bv .TrySet (k ) {
17
- t .Errorf ("Error: could not set good index %d" , k )
14
+ abv := NewAtomic (100 )
15
+ t .Run ("valid indices" , func (t * testing.T ) {
16
+ for _ , k := range GoodSets {
17
+ if ! bv .TrySet (k ) {
18
+ t .Errorf ("bv could not set good index %d" , k )
19
+ }
20
+ if ! abv .TrySet (k ) {
21
+ t .Errorf ("abv could not set good index %d" , k )
22
+ }
23
+
24
+ if x , _ := bv .Get (k ); ! x {
25
+ t .Errorf ("bv bit %d was supposed to be set but wasn't" , k )
26
+ }
27
+ if x , _ := abv .Get (k ); ! x {
28
+ t .Errorf ("abv bit %d was supposed to be set but wasn't" , k )
29
+ }
18
30
}
19
- }
20
- for _ , k := range BadSets {
21
- if bv .TrySet (k ) {
22
- t .Errorf ("Error: successfully set bad index %d " , k )
31
+ })
32
+ t .Run ("invalid indices" , func (t * testing.T ) {
33
+ for _ , k := range BadSets {
34
+ if bv .TrySet (k ) {
35
+ t .Errorf ("bv successfully set bad index %d " , k )
36
+ }
37
+ if abv .TrySet (k ) {
38
+ t .Errorf ("abv successfully set bad index %d " , k )
39
+ }
23
40
}
24
- }
25
- for _ , k := range GoodSets {
26
- if x , _ := bv .Get (k ); ! x {
27
- t .Errorf ("Error: bit %d was supposed to be set but wasn't" , k )
41
+ if _ , err := bv .Get (101 ); err == nil {
42
+ t .Errorf ("bv error should be thrown for out of bounds access" )
28
43
}
29
- }
30
- for _ , k := range UnSets {
31
- if x , _ := bv .Get (k ); x {
32
- t .Errorf ("Error: bit %d was not supposed to be set but was" , k )
44
+ if _ , err := abv .Get (101 ); err == nil {
45
+ t .Errorf ("abv error should be thrown for out of bounds access" )
33
46
}
34
- }
35
-
36
- if _ , err := bv .Get (101 ); err == nil {
37
- t .Errorf ("Error should be thrown for out of bounds access" )
38
- }
39
- }
40
47
41
- func TestAtomicBV (t * testing.T ) {
42
- bv := NewAtomic (100 )
43
- for _ , k := range GoodSets {
44
- if ! bv .TrySet (k ) {
45
- t .Errorf ("Error: could not set good index %d" , k )
48
+ })
49
+ t .Run ("unset indices" , func (t * testing.T ) {
50
+ for _ , k := range UnSets {
51
+ if x , _ := bv .Get (k ); x {
52
+ t .Errorf ("bv bit %d was not supposed to be set but was" , k )
53
+ }
54
+ if x , _ := abv .Get (k ); x {
55
+ t .Errorf ("abv bit %d was not supposed to be set but was" , k )
56
+ }
46
57
}
47
- }
48
- for _ , k := range BadSets {
49
- if bv .TrySet (k ) {
50
- t .Errorf ("Error: successfully set bad index %d " , k )
51
- }
52
- }
58
+ })
53
59
54
- for _ , k := range GoodSets {
55
- if x , _ := bv .Get (k ); ! x {
56
- t .Errorf ("Error: bit %d was supposed to be set but wasn't" , k )
57
- }
58
- }
59
- for _ , k := range UnSets {
60
- if x , _ := bv .Get (k ); x {
61
- t .Errorf ("Error: bit %d was not supposed to be set but was" , k )
62
- }
63
- }
64
- if _ , err := bv .Get (101 ); err == nil {
65
- t .Errorf ("Error should be thrown for out of bounds access" )
66
- }
67
60
}
68
61
69
- var r = rand .New (rand .NewSource (99 ))
70
- var sets = r .Perm (10_000_000 )
71
-
72
- func BenchmarkBVSet (b * testing.B ) {
73
- bv := New (uint64 (b .N / 64 ))
74
- b .ResetTimer ()
75
- for n := 1 ; n < b .N ; n ++ {
76
- bv .TrySet (uint64 (sets [n % len (sets )] % n ))
77
- }
78
- }
62
+ // func TestAtomicBV(t *testing.T) {
63
+ // for _, k := range GoodSets {
64
+ // if !bv.TrySet(k) {
65
+ // t.Errorf("Error: could not set good index %d", k)
66
+ // }
67
+ // }
68
+ // for _, k := range BadSets {
69
+ // if bv.TrySet(k) {
70
+ // t.Errorf("Error: successfully set bad index %d ", k)
71
+ // }
72
+ // }
79
73
80
- func BenchmarkBVAtomicSet (b * testing.B ) {
81
- bv := NewAtomic (uint64 (b .N / 64 ))
82
- b .ResetTimer ()
83
- for n := 1 ; n < b .N ; n ++ {
84
- bv .TrySet (uint64 (sets [n % len (sets )] % n ))
85
- }
86
- }
87
- func BenchmarkSliceSet (b * testing.B ) {
88
- bv := make ([]bool , b .N )
89
- b .ResetTimer ()
90
- for n := 1 ; n < b .N ; n ++ {
91
- bv [sets [n % len (sets )]% n ] = true
92
- }
74
+ // for _, k := range UnSets {
75
+ // if x, _ := bv.Get(k); x {
76
+ // t.Errorf("Error: bit %d was not supposed to be set but was", k)
77
+ // }
78
+ // }
79
+ // if _, err := bv.Get(101); err == nil {
80
+ // t.Errorf("Error should be thrown for out of bounds access")
81
+ // }
82
+ // }
93
83
94
- }
84
+ var r = rand .New (rand .NewSource (99 ))
85
+ var sets = r .Perm (10_000_000 )
95
86
96
- func BenchmarkBVGet (b * testing.B ) {
97
- bv := New (uint64 (b .N / 64 ) + 1 )
98
- b .ResetTimer ()
99
- for n := 1 ; n < b .N ; n ++ {
100
- bv .Get (uint64 (sets [n % len (sets )] % n ))
101
- }
102
- }
103
- func BenchmarkBVAtomicGet (b * testing.B ) {
104
- bv := NewAtomic (uint64 (b .N / 64 ))
105
- b .ResetTimer ()
106
- for n := 1 ; n < b .N ; n ++ {
107
- bv .Get (uint64 (sets [n % len (sets )% n ]))
108
- }
87
+ func BenchmarkSet (b * testing.B ) {
88
+ b .Run ("bitvec/Set" , func (b * testing.B ) {
89
+ bv := New (uint64 (b .N / 64 ))
90
+ b .ResetTimer ()
91
+ for n := 1 ; n < b .N ; n ++ {
92
+ bv .TrySet (uint64 (sets [n % len (sets )] % n ))
93
+ }
94
+ })
95
+ b .Run ("abitvec/Set" , func (b * testing.B ) {
96
+ abv := NewAtomic (uint64 (b .N / 64 ))
97
+ b .ResetTimer ()
98
+ for n := 1 ; n < b .N ; n ++ {
99
+ abv .TrySet (uint64 (sets [n % len (sets )] % n ))
100
+ }
101
+ })
102
+ b .Run ("slice/Set" , func (b * testing.B ) {
103
+ slice := make ([]bool , b .N )
104
+ b .ResetTimer ()
105
+ for n := 1 ; n < b .N ; n ++ {
106
+ slice [sets [n % len (sets )]% n ] = true
107
+ }
108
+ })
109
109
}
110
- func BenchmarkSliceGet (b * testing.B ) {
111
- bv := make ([]bool , b .N )
112
- b .ResetTimer ()
113
- for n := 1 ; n < b .N ; n ++ {
114
- _ = bv [sets [n % len (sets )]% n ]
115
- }
116
110
111
+ func BenchmarkGet (b * testing.B ) {
112
+ b .Run ("bitvec/Get" , func (b * testing.B ) {
113
+ bv := New (uint64 (b .N / 64 ))
114
+ for n := 0 ; n < b .N && sets [n ]% 2 == 0 ; n ++ {
115
+ bv .TrySet (uint64 (n ))
116
+ }
117
+ b .ResetTimer ()
118
+ for n := 1 ; n < b .N ; n ++ {
119
+ bv .Get (uint64 (sets [n % len (sets )] % n ))
120
+ }
121
+ })
122
+ b .Run ("abitvec/Get" , func (b * testing.B ) {
123
+ abv := NewAtomic (uint64 (b .N / 64 ))
124
+ for n := 0 ; n < b .N && sets [n ]% 2 == 0 ; n ++ {
125
+ abv .TrySet (uint64 (n ))
126
+ }
127
+ b .ResetTimer ()
128
+ for n := 1 ; n < b .N ; n ++ {
129
+ abv .Get (uint64 (sets [n % len (sets )] % n ))
130
+ }
131
+ })
132
+ b .Run ("slice/Get" , func (b * testing.B ) {
133
+ slice := make ([]bool , b .N )
134
+ for n := 0 ; n < b .N && sets [n ]% 2 == 0 ; n ++ {
135
+ slice [n ] = true
136
+ }
137
+ b .ResetTimer ()
138
+ for n := 1 ; n < b .N ; n ++ {
139
+ _ = slice [sets [n % len (sets )]% n ]
140
+ }
141
+ })
117
142
}
0 commit comments