HexDump_test.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "catch.hpp"
  2. #include "HexDump.hh"
  3. #include <cstring>
  4. static void test_decode(const std::string& encoded, const std::string& decoded)
  5. {
  6. auto [buf, bufSize] = HexDump::decode(encoded);
  7. REQUIRE(bufSize == decoded.size());
  8. CHECK(memcmp(buf.data(), decoded.data(), decoded.size()) == 0);
  9. }
  10. static void test(const std::string& decoded, const std::string& encoded)
  11. {
  12. CHECK(HexDump::encode(reinterpret_cast<const uint8_t*>(decoded.data()),
  13. decoded.size())
  14. == encoded);
  15. test_decode(encoded, decoded);
  16. }
  17. TEST_CASE("HexDump")
  18. {
  19. test("", "");
  20. test("a", "61");
  21. test("ab", "61 62");
  22. test("abc", "61 62 63");
  23. test("0123456789", "30 31 32 33 34 35 36 37 38 39");
  24. test("abcdefghijklmnopqrstuvwxyz",
  25. "61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70\n"
  26. "71 72 73 74 75 76 77 78 79 7A");
  27. test("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
  28. "30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46\n"
  29. "47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56\n"
  30. "57 58 59 5A 61 62 63 64 65 66 67 68 69 6A 6B 6C\n"
  31. "6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A");
  32. test("111111111111111111111111111111111111111111111111111111111111111111111111111111"
  33. "111111111111111111111111111111111111111111111111111111111111111111111111111111"
  34. "111111111111111111111111111111111111111111111111111111111111111111111111111111"
  35. "111111111111111111111111111111111111111111111",
  36. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  37. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  38. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  39. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  40. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  41. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  42. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  43. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  44. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  45. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  46. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  47. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  48. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  49. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  50. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  51. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  52. "31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31\n"
  53. "31 31 31 31 31 31 31");
  54. // Decode-only:
  55. // - extra newlines don't matter
  56. test_decode("30 31\n32\n33 34", "01234");
  57. // - no spaces inbetween is fine as well
  58. test_decode("3031323334", "01234");
  59. // - any non [0-9][a-f][A-F] character is ignored
  60. test_decode("30|31G32g33+34", "01234");
  61. // - lower-case [a-f] is allowed
  62. test_decode("4A+4b4c\n4d", "JKLM");
  63. }