From f69ac590c857a803d5fa7097ecdf0bc6dc2d1307 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sat, 8 Jun 2024 16:32:57 -0400 Subject: [PATCH] Avoid rvalue storage as member reference. --- test/primitives/iterator.cpp | 146 ++++++++++++++++++++++++++++------- 1 file changed, 118 insertions(+), 28 deletions(-) diff --git a/test/primitives/iterator.cpp b/test/primitives/iterator.cpp index 196f0391..2dcb0b84 100644 --- a/test/primitives/iterator.cpp +++ b/test/primitives/iterator.cpp @@ -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(memory, start, key) { } @@ -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_; + 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_; + 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>; @@ -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_; - static_assert(link::terminal == 0xff); + using record_iterate = iterator_; - constexpr link head{ 0 }; + constexpr auto start = 0; constexpr key key2{ 0x1a, 0x2a }; data_chunk data { @@ -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) @@ -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()