@@ -2222,6 +2222,36 @@ func TestDeleteWithBitSetInstance(t *testing.T) {
22222222 }
22232223}
22242224
2225+ // DeleteAt reduces the length of the bitset, and it must shrink the
2226+ // underlying slice accordingly, otherwise operations that scan the whole
2227+ // slice see words beyond the length of the bitset. See issue #225.
2228+ func TestDeleteAtKeepsWordCount (t * testing.T ) {
2229+ for _ , length := range []uint {1 , 64 , 65 , 128 , 129 , 192 , 256 , 257 } {
2230+ b := New (length - 1 )
2231+ b .Set (length - 1 ) // b has 'length' bits, the last one set
2232+ b .DeleteAt (0 )
2233+
2234+ if b .Len () != length - 1 {
2235+ t .Fatalf ("length %d: expected a length of %d, got %d" , length , length - 1 , b .Len ())
2236+ }
2237+ if len (b .set ) != wordsNeeded (b .Len ()) {
2238+ t .Errorf ("length %d: expected %d words, got %d" , length , wordsNeeded (b .Len ()), len (b .set ))
2239+ }
2240+
2241+ b .SetAll ()
2242+ if ! b .All () {
2243+ t .Errorf ("length %d: All should be true after SetAll, %d bits set out of %d" , length , b .Count (), b .Len ())
2244+ }
2245+
2246+ // a word dropped by DeleteAt must not come back with stale bits
2247+ // when the bitset grows back to its former size
2248+ b .Set (length - 1 )
2249+ if b .Count () != length {
2250+ t .Errorf ("length %d: expected %d bits set, got %d" , length , length , b .Count ())
2251+ }
2252+ }
2253+ }
2254+
22252255func TestWriteTo (t * testing.T ) {
22262256 const length = 9585
22272257 const oneEvery = 97
0 commit comments