values_flex_test.go 7.4 KB

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