marshal_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. // +build all unit
  2. package gocql
  3. import (
  4. "bytes"
  5. "math"
  6. "math/big"
  7. "net"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "time"
  12. "speter.net/go/exp/math/dec/inf"
  13. )
  14. var marshalTests = []struct {
  15. Info TypeInfo
  16. Data []byte
  17. Value interface{}
  18. }{
  19. {
  20. NativeType{proto: 2, typ: TypeVarchar},
  21. []byte("hello world"),
  22. []byte("hello world"),
  23. },
  24. {
  25. NativeType{proto: 2, typ: TypeVarchar},
  26. []byte("hello world"),
  27. "hello world",
  28. },
  29. {
  30. NativeType{proto: 2, typ: TypeVarchar},
  31. []byte(nil),
  32. []byte(nil),
  33. },
  34. {
  35. NativeType{proto: 2, typ: TypeVarchar},
  36. []byte("hello world"),
  37. MyString("hello world"),
  38. },
  39. {
  40. NativeType{proto: 2, typ: TypeVarchar},
  41. []byte("HELLO WORLD"),
  42. CustomString("hello world"),
  43. },
  44. {
  45. NativeType{proto: 2, typ: TypeBlob},
  46. []byte("hello\x00"),
  47. []byte("hello\x00"),
  48. },
  49. {
  50. NativeType{proto: 2, typ: TypeBlob},
  51. []byte(nil),
  52. []byte(nil),
  53. },
  54. {
  55. NativeType{proto: 2, typ: TypeTimeUUID},
  56. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  57. func() UUID {
  58. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  59. return x
  60. }(),
  61. },
  62. {
  63. NativeType{proto: 2, typ: TypeInt},
  64. []byte("\x00\x00\x00\x00"),
  65. 0,
  66. },
  67. {
  68. NativeType{proto: 2, typ: TypeInt},
  69. []byte("\x01\x02\x03\x04"),
  70. int(16909060),
  71. },
  72. {
  73. NativeType{proto: 2, typ: TypeInt},
  74. []byte("\x80\x00\x00\x00"),
  75. int32(math.MinInt32),
  76. },
  77. {
  78. NativeType{proto: 2, typ: TypeInt},
  79. []byte("\x7f\xff\xff\xff"),
  80. int32(math.MaxInt32),
  81. },
  82. {
  83. NativeType{proto: 2, typ: TypeInt},
  84. []byte("\x00\x00\x00\x00"),
  85. "0",
  86. },
  87. {
  88. NativeType{proto: 2, typ: TypeInt},
  89. []byte("\x01\x02\x03\x04"),
  90. "16909060",
  91. },
  92. {
  93. NativeType{proto: 2, typ: TypeInt},
  94. []byte("\x80\x00\x00\x00"),
  95. "-2147483648", // math.MinInt32
  96. },
  97. {
  98. NativeType{proto: 2, typ: TypeInt},
  99. []byte("\x7f\xff\xff\xff"),
  100. "2147483647", // math.MaxInt32
  101. },
  102. {
  103. NativeType{proto: 2, typ: TypeBigInt},
  104. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  105. 0,
  106. },
  107. {
  108. NativeType{proto: 2, typ: TypeBigInt},
  109. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  110. 72623859790382856,
  111. },
  112. {
  113. NativeType{proto: 2, typ: TypeBigInt},
  114. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  115. int64(math.MinInt64),
  116. },
  117. {
  118. NativeType{proto: 2, typ: TypeBigInt},
  119. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  120. int64(math.MaxInt64),
  121. },
  122. {
  123. NativeType{proto: 2, typ: TypeBigInt},
  124. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  125. "0",
  126. },
  127. {
  128. NativeType{proto: 2, typ: TypeBigInt},
  129. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  130. "72623859790382856",
  131. },
  132. {
  133. NativeType{proto: 2, typ: TypeBigInt},
  134. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  135. "-9223372036854775808", // math.MinInt64
  136. },
  137. {
  138. NativeType{proto: 2, typ: TypeBigInt},
  139. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  140. "9223372036854775807", // math.MaxInt64
  141. },
  142. {
  143. NativeType{proto: 2, typ: TypeBoolean},
  144. []byte("\x00"),
  145. false,
  146. },
  147. {
  148. NativeType{proto: 2, typ: TypeBoolean},
  149. []byte("\x01"),
  150. true,
  151. },
  152. {
  153. NativeType{proto: 2, typ: TypeFloat},
  154. []byte("\x40\x49\x0f\xdb"),
  155. float32(3.14159265),
  156. },
  157. {
  158. NativeType{proto: 2, typ: TypeDouble},
  159. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  160. float64(3.14159265),
  161. },
  162. {
  163. NativeType{proto: 2, typ: TypeDecimal},
  164. []byte("\x00\x00\x00\x00\x00"),
  165. inf.NewDec(0, 0),
  166. },
  167. {
  168. NativeType{proto: 2, typ: TypeDecimal},
  169. []byte("\x00\x00\x00\x00\x64"),
  170. inf.NewDec(100, 0),
  171. },
  172. {
  173. NativeType{proto: 2, typ: TypeDecimal},
  174. []byte("\x00\x00\x00\x02\x19"),
  175. decimalize("0.25"),
  176. },
  177. {
  178. NativeType{proto: 2, typ: TypeDecimal},
  179. []byte("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91"),
  180. decimalize("-0.0012095473475870063"), // From the iconara/cql-rb test suite
  181. },
  182. {
  183. NativeType{proto: 2, typ: TypeDecimal},
  184. []byte("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o"),
  185. decimalize("0.0012095473475870063"), // From the iconara/cql-rb test suite
  186. },
  187. {
  188. NativeType{proto: 2, typ: TypeDecimal},
  189. []byte("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V"),
  190. decimalize("-1042342234234.123423435647768234"), // From the iconara/cql-rb test suite
  191. },
  192. {
  193. NativeType{proto: 2, typ: TypeDecimal},
  194. []byte("\x00\x00\x00\r\nJ\x04\"^\x91\x04\x8a\xb1\x18\xfe"),
  195. decimalize("1243878957943.1234124191998"), // From the datastax/python-driver test suite
  196. },
  197. {
  198. NativeType{proto: 2, typ: TypeDecimal},
  199. []byte("\x00\x00\x00\x06\xe5\xde]\x98Y"),
  200. decimalize("-112233.441191"), // From the datastax/python-driver test suite
  201. },
  202. {
  203. NativeType{proto: 2, typ: TypeDecimal},
  204. []byte("\x00\x00\x00\x14\x00\xfa\xce"),
  205. decimalize("0.00000000000000064206"), // From the datastax/python-driver test suite
  206. },
  207. {
  208. NativeType{proto: 2, typ: TypeDecimal},
  209. []byte("\x00\x00\x00\x14\xff\x052"),
  210. decimalize("-0.00000000000000064206"), // From the datastax/python-driver test suite
  211. },
  212. {
  213. NativeType{proto: 2, typ: TypeDecimal},
  214. []byte("\xff\xff\xff\x9c\x00\xfa\xce"),
  215. inf.NewDec(64206, -100), // From the datastax/python-driver test suite
  216. },
  217. {
  218. NativeType{proto: 2, typ: TypeTimestamp},
  219. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  220. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  221. },
  222. {
  223. NativeType{proto: 2, typ: TypeTimestamp},
  224. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  225. int64(1376387523000),
  226. },
  227. {
  228. CollectionType{
  229. NativeType: NativeType{proto: 2, typ: TypeList},
  230. Elem: NativeType{proto: 2, typ: TypeInt},
  231. },
  232. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  233. []int{1, 2},
  234. },
  235. {
  236. CollectionType{
  237. NativeType: NativeType{proto: 2, typ: TypeList},
  238. Elem: NativeType{proto: 2, typ: TypeInt},
  239. },
  240. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  241. [2]int{1, 2},
  242. },
  243. {
  244. CollectionType{
  245. NativeType: NativeType{proto: 2, typ: TypeSet},
  246. Elem: NativeType{proto: 2, typ: TypeInt},
  247. },
  248. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  249. []int{1, 2},
  250. },
  251. {
  252. CollectionType{
  253. NativeType: NativeType{proto: 2, typ: TypeSet},
  254. Elem: NativeType{proto: 2, typ: TypeInt},
  255. },
  256. []byte(nil),
  257. []int(nil),
  258. },
  259. {
  260. CollectionType{
  261. NativeType: NativeType{proto: 2, typ: TypeMap},
  262. Key: NativeType{proto: 2, typ: TypeVarchar},
  263. Elem: NativeType{proto: 2, typ: TypeInt},
  264. },
  265. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  266. map[string]int{"foo": 1},
  267. },
  268. {
  269. CollectionType{
  270. NativeType: NativeType{proto: 2, typ: TypeMap},
  271. Key: NativeType{proto: 2, typ: TypeVarchar},
  272. Elem: NativeType{proto: 2, typ: TypeInt},
  273. },
  274. []byte(nil),
  275. map[string]int(nil),
  276. },
  277. {
  278. CollectionType{
  279. NativeType: NativeType{proto: 2, typ: TypeList},
  280. Elem: NativeType{proto: 2, typ: TypeVarchar},
  281. },
  282. bytes.Join([][]byte{
  283. []byte("\x00\x01\xFF\xFF"),
  284. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  285. []string{strings.Repeat("X", 65535)},
  286. },
  287. {
  288. CollectionType{
  289. NativeType: NativeType{proto: 2, typ: TypeMap},
  290. Key: NativeType{proto: 2, typ: TypeVarchar},
  291. Elem: NativeType{proto: 2, typ: TypeVarchar},
  292. },
  293. bytes.Join([][]byte{
  294. []byte("\x00\x01\xFF\xFF"),
  295. bytes.Repeat([]byte("X"), 65535),
  296. []byte("\xFF\xFF"),
  297. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  298. map[string]string{
  299. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  300. },
  301. },
  302. {
  303. NativeType{proto: 2, typ: TypeVarint},
  304. []byte("\x00"),
  305. 0,
  306. },
  307. {
  308. NativeType{proto: 2, typ: TypeVarint},
  309. []byte("\x37\xE2\x3C\xEC"),
  310. int32(937573612),
  311. },
  312. {
  313. NativeType{proto: 2, typ: TypeVarint},
  314. []byte("\x37\xE2\x3C\xEC"),
  315. big.NewInt(937573612),
  316. },
  317. {
  318. NativeType{proto: 2, typ: TypeVarint},
  319. []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
  320. bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
  321. },
  322. {
  323. NativeType{proto: 2, typ: TypeVarint},
  324. []byte("\xC9v\x8D:\x86"),
  325. big.NewInt(-234234234234), // From the iconara/cql-rb test suite
  326. },
  327. {
  328. NativeType{proto: 2, typ: TypeVarint},
  329. []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
  330. bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
  331. },
  332. {
  333. NativeType{proto: 2, typ: TypeInet},
  334. []byte("\x7F\x00\x00\x01"),
  335. net.ParseIP("127.0.0.1").To4(),
  336. },
  337. {
  338. NativeType{proto: 2, typ: TypeInet},
  339. []byte("\xFF\xFF\xFF\xFF"),
  340. net.ParseIP("255.255.255.255").To4(),
  341. },
  342. {
  343. NativeType{proto: 2, typ: TypeInet},
  344. []byte("\x7F\x00\x00\x01"),
  345. "127.0.0.1",
  346. },
  347. {
  348. NativeType{proto: 2, typ: TypeInet},
  349. []byte("\xFF\xFF\xFF\xFF"),
  350. "255.255.255.255",
  351. },
  352. {
  353. NativeType{proto: 2, typ: TypeInet},
  354. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  355. "21da:d3:0:2f3b:2aa:ff:fe28:9c5a",
  356. },
  357. {
  358. NativeType{proto: 2, typ: TypeInet},
  359. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  360. "fe80::202:b3ff:fe1e:8329",
  361. },
  362. {
  363. NativeType{proto: 2, typ: TypeInet},
  364. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  365. net.ParseIP("21da:d3:0:2f3b:2aa:ff:fe28:9c5a"),
  366. },
  367. {
  368. NativeType{proto: 2, typ: TypeInet},
  369. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  370. net.ParseIP("fe80::202:b3ff:fe1e:8329"),
  371. },
  372. {
  373. NativeType{proto: 2, typ: TypeInt},
  374. []byte(nil),
  375. nil,
  376. },
  377. {
  378. NativeType{proto: 2, typ: TypeVarchar},
  379. []byte("nullable string"),
  380. func() *string {
  381. value := "nullable string"
  382. return &value
  383. }(),
  384. },
  385. {
  386. NativeType{proto: 2, typ: TypeVarchar},
  387. []byte{},
  388. (*string)(nil),
  389. },
  390. {
  391. NativeType{proto: 2, typ: TypeInt},
  392. []byte("\x7f\xff\xff\xff"),
  393. func() *int {
  394. var value int = math.MaxInt32
  395. return &value
  396. }(),
  397. },
  398. {
  399. NativeType{proto: 2, typ: TypeInt},
  400. []byte(nil),
  401. (*int)(nil),
  402. },
  403. {
  404. NativeType{proto: 2, typ: TypeTimeUUID},
  405. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  406. &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  407. },
  408. {
  409. NativeType{proto: 2, typ: TypeTimeUUID},
  410. []byte{},
  411. (*UUID)(nil),
  412. },
  413. {
  414. NativeType{proto: 2, typ: TypeTimestamp},
  415. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  416. func() *time.Time {
  417. t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
  418. return &t
  419. }(),
  420. },
  421. {
  422. NativeType{proto: 2, typ: TypeTimestamp},
  423. []byte(nil),
  424. (*time.Time)(nil),
  425. },
  426. {
  427. NativeType{proto: 2, typ: TypeBoolean},
  428. []byte("\x00"),
  429. func() *bool {
  430. b := false
  431. return &b
  432. }(),
  433. },
  434. {
  435. NativeType{proto: 2, typ: TypeBoolean},
  436. []byte("\x01"),
  437. func() *bool {
  438. b := true
  439. return &b
  440. }(),
  441. },
  442. {
  443. NativeType{proto: 2, typ: TypeBoolean},
  444. []byte(nil),
  445. (*bool)(nil),
  446. },
  447. {
  448. NativeType{proto: 2, typ: TypeFloat},
  449. []byte("\x40\x49\x0f\xdb"),
  450. func() *float32 {
  451. f := float32(3.14159265)
  452. return &f
  453. }(),
  454. },
  455. {
  456. NativeType{proto: 2, typ: TypeFloat},
  457. []byte(nil),
  458. (*float32)(nil),
  459. },
  460. {
  461. NativeType{proto: 2, typ: TypeDouble},
  462. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  463. func() *float64 {
  464. d := float64(3.14159265)
  465. return &d
  466. }(),
  467. },
  468. {
  469. NativeType{proto: 2, typ: TypeDouble},
  470. []byte(nil),
  471. (*float64)(nil),
  472. },
  473. {
  474. NativeType{proto: 2, typ: TypeInet},
  475. []byte("\x7F\x00\x00\x01"),
  476. func() *net.IP {
  477. ip := net.ParseIP("127.0.0.1").To4()
  478. return &ip
  479. }(),
  480. },
  481. {
  482. NativeType{proto: 2, typ: TypeInet},
  483. []byte(nil),
  484. (*net.IP)(nil),
  485. },
  486. {
  487. CollectionType{
  488. NativeType: NativeType{proto: 2, typ: TypeList},
  489. Elem: NativeType{proto: 2, typ: TypeInt},
  490. },
  491. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  492. func() *[]int {
  493. l := []int{1, 2}
  494. return &l
  495. }(),
  496. },
  497. {
  498. CollectionType{
  499. NativeType: NativeType{proto: 2, typ: TypeList},
  500. Elem: NativeType{proto: 2, typ: TypeInt},
  501. },
  502. []byte(nil),
  503. (*[]int)(nil),
  504. },
  505. {
  506. CollectionType{
  507. NativeType: NativeType{proto: 2, typ: TypeMap},
  508. Key: NativeType{proto: 2, typ: TypeVarchar},
  509. Elem: NativeType{proto: 2, typ: TypeInt},
  510. },
  511. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  512. func() *map[string]int {
  513. m := map[string]int{"foo": 1}
  514. return &m
  515. }(),
  516. },
  517. {
  518. CollectionType{
  519. NativeType: NativeType{proto: 2, typ: TypeMap},
  520. Key: NativeType{proto: 2, typ: TypeVarchar},
  521. Elem: NativeType{proto: 2, typ: TypeInt},
  522. },
  523. []byte(nil),
  524. (*map[string]int)(nil),
  525. },
  526. }
  527. func decimalize(s string) *inf.Dec {
  528. i, _ := new(inf.Dec).SetString(s)
  529. return i
  530. }
  531. func bigintize(s string) *big.Int {
  532. i, _ := new(big.Int).SetString(s, 10)
  533. return i
  534. }
  535. func TestMarshal(t *testing.T) {
  536. for i, test := range marshalTests {
  537. data, err := Marshal(test.Info, test.Value)
  538. if err != nil {
  539. t.Errorf("marshalTest[%d]: %v", i, err)
  540. continue
  541. }
  542. if !bytes.Equal(data, test.Data) {
  543. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  544. }
  545. }
  546. }
  547. func TestUnmarshal(t *testing.T) {
  548. for i, test := range marshalTests {
  549. if test.Value != nil {
  550. v := reflect.New(reflect.TypeOf(test.Value))
  551. err := Unmarshal(test.Info, test.Data, v.Interface())
  552. if err != nil {
  553. t.Errorf("unmarshalTest[%d]: %v", i, err)
  554. continue
  555. }
  556. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  557. t.Errorf("unmarshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  558. }
  559. } else {
  560. if err := Unmarshal(test.Info, test.Data, test.Value); nil == err {
  561. t.Errorf("unmarshalTest[%d]: %#v not return error.", i, test.Value)
  562. }
  563. }
  564. }
  565. }
  566. func TestMarshalVarint(t *testing.T) {
  567. varintTests := []struct {
  568. Value interface{}
  569. Marshaled []byte
  570. Unmarshaled *big.Int
  571. }{
  572. {
  573. Value: int8(0),
  574. Marshaled: []byte("\x00"),
  575. Unmarshaled: big.NewInt(0),
  576. },
  577. {
  578. Value: uint8(255),
  579. Marshaled: []byte("\x00\xFF"),
  580. Unmarshaled: big.NewInt(255),
  581. },
  582. {
  583. Value: int8(-1),
  584. Marshaled: []byte("\xFF"),
  585. Unmarshaled: big.NewInt(-1),
  586. },
  587. {
  588. Value: big.NewInt(math.MaxInt32),
  589. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  590. Unmarshaled: big.NewInt(math.MaxInt32),
  591. },
  592. {
  593. Value: big.NewInt(int64(math.MaxInt32) + 1),
  594. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  595. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  596. },
  597. {
  598. Value: big.NewInt(math.MinInt32),
  599. Marshaled: []byte("\x80\x00\x00\x00"),
  600. Unmarshaled: big.NewInt(math.MinInt32),
  601. },
  602. {
  603. Value: big.NewInt(int64(math.MinInt32) - 1),
  604. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  605. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  606. },
  607. {
  608. Value: math.MinInt64,
  609. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  610. Unmarshaled: big.NewInt(math.MinInt64),
  611. },
  612. {
  613. Value: uint64(math.MaxInt64) + 1,
  614. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  615. Unmarshaled: bigintize("9223372036854775808"),
  616. },
  617. {
  618. Value: bigintize("2361183241434822606848"), // 2**71
  619. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  620. Unmarshaled: bigintize("2361183241434822606848"),
  621. },
  622. {
  623. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  624. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  625. Unmarshaled: bigintize("-9223372036854775809"),
  626. },
  627. }
  628. for i, test := range varintTests {
  629. data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
  630. if err != nil {
  631. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  632. }
  633. if !bytes.Equal(test.Marshaled, data) {
  634. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  635. }
  636. binder := new(big.Int)
  637. err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
  638. if err != nil {
  639. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  640. }
  641. if test.Unmarshaled.Cmp(binder) != 0 {
  642. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  643. }
  644. }
  645. }
  646. func equalStringSlice(leftList, rightList []string) bool {
  647. if len(leftList) != len(rightList) {
  648. return false
  649. }
  650. for index := range leftList {
  651. if rightList[index] != leftList[index] {
  652. return false
  653. }
  654. }
  655. return true
  656. }
  657. func TestMarshalList(t *testing.T) {
  658. typeInfo := CollectionType {
  659. NativeType: NativeType { proto: 2, typ: TypeList },
  660. Elem: NativeType{ proto: 2, typ: TypeVarchar },
  661. }
  662. sourceLists := [][]string {
  663. []string{ "valueA" },
  664. []string{ "valueA", "valueB" },
  665. []string{ "valueB" },
  666. }
  667. listDatas := [][]byte{}
  668. for _, list := range sourceLists {
  669. listData, marshalErr := Marshal(typeInfo, list)
  670. if nil != marshalErr {
  671. t.Errorf("Error marshal %+v of type %+v: %s", list, typeInfo, marshalErr)
  672. }
  673. listDatas = append(listDatas, listData)
  674. }
  675. outputLists := [][]string{}
  676. var outputList []string
  677. for _, listData := range listDatas {
  678. if unmarshalErr := Unmarshal(typeInfo, listData, &outputList); nil != unmarshalErr {
  679. t.Error(unmarshalErr)
  680. }
  681. outputLists = append(outputLists, outputList)
  682. }
  683. for index, sourceList := range sourceLists {
  684. outputList := outputLists[index]
  685. if !equalStringSlice(sourceList, outputList) {
  686. t.Errorf("Lists %+v not equal to lists %+v, but should", sourceList, outputList)
  687. }
  688. }
  689. }
  690. type CustomString string
  691. func (c CustomString) MarshalCQL(info TypeInfo) ([]byte, error) {
  692. return []byte(strings.ToUpper(string(c))), nil
  693. }
  694. func (c *CustomString) UnmarshalCQL(info TypeInfo, data []byte) error {
  695. *c = CustomString(strings.ToLower(string(data)))
  696. return nil
  697. }
  698. type MyString string
  699. type MyInt int
  700. var typeLookupTest = []struct {
  701. TypeName string
  702. ExpectedType Type
  703. }{
  704. {"AsciiType", TypeAscii},
  705. {"LongType", TypeBigInt},
  706. {"BytesType", TypeBlob},
  707. {"BooleanType", TypeBoolean},
  708. {"CounterColumnType", TypeCounter},
  709. {"DecimalType", TypeDecimal},
  710. {"DoubleType", TypeDouble},
  711. {"FloatType", TypeFloat},
  712. {"Int32Type", TypeInt},
  713. {"DateType", TypeTimestamp},
  714. {"TimestampType", TypeTimestamp},
  715. {"UUIDType", TypeUUID},
  716. {"UTF8Type", TypeVarchar},
  717. {"IntegerType", TypeVarint},
  718. {"TimeUUIDType", TypeTimeUUID},
  719. {"InetAddressType", TypeInet},
  720. {"MapType", TypeMap},
  721. {"ListType", TypeList},
  722. {"SetType", TypeSet},
  723. {"unknown", TypeCustom},
  724. }
  725. func testType(t *testing.T, cassType string, expectedType Type) {
  726. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  727. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  728. }
  729. }
  730. func TestLookupCassType(t *testing.T) {
  731. for _, lookupTest := range typeLookupTest {
  732. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  733. }
  734. }