xints_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package i64b
  2. import (
  3. "testing"
  4. )
  5. func TestMarshalAndUnmarshal(t *testing.T) {
  6. var a = Int64Bytes{1, 2, 3}
  7. data := make([]byte, a.Size())
  8. n, err := a.MarshalTo(data)
  9. if n != 24 {
  10. t.Logf("marshal size must be 24")
  11. t.FailNow()
  12. }
  13. if err != nil {
  14. t.Fatalf("err:%v", err)
  15. }
  16. var b Int64Bytes
  17. err = b.Unmarshal(data)
  18. if err != nil {
  19. t.Fatalf("err:%v", err)
  20. }
  21. if b[0] != 1 || b[1] != 2 || b[2] != 3 {
  22. t.Logf("unmarshal failed!b:%v", b)
  23. t.FailNow()
  24. }
  25. }
  26. func TestUncompleteMarshal(t *testing.T) {
  27. var a = Int64Bytes{1, 2, 3}
  28. data := make([]byte, a.Size()-7)
  29. n, err := a.MarshalTo(data)
  30. if n != 16 {
  31. t.Logf("marshal size must be 16")
  32. t.FailNow()
  33. }
  34. if err != nil {
  35. t.Fatalf("err:%v", err)
  36. }
  37. var b Int64Bytes
  38. err = b.Unmarshal(data)
  39. if err != nil {
  40. t.Fatalf("err:%v", err)
  41. }
  42. if b[0] != 1 || b[1] != 2 {
  43. t.Logf("unmarshal failed!b:%v", b)
  44. t.FailNow()
  45. }
  46. }
  47. func TestNilMarshal(t *testing.T) {
  48. var a = Int64Bytes{1, 2, 3}
  49. var data []byte
  50. n, err := a.MarshalTo(data)
  51. if n != 0 {
  52. t.Logf("marshal size must be 0")
  53. t.FailNow()
  54. }
  55. if err != nil {
  56. t.Fatalf("err:%v", err)
  57. }
  58. var b Int64Bytes
  59. err = b.Unmarshal(data)
  60. if err != nil {
  61. t.Fatalf("err:%v", err)
  62. }
  63. if b != nil {
  64. t.Logf("unmarshal failed!b:%v", b)
  65. t.FailNow()
  66. }
  67. }