2#include <cppunit/extensions/HelperMacros.h>
3#include <cppunit/TestFixture.h>
5#include "../../src/linker/location.h"
6#include "../../src/linker/position.h"
7#include "../../src/linker/section.h"
8#include "../../src/linker/segment.h"
10using namespace Linker;
18 CPPUNIT_TEST(testDefaultLocations);
19 CPPUNIT_TEST(testLocationArithmetic);
20 CPPUNIT_TEST(testLocationToPosition);
21 CPPUNIT_TEST(testLocationDisplacement);
22 CPPUNIT_TEST_SUITE_END();
24 std::shared_ptr<Section> test_section;
25 void testDefaultLocations();
26 void testLocationArithmetic();
27 void testLocationToPosition();
28 void testLocationDisplacement();
30 void setUp()
override;
31 void tearDown()
override;
34void TestLocation::testDefaultLocations()
41void TestLocation::testLocationArithmetic()
46 CPPUNIT_ASSERT_EQUAL(location,
Location(test_section, 123 + 456));
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));
53void TestLocation::testLocationToPosition()
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;
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));
71void TestLocation::testLocationDisplacement()
73 Displacement displacement;
75 CPPUNIT_ASSERT(!location.
Displace(displacement));
76 CPPUNIT_ASSERT_EQUAL(location,
Location(test_section, 123));
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));
83 displacement[test_section] =
Location(unrelated_section, 789);
84 CPPUNIT_ASSERT(location.
Displace(displacement));
85 CPPUNIT_ASSERT_EQUAL(location,
Location(unrelated_section, 123 + 789));
88 CPPUNIT_ASSERT(!location.
Displace(displacement));
89 CPPUNIT_ASSERT_EQUAL(location,
Location(
nullptr, 123));
92void TestLocation::setUp()
94 test_section = std::make_shared<Section>(
".test");
97void TestLocation::tearDown()
99 test_section =
nullptr;
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