values_flex_test.go 4.6 KB

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