GCC Code Coverage Report


Directory: ./
File: openvdb_ax/openvdb_ax/test/frontend/TestTernaryOperatorNode.cc
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 17 23 73.9%
Functions: 4 4 100.0%
Branches: 76 178 42.7%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #include <openvdb_ax/ast/AST.h>
5 #include <openvdb_ax/ast/Scanners.h>
6 #include <openvdb_ax/ast/PrintTree.h>
7 #include <openvdb_ax/Exceptions.h>
8
9 #include "../util.h"
10
11 #include <cppunit/extensions/HelperMacros.h>
12
13 #include <string>
14
15 using namespace openvdb::ax::ast;
16 using namespace openvdb::ax::ast::tokens;
17
18 namespace {
19
20 static const unittest_util::CodeTests tests =
21 {
22 { "true ? 1 : 0;", Node::Ptr(new TernaryOperator(new Value<bool>(true), new Value<int32_t>(1), new Value<int32_t>(0)))},
23 { "true ? a : 1.5f;", Node::Ptr(new TernaryOperator(new Value<bool>(true), new Local("a"), new Value<float>(1.5f)))},
24 { "false ? true : false;", Node::Ptr(new TernaryOperator(new Value<bool>(false), new Value<bool>(true), new Value<bool>(false)))},
25 { "a == b ? 1 : 0;", Node::Ptr(new TernaryOperator(
26 new BinaryOperator(
27 new Local("a"),
28 new Local("b"),
29 OperatorToken::EQUALSEQUALS),
30 new Value<int32_t>(1),
31 new Value<int32_t>(0)))},
32 { "a++ ? 1 : 0;", Node::Ptr(new TernaryOperator(
33 new Crement(new Local("a"), Crement::Operation::Increment, true),
34 new Value<int32_t>(1),
35 new Value<int32_t>(0)))},
36 { "@a ? 1 : 0;", Node::Ptr(new TernaryOperator(new Attribute("a", CoreType::FLOAT, true), new Value<int32_t>(1), new Value<int32_t>(0)))},
37 { "func() ? 1 : 0;", Node::Ptr(new TernaryOperator(new FunctionCall("func"), new Value<int32_t>(1), new Value<int32_t>(0)))},
38 { "(true) ? 1 : 0;", Node::Ptr(new TernaryOperator(new Value<bool>(true), new Value<int32_t>(1), new Value<int32_t>(0)))},
39 { "true ? 3 : 2 ? 1 : 0;", Node::Ptr(new TernaryOperator(
40 new Value<bool>(true),
41 new Value<int32_t>(3),
42 new TernaryOperator(new Value<int32_t>(2), new Value<int32_t>(1), new Value<int32_t>(0))))},
43 { "(true ? 3 : 2) ? 1 : 0;", Node::Ptr(new TernaryOperator(
44 new TernaryOperator(new Value<bool>(true), new Value<int32_t>(3), new Value<int32_t>(2)),
45 new Value<int32_t>(1),
46 new Value<int32_t>(0)))},
47 { "true ? \"foo\" : \"bar\";", Node::Ptr(new TernaryOperator(new Value<bool>(true), new Value<std::string>("foo"), new Value<std::string>("bar")))},
48 { "true ? voidfunc1() : voidfunc2();", Node::Ptr(new TernaryOperator(new Value<bool>(true), new FunctionCall("voidfunc1"), new FunctionCall("voidfunc2")))},
49 { "true ? {1,1,1} : {0,0,0};", Node::Ptr(new TernaryOperator(
50 new Value<bool>(true),
51 new ArrayPack({
52 new Value<int32_t>(1),
53 new Value<int32_t>(1),
54 new Value<int32_t>(1)
55 })
56 ,
57 new ArrayPack({
58 new Value<int32_t>(0),
59 new Value<int32_t>(0),
60 new Value<int32_t>(0)
61 })
62 ))},
63 { "true ? false ? 3 : 2 : 1;" , Node::Ptr(new TernaryOperator(
64 new Value<bool>(true),
65 new TernaryOperator(
66 new Value<bool>(false),
67 new Value<int32_t>(3),
68 new Value<int32_t>(2)),
69 new Value<int32_t>(1)))},
70 { "true ? false ? 3 : 2 : (true ? 4 : 5);" , Node::Ptr(new TernaryOperator(
71 new Value<bool>(true),
72 new TernaryOperator(
73 new Value<bool>(false),
74 new Value<int32_t>(3),
75 new Value<int32_t>(2)),
76 new TernaryOperator(
77 new Value<bool>(true),
78 new Value<int32_t>(4),
79 new Value<int32_t>(5))))},
80 { "true ? : 0;", Node::Ptr(new TernaryOperator(new Value<bool>(true), nullptr, new Value<int32_t>(0)))},
81 };
82
83 }
84
85
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 class TestTernaryOperatorNode : public CppUnit::TestCase
86 {
87 public:
88
89
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
4 CPPUNIT_TEST_SUITE(TestTernaryOperatorNode);
90
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
6 CPPUNIT_TEST(testSyntax);
91
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
6 CPPUNIT_TEST(testASTNode);
92
4/8
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
4 CPPUNIT_TEST_SUITE_END();
93
94
20/40
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 16 times.
✓ Branch 8 taken 1 times.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 16 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 16 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 16 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 16 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 16 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 16 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 16 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 16 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 16 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 16 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 16 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 16 times.
✗ Branch 47 not taken.
✓ Branch 48 taken 16 times.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✓ Branch 51 taken 16 times.
✓ Branch 53 taken 16 times.
✗ Branch 54 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
51 void testSyntax() { TEST_SYNTAX_PASSES(tests); }
95 void testASTNode();
96 };
97
98 CPPUNIT_TEST_SUITE_REGISTRATION(TestTernaryOperatorNode);
99
100 1 void TestTernaryOperatorNode::testASTNode()
101 {
102
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 for (const auto& test : tests) {
103
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 const std::string& code = test.first;
104 const Node* expected = test.second.get();
105
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 const Tree::ConstPtr tree = parse(code.c_str());
106
11/22
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 16 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 16 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 16 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 16 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 16 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 16 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 16 times.
✗ Branch 29 not taken.
✓ Branch 41 taken 16 times.
✗ Branch 42 not taken.
48 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("No AST returned", code), static_cast<bool>(tree));
107
108 // get the first statement
109 const Node* result = tree->child(0)->child(0);
110
6/12
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 16 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 16 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 16 times.
✗ Branch 17 not taken.
16 CPPUNIT_ASSERT(result);
111
12/24
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 16 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 16 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 16 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 16 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 16 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 16 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 16 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 16 times.
✗ Branch 32 not taken.
✓ Branch 45 taken 16 times.
✗ Branch 46 not taken.
48 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Invalid AST node", code),
112 Node::TernaryOperatorNode == result->nodetype());
113
114 std::vector<const Node*> resultList, expectedList;
115
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 linearize(*result, resultList);
116
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 linearize(*expected, expectedList);
117
118
2/4
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
16 if (!unittest_util::compareLinearTrees(expectedList, resultList)) {
119 std::ostringstream os;
120 os << "\nExpected:\n";
121 openvdb::ax::ast::print(*expected, true, os);
122 os << "Result:\n";
123 openvdb::ax::ast::print(*result, true, os);
124 CPPUNIT_FAIL(ERROR_MSG("Mismatching Trees for Ternary Operator code", code) + os.str());
125 }
126 }
127 1 }
128
129