RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
location.cc
1
2#include <cppunit/extensions/HelperMacros.h>
3#include <cppunit/TestFixture.h>
4
5#include "../../src/linker/location.h"
6#include "../../src/linker/position.h"
7#include "../../src/linker/section.h"
8#include "../../src/linker/segment.h"
9
10using namespace Linker;
11
12namespace UnitTests
13{
14
15class TestLocation : public CppUnit::TestFixture
16{
17 CPPUNIT_TEST_SUITE(TestLocation);
18 CPPUNIT_TEST(testDefaultLocations);
19 CPPUNIT_TEST(testLocationArithmetic);
20 CPPUNIT_TEST(testLocationToPosition);
21 CPPUNIT_TEST(testLocationDisplacement);
22 CPPUNIT_TEST_SUITE_END();
23private:
24 std::shared_ptr<Section> test_section;
25 void testDefaultLocations();
26 void testLocationArithmetic();
27 void testLocationToPosition();
28 void testLocationDisplacement();
29public:
30 void setUp() override;
31 void tearDown() override;
32};
33
34void TestLocation::testDefaultLocations()
35{
36 CPPUNIT_ASSERT_EQUAL(Location(), Location(nullptr, 0));
37 CPPUNIT_ASSERT_EQUAL(Location(123), Location(nullptr, 123));
38 CPPUNIT_ASSERT_EQUAL(Location(test_section), Location(test_section, 0));
39}
40
41void TestLocation::testLocationArithmetic()
42{
43 Section test(".test");
44 Location location = Location(test_section, 123);
45 location += 456;
46 CPPUNIT_ASSERT_EQUAL(location, Location(test_section, 123 + 456));
47 location -= 123;
48 CPPUNIT_ASSERT_EQUAL(location, Location(test_section, 456));
49 CPPUNIT_ASSERT_EQUAL(Location(test_section, 123) + 456, Location(test_section, 123 + 456));
50 CPPUNIT_ASSERT_EQUAL(Location(test_section, 123) - 456, Location(test_section, 123 - 456));
51}
52
53void TestLocation::testLocationToPosition()
54{
55 std::shared_ptr<Segment> the_segment = std::make_shared<Segment>(".test");
56 test_section->segment = the_segment;
57 test_section->SetAddress(0x1234);
58 test_section->bias = 0x123;
59
60 Location location = Location(test_section, 123);
61
62 CPPUNIT_ASSERT_EQUAL(Position(0x1234 + 123, test_section->segment.lock()), location.GetPosition());
63 CPPUNIT_ASSERT_EQUAL(Position(0x1234 - 0x123, test_section->segment.lock()), location.GetPosition(true));
64
65 location = Location(123);
66
67 CPPUNIT_ASSERT_EQUAL(Position(123, nullptr), location.GetPosition());
68 CPPUNIT_ASSERT_EQUAL(Position(0, nullptr), location.GetPosition(true));
69}
70
71void TestLocation::testLocationDisplacement()
72{
73 Displacement displacement;
74 Location location = Location(test_section, 123);
75 CPPUNIT_ASSERT(!location.Displace(displacement));
76 CPPUNIT_ASSERT_EQUAL(location, Location(test_section, 123));
77
78 std::shared_ptr<Section> unrelated_section = std::make_shared<Section>(".unrelated");
79 displacement[unrelated_section] = 456;
80 CPPUNIT_ASSERT(!location.Displace(displacement));
81 CPPUNIT_ASSERT_EQUAL(location, Location(test_section, 123));
82
83 displacement[test_section] = Location(unrelated_section, 789);
84 CPPUNIT_ASSERT(location.Displace(displacement));
85 CPPUNIT_ASSERT_EQUAL(location, Location(unrelated_section, 123 + 789));
86
87 location = Location(nullptr, 123);
88 CPPUNIT_ASSERT(!location.Displace(displacement));
89 CPPUNIT_ASSERT_EQUAL(location, Location(nullptr, 123));
90}
91
92void TestLocation::setUp()
93{
94 test_section = std::make_shared<Section>(".test");
95}
96
97void TestLocation::tearDown()
98{
99 test_section = nullptr;
100}
101
102}
103
Represents a single offset within a section, or an absolute location in memory if the section is null...
Definition location.h:17
Position GetPosition(bool segment_of=false) const
Calculates the address.
Definition location.cc:22
bool Displace(const Displacement &displacement)
Recalculates location after a section has moved.
Definition location.cc:8
Stores an absolute address along with the containing segment or address space.
Definition position.h:17
A section of data as read from an object file.
Definition section.h:24
Definition location.cc:16