github.com/matrixorigin/matrixone@v1.2.0/pkg/container/vector/utils_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 vector
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestFindFirstIndex(t *testing.T) {
    26  	testCases := []struct {
    27  		items  []int32
    28  		target int32
    29  		result int
    30  	}{
    31  		{[]int32{1, 2, 2, 3, 3}, 3, 3},
    32  		{[]int32{1, 2, 3, 3, 5}, 3, 2},
    33  		{[]int32{1, 2, 2, 2, 2}, 3, -1},
    34  	}
    35  
    36  	t.Run("test orderedFindFirstIndexInSortedSlice", func(t *testing.T) {
    37  		for i, testCase := range testCases {
    38  			result := OrderedFindFirstIndexInSortedSlice(testCase.target, testCase.items)
    39  			require.Equal(t, testCase.result, result, "test OrderedFindFirstIndexInSortedSlice at cases[%d], get result is different with expected", i)
    40  		}
    41  	})
    42  }
    43  
    44  // test FindFirstIndexInSortedVarlenVector
    45  func TestFindFirstIndexInSortedVarlenVector(t *testing.T) {
    46  	mp := mpool.MustNewZero()
    47  	v1 := NewVec(types.T_char.ToType())
    48  	err := AppendStringList(v1, []string{"a", "b", "b", "c", "c"}, nil, mp)
    49  	require.NoError(t, err)
    50  	defer v1.Free(mp)
    51  	v2 := NewVec(types.T_char.ToType())
    52  	err = AppendStringList(v2, []string{"a", "b", "c", "c", "e"}, nil, mp)
    53  	require.NoError(t, err)
    54  	defer v2.Free(mp)
    55  	v3 := NewVec(types.T_char.ToType())
    56  	err = AppendStringList(v3, []string{"a", "b", "b", "b", "b"}, nil, mp)
    57  	require.NoError(t, err)
    58  	defer v3.Free(mp)
    59  
    60  	testCases := []struct {
    61  		items  *Vector
    62  		target string
    63  		result int
    64  	}{
    65  		{v1, "c", 3},
    66  		{v2, "c", 2},
    67  		{v3, "c", -1},
    68  	}
    69  
    70  	t.Run("test FindFirstIndexInSortedVarlenVector", func(t *testing.T) {
    71  		for i, testCase := range testCases {
    72  			result := FindFirstIndexInSortedVarlenVector(testCase.items, []byte(testCase.target))
    73  			require.Equal(t, testCase.result, result, "test FindFirstIndexInSortedVarlenVector at cases[%d], get result is different with expected", i)
    74  		}
    75  	})
    76  }
    77  
    78  func TestCollectOffsetsByPrefixEqFactory(t *testing.T) {
    79  	mp := mpool.MustNewZero()
    80  	v1 := NewVec(types.T_char.ToType())
    81  	defer v1.Free(mp)
    82  
    83  	AppendBytes(v1, []byte("1111"), false, mp)
    84  	AppendBytes(v1, []byte("1121"), false, mp)
    85  	AppendBytes(v1, []byte("1211"), false, mp)
    86  	AppendBytes(v1, []byte("1221"), false, mp)
    87  	AppendBytes(v1, []byte("1231"), false, mp)
    88  	AppendBytes(v1, []byte("1311"), false, mp)
    89  
    90  	prefix1 := []byte("01")
    91  	prefix2 := []byte("12")
    92  	prefix3 := []byte("14")
    93  	prefix4 := []byte("113")
    94  
    95  	fn1 := CollectOffsetsByPrefixEqFactory(prefix1)
    96  	fn2 := CollectOffsetsByPrefixEqFactory(prefix2)
    97  	fn3 := CollectOffsetsByPrefixEqFactory(prefix3)
    98  	fn4 := CollectOffsetsByPrefixEqFactory(prefix4)
    99  	off1 := fn1(v1)
   100  	off2 := fn2(v1)
   101  	off3 := fn3(v1)
   102  	off4 := fn4(v1)
   103  	require.Equal(t, 0, len(off1))
   104  	require.Equal(t, []int32{2, 3, 4}, off2)
   105  	require.Equal(t, 0, len(off3))
   106  	require.Equal(t, 0, len(off4))
   107  }
   108  
   109  func TestCollectOffsetsByPrefixBetweenFactory(t *testing.T) {
   110  	mp := mpool.MustNewZero()
   111  	v1 := NewVec(types.T_char.ToType())
   112  	defer v1.Free(mp)
   113  
   114  	AppendBytes(v1, []byte("1111"), false, mp)
   115  	AppendBytes(v1, []byte("1121"), false, mp)
   116  	AppendBytes(v1, []byte("1211"), false, mp)
   117  	AppendBytes(v1, []byte("1221"), false, mp)
   118  	AppendBytes(v1, []byte("1231"), false, mp)
   119  	AppendBytes(v1, []byte("1311"), false, mp)
   120  
   121  	left1 := []byte("11")
   122  	right1 := []byte("12")
   123  	left2 := []byte("113")
   124  	right2 := []byte("124")
   125  
   126  	fn1 := CollectOffsetsByPrefixBetweenFactory(left1, right1)
   127  	fn2 := CollectOffsetsByPrefixBetweenFactory(left2, right2)
   128  	off1 := fn1(v1)
   129  	off2 := fn2(v1)
   130  
   131  	require.Equal(t, []int32{0, 1, 2, 3, 4}, off1)
   132  	require.Equal(t, []int32{2, 3, 4}, off2)
   133  }