github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/container/set/bitset_test.go (about) 1 package set 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 ) 8 9 func TestNew(t *testing.T) { 10 bitSet := NewBitSet(64) 11 fmt.Println(bitSet) 12 } 13 14 func TestString(t *testing.T) { 15 bitSet := NewBitSet(256) 16 bitSet.Set(64, 1) 17 fmt.Println(bitSet) 18 } 19 20 func TestSet(t *testing.T) { 21 bitSet := NewBitSet(64) 22 bitSet.Set(0, 1) 23 bitSet.Set(1, 1) 24 bitSet.Set(63, 1) 25 if bitSet.Set(64, 1) != -1 { 26 t.Error("set error") 27 } 28 bitSet = NewBitSet(100) 29 bitSet.Set(81, 1) 30 if bitSet.Set(81, 1) != 1 { 31 t.Error("set error") 32 } 33 fmt.Println(bitSet) 34 } 35 36 func TestSetGet(t *testing.T) { 37 bitSet := NewBitSet(10000) 38 bitSet.Set(1234, 1) 39 if 1 != bitSet.Get(1234) { 40 t.Error("set/get error") 41 } 42 if 0 != bitSet.Get(1235) { 43 t.Error("get error") 44 } 45 old := bitSet.Set(1234, 0) 46 if old != 1 { 47 t.Error("set error") 48 } 49 if 0 != bitSet.Get(1234) { 50 t.Error("set/get error") 51 } 52 } 53 54 func TestBitSet_Count(t *testing.T) { 55 bs := NewBitSet(5000) 56 bs.Set(1, 1) 57 bs.Set(101, 1) 58 bs.Set(200, 1) 59 bs.Set(1059, 1) 60 bs.Set(3594, 1) 61 bs.Set(1059, 0) 62 if bs.Count() != 4 { 63 t.Error("bit set count err") 64 } 65 66 //567 = 0010 0011 0111 67 cn := swar(uint64(567)) //6 68 if cn != 6 { 69 t.Error("SWAR err") 70 } 71 //1234 = 0100 1101 0010 72 cn = swar(uint64(1234)) //5 73 if cn != 5 { 74 t.Error("SWAR err") 75 } 76 //9874 = 0010 0110 1001 0010 77 cn = swar(uint64(9874)) //6 78 if cn != 6 { 79 t.Error("SWAR err") 80 } 81 //1357913 = 0001 0100 1011 1000 0101 1001 82 cn = swar(uint64(1357913)) //10 83 if cn != 10 { 84 t.Error("SWAR err") 85 } 86 } 87 88 func TestBitSet_LeftShift(t *testing.T) { 89 bitSet := NewBitSet(254) 90 bitSet.Set(0, 1) 91 fmt.Println("set 0->1", bitSet) 92 bitSet.LeftShift(1) 93 fmt.Println("leftShift 1", bitSet) 94 bitSet.Set(63, 1) 95 fmt.Println("set 63->1", bitSet) 96 bitSet.Set(64, 1) 97 fmt.Println("set 64->1", bitSet) 98 bitSet.LeftShift(65) 99 fmt.Println("leftShift 65", bitSet) 100 bitSet.LeftShift(65) 101 fmt.Println("leftShift 65", bitSet) 102 } 103 104 func TestBitSet_RightShift(t *testing.T) { 105 bitSet := NewBitSet(254) 106 bitSet.Set(0, 1) 107 fmt.Println("set 0->1", bitSet) 108 bitSet.LeftShift(1) 109 fmt.Println("leftShift 1", bitSet) 110 bitSet.Set(63, 1) 111 fmt.Println("set 63->1", bitSet) 112 bitSet.Set(64, 1) 113 fmt.Println("set 64->1", bitSet) 114 bitSet.LeftShift(65) 115 fmt.Println("leftShift 65", bitSet) 116 bitSet.RightShift(65) 117 fmt.Println("rightShift 65", bitSet) 118 bitSet.RightShift(65) 119 fmt.Println("rightShift 65", bitSet) 120 } 121 122 func TestBitSet_And(t *testing.T) { 123 bitSet := NewBitSet(65) 124 bitSet.Set(0, 1) 125 compare := NewBitSet(64) 126 compare.Set(1, 1) 127 res := bitSet.And(compare) 128 fmt.Println("bitset:", bitSet) 129 fmt.Println("compare:", compare) 130 fmt.Println("bitset & compare:", res) 131 132 println() 133 134 bitSet = NewBitSet(64) 135 bitSet.Set(0, 1) 136 compare = NewBitSet(65) 137 compare.Set(1, 1) 138 res = bitSet.And(compare) 139 fmt.Println("bitset:", bitSet) 140 fmt.Println("compare:", compare) 141 fmt.Println("bitset & compare:", res) 142 143 println() 144 145 bitSet = NewBitSet(64) 146 bitSet.Set(0, 1) 147 bitSet.Set(1, 1) 148 compare = NewBitSet(129) 149 compare.Set(1, 1) 150 compare.Set(64, 1) 151 res = bitSet.And(compare) 152 fmt.Println("bitset:", bitSet) 153 fmt.Println("compare:", compare) 154 fmt.Println("bitset & compare:", res) 155 } 156 157 func TestBitSet_Or(t *testing.T) { 158 bitSet := NewBitSet(65) 159 bitSet.Set(0, 1) 160 compare := NewBitSet(64) 161 compare.Set(1, 1) 162 res := bitSet.Or(compare) 163 fmt.Println("bitset:", bitSet) 164 fmt.Println("compare:", compare) 165 fmt.Println("bitset | compare:", res) 166 167 println() 168 169 bitSet = NewBitSet(129) 170 bitSet.Set(0, 1) 171 compare = NewBitSet(64) 172 compare.Set(1, 1) 173 res = bitSet.Or(compare) 174 fmt.Println("bitset:", bitSet) 175 fmt.Println("compare:", compare) 176 fmt.Println("bitset | compare:", res) 177 178 println() 179 180 bitSet = NewBitSet(65) 181 bitSet.Set(0, 1) 182 bitSet.Set(64, 1) 183 compare = NewBitSet(129) 184 compare.Set(1, 1) 185 res = bitSet.Or(compare) 186 fmt.Println("bitset:", bitSet) 187 fmt.Println("compare:", compare) 188 fmt.Println("bitset | compare:", res) 189 } 190 191 func TestBitSet_Xor(t *testing.T) { 192 bitSet := NewBitSet(65) 193 bitSet.Set(0, 1) 194 compare := NewBitSet(64) 195 compare.Set(1, 1) 196 res := bitSet.Xor(compare) 197 fmt.Println("bitset:", bitSet) 198 fmt.Println("compare:", compare) 199 fmt.Println("bitset ^ compare:", res) 200 201 println() 202 203 bitSet = NewBitSet(129) 204 bitSet.Set(0, 1) 205 bitSet.Set(64, 1) 206 compare = NewBitSet(64) 207 compare.Set(1, 1) 208 res = bitSet.Or(compare) 209 fmt.Println("bitset:", bitSet) 210 fmt.Println("compare:", compare) 211 fmt.Println("bitset ^ compare:", res) 212 } 213 214 func TestBitSet_Diff(t *testing.T) { 215 bitSet := NewBitSet(129) 216 bitSet.Set(1, 1) 217 bitSet.Set(128, 1) 218 compare := NewBitSet(65) 219 compare.Set(1, 1) 220 res := bitSet.Diff(compare) 221 fmt.Println("bitset:", bitSet) 222 fmt.Println("compare:", compare) 223 fmt.Println("bitset ~ compare:", res) 224 225 println() 226 227 bitSet = NewBitSet(64) 228 bitSet.Set(0, 1) 229 bitSet.Set(2, 1) 230 bitSet.Set(3, 1) 231 fmt.Println("bitset:", bitSet) 232 compare = NewBitSet(129) 233 compare.Set(1, 1) 234 compare.Set(2, 1) 235 fmt.Println("compare:", compare) 236 res = bitSet.Diff(compare) 237 fmt.Println("bitset ~ compare:", res) 238 } 239 240 func TestBitSet_Not(t *testing.T) { 241 bitSet := NewBitSet(129) 242 bitSet.Set(1, 1) 243 bitSet.Set(128, 1) 244 fmt.Println("bitset:", bitSet) 245 res := bitSet.Not() 246 fmt.Println("~bitset", res) 247 248 println() 249 250 } 251 252 func BenchmarkBitSet_Set(b *testing.B) { 253 b.StopTimer() 254 size := 100 255 bs := NewBitSet(uint64(size)) 256 257 b.StartTimer() 258 for i := 0; i < b.N; i++ { 259 pos := rand.Intn(size) 260 val := rand.Intn(1) 261 bs.Set(uint64(pos), val) 262 } 263 }