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