Skip to content

Commit

Permalink
Fix rvalue assign to class member in iterator tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jun 8, 2024
1 parent 7ee563f commit f08f584
Showing 1 changed file with 55 additions and 24 deletions.
79 changes: 55 additions & 24 deletions test/primitives/iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ class iterator_
: public iterator<Link, Key, Size>
{
public:
using iterator<Link, Key, Size>::iterator;
using base = iterator<Link, Key, Size>;

iterator_(const typename base::manager& memory, const Link& start,
const Key& key) NOEXCEPT
: iterator<Link, Key, Size>(memory, start, key)
{
}

bool is_match_() const NOEXCEPT
{
Expand All @@ -39,9 +45,6 @@ class iterator_
{
return base::get_next(base::get_ptr());
}

private:
using base = iterator<Link, Key, Size>;
};

BOOST_AUTO_TEST_CASE(iterator__get_next__empty__terminal)
Expand All @@ -51,8 +54,9 @@ BOOST_AUTO_TEST_CASE(iterator__get_next__empty__terminal)
using slab_iterate = iterator_<link, key, max_size_t>;

constexpr key key0{};
test::chunk_storage file;
const slab_iterate iterator{ file, link::terminal, key0 };
test::chunk_storage file{};
slab_iterate::manager manager{ file };
slab_iterate iterator{ manager, link::terminal, key0 };
BOOST_REQUIRE(iterator.get_next_().is_terminal());
}

Expand All @@ -62,16 +66,18 @@ BOOST_AUTO_TEST_CASE(iterator__get_next__overflow__terminal)
using key = data_array<0>;
using slab_iterate = iterator_<link, key, max_size_t>;

constexpr auto start = 13;
constexpr link head{ 13 };
constexpr key key0{};
data_chunk data
{
0x00, 0x01, 0x02, 0x03,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};

test::chunk_storage file{ data };
const slab_iterate iterator{ file, start, key0 };
slab_iterate::manager manager{ file };
slab_iterate iterator{ manager, head, key0 };
BOOST_REQUIRE(iterator.get_next_().is_terminal());
}

Expand All @@ -81,16 +87,18 @@ BOOST_AUTO_TEST_CASE(iterator__get__offset0__expected)
using key = data_array<0>;
using slab_iterate = iterator_<link, key, max_size_t>;

constexpr auto start = 0;
constexpr link head{ 0 };
constexpr key key0{};
data_chunk data
{
0x00, 0x01, 0x02, 0x03,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};

test::chunk_storage file{ data };
const slab_iterate iterator{ file, start, key0 };
slab_iterate::manager manager{ file };
slab_iterate iterator{ manager, head, key0 };
BOOST_REQUIRE_EQUAL(iterator.get_next_(), 0x03020100_u32);
}

Expand All @@ -100,16 +108,18 @@ BOOST_AUTO_TEST_CASE(iterator__get__offset1__expected)
using key = data_array<0>;
using slab_iterate = iterator_<link, key, max_size_t>;

constexpr auto start = 8;
constexpr link head{ 8 };
constexpr key key0{};
data_chunk data
{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x02, 0x03
};

test::chunk_storage file{ data };
const slab_iterate iterator{ file, start, key0 };
slab_iterate::manager manager{ file };
slab_iterate iterator{ manager, head, key0 };
BOOST_REQUIRE_EQUAL(iterator.get_next_(), 0x03020100_u32);
}

Expand All @@ -119,60 +129,81 @@ BOOST_AUTO_TEST_CASE(iterator__get__offset_to_back__expected)
using key = data_array<0>;
using slab_iterate = iterator_<link, key, max_size_t>;

constexpr auto start = 41;
constexpr link head{ 41 };
constexpr key key0{};
data_chunk data(42u, 0xff);

test::chunk_storage file{ data };
const slab_iterate iterator{ file, start, key0 };
slab_iterate::manager manager{ file };
slab_iterate iterator{ manager, head, key0 };
BOOST_REQUIRE_EQUAL(iterator.get_next_(), 0xffu);
}

BOOST_AUTO_TEST_CASE(iterator__next__self__expected)
{
using link = linkage<1>;
using key = data_array<2>;
using record_iterate = iterator_<link, key, 1>;
constexpr size_t value = 1;
using record_iterate = iterator_<link, key, value>;
static_assert(link::terminal == 0xff);

constexpr auto start = 0;
constexpr link head{ 0 };
constexpr key key2{ 0x1a, 0x2a };
data_chunk data
{
0x01, 0x1a, 0x2a, 0xee,
0x02, 0x1a, 0x2a, 0xee,
0xff, 0xcc, 0xcc, 0xee
};

test::chunk_storage file{ data };
record_iterate iterator1{ file, start, key2 };
record_iterate::manager manager{ file };
record_iterate iterator1{ manager, head, key2 };

// First link is zero, matched.
// First record is 0x00.
BOOST_REQUIRE_EQUAL(iterator1.self(), 0x00u);

// Sets self/link to 0x01 (data[4]), matched.
// Next record is 0x01 (data[0]->data[4]).
BOOST_REQUIRE_EQUAL(iterator1.get_next_(), 0x01u);
BOOST_REQUIRE(iterator1.is_match_());

// Set self/link to record 0x01.
BOOST_REQUIRE(iterator1.advance());
BOOST_REQUIRE(!iterator1.self().is_terminal());
BOOST_REQUIRE_EQUAL(iterator1.self(), 0x01u);
BOOST_REQUIRE(iterator1.is_match_());

// No more matches.
// Next record is 0x02 (data[4]->data[8]).
BOOST_REQUIRE_EQUAL(iterator1.get_next_(), 0x02u);
BOOST_REQUIRE_EQUAL(iterator1.self(), 0x01u);
BOOST_REQUIRE(iterator1.is_match_());

// Advance returns false (no match) and self becomes terminal.
BOOST_REQUIRE(!iterator1.advance());
BOOST_REQUIRE_EQUAL(iterator1.self(), link::terminal);
BOOST_REQUIRE(!iterator1.is_match_());
BOOST_REQUIRE(iterator1.self().is_terminal());
BOOST_REQUIRE(!iterator1.advance());
BOOST_REQUIRE(!iterator1.is_match_());
BOOST_REQUIRE(iterator1.self().is_terminal());
}

BOOST_AUTO_TEST_CASE(iterator__next__true__non_terminal)
{
using link = linkage<1>;
using key = data_array<2>;
using slab_iterate = iterator_<link, key, max_size_t>;
constexpr auto start = 0;

constexpr link head{ 0 };
constexpr key key2{ 0x1a, 0x2a };
data_chunk data
{
0x04, 0x1a, 0x2a, 0xee,
0x08, 0x1a, 0x2a, 0xee,
0xff, 0xcc, 0xcc, 0xee
};

test::chunk_storage file{ data };
slab_iterate iterator{ file, start, key2 };
slab_iterate::manager manager{ file };
slab_iterate iterator{ manager, head, key2 };

BOOST_REQUIRE(!iterator.self().is_terminal());
BOOST_REQUIRE(iterator.advance());
Expand Down

0 comments on commit f08f584

Please sign in to comment.