1
2
3
4
5
6
7 package json
8
9 import (
10 "fmt"
11 "reflect"
12 "testing"
13 "unicode"
14 )
15
16 var equalFoldTestdata = []struct {
17 in1, in2 string
18 want bool
19 }{
20 {"", "", true},
21 {"abc", "abc", true},
22 {"ABcd", "ABcd", true},
23 {"123abc", "123ABC", true},
24 {"_1_2_-_3__--a-_-b-c-", "123ABC", true},
25 {"αβδ", "ΑΒΔ", true},
26 {"abc", "xyz", false},
27 {"abc", "XYZ", false},
28 {"abcdefghijk", "abcdefghijX", false},
29 {"abcdefghijk", "abcdefghij\u212A", true},
30 {"abcdefghijK", "abcdefghij\u212A", true},
31 {"abcdefghijkz", "abcdefghij\u212Ay", false},
32 {"abcdefghijKz", "abcdefghij\u212Ay", false},
33 {"1", "2", false},
34 {"utf-8", "US-ASCII", false},
35 {"hello, world!", "hello, world!", true},
36 {"hello, world!", "Hello, World!", true},
37 {"hello, world!", "HELLO, WORLD!", true},
38 {"hello, world!", "jello, world!", false},
39 {"γειά, κόσμε!", "γειά, κόσμε!", true},
40 {"γειά, κόσμε!", "Γειά, Κόσμε!", true},
41 {"γειά, κόσμε!", "ΓΕΙΆ, ΚΌΣΜΕ!", true},
42 {"γειά, κόσμε!", "ΛΕΙΆ, ΚΌΣΜΕ!", false},
43 {"AESKey", "aesKey", true},
44 {"γειά, κόσμε!", "Γ\xce_\xb5ιά, Κόσμε!", false},
45 {"aeskey", "AESKEY", true},
46 {"AESKEY", "aes_key", true},
47 {"aes_key", "AES_KEY", true},
48 {"AES_KEY", "aes-key", true},
49 {"aes-key", "AES-KEY", true},
50 {"AES-KEY", "aesKey", true},
51 {"aesKey", "AesKey", true},
52 {"AesKey", "AESKey", true},
53 {"AESKey", "aeskey", true},
54 {"DESKey", "aeskey", false},
55 {"AES Key", "aeskey", false},
56 {"aes﹏key", "aeskey", false},
57 {"aes〰key", "aeskey", false},
58 }
59
60 func TestEqualFold(t *testing.T) {
61 for _, tt := range equalFoldTestdata {
62 got := equalFold([]byte(tt.in1), []byte(tt.in2))
63 if got != tt.want {
64 t.Errorf("equalFold(%q, %q) = %v, want %v", tt.in1, tt.in2, got, tt.want)
65 }
66 }
67 }
68
69 func equalFold(x, y []byte) bool {
70 return string(foldName(x)) == string(foldName(y))
71 }
72
73 func TestFoldRune(t *testing.T) {
74 if testing.Short() {
75 t.Skip()
76 }
77
78 var foldSet []rune
79 for r := range rune(unicode.MaxRune + 1) {
80
81 foldSet = foldSet[:0]
82 for r0 := r; r != r0 || len(foldSet) == 0; r = unicode.SimpleFold(r) {
83 foldSet = append(foldSet, r)
84 }
85
86
87
88 var withinSet bool
89 rr0 := foldRune(foldSet[0])
90 for _, r := range foldSet {
91 withinSet = withinSet || rr0 == r
92 rr := foldRune(r)
93 if rr0 != rr {
94 t.Errorf("foldRune(%q) = %q, want %q", r, rr, rr0)
95 }
96 }
97 if !withinSet {
98 t.Errorf("foldRune(%q) = %q not in fold set %q", foldSet[0], rr0, string(foldSet))
99 }
100 }
101 }
102
103
104
105
106 func TestBenchmarkUnmarshalUnknown(t *testing.T) {
107 in := []byte(`{"NameUnknown":null}`)
108 for _, n := range []int{1, 2, 5, 10, 20, 50, 100} {
109 unmarshal := Unmarshal
110
111 var fields []reflect.StructField
112 for i := range n {
113 fields = append(fields, reflect.StructField{
114 Name: fmt.Sprintf("Name%d", i),
115 Type: T[int](),
116 Tag: `json:",case:ignore"`,
117 })
118 }
119 out := reflect.New(reflect.StructOf(fields)).Interface()
120
121 t.Run(fmt.Sprintf("N%d", n), func(t *testing.T) {
122 if err := unmarshal(in, out); err != nil {
123 t.Fatalf("Unmarshal error: %v", err)
124 }
125 })
126 }
127 }
128
View as plain text