-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
149 lines (129 loc) · 3.56 KB
/
test.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
#include "nrex.hpp"
#include <locale>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#ifdef NREX_UNICODE
typedef std::wstring string;
typedef std::wifstream ifstream;
typedef std::wistringstream isstream;
#else
typedef std::string string;
typedef std::ifstream ifstream;
typedef std::istringstream isstream;
#endif
class ctype : public std::ctype<char>
{
mask table[table_size];
public:
ctype(size_t refs = 0)
: std::ctype<char>(&table[0], false, refs)
{
std::copy(classic_table(), classic_table() + table_size, table);
table['/'] = (mask)space;
table[' '] = (mask)punct;
}
};
int main()
{
ifstream file("test.txt");
if (!file.is_open())
{
std::cout << "could not find test.txt" << std::endl;
return -2;
}
int tests = 0;
int passed = 0;
int line_number = 0;
std::locale locale(std::locale::classic(), new ctype);
nrex n;
std::cout << "==================" << std::endl;
while (!file.eof())
{
string line;
std::getline(file, line);
line_number++;
if (line.length() == 0 || line.at(0) == '#')
{
continue;
}
isstream stream(line);
stream.imbue(locale);
string pattern;
stream >> pattern;
tests++;
std::cout << "Line " << line_number << std::endl;
int captures = 0;
stream >> captures;
n.compile(pattern.c_str());
if (n.capture_size() != captures)
{
std::cout << " FAILED (Compile)" << std::endl;
continue;
}
if (captures == 0)
{
std::cout << " OK" << std::endl;
passed++;
continue;
}
string text;
stream >> text;
if (text.length() == 1 && text.at(0) == '#')
{
text.clear();
}
nrex_result* results = new nrex_result[captures];
bool found = n.match(text.c_str(), results);
bool failed = false;
int position;
stream >> position;
if ((position >= 0) != found || (found && position != results[0].start))
{
failed = true;
std::cout << " Mismatched starting positions. Expected: ";
std::cout << position << " Got: " << results[0].start << std::endl;
}
for (int i = 0; i < captures; i++)
{
string result;
stream >> result;
if (result.length() == 1 && result.at(0) == '#')
{
result.clear();
}
std::string capture;
if (results[i].length != 0)
{
capture = text.substr(results[i].start, results[i].length);
}
if (capture != result)
{
failed = true;
std::cout << " Mismatched results. Expected: '" << result;
std::cout << "'' Got: '" << capture << "'" << std::endl;
}
}
if (!failed)
{
std::cout << " OK" << std::endl;
passed++;
}
else
{
std::cout << " FAILED (Tests)" << std::endl;
}
delete[] results;
}
std::cout << "==================" << std::endl;
std::cout << "Tests: " << tests << std::endl;
std::cout << "Successes: " << passed << std::endl;
std::cout << "Failed: " << tests - passed << std::endl;
if (tests != passed)
{
return -1;
}
return 0;
}