Skip to content

Commit

Permalink
Avoid rvalue storage as member reference.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jun 8, 2024
1 parent df555ea commit f69ac59
Showing 1 changed file with 118 additions and 28 deletions.
146 changes: 118 additions & 28 deletions test/primitives/iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class iterator_

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

Expand All @@ -46,11 +45,115 @@ class iterator_
{
return base::get_next(base::get_ptr());
}

private:
const base::manager manager_;
};

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

constexpr link head{ 0 };
constexpr key key2{ 0x1a, 0x2a };

// raw memory
data_chunk data
{
0x01, 0x1a, 0x2a, 0xee,
0x02, 0x1a, 0x2a, 0xee,
0xff, 0xcc, 0xcc, 0xee
};

// database::storage
test::chunk_storage file{ data };

// database::manager
record_iterate::manager manager{ file };

// database::iterator
record_iterate iterator1{ manager, head, key2 };

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

// 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_EQUAL(iterator1.self(), 0x01u);
BOOST_REQUIRE(iterator1.is_match_());

// 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(!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_test2)
{
using link = linkage<1>;
using key = data_array<2>;
constexpr size_t value = 1;
using record_iterate = iterator_<link, key, value>;
static_assert(link::terminal == 0xff);

constexpr link head{ 0 };
constexpr key key2{ 0x1a, 0x2a };

// raw memory
data_chunk data
{
0x01, 0x1a, 0x2a, 0xee,
0x02, 0x1a, 0x2a, 0xee,
0xff, 0xcc, 0xcc, 0xee
};

// database::storage
test::chunk_storage file{ data };

// database::manager{ database::iterator } <= database::manager is rvalue.
record_iterate iterator1{ file, head, key2 };

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

// 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_EQUAL(iterator1.self(), 0x01u);
BOOST_REQUIRE(iterator1.is_match_());

// 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(!iterator1.is_match_());
BOOST_REQUIRE(iterator1.self().is_terminal());
BOOST_REQUIRE(!iterator1.advance());
BOOST_REQUIRE(!iterator1.is_match_());
BOOST_REQUIRE(iterator1.self().is_terminal());
}

#if defined(DISABLED)

BOOST_AUTO_TEST_CASE(iterator__get_next__empty__terminal)
{
using link = linkage<4>;
Expand Down Expand Up @@ -138,11 +241,9 @@ BOOST_AUTO_TEST_CASE(iterator__next__self__expected)
{
using link = linkage<1>;
using key = data_array<2>;
constexpr size_t value = 1;
using record_iterate = iterator_<link, key, value>;
static_assert(link::terminal == 0xff);
using record_iterate = iterator_<link, key, 1>;

constexpr link head{ 0 };
constexpr auto start = 0;
constexpr key key2{ 0x1a, 0x2a };
data_chunk data
{
Expand All @@ -151,32 +252,19 @@ BOOST_AUTO_TEST_CASE(iterator__next__self__expected)
0xff, 0xcc, 0xcc, 0xee
};
test::chunk_storage file{ data };
record_iterate iterator1{ file, head, key2 };
record_iterate iterator1{ file, start, key2 };

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

// 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.
// Sets self/link to 0x01 (data[4]), matched.
BOOST_REQUIRE(iterator1.advance());
BOOST_REQUIRE(!iterator1.self().is_terminal());
BOOST_REQUIRE_EQUAL(iterator1.self(), 0x01u);
BOOST_REQUIRE(iterator1.is_match_());

// 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(!iterator1.is_match_());
BOOST_REQUIRE(iterator1.self().is_terminal());
// No more matches.
BOOST_REQUIRE(!iterator1.advance());
BOOST_REQUIRE(!iterator1.is_match_());
BOOST_REQUIRE(iterator1.self().is_terminal());
BOOST_REQUIRE_EQUAL(iterator1.self(), link::terminal);
}

BOOST_AUTO_TEST_CASE(iterator__next__true__non_terminal)
Expand All @@ -203,4 +291,6 @@ BOOST_AUTO_TEST_CASE(iterator__next__true__non_terminal)
BOOST_REQUIRE(iterator.self().is_terminal());
}

#endif // DISABLED

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit f69ac59

Please sign in to comment.