marshal_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package gocql
  2. import (
  3. "bytes"
  4. "math"
  5. "reflect"
  6. "speter.net/go/exp/math/dec/inf"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. var quarter, _ = inf.NewDec(0, 0).SetString("0.25")
  12. var marshalTests = []struct {
  13. Info *TypeInfo
  14. Data []byte
  15. Value interface{}
  16. }{
  17. {
  18. &TypeInfo{Type: TypeVarchar},
  19. []byte("hello world"),
  20. []byte("hello world"),
  21. },
  22. {
  23. &TypeInfo{Type: TypeVarchar},
  24. []byte("hello world"),
  25. "hello world",
  26. },
  27. {
  28. &TypeInfo{Type: TypeVarchar},
  29. []byte(nil),
  30. []byte(nil),
  31. },
  32. {
  33. &TypeInfo{Type: TypeVarchar},
  34. []byte("hello world"),
  35. MyString("hello world"),
  36. },
  37. {
  38. &TypeInfo{Type: TypeVarchar},
  39. []byte("HELLO WORLD"),
  40. CustomString("hello world"),
  41. },
  42. {
  43. &TypeInfo{Type: TypeBlob},
  44. []byte("hello\x00"),
  45. []byte("hello\x00"),
  46. },
  47. {
  48. &TypeInfo{Type: TypeBlob},
  49. []byte(nil),
  50. []byte(nil),
  51. },
  52. {
  53. &TypeInfo{Type: TypeTimeUUID},
  54. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  55. func() UUID {
  56. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  57. return x
  58. }(),
  59. },
  60. {
  61. &TypeInfo{Type: TypeInt},
  62. []byte("\x00\x00\x00\x00"),
  63. 0,
  64. },
  65. {
  66. &TypeInfo{Type: TypeInt},
  67. []byte("\x01\x02\x03\x04"),
  68. 16909060,
  69. },
  70. {
  71. &TypeInfo{Type: TypeInt},
  72. []byte("\x80\x00\x00\x00"),
  73. int32(math.MinInt32),
  74. },
  75. {
  76. &TypeInfo{Type: TypeInt},
  77. []byte("\x7f\xff\xff\xff"),
  78. int32(math.MaxInt32),
  79. },
  80. {
  81. &TypeInfo{Type: TypeBigInt},
  82. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  83. 0,
  84. },
  85. {
  86. &TypeInfo{Type: TypeBigInt},
  87. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  88. 72623859790382856,
  89. },
  90. {
  91. &TypeInfo{Type: TypeBigInt},
  92. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  93. int64(math.MinInt64),
  94. },
  95. {
  96. &TypeInfo{Type: TypeBigInt},
  97. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  98. int64(math.MaxInt64),
  99. },
  100. {
  101. &TypeInfo{Type: TypeBoolean},
  102. []byte("\x00"),
  103. false,
  104. },
  105. {
  106. &TypeInfo{Type: TypeBoolean},
  107. []byte("\x01"),
  108. true,
  109. },
  110. {
  111. &TypeInfo{Type: TypeFloat},
  112. []byte("\x40\x49\x0f\xdb"),
  113. float32(3.14159265),
  114. },
  115. {
  116. &TypeInfo{Type: TypeDouble},
  117. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  118. float64(3.14159265),
  119. },
  120. {
  121. &TypeInfo{Type: TypeDecimal},
  122. []byte("\x00\x01\x86\xa0\xcb\xd7\x12\xbb\x6d"),
  123. inf.NewDec(875486690157, 100000),
  124. },
  125. {
  126. &TypeInfo{Type: TypeDecimal},
  127. []byte("\x00\x00\x00\x00\x00"),
  128. inf.NewDec(0, 0),
  129. },
  130. {
  131. &TypeInfo{Type: TypeDecimal},
  132. []byte("\x00\x00\x00\x00\x64"),
  133. inf.NewDec(100, 0),
  134. },
  135. {
  136. &TypeInfo{Type: TypeDecimal},
  137. []byte("\x00\x00\x00\x02\x19"),
  138. quarter,
  139. },
  140. {
  141. &TypeInfo{Type: TypeTimestamp},
  142. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  143. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  144. },
  145. {
  146. &TypeInfo{Type: TypeTimestamp},
  147. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  148. int64(1376387523000),
  149. },
  150. {
  151. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  152. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  153. []int{1, 2},
  154. },
  155. {
  156. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  157. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  158. [2]int{1, 2},
  159. },
  160. {
  161. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  162. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  163. []int{1, 2},
  164. },
  165. {
  166. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  167. []byte(nil),
  168. []int(nil),
  169. },
  170. {
  171. &TypeInfo{Type: TypeMap,
  172. Key: &TypeInfo{Type: TypeVarchar},
  173. Elem: &TypeInfo{Type: TypeInt},
  174. },
  175. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  176. map[string]int{"foo": 1},
  177. },
  178. {
  179. &TypeInfo{Type: TypeMap,
  180. Key: &TypeInfo{Type: TypeVarchar},
  181. Elem: &TypeInfo{Type: TypeInt},
  182. },
  183. []byte(nil),
  184. map[string]int(nil),
  185. },
  186. {
  187. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeVarchar}},
  188. bytes.Join([][]byte{
  189. []byte("\x00\x01\xFF\xFF"),
  190. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  191. []string{strings.Repeat("X", 65535)},
  192. },
  193. {
  194. &TypeInfo{Type: TypeMap,
  195. Key: &TypeInfo{Type: TypeVarchar},
  196. Elem: &TypeInfo{Type: TypeVarchar},
  197. },
  198. bytes.Join([][]byte{
  199. []byte("\x00\x01\xFF\xFF"),
  200. bytes.Repeat([]byte("X"), 65535),
  201. []byte("\xFF\xFF"),
  202. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  203. map[string]string{
  204. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  205. },
  206. },
  207. }
  208. func TestMarshal(t *testing.T) {
  209. for i, test := range marshalTests {
  210. data, err := Marshal(test.Info, test.Value)
  211. if err != nil {
  212. t.Errorf("marshalTest[%d]: %v", i, err)
  213. continue
  214. }
  215. if !bytes.Equal(data, test.Data) {
  216. t.Errorf("marshalTest[%d]: expected %q, got %q.", i, test.Data, data)
  217. }
  218. }
  219. }
  220. func TestUnmarshal(t *testing.T) {
  221. for i, test := range marshalTests {
  222. v := reflect.New(reflect.TypeOf(test.Value))
  223. err := Unmarshal(test.Info, test.Data, v.Interface())
  224. if err != nil {
  225. t.Errorf("marshalTest[%d]: %v", i, err)
  226. continue
  227. }
  228. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  229. t.Errorf("marshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  230. }
  231. }
  232. }
  233. type CustomString string
  234. func (c CustomString) MarshalCQL(info *TypeInfo) ([]byte, error) {
  235. return []byte(strings.ToUpper(string(c))), nil
  236. }
  237. func (c *CustomString) UnmarshalCQL(info *TypeInfo, data []byte) error {
  238. *c = CustomString(strings.ToLower(string(data)))
  239. return nil
  240. }
  241. type MyString string
  242. type MyInt int