RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
buffer.cc
1
2#include <cppunit/extensions/HelperMacros.h>
3#include <cppunit/TestFixture.h>
4
5#include "../../src/linker/buffer.h"
6
7using namespace Linker;
8
9namespace UnitTests
10{
11
12class TestBuffer : public CppUnit::TestFixture
13{
14 CPPUNIT_TEST_SUITE(TestBuffer);
15 CPPUNIT_TEST(testCreateBuffer);
16 CPPUNIT_TEST(testReadFile);
17 CPPUNIT_TEST(testWriteFile);
18 CPPUNIT_TEST_SUITE_END();
19private:
20
21 void testCreateBuffer();
22 void testReadFile();
23 void testWriteFile();
24};
25
26void TestBuffer::testCreateBuffer()
27{
28 Buffer buffer (0x1234);
29 CPPUNIT_ASSERT_EQUAL(offset_t(0x1234), buffer.ActualDataSize());
30}
31
32void TestBuffer::testReadFile()
33{
34 Buffer buffer;
35
36 std::string input = "\1\2\3\4\5\6\7\x8\x9\xA\xB\xC\xD\xE\xF";
37
38 {
39 std::istringstream iss(input);
40 Reader rd(::LittleEndian, &iss);
41 buffer.ReadFile(rd);
42 CPPUNIT_ASSERT_EQUAL(offset_t(input.size()), buffer.ActualDataSize());
43 }
44
45 {
46 std::istringstream iss(input);
47 Reader rd(::LittleEndian, &iss);
48 buffer.ReadFile(rd, 0);
49 CPPUNIT_ASSERT_EQUAL(offset_t(0), buffer.ActualDataSize());
50 }
51
52 {
53 std::istringstream iss(input);
54 Reader rd(::LittleEndian, &iss);
55 buffer.ReadFile(rd, input.size() / 2);
56 CPPUNIT_ASSERT_EQUAL(offset_t(input.size() / 2), buffer.ActualDataSize());
57 }
58
59 {
60 std::istringstream iss(input);
61 Reader rd(::LittleEndian, &iss);
62 buffer.ReadFile(rd);
63 size_t offset = input.size() / 2;
64 CPPUNIT_ASSERT_EQUAL(offset_t(input.size()), buffer.ActualDataSize());
65 CPPUNIT_ASSERT_EQUAL(int(input[offset]), buffer.GetByte(offset));
66 }
67}
68
69void TestBuffer::testWriteFile()
70{
71 Buffer buffer;
72
73 std::string input = "\1\2\3\4\5\6\7\x8\x9\xA\xB\xC\xD\xE\xF";
74
75 {
76 std::istringstream iss(input);
77 Reader rd(::LittleEndian, &iss);
78 buffer.ReadFile(rd);
79 }
80
81 {
82 std::ostringstream oss;
83 Writer wr(::LittleEndian, &oss);
84 buffer.WriteFile(wr);
85 CPPUNIT_ASSERT_EQUAL(oss.str(), input);
86 }
87
88 {
89 std::ostringstream oss;
90 Writer wr(::LittleEndian, &oss);
91 offset_t start = input.size() / 5;
92 offset_t length = input.size() / 3;
93 buffer.WriteFile(wr, length, start);
94 CPPUNIT_ASSERT_EQUAL(oss.str(), input.substr(start, length));
95 }
96}
97
98}
99
A buffer that can be used to read and store data from a file.
Definition buffer.h:22
offset_t ActualDataSize() override
Retrieves size of stored data.
Definition buffer.cc:6
offset_t WriteFile(Writer &wr, offset_t count, offset_t offset=0) override
Writes data of non-zero filled sections.
Definition buffer.cc:40
int GetByte(offset_t offset) override
Retrieve byte at a certain offset (optional, might not be defined)
Definition buffer.cc:45
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15
Definition buffer.cc:13