values_flex_test.go 5.1 KB

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