99)
1010
1111func TestExhaustive24 (t * testing.T ) {
12- bf := NewBitfield (24 )
12+ bf , err := NewBitfield (24 )
13+ assertNoError (t , err )
1314 max := 1 << 24
1415
1516 bint := new (big.Int )
@@ -58,7 +59,8 @@ func TestExhaustive24(t *testing.T) {
5859}
5960
6061func TestBitfield (t * testing.T ) {
61- bf := NewBitfield (128 )
62+ bf , err := NewBitfield (128 )
63+ assertNoError (t , err )
6264 if bf .OnesBefore (20 ) != 0 {
6365 t .Fatal ("expected no bits set" )
6466 }
@@ -91,10 +93,20 @@ func TestBitfield(t *testing.T) {
9193 }
9294}
9395
96+ func TestBadSizeFails (t * testing.T ) {
97+ for _ , size := range [... ]int {- 8 , 2 , 1337 , - 3 } {
98+ _ , err := NewBitfield (size )
99+ if err == nil {
100+ t .Fatalf ("missing error for %d sized bitfield" , size )
101+ }
102+ }
103+ }
104+
94105var benchmarkSize = 512
95106
96107func BenchmarkBitfield (t * testing.B ) {
97- bf := NewBitfield (benchmarkSize )
108+ bf , err := NewBitfield (benchmarkSize )
109+ assertNoError (t , err )
98110 t .ResetTimer ()
99111 for i := 0 ; i < t .N ; i ++ {
100112 if bf .Bit (i % benchmarkSize ) {
@@ -123,13 +135,14 @@ func BenchmarkBitfield(t *testing.B) {
123135 }
124136}
125137
126- func BenchmarkOnes (t * testing.B ) {
127- bf := NewBitfield (benchmarkSize )
128- t .ResetTimer ()
129- for i := 0 ; i < t .N ; i ++ {
138+ func BenchmarkOnes (b * testing.B ) {
139+ bf , err := NewBitfield (benchmarkSize )
140+ assertNoError (b , err )
141+ b .ResetTimer ()
142+ for i := 0 ; i < b .N ; i ++ {
130143 for j := 0 ; j * 4 < benchmarkSize ; j ++ {
131144 if bf .Ones () != j {
132- t .Fatal ("bad" , i )
145+ b .Fatal ("bad" , i )
133146 }
134147 bf .SetBit (j * 4 )
135148 }
@@ -139,14 +152,16 @@ func BenchmarkOnes(t *testing.B) {
139152 }
140153}
141154
142- func BenchmarkBytes (t * testing.B ) {
143- bfa := NewBitfield (211 )
144- bfb := NewBitfield (211 )
145- for j := 0 ; j * 4 < 211 ; j ++ {
155+ func BenchmarkBytes (b * testing.B ) {
156+ bfa , err := NewBitfield (216 )
157+ assertNoError (b , err )
158+ bfb , err := NewBitfield (216 )
159+ assertNoError (b , err )
160+ for j := 0 ; j * 4 < 216 ; j ++ {
146161 bfa .SetBit (j * 4 )
147162 }
148- t .ResetTimer ()
149- for i := 0 ; i < t .N ; i ++ {
163+ b .ResetTimer ()
164+ for i := 0 ; i < b .N ; i ++ {
150165 bfb .SetBytes (bfa .Bytes ())
151166 }
152167}
@@ -180,3 +195,18 @@ func BenchmarkBigInt(t *testing.B) {
180195 }
181196 }
182197}
198+
199+ func FuzzFromBytes (f * testing.F ) {
200+ f .Fuzz (func (_ * testing.T , size int , bytes []byte ) {
201+ if size > 1 << 20 { // We relly on consumers for limit checks, hopefully they understand that a New... factory allocates memory.
202+ return
203+ }
204+ FromBytes (size , bytes )
205+ })
206+ }
207+
208+ func assertNoError (t testing.TB , e error ) {
209+ if e != nil {
210+ t .Fatal (e )
211+ }
212+ }
0 commit comments