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