Debian Codegraph @

fold_test.go

raw symbols SCIP
1 // Copyright 2016 Richard Gibson. All rights reserved.
2 // Copyright 2013 The Go Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package canonicaljson
7
8 import (
9 "bytes"
10 "strings"
11 "testing"
12 "unicode/utf8"
13 )
14
15 var foldTests = []struct {
16 fn func(s, t []byte) bool
17 s, t string
18 want bool
19 }{
20 {equalFoldRight, "", "", true},
21 {equalFoldRight, "a", "a", true},
22 {equalFoldRight, "", "a", false},
23 {equalFoldRight, "a", "", false},
24 {equalFoldRight, "a", "A", true},
25 {equalFoldRight, "AB", "ab", true},
26 {equalFoldRight, "AB", "ac", false},
27 {equalFoldRight, "sbkKc", "ſbKKc", true},
28 {equalFoldRight, "SbKkc", "ſbKKc", true},
29 {equalFoldRight, "SbKkc", "ſbKK", false},
30 {equalFoldRight, "e", "é", false},
31 {equalFoldRight, "s", "S", true},
32
33 {simpleLetterEqualFold, "", "", true},
34 {simpleLetterEqualFold, "abc", "abc", true},
35 {simpleLetterEqualFold, "abc", "ABC", true},
36 {simpleLetterEqualFold, "abc", "ABCD", false},
37 {simpleLetterEqualFold, "abc", "xxx", false},
38
39 {asciiEqualFold, "a_B", "A_b", true},
40 {asciiEqualFold, "aa@", "aa`", false}, // verify 0x40 and 0x60 aren't case-equivalent
41 }
42
43 func TestFold(t *testing.T) {
44 for i, tt := range foldTests {
45 if got := tt.fn([]byte(tt.s), []byte(tt.t)); got != tt.want {
46 t.Errorf("%d. %q, %q = %v; want %v", i, tt.s, tt.t, got, tt.want)
47 }
48 truth := strings.EqualFold(tt.s, tt.t)
49 if truth != tt.want {
50 t.Errorf("strings.EqualFold doesn't agree with case %d", i)
51 }
52 }
53 }
54
55 func TestFoldAgainstUnicode(t *testing.T) {
56 const bufSize = 5
57 buf1 := make([]byte, 0, bufSize)
58 buf2 := make([]byte, 0, bufSize)
59 var runes []rune
60 for i := 0x20; i <= 0x7f; i++ {
61 runes = append(runes, rune(i))
62 }
63 runes = append(runes, kelvin, smallLongEss)
64
65 funcs := []struct {
66 name string
67 fold func(s, t []byte) bool
68 letter bool // must be ASCII letter
69 simple bool // must be simple ASCII letter (not 'S' or 'K')
70 }{
71 {
72 name: "equalFoldRight",
73 fold: equalFoldRight,
74 },
75 {
76 name: "asciiEqualFold",
77 fold: asciiEqualFold,
78 simple: true,
79 },
80 {
81 name: "simpleLetterEqualFold",
82 fold: simpleLetterEqualFold,
83 simple: true,
84 letter: true,
85 },
86 }
87
88 for _, ff := range funcs {
89 for _, r := range runes {
90 if r >= utf8.RuneSelf {
91 continue
92 }
93 if ff.letter && !isASCIILetter(byte(r)) {
94 continue
95 }
96 if ff.simple && (r == 's' || r == 'S' || r == 'k' || r == 'K') {
97 continue
98 }
99 for _, r2 := range runes {
100 buf1 := append(buf1[:0], 'x')
101 buf2 := append(buf2[:0], 'x')
102 buf1 = buf1[:1+utf8.EncodeRune(buf1[1:bufSize], r)]
103 buf2 = buf2[:1+utf8.EncodeRune(buf2[1:bufSize], r2)]
104 buf1 = append(buf1, 'x')
105 buf2 = append(buf2, 'x')
106 want := bytes.EqualFold(buf1, buf2)
107 if got := ff.fold(buf1, buf2); got != want {
108 t.Errorf("%s(%q, %q) = %v; want %v", ff.name, buf1, buf2, got, want)
109 }
110 }
111 }
112 }
113 }
114
115 func isASCIILetter(b byte) bool {
116 return ('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z')
117 }