2#include <cppunit/extensions/HelperMacros.h>
3#include <cppunit/TestFixture.h>
7#include "../../src/linker/reader.h"
17 CPPUNIT_TEST(testReadData);
18 CPPUNIT_TEST(testReadBinaryData);
19 CPPUNIT_TEST(testReadInteger);
20 CPPUNIT_TEST(testSeekSkip);
21 CPPUNIT_TEST_SUITE_END();
24 void testReadBinaryData();
25 void testReadInteger();
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);
36std::string TestReader::readData(std::string payload)
38 char buffer[payload.size()];
39 std::istringstream * iss =
new std::istringstream(payload);
42 reader->
ReadData(
sizeof buffer, buffer);
43 std::string result = std::string(buffer,
sizeof buffer);
44 CPPUNIT_ASSERT_EQUAL(payload, result);
50void TestReader::testReadData()
52 char payload[] =
"Test data loaded successfully";
53 std::string payload_string(payload,
sizeof payload - 1);
54 readData(payload_string);
57void TestReader::testReadBinaryData()
60 for(
int i = 0; i < 256; i++)
62 payload[i] = 0xD5 ^ i;
64 std::string payload_string(payload,
sizeof payload - 1);
65 readData(payload_string);
68uint64_t TestReader::readInteger(std::string payload, ::EndianType endiantype,
bool isSigned)
70 std::istringstream * iss =
new std::istringstream(payload);
75 ? reader->
ReadSigned(payload.size(), endiantype)
83uint64_t TestReader::parseInteger(std::string buffer, ::EndianType endiantype,
bool isSigned)
85 return readInteger(buffer, endiantype, isSigned);
88void TestReader::testReadInteger()
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));
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));
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));
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));
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));
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));
145void TestReader::testSeekSkip()
149 for(
int i = 0; i < 256; i++)
154 std::string payload_string(payload,
sizeof payload);
155 std::istringstream * iss =
new std::istringstream(payload_string);
160 CPPUNIT_ASSERT_EQUAL(123, (
int)(
unsigned char)buffer[0]);
164 CPPUNIT_ASSERT_EQUAL(123 + 1 + 45, (
int)(
unsigned char)buffer[0]);
170void TestReader::setUp()
174void TestReader::tearDown()
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