Skip to content

Commit

Permalink
test prepared for 2nd chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
6rayWa1cher committed Jun 6, 2020
1 parent 43a8979 commit 8c7ed01
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/include/Translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <queue>

class Translator {
private:
protected:
std::vector<std::shared_ptr<Atom>> _atoms;
SymbolTable _symbolTable;
StringTable _stringTable;
Expand All @@ -25,7 +25,7 @@ class Translator {
public:
Translator(std::istream& inputStream);

void startTranslation();
virtual void startTranslation();

const SymbolTable& getSymbolTable() const;

Expand All @@ -43,7 +43,7 @@ class Translator {

const std::deque<Token>& getLastLexems() const;

private:
protected:
std::shared_ptr<RValue> E();

std::shared_ptr<RValue> E1();
Expand Down
76 changes: 45 additions & 31 deletions test/integrationtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,23 @@ std::vector<std::string> split(const std::string& string, const char delimiter)
}


std::vector<std::string> getAtoms(const std::string& s) {
std::vector<std::string> getAtomsExpression(const std::string& s) {
class LocalTranslator : public Translator {
public:
explicit LocalTranslator(std::istream& inputStream) : Translator(inputStream) {

};

void startTranslation() override {
E();
const Token& token = _scanner.getNextToken();
if (token != LexemType::eof) {
syntaxError("Syntax analysis ended, but additional token appears");
}
}
};
auto iss = std::istringstream(s);
Translator translator(iss);
LocalTranslator translator(iss);
translator.startTranslation();
std::ostringstream oss;
translator.printAtoms(oss);
Expand All @@ -46,15 +60,15 @@ TEST(TranslatorTests, Grammar2) {
"(OR, 0[a], `2`, 1[!temp1])",
"(OR, 1[!temp1], 2[b], 3[!temp2])"
};
std::vector<std::string> actual = getAtoms("a || 2 || b");
std::vector<std::string> actual = getAtomsExpression("a || 2 || b");
ASSERT_EQ(expected, actual);
}

TEST(TranslatorTests, Grammar3_4) {
std::vector<std::string> expected = {
"(OR, 0[a], `2`, 1[!temp1])"
};
std::vector<std::string> actual = getAtoms("a || 2");
std::vector<std::string> actual = getAtomsExpression("a || 2");
ASSERT_EQ(expected, actual);
}

Expand All @@ -63,20 +77,20 @@ TEST(TranslatorTests, Grammar5) {
"(AND, 0[a], `2`, 1[!temp1])",
"(AND, 1[!temp1], 2[b], 3[!temp2])"
};
std::vector<std::string> actual = getAtoms("a && 2 && b");
std::vector<std::string> actual = getAtomsExpression("a && 2 && b");
ASSERT_EQ(expected, actual);
}

TEST(TranslatorTests, Grammar6_7) {
std::vector<std::string> expected = {
"(AND, 0[a], `2`, 1[!temp1])"
};
std::vector<std::string> actual = getAtoms("a && 2");
std::vector<std::string> actual = getAtomsExpression("a && 2");
ASSERT_EQ(expected, actual);
}

TEST(TranslatorTests, Grammar8) {
ASSERT_ANY_THROW(getAtoms("a == 2 == r"));
ASSERT_ANY_THROW(getAtomsExpression("a == 2 == r"));
}

TEST(TranslatorTests, Grammar9) {
Expand All @@ -86,7 +100,7 @@ TEST(TranslatorTests, Grammar9) {
"(MOV, `0`,, 1[!temp1])",
"(LBL,,, L0)"
};
std::vector<std::string> actual = getAtoms("a == 2");
std::vector<std::string> actual = getAtomsExpression("a == 2");
ASSERT_EQ(expected, actual);
}

Expand All @@ -97,7 +111,7 @@ TEST(TranslatorTests, Grammar10) {
"(MOV, `0`,, 1[!temp1])",
"(LBL,,, L0)"
};
std::vector<std::string> actual = getAtoms("a != 2");
std::vector<std::string> actual = getAtomsExpression("a != 2");
ASSERT_EQ(expected, actual);
}

Expand All @@ -108,7 +122,7 @@ TEST(TranslatorTests, Grammar11) {
"(MOV, `0`,, 1[!temp1])",
"(LBL,,, L0)"
};
std::vector<std::string> actual = getAtoms("a > 2");
std::vector<std::string> actual = getAtomsExpression("a > 2");
ASSERT_EQ(expected, actual);
}

Expand All @@ -119,7 +133,7 @@ TEST(TranslatorTests, Grammar12) {
"(MOV, `0`,, 1[!temp1])",
"(LBL,,, L0)"
};
std::vector<std::string> actual = getAtoms("a < 2");
std::vector<std::string> actual = getAtomsExpression("a < 2");
ASSERT_EQ(expected, actual);
}

Expand All @@ -130,7 +144,7 @@ TEST(TranslatorTests, Grammar13) {
"(MOV, `0`,, 1[!temp1])",
"(LBL,,, L0)"
};
std::vector<std::string> actual = getAtoms("a <= 2");
std::vector<std::string> actual = getAtomsExpression("a <= 2");
ASSERT_EQ(expected, actual);
}

Expand All @@ -140,7 +154,7 @@ TEST(TranslatorTests, Grammar14_15_16_17_18) {
"(ADD, `2`, `3`, 0[!temp1])",
"(SUB, 0[!temp1], `4`, 1[!temp2])"
};
std::vector<std::string> actual = getAtoms("2 + 3 - 4");
std::vector<std::string> actual = getAtomsExpression("2 + 3 - 4");
ASSERT_EQ(expected, actual);
}

