values_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // // +build testing
  2. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a BSD-style 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. AnonInTestStruc
  49. //M map[interface{}]interface{} `json:"-",bson:"-"`
  50. Msi64 map[string]int64
  51. // make this a ptr, so that it could be set or not.
  52. // for comparison (e.g. with msgp), give it a struct tag (so it is not inlined),
  53. // make this one omitempty (so it is included if nil).
  54. *AnonInTestStrucIntf `codec:",omitempty"`
  55. Nmap map[string]bool //don't set this, so we can test for nil
  56. Nslice []byte //don't set this, so we can test for nil
  57. Nint64 *int64 //don't set this, so we can test for nil
  58. Mtsptr map[string]*TestStruc
  59. Mts map[string]TestStruc
  60. Its []*TestStruc
  61. Nteststruc *TestStruc
  62. }
  63. func newTestStruc(depth int, bench bool, useInterface, useStringKeyOnly bool) (ts *TestStruc) {
  64. var i64a, i64b, i64c, i64d int64 = 64, 6464, 646464, 64646464
  65. ts = &TestStruc{
  66. S: "some string",
  67. I64: math.MaxInt64 * 2 / 3, // 64,
  68. I16: 1616,
  69. Ui64: uint64(int64(math.MaxInt64 * 2 / 3)), // 64, //don't use MaxUint64, as bson can't write it
  70. Ui8: 160,
  71. B: true,
  72. By: 5,
  73. Sslice: []string{"one", "two", "three"},
  74. I64slice: []int64{1111, 2222, 3333},
  75. I16slice: []int16{44, 55, 66},
  76. Ui64slice: []uint64{12121212, 34343434, 56565656},
  77. Ui8slice: []uint8{210, 211, 212},
  78. Bslice: []bool{true, false, true, false},
  79. Byslice: []byte{13, 14, 15},
  80. Msi64: map[string]int64{
  81. "one": 1,
  82. "two": 2,
  83. },
  84. AnonInTestStruc: AnonInTestStruc{
  85. // There's more leeway in altering this.
  86. AS: "A-String",
  87. AI64: -64646464,
  88. AI16: 1616,
  89. AUi64: 64646464,
  90. // (U+1D11E)G-clef character may be represented in json as "\uD834\uDD1E".
  91. // single reverse solidus character may be represented in json as "\u005C".
  92. // include these in ASslice below.
  93. ASslice: []string{"Aone", "Atwo", "Athree",
  94. "Afour.reverse_solidus.\u005c", "Afive.Gclef.\U0001d11E"},
  95. AI64slice: []int64{1, -22, 333, -4444, 55555, -666666},
  96. AMSU16: map[string]uint16{"1": 1, "22": 2, "333": 3, "4444": 4},
  97. AF64slice: []float64{11.11e-11, 22.22E+22, 33.33E-33, 44.44e+44, 555.55E-6, 666.66E6},
  98. },
  99. }
  100. if useInterface {
  101. ts.AnonInTestStrucIntf = &AnonInTestStrucIntf{
  102. Islice: []interface{}{"true", true, "no", false, uint64(288), float64(0.4)},
  103. Ms: map[string]interface{}{
  104. "true": "true",
  105. "int64(9)": false,
  106. },
  107. T: testStrucTime,
  108. }
  109. }
  110. //For benchmarks, some things will not work.
  111. if !bench {
  112. //json and bson require string keys in maps
  113. //ts.M = map[interface{}]interface{}{
  114. // true: "true",
  115. // int8(9): false,
  116. //}
  117. //gob cannot encode nil in element in array (encodeArray: nil element)
  118. ts.Iptrslice = []*int64{nil, &i64a, nil, &i64b, nil, &i64c, nil, &i64d, nil}
  119. // ts.Iptrslice = nil
  120. }
  121. if !useStringKeyOnly {
  122. // ts.AnonInTestStruc.AMU32F64 = map[uint32]float64{1: 1, 2: 2, 3: 3} // Json/Bson barf
  123. }
  124. if depth > 0 {
  125. depth--
  126. if ts.Mtsptr == nil {
  127. ts.Mtsptr = make(map[string]*TestStruc)
  128. }
  129. if ts.Mts == nil {
  130. ts.Mts = make(map[string]TestStruc)
  131. }
  132. ts.Mtsptr["0"] = newTestStruc(depth, bench, useInterface, useStringKeyOnly)
  133. ts.Mts["0"] = *(ts.Mtsptr["0"])
  134. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  135. }
  136. return
  137. }
  138. // Some other types
  139. type Sstring string
  140. type Bbool bool
  141. type Sstructsmall struct {
  142. A int
  143. }
  144. type Sstructbig struct {
  145. A int
  146. B bool
  147. c string
  148. // Sval Sstruct
  149. Ssmallptr *Sstructsmall
  150. Ssmall *Sstructsmall
  151. Sptr *Sstructbig
  152. }
  153. type SstructbigMapBySlice struct {
  154. _struct struct{} `codec:",toarray"`
  155. A int
  156. B bool
  157. c string
  158. // Sval Sstruct
  159. Ssmallptr *Sstructsmall
  160. Ssmall *Sstructsmall
  161. Sptr *Sstructbig
  162. }
  163. type Sinterface interface {
  164. Noop()
  165. }