Skip to content
This repository was archived by the owner on May 8, 2019. It is now read-only.

Commit e1e324a

Browse files
committed
removed ReadFile and WriteFile and added a test
1 parent a7bba7c commit e1e324a

File tree

4 files changed

+81
-156
lines changed

4 files changed

+81
-156
lines changed

msgp/defs_test.go

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,88 @@
11
package msgp_test
22

3+
import (
4+
"bytes"
5+
"math/rand"
6+
"testing"
7+
8+
"github.com/dchenk/msgp/msgp"
9+
)
10+
311
//go:generate msgp -o=defs_gen_test.go -tests=false
412

513
type Blobs []Blob // needed separately for Msgsize()
614

715
type Blob struct {
8-
Name string `msgp:"name"`
9-
Float float64 `msgp:"float"`
10-
Bytes []byte `msgp:"bytes"`
11-
Amount int64 `msgp:"amount"`
16+
Name string `msgp:"name"`
17+
Float float64 `msgp:"float"`
18+
Inner struct {
19+
F float32 `msgp:"f32"`
20+
} `msgp:"inner"`
21+
Bytes []byte `msgp:"bytes"`
22+
Amount int64 `msgp:"amount"`
23+
Unsigned uint16 // Use field name
24+
}
25+
26+
var (
27+
blobStrings = []string{"", "a string", "a longer string here!"}
28+
blobFloats = []float64{0.0, -1.00000007, 1.0, 3.1415926535}
29+
blobFloats32 = []float32{-34.5243, 0.0, -1.0, 1.0, 3.526258}
30+
blobIntegers = []int64{0, 1, -1, 80000, 1 << 30}
31+
blobBytes = [][]byte{{}, []byte("hello"), []byte(`{"is_json":true, "more_stuff":[75]}`)}
32+
)
33+
34+
// TestEncodeDecode tests Blobs for actual data integrity.
35+
func TestEncodeDecode(t *testing.T) {
36+
37+
const size = 5
38+
data := make(Blobs, size)
39+
40+
for i := range data {
41+
datum := &data[i]
42+
datum.Name = blobStrings[rand.Intn(len(blobStrings))]
43+
datum.Float = blobFloats[rand.Intn(len(blobFloats))]
44+
datum.Inner.F = blobFloats32[rand.Intn(len(blobFloats32))]
45+
datum.Amount = blobIntegers[rand.Intn(len(blobIntegers))]
46+
datum.Bytes = blobBytes[rand.Intn(len(blobBytes))]
47+
}
48+
49+
var msg bytes.Buffer
50+
51+
err := msgp.Encode(&msg, &data)
52+
if err != nil {
53+
t.Fatalf("could not encode; %v", err)
54+
}
55+
56+
// Each element that we are encoding has five objects (the minimum length
57+
// of any single thing in MessagePack is one byte).
58+
if msg.Len() < size*5 {
59+
t.Fatalf("not enough data encoded")
60+
}
61+
62+
var decoded Blobs
63+
err = msgp.Decode(&msg, &decoded)
64+
if err != nil {
65+
t.Fatalf("could not decode; %v", err)
66+
}
67+
68+
// Ensure we have five things.
69+
for i := 0; i < size; i++ {
70+
datum := data[i]
71+
if v := decoded[i].Name; datum.Name != v {
72+
t.Errorf("(index %d) bad Name: %q", i, v)
73+
}
74+
if v := decoded[i].Float; datum.Float != v {
75+
t.Errorf("(index %d) bad Float: %v", i, v)
76+
}
77+
if v := decoded[i].Inner.F; datum.Inner.F != v {
78+
t.Errorf("(index %d) bad Inner.F: %v", i, v)
79+
}
80+
if v := decoded[i].Bytes; !bytes.Equal(datum.Bytes, v) {
81+
t.Errorf("(index %d) bad Bytes: %v", i, v)
82+
}
83+
if v := decoded[i].Unsigned; datum.Unsigned != v {
84+
t.Errorf("(index %d) bad Unsigned: %v", i, v)
85+
}
86+
}
87+
1288
}

msgp/file.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

msgp/file_test.go

Lines changed: 0 additions & 114 deletions
This file was deleted.

tests/def.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ type ArrayConstants struct {
236236
ConstantOctal [07]string
237237
}
238238

239-
// Ensure non-msg struct tags work:
239+
// Ensure non-msgp struct tags work:
240240

241241
type NonMsgStructTags struct {
242242
A []string `json:"fooJSON" msg:"fooMsgp"`

0 commit comments

Comments
 (0)