📣 An issue occured with the embedded container registry on October 25 2021, between 10:30 and 12:10 (UTC+2). Any persisting issues should be reported to CC-IN2P3 Support. 🐛

Commit 361a42c3 authored by Pierre Aubert's avatar Pierre Aubert
Browse files

Merge branch 'add_test' into 'master'

Add tests to parse mathematical expression inside other generic expression

See merge request !8
parents b11f3de8 2170e624
Pipeline #136575 passed with stages
in 8 minutes and 31 seconds
......@@ -13,6 +13,7 @@ add_subdirectory(TEST_VAR_DECL)
add_subdirectory(TEST_VAR_DECL_STRING)
add_subdirectory(TEST_OPERATOR)
add_subdirectory(TEST_OPERATOR_PRIORITY)
add_subdirectory(TEST_OPERATOR_EXPR)
add_subdirectory(TEST_C_ENUM_INCLUDE)
......
# compileLibTestGenerator(test_math_operator_expr_parser "PTestOperatorParser" parser_operator.pdecparser)
# compileMainTestGenerator(test_math_operator_expr_parser "PTestOperatorParser" parser_operator.pdecparser main_test.cpp)
# Only when the test will be OK
# compileTestGenerator(test_math_operator_expr_parser "PTestOperatorParser" parser_operator.pdecparser main_test.cpp fileAdd.math)
compileBaseTestGenerator(test_math_operator_expr_parser "PTestOperatorParser" parser_operator.pdecparser main_base_test.cpp)
add_executable(test_math_operator_expr_parser_input main_test.cpp)
target_link_libraries(test_math_operator_expr_parser_input test_math_operator_expr_parser_parser phoenix_parser_stmt file_parser option_parser string_utils)
add_test(NAME Test_test_math_operator_expr_parser_input_var
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileA.math -r "a"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_input
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileAdd.math -r "(a+b)"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_input3
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileAdd3.math -r "((a+b)+c)"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputMult
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileMult.math -r "(a*b)"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputMult3
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileMult3.math -r "((a*b)*c)"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputMultAdd
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileMultAdd.math -r "((a*b)+c)"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputMultAdd_global_parenthesis
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileMultAdd_globalParenthesis.math -r "((a*b)+c)"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputMultParenthesisAdd
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileMultParenthesisAdd.math -r "(a*(b+c))"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputAddMult
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileAddMult.math -r "(a+(b*c))"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputAddMult_parenthesis
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileAddMultParenthesis.math -r "((a+b)*c)"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputMultAddMult_parenthesis
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileMultAddMultParenthesis.math -r "((a*(b+c))*d)"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME Test_test_math_operator_expr_parser_inputAddMultAdd_parenthesis
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_math_operator_expr_parser_input -i ${CMAKE_CURRENT_SOURCE_DIR}/fileAddMultAddParenthesis.math -r "((a+b)*(c+d))"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
#include "phoenix_check.h"
// To get the generated parser
#include "PTestOperatorParserFull.h"
///Test the conversion of and add expression into a string
/** @return true on success, false otherwise
*/
bool testExprAddToString(){
PExpr varA = PExpr_default();
PExpr_setType(varA, PExprType::VAR);
PExpr_setVarName(varA, "a");
PExpr varB = PExpr_default();
PExpr_setType(varB, PExprType::VAR);
PExpr_setVarName(varB, "b");
PExpr exprAdd = PExpr_default();
PExpr_setType(exprAdd, PExprType::ADD);
PExpr_getVecOperand(exprAdd).push_back(varA);
PExpr_getVecOperand(exprAdd).push_back(varB);
std::string exprStr = gExprVarName(exprAdd);
std::cout << "testExprAddToString : exprAdd = '"<<exprStr<<"'" << std::endl;
// std::cout << "testExprAddToString : exprAdd ok example :"<< std::endl;
// exprAdd.print();
return exprStr == "(a+b)";
}
///Test the conversion of and add expression into a string
/** @return true on success, false otherwise
*/
bool testExprAdd2ToString(){
PExpr varA = PExpr_default();
PExpr_setType(varA, PExprType::VAR);
PExpr_setVarName(varA, "a");
PExpr varB = PExpr_default();
PExpr_setType(varB, PExprType::VAR);
PExpr_setVarName(varB, "b");
PExpr exprAddAB = PExpr_default();
PExpr_setType(exprAddAB, PExprType::ADD);
PExpr_getVecOperand(exprAddAB).push_back(varA);
PExpr_getVecOperand(exprAddAB).push_back(varB);
PExpr varC = PExpr_default();
PExpr_setType(varC, PExprType::VAR);
PExpr_setVarName(varC, "c");
PExpr exprAdd = PExpr_default();
PExpr_setType(exprAdd, PExprType::ADD);
PExpr_getVecOperand(exprAdd).push_back(exprAddAB);
PExpr_getVecOperand(exprAdd).push_back(varC);
std::string exprStr = gExprVarName(exprAdd);
std::cout << "testExprAdd2ToString : exprAdd = '"<<exprStr<<"'" << std::endl;
return exprStr == "((a+b)+c)";
}
///Test the conversion of and add expression into a string
/** @return true on success, false otherwise
*/
bool testExprAdd3ToString(){
PExpr varA = PExpr_default();
PExpr_setType(varA, PExprType::VAR);
PExpr_setVarName(varA, "a");
PExpr varB = PExpr_default();
PExpr_setType(varB, PExprType::VAR);
PExpr_setVarName(varB, "b");
PExpr exprAddAB = PExpr_default();
PExpr_setType(exprAddAB, PExprType::ADD);
PExpr_getVecOperand(exprAddAB).push_back(varA);
PExpr_getVecOperand(exprAddAB).push_back(varB);
PExpr varC = PExpr_default();
PExpr_setType(varC, PExprType::VAR);
PExpr_setVarName(varC, "c");
PExpr varD = PExpr_default();
PExpr_setType(varD, PExprType::VAR);
PExpr_setVarName(varD, "d");
PExpr exprAddCD = PExpr_default();
PExpr_setType(exprAddCD, PExprType::ADD);
PExpr_getVecOperand(exprAddCD).push_back(varC);
PExpr_getVecOperand(exprAddCD).push_back(varD);
PExpr exprAdd = PExpr_default();
PExpr_setType(exprAdd, PExprType::ADD);
PExpr_getVecOperand(exprAdd).push_back(exprAddAB);
PExpr_getVecOperand(exprAdd).push_back(exprAddCD);
std::string exprStr = gExprVarName(exprAdd);
std::cout << "testExprAdd3ToString : exprAdd = '"<<exprStr<<"'" << std::endl;
return exprStr == "((a+b)+(c+d))";
}
int main(int argc, char** argv){
bool b(testExprAddToString());
b &= testExprAdd2ToString();
b &= testExprAdd3ToString();
phoenix_functionOk("final", b);
return b - 1;
}
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
#include "phoenix_check.h"
#include "OptionParser.h"
// To get the generated parser
#include "PTestOperatorParserFull.h"
///Create the OptionParser of this program
/** @return OptionParser of this program
*/
OptionParser createOptionParser(){
OptionParser parser(true, __PROGRAM_VERSION__);
parser.setExampleLongOption("use_parser --input=fileName.shadok");
parser.setExampleShortOption("use_parser -i fileName.shadok");
parser.addOption("input", "i", OptionType::FILENAME, true, "input code file name");
parser.addOption("reference", "r", OptionType::STRING, true, "reference value to be parsed");
return parser;
}
///Parse a source file
/** @param fileName : name of the source file
* @param reference : expeced result of the parsed file
* @return true on success, false otherwise
*/
bool testParseFileName(const std::string & fileName, const std::string & reference){
bool b(true);
//We create the parser
PTestOperatorParser parser;
b &= parser.load(fileName);
//We get the parsed internal representation
Source & source = parser.getSourceS();
PStmt & vecExpr = Source_getVecExpr(source);
b &= phoenix_check("Check source size", vecExpr.size(), 1lu);
PExpr & expr = vecExpr.front();
std::cout << "PExpr full stmt :" << std::endl;
expr.print();
std::string exprStr = gExprVarName(expr);
b &= phoenix_check("Check backend", exprStr, reference);
return b;
}
int main(int argc, char** argv){
OptionParser optionParser = createOptionParser();
optionParser.parseArgument(argc, argv);
const OptionMode & defaultMode = optionParser.getDefaultMode();
std::string fileName(""), reference("");
defaultMode.getValue(fileName, "input");
defaultMode.getValue(reference, "reference");
bool b(true);
b &= testParseFileName(fileName, reference);
phoenix_functionOk("final", b);
return b - 1;
}
///Type of the expression
Enum PExprType{
VAR
ADD
MULT
};
///Describes a simple mathematic expression
Class PExpr{
///Type of the expression
PExprType::PExprType type;
///Name of the variable
String varName[csVariableName];
///Vector of operand
Vector(PExpr) vecOperand;
}
Graph gExprVarName(PExpr e){
e.varName e.type=PExprType::VAR
}
Operator("+", PExpr, type=PExprType::ADD, vecOperand, gExprVarName, gExprVarName)
Operator("*", PExpr, type=PExprType::MULT, vecOperand, gExprVarName, gExprVarName)
///Main class
Class Source{
///Vector of PExpr
Vector(PExpr) vecExpr;
}
///Parses a complete source of PExpr
Graph gSource(Source s){
REPEAT{s.vecExpr.pushBack(gExprVarName()) ";"}
}
......@@ -105,6 +105,8 @@ std::string graph_cpp_graphTokenOperator(const std::string & parentClassName, co
body += "\t"+varType+" lastVar = "+defaultConstructor+";\n";
body += "\tPStmt vecTmp;\t//Vector of temporary "+varType+" which are parsed\n";
body += "\tvecTmp.push_back("+defaultConstructor+");\t//We add a default\n";
body += "\t"+functionSetType+"(vecTmp.back(), "+exprNone+");\t//Do not forget to initialise the first tmp operand as NONE\n";
body += "\tstd::vector<"+exprType+"> vecLastOpType;\t//Vector to remember all operator type\n";
body += "\tvecLastOpType.push_back("+exprNone+");\t//We add a default\n";
body += "\tsize_t nbParenthesis(0lu);\t//Reminder of the number of parenthesis which have to be closed\n";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment