-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjptr_test.go
147 lines (125 loc) · 3.77 KB
/
jptr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package gson
import "testing"
import "strings"
import "sort"
func TestParsePointer(t *testing.T) {
var tcasesJSONPointers = [][3]interface{}{
{``, []string{}, ``},
{`/`, []string{""}, `/`},
{"/foo", []string{"foo"}, "/foo"},
{"/foo/0", []string{"foo", "0"}, "/foo/0"},
{"/a~1b", []string{"a/b"}, "/a~1b"},
{"/c%d", []string{"c%d"}, "/c%d"},
{"/e^f", []string{"e^f"}, "/e^f"},
{"/g|h", []string{"g|h"}, "/g|h"},
{`/i\j`, []string{`i\j`}, `/i\\j`},
{`/k\"l`, []string{`k\"l`}, `/k\\\"l`},
{"/ ", []string{" "}, "/ "},
{"/m~0n", []string{"m~n"}, "/m~0n"},
{"/g~1n~1r", []string{"g/n/r"}, "/g~1n~1r"},
{"/g/汉语/r", []string{"g", "汉语", "r"}, "/g/汉语/r"},
{"/dict/", []string{"dict", ""}, "/dict/"},
}
// test ParseJsonPointer
config := NewDefaultConfig()
jptr := config.NewJsonpointer("")
for _, tcase := range tcasesJSONPointers {
t.Logf("input pointer %q", tcase[0].(string))
ref := tcase[1].([]string)
jptr.ResetPath(tcase[0].(string))
segments := jptr.Segments()
if len(segments) != len(ref) {
t.Errorf("expected %v, got %v", len(ref), len(segments))
} else {
for i, x := range ref {
if string(segments[i]) != x {
t.Errorf("expected %v, got %v", x, string(segments[i]))
}
}
}
// test encode pointers
jptr.ResetPath("").ResetSegments(ref)
if path := string(jptr.Path()); path != tcase[2].(string) {
t.Errorf("expected %v, got %v", tcase[2].(string), path)
}
}
}
func TestTypicalPointers(t *testing.T) {
refs := strings.Split(string(testdataFile("testdata/typical_pointers")), "\n")
refs = refs[:len(refs)-1] // skip the last empty line
sort.Strings(refs)
config := NewDefaultConfig()
data := testdataFile("testdata/typical.json")
_, value := config.NewJson(data).Tovalue()
val := config.NewValue(value)
// test list pointers
pointers := val.ListPointers(make([]string, 0, 1024))
sort.Strings(pointers)
if len(refs) != len(pointers) {
t.Errorf("expected %v, got %v", len(refs), len(pointers))
}
for i, r := range refs {
if r != pointers[i] {
t.Errorf("expected %v, got %v", r, pointers[i])
}
}
// test list pointers for document using [][2]interface{} for map.
value = GolangMap2cborMap(value)
val = config.NewValue(value)
pointers = val.ListPointers(make([]string, 0, 1024))
sort.Strings(pointers)
if len(refs) != len(pointers) {
t.Errorf("expected %v, got %v", len(refs), len(pointers))
}
for i, r := range refs {
if r != pointers[i] {
t.Errorf("expected %v, got %v", r, pointers[i])
}
}
}
func BenchmarkParseJsonPtr3(b *testing.B) {
path := "/foo/g/0"
jptr := NewDefaultConfig().NewJsonpointer(path)
b.SetBytes(int64(len(path)))
for i := 0; i < b.N; i++ {
jptr.ResetPath(path).Segments()
}
}
func BenchmarkParseJsonPtr4(b *testing.B) {
path := "/foo/g~1n~1r/0/hello"
jptr := NewDefaultConfig().NewJsonpointer(path)
b.SetBytes(int64(len(path)))
for i := 0; i < b.N; i++ {
jptr.ResetPath(path).Segments()
}
}
func BenchmarkParseJsonPtr5(b *testing.B) {
segments := []string{"a", "ab", "a~b", "a/b", "a~/~/b"}
jptr := NewDefaultConfig().NewJsonpointer("").ResetSegments(segments)
path := string(jptr.Path())
b.ResetTimer()
for i := 0; i < b.N; i++ {
jptr.ResetPath(path).Segments()
}
b.SetBytes(int64(len(jptr.Path())))
}
func BenchmarkToJsonPtr5(b *testing.B) {
segments := []string{"a", "ab", "a~b", "a/b", "a~/~/b"}
jptr := NewDefaultConfig().NewJsonpointer("")
b.SetBytes(15)
for i := 0; i < b.N; i++ {
jptr.ResetSegments(segments)
}
}
func BenchmarkListPtrsTyp(b *testing.B) {
data := testdataFile("testdata/typical.json")
config := NewDefaultConfig()
_, value := config.NewJson(data).Tovalue()
val := config.NewValue(value)
pointers := []string{}
b.SetBytes(int64(len(data)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
pointers = val.ListPointers(pointers[:0])
}
}