marshal_test.go 15 KB

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