You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
2.9 KiB

  1. package _generated
  2. import (
  3. "bytes"
  4. "github.com/tinylib/msgp/msgp"
  5. "reflect"
  6. "testing"
  7. "time"
  8. )
  9. // benchmark encoding a small, "fast" type.
  10. // the point here is to see how much garbage
  11. // is generated intrinsically by the encoding/
  12. // decoding process as opposed to the nature
  13. // of the struct.
  14. func BenchmarkFastEncode(b *testing.B) {
  15. v := &TestFast{
  16. Lat: 40.12398,
  17. Long: -41.9082,
  18. Alt: 201.08290,
  19. Data: []byte("whaaaaargharbl"),
  20. }
  21. var buf bytes.Buffer
  22. msgp.Encode(&buf, v)
  23. en := msgp.NewWriter(msgp.Nowhere)
  24. b.SetBytes(int64(buf.Len()))
  25. b.ReportAllocs()
  26. b.ResetTimer()
  27. for i := 0; i < b.N; i++ {
  28. v.EncodeMsg(en)
  29. }
  30. en.Flush()
  31. }
  32. // benchmark decoding a small, "fast" type.
  33. // the point here is to see how much garbage
  34. // is generated intrinsically by the encoding/
  35. // decoding process as opposed to the nature
  36. // of the struct.
  37. func BenchmarkFastDecode(b *testing.B) {
  38. v := &TestFast{
  39. Lat: 40.12398,
  40. Long: -41.9082,
  41. Alt: 201.08290,
  42. Data: []byte("whaaaaargharbl"),
  43. }
  44. var buf bytes.Buffer
  45. msgp.Encode(&buf, v)
  46. dc := msgp.NewReader(msgp.NewEndlessReader(buf.Bytes(), b))
  47. b.SetBytes(int64(buf.Len()))
  48. b.ReportAllocs()
  49. b.ResetTimer()
  50. for i := 0; i < b.N; i++ {
  51. v.DecodeMsg(dc)
  52. }
  53. }
  54. func (a *TestType) Equal(b *TestType) bool {
  55. // compare times, then zero out those
  56. // fields, perform a DeepEqual, and restore them
  57. ta, tb := a.Time, b.Time
  58. if !ta.Equal(tb) {
  59. return false
  60. }
  61. a.Time, b.Time = time.Time{}, time.Time{}
  62. ok := reflect.DeepEqual(a, b)
  63. a.Time, b.Time = ta, tb
  64. return ok
  65. }
  66. // This covers the following cases:
  67. // - Recursive types
  68. // - Non-builtin identifiers (and recursive types)
  69. // - time.Time
  70. // - map[string]string
  71. // - anonymous structs
  72. //
  73. func Test1EncodeDecode(t *testing.T) {
  74. f := 32.00
  75. tt := &TestType{
  76. F: &f,
  77. Els: map[string]string{
  78. "thing_one": "one",
  79. "thing_two": "two",
  80. },
  81. Obj: struct {
  82. ValueA string `msg:"value_a"`
  83. ValueB []byte `msg:"value_b"`
  84. }{
  85. ValueA: "here's the first inner value",
  86. ValueB: []byte("here's the second inner value"),
  87. },
  88. Child: nil,
  89. Time: time.Now(),
  90. Appended: msgp.Raw([]byte{0xc0}), // 'nil'
  91. }
  92. var buf bytes.Buffer
  93. err := msgp.Encode(&buf, tt)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. tnew := new(TestType)
  98. err = msgp.Decode(&buf, tnew)
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. if !tt.Equal(tnew) {
  103. t.Logf("in: %v", tt)
  104. t.Logf("out: %v", tnew)
  105. t.Fatal("objects not equal")
  106. }
  107. tanother := new(TestType)
  108. buf.Reset()
  109. msgp.Encode(&buf, tt)
  110. var left []byte
  111. left, err = tanother.UnmarshalMsg(buf.Bytes())
  112. if err != nil {
  113. t.Error(err)
  114. }
  115. if len(left) > 0 {
  116. t.Errorf("%d bytes left", len(left))
  117. }
  118. if !tt.Equal(tanother) {
  119. t.Logf("in: %v", tt)
  120. t.Logf("out: %v", tanother)
  121. t.Fatal("objects not equal")
  122. }
  123. }
  124. func TestIssue168(t *testing.T) {
  125. buf := bytes.Buffer{}
  126. test := TestObj{}
  127. msgp.Encode(&buf, &TestObj{ID1: "1", ID2: "2"})
  128. msgp.Decode(&buf, &test)
  129. if test.ID1 != "1" || test.ID2 != "2" {
  130. t.Fatalf("got back %+v", test)
  131. }
  132. }