From 601be444a7beb968b4e35f758c4945cf378ee23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Av=C3=A9?= Date: Tue, 3 Jan 2017 15:58:55 +0100 Subject: [PATCH] Fix tests --- include/Parsodus/lrtables/generator.h | 4 +- tests/CMakeLists.txt | 3 +- tests/lr0_shift-red.cpp | 147 +++++++++++++------------- tests/test.cpp | 7 -- 4 files changed, 77 insertions(+), 84 deletions(-) delete mode 100644 tests/test.cpp diff --git a/include/Parsodus/lrtables/generator.h b/include/Parsodus/lrtables/generator.h index 0b38e6e..52aecc7 100644 --- a/include/Parsodus/lrtables/generator.h +++ b/include/Parsodus/lrtables/generator.h @@ -131,11 +131,11 @@ LRTable Generator::generate() { } else if (table.act[curIdx].count(term)) { if (table.act[curIdx][term].first == Action::SHIFT) { //Shift-Reduce conflict, rapport and resolve it (TODO) - throw "shift-reduce"; + throw std::runtime_error("shift-reduce"); } else if (table.act[curIdx][term].first == Action::REDUCE && table.act[curIdx][term].second != rule_applied) { //Reduce-Reduce conflict, rapport it (TODO) - throw 1; + throw std::runtime_error("reduce-reduce"); } else { //Reduce using the same rule, no problem, NO-OP } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 82cff2a..b62fe1e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,8 +2,9 @@ add_executable(Parsodus-test lr0_shift-red.cpp ) target_link_libraries(Parsodus-test - #Parsodus-tables # Parsodus-backends + Parsodus-tables + Parsodus-util pds mstch::mstch ${GTEST_BOTH_LIBRARIES} diff --git a/tests/lr0_shift-red.cpp b/tests/lr0_shift-red.cpp index 2d2fb81..401fdae 100644 --- a/tests/lr0_shift-red.cpp +++ b/tests/lr0_shift-red.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "Parsodus/grammar.h" #include "Parsodus/lrtables/generator.h" #include "Parsodus/lrtables/SLR1Itemset.h" @@ -18,105 +19,104 @@ TEST(lr0, test0) { grammar.variables = {"S'","S","A","E"}; grammar.terminals = {"$",";", "id", ":=", "+"}; - pds::Rule rule; - rule.head = "S'"; - rule.tail = {"S","$"}; - grammar.rules.push_back(rule); + std::shared_ptr rule(new pds::Rule()); + rule->head = "S'"; + rule->tail = {"S","$"}; + grammar.rules.push_back(rule); - rule.head = "S"; - rule.tail = {"S",";","A"}; - grammar.rules.push_back(rule); + rule->head = "S"; + rule->tail = {"S",";","A"}; + grammar.rules.push_back(rule); - rule.head = "S"; - rule.tail = {"A"}; - grammar.rules.push_back(rule); + rule->head = "S"; + rule->tail = {"A"}; + grammar.rules.push_back(rule); - rule.head = "A"; - rule.tail = {"E"}; - grammar.rules.push_back(rule); + rule->head = "A"; + rule->tail = {"E"}; + grammar.rules.push_back(rule); - rule.head = "A"; - rule.tail = {"id",":=","E"}; - grammar.rules.push_back(rule); + rule->head = "A"; + rule->tail = {"id",":=","E"}; + grammar.rules.push_back(rule); - rule.head = "E"; - rule.tail = {"E","+","id"}; - grammar.rules.push_back(rule); + rule->head = "E"; + rule->tail = {"E","+","id"}; + grammar.rules.push_back(rule); - rule.head = "E"; - rule.tail = {"id"}; - grammar.rules.push_back(rule); + rule->head = "E"; + rule->tail = {"id"}; + grammar.rules.push_back(rule); pds::lr::Generator g(grammar); auto tbl = g.generate(); // LRTable - EXPECT_THROW(throw "shift-reduce", std::exception); - - /* + // EXPECT_THROW(throw "shift-reduce", std::exception); + /* std::vector >> act { - { {"id", make_pair(pds::lr::Action::SHIFT,4) } }, - { {";", make_pair(pds::lr::Action::SHIFT,5) }, - {"$", make_pair(pds::lr::Action::SHIFT, ACCEPT_STATE)} + { {"id", std::make_pair(pds::lr::Action::SHIFT,4) } }, + { {";", std::make_pair(pds::lr::Action::SHIFT,5) }, + {"$", std::make_pair(pds::lr::Action::SHIFT, ACCEPT_STATE)} }, - { {"id", make_pair(pds::lr::Action::REDUCE,3) }, - {";", make_pair(pds::lr::Action::REDUCE,3) }, - {"+", make_pair(pds::lr::Action::REDUCE,3) }, - {":=", make_pair(pds::lr::Action::REDUCE,3) }, - {"$", make_pair(pds::lr::Action::REDUCE, 3)} + { {"id", std::make_pair(pds::lr::Action::REDUCE,3) }, + {";", std::make_pair(pds::lr::Action::REDUCE,3) }, + {"+", std::make_pair(pds::lr::Action::REDUCE,3) }, + {":=", std::make_pair(pds::lr::Action::REDUCE,3) }, + {"$", std::make_pair(pds::lr::Action::REDUCE, 3)} }, - { {"id", make_pair(pds::lr::Action::REDUCE,4) }, - {";", make_pair(pds::lr::Action::REDUCE,4) }, - {"+", make_pair(pds::lr::Action::ERROR, ERROR_STATE) }, - {":=", make_pair(pds::lr::Action::REDUCE,4) }, - {"$", make_pair(pds::lr::Action::REDUCE,4)} + { {"id", std::make_pair(pds::lr::Action::REDUCE,4) }, + {";", std::make_pair(pds::lr::Action::REDUCE,4) }, + {"+", std::make_pair(pds::lr::Action::ERROR, ERROR_STATE) }, + {":=", std::make_pair(pds::lr::Action::REDUCE,4) }, + {"$", std::make_pair(pds::lr::Action::REDUCE,4)} }, - { {"id", make_pair(pds::lr::Action::REDUCE,7) }, - {";", make_pair(pds::lr::Action::REDUCE,7) }, - {"+", make_pair(pds::lr::Action::REDUCE,7) }, - {":=", make_pair(pds::lr::Action::ERROR, ERROR_STATE) }, - {"$", make_pair(pds::lr::Action::REDUCE, 7)} + { {"id", std::make_pair(pds::lr::Action::REDUCE,7) }, + {";", std::make_pair(pds::lr::Action::REDUCE,7) }, + {"+", std::make_pair(pds::lr::Action::REDUCE,7) }, + {":=", std::make_pair(pds::lr::Action::ERROR, ERROR_STATE) }, + {"$", std::make_pair(pds::lr::Action::REDUCE, 7)} }, - { {"id", make_pair(pds::lr::Action::SHIFT,4) } }, - { {"id", make_pair(pds::lr::Action::SHIFT,9) } }, - { {"id", make_pair(pds::lr::Action::SHIFT, 11) } }, - { {"id", make_pair(pds::lr::Action::REDUCE,2) }, - {";", make_pair(pds::lr::Action::REDUCE,2) }, - {"+", make_pair(pds::lr::Action::REDUCE,2) }, - {":=", make_pair(pds::lr::Action::REDUCE,2) }, - {"$", make_pair(pds::lr::Action::REDUCE, 2)} + { {"id", std::make_pair(pds::lr::Action::SHIFT,4) } }, + { {"id", std::make_pair(pds::lr::Action::SHIFT,9) } }, + { {"id", std::make_pair(pds::lr::Action::SHIFT, 11) } }, + { {"id", std::make_pair(pds::lr::Action::REDUCE,2) }, + {";", std::make_pair(pds::lr::Action::REDUCE,2) }, + {"+", std::make_pair(pds::lr::Action::REDUCE,2) }, + {":=", std::make_pair(pds::lr::Action::REDUCE,2) }, + {"$", std::make_pair(pds::lr::Action::REDUCE, 2)} }, - { {"id", make_pair(pds::lr::Action::REDUCE,6) }, - {";", make_pair(pds::lr::Action::REDUCE,6) }, - {"+", make_pair(pds::lr::Action::REDUCE,6) }, - {":=", make_pair(pds::lr::Action::REDUCE,6) }, - {"$", make_pair(pds::lr::Action::REDUCE, 6)} + { {"id", std::make_pair(pds::lr::Action::REDUCE,6) }, + {";", std::make_pair(pds::lr::Action::REDUCE,6) }, + {"+", std::make_pair(pds::lr::Action::REDUCE,6) }, + {":=", std::make_pair(pds::lr::Action::REDUCE,6) }, + {"$", std::make_pair(pds::lr::Action::REDUCE, 6)} }, - { {"id", make_pair(pds::lr::Action::REDUCE,5) }, - {";", make_pair(pds::lr::Action::REDUCE,5) }, - {"+", make_pair(pds::lr::Action::ERROR, ERROR_STATE) }, - {":=", make_pair(pds::lr::Action::REDUCE,5) }, - {"$", make_pair(pds::lr::Action::REDUCE,5)} + { {"id", std::make_pair(pds::lr::Action::REDUCE,5) }, + {";", std::make_pair(pds::lr::Action::REDUCE,5) }, + {"+", std::make_pair(pds::lr::Action::ERROR, ERROR_STATE) }, + {":=", std::make_pair(pds::lr::Action::REDUCE,5) }, + {"$", std::make_pair(pds::lr::Action::REDUCE,5)} }, - { {"id", make_pair(pds::lr::Action::REDUCE,7) }, - {";", make_pair(pds::lr::Action::REDUCE,7) }, - {"+", make_pair(pds::lr::Action::REDUCE, 7) }, - {":=", make_pair(pds::lr::Action::REDUCE,7) }, - {"$", make_pair(pds::lr::Action::REDUCE,7)} + { {"id", std::make_pair(pds::lr::Action::REDUCE,7) }, + {";", std::make_pair(pds::lr::Action::REDUCE,7) }, + {"+", std::make_pair(pds::lr::Action::REDUCE, 7) }, + {":=", std::make_pair(pds::lr::Action::REDUCE,7) }, + {"$", std::make_pair(pds::lr::Action::REDUCE,7)} }, }; std::vector> goto_ = { - { {"S",1}, {"A",2}, {"E",3} }, /*state_num:1*/ -// { {"A",8}, {"E",3} }, /*state_num:5*/ -// { {"E",10} }, /*state_num:7*/ -// }; + { {"S",1}, {"A",2}, {"E",3} }, // state_num:1 + { {"A",8}, {"E",3} }, // state_num:5 + { {"E",10} }, // state_num:7 + }; // Action: enum {ERROR,SHIFT,REDUCE,ACCEPT} - /* - ASSERT_EQ(goto_.size(),tbl.goto_.size()); + + ASSERT_EQ(goto_.size(),tbl.goto_.size()); ASSERT_EQ(act.size(),tbl.act.size()); for(std::size_t i = 0; i < goto_.size(); i++) { @@ -130,9 +130,8 @@ TEST(lr0, test0) { EXPECT_EQ(act[i].second.first,tbl.act[i].second.first); EXPECT_EQ(act[i].second.second,tbl.act[i].second.second); } - */ - + */ diff --git a/tests/test.cpp b/tests/test.cpp deleted file mode 100644 index a3cee96..0000000 --- a/tests/test.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "gtest/gtest.h" - -TEST(parsodus, temporary) { - std::string parsodus = "awesome"; - ASSERT_EQ(parsodus, "awesome"); -}