You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1019 B
Go
63 lines
1019 B
Go
3 years ago
|
package json
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
type Message struct {
|
||
|
Code int `json:"code"`
|
||
|
Data string `json:"data"`
|
||
|
}
|
||
|
|
||
|
func TestSerializer_Serialize(t *testing.T) {
|
||
|
m := Message{1, "hello world"}
|
||
|
s := NewSerializer()
|
||
|
b, err := s.Marshal(m)
|
||
|
if err != nil {
|
||
|
t.Fail()
|
||
|
}
|
||
|
|
||
|
m2 := Message{}
|
||
|
if err := s.Unmarshal(b, &m2); err != nil {
|
||
|
t.Fail()
|
||
|
}
|
||
|
|
||
|
if !reflect.DeepEqual(m, m2) {
|
||
|
t.Fail()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func BenchmarkSerializer_Serialize(b *testing.B) {
|
||
|
m := &Message{100, "hell world"}
|
||
|
s := NewSerializer()
|
||
|
|
||
|
b.ResetTimer()
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
if _, err := s.Marshal(m); err != nil {
|
||
|
b.Fatalf("unmarshal failed: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
b.ReportAllocs()
|
||
|
}
|
||
|
|
||
|
func BenchmarkSerializer_Deserialize(b *testing.B) {
|
||
|
m := &Message{100, "hell world"}
|
||
|
s := NewSerializer()
|
||
|
|
||
|
d, err := s.Marshal(m)
|
||
|
if err != nil {
|
||
|
b.Error(err)
|
||
|
}
|
||
|
|
||
|
b.ResetTimer()
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
m1 := &Message{}
|
||
|
if err := s.Unmarshal(d, m1); err != nil {
|
||
|
b.Fatalf("unmarshal failed: %v", err)
|
||
|
}
|
||
|
}
|
||
|
b.ReportAllocs()
|
||
|
}
|