values_flex_test.go 5.8 KB

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