encoding_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package trie
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. )
  7. func TestCompactEncode(t *testing.T) {
  8. test1 := []byte{1, 2, 3, 4, 5}
  9. if res := CompactEncode(test1); res != "\x11\x23\x45" {
  10. t.Error(fmt.Sprintf("even compact encode failed. Got: %q", res))
  11. }
  12. test2 := []byte{0, 1, 2, 3, 4, 5}
  13. if res := CompactEncode(test2); res != "\x00\x01\x23\x45" {
  14. t.Error(fmt.Sprintf("odd compact encode failed. Got: %q", res))
  15. }
  16. test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  17. if res := CompactEncode(test3); res != "\x20\x0f\x1c\xb8" {
  18. t.Error(fmt.Sprintf("odd terminated compact encode failed. Got: %q", res))
  19. }
  20. test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16}
  21. if res := CompactEncode(test4); res != "\x3f\x1c\xb8" {
  22. t.Error(fmt.Sprintf("even terminated compact encode failed. Got: %q", res))
  23. }
  24. }
  25. func TestCompactHexDecode(t *testing.T) {
  26. exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
  27. res := CompactHexDecode("verb")
  28. if !bytes.Equal(res, exp) {
  29. t.Error("Error compact hex decode. Expected", exp, "got", res)
  30. }
  31. }
  32. func TestCompactDecode(t *testing.T) {
  33. exp := []byte{1, 2, 3, 4, 5}
  34. res := CompactDecode("\x11\x23\x45")
  35. if !bytes.Equal(res, exp) {
  36. t.Error("odd compact decode. Expected", exp, "got", res)
  37. }
  38. exp = []byte{0, 1, 2, 3, 4, 5}
  39. res = CompactDecode("\x00\x01\x23\x45")
  40. if !bytes.Equal(res, exp) {
  41. t.Error("even compact decode. Expected", exp, "got", res)
  42. }
  43. exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
  44. res = CompactDecode("\x20\x0f\x1c\xb8")
  45. if !bytes.Equal(res, exp) {
  46. t.Error("even terminated compact decode. Expected", exp, "got", res)
  47. }
  48. exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
  49. res = CompactDecode("\x3f\x1c\xb8")
  50. if !bytes.Equal(res, exp) {
  51. t.Error("even terminated compact decode. Expected", exp, "got", res)
  52. }
  53. }