github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/func_locate_test.go (about)

     1  // Copyright 2023 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 function
    16  
    17  import (
    18  	"fmt"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/testutil"
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  	"testing"
    24  )
    25  
    26  func Test_getSubstring(t *testing.T) {
    27  	tests := []struct {
    28  		name  string
    29  		str   string
    30  		start int
    31  		want  string
    32  	}{
    33  		{
    34  			name:  "test01",
    35  			str:   "asdadfd这个文档dsfsssss",
    36  			start: 8,
    37  			want:  "个文档dsfsssss",
    38  		},
    39  		{
    40  			name:  "test02",
    41  			str:   "asdadfd这个文档dsfsssss",
    42  			start: 4,
    43  			want:  "dfd这个文档dsfsssss",
    44  		},
    45  		{
    46  			name:  "test03",
    47  			str:   "asdadfd这个文档dsfsssss",
    48  			start: 0,
    49  			want:  "asdadfd这个文档dsfsssss",
    50  		},
    51  		{
    52  			name:  "test04",
    53  			str:   "asdadfd这个文档dsfsssss",
    54  			start: 40,
    55  			want:  "",
    56  		},
    57  	}
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			assert.Equalf(t, tt.want, getSubstring(tt.str, tt.start), "getSubstring(%v, %v)", tt.str, tt.start)
    61  		})
    62  	}
    63  }
    64  
    65  func initLocateTestCase() []tcTemp {
    66  	return []tcTemp{
    67  		{
    68  			info: "2",
    69  			typ:  types.T_varchar,
    70  			inputs: []testutil.FunctionTestInput{
    71  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
    72  					[]string{"ghi"},
    73  					[]bool{false}),
    74  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
    75  					[]string{"abcdefghijklmn"},
    76  					[]bool{false}),
    77  			},
    78  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
    79  				[]int64{7},
    80  				[]bool{false}),
    81  		},
    82  		{
    83  			info: "2",
    84  			typ:  types.T_varchar,
    85  			inputs: []testutil.FunctionTestInput{
    86  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
    87  					[]string{"gxhi"},
    88  					[]bool{false}),
    89  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
    90  					[]string{"abcdefghijklmn"},
    91  					[]bool{false}),
    92  			},
    93  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
    94  				[]int64{0},
    95  				[]bool{false}),
    96  		},
    97  		{
    98  			info: "3",
    99  			typ:  types.T_varchar,
   100  			inputs: []testutil.FunctionTestInput{
   101  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   102  					[]string{"ghi"},
   103  					[]bool{false}),
   104  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   105  					[]string{"abcdefghijklmn"},
   106  					[]bool{false}),
   107  				testutil.NewFunctionTestInput(types.T_int64.ToType(),
   108  					[]int64{0},
   109  					[]bool{false}),
   110  			},
   111  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
   112  				[]int64{0},
   113  				[]bool{false}),
   114  		},
   115  		{
   116  			info: "3",
   117  			typ:  types.T_varchar,
   118  			inputs: []testutil.FunctionTestInput{
   119  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   120  					[]string{"ghi"},
   121  					[]bool{false}),
   122  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   123  					[]string{"abcdefghijklmn"},
   124  					[]bool{false}),
   125  				testutil.NewFunctionTestInput(types.T_int64.ToType(),
   126  					[]int64{1},
   127  					[]bool{false}),
   128  			},
   129  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
   130  				[]int64{7},
   131  				[]bool{false}),
   132  		},
   133  		{
   134  			info: "3",
   135  			typ:  types.T_varchar,
   136  			inputs: []testutil.FunctionTestInput{
   137  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   138  					[]string{"ghi"},
   139  					[]bool{false}),
   140  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   141  					[]string{"abcdefghijklmn"},
   142  					[]bool{false}),
   143  				testutil.NewFunctionTestInput(types.T_int64.ToType(),
   144  					[]int64{6},
   145  					[]bool{false}),
   146  			},
   147  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
   148  				[]int64{7},
   149  				[]bool{false}),
   150  		},
   151  		{
   152  			info: "3",
   153  			typ:  types.T_varchar,
   154  			inputs: []testutil.FunctionTestInput{
   155  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   156  					[]string{"ghi"},
   157  					[]bool{false}),
   158  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   159  					[]string{"abcdefghijklmn"},
   160  					[]bool{false}),
   161  				testutil.NewFunctionTestInput(types.T_int64.ToType(),
   162  					[]int64{-6},
   163  					[]bool{false}),
   164  			},
   165  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
   166  				[]int64{0},
   167  				[]bool{false}),
   168  		},
   169  		{
   170  			info: "3",
   171  			typ:  types.T_varchar,
   172  			inputs: []testutil.FunctionTestInput{
   173  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   174  					[]string{"ghi"},
   175  					[]bool{false}),
   176  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   177  					[]string{"abcdefghijklmn"},
   178  					[]bool{false}),
   179  				testutil.NewFunctionTestInput(types.T_int64.ToType(),
   180  					[]int64{8},
   181  					[]bool{false}),
   182  			},
   183  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
   184  				[]int64{0},
   185  				[]bool{false}),
   186  		},
   187  		{
   188  			info: "3",
   189  			typ:  types.T_varchar,
   190  			inputs: []testutil.FunctionTestInput{
   191  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   192  					[]string{"xxx"},
   193  					[]bool{false}),
   194  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   195  					[]string{"abcdefghijklmn"},
   196  					[]bool{false}),
   197  				testutil.NewFunctionTestInput(types.T_int64.ToType(),
   198  					[]int64{8},
   199  					[]bool{false}),
   200  			},
   201  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
   202  				[]int64{0},
   203  				[]bool{false}),
   204  		},
   205  		{
   206  			info: "3",
   207  			typ:  types.T_varchar,
   208  			inputs: []testutil.FunctionTestInput{
   209  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   210  					[]string{"abcdefghijklmn"},
   211  					[]bool{false}),
   212  				testutil.NewFunctionTestInput(types.T_varchar.ToType(),
   213  					[]string{"abcdefghijklmn"},
   214  					[]bool{false}),
   215  				testutil.NewFunctionTestInput(types.T_int64.ToType(),
   216  					[]int64{0},
   217  					[]bool{false}),
   218  			},
   219  			expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false,
   220  				[]int64{0},
   221  				[]bool{false}),
   222  		},
   223  	}
   224  }
   225  
   226  func TestLocate(t *testing.T) {
   227  	testCases := initLocateTestCase()
   228  
   229  	// do the test work.
   230  	proc := testutil.NewProcess()
   231  	for _, tc := range testCases {
   232  		var fcTC testutil.FunctionTestCase
   233  		switch tc.info {
   234  		case "2":
   235  			fcTC = testutil.NewFunctionTestCase(proc,
   236  				tc.inputs, tc.expect, buildInLocate2Args)
   237  		case "3":
   238  			fcTC = testutil.NewFunctionTestCase(proc,
   239  				tc.inputs, tc.expect, buildInLocate3Args)
   240  		}
   241  		s, info := fcTC.Run()
   242  		require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info))
   243  	}
   244  }