values_flex_test.go 6.3 KB

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