values_flex_test.go 6.2 KB

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