1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package fields
16
17
18
19
20 import (
21 "bytes"
22 "strings"
23 "testing"
24 "unicode/utf8"
25 )
26
27 var foldTests = []struct {
28 fn func(s, t []byte) bool
29 s, t string
30 want bool
31 }{
32 {equalFoldRight, "", "", true},
33 {equalFoldRight, "a", "a", true},
34 {equalFoldRight, "", "a", false},
35 {equalFoldRight, "a", "", false},
36 {equalFoldRight, "a", "A", true},
37 {equalFoldRight, "AB", "ab", true},
38 {equalFoldRight, "AB", "ac", false},
39 {equalFoldRight, "sbkKc", "ſbKKc", true},
40 {equalFoldRight, "SbKkc", "ſbKKc", true},
41 {equalFoldRight, "SbKkc", "ſbKK", false},
42 {equalFoldRight, "e", "é", false},
43 {equalFoldRight, "s", "S", true},
44
45 {simpleLetterEqualFold, "", "", true},
46 {simpleLetterEqualFold, "abc", "abc", true},
47 {simpleLetterEqualFold, "abc", "ABC", true},
48 {simpleLetterEqualFold, "abc", "ABCD", false},
49 {simpleLetterEqualFold, "abc", "xxx", false},
50
51 {asciiEqualFold, "a_B", "A_b", true},
52 {asciiEqualFold, "aa@", "aa`", false},
53 }
54
55 func TestFold(t *testing.T) {
56 for i, tt := range foldTests {
57 if got := tt.fn([]byte(tt.s), []byte(tt.t)); got != tt.want {
58 t.Errorf("%d. %q, %q = %v; want %v", i, tt.s, tt.t, got, tt.want)
59 }
60 truth := strings.EqualFold(tt.s, tt.t)
61 if truth != tt.want {
62 t.Errorf("strings.EqualFold doesn't agree with case %d", i)
63 }
64 }
65 }
66
67 func TestFoldAgainstUnicode(t *testing.T) {
68 const bufSize = 5
69 buf1 := make([]byte, 0, bufSize)
70 buf2 := make([]byte, 0, bufSize)
71 var runes []rune
72 for i := 0x20; i <= 0x7f; i++ {
73 runes = append(runes, rune(i))
74 }
75 runes = append(runes, kelvin, smallLongEss)
76
77 funcs := []struct {
78 name string
79 fold func(s, t []byte) bool
80 letter bool
81 simple bool
82 }{
83 {
84 name: "equalFoldRight",
85 fold: equalFoldRight,
86 },
87 {
88 name: "asciiEqualFold",
89 fold: asciiEqualFold,
90 simple: true,
91 },
92 {
93 name: "simpleLetterEqualFold",
94 fold: simpleLetterEqualFold,
95 simple: true,
96 letter: true,
97 },
98 }
99
100 for _, ff := range funcs {
101 for _, r := range runes {
102 if r >= utf8.RuneSelf {
103 continue
104 }
105 if ff.letter && !isASCIILetter(byte(r)) {
106 continue
107 }
108 if ff.simple && (r == 's' || r == 'S' || r == 'k' || r == 'K') {
109 continue
110 }
111 for _, r2 := range runes {
112 buf1 := append(buf1[:0], 'x')
113 buf2 := append(buf2[:0], 'x')
114 buf1 = buf1[:1+utf8.EncodeRune(buf1[1:bufSize], r)]
115 buf2 = buf2[:1+utf8.EncodeRune(buf2[1:bufSize], r2)]
116 buf1 = append(buf1, 'x')
117 buf2 = append(buf2, 'x')
118 want := bytes.EqualFold(buf1, buf2)
119 if got := ff.fold(buf1, buf2); got != want {
120 t.Errorf("%s(%q, %q) = %v; want %v", ff.name, buf1, buf2, got, want)
121 }
122 }
123 }
124 }
125 }
126
127 func isASCIILetter(b byte) bool {
128 return ('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z')
129 }
130
View as plain text