values_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  3. package codec
  4. // This file contains values used by tests and benchmarks.
  5. import (
  6. "math"
  7. "time"
  8. )
  9. var testStrucTime = time.Date(2012, 2, 2, 2, 2, 2, 2000, time.UTC).UTC()
  10. type AnonInTestStruc struct {
  11. AS string
  12. AI64 int64
  13. AI16 int16
  14. AUi64 uint64
  15. ASslice []string
  16. AI64slice []int64
  17. }
  18. type TestStruc struct {
  19. S string
  20. I64 int64
  21. I16 int16
  22. Ui64 uint64
  23. Ui8 uint8
  24. B bool
  25. By byte
  26. Sslice []string
  27. I64slice []int64
  28. I16slice []int16
  29. Ui64slice []uint64
  30. Ui8slice []uint8
  31. Bslice []bool
  32. Byslice []byte
  33. Islice []interface{}
  34. Iptrslice []*int64
  35. AnonInTestStruc
  36. //M map[interface{}]interface{} `json:"-",bson:"-"`
  37. Ms map[string]interface{}
  38. Msi64 map[string]int64
  39. Nintf interface{} //don't set this, so we can test for nil
  40. T time.Time
  41. Nmap map[string]bool //don't set this, so we can test for nil
  42. Nslice []byte //don't set this, so we can test for nil
  43. Nint64 *int64 //don't set this, so we can test for nil
  44. Mtsptr map[string]*TestStruc
  45. Mts map[string]TestStruc
  46. Its []*TestStruc
  47. Nteststruc *TestStruc
  48. }
  49. func newTestStruc(depth int, bench bool) (ts *TestStruc) {
  50. var i64a, i64b, i64c, i64d int64 = 64, 6464, 646464, 64646464
  51. ts = &TestStruc{
  52. S: "some string",
  53. I64: math.MaxInt64 * 2 / 3, // 64,
  54. I16: 16,
  55. Ui64: uint64(int64(math.MaxInt64 * 2 / 3)), // 64, //don't use MaxUint64, as bson can't write it
  56. Ui8: 160,
  57. B: true,
  58. By: 5,
  59. Sslice: []string{"one", "two", "three"},
  60. I64slice: []int64{1, 2, 3},
  61. I16slice: []int16{4, 5, 6},
  62. Ui64slice: []uint64{137, 138, 139},
  63. Ui8slice: []uint8{210, 211, 212},
  64. Bslice: []bool{true, false, true, false},
  65. Byslice: []byte{13, 14, 15},
  66. Islice: []interface{}{"true", true, "no", false, uint64(288), float64(0.4)},
  67. Ms: map[string]interface{}{
  68. "true": "true",
  69. "int64(9)": false,
  70. },
  71. Msi64: map[string]int64{
  72. "one": 1,
  73. "two": 2,
  74. },
  75. T: testStrucTime,
  76. AnonInTestStruc: AnonInTestStruc{
  77. AS: "A-String",
  78. AI64: 64,
  79. AI16: 16,
  80. AUi64: 64,
  81. ASslice: []string{"Aone", "Atwo", "Athree"},
  82. AI64slice: []int64{1, 2, 3},
  83. },
  84. }
  85. //For benchmarks, some things will not work.
  86. if !bench {
  87. //json and bson require string keys in maps
  88. //ts.M = map[interface{}]interface{}{
  89. // true: "true",
  90. // int8(9): false,
  91. //}
  92. //gob cannot encode nil in element in array (encodeArray: nil element)
  93. ts.Iptrslice = []*int64{nil, &i64a, nil, &i64b, nil, &i64c, nil, &i64d, nil}
  94. // ts.Iptrslice = nil
  95. }
  96. if depth > 0 {
  97. depth--
  98. if ts.Mtsptr == nil {
  99. ts.Mtsptr = make(map[string]*TestStruc)
  100. }
  101. if ts.Mts == nil {
  102. ts.Mts = make(map[string]TestStruc)
  103. }
  104. ts.Mtsptr["0"] = newTestStruc(depth, bench)
  105. ts.Mts["0"] = *(ts.Mtsptr["0"])
  106. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  107. }
  108. return
  109. }