github.com/jxskiss/gopkg@v0.17.3/strutil/strutil_test.go (about) 1 package strutil 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestHasPrefixFold(t *testing.T) { 9 tests := []struct { 10 s, prefix string 11 result bool 12 }{ 13 {"camli", "CAML", true}, 14 {"CAMLI", "caml", true}, 15 {"cam", "Cam", true}, 16 {"camli", "car", false}, 17 {"caml", "camli", false}, 18 {"Hello, 世界 dasdsa", "HeLlO, 世界", true}, 19 {"Hello, 世界", "HeLlO, 世界-", false}, 20 21 {"kelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign 22 {"Kelvin", "\u212A" + "elvin", true}, 23 {"kelvin", "\u212A" + "el", true}, 24 {"Kelvin", "\u212A" + "el", true}, 25 {"\u212A" + "elvin", "Kelvin", true}, 26 {"\u212A" + "elvin", "kelvin", true}, 27 {"\u212A" + "elvin", "Kel", true}, 28 {"\u212A" + "elvin", "kel", true}, 29 } 30 for _, tt := range tests { 31 r := HasPrefixFold(tt.s, tt.prefix) 32 if r != tt.result { 33 t.Errorf("HasPrefixFold(%q, %q) returned %v", tt.s, tt.prefix, r) 34 } 35 } 36 } 37 38 func TestHasSuffixFold(t *testing.T) { 39 tests := []struct { 40 s, suffix string 41 result bool 42 }{ 43 {"camli", "AMLI", true}, 44 {"CAMLI", "amli", true}, 45 {"mli", "MLI", true}, 46 {"camli", "ali", false}, 47 {"amli", "camli", false}, 48 {"asas Hello, 世界", "HeLlO, 世界", true}, 49 {"Hello, 世界", "HeLlO, 世界-", false}, 50 {"KkkkKKkelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign 51 52 {"kelvin", "\u212A" + "elvin", true}, // "\u212A" is the Kelvin temperature sign 53 {"Kelvin", "\u212A" + "elvin", true}, 54 {"\u212A" + "elvin", "Kelvin", true}, 55 {"\u212A" + "elvin", "kelvin", true}, 56 {"\u212A" + "elvin", "vin", true}, 57 {"\u212A" + "elvin", "viN", true}, 58 } 59 for _, tt := range tests { 60 r := HasSuffixFold(tt.s, tt.suffix) 61 if r != tt.result { 62 t.Errorf("HasSuffixFold(%q, %q) returned %v", tt.s, tt.suffix, r) 63 } 64 } 65 } 66 67 func TestContainsFold(t *testing.T) { 68 // TODO: more tests, more languages. 69 tests := []struct { 70 s, substr string 71 result bool 72 }{ 73 {"camli", "CAML", true}, 74 {"CAMLI", "caml", true}, 75 {"cam", "Cam", true}, 76 {"мир", "ми", true}, 77 {"МИP", "ми", true}, 78 {"КАМЛИЙСТОР", "камлийс", true}, 79 {"КаМлИйСтОр", "КаМлИйС", true}, 80 {"camli", "car", false}, 81 {"caml", "camli", false}, 82 83 {"camli", "AMLI", true}, 84 {"CAMLI", "amli", true}, 85 {"mli", "MLI", true}, 86 {"мир", "ир", true}, 87 {"МИP", "ми", true}, 88 {"КАМЛИЙСТОР", "лийстор", true}, 89 {"КаМлИйСтОр", "лИйСтОр", true}, 90 {"мир", "р", true}, 91 {"camli", "ali", false}, 92 {"amli", "camli", false}, 93 94 {"МИP", "и", true}, 95 {"мир", "и", true}, 96 {"КАМЛИЙСТОР", "лийс", true}, 97 {"КаМлИйСтОр", "лИйС", true}, 98 99 {"árvíztűrő tükörfúrógép", "árvíztŰrŐ", true}, 100 {"I love ☕", "i love ☕", true}, 101 102 {"k", "\u212A", true}, // "\u212A" is the Kelvin temperature sign 103 {"\u212A" + "elvin", "k", true}, 104 {"kelvin", "\u212A" + "elvin", true}, 105 {"Kelvin", "\u212A" + "elvin", true}, 106 {"\u212A" + "elvin", "Kelvin", true}, 107 {"\u212A" + "elvin", "kelvin", true}, 108 {"273.15 kelvin", "\u212A" + "elvin", true}, 109 {"273.15 Kelvin", "\u212A" + "elvin", true}, 110 {"273.15 \u212A" + "elvin", "Kelvin", true}, 111 {"273.15 \u212A" + "elvin", "kelvin", true}, 112 } 113 for _, tt := range tests { 114 r := ContainsFold(tt.s, tt.substr) 115 if r != tt.result { 116 t.Errorf("ContainsFold(%q, %q) returned %v", tt.s, tt.substr, r) 117 } 118 } 119 } 120 121 func TestReverse(t *testing.T) { 122 tests := []struct { 123 s, result string 124 }{ 125 {"ABCD", "DCBA"}, 126 {"ABCDE", "EDCBA"}, 127 {"Hello, 世界", "界世 ,olleH"}, 128 } 129 for _, tt := range tests { 130 r := Reverse(tt.s) 131 if r != tt.result { 132 t.Errorf("Reverse(%q) returned %v", tt.s, r) 133 } 134 } 135 } 136 137 func BenchmarkHasSuffixFoldToLower(tb *testing.B) { 138 a, b := "camlik", "AMLI\u212A" 139 for i := 0; i < tb.N; i++ { 140 if !strings.HasSuffix(strings.ToLower(a), strings.ToLower(b)) { 141 tb.Fatalf("%q should have the same suffix as %q", a, b) 142 } 143 } 144 } 145 func BenchmarkHasSuffixFold(tb *testing.B) { 146 a, b := "camlik", "AMLI\u212A" 147 for i := 0; i < tb.N; i++ { 148 if !HasSuffixFold(a, b) { 149 tb.Fatalf("%q should have the same suffix as %q", a, b) 150 } 151 } 152 } 153 154 func BenchmarkHasPrefixFoldToLower(tb *testing.B) { 155 a, b := "kamlistore", "\u212AAMLI" 156 for i := 0; i < tb.N; i++ { 157 if !strings.HasPrefix(strings.ToLower(a), strings.ToLower(b)) { 158 tb.Fatalf("%q should have the same suffix as %q", a, b) 159 } 160 } 161 } 162 func BenchmarkHasPrefixFold(tb *testing.B) { 163 a, b := "kamlistore", "\u212AAMLI" 164 for i := 0; i < tb.N; i++ { 165 if !HasPrefixFold(a, b) { 166 tb.Fatalf("%q should have the same suffix as %q", a, b) 167 } 168 } 169 }