From 04109bd54b066bcd2c6f4f44f41e37c36ca8bac9 Mon Sep 17 00:00:00 2001 From: MaxanRus Date: Mon, 11 Oct 2021 01:13:50 +0300 Subject: [PATCH] Add testing minimization --- CMakeLists.txt | 1 + include/DFA/DFAGraph.hpp | 1 + include/NFA/NFAGraph.hpp | 1 + src/DFA/DFAGraph.cpp | 4 +++ src/NFA/NFAGraph.cpp | 4 +++ tests/DFAToMinDFA/CountSizesMinDFA.cpp | 40 ++++++++++++++++++++++++++ 6 files changed, 51 insertions(+) create mode 100644 tests/DFAToMinDFA/CountSizesMinDFA.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d479af6..703bedc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ set(TEST_FILES tests/DFAToRegular/DFAToRegular.cpp tests/invertFDFA/InvertFDFA.cpp tests/DFAToRegular/DFAToRegular2.cpp + tests/DFAToMinDFA/CountSizesMinDFA.cpp ) add_executable(Formalang src/main.cpp ${SOURCE_FILES}) diff --git a/include/DFA/DFAGraph.hpp b/include/DFA/DFAGraph.hpp index 606a166..f1cb616 100644 --- a/include/DFA/DFAGraph.hpp +++ b/include/DFA/DFAGraph.hpp @@ -55,6 +55,7 @@ class DFAGraph { bool NotExistVertex(size_t number); size_t GetCountVertexes() const; + size_t GetReallyCountVertexes() const; const std::vector& GetFinalVertexes() const; size_t GetStartVertex() const; diff --git a/include/NFA/NFAGraph.hpp b/include/NFA/NFAGraph.hpp index 6fd1738..db389af 100644 --- a/include/NFA/NFAGraph.hpp +++ b/include/NFA/NFAGraph.hpp @@ -59,6 +59,7 @@ class NFAGraph { bool NotExistVertex(size_t number); size_t GetCountVertexes() const; + size_t GetReallyCountVertexes() const; const std::vector& GetFinalVertexes() const; const std::vector& GetStartVertexes() const; diff --git a/src/DFA/DFAGraph.cpp b/src/DFA/DFAGraph.cpp index f6f2388..011df6e 100644 --- a/src/DFA/DFAGraph.cpp +++ b/src/DFA/DFAGraph.cpp @@ -88,6 +88,10 @@ size_t DFAGraph::GetCountVertexes() const { return count_vertexes_; } +size_t DFAGraph::GetReallyCountVertexes() const { + return vertexes_.size(); +} + const std::vector& DFAGraph::GetFinalVertexes() const { return final_vertexes_; } diff --git a/src/NFA/NFAGraph.cpp b/src/NFA/NFAGraph.cpp index b27bb21..2d31831 100644 --- a/src/NFA/NFAGraph.cpp +++ b/src/NFA/NFAGraph.cpp @@ -150,6 +150,10 @@ size_t NFAGraph::GetCountVertexes() const { return count_vertexes_; } +size_t NFAGraph::GetReallyCountVertexes() const { + return vertexes_.size(); +} + const std::vector& NFAGraph::GetFinalVertexes() const { return final_vertexes_; } diff --git a/tests/DFAToMinDFA/CountSizesMinDFA.cpp b/tests/DFAToMinDFA/CountSizesMinDFA.cpp new file mode 100644 index 0000000..66a1cf5 --- /dev/null +++ b/tests/DFAToMinDFA/CountSizesMinDFA.cpp @@ -0,0 +1,40 @@ +#include +#include "regular/RegularTree.hpp" +#include "converters/RegularToNFA.hpp" +#include "converters/NFAToDFA.hpp" +#include "converters/DFAToMinDFA.hpp" +#include "converters/DFAToRegular.hpp" + +using namespace regular; +using namespace NFA; +using namespace DFA; +using namespace converters; + +size_t GetCountVertexes(const std::string& regular) { + RegularTree r(regular); + NFAGraph NFA_tree = RegularToNFAGraph(std::move(r)); + DFAGraph DFA_graph = NFAGraphToDFAGraph(std::move(NFA_tree)); + DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph)); + + return DFA_graph.GetReallyCountVertexes(); +} + +TEST(DFA_to_min_DFA, a_start) { + EXPECT_EQ(GetCountVertexes("a*"), GetCountVertexes("( a | )*| | | aaa a a")); +} + +TEST(DFA_to_min_DFA, a_plus) { + EXPECT_EQ(GetCountVertexes("a+"), GetCountVertexes("( a | a a )+|a | a |a ")); +} + +TEST(DFA_to_min_DFA, a_star_or_b_star) { + EXPECT_EQ(GetCountVertexes("a*|b*"), GetCountVertexes("a|b|a*|a+|b*|b+|bbb b b b| a a aa a aa | a+")); +} + +TEST(DFA_to_min_DFA, epsilon) { + EXPECT_EQ(GetCountVertexes(" "), GetCountVertexes(" | | | ")); +} + +TEST(DFA_to_min_DFA, a_or_b_star) { + EXPECT_EQ(GetCountVertexes("(a|b)*"), GetCountVertexes("a+|b+|a*|b*|a*b*a*|a|b(a|b)*|b| |(a|b)*")); +}