-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathbuiltin_string_test.go
100 lines (94 loc) · 2.85 KB
/
builtin_string_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)
func (s *testEvaluatorSuite) TestLengthAndOctetLength(c *C) {
cases := []struct {
args interface{}
expected int64
isNil bool
getErr bool
}{
{"abc", 3, false, false},
{"你好", 6, false, false},
{1, 1, false, false},
{3.14, 4, false, false},
{nil, 0, true, false},
{errors.New("must error"), 0, false, true},
}
lengthMethods := []string{ast.Length, ast.OctetLength}
for _, lengthMethod := range lengthMethods {
for _, t := range cases {
f, err := newFunctionForTest(s.ctx, lengthMethod, s.primitiveValsToConstants([]interface{}{t.args})...)
c.Assert(err, IsNil)
d, err := f.Eval(chunk.Row{})
if t.getErr {
c.Assert(err, NotNil)
} else {
c.Assert(err, IsNil)
if t.isNil {
c.Assert(d.Kind(), Equals, types.KindNull)
} else {
c.Assert(d.GetInt64(), Equals, t.expected)
}
}
}
}
_, err := funcs[ast.Length].getFunction(s.ctx, []Expression{Zero})
c.Assert(err, IsNil)
}
func (s *testEvaluatorSuite) TestStrcmp(c *C) {
cases := []struct {
args []interface{}
isNil bool
getErr bool
res int64
}{
{[]interface{}{"123", "123"}, false, false, 0},
{[]interface{}{"123", "1"}, false, false, 1},
{[]interface{}{"1", "123"}, false, false, -1},
{[]interface{}{"123", "45"}, false, false, -1},
{[]interface{}{123, "123"}, false, false, 0},
{[]interface{}{"12.34", 12.34}, false, false, 0},
{[]interface{}{nil, "123"}, true, false, 0},
{[]interface{}{"123", nil}, true, false, 0},
{[]interface{}{"", "123"}, false, false, -1},
{[]interface{}{"123", ""}, false, false, 1},
{[]interface{}{"", ""}, false, false, 0},
{[]interface{}{"", nil}, true, false, 0},
{[]interface{}{nil, ""}, true, false, 0},
{[]interface{}{nil, nil}, true, false, 0},
{[]interface{}{"123", errors.New("must err")}, false, true, 0},
}
for _, t := range cases {
f, err := newFunctionForTest(s.ctx, ast.Strcmp, s.primitiveValsToConstants(t.args)...)
c.Assert(err, IsNil)
d, err := f.Eval(chunk.Row{})
if t.getErr {
c.Assert(err, NotNil)
} else {
c.Assert(err, IsNil)
if t.isNil {
c.Assert(d.Kind(), Equals, types.KindNull)
} else {
c.Assert(d.GetInt64(), Equals, t.res)
}
}
}
}