github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/instr_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 binary
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    19  	"github.com/matrixorigin/matrixone/pkg/testutil"
    20  	"github.com/stretchr/testify/require"
    21  	"testing"
    22  )
    23  
    24  func TestInstr(t *testing.T) {
    25  	proc := testutil.NewProc()
    26  	kases := []struct {
    27  		strs    []string
    28  		substrs []string
    29  		wants   []int64
    30  	}{
    31  		{
    32  			strs:    []string{"abc", "abc", "abc", "abc", "abc"},
    33  			substrs: []string{"bc", "b", "abc", "a", "dca"},
    34  			wants:   []int64{2, 2, 1, 1, 0},
    35  		},
    36  		{
    37  			strs:    []string{"abc", "abc", "abc", "abc", "abc"},
    38  			substrs: []string{"", "", "a", "b", "c"},
    39  			wants:   []int64{1, 1, 1, 2, 3},
    40  		},
    41  		{
    42  			strs:    []string{"abc", "abc", "abc", "abc", "abc"},
    43  			substrs: []string{"bc"},
    44  			wants:   []int64{2, 2, 2, 2, 2},
    45  		},
    46  		{
    47  			strs:    []string{"abc"},
    48  			substrs: []string{"bc", "b", "abc", "a", "dca"},
    49  			wants:   []int64{2, 2, 1, 1, 0},
    50  		},
    51  	}
    52  	for _, k := range kases {
    53  		inVec := testutil.MakeVarcharVector(k.strs, nil)
    54  		subVec := testutil.MakeVarcharVector(k.substrs, nil)
    55  		if len(k.strs) == 1 {
    56  			inVec.MakeScalar(1)
    57  		}
    58  		if len(k.substrs) == 1 {
    59  			subVec.MakeScalar(1)
    60  		}
    61  		v, err := Instr([]*vector.Vector{inVec, subVec}, proc)
    62  		require.NoError(t, err)
    63  		vSlice := vector.MustTCols[int64](v)
    64  		require.Equal(t, k.wants, vSlice)
    65  		if inVec.IsScalar() {
    66  			inVec.Nsp.Set(0)
    67  			_, err = Instr([]*vector.Vector{inVec, subVec}, proc)
    68  			require.NoError(t, err)
    69  		}
    70  	}
    71  
    72  }