github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/left_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/types"
    19  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    20  	"github.com/matrixorigin/matrixone/pkg/testutil"
    21  	"github.com/smartystreets/goconvey/convey"
    22  	"testing"
    23  )
    24  
    25  func TestLeft(t *testing.T) {
    26  	convey.Convey("right cases", t, func() {
    27  		type kase struct {
    28  			s    string
    29  			len  int64
    30  			want string
    31  		}
    32  
    33  		kases := []kase{
    34  			{
    35  				"abcde",
    36  				3,
    37  				"abc",
    38  			},
    39  			{
    40  				"abcde",
    41  				0,
    42  				"",
    43  			},
    44  			{
    45  				"abcde",
    46  				-1,
    47  				"",
    48  			},
    49  			{
    50  				"abcde",
    51  				100,
    52  				"abcde",
    53  			},
    54  			{
    55  				"foobarbar",
    56  				5,
    57  				"fooba",
    58  			},
    59  		}
    60  		var inStrs []string
    61  		var lens []int64
    62  		var outStrs []string
    63  		for _, k := range kases {
    64  			inStrs = append(inStrs, k.s)
    65  			lens = append(lens, k.len)
    66  			outStrs = append(outStrs, k.want)
    67  		}
    68  
    69  		inVec := testutil.MakeVarcharVector(inStrs, nil)
    70  		lenVec := testutil.MakeInt64Vector(lens, nil)
    71  		wantVec := testutil.MakeVarcharVector(outStrs, nil)
    72  		proc := testutil.NewProc()
    73  		retVec, err := Left([]*vector.Vector{inVec, lenVec}, proc)
    74  		convey.So(err, convey.ShouldBeNil)
    75  		ret := testutil.CompareVectors(wantVec, retVec)
    76  		convey.So(ret, convey.ShouldBeTrue)
    77  	})
    78  }
    79  
    80  func TestLeft1(t *testing.T) {
    81  	convey.Convey("left cases1", t, func() {
    82  		type kase struct {
    83  			s    string
    84  			len  int64
    85  			want string
    86  		}
    87  
    88  		kases := []kase{
    89  			{
    90  				"是都方式快递费",
    91  				3,
    92  				"是都方",
    93  			},
    94  			{
    95  				"アイウエオ",
    96  				3,
    97  				"アイウ",
    98  			},
    99  			{
   100  				"アイウエオ ",
   101  				3,
   102  				"アイウ",
   103  			},
   104  			{
   105  				"アイウエオ  ",
   106  				3,
   107  				"アイウ",
   108  			},
   109  			{
   110  				"アイウエオ   ",
   111  				3,
   112  				"アイウ",
   113  			},
   114  			{
   115  				"あいうえお",
   116  				3,
   117  				"あいう",
   118  			},
   119  			{
   120  				"あいうえお ",
   121  				3,
   122  				"あいう",
   123  			},
   124  			{
   125  				"あいうえお  ",
   126  				3,
   127  				"あいう",
   128  			},
   129  			{
   130  				"あいうえお   ",
   131  				3,
   132  				"あいう",
   133  			},
   134  			{
   135  				"龔龖龗龞龡",
   136  				3,
   137  				"龔龖龗",
   138  			},
   139  			{
   140  				"龔龖龗龞龡 ",
   141  				3,
   142  				"龔龖龗",
   143  			},
   144  			{
   145  				"龔龖龗龞龡  ",
   146  				3,
   147  				"龔龖龗",
   148  			},
   149  			{
   150  				"龔龖龗龞龡   ",
   151  				3,
   152  				"龔龖龗",
   153  			},
   154  			{
   155  				"2017-06-15    ",
   156  				8,
   157  				"2017-06-",
   158  			},
   159  			{
   160  				"2019-06-25    ",
   161  				8,
   162  				"2019-06-",
   163  			},
   164  			{
   165  				"    2019-06-25  ",
   166  				8,
   167  				"    2019",
   168  			},
   169  			{
   170  				"   2019-06-25   ",
   171  				8,
   172  				"   2019-",
   173  			},
   174  			{
   175  				"    2012-10-12   ",
   176  				8,
   177  				"    2012",
   178  			},
   179  			{
   180  				"   2004-04-24.   ",
   181  				8,
   182  				"   2004-",
   183  			},
   184  			{
   185  				"   2008-12-04.  ",
   186  				8,
   187  				"   2008-",
   188  			},
   189  			{
   190  				"    2012-03-23.   ",
   191  				8,
   192  				"    2012",
   193  			},
   194  			{
   195  				"    2013-04-30  ",
   196  				8,
   197  				"    2013",
   198  			},
   199  			{
   200  				"  1994-10-04  ",
   201  				8,
   202  				"  1994-1",
   203  			},
   204  			{
   205  				"   2018-06-04  ",
   206  				8,
   207  				"   2018-",
   208  			},
   209  			{
   210  				" 2012-10-12  ",
   211  				8,
   212  				" 2012-10",
   213  			},
   214  			{
   215  				"1241241^&@%#^*^!@#&*(!&    ",
   216  				12,
   217  				"1241241^&@%#",
   218  			},
   219  			{
   220  				" 123 ",
   221  				2,
   222  				" 1",
   223  			},
   224  		}
   225  		var inStrs []string
   226  		var lens []int64
   227  		var outStrs []string
   228  		for _, k := range kases {
   229  			inStrs = append(inStrs, k.s)
   230  			lens = append(lens, k.len)
   231  			outStrs = append(outStrs, k.want)
   232  		}
   233  
   234  		inVec := testutil.MakeVarcharVector(inStrs, nil)
   235  		lenVec := testutil.MakeInt64Vector(lens, nil)
   236  		wantVec := testutil.MakeVarcharVector(outStrs, nil)
   237  		proc := testutil.NewProc()
   238  		retVec, err := Left([]*vector.Vector{inVec, lenVec}, proc)
   239  		convey.So(err, convey.ShouldBeNil)
   240  		ret := testutil.CompareVectors(wantVec, retVec)
   241  		convey.So(ret, convey.ShouldBeTrue)
   242  	})
   243  }
   244  
   245  func TestLeft2(t *testing.T) {
   246  	convey.Convey("null", t, func() {
   247  		ivec := testutil.MakeScalarNull(types.T_char, 10)
   248  		lenVec := testutil.MakeScalarNull(types.T_int64, 10)
   249  		wantvec := testutil.MakeScalarNull(types.T_char, 10)
   250  		proc := testutil.NewProc()
   251  		ovec, err := Left([]*vector.Vector{ivec, lenVec}, proc)
   252  		convey.So(err, convey.ShouldBeNil)
   253  		ret := testutil.CompareVectors(wantvec, ovec)
   254  		convey.So(ret, convey.ShouldBeTrue)
   255  	})
   256  
   257  	convey.Convey("scalar", t, func() {
   258  		ivec := testutil.MakeScalarVarchar("abcdefg", 5)
   259  		lenVec := testutil.MakeScalarInt64(3, 5)
   260  		wantvec := testutil.MakeScalarVarchar("abc", 5)
   261  		proc := testutil.NewProc()
   262  		ovec, err := Left([]*vector.Vector{ivec, lenVec}, proc)
   263  		convey.So(err, convey.ShouldBeNil)
   264  		ret := testutil.CompareVectors(wantvec, ovec)
   265  		convey.So(ret, convey.ShouldBeTrue)
   266  	})
   267  }