112 lines
2.8 KiB
C++
112 lines
2.8 KiB
C++
![]() |
#include <gtest/gtest.h>
|
||
|
#include <random>
|
||
|
#include "DFA/DFAGraph.hpp"
|
||
|
#include "NFA/NFATree.hpp"
|
||
|
#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) {
|
||
|
NFA::NFATree tree;
|
||
|
|
||
|
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);
|
||
|
|
||
|
DFA::DFAGraph res = converters::NFATreeToDFAGraph(std::move(tree));
|
||
|
|
||
|
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) {
|
||
|
NFA::NFATree tree;
|
||
|
|
||
|
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);
|
||
|
|
||
|
DFA::DFAGraph res = converters::NFATreeToDFAGraph(std::move(tree));
|
||
|
|
||
|
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));
|
||
|
}
|
||
|
}
|