github.com/scottcagno/storage@v1.8.0/pkg/bits/bitset_test.go (about) 1 package bits 2 3 import ( 4 "fmt" 5 "math" 6 "testing" 7 ) 8 9 const size = 1000000 // 1 million 10 11 func AssertExpected(t *testing.T, expected, got interface{}) { 12 if expected != got { //|| !reflect.DeepEqual(expected, got) { 13 t.Errorf("error, expected: %v, got: %v\n", expected, got) 14 } 15 } 16 17 func TestBitSet_IsSet(t *testing.T) { 18 bs := NewBitSet(16) 19 bs.Set(2).Set(4).Set(6) 20 AssertExpected(t, false, bs.IsSet(1)) 21 AssertExpected(t, true, bs.IsSet(2)) 22 AssertExpected(t, false, bs.IsSet(3)) 23 AssertExpected(t, true, bs.IsSet(4)) 24 AssertExpected(t, false, bs.IsSet(5)) 25 AssertExpected(t, true, bs.IsSet(6)) 26 AssertExpected(t, false, bs.IsSet(128)) 27 bs = nil 28 } 29 30 func TestBitSet_Len(t *testing.T) { 31 bs := NewBitSet(16) 32 AssertExpected(t, uint(16), bs.Len()) 33 bs = nil 34 } 35 36 func TestBitSet_Set(t *testing.T) { 37 bs := NewBitSet(16) 38 bs.Set(2).Set(4).Set(6) 39 AssertExpected(t, false, bs.IsSet(1)) 40 AssertExpected(t, true, bs.IsSet(2)) 41 AssertExpected(t, false, bs.IsSet(3)) 42 AssertExpected(t, true, bs.IsSet(4)) 43 AssertExpected(t, false, bs.IsSet(5)) 44 AssertExpected(t, true, bs.IsSet(6)) 45 bs = nil 46 } 47 48 func TestBitSet_String(t *testing.T) { 49 bs := NewBitSet(16) 50 bs.Set(2).Set(4).Set(6) 51 str := bs.String() 52 AssertExpected(t, true, str != "") 53 fmt.Println(str) 54 bs = nil 55 } 56 57 func TestBitSet_Unset(t *testing.T) { 58 bs := NewBitSet(16) 59 bs.Set(2).Set(4).Set(6) 60 AssertExpected(t, false, bs.IsSet(1)) 61 AssertExpected(t, true, bs.IsSet(2)) 62 AssertExpected(t, false, bs.IsSet(3)) 63 AssertExpected(t, true, bs.IsSet(4)) 64 AssertExpected(t, false, bs.IsSet(5)) 65 AssertExpected(t, true, bs.IsSet(6)) 66 bs.Unset(2).Unset(4).Unset(6) 67 AssertExpected(t, false, bs.IsSet(1)) 68 AssertExpected(t, false, bs.IsSet(2)) 69 AssertExpected(t, false, bs.IsSet(3)) 70 AssertExpected(t, false, bs.IsSet(4)) 71 AssertExpected(t, false, bs.IsSet(5)) 72 AssertExpected(t, false, bs.IsSet(6)) 73 bs = nil 74 } 75 76 func TestBitSet_Value(t *testing.T) { 77 bs := NewBitSet(16) 78 bs.Set(1) 79 v := bs.Value(1) 80 AssertExpected(t, uint(2), v) 81 bs = nil 82 } 83 84 func TestBitSet_resize(t *testing.T) { 85 bs := NewBitSet(16) 86 AssertExpected(t, uint(16), bs.Len()) 87 bs.resize(32) 88 AssertExpected(t, uint(32), bs.Len()) 89 bs = nil 90 } 91 92 func TestNewBitSet(t *testing.T) { 93 bs := NewBitSet(16) 94 AssertExpected(t, uint(16), bs.Len()) 95 bs = nil 96 } 97 98 func Test_alignedSize(t *testing.T) { 99 var size uint 100 size = alignedSize(62) 101 AssertExpected(t, uint(1), size) 102 size = alignedSize(96) 103 AssertExpected(t, uint(2), size) 104 } 105 106 func TestBitSetTestMany(t *testing.T) { 107 bs := NewBitSet(16) 108 for i := 0; i < 16; i++ { 109 x := uint(i) 110 AssertExpected(t, false, bs.IsSet(x)) 111 bs.Set(x) 112 AssertExpected(t, true, bs.IsSet(x)) 113 v := bs.Value(x) 114 AssertExpected(t, uint(1<<x), v) 115 bs.Unset(x) 116 AssertExpected(t, false, bs.IsSet(x)) 117 } 118 bs = nil 119 } 120 121 func Benchmark_Log2_Version1(b *testing.B) { 122 123 var result uint 124 125 b.ResetTimer() 126 b.ReportAllocs() 127 for n := 0; n < b.N; n++ { 128 result = log2Version1(uint(n)) 129 } 130 131 _ = result 132 } 133 func Benchmark_Log2_Version2(b *testing.B) { 134 135 var result uint 136 137 b.ResetTimer() 138 b.ReportAllocs() 139 for n := 0; n < b.N; n++ { 140 result = log2Version2(uint(n)) 141 _ = result 142 } 143 144 _ = result 145 146 } 147 148 func log2Version1(i uint) uint { 149 v1 := func(i uint) uint { 150 return uint(math.Log2(float64(i))) 151 } 152 //return v1(i) 153 v := v1(i) 154 if v < 0 { 155 return 0 156 } 157 return v 158 } 159 160 func log2Version2(i uint) uint { 161 v2 := func(i uint) uint { 162 var n uint 163 for ; i > 0; n++ { 164 i >>= 1 165 } 166 return n - 1 167 } 168 //return v2(i) 169 v := v2(i) 170 if v < 0 { 171 return 0 172 } 173 return v 174 }