formalang/tests/NFAToDFA/check_equivalence.cpp

112 lines
2.8 KiB
C++
Raw Normal View History

2021-10-05 12:01:30 +00:00
#include <gtest/gtest.h>
#include <random>
#include "DFA/DFAGraph.hpp"
2021-10-05 13:11:24 +00:00
#include "NFA/NFAGraph.hpp"
2021-10-05 12:01:30 +00:00
#include "converters/NFAToDFA.hpp"
using namespace NFA;
using namespace DFA;
using namespace converters;
std::mt19937 rnd(1337);
std::string GenerateRandomString(std::vector<char> alphabet, size_t len) {
std::string res(len, ' ');
for (auto& i: res) {
i = alphabet[rnd() % alphabet.size()];
}
return res;
}
TEST(check_equivalence_NFA_to_DFA, 1) {
2021-10-05 13:11:24 +00:00
NFA::NFAGraph tree;
2021-10-05 12:01:30 +00:00
const int N = 10;
int a[N];
for (int i = 0; i < N; ++i)
a[i] = tree.AddNewVertex();
tree.GetVertex(a[0])->AddEdge('a', a[1]);
tree.GetVertex(a[1])->AddEdge('a', a[2]);
tree.GetVertex(a[0])->AddEdge('a', a[3]);
tree.GetVertex(a[0])->SetStart(true);
tree.GetVertex(a[2])->SetFinal(true);
tree.GetVertex(a[3])->SetFinal(true);
2021-10-05 13:11:24 +00:00
DFA::DFAGraph res = converters::NFAGraphToDFAGraph(std::move(tree));
2021-10-05 12:01:30 +00:00
EXPECT_FALSE(res.Accepted(""));
EXPECT_TRUE(res.Accepted("a"));
EXPECT_TRUE(res.Accepted("aa"));
EXPECT_FALSE(res.Accepted("aaa"));
EXPECT_FALSE(res.Accepted("aaaa"));
EXPECT_FALSE(res.Accepted("aaaaa"));
EXPECT_FALSE(res.Accepted("b"));
}
TEST(check_equivalence_NFA_to_DFA, 2) {
2021-10-05 13:11:24 +00:00
NFA::NFAGraph tree;
2021-10-05 12:01:30 +00:00
const int N = 10;
int a[N];
for (int i = 0; i < N; ++i)
a[i] = tree.AddNewVertex();
tree.GetVertex(a[0])->AddEdge('a', a[1]);
tree.GetVertex(a[0])->AddEdge('a', a[2]);
tree.GetVertex(a[1])->AddEdge('b', a[1]);
tree.GetVertex(a[2])->AddEdge('c', a[2]);
tree.GetVertex(a[0])->SetStart(true);
tree.GetVertex(a[1])->SetFinal(true);
tree.GetVertex(a[2])->SetFinal(true);
2021-10-05 13:11:24 +00:00
DFA::DFAGraph res = converters::NFAGraphToDFAGraph(std::move(tree));
2021-10-05 12:01:30 +00:00
auto check = [](const std::string& str) {
if (str.size() > 0) {
if (str[0] == 'a') {
if (str.size() == 1) {
return true;
}
if (str[1] == 'b') {
for (int i = 1; i < str.size(); ++i) {
if (str[i] != 'b') {
return false;
}
}
return true;
}
if (str[1] == 'c') {
for (int i = 1; i < str.size(); ++i) {
if (str[i] != 'c') {
return false;
}
}
return true;
}
}
}
return false;
};
EXPECT_FALSE(res.Accepted(""));
EXPECT_TRUE(res.Accepted("a"));
EXPECT_FALSE(res.Accepted("aa"));
EXPECT_FALSE(res.Accepted("aaa"));
EXPECT_FALSE(res.Accepted("aaaa"));
EXPECT_FALSE(res.Accepted("aaaaa"));
EXPECT_FALSE(res.Accepted("b"));
EXPECT_TRUE(res.Accepted("ab"));
EXPECT_TRUE(res.Accepted("abb"));
EXPECT_TRUE(res.Accepted("ac"));
EXPECT_TRUE(res.Accepted("acc"));
for (int i = 0; i < 5000; ++i) {
auto str = GenerateRandomString({'a', 'b', 'c', 'd'}, 20);
ASSERT_EQ(res.Accepted(str), check(str));
}
}