| 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 strutilscip-go gomod go4.org . `go4.org/strutil`/ |
| 18 | |
| 19 | import ( |
| 20 | "reflectscip-go gomod github.com/golang/go/src go1.13 reflect/" |
| 21 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 22 | "testingscip-go gomod github.com/golang/go/src go1.13 testing/" |
| 23 | ) |
| 24 | |
| 25 | func TestAppendSplitNscip-go gomod go4.org . `go4.org/strutil`/TestAppendSplitN().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 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 := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.SplitNscip-go gomod github.com/golang/go/src go1.13 strings/SplitN().(tt.s, tt.sep, tt.n) |
| 44 | got = AppendSplitNscip-go gomod go4.org . `go4.org/strutil`/AppendSplitN().(got[:0], tt.s, tt.sep, tt.n) |
| 45 | if !reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.13 reflect/DeepEqual().(want, got) { |
| 46 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("AppendSplitN(%q, %q, %d) = %q; want %q", |
| 47 | tt.s, tt.sep, tt.n, got, want) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestStringFromBytesscip-go gomod go4.org . `go4.org/strutil`/TestStringFromBytes().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 53 | for _, s := range []string{"foo", "permanode", "file", "zzzz"} { |
| 54 | got := StringFromBytesscip-go gomod go4.org . `go4.org/strutil`/StringFromBytes().([]byte(s)) |
| 55 | if got != s { |
| 56 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("StringFromBytes(%q) didn't round-trip; got %q instead", s, got) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestHasPrefixFoldscip-go gomod go4.org . `go4.org/strutil`/TestHasPrefixFold().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 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 := HasPrefixFoldscip-go gomod go4.org . `go4.org/strutil`/HasPrefixFold().(tt.s, tt.prefix) |
| 85 | if r != tt.result { |
| 86 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("HasPrefixFold(%q, %q) returned %v", tt.s, tt.prefix, r) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestHasSuffixFoldscip-go gomod go4.org . `go4.org/strutil`/TestHasSuffixFold().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 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 := HasSuffixFoldscip-go gomod go4.org . `go4.org/strutil`/HasSuffixFold().(tt.s, tt.suffix) |
| 114 | if r != tt.result { |
| 115 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("HasSuffixFold(%q, %q) returned %v", tt.s, tt.suffix, r) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestContainsFoldscip-go gomod go4.org . `go4.org/strutil`/TestContainsFold().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 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 := ContainsFoldscip-go gomod go4.org . `go4.org/strutil`/ContainsFold().(tt.s, tt.substr) |
| 168 | if r != tt.result { |
| 169 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("ContainsFold(%q, %q) returned %v", tt.s, tt.substr, r) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestIsPlausibleJSONscip-go gomod go4.org . `go4.org/strutil`/TestIsPlausibleJSON().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 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 := IsPlausibleJSONscip-go gomod go4.org . `go4.org/strutil`/IsPlausibleJSON().(tt.in) |
| 192 | if got != tt.want { |
| 193 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("IsPlausibleJSON(%q) = %v; want %v", tt.in, got, tt.want) |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func BenchmarkHasSuffixFoldToLowerscip-go gomod go4.org . `go4.org/strutil`/BenchmarkHasSuffixFoldToLower().(tb *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 199 | a, b := "camlik", "AMLI\u212A" |
| 200 | for i := 0; i < tb.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 201 | if !stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.13 strings/HasSuffix().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.13 strings/ToLower().(a), stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.13 strings/ToLower().(b)) { |
| 202 | tb.Fatalfscip-go gomod github.com/golang/go/src go1.13 testing/common#Fatalf().("%q should have the same suffix as %q", a, b) |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | func BenchmarkHasSuffixFoldscip-go gomod go4.org . `go4.org/strutil`/BenchmarkHasSuffixFold().(tb *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 207 | a, b := "camlik", "AMLI\u212A" |
| 208 | for i := 0; i < tb.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 209 | if !HasSuffixFoldscip-go gomod go4.org . `go4.org/strutil`/HasSuffixFold().(a, b) { |
| 210 | tb.Fatalfscip-go gomod github.com/golang/go/src go1.13 testing/common#Fatalf().("%q should have the same suffix as %q", a, b) |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | func BenchmarkHasPrefixFoldToLowerscip-go gomod go4.org . `go4.org/strutil`/BenchmarkHasPrefixFoldToLower().(tb *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 216 | a, b := "kamlistore", "\u212AAMLI" |
| 217 | for i := 0; i < tb.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 218 | if !stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.13 strings/ToLower().(a), stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.13 strings/ToLower().(b)) { |
| 219 | tb.Fatalfscip-go gomod github.com/golang/go/src go1.13 testing/common#Fatalf().("%q should have the same suffix as %q", a, b) |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | func BenchmarkHasPrefixFoldscip-go gomod go4.org . `go4.org/strutil`/BenchmarkHasPrefixFold().(tb *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 224 | a, b := "kamlistore", "\u212AAMLI" |
| 225 | for i := 0; i < tb.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 226 | if !HasPrefixFoldscip-go gomod go4.org . `go4.org/strutil`/HasPrefixFold().(a, b) { |
| 227 | tb.Fatalfscip-go gomod github.com/golang/go/src go1.13 testing/common#Fatalf().("%q should have the same suffix as %q", a, b) |
| 228 | } |
| 229 | } |
| 230 | } |