Expand All @@ -149,7 +163,7 @@ TEST(TranslatorTests, Grammar19_21) {
"(MUL, `2`, `101`, 0[!temp1])",
"(MUL, 0[!temp1], `4`, 1[!temp2])"
};
std::vector<std::string> actual = getAtoms("2 * 'e' * 4");
std::vector<std::string> actual = getAtomsExpression("2 * 'e' * 4");
ASSERT_EQ(expected, actual);
}

Expand All @@ -158,15 +172,15 @@ TEST(TranslatorTests, Grammar20_23) {
std::vector<std::string> expected = {
"(MUL, `2`, `3`, 0[!temp1])"
};
std::vector<std::string> actual = getAtoms("2 * 3");
std::vector<std::string> actual = getAtomsExpression("2 * 3");
ASSERT_EQ(expected, actual);
}

TEST(TranslatorTests, Grammar22) {
std::vector<std::string> expected = {
"(NOT, `6`,, 0[!temp1])"
};
std::vector<std::string> actual = getAtoms("!6");
std::vector<std::string> actual = getAtomsExpression("!6");
ASSERT_EQ(expected, actual);
}

Expand All @@ -175,7 +189,7 @@ TEST(TranslatorTests, Grammar24) {
"(ADD, 1[b], 2[c], 3[!temp1])",
"(MUL, 0[a], 3[!temp1], 4[!temp2])"
};
std::vector<std::string> actual = getAtoms("a * (b + c)");
std::vector<std::string> actual = getAtomsExpression("a * (b + c)");
ASSERT_EQ(expected, actual);
}

Expand All @@ -189,7 +203,7 @@ TEST(TranslatorTests, Grammar26) {
std::vector<std::string> expected = {
"(NOT, `101`,, 0[!temp1])"
};
std::vector<std::string> actual = getAtoms("!'e'");
std::vector<std::string> actual = getAtomsExpression("!'e'");
ASSERT_EQ(expected, actual);
}

Expand All @@ -198,15 +212,15 @@ TEST(TranslatorTests, Grammar27) {
std::vector<std::string> expected = {
"(ADD, 0[a], `1`, 0[a])"
};
std::vector<std::string> actual = getAtoms("++a");
std::vector<std::string> actual = getAtomsExpression("++a");
ASSERT_EQ(expected, actual);
}

TEST(TranslatorTests, Grammar28) {
std::vector<std::string> expected = {
"(NOT, 0[a],, 1[!temp1])"
};
std::vector<std::string> actual = getAtoms("!a");
std::vector<std::string> actual = getAtomsExpression("!a");
ASSERT_EQ(expected, actual);
}

Expand All @@ -215,15 +229,15 @@ TEST(TranslatorTests, Grammar29) {
"(MOV, 0[a],, 1[!temp1])",
"(ADD, 0[a], `1`, 0[a])"
};
std::vector<std::string> actual = getAtoms("a++");
std::vector<std::string> actual = getAtomsExpression("a++");
ASSERT_EQ(expected, actual);
}

TEST(TranslatorTests, Grammar31) {
std::vector<std::string> expected = {
"(MUL, 0[a], 1[b], 2[!temp1])"
};
std::vector<std::string> actual = getAtoms("a * b");
std::vector<std::string> actual = getAtomsExpression("a * b");
ASSERT_EQ(expected, actual);
}

Expand All @@ -248,19 +262,19 @@ TEST(TranslatorTests, BigTest1) {
"(MOV, `0`,, 11[!temp8])",
"(LBL,,, L1)"
};
std::vector<std::string> actual = getAtoms("(a == b) * 3 > 1 - 'A' + c * 2 - d++");
std::vector<std::string> actual = getAtomsExpression("(a == b) * 3 > 1 - 'A' + c * 2 - d++");
ASSERT_EQ(expected, actual);
}

TEST(TranslatorTests, BigTest2) {
ASSERT_ANY_THROW(getAtoms("(a == b) * 3 > 1 - 'A' + c * - d++"));
ASSERT_ANY_THROW(getAtoms("(a == ) * 3 > 1 - 'A' + c * 2 - d++"));
ASSERT_ANY_THROW(getAtoms("(a == b) * 3 > "));
ASSERT_ANY_THROW(getAtoms("(a == b) * 3 > 1 - 'A' + c * 2 2 - d++"));
ASSERT_ANY_THROW(getAtoms("(a == b) * 3 > 1 - 'A' + c * 2 - ++"));
ASSERT_ANY_THROW(getAtoms("(a == b * 3 > 1 - 'A' + c * 2 - d++"));
ASSERT_ANY_THROW(getAtoms("a == b) * 3 > 1 - 'A' + c * 2 - d++"));
ASSERT_ANY_THROW(getAtoms("(a == b) * 3 > 1 - + c * 2 - d++"));
ASSERT_ANY_THROW(getAtomsExpression("(a == b) * 3 > 1 - 'A' + c * - d++"));
ASSERT_ANY_THROW(getAtomsExpression("(a == ) * 3 > 1 - 'A' + c * 2 - d++"));
ASSERT_ANY_THROW(getAtomsExpression("(a == b) * 3 > "));
ASSERT_ANY_THROW(getAtomsExpression("(a == b) * 3 > 1 - 'A' + c * 2 2 - d++"));
ASSERT_ANY_THROW(getAtomsExpression("(a == b) * 3 > 1 - 'A' + c * 2 - ++"));
ASSERT_ANY_THROW(getAtomsExpression("(a == b * 3 > 1 - 'A' + c * 2 - d++"));
ASSERT_ANY_THROW(getAtomsExpression("a == b) * 3 > 1 - 'A' + c * 2 - d++"));
ASSERT_ANY_THROW(getAtomsExpression("(a == b) * 3 > 1 - + c * 2 - d++"));
}

#pragma clang diagnostic pop

0 comments on commit 8c7ed01

Please sign in to comment.