RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
reader.cc
1
2#include <cppunit/extensions/HelperMacros.h>
3#include <cppunit/TestFixture.h>
4#include <string>
5#include <sstream>
6
7#include "../../src/linker/reader.h"
8
9using namespace Linker;
10
11namespace UnitTests
12{
13
14class TestReader : public CppUnit::TestFixture
15{
16 CPPUNIT_TEST_SUITE(TestReader);
17 CPPUNIT_TEST(testReadData);
18 CPPUNIT_TEST(testReadBinaryData);
19 CPPUNIT_TEST(testReadInteger);
20 CPPUNIT_TEST(testSeekSkip);
21 CPPUNIT_TEST_SUITE_END();
22private:
23 void testReadData();
24 void testReadBinaryData();
25 void testReadInteger();
26 void testSeekSkip();
27
28 std::string readData(std::string payload);
29 uint64_t readInteger(std::string payload, ::EndianType endiantype, bool isSigned = false);
30 uint64_t parseInteger(std::string buffer, ::EndianType endiantype, bool isSigned = false);
31public:
32 void setUp();
33 void tearDown();
34};
35
36std::string TestReader::readData(std::string payload)
37{
38 char buffer[payload.size()];
39 std::istringstream * iss = new std::istringstream(payload);
40
41 Reader * reader = new Reader(::LittleEndian, iss);
42 reader->ReadData(sizeof buffer, buffer);
43 std::string result = std::string(buffer, sizeof buffer);
44 CPPUNIT_ASSERT_EQUAL(payload, result);
45 delete reader;
46 delete iss;
47 return result;
48}
49
50void TestReader::testReadData()
51{
52 char payload[] = "Test data loaded successfully";
53 std::string payload_string(payload, sizeof payload - 1);
54 readData(payload_string);
55}
56
57void TestReader::testReadBinaryData()
58{
59 char payload[256];
60 for(int i = 0; i < 256; i++)
61 {
62 payload[i] = 0xD5 ^ i;
63 }
64 std::string payload_string(payload, sizeof payload - 1);
65 readData(payload_string);
66}
67
68uint64_t TestReader::readInteger(std::string payload, ::EndianType endiantype, bool isSigned)
69{
70 std::istringstream * iss = new std::istringstream(payload);
71 Reader * reader = new Reader(endiantype, iss);
72
73 uint64_t result =
74 isSigned
75 ? reader->ReadSigned(payload.size(), endiantype)
76 : reader->ReadUnsigned(payload.size(), endiantype);
77 delete reader;
78 delete iss;
79
80 return result;
81}
82
83uint64_t TestReader::parseInteger(std::string buffer, ::EndianType endiantype, bool isSigned)
84{
85 return readInteger(buffer, endiantype, isSigned);
86}
87
88void TestReader::testReadInteger()
89{
90 CPPUNIT_ASSERT_EQUAL((uint64_t)0x12, parseInteger(std::string("\x12", 1), ::LittleEndian));
91 CPPUNIT_ASSERT_EQUAL((uint64_t)0x89, parseInteger(std::string("\x89", 1), ::LittleEndian));
92 CPPUNIT_ASSERT_EQUAL((uint64_t)0x3412, parseInteger(std::string("\x12\x34", 2), ::LittleEndian));
93 CPPUNIT_ASSERT_EQUAL((uint64_t)0x8912, parseInteger(std::string("\x12\x89", 2), ::LittleEndian));
94 CPPUNIT_ASSERT_EQUAL((uint64_t)0x563412, parseInteger(std::string("\x12\x34\x56", 3), ::LittleEndian));
95 CPPUNIT_ASSERT_EQUAL((uint64_t)0x893412, parseInteger(std::string("\x12\x34\x89", 3), ::LittleEndian));
96 CPPUNIT_ASSERT_EQUAL((uint64_t)0x78563412, parseInteger(std::string("\x12\x34\x56\x78", 4), ::LittleEndian));
97 CPPUNIT_ASSERT_EQUAL((uint64_t)0x89563412, parseInteger(std::string("\x12\x34\x56\x89", 4), ::LittleEndian));
98 CPPUNIT_ASSERT_EQUAL((uint64_t)0xF1DEBC9A78563412, parseInteger(std::string("\x12\x34\x56\x78\x9A\xBC\xDE\xF1", 8), ::LittleEndian));
99
100 CPPUNIT_ASSERT_EQUAL((uint64_t)0x12, parseInteger(std::string("\x12", 1), ::BigEndian));
101 CPPUNIT_ASSERT_EQUAL((uint64_t)0x89, parseInteger(std::string("\x89", 1), ::BigEndian));
102 CPPUNIT_ASSERT_EQUAL((uint64_t)0x1234, parseInteger(std::string("\x12\x34", 2), ::BigEndian));
103 CPPUNIT_ASSERT_EQUAL((uint64_t)0x8934, parseInteger(std::string("\x89\x34", 2), ::BigEndian));
104 CPPUNIT_ASSERT_EQUAL((uint64_t)0x123456, parseInteger(std::string("\x12\x34\x56", 3), ::BigEndian));
105 CPPUNIT_ASSERT_EQUAL((uint64_t)0x893456, parseInteger(std::string("\x89\x34\x56", 3), ::BigEndian));
106 CPPUNIT_ASSERT_EQUAL((uint64_t)0x12345678, parseInteger(std::string("\x12\x34\x56\x78", 4), ::BigEndian));
107 CPPUNIT_ASSERT_EQUAL((uint64_t)0x89345678, parseInteger(std::string("\x89\x34\x56\x78", 4), ::BigEndian));
108 CPPUNIT_ASSERT_EQUAL((uint64_t)0x123456789ABCDEF1, parseInteger(std::string("\x12\x34\x56\x78\x9A\xBC\xDE\xF1", 8), ::BigEndian));
109
110 CPPUNIT_ASSERT_EQUAL((uint64_t)0x12, parseInteger(std::string("\x12", 1), ::PDP11Endian));
111 CPPUNIT_ASSERT_EQUAL((uint64_t)0x89, parseInteger(std::string("\x89", 1), ::PDP11Endian));
112 CPPUNIT_ASSERT_EQUAL((uint64_t)0x3412, parseInteger(std::string("\x12\x34", 2), ::PDP11Endian));
113 CPPUNIT_ASSERT_EQUAL((uint64_t)0x8912, parseInteger(std::string("\x12\x89", 2), ::PDP11Endian));
114 CPPUNIT_ASSERT_EQUAL((uint64_t)0x34127856, parseInteger(std::string("\x12\x34\x56\x78", 4), ::PDP11Endian));
115 CPPUNIT_ASSERT_EQUAL((uint64_t)0x89127856, parseInteger(std::string("\x12\x89\x56\x78", 4), ::PDP11Endian));
116
117 CPPUNIT_ASSERT_EQUAL((uint64_t)0x12, parseInteger(std::string("\x12", 1), ::LittleEndian, true));
118 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x100 | 0x89, parseInteger(std::string("\x89", 1), ::LittleEndian, true));
119 CPPUNIT_ASSERT_EQUAL((uint64_t)0x3412, parseInteger(std::string("\x12\x34", 2), ::LittleEndian, true));
120 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x10000 | 0x8912, parseInteger(std::string("\x12\x89", 2), ::LittleEndian, true));
121 CPPUNIT_ASSERT_EQUAL((uint64_t)0x563412, parseInteger(std::string("\x12\x34\x56", 3), ::LittleEndian, true));
122 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x1000000 | 0x893412, parseInteger(std::string("\x12\x34\x89", 3), ::LittleEndian, true));
123 CPPUNIT_ASSERT_EQUAL((uint64_t)0x78563412, parseInteger(std::string("\x12\x34\x56\x78", 4), ::LittleEndian, true));
124 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x100000000 | 0x89563412, parseInteger(std::string("\x12\x34\x56\x89", 4), ::LittleEndian, true));
125 CPPUNIT_ASSERT_EQUAL((uint64_t)0xF1DEBC9A78563412, parseInteger(std::string("\x12\x34\x56\x78\x9A\xBC\xDE\xF1", 8), ::LittleEndian, true));
126
127 CPPUNIT_ASSERT_EQUAL((uint64_t)0x12, parseInteger(std::string("\x12", 1), ::BigEndian, true));
128 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x100 | 0x89, parseInteger(std::string("\x89", 1), ::BigEndian, true));
129 CPPUNIT_ASSERT_EQUAL((uint64_t)0x1234, parseInteger(std::string("\x12\x34", 2), ::BigEndian, true));
130 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x10000 | 0x8934, parseInteger(std::string("\x89\x34", 2), ::BigEndian, true));
131 CPPUNIT_ASSERT_EQUAL((uint64_t)0x123456, parseInteger(std::string("\x12\x34\x56", 3), ::BigEndian, true));
132 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x1000000 |0x893456, parseInteger(std::string("\x89\x34\x56", 3), ::BigEndian, true));
133 CPPUNIT_ASSERT_EQUAL((uint64_t)0x12345678, parseInteger(std::string("\x12\x34\x56\x78", 4), ::BigEndian, true));
134 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x100000000 | 0x89345678, parseInteger(std::string("\x89\x34\x56\x78", 4), ::BigEndian, true));
135 CPPUNIT_ASSERT_EQUAL((uint64_t)0x123456789ABCDEF1, parseInteger(std::string("\x12\x34\x56\x78\x9A\xBC\xDE\xF1", 8), ::BigEndian, true));
136
137 CPPUNIT_ASSERT_EQUAL((uint64_t)0x12, parseInteger(std::string("\x12", 1), ::PDP11Endian, true));
138 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x100 | 0x89, parseInteger(std::string("\x89", 1), ::PDP11Endian, true));
139 CPPUNIT_ASSERT_EQUAL((uint64_t)0x3412, parseInteger(std::string("\x12\x34", 2), ::PDP11Endian, true));
140 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x10000 | 0x8912, parseInteger(std::string("\x12\x89", 2), ::PDP11Endian, true));
141 CPPUNIT_ASSERT_EQUAL((uint64_t)0x34127856, parseInteger(std::string("\x12\x34\x56\x78", 4), ::PDP11Endian, true));
142 CPPUNIT_ASSERT_EQUAL((uint64_t)-0x100000000 | 0x89127856, parseInteger(std::string("\x12\x89\x56\x78", 4), ::PDP11Endian, true));
143}
144
145void TestReader::testSeekSkip()
146{
147 char payload[256];
148 char buffer[1];
149 for(int i = 0; i < 256; i++)
150 {
151 payload[i] = i;
152 }
153
154 std::string payload_string(payload, sizeof payload);
155 std::istringstream * iss = new std::istringstream(payload_string);
156 Reader * reader = new Reader(::LittleEndian, iss);
157
158 reader->Seek(123);
159 reader->ReadData(1, buffer);
160 CPPUNIT_ASSERT_EQUAL(123, (int)(unsigned char)buffer[0]);
161
162 reader->Skip(45);
163 reader->ReadData(1, buffer);
164 CPPUNIT_ASSERT_EQUAL(123 + 1 + 45, (int)(unsigned char)buffer[0]);
165
166 delete reader;
167 delete iss;
168}
169
170void TestReader::setUp()
171{
172}
173
174void TestReader::tearDown()
175{
176}
177
178}
179
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
void Skip(offset_t offset)
Jump to a distance in the input stream.
Definition reader.cc:81
void Seek(offset_t offset)
Jump to a specific location in the input stream.
Definition reader.cc:75
uint64_t ReadSigned(size_t bytes, EndianType endiantype)
Read a signed word.
Definition reader.cc:63
void ReadData(size_t count, void *data)
Read in a sequence of bytes.
Definition reader.cc:6
uint64_t ReadUnsigned(size_t bytes, EndianType endiantype)
Read an unsigned word.
Definition reader.cc:51
Definition reader.cc:15