values_flex_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* // +build testing */
  2. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. package codec
  5. import (
  6. "strings"
  7. "time"
  8. )
  9. const teststrucflexChanCap = 64
  10. // This file contains values used by tests alone.
  11. // This is where we may try out different things,
  12. // that other engines may not support or may barf upon
  13. // e.g. custom extensions for wrapped types, maps with non-string keys, etc.
  14. // Some unused types just stored here
  15. type Bbool bool
  16. type Aarray [1]string
  17. type Sstring string
  18. type Sstructsmall struct {
  19. A int
  20. }
  21. type Sstructbig struct {
  22. A int
  23. B bool
  24. c string
  25. // Sval Sstruct
  26. Ssmallptr *Sstructsmall
  27. Ssmall *Sstructsmall
  28. Sptr *Sstructbig
  29. }
  30. type SstructbigMapBySlice struct {
  31. _struct struct{} `codec:",toarray"`
  32. A int
  33. B bool
  34. c string
  35. // Sval Sstruct
  36. Ssmallptr *Sstructsmall
  37. Ssmall *Sstructsmall
  38. Sptr *Sstructbig
  39. }
  40. // small struct for testing that codecgen works for unexported types
  41. type tLowerFirstLetter struct {
  42. I int
  43. u uint64
  44. S string
  45. b []byte
  46. }
  47. // Some used types
  48. type wrapInt64 int64
  49. type wrapUint8 uint8
  50. type wrapBytes []uint8
  51. type AnonInTestStrucIntf struct {
  52. Islice []interface{}
  53. Ms map[string]interface{}
  54. Nintf interface{} //don't set this, so we can test for nil
  55. T time.Time
  56. Tptr *time.Time
  57. }
  58. var testWRepeated512 wrapBytes
  59. var testStrucTime = time.Date(2012, 2, 2, 2, 2, 2, 2000, time.UTC).UTC()
  60. func init() {
  61. var testARepeated512 [512]byte
  62. for i := range testARepeated512 {
  63. testARepeated512[i] = 'A'
  64. }
  65. testWRepeated512 = wrapBytes(testARepeated512[:])
  66. }
  67. type TestStrucFlex struct {
  68. _struct struct{} `codec:",omitempty"` //set omitempty for every field
  69. TestStrucCommon
  70. Chstr chan string
  71. Mis map[int]string
  72. Mbu64 map[bool]struct{}
  73. Miwu64s map[int]wrapUint64Slice
  74. Mfwss map[float64]wrapStringSlice
  75. Mf32wss map[float32]wrapStringSlice
  76. Mui2wss map[uint64]wrapStringSlice
  77. Msu2wss map[stringUint64T]wrapStringSlice
  78. Ci64 wrapInt64
  79. Swrapbytes []wrapBytes
  80. Swrapuint8 []wrapUint8
  81. ArrStrUi64T [4]stringUint64T
  82. Ui64array [4]uint64
  83. Ui64slicearray []*[4]uint64
  84. SintfAarray []interface{}
  85. // make this a ptr, so that it could be set or not.
  86. // for comparison (e.g. with msgp), give it a struct tag (so it is not inlined),
  87. // make this one omitempty (so it is excluded if nil).
  88. *AnonInTestStrucIntf `json:",omitempty"`
  89. //M map[interface{}]interface{} `json:"-",bson:"-"`
  90. Mtsptr map[string]*TestStrucFlex
  91. Mts map[string]TestStrucFlex
  92. Its []*TestStrucFlex
  93. Nteststruc *TestStrucFlex
  94. }
  95. func emptyTestStrucFlex() *TestStrucFlex {
  96. var ts TestStrucFlex
  97. // we initialize and start draining the chan, so that we can decode into it without it blocking due to no consumer
  98. ts.Chstr = make(chan string, teststrucflexChanCap)
  99. go func() {
  100. for range ts.Chstr {
  101. }
  102. }() // drain it
  103. return &ts
  104. }
  105. func newTestStrucFlex(depth, n int, bench, useInterface, useStringKeyOnly bool) (ts *TestStrucFlex) {
  106. ts = &TestStrucFlex{
  107. Chstr: make(chan string, teststrucflexChanCap),
  108. Miwu64s: map[int]wrapUint64Slice{
  109. 5: []wrapUint64{1, 2, 3, 4, 5},
  110. 3: []wrapUint64{1, 2, 3},
  111. },
  112. Mf32wss: map[float32]wrapStringSlice{
  113. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  114. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  115. },
  116. Mui2wss: map[uint64]wrapStringSlice{
  117. 5: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  118. 3: []wrapString{"1.0", "2.0", "3.0"},
  119. },
  120. Mfwss: map[float64]wrapStringSlice{
  121. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  122. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  123. },
  124. Mis: map[int]string{
  125. 1: "one",
  126. 22: "twenty two",
  127. -44: "minus forty four",
  128. },
  129. Mbu64: map[bool]struct{}{false: {}, true: {}},
  130. Ci64: -22,
  131. Swrapbytes: []wrapBytes{ // lengths of 1, 2, 4, 8, 16, 32, 64, 128, 256,
  132. testWRepeated512[:1],
  133. testWRepeated512[:2],
  134. testWRepeated512[:4],
  135. testWRepeated512[:8],
  136. testWRepeated512[:16],
  137. testWRepeated512[:32],
  138. testWRepeated512[:64],
  139. testWRepeated512[:128],
  140. testWRepeated512[:256],
  141. testWRepeated512[:512],
  142. },
  143. Swrapuint8: []wrapUint8{
  144. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  145. },
  146. Ui64array: [4]uint64{4, 16, 64, 256},
  147. ArrStrUi64T: [4]stringUint64T{{"4", 4}, {"3", 3}, {"2", 2}, {"1", 1}},
  148. SintfAarray: []interface{}{Aarray{"s"}},
  149. }
  150. numChanSend := cap(ts.Chstr) / 4 // 8
  151. for i := 0; i < numChanSend; i++ {
  152. ts.Chstr <- strings.Repeat("A", i+1)
  153. }
  154. ts.Ui64slicearray = []*[4]uint64{&ts.Ui64array, &ts.Ui64array}
  155. if useInterface {
  156. ts.AnonInTestStrucIntf = &AnonInTestStrucIntf{
  157. Islice: []interface{}{strRpt(n, "true"), true, strRpt(n, "no"), false, uint64(288), float64(0.4)},
  158. Ms: map[string]interface{}{
  159. strRpt(n, "true"): strRpt(n, "true"),
  160. strRpt(n, "int64(9)"): false,
  161. },
  162. T: testStrucTime,
  163. }
  164. }
  165. populateTestStrucCommon(&ts.TestStrucCommon, n, bench, useInterface, useStringKeyOnly)
  166. if depth > 0 {
  167. depth--
  168. if ts.Mtsptr == nil {
  169. ts.Mtsptr = make(map[string]*TestStrucFlex)
  170. }
  171. if ts.Mts == nil {
  172. ts.Mts = make(map[string]TestStrucFlex)
  173. }
  174. ts.Mtsptr["0"] = newTestStrucFlex(depth, n, bench, useInterface, useStringKeyOnly)
  175. ts.Mts["0"] = *(ts.Mtsptr["0"])
  176. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  177. }
  178. return
  179. }