github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/substring/substring_test.go (about)

     1  // Copyright 2021 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 substring
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestGetSliceFromLeft(t *testing.T) {
    24  	cases := []struct {
    25  		name   string
    26  		bytes  string
    27  		offset int64
    28  		want   string
    29  	}{
    30  		{
    31  			name:   "TEST01",
    32  			bytes:  "abcdefghijklmn",
    33  			offset: 4,
    34  			want:   "efghijklmn",
    35  		},
    36  		{
    37  			name:   "TEST02",
    38  			bytes:  "abcdefghijklmn",
    39  			offset: 6,
    40  			want:   "ghijklmn",
    41  		},
    42  		{
    43  			name:   "Test03",
    44  			bytes:  "abcdefghijklmn",
    45  			offset: 10,
    46  			want:   "klmn",
    47  		},
    48  		{
    49  			name:   "TEST04",
    50  			bytes:  "abcdefghijklmn",
    51  			offset: 15,
    52  			want:   "",
    53  		},
    54  		{
    55  			name:   "TEST05",
    56  			bytes:  "你好asd世界",
    57  			offset: 3,
    58  			want:   "sd世界",
    59  		},
    60  	}
    61  
    62  	for _, c := range cases {
    63  		t.Run(c.name, func(t *testing.T) {
    64  			got := getSliceFromLeft(c.bytes, c.offset)
    65  			require.Equal(t, c.want, got)
    66  		})
    67  	}
    68  }
    69  
    70  func TestGetSliceFromLeftWithLength(t *testing.T) {
    71  	cases := []struct {
    72  		name      string
    73  		bytes     string
    74  		offset    int64
    75  		length    int64
    76  		wantBytes string
    77  	}{
    78  		{
    79  			name:      "Test01",
    80  			bytes:     "abcdefghijklmn",
    81  			offset:    5 - 1,
    82  			length:    6,
    83  			wantBytes: "efghij",
    84  		},
    85  		{
    86  			name:      "Test02",
    87  			bytes:     "abcdefghijklmn",
    88  			offset:    5 - 1,
    89  			length:    10,
    90  			wantBytes: "efghijklmn",
    91  		},
    92  		{
    93  			name:      "Test03",
    94  			bytes:     "abcdefghijklmn",
    95  			offset:    5 - 1,
    96  			length:    0,
    97  			wantBytes: "",
    98  		},
    99  		{
   100  			name:      "Test04",
   101  			bytes:     "abcdefghijklmn",
   102  			offset:    5,
   103  			length:    -8,
   104  			wantBytes: "",
   105  		},
   106  		{
   107  			name:      "Test05",
   108  			bytes:     "abcdefghijklmn",
   109  			offset:    5,
   110  			length:    -9,
   111  			wantBytes: "",
   112  		},
   113  		{
   114  			name:      "Test06",
   115  			bytes:     "abcdefghijklmn",
   116  			offset:    5,
   117  			length:    -4,
   118  			wantBytes: "",
   119  		},
   120  		{
   121  			name:      "Test07",
   122  			bytes:     "abcdefghijklmn",
   123  			offset:    5,
   124  			length:    -1,
   125  			wantBytes: "",
   126  		},
   127  		{
   128  			name:      "Test08",
   129  			bytes:     "abcdefghijklmn",
   130  			offset:    5,
   131  			length:    -1,
   132  			wantBytes: "",
   133  		},
   134  		{
   135  			name:      "Test09",
   136  			bytes:     "明天abcdef我爱你中国",
   137  			offset:    5 - 1,
   138  			length:    6,
   139  			wantBytes: "cdef我爱",
   140  		},
   141  	}
   142  	for _, c := range cases {
   143  		t.Run(c.name, func(t *testing.T) {
   144  			gotBytes := getSliceFromLeftWithLength(c.bytes, c.offset, c.length)
   145  			require.Equal(t, c.wantBytes, gotBytes)
   146  		})
   147  	}
   148  }
   149  
   150  func TestGetSliceFromRight(t *testing.T) {
   151  	cases := []struct {
   152  		name      string
   153  		bytes     []byte
   154  		offset    int64
   155  		wantBytes []byte
   156  		wantLen   int64
   157  	}{
   158  		{
   159  			name:      "Test01",
   160  			bytes:     []byte("abcdefghijklmn"),
   161  			offset:    4,
   162  			wantBytes: []byte("klmn"),
   163  			wantLen:   4,
   164  		},
   165  		{
   166  			name:      "Test02",
   167  			bytes:     []byte("abcdefghijklmn"),
   168  			offset:    14,
   169  			wantBytes: []byte("abcdefghijklmn"),
   170  			wantLen:   14,
   171  		},
   172  		{
   173  			name:      "Test03",
   174  			bytes:     []byte("abcdefghijklmn"),
   175  			offset:    16,
   176  			wantBytes: []byte(""),
   177  			wantLen:   0,
   178  		},
   179  		{
   180  			name:      "Test04",
   181  			bytes:     []byte("abcdef我爱你中国"),
   182  			offset:    7,
   183  			wantBytes: []byte("ef我爱你中国"),
   184  			wantLen:   17,
   185  		},
   186  	}
   187  
   188  	for _, c := range cases {
   189  		t.Run(c.name, func(t *testing.T) {
   190  			gotBytes := getSliceFromRight(string(c.bytes), c.offset)
   191  			require.Equal(t, string(c.wantBytes), gotBytes)
   192  		})
   193  	}
   194  }
   195  
   196  func TestGetSliceFromRightWithLength(t *testing.T) {
   197  	cases := []struct {
   198  		name      string
   199  		bytes     []byte
   200  		offset    int64
   201  		length    int64
   202  		wantBytes []byte
   203  		wantLen   int64
   204  	}{
   205  		{
   206  			name:      "Test01",
   207  			bytes:     []byte("abcdefghijklmn"),
   208  			offset:    4,
   209  			length:    3,
   210  			wantBytes: []byte("klm"),
   211  			wantLen:   3,
   212  		},
   213  		{
   214  			name:      "Test02",
   215  			bytes:     []byte("abcdefghijklmn"),
   216  			offset:    14,
   217  			length:    10,
   218  			wantBytes: []byte("abcdefghij"),
   219  			wantLen:   10,
   220  		},
   221  		{
   222  			name:      "Test03",
   223  			bytes:     []byte("abcdefghijklmn"),
   224  			offset:    14,
   225  			length:    15,
   226  			wantBytes: []byte("abcdefghijklmn"),
   227  			wantLen:   14,
   228  		},
   229  		{
   230  			name:      "Test04",
   231  			bytes:     []byte("abcdefghijklmn"),
   232  			offset:    16,
   233  			length:    10,
   234  			wantBytes: []byte(""),
   235  			wantLen:   0,
   236  		},
   237  		{
   238  			name:      "Test05",
   239  			bytes:     []byte("abcdefghijklmn"),
   240  			offset:    16,
   241  			length:    20,
   242  			wantBytes: []byte(""),
   243  			wantLen:   0,
   244  		},
   245  		{
   246  			name:      "Test06",
   247  			bytes:     []byte("abcdefghijklmn"),
   248  			offset:    16,
   249  			length:    2,
   250  			wantBytes: []byte(""),
   251  			wantLen:   0,
   252  		},
   253  		{
   254  			name:      "Test07",
   255  			bytes:     []byte("abcdefghijklmn"),
   256  			offset:    12,
   257  			length:    2,
   258  			wantBytes: []byte("cd"),
   259  			wantLen:   2,
   260  		},
   261  		{
   262  			name:      "Test08",
   263  			bytes:     []byte("abcdefghijklmn"),
   264  			offset:    12,
   265  			length:    14,
   266  			wantBytes: []byte("cdefghijklmn"),
   267  			wantLen:   12,
   268  		},
   269  		{
   270  			name:      "Test09",
   271  			bytes:     []byte("abcdefghijklmn"),
   272  			offset:    12,
   273  			length:    0,
   274  			wantBytes: []byte(""),
   275  			wantLen:   0,
   276  		},
   277  		{
   278  			name:      "Test10",
   279  			bytes:     []byte("abcdefghijklmn"),
   280  			offset:    6,
   281  			length:    -5,
   282  			wantBytes: []byte(""),
   283  			wantLen:   0,
   284  		},
   285  		{
   286  			name:      "Test10",
   287  			bytes:     []byte("abcdefghijklmn"),
   288  			offset:    6,
   289  			length:    -10,
   290  			wantBytes: []byte(""),
   291  			wantLen:   0,
   292  		},
   293  		{
   294  			name:      "Test11",
   295  			bytes:     []byte("abcdefghijklmn"),
   296  			offset:    6,
   297  			length:    -1,
   298  			wantBytes: []byte(""),
   299  			wantLen:   0,
   300  		},
   301  		{
   302  			name:      "Test12",
   303  			bytes:     []byte("明天abcdef我爱你中国"),
   304  			offset:    8,
   305  			length:    5,
   306  			wantBytes: []byte("def我爱"),
   307  			wantLen:   9,
   308  		},
   309  	}
   310  
   311  	for _, c := range cases {
   312  		t.Run(c.name, func(t *testing.T) {
   313  			gotBytes := getSliceFromRightWithLength(string(c.bytes), c.offset, c.length)
   314  			require.Equal(t, string(c.wantBytes), gotBytes)
   315  		})
   316  	}
   317  }