github.com/chrislusf/greenpack@v3.7.1-0.20170911073826-ad5bd10b7c47+incompatible/_generated/gen_test.go (about) 1 package _generated 2 3 import ( 4 "bytes" 5 "github.com/glycerine/greenpack/msgp" 6 "reflect" 7 "testing" 8 "time" 9 ) 10 11 // benchmark encoding a small, "fast" type. 12 // the point here is to see how much garbage 13 // is generated intrinsically by the encoding/ 14 // decoding process as opposed to the nature 15 // of the struct. 16 func BenchmarkFastEncode(b *testing.B) { 17 v := &TestFast{ 18 Lat: 40.12398, 19 Long: -41.9082, 20 Alt: 201.08290, 21 Data: []byte("whaaaaargharbl"), 22 } 23 var buf bytes.Buffer 24 msgp.Encode(&buf, v) 25 en := msgp.NewWriter(msgp.Nowhere) 26 b.SetBytes(int64(buf.Len())) 27 b.ReportAllocs() 28 b.ResetTimer() 29 for i := 0; i < b.N; i++ { 30 v.EncodeMsg(en) 31 } 32 en.Flush() 33 } 34 35 // benchmark decoding a small, "fast" type. 36 // the point here is to see how much garbage 37 // is generated intrinsically by the encoding/ 38 // decoding process as opposed to the nature 39 // of the struct. 40 func BenchmarkFastDecode(b *testing.B) { 41 v := &TestFast{ 42 Lat: 40.12398, 43 Long: -41.9082, 44 Alt: 201.08290, 45 Data: []byte("whaaaaargharbl"), 46 } 47 48 var buf bytes.Buffer 49 msgp.Encode(&buf, v) 50 dc := msgp.NewReader(msgp.NewEndlessReader(buf.Bytes(), b)) 51 b.SetBytes(int64(buf.Len())) 52 b.ReportAllocs() 53 b.ResetTimer() 54 for i := 0; i < b.N; i++ { 55 v.DecodeMsg(dc) 56 } 57 } 58 59 // This covers the following cases: 60 // - Recursive types 61 // - Non-builtin identifiers (and recursive types) 62 // - time.Time 63 // - map[string]string 64 // - anonymous structs 65 // 66 func Test1EncodeDecode(t *testing.T) { 67 f := 32.00 68 tt := &TestType{ 69 F: &f, 70 Els: map[string]string{ 71 "thing_one": "one", 72 "thing_two": "two", 73 }, 74 Obj: struct { 75 ValueA string `msg:"value_a"` 76 ValueB []byte `msg:"value_b"` 77 }{ 78 ValueA: "here's the first inner value", 79 ValueB: []byte("here's the second inner value"), 80 }, 81 Child: nil, 82 Time: time.Now().Round(0), 83 Appended: msgp.Raw([]byte{0xc0}), // 'nil' 84 } 85 86 var buf bytes.Buffer 87 88 err := msgp.Encode(&buf, tt) 89 if err != nil { 90 t.Fatal(err) 91 } 92 93 tnew := new(TestType) 94 95 err = msgp.Decode(&buf, tnew) 96 if err != nil { 97 t.Error(err) 98 } 99 100 if !reflect.DeepEqual(tt, tnew) { 101 t.Logf("in: %#v", tt) 102 t.Logf("out: %#v", tnew) 103 t.Fatal("objects not equal") 104 } 105 106 tanother := new(TestType) 107 108 buf.Reset() 109 msgp.Encode(&buf, tt) 110 111 var left []byte 112 left, err = tanother.UnmarshalMsg(buf.Bytes()) 113 if err != nil { 114 t.Error(err) 115 } 116 if len(left) > 0 { 117 t.Errorf("%d bytes left", len(left)) 118 } 119 120 if !reflect.DeepEqual(tt, tanother) { 121 t.Logf("in: %#v", tt) 122 t.Logf("out: %#v", tanother) 123 t.Fatal("objects not equal") 124 } 125 } 126 127 func TestIssue168(t *testing.T) { 128 buf := bytes.Buffer{} 129 test := TestObj{} 130 131 msgp.Encode(&buf, &TestObj{ID1: "1", ID2: "2"}) 132 msgp.Decode(&buf, &test) 133 134 if test.ID1 != "1" || test.ID2 != "2" { 135 t.Fatalf("got back %+v", test) 136 } 137 } 138 139 func Test11111HonorDefaultOmitEmpty(t *testing.T) { 140 // test that an empty struct is minimally 141 // encoding, as if omitempty is applied 142 // everywhere possible. 143 // 144 145 tt := &SimpleTestType{} 146 147 var buf bytes.Buffer 148 149 err := msgp.Encode(&buf, tt) 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 if len(buf.Bytes()) != 1 { 155 t.Fatalf("should have encoding of 1 byte since omitempty is on by default") 156 } 157 158 tnew := new(SimpleTestType) 159 160 err = msgp.Decode(&buf, tnew) 161 if err != nil { 162 t.Error(err) 163 } 164 165 if !reflect.DeepEqual(tt, tnew) { 166 t.Logf("in: %#v", tt) 167 t.Logf("out: %#v", tnew) 168 t.Fatal("objects not equal") 169 } 170 }