This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutil_test.go
153 lines (144 loc) · 5.83 KB
/
util_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package tengo
import (
"fmt"
"strconv"
"strings"
"testing"
)
func TestSplitHostOptionalPort(t *testing.T) {
assertSplit := func(addr, expectHost string, expectPort int, expectErr bool) {
host, port, err := SplitHostOptionalPort(addr)
if host != expectHost {
t.Errorf("Expected SplitHostOptionalPort(\"%s\") to return host of \"%s\", instead found \"%s\"", addr, expectHost, host)
}
if port != expectPort {
t.Errorf("Expected SplitHostOptionalPort(\"%s\") to return port of %d, instead found %d", addr, expectPort, port)
}
if expectErr && err == nil {
t.Errorf("Expected SplitHostOptionalPort(\"%s\") to return an error, but instead found nil", addr)
} else if !expectErr && err != nil {
t.Errorf("Expected SplitHostOptionalPort(\"%s\") to return NOT return an error, but instead found %s", addr, err)
}
}
assertSplit("", "", 0, true)
assertSplit("foo", "foo", 0, false)
assertSplit("1.2.3.4", "1.2.3.4", 0, false)
assertSplit("some.host:1234", "some.host", 1234, false)
assertSplit("some.host:text", "", 0, true)
assertSplit("some.host:1234:5678", "", 0, true)
assertSplit("some.host:0", "", 0, true)
assertSplit("some.host:-5", "", 0, true)
assertSplit("fe80::1", "", 0, true)
assertSplit("[fe80::1]", "[fe80::1]", 0, false)
assertSplit("[fe80::1]:3306", "[fe80::1]", 3306, false)
assertSplit("[fe80::bd0f:a8bc:6480:238b%11]", "[fe80::bd0f:a8bc:6480:238b%11]", 0, false)
assertSplit("[fe80::bd0f:a8bc:6480:238b%11]:443", "[fe80::bd0f:a8bc:6480:238b%11]", 443, false)
assertSplit("[fe80::bd0f:a8bc:6480:238b%11]:sup", "", 0, true)
assertSplit("[fe80::bd0f:a8bc:6480:238b%11]:123:456", "", 0, true)
}
func TestParseCreateAutoInc(t *testing.T) {
// With auto-inc value <= 1, no AUTO_INCREMENT=%d clause will be put into the
// test table's create statement
table := aTable(1)
stmt := table.CreateStatement
if strings.Contains(stmt, "AUTO_INCREMENT=") {
t.Fatal("Assertion failed in test setup: CreateStatement unexpectedly contains an AUTO_INCREMENT clause")
}
strippedStmt, nextAutoInc := ParseCreateAutoInc(stmt)
if strippedStmt != stmt || nextAutoInc > 0 {
t.Error("Incorrect result parsing CREATE TABLE")
}
table = aTable(123)
stmt = table.CreateStatement
if !strings.Contains(stmt, "AUTO_INCREMENT=") {
t.Fatal("Assertion failed in test setup: CreateStatement does NOT contain expected AUTO_INCREMENT clause")
}
strippedStmt, nextAutoInc = ParseCreateAutoInc(stmt)
if strings.Contains(strippedStmt, "AUTO_INCREMENT=") {
t.Error("Failed to remove AUTO_INCREMENT clause from create statement")
}
if nextAutoInc != 123 {
t.Errorf("Failed to properly parse AUTO_INCREMENT value: expected 123, found %d", nextAutoInc)
}
}
func TestReformatCreateOptions(t *testing.T) {
cases := map[string]string{
"": "",
"partitioned": "",
"partitioned stats_persistent=1": "STATS_PERSISTENT=1",
"stats_persistent=1 partitioned": "STATS_PERSISTENT=1",
"row_format=DYNAMIC stats_auto_recalc=1": "ROW_FORMAT=DYNAMIC STATS_AUTO_RECALC=1",
"COMPRESSION=\"zLIB\"": "COMPRESSION='zLIB'", // MySQL style page compression
"`PAGE_compressed`=1 `page_compression_LEVEL`=9": "`PAGE_compressed`=1 `page_compression_LEVEL`=9", // MariaDB style page compression
}
for input, expected := range cases {
if actual := reformatCreateOptions(input); actual != expected {
t.Errorf("Expected reformatCreateOptions(%q) to yield %q, instead found %q", input, expected, actual)
}
}
}
func TestNormalizeCreateOptions(t *testing.T) {
input := "CREATE TABLE `problems` (\n" +
" `name` varchar(30) /*!50606 STORAGE MEMORY */ /*!50606 COLUMN_FORMAT DYNAMIC */ DEFAULT NULL,\n" +
" `code` char(20),\n" +
" `num` int(10) unsigned NOT NULL /*!50606 STORAGE DISK */ /*!50606 COLUMN_FORMAT FIXED */,\n" +
" KEY `idx1` (`name`) USING HASH KEY_BLOCK_SIZE=4 COMMENT 'lol',\n" +
" KEY `idx2` (`num`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=latin1 KEY_BLOCK_SIZE=8;\n"
expect := "CREATE TABLE `problems` (\n" +
" `name` varchar(30) DEFAULT NULL,\n" +
" `code` char(20),\n" +
" `num` int(10) unsigned NOT NULL,\n" +
" KEY `idx1` (`name`) COMMENT 'lol',\n" +
" KEY `idx2` (`num`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=latin1 KEY_BLOCK_SIZE=8;\n"
if actual := NormalizeCreateOptions(input); actual != expect {
t.Errorf("NormalizeCreateOptions returned unexpected value. Expected:\n%s\nActual:\n%s", expect, actual)
}
}
func TestStripDisplayWidth(t *testing.T) {
cases := map[string]string{
"tinyint(1)": "tinyint(1)",
"tinyint(2)": "tinyint",
"tinyint(1) unsigned": "tinyint unsigned",
"YEAR(4)": "year",
"YEAR": "YEAR",
"int(11)": "int",
"int(11) zerofill": "int(11) zerofill",
"int(10) unsigned": "int unsigned",
"bigint(20)": "bigint",
}
for input, expected := range cases {
if actual := StripDisplayWidth(input); actual != expected {
t.Errorf("Expected StripDisplayWidth(%q) to return %q, instead found %q", input, expected, actual)
}
}
}
func TestLongestIncreasingSubsequence(t *testing.T) {
cases := map[string]string{
"": "",
"3": "3",
"1 4": "1 4",
"4 1": "1",
"1 2 6 3 4": "1 2 3 4",
"5 4 3 2 1": "1",
"5 6 4 1 2 3": "1 2 3",
}
for inputStr, expectedStr := range cases {
var input []int
for _, inp := range strings.Split(inputStr, " ") {
if inp != "" {
i, _ := strconv.Atoi(inp)
input = append(input, i)
}
}
actual := longestIncreasingSubsequence(input)
var actualStrs []string
for _, out := range actual {
actualStrs = append(actualStrs, fmt.Sprintf("%d", out))
}
if actualStr := strings.Join(actualStrs, " "); actualStr != expectedStr {
t.Errorf("Expected longestIncreasingSubsequence(%s) to return %s, instead found %s", inputStr, expectedStr, actualStr)
}
}
}