values_flex_test.go 5.1 KB

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