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