From: Efimov Alexander/AI Tools Lab/./Samsung Electronics Date: Wed, 12 Dec 2018 21:53:39 +0000 (+0300) Subject: [nnc] Add new dom to text tests for acl backend (#2641) X-Git-Tag: nncc_backup~1093 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=513b45fe8cd5957d6a73766eed292d64bbb53a47;p=platform%2Fcore%2Fml%2Fnnfw.git [nnc] Add new dom to text tests for acl backend (#2641) Tests added: - ArtifactId - ArtifactRef - ArtifactDeref - ArtifactFunctionCall - ArtifactUnaryExpr - ArtifactBinaryExpr Signed-off-by: Efimov Alexander --- diff --git a/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp b/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp index ca9925a..a65d0b4 100644 --- a/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp +++ b/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp @@ -27,6 +27,7 @@ ArtifactGeneratorCppDecl::ArtifactGeneratorCppDecl(ostream& out) : _out(out) { } void ArtifactGeneratorCppDecl::visit(const ArtifactLiteral* node) { + _out << node->getValue(); } void ArtifactGeneratorCppDecl::visit(const ArtifactId* node) { diff --git a/contrib/nnc/unittests/acl_backend/DOMToText.cpp b/contrib/nnc/unittests/acl_backend/DOMToText.cpp index 14f99b0..ee52b3a 100644 --- a/contrib/nnc/unittests/acl_backend/DOMToText.cpp +++ b/contrib/nnc/unittests/acl_backend/DOMToText.cpp @@ -21,6 +21,7 @@ */ #include +#include #include "ArtifactModel.h" #include "ArtifactGeneratorCppCode.h" @@ -31,42 +32,175 @@ using namespace std; using namespace nnc; +using AF = ArtifactFactory; + TEST(acl_backend_dom_to_text, ArtifactLiteral) { stringstream code_out; stringstream decl_out; ArtifactGeneratorCppCode code_gen(code_out); ArtifactGeneratorCppDecl decl_gen(decl_out); - const char* data = "hello_world"; - shared_ptr lit = ArtifactFactory::lit(data); + const char* lit_data = "hello_world"; + shared_ptr lit = AF::lit(lit_data); lit->accept(&code_gen); lit->accept(&decl_gen); - ASSERT_EQ(code_out.str(), data); - // TODO why not generate data for literal in decl generator? - //ASSERT_EQ(decl_out.str(), data); + ASSERT_EQ(code_out.str(), lit_data); + ASSERT_EQ(decl_out.str(), lit_data); } -TEST(acl_backend_dom_to_text, DISABLED_ArtifactId) { - // TODO +TEST(acl_backend_dom_to_text, ArtifactId) { + stringstream code_out; + stringstream decl_out; + ArtifactGeneratorCppCode code_gen(code_out); + ArtifactGeneratorCppDecl decl_gen(decl_out); + const char* id_data = "some_id"; + shared_ptr id = AF::id(id_data); + id->accept(&code_gen); + id->accept(&decl_gen); + ASSERT_EQ(code_out.str(), id_data); + ASSERT_EQ(decl_out.str(), id_data); } -TEST(acl_backend_dom_to_text, DISABLED_ArtifactRef) { - // TODO +TEST(acl_backend_dom_to_text, ArtifactRef) { + stringstream code_out; + stringstream decl_out; + ArtifactGeneratorCppCode code_gen(code_out); + ArtifactGeneratorCppDecl decl_gen(decl_out); + const char* id_data = "some_id"; + shared_ptr id = AF::id(id_data); + shared_ptr ref = AF::ref(id); + ref->accept(&code_gen); + ref->accept(&decl_gen); + string ref_data = string("&") + id_data; + ASSERT_EQ(code_out.str(), ref_data); + ASSERT_EQ(decl_out.str(), ref_data); } -TEST(acl_backend_dom_to_text, DISABLED_ArtifactDeref) { - // TODO +TEST(acl_backend_dom_to_text, ArtifactDeref) { + stringstream code_out; + stringstream decl_out; + ArtifactGeneratorCppCode code_gen(code_out); + ArtifactGeneratorCppDecl decl_gen(decl_out); + const char* id_data = "some_id"; + shared_ptr id = AF::id(id_data); + shared_ptr deref = AF::deref(id); + deref->accept(&code_gen); + deref->accept(&decl_gen); + string ref_data = string("*") + id_data; + ASSERT_EQ(code_out.str(), ref_data); + ASSERT_EQ(decl_out.str(), ref_data); } -TEST(acl_backend_dom_to_text, DISABLED_ArtifactFunctionCall) { - // TODO +static void checkCall(ArtifactCallType type, + const char* call_name, + const list>& args, + shared_ptr obj, + const char* ref_data) { + stringstream code_out; + ArtifactGeneratorCppCode code_gen(code_out); + shared_ptr call = AF::call(call_name, args, obj, type); + call->accept(&code_gen); + ASSERT_EQ(code_out.str(), ref_data); } -TEST(acl_backend_dom_to_text, DISABLED_ArtifactUnaryExpr) { - //TODO +TEST(acl_backend_dom_to_text, ArtifactFunctionCall) { + const char* lit_data = "123"; + const char* id_data = "some_id"; + shared_ptr id = AF::id(id_data); + shared_ptr lit = AF::lit(lit_data); + const list> args{id, lit}; + + + shared_ptr obj = AF::id("obj"); + + using TestCase = tuple, const char*>; + TestCase test_cases[] = { + TestCase{ArtifactCallType::scope, nullptr, "foo(some_id, 123)"}, + TestCase{ArtifactCallType::obj, obj, "obj.foo(some_id, 123)"}, + TestCase{ArtifactCallType::ref, obj, "obj->foo(some_id, 123)"}, + TestCase{ArtifactCallType::scope, obj, "obj::foo(some_id, 123)"} + }; + + for (const auto& test: test_cases) { + ArtifactCallType call_type = get<0>(test); + shared_ptr obj = get<1>(test); + const char* ref_output = get<2>(test); + checkCall(call_type, "foo", args, obj, ref_output); + } } -TEST(acl_backend_dom_to_text, DISABLED_ArtifactBinaryExpr) { - //TODO +static void checkUnaryExpression(ArtifactUnOp op, + shared_ptr var, + const char* ref_data) { + stringstream code_out; + ArtifactGeneratorCppCode code_gen(code_out); + + shared_ptr expr = AF::un(op, var); + expr->accept(&code_gen); + ASSERT_EQ(code_out.str(), ref_data); +} + +TEST(acl_backend_dom_to_text, ArtifactUnaryExpr) { + const char* var_name = "id"; + shared_ptr var = AF::id(var_name); + pair test_cases[] = { + {ArtifactUnOp::preIncr, "++id"}, + {ArtifactUnOp::preDecr, "--id"}, + {ArtifactUnOp::heapNew, "new id"}, + {ArtifactUnOp::heapFree, "delete id"}, + {ArtifactUnOp::postIncr, "id++"}, + {ArtifactUnOp::postDecr, "id--"} + }; + + for (auto test: test_cases) { + auto op_type = test.first; + auto ref_output = test.second; + checkUnaryExpression(op_type, var, ref_output); + } +} + +static void checkBinaryExpression(ArtifactBinOp op, + shared_ptr op1, + shared_ptr op2, + const char* ref_data) { + stringstream code_out; + ArtifactGeneratorCppCode code_gen(code_out); + + shared_ptr expr = AF::bin(op, op1, op2); + expr->accept(&code_gen); + ASSERT_EQ(code_out.str(), ref_data); +} + +TEST(acl_backend_dom_to_text, ArtifactBinaryExpr) { + stringstream code_out; + ArtifactGeneratorCppCode code_gen(code_out); + const char* op1_name = "a"; + const char* op2_name = "b"; + shared_ptr op1 = AF::id(op1_name); + shared_ptr op2 = AF::id(op2_name); + + pair test_cases[] ={ + {ArtifactBinOp::eq, "a == b"}, + {ArtifactBinOp::notEq, "a != b"}, + {ArtifactBinOp::less, "a < b"}, + {ArtifactBinOp::lessOrEq, "a <= b"}, + {ArtifactBinOp::great, "a > b"}, + {ArtifactBinOp::greatOrEq, "a >= b"}, + {ArtifactBinOp::assign, "a = b"}, + {ArtifactBinOp::plus, "a + b"}, + {ArtifactBinOp::minus, "a - b"}, + {ArtifactBinOp::mult, "a * b"}, + {ArtifactBinOp::div, "a / b"}, + {ArtifactBinOp::plusAssign, "a += b"}, + {ArtifactBinOp::minusAssign, "a -= b"}, + {ArtifactBinOp::multAssign, "a *= b"}, + {ArtifactBinOp::divAssign, "a /= b"} + }; + + for (auto test: test_cases) { + auto op_type = test.first; + auto ref_output = test.second; + checkBinaryExpression(op_type, op1, op2, ref_output); + } } TEST(acl_backend_dom_to_text, DISABLED_ArtifactIndex) {