github.com/richardwilkes/toolbox@v1.121.0/xmath/bitset_test.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package xmath 11 12 import ( 13 "testing" 14 15 "github.com/richardwilkes/toolbox/check" 16 ) 17 18 func TestBitSet(t *testing.T) { 19 var bs BitSet 20 check.Equal(t, 0, bs.Count()) 21 bs.Set(0) 22 check.Equal(t, 1, bs.Count()) 23 bs.Set(7) 24 check.Equal(t, 2, bs.Count()) 25 bs.Set(dataBitsPerWord - 1) 26 check.Equal(t, 3, bs.Count()) 27 bs.Set(dataBitsPerWord) 28 check.Equal(t, 4, bs.Count()) 29 bs.Set(dataBitsPerWord + 1) 30 check.Equal(t, 5, bs.Count()) 31 bs.Set(0) 32 check.Equal(t, 5, bs.Count()) 33 bs.Clear(0) 34 check.Equal(t, 4, bs.Count()) 35 bs.Clear(1) 36 check.Equal(t, 4, bs.Count()) 37 bs.Clear(1000) 38 check.Equal(t, 4, bs.Count()) 39 check.False(t, bs.State(0)) 40 check.False(t, bs.State(1)) 41 check.True(t, bs.State(7)) 42 check.False(t, bs.State(77)) 43 check.True(t, bs.State(dataBitsPerWord)) 44 bs.Flip(22) 45 check.True(t, bs.State(22)) 46 bs.Flip(22) 47 check.False(t, bs.State(22)) 48 check.Equal(t, 7, bs.NextSet(0)) 49 check.Equal(t, 7, bs.NextSet(7)) 50 check.Equal(t, dataBitsPerWord-1, bs.NextSet(8)) 51 check.Equal(t, dataBitsPerWord, bs.NextSet(dataBitsPerWord)) 52 bs.Set(1234) 53 check.Equal(t, 1234, bs.NextSet(dataBitsPerWord+2)) 54 check.Equal(t, 0, bs.NextClear(0)) 55 check.Equal(t, dataBitsPerWord+2, bs.NextClear(dataBitsPerWord-1)) 56 check.Equal(t, 1235, bs.NextClear(1234)) 57 bs.Set(dataBitsPerWord*100 - 1) 58 check.Equal(t, dataBitsPerWord*100, bs.NextClear(dataBitsPerWord*100-1)) 59 check.Equal(t, dataBitsPerWord*100-1, bs.PreviousSet(dataBitsPerWord*100)) 60 check.Equal(t, 1234, bs.PreviousSet(dataBitsPerWord*100-2)) 61 check.Equal(t, -1, bs.PreviousSet(0)) 62 check.Equal(t, dataBitsPerWord*1000, bs.PreviousClear(dataBitsPerWord*1000)) 63 check.Equal(t, dataBitsPerWord*100-2, bs.PreviousClear(dataBitsPerWord*100-1)) 64 check.Equal(t, 0, bs.PreviousClear(0)) 65 bs.Set(0) 66 check.Equal(t, -1, bs.PreviousClear(0)) 67 68 bs.Reset() 69 bs.Set(65) 70 bs.SetRange(10, 300) 71 check.Equal(t, 291, bs.Count()) 72 for i := 10; i < 301; i++ { 73 check.True(t, bs.State(i)) 74 } 75 check.Equal(t, 301, bs.NextClear(10)) 76 check.Equal(t, 9, bs.PreviousClear(300)) 77 check.Equal(t, 10, bs.NextSet(0)) 78 check.Equal(t, 300, bs.PreviousSet(1000)) 79 bs.ClearRange(15, 295) 80 check.Equal(t, 10, bs.Count()) 81 for i := 15; i < 296; i++ { 82 check.False(t, bs.State(i)) 83 } 84 bs.FlipRange(10, 300) 85 check.Equal(t, 281, bs.Count()) 86 }