-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestMain.cpp
More file actions
145 lines (129 loc) · 5.66 KB
/
testMain.cpp
File metadata and controls
145 lines (129 loc) · 5.66 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
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
//
// Created by anirudhlath on 3/1/22.
//
#include <string>
#include <iostream>
#include "exec.h"
using namespace std;
string random_expr_string();
void compare(int argc, char *const *argv, const ExecResult &lhs, const ExecResult &rhs);
int main(int argc, char **argv) {
if (argc == 2) {
const char *const interp_argv[] = {argv[1], "--interp"};
const char *const print_argv[] = {argv[1], "--print"};
const char *const pretty_print_argv[] = {argv[1], "--pretty-print"};
for (int i = 0; i < 100; i++) {
std::string in = random_expr_string();
std::cout << "Test " << i + 1 << ": " << in << "\n";
ExecResult interp_result = exec_program(2, interp_argv, in);
ExecResult print_result = exec_program(2, print_argv, in);
ExecResult pretty_print_result = exec_program(2, pretty_print_argv, in);
ExecResult interp_again_result = exec_program(2, interp_argv, print_result.out);
if (interp_again_result.exit_code == 0 && interp_result.exit_code == 0) {
if (interp_again_result.out != interp_result.out) {
compare(argc, argv, interp_result, interp_again_result);
throw std::runtime_error("Different result for printed.");
}
interp_again_result = exec_program(2, interp_argv, pretty_print_result.out);
if (interp_again_result.out != interp_result.out) {
compare(argc, argv, interp_result, interp_again_result);
throw std::runtime_error("Different result for pretty printed.");
}
}
else {
cerr << "Program exited with error. Is the program name correct?" << endl;
return 1;
}
}
}
else if (argc == 3) {
const char *const interp_argv[] = {argv[1], "--interp"};
const char *const print_argv[] = {argv[1], "--print"};
const char *const pretty_print_argv[] = {argv[1], "--pretty-print"};
const char *const interp_argv2[] = {argv[2], "--interp"};
const char *const print_argv2[] = {argv[2], "--print"};
const char *const pretty_print_argv2[] = {argv[2], "--pretty-print"};
for (int i = 0; i < 100; i++) {
std::string in = random_expr_string();
std::cout << "Test " << i + 1 << ": " << in << "\n";
ExecResult interp_result = exec_program(2, interp_argv, in);
ExecResult print_result = exec_program(2, print_argv, in);
ExecResult pretty_print_result = exec_program(2, pretty_print_argv, in);
ExecResult interp_result2 = exec_program(2, interp_argv2, in);
ExecResult print_result2 = exec_program(2, print_argv2, in);
ExecResult pretty_print_result2 = exec_program(2, pretty_print_argv2, in);
if (interp_result.exit_code == 0 && interp_result2.exit_code == 0) {
if (interp_result.out != interp_result2.out) {
compare(argc, argv, interp_result, interp_result2);
throw std::runtime_error("Different result for interpret.");
}
if (print_result.out != print_result2.out) {
compare(argc, argv, print_result, print_result2);
throw std::runtime_error("Different result for print.");
}
if (pretty_print_result.out != pretty_print_result2.out) {
compare(argc, argv, pretty_print_result, pretty_print_result2);
throw std::runtime_error("Different result for pretty-print.");
}
}
else {
cerr << "Program exited with error. Is the program name correct?" << endl;
return 1;
}
}
}
else {
cerr << "No program names were passed in. You can enter upto 2 program names." << endl;
cout << "Syntax: '" << argv[0] << " <PROGRAM 1> <PROGRAM 2>'" << endl;
return 1;
}
cout << "\nAll tests have successfully passed." << endl;
return 0;
}
void compare(int argc, char *const *argv, const ExecResult &lhs, const ExecResult &rhs) {
if (!lhs.out.empty() || !lhs.err.empty()) {
cout << "LHS Results: " << endl;
cout << "EXIT_CODE: " << lhs.exit_code << endl;
cout << "OUT: " << lhs.out << endl;
cout << "ERROR: " << lhs.err << endl << endl;
}
else {
cerr
<< argv[1]
<< " has no output. Make sure you have entered the program name correctly amongst other possible problems.";
exit(1);
}
if (!rhs.out.empty() || !rhs.err.empty()) {
cout << "RHS Results: " << endl;
cout << "EXIT_CODE: " << lhs.exit_code << endl;
cout << "OUT: " << lhs.out << endl;
cout << "ERROR: " << lhs.err << endl << endl;
}
else {
if (argc == 3) {
cerr << argv[2]
<< " has no output. Make sure you have entered the program name correctly amongst other possible problems.";
}
else {
cerr << argv[1]
<< " has no output. Make sure you have entered the program name correctly amongst other possible problems.";
}
exit(1);
}
}
// Generates random expressions of numbers, additions and multiplications only.
string random_expr_string() {
int num = rand() % 10;
if (num < 6) {
return to_string(num);
}
else if (num < 8) {
return random_expr_string() + "+" + random_expr_string();
}
else if (num < 10) {
return random_expr_string() + "*" + random_expr_string();
}
else {
return to_string(num);
}
}