-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
69 lines (64 loc) · 1.36 KB
/
json_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
package goutils
import (
"encoding/json"
"fmt"
"testing"
)
func TestToJSON(t *testing.T) {
type sj struct {
I int `json:"i"`
S string `json:"s"`
}
for i := 0; i < 1000; i++ {
ic := i
t.Run(fmt.Sprintf("TestToJSON-%d", ic), func(t *testing.T) {
t.Parallel()
s := sj{ic, fmt.Sprintf("我&<%d>你", ic)}
got := ToJSON(s)
want := fmt.Sprintf(`{"i":%[1]d,"s":"我&<%[1]d>你"}`, ic)
if got != want {
t.Errorf("\nwant:`%s`\ngot :`%s`", want, got)
}
})
}
}
func TestToPrettyJSON(t *testing.T) {
type sj struct {
I int `json:"i"`
S string `json:"s"`
}
for i := 0; i < 1000; i++ {
ic := i
t.Run(fmt.Sprintf("TestToJSON-%d", ic), func(t *testing.T) {
t.Parallel()
s := sj{ic, fmt.Sprintf("我&<%d>你", ic)}
got := ToPrettyJson(s)
want := fmt.Sprintf("{\n \"i\": %[1]d,\n \"s\": \"我&<%[1]d>你\"\n}", ic)
if got != want {
t.Errorf("\nwant:`%s`\ngot :`%s`", want, got)
}
})
}
}
func BenchmarkToJSON(b *testing.B) {
type sj struct {
I int `json:"i"`
S string `json:"s"`
}
for i := 0; i < b.N; i++ {
s := sj{i, fmt.Sprintf("我&<%d>你", i)}
_ = ToJSON(s)
}
}
func BenchmarkRawJSONMarshal(b *testing.B) {
type sj struct {
I int `json:"i"`
S string `json:"s"`
}
for i := 0; i < b.N; i++ {
s := sj{i, fmt.Sprintf("我&<%d>你", i)}
e, _ := json.Marshal(s)
a := string(e)
_ = a
}
}