You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.8 KiB

  1. package p2p
  2. import "testing"
  3. /* this func decode boost variant
  4. *
  5. * 0 0a 60 == (60 >> 2 ) = 24 bytes
  6. * 1 0a c0 == (c0 >> 2 ) = 48 bytes
  7. * 2 0a 21 01 == (121 >> 2 ) = 72
  8. * 3 0a 81 01 == (181 >> 2 ) = 96 bytes
  9. * 4 0a e1 01
  10. * 5 0a 41 02
  11. * 7 0a 01 03
  12. * 8 0a 61 03
  13. * 9 0a c1 03
  14. *
  15. *
  16. *
  17. * */
  18. func Test_Boost_Varint_Serdes(t *testing.T) {
  19. buf := make([]byte, 8, 8)
  20. bytes_required := Encode_Boost_Varint(buf, 24)
  21. if bytes_required != 1 || buf[0] != 0x60 {
  22. t.Errorf("Single bytes boost varint encode test failed %d", buf[0])
  23. }
  24. // decode and test
  25. value, bytes_required := Decode_Boost_Varint(buf)
  26. if value != 24 || bytes_required != 1 {
  27. t.Errorf("Single bytes boost varint decode test failed value %d bytes %d", value, bytes_required)
  28. }
  29. // 2 bytes test
  30. bytes_required = Encode_Boost_Varint(buf, 72)
  31. if bytes_required != 2 || buf[0] != 0x21 || buf[1] != 1 {
  32. t.Errorf("2 bytes boost varint encode test failed")
  33. }
  34. // decode and test
  35. value, bytes_required = Decode_Boost_Varint(buf)
  36. if value != 72 || bytes_required != 2 {
  37. t.Errorf("2 bytes boost varint decode test failed")
  38. }
  39. bytes_required = Encode_Boost_Varint(buf, 6000)
  40. if bytes_required != 2 || buf[0] != 0xc1 || buf[1] != 0x5d {
  41. t.Errorf("2 bytes boost varint encode test failed")
  42. }
  43. // decode and test
  44. value, bytes_required = Decode_Boost_Varint(buf)
  45. if value != 6000 || bytes_required != 2 {
  46. t.Errorf("2 bytes boost varint decode test failed")
  47. }
  48. // 3 bytes test
  49. bytes_required = Encode_Boost_Varint(buf, 40096)
  50. if bytes_required != 4 || buf[0] != 0x82 || buf[1] == 0x72 && buf[2] != 0x2 && buf[3] == 0 {
  51. t.Errorf("3 bytes boost varint encode test failed")
  52. }
  53. // decode and test
  54. value, bytes_required = Decode_Boost_Varint(buf)
  55. if value != 40096 || bytes_required != 4 {
  56. t.Errorf("3 bytes boost varint decode test failed")
  57. }
  58. }