github.com/matrixorigin/matrixone@v1.2.0/pkg/container/nulls/nulls_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 nulls
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestOr(t *testing.T) {
    24  	t.Run("or test", func(t *testing.T) {
    25  		var m, m1, n, result Nulls
    26  		n.InitWithSize(4000)
    27  		for k := uint64(0); k < 4000; k++ {
    28  			n.Add(k)
    29  		}
    30  
    31  		m.InitWithSize(8000)
    32  		for k := uint64(4000); k < 8000; k++ {
    33  			m.Add(k)
    34  		}
    35  
    36  		Or(&n, &m, &result)
    37  		assert.Equal(t, n.Count()+m.Count(), result.Count())
    38  		result.Reset()
    39  		Or(&n, &m1, &result)
    40  		assert.Equal(t, n.Count(), result.Count())
    41  		result.Reset()
    42  		Or(&m1, &n, &result)
    43  		assert.Equal(t, n.Count(), result.Count())
    44  	})
    45  }
    46  
    47  func TestReset(t *testing.T) {
    48  	t.Run("reset test", func(t *testing.T) {
    49  		var n Nulls
    50  		n.InitWithSize(2000)
    51  		for i := uint64(0); i < 2000; i += 7 {
    52  			n.Add(i)
    53  		}
    54  		n.Reset()
    55  		assert.EqualValues(t, 0, n.Count())
    56  	})
    57  }
    58  
    59  func TestAny(t *testing.T) {
    60  	t.Run("Any test", func(t *testing.T) {
    61  		var n Nulls
    62  		assert.EqualValues(t, false, Any(&n))
    63  		n.InitWithSize(2000)
    64  		for i := uint64(0); i < 2000; i += 7 {
    65  			n.Add(i)
    66  		}
    67  		assert.EqualValues(t, true, Any(&n))
    68  	})
    69  }
    70  
    71  func TestSize(t *testing.T) {
    72  	t.Run("Size test", func(t *testing.T) {
    73  		var n Nulls
    74  		assert.EqualValues(t, 0, Size(&n))
    75  		n.InitWithSize(16)
    76  		for i := uint64(0); i < 16; i++ {
    77  			n.Add(i)
    78  		}
    79  		assert.EqualValues(t, 8, Size(&n))
    80  	})
    81  }
    82  
    83  func TestLength(t *testing.T) {
    84  	t.Run("Length test", func(t *testing.T) {
    85  		var n Nulls
    86  		assert.EqualValues(t, 0, n.Count())
    87  		n.InitWithSize(16)
    88  		for i := uint64(0); i < 16; i += 2 {
    89  			n.Add(i)
    90  		}
    91  		assert.EqualValues(t, 8, n.Count())
    92  	})
    93  }
    94  
    95  func TestString(t *testing.T) {
    96  	t.Run("String test", func(t *testing.T) {
    97  		var n Nulls
    98  		assert.EqualValues(t, "[]", String(&n))
    99  		n.InitWithSize(16)
   100  		for i := uint64(0); i < 16; i += 2 {
   101  			n.Add(i)
   102  		}
   103  		assert.EqualValues(t, "[0 2 4 6 8 10 12 14]", String(&n))
   104  	})
   105  }
   106  
   107  func TestContains(t *testing.T) {
   108  	t.Run("Contains test", func(t *testing.T) {
   109  		var n Nulls
   110  		assert.EqualValues(t, false, Contains(&n, 2))
   111  		n.InitWithSize(16)
   112  		for i := uint64(0); i < 16; i += 2 {
   113  			n.Add(i)
   114  		}
   115  		assert.EqualValues(t, true, Contains(&n, 2))
   116  	})
   117  }
   118  
   119  func TestAdd(t *testing.T) {
   120  	t.Run("Contains test", func(t *testing.T) {
   121  		n := Nulls{}
   122  		assert.EqualValues(t, false, Contains(&n, 2))
   123  		Add(&n, 2)
   124  		assert.EqualValues(t, true, Contains(&n, 2))
   125  	})
   126  }
   127  
   128  func TestDel(t *testing.T) {
   129  	t.Run("Contains test", func(t *testing.T) {
   130  		n := &Nulls{}
   131  		Add(n, 2)
   132  		assert.EqualValues(t, true, Contains(n, 2))
   133  		Del(n, 2)
   134  		assert.EqualValues(t, false, Contains(n, 2))
   135  	})
   136  }
   137  
   138  func TestSet(t *testing.T) {
   139  	t.Run("set test", func(t *testing.T) {
   140  		n := &Nulls{}
   141  		Add(n, 2)
   142  		m := &Nulls{}
   143  		Add(m, 5)
   144  		Set(n, m)
   145  		assert.Equal(t, 2, n.Count())
   146  	})
   147  }
   148  
   149  func TestFilterCount(t *testing.T) {
   150  	t.Run("FilterCount test", func(t *testing.T) {
   151  		var n Nulls
   152  		Add(&n, 2, 3, 5, 8)
   153  		assert.Equal(t, 2, FilterCount(&n, []int64{2, 3}))
   154  	})
   155  }
   156  
   157  func TestRemoveRange(t *testing.T) {
   158  	t.Run("set test", func(t *testing.T) {
   159  		var n Nulls
   160  		n.InitWithSize(16)
   161  		for i := uint64(0); i < 16; i++ {
   162  			n.Add(i)
   163  		}
   164  		assert.Equal(t, 16, n.Count())
   165  		RemoveRange(&n, 10, 16)
   166  		assert.Equal(t, 10, n.Count())
   167  	})
   168  }
   169  
   170  func TestRange(t *testing.T) {
   171  	t.Run("set test", func(t *testing.T) {
   172  		var m, n, correctM Nulls
   173  		Range(&n, 0, 16, 0, &m)
   174  		n.InitWithSize(16)
   175  		for i := uint64(0); i < 16; i++ {
   176  			n.Add(i)
   177  		}
   178  		Range(&n, 0, 16, 0, &m)
   179  		assert.Equal(t, n.Count(), m.Count())
   180  		Range(&n, 10, 16, 0, &m)
   181  		correctM.InitWithSize(16)
   182  		for i := uint64(10); i < 16; i++ {
   183  			correctM.Add(i)
   184  		}
   185  		assert.Equal(t, correctM.Count(), m.Count())
   186  	})
   187  }
   188  
   189  func TestFilter(t *testing.T) {
   190  	t.Run("set test", func(t *testing.T) {
   191  		var n Nulls
   192  		for i := uint64(0); i < 16; i++ {
   193  			n.Add(i)
   194  		}
   195  		assert.Equal(t, 16, n.Count())
   196  		sels := []int64{1, 3, 5}
   197  		Filter(&n, sels, false)
   198  		assert.Equal(t, 3, n.Count())
   199  	})
   200  }
   201  
   202  func TestMerge(t *testing.T) {
   203  	t.Run("merge test", func(t *testing.T) {
   204  		var n, m Nulls
   205  		for i := uint64(0); i < 16; i++ {
   206  			n.Add(i)
   207  		}
   208  		for i := uint64(8); i < 24; i++ {
   209  			m.Add(i)
   210  		}
   211  		assert.Equal(t, 16, n.Count())
   212  		assert.Equal(t, 16, m.Count())
   213  		n.Merge(&m)
   214  		assert.Equal(t, 24, n.Count())
   215  	})
   216  }