values_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // This file contains values used by tests and benchmarks.
  6. // JSON/BSON do not like maps with keys that are not strings,
  7. // so we only use maps with string keys here.
  8. import (
  9. "math"
  10. "time"
  11. )
  12. var testStrucTime = time.Date(2012, 2, 2, 2, 2, 2, 2000, time.UTC).UTC()
  13. type AnonInTestStruc struct {
  14. AS string
  15. AI64 int64
  16. AI16 int16
  17. AUi64 uint64
  18. ASslice []string
  19. AI64slice []int64
  20. AF64slice []float64
  21. // AMI32U32 map[int32]uint32
  22. // AMU32F64 map[uint32]float64 // json/bson do not like it
  23. AMSU16 map[string]uint16
  24. }
  25. type AnonInTestStrucIntf struct {
  26. Islice []interface{}
  27. Ms map[string]interface{}
  28. Nintf interface{} //don't set this, so we can test for nil
  29. T time.Time
  30. }
  31. type TestStruc struct {
  32. _struct struct{} `codec:",omitempty"` //set omitempty for every field
  33. S string
  34. I64 int64
  35. I16 int16
  36. Ui64 uint64
  37. Ui8 uint8
  38. B bool
  39. By uint8 // byte: msgp doesn't like byte
  40. Sslice []string
  41. I64slice []int64
  42. I16slice []int16
  43. Ui64slice []uint64
  44. Ui8slice []uint8
  45. Bslice []bool
  46. Byslice []byte
  47. Iptrslice []*int64
  48. // TODO: test these separately, specifically for reflection and codecgen.
  49. // Unfortunately, ffjson doesn't support these. Its compilation even fails.
  50. // Ui64array [4]uint64
  51. // Ui64slicearray [][4]uint64
  52. AnonInTestStruc
  53. //M map[interface{}]interface{} `json:"-",bson:"-"`
  54. Msi64 map[string]int64
  55. // make this a ptr, so that it could be set or not.
  56. // for comparison (e.g. with msgp), give it a struct tag (so it is not inlined),
  57. // make this one omitempty (so it is included if nil).
  58. *AnonInTestStrucIntf `codec:",omitempty"`
  59. Nmap map[string]bool //don't set this, so we can test for nil
  60. Nslice []byte //don't set this, so we can test for nil
  61. Nint64 *int64 //don't set this, so we can test for nil
  62. Mtsptr map[string]*TestStruc
  63. Mts map[string]TestStruc
  64. Its []*TestStruc
  65. Nteststruc *TestStruc
  66. }
  67. // small struct for testing that codecgen works for unexported types
  68. type tLowerFirstLetter struct {
  69. I int
  70. u uint64
  71. S string
  72. b []byte
  73. }
  74. func newTestStruc(depth int, bench bool, useInterface, useStringKeyOnly bool) (ts *TestStruc) {
  75. var i64a, i64b, i64c, i64d int64 = 64, 6464, 646464, 64646464
  76. ts = &TestStruc{
  77. S: "some string",
  78. I64: math.MaxInt64 * 2 / 3, // 64,
  79. I16: 1616,
  80. Ui64: uint64(int64(math.MaxInt64 * 2 / 3)), // 64, //don't use MaxUint64, as bson can't write it
  81. Ui8: 160,
  82. B: true,
  83. By: 5,
  84. Sslice: []string{"one", "two", "three"},
  85. I64slice: []int64{1111, 2222, 3333},
  86. I16slice: []int16{44, 55, 66},
  87. Ui64slice: []uint64{12121212, 34343434, 56565656},
  88. Ui8slice: []uint8{210, 211, 212},
  89. Bslice: []bool{true, false, true, false},
  90. Byslice: []byte{13, 14, 15},
  91. Msi64: map[string]int64{
  92. "one": 1,
  93. "two": 2,
  94. },
  95. AnonInTestStruc: AnonInTestStruc{
  96. // There's more leeway in altering this.
  97. AS: "A-String",
  98. AI64: -64646464,
  99. AI16: 1616,
  100. AUi64: 64646464,
  101. // (U+1D11E)G-clef character may be represented in json as "\uD834\uDD1E".
  102. // single reverse solidus character may be represented in json as "\u005C".
  103. // include these in ASslice below.
  104. ASslice: []string{"Aone", "Atwo", "Athree",
  105. "Afour.reverse_solidus.\u005c", "Afive.Gclef.\U0001d11E"},
  106. AI64slice: []int64{1, -22, 333, -4444, 55555, -666666},
  107. AMSU16: map[string]uint16{"1": 1, "22": 2, "333": 3, "4444": 4},
  108. AF64slice: []float64{11.11e-11, 22.22E+22, 33.33E-33, 44.44e+44, 555.55E-6, 666.66E6},
  109. },
  110. }
  111. if useInterface {
  112. ts.AnonInTestStrucIntf = &AnonInTestStrucIntf{
  113. Islice: []interface{}{"true", true, "no", false, uint64(288), float64(0.4)},
  114. Ms: map[string]interface{}{
  115. "true": "true",
  116. "int64(9)": false,
  117. },
  118. T: testStrucTime,
  119. }
  120. }
  121. //For benchmarks, some things will not work.
  122. if !bench {
  123. //json and bson require string keys in maps
  124. //ts.M = map[interface{}]interface{}{
  125. // true: "true",
  126. // int8(9): false,
  127. //}
  128. //gob cannot encode nil in element in array (encodeArray: nil element)
  129. ts.Iptrslice = []*int64{nil, &i64a, nil, &i64b, nil, &i64c, nil, &i64d, nil}
  130. // ts.Iptrslice = nil
  131. }
  132. if !useStringKeyOnly {
  133. // ts.AnonInTestStruc.AMU32F64 = map[uint32]float64{1: 1, 2: 2, 3: 3} // Json/Bson barf
  134. }
  135. if depth > 0 {
  136. depth--
  137. if ts.Mtsptr == nil {
  138. ts.Mtsptr = make(map[string]*TestStruc)
  139. }
  140. if ts.Mts == nil {
  141. ts.Mts = make(map[string]TestStruc)
  142. }
  143. ts.Mtsptr["0"] = newTestStruc(depth, bench, useInterface, useStringKeyOnly)
  144. ts.Mts["0"] = *(ts.Mtsptr["0"])
  145. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  146. }
  147. return
  148. }
  149. // Some other types
  150. type Sstring string
  151. type Bbool bool
  152. type Sstructsmall struct {
  153. A int
  154. }
  155. type Sstructbig struct {
  156. A int
  157. B bool
  158. c string
  159. // Sval Sstruct
  160. Ssmallptr *Sstructsmall
  161. Ssmall *Sstructsmall
  162. Sptr *Sstructbig
  163. }
  164. type SstructbigMapBySlice struct {
  165. _struct struct{} `codec:",toarray"`
  166. A int
  167. B bool
  168. c string
  169. // Sval Sstruct
  170. Ssmallptr *Sstructsmall
  171. Ssmall *Sstructsmall
  172. Sptr *Sstructbig
  173. }
  174. type Sinterface interface {
  175. Noop()
  176. }