github.com/camlistore/go4@v0.0.0-20200104003542-c7e774b10ea0/strutil/strutil_test.go (about) 1 /* 2 Copyright 2013 The Perkeep Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package strutil 18 19 import ( 20 "reflect" 21 "strings" 22 "testing" 23 ) 24 25 func TestAppendSplitN(t *testing.T) { 26 var got []string 27 tests := []struct { 28 s, sep string 29 n int 30 }{ 31 {"foo", "|", 1}, 32 {"foo", "|", -1}, 33 {"foo|bar", "|", 1}, 34 {"foo|bar", "|", -1}, 35 {"foo|bar|", "|", 2}, 36 {"foo|bar|", "|", -1}, 37 {"foo|bar|baz", "|", 1}, 38 {"foo|bar|baz", "|", 2}, 39 {"foo|bar|baz", "|", 3}, 40 {"foo|bar|baz", "|", -1}, 41 } 42 for _, tt := range tests { 43 want := strings.SplitN(tt.s, tt.sep, tt.n) 44 got = AppendSplitN(got[:0], tt.s, tt.sep, tt.n) 45 if !reflect.DeepEqual(want, got) { 46 t.Errorf("AppendSplitN(%q, %q, %d) = %q; want %q", 47 tt.s, tt.sep, tt.n, got, want) 48 } 49 } 50 } 51 52 func TestStringFromBytes(t *testing.T) { 53 for _, s := range []string{"foo", "permanode", "file", "zzzz"} { 54 got := StringFromBytes([]byte(s)) 55 if got != s { 56 t.Errorf("StringFromBytes(%q) didn't round-trip; got %q instead", s, got) 57 } 58 } 59 } 60 61 func TestHasPrefixFold(t *testing.T) { 62 tests := []struct { 63 s, prefix string 64 result bool 65 }{ 66 {"camli", "CAML", true}, 67 {"CAMLI", "caml", true}, 68 {"cam", "Cam", true}, 69 {"camli", "car", false}, 70 {"caml", "camli", false}, 71 {"Hello, 世界 dasdsa", "HeLlO, 世界", true}, 72 {"Hello, 世界", "HeLlO, 世界-", false}, 73 74 {"kelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign 75 {"Kelvin", "\u212A" + "elvin", true}, 76 {"kelvin", "\u212A" + "el", true}, 77 {"Kelvin", "\u212A" + "el", true}, 78 {"\u212A" + "elvin", "Kelvin", true}, 79 {"\u212A" + "elvin", "kelvin", true}, 80 {"\u212A" + "elvin", "Kel", true}, 81 {"\u212A" + "elvin", "kel", true}, 82 } 83 for _, tt := range tests { 84 r := HasPrefixFold(tt.s, tt.prefix) 85 if r != tt.result { 86 t.Errorf("HasPrefixFold(%q, %q) returned %v", tt.s, tt.prefix, r) 87 } 88 } 89 } 90 91 func TestHasSuffixFold(t *testing.T) { 92 tests := []struct { 93 s, suffix string 94 result bool 95 }{ 96 {"camli", "AMLI", true}, 97 {"CAMLI", "amli", true}, 98 {"mli", "MLI", true}, 99 {"camli", "ali", false}, 100 {"amli", "camli", false}, 101 {"asas Hello, 世界", "HeLlO, 世界", true}, 102 {"Hello, 世界", "HeLlO, 世界-", false}, 103 {"KkkkKKkelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign 104 105 {"kelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign 106 {"Kelvin", "\u212A" + "elvin", true}, 107 {"\u212A" + "elvin", "Kelvin", true}, 108 {"\u212A" + "elvin", "kelvin", true}, 109 {"\u212A" + "elvin", "vin", true}, 110 {"\u212A" + "elvin", "viN", true}, 111 } 112 for _, tt := range tests { 113 r := HasSuffixFold(tt.s, tt.suffix) 114 if r != tt.result { 115 t.Errorf("HasSuffixFold(%q, %q) returned %v", tt.s, tt.suffix, r) 116 } 117 } 118 } 119 120 func TestContainsFold(t *testing.T) { 121 // TODO: more tests, more languages. 122 tests := []struct { 123 s, substr string 124 result bool 125 }{ 126 {"camli", "CAML", true}, 127 {"CAMLI", "caml", true}, 128 {"cam", "Cam", true}, 129 {"мир", "ми", true}, 130 {"МИP", "ми", true}, 131 {"КАМЛИЙСТОР", "камлийс", true}, 132 {"КаМлИйСтОр", "КаМлИйС", true}, 133 {"camli", "car", false}, 134 {"caml", "camli", false}, 135 136 {"camli", "AMLI", true}, 137 {"CAMLI", "amli", true}, 138 {"mli", "MLI", true}, 139 {"мир", "ир", true}, 140 {"МИP", "ми", true}, 141 {"КАМЛИЙСТОР", "лийстор", true}, 142 {"КаМлИйСтОр", "лИйСтОр", true}, 143 {"мир", "р", true}, 144 {"camli", "ali", false}, 145 {"amli", "camli", false}, 146 147 {"МИP", "и", true}, 148 {"мир", "и", true}, 149 {"КАМЛИЙСТОР", "лийс", true}, 150 {"КаМлИйСтОр", "лИйС", true}, 151 152 {"árvíztűrő tükörfúrógép", "árvíztŰrŐ", true}, 153 {"I love ☕", "i love ☕", true}, 154 155 {"k", "\u212A", true}, // "\u212A" is the Kelvin temperature sign 156 {"\u212A" + "elvin", "k", true}, 157 {"kelvin", "\u212A" + "elvin", true}, 158 {"Kelvin", "\u212A" + "elvin", true}, 159 {"\u212A" + "elvin", "Kelvin", true}, 160 {"\u212A" + "elvin", "kelvin", true}, 161 {"273.15 kelvin", "\u212A" + "elvin", true}, 162 {"273.15 Kelvin", "\u212A" + "elvin", true}, 163 {"273.15 \u212A" + "elvin", "Kelvin", true}, 164 {"273.15 \u212A" + "elvin", "kelvin", true}, 165 } 166 for _, tt := range tests { 167 r := ContainsFold(tt.s, tt.substr) 168 if r != tt.result { 169 t.Errorf("ContainsFold(%q, %q) returned %v", tt.s, tt.substr, r) 170 } 171 } 172 } 173 174 func TestIsPlausibleJSON(t *testing.T) { 175 tests := []struct { 176 in string 177 want bool 178 }{ 179 {"{}", true}, 180 {" {}", true}, 181 {"{} ", true}, 182 {"\n\r\t {}\t \r \n", true}, 183 184 {"\n\r\t {x\t \r \n", false}, 185 {"{x", false}, 186 {"x}", false}, 187 {"x", false}, 188 {"", false}, 189 } 190 for _, tt := range tests { 191 got := IsPlausibleJSON(tt.in) 192 if got != tt.want { 193 t.Errorf("IsPlausibleJSON(%q) = %v; want %v", tt.in, got, tt.want) 194 } 195 } 196 } 197 198 func BenchmarkHasSuffixFoldToLower(tb *testing.B) { 199 a, b := "camlik", "AMLI\u212A" 200 for i := 0; i < tb.N; i++ { 201 if !strings.HasSuffix(strings.ToLower(a), strings.ToLower(b)) { 202 tb.Fatalf("%q should have the same suffix as %q", a, b) 203 } 204 } 205 } 206 func BenchmarkHasSuffixFold(tb *testing.B) { 207 a, b := "camlik", "AMLI\u212A" 208 for i := 0; i < tb.N; i++ { 209 if !HasSuffixFold(a, b) { 210 tb.Fatalf("%q should have the same suffix as %q", a, b) 211 } 212 } 213 } 214 215 func BenchmarkHasPrefixFoldToLower(tb *testing.B) { 216 a, b := "kamlistore", "\u212AAMLI" 217 for i := 0; i < tb.N; i++ { 218 if !strings.HasPrefix(strings.ToLower(a), strings.ToLower(b)) { 219 tb.Fatalf("%q should have the same suffix as %q", a, b) 220 } 221 } 222 } 223 func BenchmarkHasPrefixFold(tb *testing.B) { 224 a, b := "kamlistore", "\u212AAMLI" 225 for i := 0; i < tb.N; i++ { 226 if !HasPrefixFold(a, b) { 227 tb.Fatalf("%q should have the same suffix as %q", a, b) 228 } 229 } 230 }