[nnc] Add new dom to text tests for acl backend (#2641)
authorEfimov Alexander/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Wed, 12 Dec 2018 21:53:39 +0000 (00:53 +0300)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 12 Dec 2018 21:53:39 +0000 (00:53 +0300)
Tests added:
- ArtifactId
- ArtifactRef
- ArtifactDeref
- ArtifactFunctionCall
- ArtifactUnaryExpr
- ArtifactBinaryExpr

Signed-off-by: Efimov Alexander <a.efimov@samsung.com>
contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp
contrib/nnc/unittests/acl_backend/DOMToText.cpp

index ca9925a..a65d0b4 100644 (file)
@@ -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) {
index 14f99b0..ee52b3a 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <sstream>
+#include <tuple>
 
 #include "ArtifactModel.h"
 #include "ArtifactGeneratorCppCode.h"
 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<ArtifactLiteral> lit = ArtifactFactory::lit(data);
+  const char* lit_data = "hello_world";
+  shared_ptr<ArtifactLiteral> 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<ArtifactId> 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<ArtifactId> id = AF::id(id_data);
+  shared_ptr<ArtifactRef> 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<ArtifactId> id = AF::id(id_data);
+  shared_ptr<ArtifactDeref> 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<shared_ptr<ArtifactExpr>>& args,
+                      shared_ptr<ArtifactExpr> obj,
+                      const char* ref_data) {
+  stringstream code_out;
+  ArtifactGeneratorCppCode code_gen(code_out);
+  shared_ptr<ArtifactFunctionCall> 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<ArtifactExpr> id = AF::id(id_data);
+  shared_ptr<ArtifactExpr> lit = AF::lit(lit_data);
+  const list<shared_ptr<ArtifactExpr>> args{id, lit};
+
+
+  shared_ptr<ArtifactId> obj = AF::id("obj");
+
+  using TestCase = tuple<ArtifactCallType, shared_ptr<ArtifactExpr>, 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<ArtifactExpr> 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<ArtifactExpr> var,
+                                 const char* ref_data) {
+  stringstream code_out;
+  ArtifactGeneratorCppCode code_gen(code_out);
+
+  shared_ptr<ArtifactUnaryExpr> 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<ArtifactId> var = AF::id(var_name);
+  pair<ArtifactUnOp, const char*> 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<ArtifactExpr> op1,
+                                  shared_ptr<ArtifactExpr> op2,
+                                  const char* ref_data) {
+  stringstream code_out;
+  ArtifactGeneratorCppCode code_gen(code_out);
+
+  shared_ptr<ArtifactBinaryExpr> 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<ArtifactId> op1 = AF::id(op1_name);
+  shared_ptr<ArtifactId> op2 = AF::id(op2_name);
+
+  pair<ArtifactBinOp, const char*> 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) {