1
2
3
4
5
6
7 package jsonwire
8
9 import (
10 "cmp"
11 "slices"
12 "testing"
13 "unicode/utf16"
14 "unicode/utf8"
15 )
16
17 func TestQuoteRune(t *testing.T) {
18 tests := []struct{ in, want string }{
19 {"x", `'x'`},
20 {"\n", `'\n'`},
21 {"'", `'\''`},
22 {"\xff", `'\xff'`},
23 {"💩", `'💩'`},
24 {"💩"[:1], `'\xf0'`},
25 {"\uffff", `'\uffff'`},
26 {"\U00101234", `'\U00101234'`},
27 }
28 for _, tt := range tests {
29 got := QuoteRune([]byte(tt.in))
30 if got != tt.want {
31 t.Errorf("quoteRune(%q) = %s, want %s", tt.in, got, tt.want)
32 }
33 }
34 }
35
36 var compareUTF16Testdata = []string{"", "\r", "1", "f\xfe", "f\xfe\xff", "f\xff", "\u0080", "\u00f6", "\u20ac", "\U0001f600", "\ufb33"}
37
38 func TestCompareUTF16(t *testing.T) {
39 for i, si := range compareUTF16Testdata {
40 for j, sj := range compareUTF16Testdata {
41 got := CompareUTF16([]byte(si), []byte(sj))
42 want := cmp.Compare(i, j)
43 if got != want {
44 t.Errorf("CompareUTF16(%q, %q) = %v, want %v", si, sj, got, want)
45 }
46 }
47 }
48 }
49
50 func FuzzCompareUTF16(f *testing.F) {
51 for _, td1 := range compareUTF16Testdata {
52 for _, td2 := range compareUTF16Testdata {
53 f.Add([]byte(td1), []byte(td2))
54 }
55 }
56
57
58
59
60 CompareUTF16Simple := func(x, y []byte) int {
61 ux := utf16.Encode([]rune(string(x)))
62 uy := utf16.Encode([]rune(string(y)))
63 return slices.Compare(ux, uy)
64 }
65
66 f.Fuzz(func(t *testing.T, s1, s2 []byte) {
67
68 got := CompareUTF16(s1, s2)
69 want := CompareUTF16Simple(s1, s2)
70 if got != want && utf8.Valid(s1) && utf8.Valid(s2) {
71 t.Errorf("CompareUTF16(%q, %q) = %v, want %v", s1, s2, got, want)
72 }
73 })
74 }
75
76 func TestTruncatePointer(t *testing.T) {
77 tests := []struct{ in, want string }{
78 {"hello", "hello"},
79 {"/a/b/c", "/a/b/c"},
80 {"/a/b/c/d/e/f/g", "/a/b/…/f/g"},
81 {"supercalifragilisticexpialidocious", "super…cious"},
82 {"/supercalifragilisticexpialidocious/supercalifragilisticexpialidocious", "/supe…/…cious"},
83 {"/supercalifragilisticexpialidocious/supercalifragilisticexpialidocious/supercalifragilisticexpialidocious", "/supe…/…/…cious"},
84 {"/a/supercalifragilisticexpialidocious/supercalifragilisticexpialidocious", "/a/…/…cious"},
85 {"/supercalifragilisticexpialidocious/supercalifragilisticexpialidocious/b", "/supe…/…/b"},
86 {"/fizz/buzz/bazz", "/fizz/…/bazz"},
87 {"/fizz/buzz/bazz/razz", "/fizz/…/razz"},
88 {"/////////////////////////////", "/////…/////"},
89 {"/🎄❤️✨/🎁✅😊/🎅🔥⭐", "/🎄…/…/…⭐"},
90 }
91 for _, tt := range tests {
92 got := TruncatePointer(tt.in, 10)
93 if got != tt.want {
94 t.Errorf("TruncatePointer(%q) = %q, want %q", tt.in, got, tt.want)
95 }
96 }
97
98 }
99
View as plain text