github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/startswith_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  	"log"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  	"github.com/matrixorigin/matrixone/pkg/testutil"
    24  	"github.com/smartystreets/goconvey/convey"
    25  )
    26  
    27  func Test_StartsWith(t *testing.T) {
    28  	convey.Convey("Test StartsWith succ", t, func() {
    29  		var charVecBase = []string{"-123", "123", "+123", "8", ""}
    30  		var charVecBase2 = []string{"-", "+", "1", ""}
    31  		var nsp1, nsp2 []uint64
    32  		var origVecs = make([]*vector.Vector, 2)
    33  		var proc = testutil.NewProc()
    34  		n1, n2 := len(charVecBase), len(charVecBase2)
    35  		inputVec := make([]string, n1*n2)
    36  		inputVec2 := make([]string, len(inputVec))
    37  		for i := 0; i < len(inputVec); i++ {
    38  			inputVec[i] = charVecBase[i/n2]
    39  			inputVec2[i] = charVecBase2[i%n2]
    40  			if (i / n2) == (n1 - 1) {
    41  				nsp1 = append(nsp1, uint64(i))
    42  			}
    43  			if (i % n2) == (n2 - 1) {
    44  				nsp2 = append(nsp2, uint64(i))
    45  			}
    46  		}
    47  		origVecs[0] = testutil.MakeCharVector(inputVec, nsp1)
    48  		origVecs[1] = testutil.MakeCharVector(inputVec2, nsp2)
    49  		vec, err := Startswith(origVecs, proc)
    50  		if err != nil {
    51  			log.Fatal(err)
    52  		}
    53  		data, ok := vec.Col.([]uint8)
    54  		if !ok {
    55  			log.Fatal(moerr.NewInternalError(proc.Ctx, "the Startswith function return value type is not []uint8"))
    56  		}
    57  		compVec := []uint8{1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1}
    58  		compNsp := []int64{3, 7, 11, 15, 16, 17, 18, 19}
    59  
    60  		for i := 0; i < len(compVec); i++ {
    61  			convey.So(data[i], convey.ShouldEqual, compVec[i])
    62  		}
    63  		j := 0
    64  		for i := 0; i < len(compVec); i++ {
    65  			if j < len(compNsp) {
    66  				if compNsp[j] == int64(i) {
    67  					convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue)
    68  					j++
    69  				} else {
    70  					convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse)
    71  				}
    72  			} else {
    73  				convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse)
    74  			}
    75  		}
    76  	})
    77  }