github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/func_builtin_regexp_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 function
    16  
    17  import (
    18  	"github.com/stretchr/testify/require"
    19  	"testing"
    20  )
    21  
    22  func Test_BuiltIn_RegularInstr(t *testing.T) {
    23  	op := newOpBuiltInRegexp()
    24  
    25  	cs := []struct {
    26  		pat       string
    27  		str       string
    28  		pos       int64
    29  		ocr       int64
    30  		retOption int8
    31  		expected  int64
    32  	}{
    33  		{pat: "at", str: "Cat", pos: 1, ocr: 1, retOption: 0, expected: 2},
    34  		{pat: "^at", str: "at", pos: 1, ocr: 1, retOption: 0, expected: 1},
    35  		{pat: "Cat", str: "Cat Cat", pos: 2, ocr: 1, retOption: 0, expected: 5},
    36  		{pat: "Cat", str: "Cat Cat", pos: 3, ocr: 1, retOption: 0, expected: 5},
    37  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 1, ocr: 1, retOption: 0, expected: 1},
    38  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 2, ocr: 1, retOption: 0, expected: 5},
    39  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 6, ocr: 1, retOption: 0, expected: 16},
    40  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 1, ocr: 1, retOption: 0, expected: 1},
    41  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 1, ocr: 2, retOption: 0, expected: 5},
    42  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 1, ocr: 3, retOption: 0, expected: 16},
    43  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 2, ocr: 1, retOption: 0, expected: 5},
    44  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 2, ocr: 2, retOption: 0, expected: 16},
    45  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 2, ocr: 3, retOption: 0, expected: 0},
    46  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 1, ocr: 1, retOption: 1, expected: 4},
    47  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 1, ocr: 2, retOption: 1, expected: 8},
    48  		{pat: "C.t", str: "Cat City is SO Cute!", pos: 1, ocr: 3, retOption: 1, expected: 19},
    49  	}
    50  
    51  	for i, c := range cs {
    52  		v, err := op.regMap.regularInstr(c.pat, c.str, c.pos, c.ocr, c.retOption)
    53  		require.NoError(t, err)
    54  		require.Equal(t, c.expected, v, i)
    55  	}
    56  
    57  	_, err := op.regMap.regularInstr("at", "Cat", 100, 1, 0)
    58  	require.True(t, err != nil)
    59  }
    60  
    61  func Test_BuiltIn_RegularLike(t *testing.T) {
    62  	op := newOpBuiltInRegexp()
    63  
    64  	cs := []struct {
    65  		pat       string
    66  		str       string
    67  		matchType string
    68  		expected  bool
    69  	}{
    70  		{pat: ".*", str: "Cat", matchType: "c", expected: true},
    71  		{pat: "b+", str: "Cat", matchType: "c", expected: false},
    72  		{pat: "^Ca", str: "Cat", matchType: "c", expected: true},
    73  		{pat: "^Da", str: "Cat", matchType: "c", expected: false},
    74  		{pat: "cat", str: "Cat", matchType: "", expected: false},
    75  		{pat: "cat", str: "Cat", matchType: "i", expected: true},
    76  		{pat: ".", str: "\n", matchType: "", expected: false},
    77  		{pat: ".", str: "\n", matchType: "n", expected: true},
    78  		{pat: "last$", str: "last\nday", matchType: "", expected: false},
    79  		{pat: "last$", str: "last\nday", matchType: "m", expected: true},
    80  		{pat: "abc", str: "ABC", matchType: "icicc", expected: false},
    81  		{pat: "abc", str: "ABC", matchType: "ccici", expected: true},
    82  	}
    83  
    84  	for i, c := range cs {
    85  		match, err := op.regMap.regularLike(c.pat, c.str, c.matchType)
    86  		require.NoError(t, err, i)
    87  		require.Equal(t, c.expected, match, i)
    88  	}
    89  
    90  }
    91  
    92  func Test_BuiltIn_RegularReplace(t *testing.T) {
    93  	op := newOpBuiltInRegexp()
    94  
    95  	cs := []struct {
    96  		pat      string
    97  		str      string
    98  		repl     string
    99  		pos      int64
   100  		ocr      int64
   101  		expected string
   102  	}{
   103  		{pat: "[0-9]", str: "1abc2", repl: "#", pos: 1, ocr: 1, expected: "#abc2"},
   104  		{pat: "[0-9]", str: "12abc", repl: "#", pos: 2, ocr: 1, expected: "1#abc"},
   105  		{pat: "[0-9]", str: "01234abcde56789", repl: "#", pos: 1, ocr: 1, expected: "#1234abcde56789"},
   106  		{pat: "[09]", str: "01234abcde56789", repl: "#", pos: 1, ocr: 1, expected: "#1234abcde56789"},
   107  		{pat: "[0-9]", str: "abcdefg123456ABC", repl: "", pos: 4, ocr: 0, expected: "abcdefgABC"},
   108  		{pat: "[0-9]", str: "abcDEfg123456ABC", repl: "", pos: 4, ocr: 0, expected: "abcDEfgABC"},
   109  		{pat: "[0-9]", str: "abcDEfg123456ABC", repl: "", pos: 7, ocr: 0, expected: "abcDEfgABC"},
   110  		{pat: "[0-9]", str: "abcDefg123456ABC", repl: "", pos: 10, ocr: 0, expected: "abcDefgABC"},
   111  	}
   112  
   113  	for i, c := range cs {
   114  		val, err := op.regMap.regularReplace(c.pat, c.str, c.repl, c.pos, c.ocr)
   115  		require.NoError(t, err, i)
   116  		require.Equal(t, c.expected, val, i)
   117  	}
   118  }
   119  
   120  func Test_BuiltIn_RegularSubstr(t *testing.T) {
   121  	op := newOpBuiltInRegexp()
   122  
   123  	cc := []struct {
   124  		pat      string
   125  		str      string
   126  		pos      int64
   127  		ocr      int64
   128  		expected string
   129  	}{
   130  		{pat: "[a-z]+", str: "abc def ghi", pos: 1, ocr: 1, expected: "abc"},
   131  		{pat: "[a-z]+", str: "abc def ghi", pos: 1, ocr: 3, expected: "ghi"},
   132  		{pat: "[a-z]+", str: "java t point", pos: 2, ocr: 3, expected: "point"},
   133  		{pat: "[a-z]+", str: "my sql function", pos: 1, ocr: 3, expected: "function"},
   134  	}
   135  
   136  	for i, c := range cc {
   137  		match, val, err := op.regMap.regularSubstr(c.pat, c.str, c.pos, c.ocr)
   138  		require.NoError(t, err, i)
   139  		require.True(t, match, i)
   140  		require.Equal(t, c.expected, val, i)
   141  	}
   142  }