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.

89 lines
1.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package modhex
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. type encDecTest struct {
  7. enc string
  8. dec []byte
  9. }
  10. var encDecTests = []encDecTest{
  11. {"", []byte{}},
  12. {"dteffuje", []byte{0x2d, 0x34, 0x4e, 0x83}},
  13. {
  14. "hknhfjbrjnlnldnhcujvddbikngjrtgh",
  15. []byte{0x69, 0xb6, 0x48, 0x1c, 0x8b, 0xab,
  16. 0xa2, 0xb6, 0x0e, 0x8f, 0x22, 0x17,
  17. 0x9b, 0x58, 0xcd, 0x56},
  18. },
  19. {
  20. "urtubjtnuihvntcreeeecvbregfjibtn",
  21. []byte{0xec, 0xde, 0x18, 0xdb, 0xe7, 0x6f,
  22. 0xbd, 0x0c, 0x33, 0x33, 0x0f, 0x1c,
  23. 0x35, 0x48, 0x71, 0xdb},
  24. },
  25. }
  26. func TestEncode(t *testing.T) {
  27. for i, test := range encDecTests {
  28. dst := make([]byte, EncodedLen(len(test.dec)))
  29. n := Encode(dst, test.dec)
  30. if n != len(dst) {
  31. t.Errorf("#%d: bad return value: got %d want: %d", i, n, len(dst))
  32. }
  33. if string(dst) != test.enc {
  34. t.Errorf("#%d: got: %#v want %#v", i, dst, []byte(test.enc))
  35. }
  36. }
  37. }
  38. func TestDecode(t *testing.T) {
  39. for i, test := range encDecTests {
  40. dst := make([]byte, DecodedLen(len(test.enc)))
  41. n, err := Decode(dst, []byte(test.enc))
  42. if err != nil {
  43. t.Errorf("#%d: unexpected err value: %s", i, err)
  44. continue
  45. }
  46. if n != len(dst) {
  47. t.Errorf("#%d: bad return value got: %d want:%d", i, n, len(dst))
  48. }
  49. if !bytes.Equal(dst, test.dec) {
  50. t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec)
  51. }
  52. }
  53. }
  54. func TestEncodeToString(t *testing.T) {
  55. for i, test := range encDecTests {
  56. s := EncodeToString(test.dec)
  57. if s != test.enc {
  58. t.Errorf("#%d: got: %s want: %s", i, s, test.enc)
  59. }
  60. }
  61. }
  62. func TestDecodeString(t *testing.T) {
  63. for i, test := range encDecTests {
  64. dst, err := DecodeString(test.enc)
  65. if err != nil {
  66. t.Errorf("#%d: unexpected err value: %s", i, err)
  67. continue
  68. }
  69. if !bytes.Equal(dst, test.dec) {
  70. t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec)
  71. }
  72. }
  73. }