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/section.h"
7#include "../../src/linker/segment.h"
8
9using namespace Linker;
10
11namespace UnitTests
12{
13
14class TestLocation : public CppUnit::TestFixture
15{
16 CPPUNIT_TEST_SUITE(TestLocation);
17 CPPUNIT_TEST(testDefaultLocations);
18 CPPUNIT_TEST(testLocationArithmetic);
19 CPPUNIT_TEST(testLocationToPosition);
20 CPPUNIT_TEST(testLocationDisplacement);
21 CPPUNIT_TEST_SUITE_END();
22private:
23 std::shared_ptr<Section> test_section;
24 void testDefaultLocations();
25 void testLocationArithmetic();
26 void testLocationToPosition();
27 void testLocationDisplacement();
28public:
29 void setUp();
30 void tearDown();
31};
32
33void TestLocation::testDefaultLocations()
34{
35 CPPUNIT_ASSERT_EQUAL(Location(), Location(nullptr, 0));
36 CPPUNIT_ASSERT_EQUAL(Location(123), Location(nullptr, 123));
37 CPPUNIT_ASSERT_EQUAL(Location(test_section), Location(test_section, 0));
38}
39
40void TestLocation::testLocationArithmetic()
41{
42 Section test(".test");
43 Location location = Location(test_section, 123);
44 location += 456;
45 CPPUNIT_ASSERT_EQUAL(location, Location(test_section, 123 + 456));
46 location -= 123;
47 CPPUNIT_ASSERT_EQUAL(location, Location(test_section, 456));
48 CPPUNIT_ASSERT_EQUAL(Location(test_section, 123) + 456, Location(test_section, 123 + 456));
49 CPPUNIT_ASSERT_EQUAL(Location(test_section, 123) - 456, Location(test_section, 123 - 456));
50}
51
52void TestLocation::testLocationToPosition()
53{
54 std::shared_ptr<Segment> the_segment = std::make_shared<Segment>(".test");
55 test_section->segment = the_segment;
56 test_section->SetAddress(0x1234);
57 test_section->bias = 0x123;
58
59 Location location = Location(test_section, 123);
60
61 CPPUNIT_ASSERT_EQUAL(Position(0x1234 + 123, test_section->segment.lock()), location.GetPosition());
62 CPPUNIT_ASSERT_EQUAL(Position(0x1234 - 0x123, test_section->segment.lock()), location.GetPosition(true));
63
64 location = Location(123);
65
66 CPPUNIT_ASSERT_EQUAL(Position(123, nullptr), location.GetPosition());
67 CPPUNIT_ASSERT_EQUAL(Position(0, nullptr), location.GetPosition(true));
68}
69
70void TestLocation::testLocationDisplacement()
71{
72 Displacement displacement;
73 Location location = Location(test_section, 123);
74 CPPUNIT_ASSERT(!location.Displace(displacement));
75 CPPUNIT_ASSERT_EQUAL(location, Location(test_section, 123));
76
77 std::shared_ptr<Section> unrelated_section = std::make_shared<Section>(".unrelated");
78 displacement[unrelated_section] = 456;
79 CPPUNIT_ASSERT(!location.Displace(displacement));
80 CPPUNIT_ASSERT_EQUAL(location, Location(test_section, 123));
81
82 displacement[test_section] = Location(unrelated_section, 789);
83 CPPUNIT_ASSERT(location.Displace(displacement));
84 CPPUNIT_ASSERT_EQUAL(location, Location(unrelated_section, 123 + 789));
85
86 location = Location(nullptr, 123);
87 CPPUNIT_ASSERT(!location.Displace(displacement));
88 CPPUNIT_ASSERT_EQUAL(location, Location(nullptr, 123));
89}
90
91void TestLocation::setUp()
92{
93 test_section = std::make_shared<Section>(".test");
94}
95
96void TestLocation::tearDown()
97{
98 test_section = nullptr;
99}
100
101}
102
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:25
Definition location.cc:15