github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/subStrIndex/subStr_Index_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 substrindex 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestSubStringIndex(t *testing.T) { 24 cases := []struct { 25 name string 26 bytes string 27 delim string 28 count int64 29 want string 30 }{ 31 { 32 name: "TEST01", 33 bytes: "www.mysql.com", 34 delim: ".", 35 count: 0, 36 want: "", 37 }, 38 { 39 name: "TEST02", 40 bytes: "www.mysql.com", 41 delim: ".", 42 count: 1, 43 want: "www", 44 }, 45 { 46 name: "TEST03", 47 bytes: "www.mysql.com", 48 delim: ".", 49 count: 2, 50 want: "www.mysql", 51 }, 52 { 53 name: "TEST04", 54 bytes: "www.mysql.com", 55 delim: ".", 56 count: 3, 57 want: "www.mysql.com", 58 }, 59 { 60 name: "TEST05", 61 bytes: "www.mysql.com", 62 delim: ".", 63 count: -3, 64 want: "www.mysql.com", 65 }, 66 { 67 name: "TEST06", 68 bytes: "www.mysql.com", 69 delim: ".", 70 count: -2, 71 want: "mysql.com", 72 }, 73 { 74 name: "TEST07", 75 bytes: "www.mysql.com", 76 delim: ".", 77 count: -1, 78 want: "com", 79 }, 80 { 81 name: "TEST08", 82 bytes: "xyz", 83 delim: "abc", 84 count: 223372036854775808, 85 want: "xyz", 86 }, 87 { 88 name: "TEST09", 89 bytes: "aaa.bbb.ccc.ddd.eee", 90 delim: ".", 91 count: 9223372036854775807, 92 want: "aaa.bbb.ccc.ddd.eee", 93 }, 94 { 95 name: "TEST10", 96 bytes: "aaa.bbb.ccc.ddd.eee", 97 delim: ".", 98 count: -9223372036854775808, 99 want: "aaa.bbb.ccc.ddd.eee", 100 }, 101 { 102 name: "TEST11", 103 bytes: "aaa.bbb.ccc.ddd.eee", 104 delim: ".", 105 count: int64(922337203685477580), 106 want: "aaa.bbb.ccc.ddd.eee", 107 }, 108 } 109 110 for _, c := range cases { 111 t.Run(c.name, func(t *testing.T) { 112 got, _ := subStrIndex(c.bytes, c.delim, c.count) 113 require.Equal(t, c.want, got) 114 }) 115 } 116 }