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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*
Copyright 2013 The Perkeep Authors
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package strutil
import (
"reflect"
"strings"
"testing"
)
func TestAppendSplitN(t *testing.T) {
var got []string
tests := []struct {
s, sep string
n int
}{
{"foo", "|", 1},
{"foo", "|", -1},
{"foo|bar", "|", 1},
{"foo|bar", "|", -1},
{"foo|bar|", "|", 2},
{"foo|bar|", "|", -1},
{"foo|bar|baz", "|", 1},
{"foo|bar|baz", "|", 2},
{"foo|bar|baz", "|", 3},
{"foo|bar|baz", "|", -1},
}
for _, tt := range tests {
want := strings.SplitN(tt.s, tt.sep, tt.n)
got = AppendSplitN(got[:0], tt.s, tt.sep, tt.n)
if !reflect.DeepEqual(want, got) {
t.Errorf("AppendSplitN(%q, %q, %d) = %q; want %q",
tt.s, tt.sep, tt.n, got, want)
}
}
}
func TestStringFromBytes(t *testing.T) {
for _, s := range []string{"foo", "permanode", "file", "zzzz"} {
got := StringFromBytes([]byte(s))
if got != s {
t.Errorf("StringFromBytes(%q) didn't round-trip; got %q instead", s, got)
}
}
}
func TestHasPrefixFold(t *testing.T) {
tests := []struct {
s, prefix string
result bool
}{
{"camli", "CAML", true},
{"CAMLI", "caml", true},
{"cam", "Cam", true},
{"camli", "car", false},
{"caml", "camli", false},
{"Hello, 世界 dasdsa", "HeLlO, 世界", true},
{"Hello, 世界", "HeLlO, 世界-", false},
{"kelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign
{"Kelvin", "\u212A" + "elvin", true},
{"kelvin", "\u212A" + "el", true},
{"Kelvin", "\u212A" + "el", true},
{"\u212A" + "elvin", "Kelvin", true},
{"\u212A" + "elvin", "kelvin", true},
{"\u212A" + "elvin", "Kel", true},
{"\u212A" + "elvin", "kel", true},
}
for _, tt := range tests {
r := HasPrefixFold(tt.s, tt.prefix)
if r != tt.result {
t.Errorf("HasPrefixFold(%q, %q) returned %v", tt.s, tt.prefix, r)
}
}
}
func TestHasSuffixFold(t *testing.T) {
tests := []struct {
s, suffix string
result bool
}{
{"camli", "AMLI", true},
{"CAMLI", "amli", true},
{"mli", "MLI", true},
{"camli", "ali", false},
{"amli", "camli", false},
{"asas Hello, 世界", "HeLlO, 世界", true},
{"Hello, 世界", "HeLlO, 世界-", false},
{"KkkkKKkelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign
{"kelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign
{"Kelvin", "\u212A" + "elvin", true},
{"\u212A" + "elvin", "Kelvin", true},
{"\u212A" + "elvin", "kelvin", true},
{"\u212A" + "elvin", "vin", true},
{"\u212A" + "elvin", "viN", true},
}
for _, tt := range tests {
r := HasSuffixFold(tt.s, tt.suffix)
if r != tt.result {
t.Errorf("HasSuffixFold(%q, %q) returned %v", tt.s, tt.suffix, r)
}
}
}
func TestContainsFold(t *testing.T) {
// TODO: more tests, more languages.
tests := []struct {
s, substr string
result bool
}{
{"camli", "CAML", true},
{"CAMLI", "caml", true},
{"cam", "Cam", true},
{"мир", "ми", true},
{"МИP", "ми", true},
{"КАМЛИЙСТОР", "камлийс", true},
{"КаМлИйСтОр", "КаМлИйС", true},
{"camli", "car", false},
{"caml", "camli", false},
{"camli", "AMLI", true},
{"CAMLI", "amli", true},
{"mli", "MLI", true},
{"мир", "ир", true},
{"МИP", "ми", true},
{"КАМЛИЙСТОР", "лийстор", true},
{"КаМлИйСтОр", "лИйСтОр", true},
{"мир", "р", true},
{"camli", "ali", false},
{"amli", "camli", false},
{"МИP", "и", true},
{"мир", "и", true},
{"КАМЛИЙСТОР", "лийс", true},
{"КаМлИйСтОр", "лИйС", true},
{"árvíztűrő tükörfúrógép", "árvíztŰrŐ", true},
{"I love ☕", "i love ☕", true},
{"k", "\u212A", true}, // "\u212A" is the Kelvin temperature sign
{"\u212A" + "elvin", "k", true},
{"kelvin", "\u212A" + "elvin", true},
{"Kelvin", "\u212A" + "elvin", true},
{"\u212A" + "elvin", "Kelvin", true},
{"\u212A" + "elvin", "kelvin", true},
{"273.15 kelvin", "\u212A" + "elvin", true},
{"273.15 Kelvin", "\u212A" + "elvin", true},
{"273.15 \u212A" + "elvin", "Kelvin", true},
{"273.15 \u212A" + "elvin", "kelvin", true},
}
for _, tt := range tests {
r := ContainsFold(tt.s, tt.substr)
if r != tt.result {
t.Errorf("ContainsFold(%q, %q) returned %v", tt.s, tt.substr, r)
}
}
}
func TestIsPlausibleJSON(t *testing.T) {
tests := []struct {
in string
want bool
}{
{"{}", true},
{" {}", true},
{"{} ", true},
{"\n\r\t {}\t \r \n", true},
{"\n\r\t {x\t \r \n", false},
{"{x", false},
{"x}", false},
{"x", false},
{"", false},
}
for _, tt := range tests {
got := IsPlausibleJSON(tt.in)
if got != tt.want {
t.Errorf("IsPlausibleJSON(%q) = %v; want %v", tt.in, got, tt.want)
}
}
}
func BenchmarkHasSuffixFoldToLower(tb *testing.B) {
a, b := "camlik", "AMLI\u212A"
for i := 0; i < tb.N; i++ {
if !strings.HasSuffix(strings.ToLower(a), strings.ToLower(b)) {
tb.Fatalf("%q should have the same suffix as %q", a, b)
}
}
}
func BenchmarkHasSuffixFold(tb *testing.B) {
a, b := "camlik", "AMLI\u212A"
for i := 0; i < tb.N; i++ {
if !HasSuffixFold(a, b) {
tb.Fatalf("%q should have the same suffix as %q", a, b)
}
}
}
func BenchmarkHasPrefixFoldToLower(tb *testing.B) {
a, b := "kamlistore", "\u212AAMLI"
for i := 0; i < tb.N; i++ {
if !strings.HasPrefix(strings.ToLower(a), strings.ToLower(b)) {
tb.Fatalf("%q should have the same suffix as %q", a, b)
}
}
}
func BenchmarkHasPrefixFold(tb *testing.B) {
a, b := "kamlistore", "\u212AAMLI"
for i := 0; i < tb.N; i++ {
if !HasPrefixFold(a, b) {
tb.Fatalf("%q should have the same suffix as %q", a, b)
}
}
}
|