Skip to content

Commit

Permalink
Merge branch 'individual-dfa-files' into remove-redundant-typenames
Browse files Browse the repository at this point in the history
  • Loading branch information
SharafMohamed committed Dec 5, 2024
2 parents d5b20fe + fab801f commit db84cb7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/log_surgeon/finite_automata/PrefixTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ auto PrefixTree::get_reversed_positions(id_t const node_id) const -> std::vector
auto current_node{m_nodes[node_id]};
while (false == current_node.is_root()) {
reversed_positions.push_back(current_node.get_position());
current_node = m_nodes[current_node.get_parent_node_id().value()];
current_node = m_nodes[current_node.get_parent_id_unsafe()];
}
return reversed_positions;
}
Expand Down
21 changes: 11 additions & 10 deletions src/log_surgeon/finite_automata/PrefixTree.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef LOG_SURGEON_FINITE_AUTOMATA_PREFIX_TREE_HPP
#define LOG_SURGEON_FINITE_AUTOMATA_PREFIX_TREE_HPP

#include <cstddef>
#include <cstdint>
#include <optional>
#include <stdexcept>
Expand All @@ -13,9 +14,9 @@ namespace log_surgeon::finite_automata {
* a sequence of positions for an individual tag:
* - Positive position node: Indicates the tag was matched at the position.
* - Negative position node: Indicates the tag was unmatched. If a negative node is the entire path,
* it indicates the tag was never matched. If the negative tag is along a path containing positive
* nodes, it functions as a placeholder. This can be useful for nested capture groups, to maintain a
* one-to-one mapping between the contained capture group and the enclosing capture group.
* it indicates the tag was never matched. If the negative tag is along a path containing positive
* nodes, it functions as a placeholder. This can be useful for nested capture groups, to maintain
* a one-to-one mapping between the contained capture group and the enclosing capture group.
*/
class PrefixTree {
public:
Expand Down Expand Up @@ -58,28 +59,28 @@ class PrefixTree {
private:
class Node {
public:
Node(std::optional<id_t> const parent_node_id, position_t const position)
: m_parent_node_id{parent_node_id},
Node(std::optional<id_t> const parent_id, position_t const position)
: m_parent_id{parent_id},
m_position{position} {}

[[nodiscard]] auto is_root() const -> bool { return false == m_parent_node_id.has_value(); }
[[nodiscard]] auto is_root() const -> bool { return false == m_parent_id.has_value(); }

[[nodiscard]] auto get_parent_node_id() const -> std::optional<id_t> {
return m_parent_node_id;
[[nodiscard]] auto get_parent_id_unsafe() const -> id_t {
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
return m_parent_id.value();
}

auto set_position(position_t const position) -> void { m_position = position; }

[[nodiscard]] auto get_position() const -> position_t { return m_position; }

private:
std::optional<id_t> m_parent_node_id;
std::optional<id_t> m_parent_id;
position_t m_position;
};

std::vector<Node> m_nodes;
};

} // namespace log_surgeon::finite_automata

#endif // LOG_SURGEON_FINITE_AUTOMATA_PREFIX_TREE_HPP
2 changes: 1 addition & 1 deletion src/log_surgeon/finite_automata/RegisterHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace log_surgeon::finite_automata {
* operations for these registers.
*
* NOTE: For efficiency, registers are not initialized when lexing a new string; instead, it is the
* responsibility of the DFA to set the register values when needed.
* DFA's responsibility to set the register values when needed.
*/
class RegisterHandler {
public:
Expand Down
4 changes: 2 additions & 2 deletions tests/test-prefix-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using id_t = PrefixTree::id_t;
using position_t = PrefixTree::position_t;

constexpr auto cRootId{PrefixTree::cRootId};
constexpr id_t cInvaidNodeId{100};
constexpr id_t cInvalidNodeId{100};
constexpr position_t cInsertPos1{4};
constexpr position_t cInsertPos2{7};
constexpr position_t cInsertPos3{9};
Expand Down Expand Up @@ -108,7 +108,7 @@ TEST_CASE("`PrefixTree` operations", "[PrefixTree]") {
PrefixTree tree;

// Test setting position before any insertions
REQUIRE_THROWS_AS(tree.set(cInvaidNodeId, cSetPos4), std::out_of_range);
REQUIRE_THROWS_AS(tree.set(cInvalidNodeId, cSetPos4), std::out_of_range);

// Test setting position just beyond valid range
auto const node_id_1{tree.insert(cRootId, cInsertPos1)};
Expand Down
74 changes: 43 additions & 31 deletions tests/test-register-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,88 @@
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 {
auto add_register_to_handler(RegisterHandler& handler) -> void {
for (size_t i{0}; i < cNumRegisters; ++i) {
handler.add_register(i, cInitialPos);
/**
* @param handler The register handler that will contain the new registers.
* @param num_registers The number of registers to initialize.
*/
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, 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") {
REQUIRE_THROWS_AS(handler.get_reversed_positions(cRegId1), std::out_of_range);
}

add_register_to_handler(handler);
registers_init(handler, cNumRegisters);

SECTION("Set register position correctly") {
handler.set_register(cRegId1, cSetPos1);
REQUIRE(std::vector<position_t>{cSetPos1} == handler.get_reversed_positions(cRegId1));
handler.set_register(cRegId1, cInitialPos1);
REQUIRE(std::vector<position_t>{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<position_t>{cSetPos3, cSetPos2, cSetPos1}
REQUIRE(std::vector<position_t>{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<position_t>{cSetPos1} == handler.get_reversed_positions(cRegId2));
REQUIRE(std::vector<position_t>{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<position_t>{cSetPos2, cSetPos1}
constexpr position_t cAppendPos{10};

handler.set_register(cRegId1, cInitialPos1);
handler.append_position(cRegId1, cAppendPos);
REQUIRE(std::vector<position_t>{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<position_t>{cNegativePos2, cSetPos1, cNegativePos1}
REQUIRE(std::vector<position_t>{cNegativePos2, cInitialPos1, cNegativePos1}
== handler.get_reversed_positions(cRegId1));
}
}

0 comments on commit db84cb7

Please sign in to comment.