-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
78 lines (67 loc) · 2.24 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
#include "XMLOutputEngine.h"
#include "compilation_engine.h"
#include "error.h"
#include "hack_map.h"
#include "tokenizer.h"
#include <filesystem>
#include <iostream>
#include <memory>
using std::string;
bool test_hack_map ();
bool test_tokenizer ();
bool test_compilation_engine_and_xml_output_engine ();
bool test (bool (*func) (), string&& test);
int main () {
test (test_hack_map, std::string ("Testing HackMap"));
test (test_tokenizer, std::string ("Testing Tokenizer"));
test (test_compilation_engine_and_xml_output_engine,
"Testing the compilation engine and the xml output engine");
}
bool test (bool (*func) (), string&& test_name) {
if (func ()) {
std::cout << "-- " << test_name << " Succeced\n";
return true;
}
std::cout << "-- " << test_name << " Failed\n";
return false;
}
bool test_hack_map () {
HackMap hack_map;
return hack_map.contains_keyword ("class") && hack_map.contains_symbol ('*');
}
bool test_tokenizer () {
std::shared_ptr<HackMap> hack_map;
std::filesystem::path inp_file ("tests/test.jack");
if (!std::filesystem::exists (inp_file)) {
std::cout << "--- file not found" << std::endl;
return false;
}
Tokenizer tokenizer (inp_file, hack_map);
while (tokenizer.has_more_tokens ()) {
auto token = tokenizer.advance ();
std::cout << token.value << " " << hack_map->get_token (token.type) << std::endl;
}
return true;
}
bool test_compilation_engine_and_xml_output_engine () {
std::filesystem::path inp_file ("tests/test.jack");
if (!std::filesystem::exists (inp_file)) {
std::cout << "--- file not found" << std::endl;
return false;
}
try {
const std::shared_ptr<HackMap> hack_map;
const std::shared_ptr<Tokenizer> tokenizer =
std::make_shared<Tokenizer> (inp_file, hack_map);
const std::shared_ptr<XMLOutputEngine> output_engine =
std::make_shared<XMLOutputEngine> ("tests/test.xml", hack_map, 1);
CompilationEngine engine (hack_map);
engine.set_tokenizer (tokenizer);
engine.set_output_engine (output_engine);
engine.compile ();
return true;
} catch (Error& errr) {
// throw errr;
return false;
}
}