|
1 | 1 | package msgp_test |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "math/rand" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/dchenk/msgp/msgp" |
| 9 | +) |
| 10 | + |
3 | 11 | //go:generate msgp -o=defs_gen_test.go -tests=false |
4 | 12 |
|
5 | 13 | type Blobs []Blob // needed separately for Msgsize() |
6 | 14 |
|
7 | 15 | 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 | + |
12 | 88 | } |
0 commit comments