values_flex_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // comment this out // // + 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. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. const teststrucflexChanCap = 64
  11. // This file contains values used by tests alone.
  12. // This is where we may try out different things,
  13. // that other engines may not support or may barf upon
  14. // e.g. custom extensions for wrapped types, maps with non-string keys, etc.
  15. // some funky types to test codecgen
  16. type codecgenA struct {
  17. ZZ []byte
  18. }
  19. type codecgenB struct {
  20. AA codecgenA
  21. }
  22. type codecgenC struct {
  23. _struct struct{} `codec:",omitempty"`
  24. BB codecgenB
  25. }
  26. type TestCodecgenG struct {
  27. TestCodecgenG int
  28. }
  29. type codecgenH struct {
  30. TestCodecgenG
  31. }
  32. type codecgenI struct {
  33. codecgenH
  34. }
  35. type codecgenK struct {
  36. X int
  37. Y string
  38. }
  39. type codecgenL struct {
  40. X int
  41. Y uint32
  42. }
  43. type codecgenM struct {
  44. codecgenK
  45. codecgenL
  46. }
  47. // Some unused types just stored here
  48. type Bbool bool
  49. type Aarray [1]string
  50. type Sstring string
  51. type Sstructsmall struct {
  52. A int
  53. }
  54. type Sstructbig struct {
  55. A int
  56. B bool
  57. c string
  58. // Sval Sstruct
  59. Ssmallptr *Sstructsmall
  60. Ssmall *Sstructsmall
  61. Sptr *Sstructbig
  62. }
  63. type SstructbigMapBySlice struct {
  64. _struct struct{} `codec:",toarray"`
  65. A int
  66. B bool
  67. c string
  68. // Sval Sstruct
  69. Ssmallptr *Sstructsmall
  70. Ssmall *Sstructsmall
  71. Sptr *Sstructbig
  72. }
  73. // small struct for testing that codecgen works for unexported types
  74. type tLowerFirstLetter struct {
  75. I int
  76. u uint64
  77. S string
  78. b []byte
  79. }
  80. // Some used types
  81. type wrapInt64 int64
  82. type wrapUint8 uint8
  83. type wrapBytes []uint8
  84. type AnonInTestStrucIntf struct {
  85. Islice []interface{}
  86. Ms map[string]interface{}
  87. Nintf interface{} //don't set this, so we can test for nil
  88. T time.Time
  89. Tptr *time.Time
  90. }
  91. type missingFielderT1 struct {
  92. S string
  93. B bool
  94. f float64
  95. i int64
  96. }
  97. func (t *missingFielderT1) CodecMissingField(field []byte, value interface{}) bool {
  98. // xdebugf(">> calling CodecMissingField with field: %s, value: %v", field, value)
  99. switch string(field) {
  100. case "F":
  101. t.f = value.(float64)
  102. case "I":
  103. t.i = value.(int64)
  104. default:
  105. return false
  106. }
  107. return true
  108. }
  109. func (t *missingFielderT1) CodecMissingFields() map[string]interface{} {
  110. return map[string]interface{}{"F": t.f, "I": t.i}
  111. }
  112. type missingFielderT2 struct {
  113. S string
  114. B bool
  115. F float64
  116. I int64
  117. }
  118. type testSelfExtHelper struct {
  119. S string
  120. I int64
  121. B bool
  122. }
  123. type TestSelfExtImpl struct {
  124. testSelfExtHelper
  125. }
  126. var testWRepeated512 wrapBytes
  127. var testStrucTime = time.Date(2012, 2, 2, 2, 2, 2, 2000, time.UTC).UTC()
  128. func init() {
  129. var testARepeated512 [512]byte
  130. for i := range testARepeated512 {
  131. testARepeated512[i] = 'A'
  132. }
  133. testWRepeated512 = wrapBytes(testARepeated512[:])
  134. }
  135. type TestStrucFlex struct {
  136. _struct struct{} `codec:",omitempty"` //set omitempty for every field
  137. TestStrucCommon
  138. Chstr chan string
  139. Mis map[int]string
  140. Mbu64 map[bool]struct{}
  141. Miwu64s map[int]wrapUint64Slice
  142. Mfwss map[float64]wrapStringSlice
  143. Mf32wss map[float32]wrapStringSlice
  144. Mui2wss map[uint64]wrapStringSlice
  145. // DecodeNaked bombs because stringUint64T is decoded as a map,
  146. // and a map cannot be the key type of a map.
  147. // Ensure this is set to nil if decoding into a nil interface{}.
  148. Msu2wss map[stringUint64T]wrapStringSlice
  149. Ci64 wrapInt64
  150. Swrapbytes []wrapBytes
  151. Swrapuint8 []wrapUint8
  152. ArrStrUi64T [4]stringUint64T
  153. Ui64array [4]uint64
  154. Ui64slicearray []*[4]uint64
  155. SintfAarray []interface{}
  156. // Ensure this is set to nil if decoding into a nil interface{}.
  157. MstrUi64TSelf map[stringUint64T]*stringUint64T
  158. // make this a ptr, so that it could be set or not.
  159. // for comparison (e.g. with msgp), give it a struct tag (so it is not inlined),
  160. // make this one omitempty (so it is excluded if nil).
  161. *AnonInTestStrucIntf `json:",omitempty"`
  162. //M map[interface{}]interface{} `json:"-",bson:"-"`
  163. Mtsptr map[string]*TestStrucFlex
  164. Mts map[string]TestStrucFlex
  165. Its []*TestStrucFlex
  166. Nteststruc *TestStrucFlex
  167. }
  168. func emptyTestStrucFlex() *TestStrucFlex {
  169. var ts TestStrucFlex
  170. // we initialize and start draining the chan, so that we can decode into it without it blocking due to no consumer
  171. ts.Chstr = make(chan string, teststrucflexChanCap)
  172. go func() {
  173. for range ts.Chstr {
  174. }
  175. }() // drain it
  176. return &ts
  177. }
  178. func newTestStrucFlex(depth, n int, bench, useInterface, useStringKeyOnly bool) (ts *TestStrucFlex) {
  179. ts = &TestStrucFlex{
  180. Chstr: make(chan string, teststrucflexChanCap),
  181. Miwu64s: map[int]wrapUint64Slice{
  182. 5: []wrapUint64{1, 2, 3, 4, 5},
  183. 3: []wrapUint64{1, 2, 3},
  184. },
  185. Mf32wss: map[float32]wrapStringSlice{
  186. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  187. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  188. },
  189. Mui2wss: map[uint64]wrapStringSlice{
  190. 5: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  191. 3: []wrapString{"1.0", "2.0", "3.0"},
  192. },
  193. Mfwss: map[float64]wrapStringSlice{
  194. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  195. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  196. },
  197. // DecodeNaked bombs here, because the stringUint64T is decoded as a map,
  198. // and a map cannot be the key type of a map.
  199. // Ensure this is set to nil if decoding into a nil interface{}.
  200. Msu2wss: map[stringUint64T]wrapStringSlice{
  201. {"5", 5}: []wrapString{"1", "2", "3", "4", "5"},
  202. {"3", 3}: []wrapString{"1", "2", "3"},
  203. },
  204. Mis: map[int]string{
  205. 1: "one",
  206. 22: "twenty two",
  207. -44: "minus forty four",
  208. },
  209. Mbu64: map[bool]struct{}{false: {}, true: {}},
  210. Ci64: -22,
  211. Swrapbytes: []wrapBytes{ // lengths of 1, 2, 4, 8, 16, 32, 64, 128, 256,
  212. testWRepeated512[:1],
  213. testWRepeated512[:2],
  214. testWRepeated512[:4],
  215. testWRepeated512[:8],
  216. testWRepeated512[:16],
  217. testWRepeated512[:32],
  218. testWRepeated512[:64],
  219. testWRepeated512[:128],
  220. testWRepeated512[:256],
  221. testWRepeated512[:512],
  222. },
  223. Swrapuint8: []wrapUint8{
  224. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  225. },
  226. Ui64array: [4]uint64{4, 16, 64, 256},
  227. ArrStrUi64T: [4]stringUint64T{{"4", 4}, {"3", 3}, {"2", 2}, {"1", 1}},
  228. SintfAarray: []interface{}{Aarray{"s"}},
  229. MstrUi64TSelf: make(map[stringUint64T]*stringUint64T, numStrUi64T),
  230. }
  231. for i := uint64(0); i < numStrUi64T; i++ {
  232. ss := stringUint64T{S: strings.Repeat(strconv.FormatUint(i, 10), 4), U: i}
  233. // Ensure this is set to nil if decoding into a nil interface{}.
  234. ts.MstrUi64TSelf[ss] = &ss
  235. }
  236. numChanSend := cap(ts.Chstr) / 4 // 8
  237. for i := 0; i < numChanSend; i++ {
  238. ts.Chstr <- strings.Repeat("A", i+1)
  239. }
  240. ts.Ui64slicearray = []*[4]uint64{&ts.Ui64array, &ts.Ui64array}
  241. if useInterface {
  242. ts.AnonInTestStrucIntf = &AnonInTestStrucIntf{
  243. Islice: []interface{}{strRpt(n, "true"), true, strRpt(n, "no"), false, uint64(288), float64(0.4)},
  244. Ms: map[string]interface{}{
  245. strRpt(n, "true"): strRpt(n, "true"),
  246. strRpt(n, "int64(9)"): false,
  247. },
  248. T: testStrucTime,
  249. }
  250. }
  251. populateTestStrucCommon(&ts.TestStrucCommon, n, bench, useInterface, useStringKeyOnly)
  252. if depth > 0 {
  253. depth--
  254. if ts.Mtsptr == nil {
  255. ts.Mtsptr = make(map[string]*TestStrucFlex)
  256. }
  257. if ts.Mts == nil {
  258. ts.Mts = make(map[string]TestStrucFlex)
  259. }
  260. ts.Mtsptr["0"] = newTestStrucFlex(depth, n, bench, useInterface, useStringKeyOnly)
  261. ts.Mts["0"] = *(ts.Mtsptr["0"])
  262. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  263. }
  264. return
  265. }