*/
#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) {