github.com/matrixorigin/matrixone@v1.2.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 newBm(n int) *Bitmap {
    30  	var bm Bitmap
    31  	bm.InitWithSize(int64(n))
    32  	return &bm
    33  }
    34  
    35  func TestNulls(t *testing.T) {
    36  	np := newBm(Rows)
    37  	np.AddRange(0, 0)
    38  	np.AddRange(1, 10)
    39  	require.Equal(t, 9, np.Count())
    40  	np.Reset()
    41  
    42  	ok := np.IsEmpty()
    43  	require.Equal(t, true, ok)
    44  	np.TryExpandWithSize(10)
    45  	np.Add(0)
    46  	ok = np.Contains(0)
    47  	require.Equal(t, true, ok)
    48  	require.Equal(t, 1, np.Count())
    49  
    50  	itr := np.Iterator()
    51  	require.Equal(t, uint64(0), itr.PeekNext())
    52  	require.Equal(t, true, itr.HasNext())
    53  	require.Equal(t, uint64(0), itr.Next())
    54  
    55  	np.Remove(0)
    56  	ok = np.IsEmpty()
    57  	require.Equal(t, true, ok)
    58  
    59  	np.AddMany([]uint64{1, 2, 3})
    60  	require.Equal(t, 3, np.Count())
    61  	np.RemoveRange(1, 3)
    62  	require.Equal(t, 0, np.Count())
    63  
    64  	np.AddMany([]uint64{1, 2, 3})
    65  	np.Filter([]int64{0})
    66  	fmt.Printf("%v\n", np.String())
    67  	fmt.Printf("size: %v\n", np.Size())
    68  	fmt.Printf("numbers: %v\n", np.Count())
    69  
    70  	nq := newBm(Rows)
    71  	nq.Unmarshal(np.Marshal())
    72  
    73  	require.Equal(t, np.ToArray(), nq.ToArray())
    74  
    75  	np.Reset()
    76  }
    77  
    78  func BenchmarkAdd(b *testing.B) {
    79  	np := newBm(BenchmarkRows)
    80  	for i := 0; i < b.N; i++ {
    81  		for j := 0; j < BenchmarkRows; j++ {
    82  			np.Add(uint64(j))
    83  		}
    84  		for j := 0; j < BenchmarkRows; j++ {
    85  			np.Contains(uint64(j))
    86  		}
    87  		for j := 0; j < BenchmarkRows; j++ {
    88  			np.Remove(uint64(j))
    89  		}
    90  	}
    91  }
    92  
    93  func TestC(t *testing.T) {
    94  	bm3 := newBm(10000)
    95  	bm5 := newBm(10000)
    96  
    97  	require.True(t, bm3.IsEmpty())
    98  	require.True(t, bm3.C_IsEmpty())
    99  	require.True(t, bm3.Count() == 0)
   100  	require.True(t, bm3.C_Count() == 0)
   101  
   102  	for i := 0; i < 10000; i += 3 {
   103  		bm3.Add(uint64(i))
   104  	}
   105  	for i := 0; i < 10000; i += 5 {
   106  		bm3.Add(uint64(i))
   107  	}
   108  
   109  	require.False(t, bm3.IsEmpty())
   110  	require.False(t, bm3.C_IsEmpty())
   111  
   112  	bm3Copy := bm3.Clone()
   113  	require.True(t, bm3.IsSame(bm3Copy))
   114  	require.True(t, bm3.C_Count() == bm3Copy.C_Count())
   115  	bm5Copy := bm5.Clone()
   116  	require.True(t, bm5.IsSame(bm5Copy))
   117  	require.True(t, bm5.C_Count() == bm5Copy.C_Count())
   118  
   119  	bm3CopyAgain := bm3.Clone()
   120  
   121  	bm3.Or(bm5)
   122  	bm3Copy.C_Or(bm5)
   123  	require.True(t, bm3.IsSame(bm3Copy))
   124  	require.True(t, bm3.Count() == int(bm3Copy.C_Count()))
   125  
   126  	bm5.And(bm3CopyAgain)
   127  	bm5Copy.C_And(bm3CopyAgain)
   128  	require.True(t, bm5.IsSame(bm5Copy))
   129  	require.True(t, bm5.Count() == int(bm5Copy.C_Count()))
   130  }
   131  
   132  func TestBitmapIterator_Next(t *testing.T) {
   133  	np := newBm(BenchmarkRows)
   134  	np.AddRange(0, 64)
   135  
   136  	// | 63 -- 0 | 127 -- 64 | 191 -- 128 | 255 -- 192 | 319 -- 256 | 383 -- 320 | ... |
   137  	rows := []uint64{127, 192, 320} // add some boundary case to check if loops over word
   138  	np.AddMany(rows)
   139  
   140  	itr := np.Iterator()
   141  	i := uint64(0)
   142  	for itr.HasNext() {
   143  		r := itr.Next()
   144  		if r < uint64(64) {
   145  			require.Equal(t, i, r)
   146  			i++
   147  		} else {
   148  			t.Logf("r now is %d\n", r)
   149  		}
   150  	}
   151  }