github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/lengthutf8_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 unary
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/testutil"
    23  	"github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestLengthUTF8(t *testing.T) {
    27  	convey.Convey("right case", t, func() {
    28  		type kase struct {
    29  			s    string
    30  			want uint64
    31  		}
    32  
    33  		kases := []kase{
    34  			{"abc", 3},
    35  			{"", 0},
    36  			{"   ", 3},
    37  			{"中国123", 5},
    38  			{"abc😄", 4},
    39  			{"中国中国中国中国中国中国中国中国中国中国1234", 24},
    40  			{"中国中国中国中国中国中国中国中国中国中国1234😄ggg!", 29},
    41  			{
    42  				"你好",
    43  				2,
    44  			},
    45  			{
    46  				"français",
    47  				8,
    48  			},
    49  			{
    50  				"にほんご",
    51  				4,
    52  			},
    53  			{
    54  				"Español",
    55  				7,
    56  			},
    57  			{
    58  				"123456",
    59  				6,
    60  			},
    61  			{
    62  				"андрей",
    63  				6,
    64  			},
    65  			{
    66  				"\\",
    67  				1,
    68  			},
    69  			{
    70  				string(rune(0x0c)),
    71  				1,
    72  			},
    73  			{
    74  				string('"'),
    75  				1,
    76  			},
    77  			{
    78  				string('\a'),
    79  				1,
    80  			},
    81  			{
    82  				string('\b'),
    83  				1,
    84  			},
    85  			{
    86  				string('\t'),
    87  				1,
    88  			},
    89  			{
    90  				string('\n'),
    91  				1,
    92  			},
    93  			{
    94  				string('\r'),
    95  				1,
    96  			},
    97  			{
    98  				string(rune(0x10)),
    99  				1,
   100  			},
   101  			{
   102  				"你好",
   103  				2,
   104  			},
   105  			{
   106  				"再见",
   107  				2,
   108  			},
   109  			{
   110  				"今天",
   111  				2,
   112  			},
   113  			{
   114  				"日期时间",
   115  				4,
   116  			},
   117  			{
   118  				"明天",
   119  				2,
   120  			},
   121  			{
   122  				"\n\t\r\b" + string(rune(0)) + "\\_\\%\\",
   123  				10,
   124  			},
   125  		}
   126  
   127  		var input []string
   128  		var output []uint64
   129  		for _, k := range kases {
   130  			input = append(input, k.s)
   131  			output = append(output, k.want)
   132  		}
   133  
   134  		ivec := testutil.MakeVarcharVector(input, nil)
   135  		wantvec := testutil.MakeUint64Vector(output, nil)
   136  		proc := testutil.NewProc()
   137  		ovec, err := LengthUTF8([]*vector.Vector{ivec}, proc)
   138  		convey.So(err, convey.ShouldBeNil)
   139  		ret := testutil.CompareVectors(wantvec, ovec)
   140  		convey.So(ret, convey.ShouldBeTrue)
   141  	})
   142  	convey.Convey("null", t, func() {
   143  		ivec := testutil.MakeScalarNull(types.T_char, 10)
   144  		wantvec := testutil.MakeScalarNull(types.T_uint64, 10)
   145  		proc := testutil.NewProc()
   146  		ovec, err := LengthUTF8([]*vector.Vector{ivec}, proc)
   147  		convey.So(err, convey.ShouldBeNil)
   148  		ret := testutil.CompareVectors(wantvec, ovec)
   149  		convey.So(ret, convey.ShouldBeTrue)
   150  
   151  	})
   152  
   153  	convey.Convey("scalar", t, func() {
   154  		ivec := testutil.MakeScalarVarchar("abc", 5)
   155  		wantvec := testutil.MakeScalarUint64(3, 5)
   156  		proc := testutil.NewProc()
   157  		ovec, err := LengthUTF8([]*vector.Vector{ivec}, proc)
   158  		convey.So(err, convey.ShouldBeNil)
   159  		ret := testutil.CompareVectors(wantvec, ovec)
   160  		convey.So(ret, convey.ShouldBeTrue)
   161  	})
   162  }