From ed70bd5250a4919650df109e622248b7bbe37d63 Mon Sep 17 00:00:00 2001 From: SharafMohamed Date: Thu, 5 Dec 2024 11:01:59 -0500 Subject: [PATCH] Move constants in test-register-handler.hpp to minimize scope. --- tests/test-register-handler.cpp | 62 ++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/tests/test-register-handler.cpp b/tests/test-register-handler.cpp index 6e9f010..2371fc9 100644 --- a/tests/test-register-handler.cpp +++ b/tests/test-register-handler.cpp @@ -10,18 +10,6 @@ using log_surgeon::finite_automata::RegisterHandler; using position_t = log_surgeon::finite_automata::PrefixTree::position_t; -constexpr position_t cInitialPos{0}; -constexpr position_t cNegativePos1{-1}; -constexpr position_t cNegativePos2{-100}; -constexpr position_t cSetPos1{5}; -constexpr position_t cSetPos2{10}; -constexpr position_t cSetPos3{15}; -constexpr size_t cNumRegisters{5}; -constexpr size_t cRegId1{0}; -constexpr size_t cRegId2{1}; -constexpr size_t cRegId3{2}; -constexpr size_t cInvalidRegId{10}; - namespace { /** * @param handler The register handler that will contain the new registers. @@ -30,13 +18,20 @@ namespace { auto registers_init(RegisterHandler& handler, size_t num_registers) -> void; auto registers_init(RegisterHandler& handler, size_t const num_registers) -> void { + constexpr position_t cDefaultPos{0}; + for (size_t i{0}; i < num_registers; ++i) { - handler.add_register(i, cInitialPos); + handler.add_register(i, cDefaultPos); } } } // namespace TEST_CASE("`RegisterHandler` tests", "[RegisterHandler]") { + constexpr position_t cInitialPos1{5}; + constexpr size_t cNumRegisters{5}; + constexpr size_t cRegId1{0}; + constexpr size_t cRegId2{1}; + RegisterHandler handler; SECTION("Initial state is empty") { @@ -46,46 +41,57 @@ TEST_CASE("`RegisterHandler` tests", "[RegisterHandler]") { registers_init(handler, cNumRegisters); SECTION("Set register position correctly") { - handler.set_register(cRegId1, cSetPos1); - REQUIRE(std::vector{cSetPos1} == handler.get_reversed_positions(cRegId1)); + handler.set_register(cRegId1, cInitialPos1); + REQUIRE(std::vector{cInitialPos1} == handler.get_reversed_positions(cRegId1)); } SECTION("Register relationships are maintained") { - handler.set_register(cRegId1, cSetPos1); - handler.set_register(cRegId2, cSetPos2); - handler.set_register(cRegId3, cSetPos3); + constexpr position_t cInitialPos2{10}; + constexpr position_t cInitialPos3{15}; + constexpr size_t cRegId3{2}; + + handler.set_register(cRegId1, cInitialPos1); + handler.set_register(cRegId2, cInitialPos2); + handler.set_register(cRegId3, cInitialPos3); auto positions{handler.get_reversed_positions(cRegId3)}; - REQUIRE(std::vector{cSetPos3, cSetPos2, cSetPos1} + REQUIRE(std::vector{cInitialPos3, cInitialPos2, cInitialPos1} == handler.get_reversed_positions(cRegId3)); } SECTION("Copy register index correctly") { - handler.set_register(cRegId1, cSetPos1); + handler.set_register(cRegId1, cInitialPos1); handler.copy_register(cRegId2, cRegId1); - REQUIRE(std::vector{cSetPos1} == handler.get_reversed_positions(cRegId2)); + REQUIRE(std::vector{cInitialPos1} == handler.get_reversed_positions(cRegId2)); } SECTION("`append_position` appends position correctly") { - handler.set_register(cRegId1, cSetPos1); - handler.append_position(cRegId1, cSetPos2); - REQUIRE(std::vector{cSetPos2, cSetPos1} + constexpr position_t cAppendPos{10}; + + handler.set_register(cRegId1, cInitialPos1); + handler.append_position(cRegId1, cAppendPos); + REQUIRE(std::vector{cAppendPos, cInitialPos1} == handler.get_reversed_positions(cRegId1)); } SECTION("Throws out of range correctly") { - REQUIRE_THROWS_AS(handler.set_register(cInvalidRegId, cSetPos1), std::out_of_range); + constexpr size_t cInvalidRegId{10}; + + REQUIRE_THROWS_AS(handler.set_register(cInvalidRegId, cInitialPos1), std::out_of_range); REQUIRE_THROWS_AS(handler.copy_register(cInvalidRegId, cRegId2), std::out_of_range); REQUIRE_THROWS_AS(handler.copy_register(cRegId1, cInvalidRegId), std::out_of_range); - REQUIRE_THROWS_AS(handler.append_position(cInvalidRegId, cSetPos1), std::out_of_range); + REQUIRE_THROWS_AS(handler.append_position(cInvalidRegId, cInitialPos1), std::out_of_range); REQUIRE_THROWS_AS(handler.get_reversed_positions(cInvalidRegId), std::out_of_range); } SECTION("Handles negative position values correctly") { + constexpr position_t cNegativePos1{-1}; + constexpr position_t cNegativePos2{-100}; + handler.set_register(cRegId1, cNegativePos1); - handler.append_position(cRegId1, cSetPos1); + handler.append_position(cRegId1, cInitialPos1); handler.append_position(cRegId1, cNegativePos2); - REQUIRE(std::vector{cNegativePos2, cSetPos1, cNegativePos1} + REQUIRE(std::vector{cNegativePos2, cInitialPos1, cNegativePos1} == handler.get_reversed_positions(cRegId1)); } }