-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode_test.go
65 lines (55 loc) · 2.59 KB
/
decode_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
package bson
import (
"encoding/hex"
"testing"
)
func TestDecode(t *testing.T) {
doc := D{
{"a", int32(123)},
{"b", float64(123)},
{"c", "string"},
{
"d", D{
{"x", true},
{"y", "str"},
{"z", false},
},
},
}
raw, err := Marshal(doc)
mustOk(t, err)
doc = nil
mustOk(t, Unmarshal(raw, &doc))
t.Logf("got %+v\n", doc)
}
func Benchmark_cristalhq_Unmarshal(b *testing.B) {
buf, _ := hex.DecodeString("a001000002616e6e6f756e636500270000007564703a2f2f747261636b65722e7075626c696362742e636f6d3a38302f616e6e6f756e63650004616e6e6f756e63656c69737400cf000000023000270000007564703a2f2f747261636b65722e7075626c696362742e636f6d3a38302f616e6e6f756e6365000231002d0000007564703a2f2f747261636b65722e6f70656e626974746f7272656e742e636f6d3a38302f616e6e6f756e6365000232002d0000007564703a2f2f747261636b65722e6f70656e626974746f7272656e742e636f6d3a38302f616e6e6f756e6365000233002d0000007564703a2f2f747261636b65722e6f70656e626974746f7272656e742e636f6d3a38302f616e6e6f756e6365000002636f6d6d656e74002200000044656269616e2043442066726f6d206364696d6167652e64656269616e2e6f72670003696e666f0054000000126c656e677468000000300a00000000026e616d65001f00000064656269616e2d382e382e302d61726d36342d6e6574696e73742e69736f00127069656365206c656e6774680000000400000000000000")
b.ReportAllocs()
v := map[string]any{}
var count int64
for n := 0; n < b.N; n++ {
err := Unmarshal(buf, &v)
if err != nil {
panic(err)
}
count += int64(len(v))
}
sink(b, count)
}
func Fuzz_Decoder2(f *testing.F) {
f.Add(unhex("a001000002616e6e6f756e636500270000007564703a2f2f747261636b65722e7075626c696362742e636f6d3a38302f616e6e6f756e63650004616e6e6f756e63656c69737400cf000000023000270000007564703a2f2f747261636b65722e7075626c696362742e636f6d3a38302f616e6e6f756e6365000231002d0000007564703a2f2f747261636b65722e6f70656e626974746f7272656e742e636f6d3a38302f616e6e6f756e6365000232002d0000007564703a2f2f747261636b65722e6f70656e626974746f7272656e742e636f6d3a38302f616e6e6f756e6365000233002d0000007564703a2f2f747261636b65722e6f70656e626974746f7272656e742e636f6d3a38302f616e6e6f756e6365000002636f6d6d656e74002200000044656269616e2043442066726f6d206364696d6167652e64656269616e2e6f72670003696e666f0054000000126c656e677468000000300a00000000026e616d65001f00000064656269616e2d382e382e302d61726d36342d6e6574696e73742e69736f00127069656365206c656e6774680000000400000000000000"))
f.Add(unhex("4d88e15b60f486e428412dc9"))
v := map[string]any{}
f.Fuzz(func(t *testing.T, buf []byte) {
dec := NewDecodeBytes(buf)
if err := dec.Decode(v); err != nil {
}
})
}
func unhex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}