I do tests for evaluating logical expressions. Sadly I enountered

segmentation fault :(.
functions
Justyna Ilczuk 2012-12-22 18:52:19 +01:00
parent 6721f24be8
commit 8ef1eb14c5
9 changed files with 91 additions and 23 deletions

View File

@ -20,6 +20,7 @@ public:
virtual void accept(Visitor * visitor){ visitor->visit(this); };
virtual SenchaObject evaluate() = 0;
virtual std::string debug() = 0;
virtual void execute() = 0;
virtual ~ASTNode();

View File

@ -13,6 +13,7 @@ class ASTStatement : public ASTNode {
public:
ASTStatement();
virtual void execute() = 0;
virtual SenchaObject evaluate() { return SenchaObject(); }
virtual ~ASTStatement();
};

View File

@ -36,6 +36,11 @@ void BasicStatement::execute()
child->execute() ;
}
SenchaObject BasicStatement::evaluate()
{
return children[children.size() - 1]->evaluate();
}
void BasicStatement::accept(Visitor * visitor)
{
visitor->visit(this);

View File

@ -17,6 +17,7 @@ public:
BasicStatement(ASTNode * parent);
virtual std::string debug();
void add_expression(ASTExpression * expr);
virtual SenchaObject evaluate();
virtual void execute();
virtual void accept(Visitor * vistitor);
virtual ~BasicStatement();

View File

@ -36,6 +36,11 @@ void ProgramNode::execute_last()
children[children.size() - 1]->execute();
}
SenchaObject ProgramNode::evaluate_last()
{
return static_cast<ASTStatement * >(children[children.size() - 1])->evaluate();
}
std::string ProgramNode::debug()
{
std::string debug_note = "Program started debugging\n";

View File

@ -19,6 +19,8 @@ public:
virtual ~ProgramNode();
virtual void execute();
virtual void execute_last();
virtual SenchaObject evaluate() { return evaluate_last(); }
SenchaObject evaluate_last();
virtual void accept(Visitor * visitor);
};

View File

@ -16,3 +16,61 @@ TestParser::~TestParser() {
// TODO Auto-generated destructor stub
}
std::string TestParser::test_parsing_and_evaluating_logical_expressions()
{
std::string test_report = "";
std::vector<InputOutputPair> logical_inputs = prepare_logical_input();
Lexer lexer;
Context context;
Parser parser(&context);
for(auto logical_case : logical_inputs)
{
auto tokens = lexer.parse_line(logical_case.first);
parser.add_tokens(tokens);
auto value = parser.program->evaluate();
parser.erase_all();
muu_assert("Logical value isn't correct", value.truthy == logical_case.second);
}
return test_report;
}
std::vector<TestParser::InputOutputPair> TestParser::prepare_logical_input()
{
std::vector<InputOutputPair> logical_inputs;
logical_inputs.push_back(InputOutputPair("false and false", false));
logical_inputs.push_back(InputOutputPair("false and true", false));
logical_inputs.push_back(InputOutputPair("true and false", false));
logical_inputs.push_back(InputOutputPair("true and true", true));
logical_inputs.push_back(InputOutputPair("false or false", false));
logical_inputs.push_back(InputOutputPair("false or true", true));
logical_inputs.push_back(InputOutputPair("true or false", true));
logical_inputs.push_back(InputOutputPair("true or true", true));
logical_inputs.push_back(InputOutputPair("(true or false) and (true or false)", true));
logical_inputs.push_back(InputOutputPair("7 + 2 > 55 or 4-5 == -1", true));
return logical_inputs;
}
std::string TestParser::all_tests()
{
std::string test_report = "";
mu_run_test(test_parsing_and_evaluating_logical_expressions);
return test_report;
}
/*TODO
* write test of evaluating logical expression
* possible input:
*
* some variable == "g" and other != "a"
*
* some easy expression like:
* if("aaa" != "bbb" and 2 < 5) {
>> print(1);
>> } else {
>> print(0);
>> }
*/

View File

@ -9,33 +9,26 @@
#define TESTPARSER_H_
#include "TestSuite.h"
#include "../Lexer.h"
#include "../Parser.h"
#include "../ASTInspector.h"
#include <utility>
#include <vector>
class TestParser: public TestSuite {
public:
TestParser();
virtual ~TestParser();
typedef std::pair<std::string, bool> InputOutputPair;
private:
std::string test_parsing_and_evaluating_logical_expressions();
std::vector<InputOutputPair> prepare_logical_input();
virtual std::string all_tests();
};
#endif /* TESTPARSER_H_ */
/*TODO
* write test of evaluating logical expression
* possible input:
* false and false
* false and true
* true and false
* true and true
* false or false
* false or true
* true or false
* true or true
* 7 + 2 > 55 or 4-5 == -1
* some variable == "g" and other != "a"
*
* some easy expression like:
* if("aaa" != "bbb" and 2 < 5) {
>> print(1);
>> } else {
>> print(0);
>> }
*/

View File

@ -11,6 +11,7 @@
#include <iostream>
#include "TestLexer.h"
#include "TestASTInspector.h"
#include "TestParser.h"
#include "../Lexer.h"
#include "../Parser.h"
@ -68,7 +69,8 @@ void run_test_suites()
TestASTInspector test_inspector;
test_inspector.run_tests();
TestParser test_parser;
test_parser.run_tests();
}