-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_main.cpp
175 lines (140 loc) · 4.9 KB
/
regex_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Tests the TokEx implementation of RegEx.
Jordan Dehmel, 2024
jdehmel@outlook.com
*/
// #define SAVEFIGPATH "regex_dots/"
// #define SAVEFIG
#include "regex.hpp"
#include "regex_manager.hpp"
#include <chrono>
#include <initializer_list>
#include <iostream>
#include <stdexcept>
namespace clk = std::chrono;
////////////////////////////////////////////////////////////////
// Regex Macros
static RegexManager re_manager;
#define H "(a|b|c|d|e|f|A|B|C|D|E|F|0|1|2|3|4|5|6|7|8|9)"
#define O "(0|1|2|3|4|5|6|7)"
#define HEX_RE "0(x|X)(" H "+')*" H "+"
#define OCTAL_RE "(0|0(" O "+')*" O "+)"
#define BINARY_RE "0(b|B)((0|1)+')*(0|1)+"
#define DECIMAL_RE "-?(1|2|3|4|5|6|7|8|9)(\\d+')*\\d+"
#define INT_RE "(#{hex}|#{dec}|#{oct}|#{bin})"
////////////////////////////////////////////////////////////////
// Helper function(s)
/*
Asserts that all test cases pass.
*/
void test_regex(
const char *const _pattern,
const std::initializer_list<const char *> &_should_pass,
const std::initializer_list<const char *> &_should_fail)
{
// Timing objects
clk::high_resolution_clock::time_point start, end;
uint64_t compilation_us, case_sum_us = 0, n = 0;
std::list<const char *> failures;
bool r;
// Compile
start = clk::high_resolution_clock::now();
RegEx pattern = re_manager.create_regex(_pattern);
end = clk::high_resolution_clock::now();
compilation_us =
clk::duration_cast<clk::microseconds>(end - start)
.count();
// Run positive
for (const auto &item : _should_pass)
{
start = clk::high_resolution_clock::now();
r = regex_match(pattern, item);
end = clk::high_resolution_clock::now();
case_sum_us +=
clk::duration_cast<clk::microseconds>(end - start)
.count();
++n;
if (!r)
{
failures.push_back(item);
}
}
// Run negative
for (const auto &item : _should_fail)
{
start = clk::high_resolution_clock::now();
r = regex_match(pattern, item);
end = clk::high_resolution_clock::now();
case_sum_us +=
clk::duration_cast<clk::microseconds>(end - start)
.count();
++n;
if (r)
{
failures.push_back(item);
}
}
std::cout << "In RegEx pattern /" << _pattern << "/:\n"
<< "Success rate: "
<< (100.0 * (n - failures.size()) / (double)n)
<< "%\n";
// If failures happened, output them
if (!failures.empty())
{
std::cout << failures.size() << " errors ocurred!\n";
for (const auto &f : failures)
{
std::cout << "\t" << f << "\n";
}
throw std::runtime_error("Not all test cases passed!");
}
// Output timing data
std::cout << "Compilation us: " << compilation_us << "\n"
<< "Average run us: " << case_sum_us / (double)n
<< "\n\n";
}
////////////////////////////////////////////////////////////////
// Main function
int main()
{
re_manager.register_substitution("#{bin}", BINARY_RE);
re_manager.register_substitution("#{oct}", OCTAL_RE);
re_manager.register_substitution("#{dec}", DECIMAL_RE);
re_manager.register_substitution("#{hex}", HEX_RE);
test_regex("a*b+c?d", {"bbd", "aaaabcd"}, {"aaacd", "abc"});
test_regex("\\d+", {"123", "09876"}, {"", "123abc"});
test_regex("\\w+", {"foobar", "BobErt"}, {"greg123"});
test_regex("\\w+\\s\\w+", {"foo bbbar", "BobErt ROCKS"},
{"foobar", "foo ", " foo", "greg 123"});
// Email example
test_regex("(\\w|\\d)+@\\w+\\.\\w+",
{"jdehmel@outlook.com", "a@b.c"},
{"jdehmel@foobar@outlook.com", "1@2.c.d",
"jedehmel@ outlook. com"});
// Testing basics used for int literals
test_regex("(0+1)+", {"01001000101001"}, {"0100110011"});
test_regex("((0|1)+')*", {"11001100'1010'"},
{"11001100'101''"});
test_regex("(1+')*0+", {"1'1'11'11'00"}, {"'11'00", "11'"});
// Parshal int literal testing
test_regex(BINARY_RE,
{"0b1111'0000'1111'0000", "0B01011010101",
"0b101010'1'1"},
{"b1111'0000", "0v1111'0000", "0b1000'2011"});
test_regex(OCTAL_RE, {"01'234'567'654", "0", "0'1'2'3"},
{"012345678", "01234567'"});
test_regex(DECIMAL_RE,
{"10", "-123", "516", "-9999", "-19'92"},
{"0", "-0", "12349A"});
test_regex(HEX_RE, {"0x12'34'56'67'9A'bC'dd'ee'FF", "0x0"},
{"0xG", "0x"});
// Int literal example
test_regex(INT_RE,
{"123", "0123", "0x123", "0B1010'1010'1", "100",
"0x0", "201", "200"},
{"foo", "0xGorilla", "'0101010'", "0x", "0b", "",
"char", "0b1010'1002", "0xx0", "0xG", "10.0",
"100 0"});
std::cout << "All tests of RegEx via TokEx passed.\n";
return 0;
}