github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/left/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 left
    16  
    17  import (
    18  	"github.com/smartystreets/goconvey/convey"
    19  	"github.com/stretchr/testify/require"
    20  	"testing"
    21  )
    22  
    23  func TestLeft(t *testing.T) {
    24  	convey.Convey("TestLeft", t, func() {
    25  		type kase struct {
    26  			s    string
    27  			len  int64
    28  			want string
    29  		}
    30  		kases := []kase{
    31  			{
    32  				"abcde",
    33  				3,
    34  				"abc",
    35  			},
    36  			{
    37  				"abcde",
    38  				0,
    39  				"",
    40  			},
    41  			{
    42  				"abcde",
    43  				-1,
    44  				"",
    45  			},
    46  			{
    47  				"abcde",
    48  				100,
    49  				"abcde",
    50  			},
    51  			{
    52  				"foobarbar",
    53  				5,
    54  				"fooba",
    55  			},
    56  		}
    57  		var inStrs []string
    58  		var lens []int64
    59  		var outStrs []string
    60  		for _, k := range kases {
    61  			inStrs = append(inStrs, k.s)
    62  			lens = append(lens, k.len)
    63  			outStrs = append(outStrs, k.want)
    64  		}
    65  
    66  		res := make([]string, len(inStrs))
    67  		Left(inStrs, lens, res)
    68  
    69  		require.Equal(t, outStrs, res)
    70  	})
    71  }
    72  
    73  func TestLeftAllConst(t *testing.T) {
    74  	convey.Convey("TestLeftAllConst", t, func() {
    75  		type kase struct {
    76  			s    string
    77  			len  int64
    78  			want string
    79  		}
    80  		kases := []kase{
    81  			{
    82  				"foobarbar",
    83  				5,
    84  				"fooba",
    85  			},
    86  		}
    87  		var inStrs []string
    88  		var lens []int64
    89  		var outStrs []string
    90  		for _, k := range kases {
    91  			inStrs = append(inStrs, k.s)
    92  			lens = append(lens, k.len)
    93  			outStrs = append(outStrs, k.want)
    94  		}
    95  
    96  		res := make([]string, len(inStrs))
    97  		LeftAllConst(inStrs, lens, res)
    98  
    99  		require.Equal(t, outStrs, res)
   100  	})
   101  }
   102  
   103  func TestLeftLeftConst(t *testing.T) {
   104  	convey.Convey("TestLeftLeftConst", t, func() {
   105  		type kase struct {
   106  			s    string
   107  			len  int64
   108  			want string
   109  		}
   110  		kases := []kase{
   111  			{
   112  				"abcde",
   113  				3,
   114  				"abc",
   115  			},
   116  			{
   117  				"abcde",
   118  				0,
   119  				"",
   120  			},
   121  			{
   122  				"abcde",
   123  				-1,
   124  				"",
   125  			},
   126  			{
   127  				"abcde",
   128  				100,
   129  				"abcde",
   130  			},
   131  		}
   132  		var inStrs []string
   133  		var lens []int64
   134  		var outStrs []string
   135  		for _, k := range kases {
   136  			inStrs = append(inStrs, k.s)
   137  			lens = append(lens, k.len)
   138  			outStrs = append(outStrs, k.want)
   139  		}
   140  
   141  		res := make([]string, len(inStrs))
   142  		LeftLeftConst(inStrs, lens, res)
   143  
   144  		require.Equal(t, outStrs, res)
   145  	})
   146  }
   147  
   148  func TestLeftRightConst(t *testing.T) {
   149  	convey.Convey("TestLeftRightConst", t, func() {
   150  		type kase struct {
   151  			s    string
   152  			len  int64
   153  			want string
   154  		}
   155  		kases := []kase{
   156  			{
   157  				"abcde",
   158  				3,
   159  				"abc",
   160  			},
   161  			{
   162  				"是都方式快递费",
   163  				3,
   164  				"是都方",
   165  			},
   166  			{
   167  				"アイウエオ",
   168  				3,
   169  				"アイウ",
   170  			},
   171  			{
   172  				"あいうえお",
   173  				3,
   174  				"あいう",
   175  			},
   176  			{
   177  				"foobarbar",
   178  				3,
   179  				"foo",
   180  			},
   181  		}
   182  		var inStrs []string
   183  		var lens []int64
   184  		var outStrs []string
   185  		for _, k := range kases {
   186  			inStrs = append(inStrs, k.s)
   187  			lens = append(lens, k.len)
   188  			outStrs = append(outStrs, k.want)
   189  		}
   190  
   191  		res := make([]string, len(inStrs))
   192  		LeftRightConst(inStrs, lens, res)
   193  
   194  		require.Equal(t, outStrs, res)
   195  	})
   196  }