values_flex_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. switch string(field) {
  66. case "F":
  67. t.f = value.(float64)
  68. case "I":
  69. t.i = value.(int64)
  70. default:
  71. return false
  72. }
  73. return true
  74. }
  75. func (t *missingFielderT1) CodecMissingFields() map[string]interface{} {
  76. return map[string]interface{}{"F": t.f, "I": t.i}
  77. }
  78. type missingFielderT2 struct {
  79. S string
  80. B bool
  81. F float64
  82. I int64
  83. }
  84. var testWRepeated512 wrapBytes
  85. var testStrucTime = time.Date(2012, 2, 2, 2, 2, 2, 2000, time.UTC).UTC()
  86. func init() {
  87. var testARepeated512 [512]byte
  88. for i := range testARepeated512 {
  89. testARepeated512[i] = 'A'
  90. }
  91. testWRepeated512 = wrapBytes(testARepeated512[:])
  92. }
  93. type TestStrucFlex struct {
  94. _struct struct{} `codec:",omitempty"` //set omitempty for every field
  95. TestStrucCommon
  96. Chstr chan string
  97. Mis map[int]string
  98. Mbu64 map[bool]struct{}
  99. Miwu64s map[int]wrapUint64Slice
  100. Mfwss map[float64]wrapStringSlice
  101. Mf32wss map[float32]wrapStringSlice
  102. Mui2wss map[uint64]wrapStringSlice
  103. Msu2wss map[stringUint64T]wrapStringSlice
  104. Ci64 wrapInt64
  105. Swrapbytes []wrapBytes
  106. Swrapuint8 []wrapUint8
  107. ArrStrUi64T [4]stringUint64T
  108. Ui64array [4]uint64
  109. Ui64slicearray []*[4]uint64
  110. SintfAarray []interface{}
  111. // make this a ptr, so that it could be set or not.
  112. // for comparison (e.g. with msgp), give it a struct tag (so it is not inlined),
  113. // make this one omitempty (so it is excluded if nil).
  114. *AnonInTestStrucIntf `json:",omitempty"`
  115. //M map[interface{}]interface{} `json:"-",bson:"-"`
  116. Mtsptr map[string]*TestStrucFlex
  117. Mts map[string]TestStrucFlex
  118. Its []*TestStrucFlex
  119. Nteststruc *TestStrucFlex
  120. }
  121. func emptyTestStrucFlex() *TestStrucFlex {
  122. var ts TestStrucFlex
  123. // we initialize and start draining the chan, so that we can decode into it without it blocking due to no consumer
  124. ts.Chstr = make(chan string, teststrucflexChanCap)
  125. go func() {
  126. for range ts.Chstr {
  127. }
  128. }() // drain it
  129. return &ts
  130. }
  131. func newTestStrucFlex(depth, n int, bench, useInterface, useStringKeyOnly bool) (ts *TestStrucFlex) {
  132. ts = &TestStrucFlex{
  133. Chstr: make(chan string, teststrucflexChanCap),
  134. Miwu64s: map[int]wrapUint64Slice{
  135. 5: []wrapUint64{1, 2, 3, 4, 5},
  136. 3: []wrapUint64{1, 2, 3},
  137. },
  138. Mf32wss: map[float32]wrapStringSlice{
  139. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  140. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  141. },
  142. Mui2wss: map[uint64]wrapStringSlice{
  143. 5: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  144. 3: []wrapString{"1.0", "2.0", "3.0"},
  145. },
  146. Mfwss: map[float64]wrapStringSlice{
  147. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  148. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  149. },
  150. Mis: map[int]string{
  151. 1: "one",
  152. 22: "twenty two",
  153. -44: "minus forty four",
  154. },
  155. Mbu64: map[bool]struct{}{false: {}, true: {}},
  156. Ci64: -22,
  157. Swrapbytes: []wrapBytes{ // lengths of 1, 2, 4, 8, 16, 32, 64, 128, 256,
  158. testWRepeated512[:1],
  159. testWRepeated512[:2],
  160. testWRepeated512[:4],
  161. testWRepeated512[:8],
  162. testWRepeated512[:16],
  163. testWRepeated512[:32],
  164. testWRepeated512[:64],
  165. testWRepeated512[:128],
  166. testWRepeated512[:256],
  167. testWRepeated512[:512],
  168. },
  169. Swrapuint8: []wrapUint8{
  170. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  171. },
  172. Ui64array: [4]uint64{4, 16, 64, 256},
  173. ArrStrUi64T: [4]stringUint64T{{"4", 4}, {"3", 3}, {"2", 2}, {"1", 1}},
  174. SintfAarray: []interface{}{Aarray{"s"}},
  175. }
  176. numChanSend := cap(ts.Chstr) / 4 // 8
  177. for i := 0; i < numChanSend; i++ {
  178. ts.Chstr <- strings.Repeat("A", i+1)
  179. }
  180. ts.Ui64slicearray = []*[4]uint64{&ts.Ui64array, &ts.Ui64array}
  181. if useInterface {
  182. ts.AnonInTestStrucIntf = &AnonInTestStrucIntf{
  183. Islice: []interface{}{strRpt(n, "true"), true, strRpt(n, "no"), false, uint64(288), float64(0.4)},
  184. Ms: map[string]interface{}{
  185. strRpt(n, "true"): strRpt(n, "true"),
  186. strRpt(n, "int64(9)"): false,
  187. },
  188. T: testStrucTime,
  189. }
  190. }
  191. populateTestStrucCommon(&ts.TestStrucCommon, n, bench, useInterface, useStringKeyOnly)
  192. if depth > 0 {
  193. depth--
  194. if ts.Mtsptr == nil {
  195. ts.Mtsptr = make(map[string]*TestStrucFlex)
  196. }
  197. if ts.Mts == nil {
  198. ts.Mts = make(map[string]TestStrucFlex)
  199. }
  200. ts.Mtsptr["0"] = newTestStrucFlex(depth, n, bench, useInterface, useStringKeyOnly)
  201. ts.Mts["0"] = *(ts.Mtsptr["0"])
  202. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  203. }
  204. return
  205. }