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.

182 lines
3.3 KiB

  1. package gen
  2. import (
  3. "io"
  4. "text/template"
  5. )
  6. var (
  7. marshalTestTempl = template.New("MarshalTest")
  8. encodeTestTempl = template.New("EncodeTest")
  9. )
  10. // TODO(philhofer):
  11. // for simplicity's sake, right now
  12. // we can only generate tests for types
  13. // that can be initialized with the
  14. // "Type{}" syntax.
  15. // we should support all the types.
  16. func mtest(w io.Writer) *mtestGen {
  17. return &mtestGen{w: w}
  18. }
  19. type mtestGen struct {
  20. passes
  21. w io.Writer
  22. }
  23. func (m *mtestGen) Execute(p Elem) error {
  24. p = m.applyall(p)
  25. if p != nil && IsPrintable(p) {
  26. switch p.(type) {
  27. case *Struct, *Array, *Slice, *Map:
  28. return marshalTestTempl.Execute(m.w, p)
  29. }
  30. }
  31. return nil
  32. }
  33. func (m *mtestGen) Method() Method { return marshaltest }
  34. type etestGen struct {
  35. passes
  36. w io.Writer
  37. }
  38. func etest(w io.Writer) *etestGen {
  39. return &etestGen{w: w}
  40. }
  41. func (e *etestGen) Execute(p Elem) error {
  42. p = e.applyall(p)
  43. if p != nil && IsPrintable(p) {
  44. switch p.(type) {
  45. case *Struct, *Array, *Slice, *Map:
  46. return encodeTestTempl.Execute(e.w, p)
  47. }
  48. }
  49. return nil
  50. }
  51. func (e *etestGen) Method() Method { return encodetest }
  52. func init() {
  53. template.Must(marshalTestTempl.Parse(`func TestMarshalUnmarshal{{.TypeName}}(t *testing.T) {
  54. v := {{.TypeName}}{}
  55. bts, err := v.MarshalMsg(nil)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. left, err := v.UnmarshalMsg(bts)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if len(left) > 0 {
  64. t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
  65. }
  66. left, err = msgp.Skip(bts)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if len(left) > 0 {
  71. t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
  72. }
  73. }
  74. func BenchmarkMarshalMsg{{.TypeName}}(b *testing.B) {
  75. v := {{.TypeName}}{}
  76. b.ReportAllocs()
  77. b.ResetTimer()
  78. for i:=0; i<b.N; i++ {
  79. v.MarshalMsg(nil)
  80. }
  81. }
  82. func BenchmarkAppendMsg{{.TypeName}}(b *testing.B) {
  83. v := {{.TypeName}}{}
  84. bts := make([]byte, 0, v.Msgsize())
  85. bts, _ = v.MarshalMsg(bts[0:0])
  86. b.SetBytes(int64(len(bts)))
  87. b.ReportAllocs()
  88. b.ResetTimer()
  89. for i:=0; i<b.N; i++ {
  90. bts, _ = v.MarshalMsg(bts[0:0])
  91. }
  92. }
  93. func BenchmarkUnmarshal{{.TypeName}}(b *testing.B) {
  94. v := {{.TypeName}}{}
  95. bts, _ := v.MarshalMsg(nil)
  96. b.ReportAllocs()
  97. b.SetBytes(int64(len(bts)))
  98. b.ResetTimer()
  99. for i:=0; i<b.N; i++ {
  100. _, err := v.UnmarshalMsg(bts)
  101. if err != nil {
  102. b.Fatal(err)
  103. }
  104. }
  105. }
  106. `))
  107. template.Must(encodeTestTempl.Parse(`func TestEncodeDecode{{.TypeName}}(t *testing.T) {
  108. v := {{.TypeName}}{}
  109. var buf bytes.Buffer
  110. msgp.Encode(&buf, &v)
  111. m := v.Msgsize()
  112. if buf.Len() > m {
  113. t.Logf("WARNING: Msgsize() for %v is inaccurate", v)
  114. }
  115. vn := {{.TypeName}}{}
  116. err := msgp.Decode(&buf, &vn)
  117. if err != nil {
  118. t.Error(err)
  119. }
  120. buf.Reset()
  121. msgp.Encode(&buf, &v)
  122. err = msgp.NewReader(&buf).Skip()
  123. if err != nil {
  124. t.Error(err)
  125. }
  126. }
  127. func BenchmarkEncode{{.TypeName}}(b *testing.B) {
  128. v := {{.TypeName}}{}
  129. var buf bytes.Buffer
  130. msgp.Encode(&buf, &v)
  131. b.SetBytes(int64(buf.Len()))
  132. en := msgp.NewWriter(msgp.Nowhere)
  133. b.ReportAllocs()
  134. b.ResetTimer()
  135. for i:=0; i<b.N; i++ {
  136. v.EncodeMsg(en)
  137. }
  138. en.Flush()
  139. }
  140. func BenchmarkDecode{{.TypeName}}(b *testing.B) {
  141. v := {{.TypeName}}{}
  142. var buf bytes.Buffer
  143. msgp.Encode(&buf, &v)
  144. b.SetBytes(int64(buf.Len()))
  145. rd := msgp.NewEndlessReader(buf.Bytes(), b)
  146. dc := msgp.NewReader(rd)
  147. b.ReportAllocs()
  148. b.ResetTimer()
  149. for i:=0; i<b.N; i++ {
  150. err := v.DecodeMsg(dc)
  151. if err != nil {
  152. b.Fatal(err)
  153. }
  154. }
  155. }
  156. `))
  157. }