github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/findinset/findinset_test.go (about)

     1  // Copyright 2022 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 findinset
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestFindInSet(t *testing.T) {
    24  	tt := []struct {
    25  		str     string
    26  		strlist string
    27  		idx     int
    28  	}{
    29  		{"abc", "abc,uzi", 1},
    30  		{"xyz", "dec,xyz,abc", 2},
    31  		{"z", "a,e,c,z", 4},
    32  		// not match
    33  		{"a,", "a,d,", 0},
    34  		{"", "a,d,", 3},
    35  		{" xxx ", "wwww, xxx ", 2},
    36  		{"dbs", "abc,def", 0},
    37  		{"测试", "测试1,测试", 2},
    38  	}
    39  	strs := make([]string, len(tt))
    40  	strlists := make([]string, len(tt))
    41  	want := make([]uint64, len(tt))
    42  	for i, tc := range tt {
    43  		rs := findInStrList(tc.str, tc.strlist)
    44  		if rs != uint64(tc.idx) {
    45  			t.Fatalf("findInStrList(%s, %s) = %d, want %d", tc.str, tc.strlist, rs, tc.idx)
    46  		}
    47  		strs[i] = tc.str
    48  		strlists[i] = tc.strlist
    49  		want[i] = uint64(tc.idx)
    50  	}
    51  
    52  	lv := strs
    53  	rv := strlists
    54  	got := make([]uint64, len(tt))
    55  	got = FindInSet(lv, rv, got)
    56  	require.Equal(t, want, got)
    57  }
    58  
    59  func TestFindInSetWithLeftConst(t *testing.T) {
    60  	tt := []struct {
    61  		str      string
    62  		strlists []string
    63  		want     []uint64
    64  		got      []uint64
    65  	}{
    66  		{
    67  			str:      "U",
    68  			strlists: []string{"I,U,O", "U,P,V", "V,W,Z", "W,X,U"},
    69  			want:     []uint64{2, 1, 0, 3},
    70  			got:      make([]uint64, 4),
    71  		},
    72  	}
    73  
    74  	for _, tc := range tt {
    75  		lv := tc.str
    76  		rv := tc.strlists
    77  		got := FindInSetWithLeftConst(lv, rv, tc.got)
    78  		require.Equal(t, tc.want, got)
    79  	}
    80  }
    81  
    82  func TestFindInSetWithRightConst(t *testing.T) {
    83  	tt := []struct {
    84  		str      []string
    85  		strlists string
    86  		want     []uint64
    87  		got      []uint64
    88  	}{
    89  		{
    90  			str:      []string{"10", "2", "6", "8"},
    91  			strlists: "1,2,3,4,5,6,7,8,9,10",
    92  			want:     []uint64{10, 2, 6, 8},
    93  			got:      make([]uint64, 4),
    94  		},
    95  	}
    96  
    97  	for _, tc := range tt {
    98  		lv := tc.str
    99  		rv := tc.strlists
   100  		got := FindInSetWithRightConst(lv, rv, tc.got)
   101  		require.Equal(t, tc.want, got)
   102  	}
   103  }