values_flex_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. switch string(field) {
  99. case "F":
  100. t.f = value.(float64)
  101. case "I":
  102. t.i = value.(int64)
  103. default:
  104. return false
  105. }
  106. return true
  107. }
  108. func (t *missingFielderT1) CodecMissingFields() map[string]interface{} {
  109. return map[string]interface{}{"F": t.f, "I": t.i}
  110. }
  111. type missingFielderT2 struct {
  112. S string
  113. B bool
  114. F float64
  115. I int64
  116. }
  117. type testSelfExtHelper struct {
  118. S string
  119. I int64
  120. B bool
  121. }
  122. type TestSelfExtImpl struct {
  123. testSelfExtHelper
  124. }
  125. type TestSelfExtImpl2 struct {
  126. M string
  127. O bool
  128. }
  129. type TestTwoNakedInterfaces struct {
  130. A interface{}
  131. B interface{}
  132. }
  133. var testWRepeated512 wrapBytes
  134. var testStrucTime = time.Date(2012, 2, 2, 2, 2, 2, 2000, time.UTC).UTC()
  135. func init() {
  136. var testARepeated512 [512]byte
  137. for i := range testARepeated512 {
  138. testARepeated512[i] = 'A'
  139. }
  140. testWRepeated512 = wrapBytes(testARepeated512[:])
  141. }
  142. type TestStrucFlex struct {
  143. _struct struct{} `codec:",omitempty"` //set omitempty for every field
  144. TestStrucCommon
  145. Chstr chan string
  146. Mis map[int]string
  147. Mbu64 map[bool]struct{}
  148. Miwu64s map[int]wrapUint64Slice
  149. Mfwss map[float64]wrapStringSlice
  150. Mf32wss map[float32]wrapStringSlice
  151. Mui2wss map[uint64]wrapStringSlice
  152. // DecodeNaked bombs because stringUint64T is decoded as a map,
  153. // and a map cannot be the key type of a map.
  154. // Ensure this is set to nil if decoding into a nil interface{}.
  155. Msu2wss map[stringUint64T]wrapStringSlice
  156. Ci64 wrapInt64
  157. Swrapbytes []wrapBytes
  158. Swrapuint8 []wrapUint8
  159. ArrStrUi64T [4]stringUint64T
  160. Ui64array [4]uint64
  161. Ui64slicearray []*[4]uint64
  162. SintfAarray []interface{}
  163. // Ensure this is set to nil if decoding into a nil interface{}.
  164. MstrUi64TSelf map[stringUint64T]*stringUint64T
  165. // make this a ptr, so that it could be set or not.
  166. // for comparison (e.g. with msgp), give it a struct tag (so it is not inlined),
  167. // make this one omitempty (so it is excluded if nil).
  168. *AnonInTestStrucIntf `json:",omitempty"`
  169. //M map[interface{}]interface{} `json:"-",bson:"-"`
  170. Mtsptr map[string]*TestStrucFlex
  171. Mts map[string]TestStrucFlex
  172. Its []*TestStrucFlex
  173. Nteststruc *TestStrucFlex
  174. }
  175. func emptyTestStrucFlex() *TestStrucFlex {
  176. var ts TestStrucFlex
  177. // we initialize and start draining the chan, so that we can decode into it without it blocking due to no consumer
  178. ts.Chstr = make(chan string, teststrucflexChanCap)
  179. go func() {
  180. for range ts.Chstr {
  181. }
  182. }() // drain it
  183. return &ts
  184. }
  185. func newTestStrucFlex(depth, n int, bench, useInterface, useStringKeyOnly bool) (ts *TestStrucFlex) {
  186. ts = &TestStrucFlex{
  187. Chstr: make(chan string, teststrucflexChanCap),
  188. Miwu64s: map[int]wrapUint64Slice{
  189. 5: []wrapUint64{1, 2, 3, 4, 5},
  190. 3: []wrapUint64{1, 2, 3},
  191. },
  192. Mf32wss: map[float32]wrapStringSlice{
  193. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  194. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  195. },
  196. Mui2wss: map[uint64]wrapStringSlice{
  197. 5: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  198. 3: []wrapString{"1.0", "2.0", "3.0"},
  199. },
  200. Mfwss: map[float64]wrapStringSlice{
  201. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  202. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  203. },
  204. // DecodeNaked bombs here, because the stringUint64T is decoded as a map,
  205. // and a map cannot be the key type of a map.
  206. // Ensure this is set to nil if decoding into a nil interface{}.
  207. Msu2wss: map[stringUint64T]wrapStringSlice{
  208. {"5", 5}: []wrapString{"1", "2", "3", "4", "5"},
  209. {"3", 3}: []wrapString{"1", "2", "3"},
  210. },
  211. Mis: map[int]string{
  212. 1: "one",
  213. 22: "twenty two",
  214. -44: "minus forty four",
  215. },
  216. Mbu64: map[bool]struct{}{false: {}, true: {}},
  217. Ci64: -22,
  218. Swrapbytes: []wrapBytes{ // lengths of 1, 2, 4, 8, 16, 32, 64, 128, 256,
  219. testWRepeated512[:1],
  220. testWRepeated512[:2],
  221. testWRepeated512[:4],
  222. testWRepeated512[:8],
  223. testWRepeated512[:16],
  224. testWRepeated512[:32],
  225. testWRepeated512[:64],
  226. testWRepeated512[:128],
  227. testWRepeated512[:256],
  228. testWRepeated512[:512],
  229. },
  230. Swrapuint8: []wrapUint8{
  231. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  232. },
  233. Ui64array: [4]uint64{4, 16, 64, 256},
  234. ArrStrUi64T: [4]stringUint64T{{"4", 4}, {"3", 3}, {"2", 2}, {"1", 1}},
  235. SintfAarray: []interface{}{Aarray{"s"}},
  236. MstrUi64TSelf: make(map[stringUint64T]*stringUint64T, numStrUi64T),
  237. }
  238. for i := uint64(0); i < numStrUi64T; i++ {
  239. ss := stringUint64T{S: strings.Repeat(strconv.FormatUint(i, 10), 4), U: i}
  240. // Ensure this is set to nil if decoding into a nil interface{}.
  241. ts.MstrUi64TSelf[ss] = &ss
  242. }
  243. numChanSend := cap(ts.Chstr) / 4 // 8
  244. for i := 0; i < numChanSend; i++ {
  245. ts.Chstr <- strings.Repeat("A", i+1)
  246. }
  247. ts.Ui64slicearray = []*[4]uint64{&ts.Ui64array, &ts.Ui64array}
  248. if useInterface {
  249. ts.AnonInTestStrucIntf = &AnonInTestStrucIntf{
  250. Islice: []interface{}{strRpt(n, "true"), true, strRpt(n, "no"), false, uint64(288), float64(0.4)},
  251. Ms: map[string]interface{}{
  252. strRpt(n, "true"): strRpt(n, "true"),
  253. strRpt(n, "int64(9)"): false,
  254. },
  255. T: testStrucTime,
  256. }
  257. }
  258. populateTestStrucCommon(&ts.TestStrucCommon, n, bench, useInterface, useStringKeyOnly)
  259. if depth > 0 {
  260. depth--
  261. if ts.Mtsptr == nil {
  262. ts.Mtsptr = make(map[string]*TestStrucFlex)
  263. }
  264. if ts.Mts == nil {
  265. ts.Mts = make(map[string]TestStrucFlex)
  266. }
  267. ts.Mtsptr["0"] = newTestStrucFlex(depth, n, bench, useInterface, useStringKeyOnly)
  268. ts.Mts["0"] = *(ts.Mtsptr["0"])
  269. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  270. }
  271. return
  272. }