File: raw_test.go

package info (click to toggle)
golang-github-tinylib-msgp 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: makefile: 47
file content (113 lines) | stat: -rw-r--r-- 2,157 bytes parent folder | download | duplicates (3)
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
package msgp

import (
	"bytes"
	"testing"
	"time"
)

// all standard interfaces
type allifaces interface {
	Encodable
	Decodable
	Marshaler
	Unmarshaler
	Sizer
}

func TestRaw(t *testing.T) {
	bts := make([]byte, 0, 512)
	bts = AppendMapHeader(bts, 3)
	bts = AppendString(bts, "key_one")
	bts = AppendFloat64(bts, -1.0)
	bts = AppendString(bts, "key_two")
	bts = AppendString(bts, "value_two")
	bts = AppendString(bts, "key_three")
	bts = AppendTime(bts, time.Now())

	var r Raw

	// verify that Raw satisfies
	// the interfaces we want it to
	var _ allifaces = &r

	// READ TESTS

	extra, err := r.UnmarshalMsg(bts)
	if err != nil {
		t.Fatal("error from UnmarshalMsg:", err)
	}
	if len(extra) != 0 {
		t.Errorf("expected 0 bytes left; found %d", len(extra))
	}
	if !bytes.Equal([]byte(r), bts) {
		t.Fatal("value of raw and input slice are not equal after UnmarshalMsg")
	}

	r = r[:0]

	var buf bytes.Buffer
	buf.Write(bts)

	rd := NewReader(&buf)

	err = r.DecodeMsg(rd)
	if err != nil {
		t.Fatal("error from DecodeMsg:", err)
	}

	if !bytes.Equal([]byte(r), bts) {
		t.Fatal("value of raw and input slice are not equal after DecodeMsg")
	}

	// WRITE TESTS

	buf.Reset()
	wr := NewWriter(&buf)
	err = r.EncodeMsg(wr)
	if err != nil {
		t.Fatal("error from EncodeMsg:", err)
	}

	wr.Flush()
	if !bytes.Equal(buf.Bytes(), bts) {
		t.Fatal("value of buf.Bytes() and input slice are not equal after EncodeMsg")
	}

	var outsl []byte
	outsl, err = r.MarshalMsg(outsl)
	if err != nil {
		t.Fatal("error from MarshalMsg:", err)
	}
	if !bytes.Equal(outsl, bts) {
		t.Fatal("value of output and input of MarshalMsg are not equal.")
	}
}

func TestNullRaw(t *testing.T) {
	// Marshal/Unmarshal
	var x, y Raw
	if bts, err := x.MarshalMsg(nil); err != nil {
		t.Fatal(err)
	} else if _, err = y.UnmarshalMsg(bts); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(x, y) {
		t.Fatal("compare")
	}

	// Encode/Decode
	var buf bytes.Buffer
	wr := NewWriter(&buf)
	if err := x.EncodeMsg(wr); err != nil {
		t.Fatal(err)
	}
	wr.Flush()
	rd := NewReader(&buf)
	if err := y.DecodeMsg(rd); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(x, y) {
		t.Fatal("compare")
	}
}