2#include <cppunit/extensions/HelperMacros.h>
3#include <cppunit/TestFixture.h>
5#include "../../src/linker/location.h"
6#include "../../src/linker/section.h"
7#include "../../src/linker/segment.h"
17 CPPUNIT_TEST(testDefaultLocations);
18 CPPUNIT_TEST(testLocationArithmetic);
19 CPPUNIT_TEST(testLocationToPosition);
20 CPPUNIT_TEST(testLocationDisplacement);
21 CPPUNIT_TEST_SUITE_END();
23 std::shared_ptr<Section> test_section;
24 void testDefaultLocations();
25 void testLocationArithmetic();
26 void testLocationToPosition();
27 void testLocationDisplacement();
33void TestLocation::testDefaultLocations()
40void TestLocation::testLocationArithmetic()
45 CPPUNIT_ASSERT_EQUAL(location,
Location(test_section, 123 + 456));
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));
52void TestLocation::testLocationToPosition()
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;
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));
70void TestLocation::testLocationDisplacement()
72 Displacement displacement;
74 CPPUNIT_ASSERT(!location.
Displace(displacement));
75 CPPUNIT_ASSERT_EQUAL(location,
Location(test_section, 123));
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));
82 displacement[test_section] =
Location(unrelated_section, 789);
83 CPPUNIT_ASSERT(location.
Displace(displacement));
84 CPPUNIT_ASSERT_EQUAL(location,
Location(unrelated_section, 123 + 789));
87 CPPUNIT_ASSERT(!location.
Displace(displacement));
88 CPPUNIT_ASSERT_EQUAL(location,
Location(
nullptr, 123));
91void TestLocation::setUp()
93 test_section = std::make_shared<Section>(
".test");
96void TestLocation::tearDown()
98 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:25
Definition location.cc:15