github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/startswith/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 startswith
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  func makeBytes(strs []string) []string {
    23  	return strs
    24  }
    25  
    26  func TestStartsWith(t *testing.T) {
    27  	tests := []struct {
    28  		name   string
    29  		lv, rv []string
    30  		want   []uint8
    31  		rs     []uint8
    32  	}{
    33  		{
    34  			name: "English Match",
    35  			lv:   makeBytes([]string{"Hello", "World", "Hello", "world"}),
    36  			rv:   makeBytes([]string{"He", "Wor", "Hell", "world"}),
    37  			rs:   make([]uint8, 4),
    38  			want: []uint8{1, 1, 1, 1},
    39  		},
    40  		{
    41  			name: "English Mismatch",
    42  			lv:   makeBytes([]string{"Hello", "World", "Hello", "world"}),
    43  			rv:   makeBytes([]string{"Ho", "wor", "Helloo", "abc"}),
    44  			rs:   make([]uint8, 4),
    45  			want: []uint8{0, 0, 0, 0},
    46  		},
    47  		{
    48  			name: "Chinese Match",
    49  			lv:   makeBytes([]string{"你好世界", "世界你好", "你好 世界", "你好,世界"}),
    50  			rv:   makeBytes([]string{"你好", "世", "你好 ", "你好,世界"}),
    51  			rs:   make([]uint8, 4),
    52  			want: []uint8{1, 1, 1, 1},
    53  		},
    54  		{
    55  			name: "Chinese Mismatch",
    56  			lv:   makeBytes([]string{"你好世界", "世界你好", "你好 世界", "你好,世界"}),
    57  			rv:   makeBytes([]string{"世界", "世 界", "你好 世界 ", "你好,世界"}),
    58  			rs:   make([]uint8, 4),
    59  			want: []uint8{0, 0, 0, 0},
    60  		},
    61  		{
    62  			name: "Chinese + English",
    63  			lv:   makeBytes([]string{"你好World", "Hello世界", "你好World", "Hello世界"}),
    64  			rv:   makeBytes([]string{"你好Wor", "Hello世界", "你好world", "Hello界世"}),
    65  			rs:   make([]uint8, 4),
    66  			want: []uint8{1, 1, 0, 0},
    67  		},
    68  		{
    69  			name: "Special Match",
    70  			lv:   makeBytes([]string{"Hello", "  ", " 你好", ""}),
    71  			rv:   makeBytes([]string{"", " ", " 你", ""}),
    72  			rs:   make([]uint8, 4),
    73  			want: []uint8{1, 1, 1, 1},
    74  		},
    75  	}
    76  
    77  	for _, tt := range tests {
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			if got := StartsWith(tt.lv, tt.rv, tt.rs); !reflect.DeepEqual(got, tt.want) {
    80  				t.Errorf("StartsWith() = %v, want %v", got, tt.want)
    81  			}
    82  		})
    83  	}
    84  }
    85  
    86  func TestStartsWithRightConst(t *testing.T) {
    87  	tests := []struct {
    88  		name   string
    89  		lv, rv []string
    90  		want   []uint8
    91  		rs     []uint8
    92  	}{
    93  		{
    94  			name: "Right Const",
    95  			lv:   makeBytes([]string{"H", "He", "Hello", "world"}),
    96  			rv:   makeBytes([]string{"He"}),
    97  			rs:   make([]uint8, 4),
    98  			want: []uint8{0, 1, 1, 0},
    99  		},
   100  	}
   101  
   102  	for _, tt := range tests {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			if got := StartsWithRightConst(tt.lv, tt.rv[0], tt.rs); !reflect.DeepEqual(got, tt.want) {
   105  				t.Errorf("StartsWithRightConst() = %v, want %v", got, tt.want)
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func TestStartsWithLeftConst(t *testing.T) {
   112  	tests := []struct {
   113  		name   string
   114  		lv, rv []string
   115  		want   []uint8
   116  		rs     []uint8
   117  	}{
   118  		{
   119  			name: "Left Const",
   120  			lv:   makeBytes([]string{"Hello"}),
   121  			rv:   makeBytes([]string{"He", "Hello", "", "Helloo"}),
   122  			rs:   make([]uint8, 4),
   123  			want: []uint8{1, 1, 1, 0},
   124  		},
   125  	}
   126  
   127  	for _, tt := range tests {
   128  		t.Run(tt.name, func(t *testing.T) {
   129  			if got := StartsWithLeftConst(tt.lv[0], tt.rv, tt.rs); !reflect.DeepEqual(got, tt.want) {
   130  				t.Errorf("StartsWithLeftConst() = %v, want %v", got, tt.want)
   131  			}
   132  		})
   133  	}
   134  }
   135  
   136  func TestStartsWithAllConst(t *testing.T) {
   137  	tests := []struct {
   138  		name   string
   139  		lv, rv []string
   140  		want   []uint8
   141  		rs     []uint8
   142  	}{
   143  		{
   144  			name: "All Const",
   145  			lv:   makeBytes([]string{"Hello"}),
   146  			rv:   makeBytes([]string{"He"}),
   147  			rs:   make([]uint8, 1),
   148  			want: []uint8{1},
   149  		},
   150  		{
   151  			name: "All Const2",
   152  			lv:   makeBytes([]string{"Hello"}),
   153  			rv:   makeBytes([]string{"World"}),
   154  			rs:   make([]uint8, 1),
   155  			want: []uint8{0},
   156  		},
   157  	}
   158  
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			if got := StartsWithAllConst(tt.lv[0], tt.rv[0], tt.rs); !reflect.DeepEqual(got, tt.want) {
   162  				t.Errorf("StartsWithAllConst() = %v, want %v", got, tt.want)
   163  			}
   164  		})
   165  	}
   166  }