values_flex_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* // +build testing */
  2. // Copyright (c) 2012-2015 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. Ui64array [4]uint64
  78. Ui64slicearray []*[4]uint64
  79. // make this a ptr, so that it could be set or not.
  80. // for comparison (e.g. with msgp), give it a struct tag (so it is not inlined),
  81. // make this one omitempty (so it is excluded if nil).
  82. *AnonInTestStrucIntf `json:",omitempty"`
  83. //M map[interface{}]interface{} `json:"-",bson:"-"`
  84. Mtsptr map[string]*TestStrucFlex
  85. Mts map[string]TestStrucFlex
  86. Its []*TestStrucFlex
  87. Nteststruc *TestStrucFlex
  88. }
  89. func newTestStrucFlex(depth, n int, bench, useInterface, useStringKeyOnly bool) (ts *TestStrucFlex) {
  90. ts = &TestStrucFlex{
  91. Miwu64s: map[int]wrapUint64Slice{
  92. 5: []wrapUint64{1, 2, 3, 4, 5},
  93. 3: []wrapUint64{1, 2, 3},
  94. },
  95. Mf32wss: map[float32]wrapStringSlice{
  96. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  97. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  98. },
  99. Mui2wss: map[uint64]wrapStringSlice{
  100. 5: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  101. 3: []wrapString{"1.0", "2.0", "3.0"},
  102. },
  103. Mfwss: map[float64]wrapStringSlice{
  104. 5.0: []wrapString{"1.0", "2.0", "3.0", "4.0", "5.0"},
  105. 3.0: []wrapString{"1.0", "2.0", "3.0"},
  106. },
  107. Mis: map[int]string{
  108. 1: "one",
  109. 22: "twenty two",
  110. -44: "minus forty four",
  111. },
  112. Mbu64: map[bool]struct{}{false: {}, true: {}},
  113. Ci64: -22,
  114. Swrapbytes: []wrapBytes{ // lengths of 1, 2, 4, 8, 16, 32, 64, 128, 256,
  115. testWRepeated512[:1],
  116. testWRepeated512[:2],
  117. testWRepeated512[:4],
  118. testWRepeated512[:8],
  119. testWRepeated512[:16],
  120. testWRepeated512[:32],
  121. testWRepeated512[:64],
  122. testWRepeated512[:128],
  123. testWRepeated512[:256],
  124. testWRepeated512[:512],
  125. },
  126. Swrapuint8: []wrapUint8{
  127. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  128. },
  129. Ui64array: [4]uint64{4, 16, 64, 256},
  130. }
  131. ts.Ui64slicearray = []*[4]uint64{&ts.Ui64array, &ts.Ui64array}
  132. if useInterface {
  133. ts.AnonInTestStrucIntf = &AnonInTestStrucIntf{
  134. Islice: []interface{}{strRpt(n, "true"), true, strRpt(n, "no"), false, uint64(288), float64(0.4)},
  135. Ms: map[string]interface{}{
  136. strRpt(n, "true"): strRpt(n, "true"),
  137. strRpt(n, "int64(9)"): false,
  138. },
  139. T: testStrucTime,
  140. }
  141. }
  142. populateTestStrucCommon(&ts.TestStrucCommon, n, bench, useInterface, useStringKeyOnly)
  143. if depth > 0 {
  144. depth--
  145. if ts.Mtsptr == nil {
  146. ts.Mtsptr = make(map[string]*TestStrucFlex)
  147. }
  148. if ts.Mts == nil {
  149. ts.Mts = make(map[string]TestStrucFlex)
  150. }
  151. ts.Mtsptr["0"] = newTestStrucFlex(depth, n, bench, useInterface, useStringKeyOnly)
  152. ts.Mts["0"] = *(ts.Mtsptr["0"])
  153. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  154. }
  155. return
  156. }