values_flex_test.go 3.5 KB

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