Skip to content

Commit

Permalink
feat(tests): add address tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Jan 9, 2024
1 parent 8c7410d commit 8b3a662
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/address.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <random>
#include <boost/ut.hpp>
#include <lime/address.hpp>
#include <ranges>

using namespace boost::ut;
using namespace boost::ut::literals;

suite<"Address"> address_suite = []
{
auto seed = std::seed_seq{std::random_device{}()};
std::mt19937 random{seed};

static constexpr auto n = 50;
static constexpr auto offset = sizeof(int);

std::vector<int> data;
data.reserve(n);

for (auto i = 0u; n > i; i++)
{
data.emplace_back(random());
}

auto address = lime::address::at(reinterpret_cast<std::uintptr_t>(data.data()));
{
expect(eq(address.has_value(), true));
expect(eq(address->addr(), reinterpret_cast<std::uintptr_t>(data.data())));

for (const auto &value : data)
{
auto &current = address.value();
expect(eq(current.read<int>(), value));

auto next = current + offset;
expect(eq(next.has_value(), true));

address.emplace(std::move(next.value()));
}
}

auto back_address = lime::address::at(address->addr() - offset);
{
expect(eq(back_address.has_value(), true));

for (const auto &value : data | std::ranges::views::reverse)
{
auto &current = back_address.value();

expect(eq(current.read<int>(), value));
current.write(1337);

auto next = current - offset;
expect(eq(next.has_value(), true));

back_address.emplace(std::move(next.value()));
}
}

for (const auto &value : data)
{
expect(eq(value, 1337));
}
};

0 comments on commit 8b3a662

Please sign in to comment.