github.com/matrixorigin/matrixone@v0.7.0/pkg/common/bitmap/bitmap_test.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package bitmap 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 ) 23 24 const ( 25 Rows = 10 26 BenchmarkRows = 8192 27 ) 28 29 func TestNulls(t *testing.T) { 30 np := New(Rows) 31 np.AddRange(0, 0) 32 np.AddRange(1, 10) 33 require.Equal(t, 9, np.Count()) 34 np.Clear() 35 36 ok := np.IsEmpty() 37 require.Equal(t, true, ok) 38 np.Add(0) 39 ok = np.Contains(0) 40 require.Equal(t, true, ok) 41 require.Equal(t, 1, np.Count()) 42 43 itr := np.Iterator() 44 require.Equal(t, uint64(0), itr.PeekNext()) 45 require.Equal(t, true, itr.HasNext()) 46 require.Equal(t, uint64(0), itr.Next()) 47 48 np.Remove(0) 49 ok = np.IsEmpty() 50 require.Equal(t, true, ok) 51 52 np.AddMany([]uint64{1, 2, 3}) 53 require.Equal(t, 3, np.Count()) 54 np.RemoveRange(1, 3) 55 require.Equal(t, 0, np.Count()) 56 57 np.AddMany([]uint64{1, 2, 3}) 58 np.Filter([]int64{0}) 59 fmt.Printf("%v\n", np.String()) 60 fmt.Printf("size: %v\n", np.Size()) 61 fmt.Printf("numbers: %v\n", np.Count()) 62 63 nq := New(Rows) 64 nq.Unmarshal(np.Marshal()) 65 66 require.Equal(t, np.ToArray(), nq.ToArray()) 67 68 np.Clear() 69 } 70 71 func BenchmarkAdd(b *testing.B) { 72 np := New(BenchmarkRows) 73 for i := 0; i < b.N; i++ { 74 for j := 0; j < BenchmarkRows; j++ { 75 np.Add(uint64(j)) 76 } 77 for j := 0; j < BenchmarkRows; j++ { 78 np.Contains(uint64(j)) 79 } 80 for j := 0; j < BenchmarkRows; j++ { 81 np.Remove(uint64(j)) 82 } 83 } 84 } 85 86 func TestC(t *testing.T) { 87 bm3 := New(10000) 88 bm5 := New(10000) 89 90 require.True(t, bm3.IsEmpty()) 91 require.True(t, bm3.C_IsEmpty()) 92 require.True(t, bm3.Count() == 0) 93 require.True(t, bm3.C_Count() == 0) 94 95 for i := 0; i < 10000; i += 3 { 96 bm3.Add(uint64(i)) 97 } 98 for i := 0; i < 10000; i += 5 { 99 bm3.Add(uint64(i)) 100 } 101 102 require.False(t, bm3.IsEmpty()) 103 require.False(t, bm3.C_IsEmpty()) 104 105 bm3Copy := bm3.Clone() 106 require.True(t, bm3.IsSame(bm3Copy)) 107 require.True(t, bm3.C_Count() == bm3Copy.C_Count()) 108 bm5Copy := bm5.Clone() 109 require.True(t, bm5.IsSame(bm5Copy)) 110 require.True(t, bm5.C_Count() == bm5Copy.C_Count()) 111 112 bm3CopyAgain := bm3.Clone() 113 114 bm3.Or(bm5) 115 bm3Copy.C_Or(bm5) 116 require.True(t, bm3.IsSame(bm3Copy)) 117 require.True(t, bm3.Count() == int(bm3Copy.C_Count())) 118 119 bm5.And(bm3CopyAgain) 120 bm5Copy.C_And(bm3CopyAgain) 121 require.True(t, bm5.IsSame(bm5Copy)) 122 require.True(t, bm5.Count() == int(bm5Copy.C_Count())) 123 } 124 125 func TestBitmapIterator_Next(t *testing.T) { 126 np := New(BenchmarkRows) 127 np.AddRange(0, 64) 128 129 // | 63 -- 0 | 127 -- 64 | 191 -- 128 | 255 -- 192 | 319 -- 256 | 383 -- 320 | ... | 130 rows := []uint64{127, 192, 320} // add some boundary case to check if loops over word 131 np.AddMany(rows) 132 133 itr := np.Iterator() 134 i := uint64(0) 135 for itr.HasNext() { 136 r := itr.Next() 137 if r < uint64(64) { 138 require.Equal(t, i, r) 139 i++ 140 } else { 141 t.Logf("r now is %d\n", r) 142 } 143 } 144 }