70 lines
1.8 KiB
C++
70 lines
1.8 KiB
C++
![]() |
#include <gtest/gtest.h>
|
||
|
#include <random>
|
||
|
#include "regular/RegularTree.hpp"
|
||
|
#include "converters/RegularToNFA.hpp"
|
||
|
#include "converters/NFAToDFA.hpp"
|
||
|
#include "converters/DFAToMinDFA.hpp"
|
||
|
#include "converters/DFAToFDFA.hpp"
|
||
|
#include "converters/InvertFDFA.hpp"
|
||
|
#include "converters/DFAToRegular.hpp"
|
||
|
|
||
|
using namespace regular;
|
||
|
using namespace NFA;
|
||
|
using namespace DFA;
|
||
|
using namespace converters;
|
||
|
|
||
|
extern std::mt19937 rnd;
|
||
|
|
||
|
extern std::string GenerateRandomString(std::vector<char> alphabet, size_t len);
|
||
|
|
||
|
TEST(generic_tests, divided_5) {
|
||
|
DFAGraph DFA_graph;
|
||
|
size_t a[10];
|
||
|
for (int i = 0; i < 10; ++i) {
|
||
|
a[i] = DFA_graph.AddNewVertex();
|
||
|
}
|
||
|
auto add_edge = [&DFA_graph, &a](size_t in, size_t out, char x) {
|
||
|
DFA_graph.GetVertex(a[in]).AddEdge(x, a[out]);
|
||
|
};
|
||
|
add_edge(5, 6, '0');
|
||
|
add_edge(5, 1, '1');
|
||
|
add_edge(1, 2, '0');
|
||
|
add_edge(1, 3, '1');
|
||
|
add_edge(2, 4, '0');
|
||
|
add_edge(2, 0, '1');
|
||
|
add_edge(3, 1, '0');
|
||
|
add_edge(3, 2, '1');
|
||
|
add_edge(4, 3, '0');
|
||
|
add_edge(4, 4, '1');
|
||
|
add_edge(0, 0, '0');
|
||
|
add_edge(0, 1, '1');
|
||
|
DFA_graph.GetVertex(a[5]).SetStart(true);
|
||
|
DFA_graph.GetVertex(a[6]).SetFinal(true);
|
||
|
DFA_graph.GetVertex(a[0]).SetFinal(true);
|
||
|
|
||
|
DFA_graph = DFAGraphToMinDFAGraph(std::move(DFA_graph));
|
||
|
DFA_graph = NFAGraphToDFAGraph(RegularToNFAGraph(RegularTree(DFAGraphToRegular(std::move(DFA_graph)))));
|
||
|
|
||
|
auto check = [](const std::string& str) -> bool {
|
||
|
bool answer = true;
|
||
|
if (str.size() == 1) {
|
||
|
return str == "0";
|
||
|
}
|
||
|
if (str[0] == '0') {
|
||
|
return false;
|
||
|
}
|
||
|
int remainder = 0;
|
||
|
for (auto i: str) {
|
||
|
remainder = remainder * 2 + i - '0';
|
||
|
remainder %= 5;
|
||
|
}
|
||
|
return remainder == 0;
|
||
|
};
|
||
|
|
||
|
for (int i = 0; i < 2000; ++i) {
|
||
|
std::string test = GenerateRandomString({'0', '1'}, rnd() % 1000 + 1);
|
||
|
|
||
|
ASSERT_EQ(DFA_graph.Accepted(test), check(test));
|
||
|
}
|
||
|
}
|