Add testing
This commit is contained in:
parent
88ca215573
commit
c33b2eee86
9 changed files with 109 additions and 6 deletions
|
@ -1,4 +1,46 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "preprocessor.hpp"
|
||||
#include "lexer.hpp"
|
||||
#include "syntax_tree.hpp"
|
||||
|
||||
std::string LoadFile(const std::string& file) {
|
||||
std::string program;
|
||||
std::ifstream fin(file);
|
||||
{
|
||||
std::string str;
|
||||
while (getline(fin, str)) {
|
||||
str.push_back('\n');
|
||||
program += str;
|
||||
}
|
||||
}
|
||||
return program;
|
||||
}
|
||||
|
||||
void RunTest(const std::string& program_file, const std::string& result_file) {
|
||||
std::string program = LoadFile(program_file);
|
||||
program = Preprocessor(std::move(program));
|
||||
Lexer lexer;
|
||||
lexer.ParseText(program);
|
||||
auto tokens = lexer.GetTokens();
|
||||
SyntaxTree tree;
|
||||
tree.PushLexerTokenList(tokens);
|
||||
tree.Compile();
|
||||
|
||||
SetPrintStringStream();
|
||||
tree.Run();
|
||||
std::string result = LoadFile(result_file);
|
||||
ASSERT_EQ(result, GetPrintStringStream().str());
|
||||
}
|
||||
|
||||
TEST(test_int, full_test) {
|
||||
RunTest("programs/int/1.omgpl", "programs/int/1.res");
|
||||
}
|
||||
|
||||
TEST(test_string, full_test) {
|
||||
RunTest("programs/string/1.omgpl", "programs/string/1.res");
|
||||
}
|
||||
|
||||
int main() {
|
||||
testing::InitGoogleTest();
|
||||
|
|
13
tests/programs/int/1.omgpl
Normal file
13
tests/programs/int/1.omgpl
Normal file
|
@ -0,0 +1,13 @@
|
|||
int x = 0;
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
x += i;
|
||||
}
|
||||
print(x);
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
x -= i;
|
||||
}
|
||||
print(x);
|
||||
for (int i = 0; i < 4; i += 1) {
|
||||
x = (x + 1) * i;
|
||||
}
|
||||
print(x);
|
3
tests/programs/int/1.res
Normal file
3
tests/programs/int/1.res
Normal file
|
@ -0,0 +1,3 @@
|
|||
45
|
||||
0
|
||||
15
|
11
tests/programs/string/1.omgpl
Normal file
11
tests/programs/string/1.omgpl
Normal file
|
@ -0,0 +1,11 @@
|
|||
string s = "";
|
||||
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
if ((i / 2) * 2 == i) {
|
||||
s += "a";
|
||||
}
|
||||
if ((i / 2) * 2 != i) {
|
||||
s += "b";
|
||||
}
|
||||
}
|
||||
print(s);
|
1
tests/programs/string/1.res
Normal file
1
tests/programs/string/1.res
Normal file
|
@ -0,0 +1 @@
|
|||
ababababab
|
Loading…
Add table
Add a link
Reference in a new issue