-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.cpp
More file actions
86 lines (71 loc) · 1.95 KB
/
Copy pathTests.cpp
File metadata and controls
86 lines (71 loc) · 1.95 KB
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
#include "tests.h"
#include "lexer.h"
#include "parser.h"
#include <string>
#include <fstream>
#define LTESTS "Tests\\lexer\\"
#define PTESTS "Tests\\parser\\"
#define IN ".in"
#define OUT ".out"
#define LCNT 15
#define PCNT 50
#define OK "OK"
#define WA "WA"
string int2str(int a){
string res = "";
int i = 0;
while (a > 9){
res = int2str(a % 10) + res;
a /= 10;
}
res = char('0' + a) + res;
return res;
}
char tk_name[10];
void tests::lexer_tests(){
bool fail = 0;
token tk, cur_tk;
cout << "LEXER:" << endl;
for (int i = 1; i <= LCNT; i++){
string fin_name = LTESTS; fin_name += int2str(i); fin_name += IN;
string fout_name = LTESTS; fout_name += int2str(i); fout_name += OUT;
lexer L(fin_name.c_str());
ifstream fout(fout_name.c_str(), ios::in);
while (L.token_can_exist()){
tk = token();
fout >> tk.pos.row >> tk.pos.col >> tk.src >> tk_name;
cur_tk = L.next();
if (!(cur_tk.pos == tk.pos && cur_tk.src == tk.src && strcmp(tk_name, token_names[cur_tk.type]) == 0)){
cout << i << ". " << WA << ": row = " << tk.pos.row << ", col = " << tk.pos.col << endl;
fail = 1;
break;
}
}
if (fail) break;
cout << i << ". " << OK << endl;
fout.close();
}
}
void tests::parser_tests(){
bool fail = 0;
token tk, cur_tk;
cout << "PARSER:" << endl;
for (int i = 1; i <= PCNT; i++){
ofstream ans("Tests\\parser\\ans.a");
ifstream ans2("Tests\\parser\\ans.a");
string fin_name = PTESTS; fin_name += int2str(i); fin_name += IN;
string fout_name = PTESTS; fout_name += int2str(i); fout_name += OUT;
lexer L(fin_name.c_str());
parser P(&L);
ifstream fout(fout_name.c_str(), ios::in);
L.next();
expr *e = P.parse_expr();
e->print(ans, 0);
ans.close();
string s1((istreambuf_iterator<char>(fout)), istreambuf_iterator<char>());
string s2((istreambuf_iterator<char>(ans2)), istreambuf_iterator<char>());
cout << i << ". " << ((s1 == s2) ? "OK" : "WA") << endl;
ans2.close();
fout.close();
}
}