-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequal_test.go
78 lines (60 loc) · 1.35 KB
/
equal_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
package jsonoscope
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)
func TestEqual(t *testing.T) {
cases := []struct {
First, Second []byte
Equal bool
}{
// formatting does not matter
{
First: []byte(`[1,2,3]`),
Second: []byte(`[ 1, 2, 3 ]`),
Equal: true,
},
// order matters in arrays
{
First: []byte(`[1, 2, 3]`),
Second: []byte(`[1, 3, 2]`),
Equal: false,
},
// order does not matter in objects
{
First: []byte(`{ "Planet": "Earth", "Index": 3 }`),
Second: []byte(`{ "Index": 3, "Planet": "Earth" }`),
Equal: true,
},
}
for i, c := range cases {
eq, err := Equal(bytes.NewReader(c.First), bytes.NewReader(c.Second))
if err != nil {
panic(err)
}
if eq != c.Equal {
t.Errorf("[case %d] Unexpected equality: expected %t but got %t\n", i, c.Equal, eq)
}
}
}
func BenchmarkEqual(b *testing.B) {
for i := 0; i < b.N; i++ {
eq, _ := Equal(bytes.NewReader(SampleJSON), bytes.NewReader(SampleJSON))
if !eq {
b.Fatalf("not equal")
}
}
}
func BenchmarkDeepEqual(b *testing.B) {
for i := 0; i < b.N; i++ {
var json1, json2 map[string]interface{}
_ = json.NewDecoder(bytes.NewReader(SampleJSON)).Decode(&json1)
_ = json.NewDecoder(bytes.NewReader(SampleJSON)).Decode(&json2)
eq := reflect.DeepEqual(json1, json2)
if !eq {
b.Fatalf("not equal")
}
}
}