CGRA-ME
createOpGraphFromConfig_tests.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * The software programs comprising "CGRA-ME" and the documentation provided
3  * with them are copyright by its authors and the University of Toronto. Only
4  * non-commercial, not-for-profit use of this software is permitted without ex-
5  * plicit permission. This software is provided "as is" with no warranties or
6  * guarantees of support. See the LICENCE for more details. You should have re-
7  * ceived a copy of the full licence along with this software. If not, see
8  * <http://cgra-me.ece.utoronto.ca/license/>.
9  ******************************************************************************/
10 
11 #include <CGRA/OpGraphProcedures.h>
13 
15 
16 #include <catch2/catch.hpp>
17 
18 namespace {
19 
20 struct CreateOpGraphFromConfigTest {
21  std::string input_description;
22  bool expect_to_parse;
23  ConfigGraph input;
24  OpGraph expected;
25 };
26 
27 template<typename Test>
28 void run_createOpGraphFromConfig_test(const Test& test);
29 
30 } // end anon namespace
31 
32 TEST_CASE("createOpGraphFromConfig Tests") {
33  const CreateOpGraphFromConfigTest tests[] = { // OpGraph is move only...
34  { "one op", true,
35  []{ // (immediately invoked)
36  ConfigGraph cg;
37  cg.insert("a", {{"opcode", "add"}});
38  return cg;
39  }(),
40  []{ // (immediately invoked)
41  OpGraph og;
42  og.insert({"a", 32, OpCode::ADD});
43  return finalFixup(std::move(og));
44  }(),
45  },
46  { "a; b", true,
47  []{ // (immediately invoked)
48  ConfigGraph cg;
49  cg.insert("a", {{"opcode","add"}});
50  cg.insert("b", {{"opcode","add"}});
51  return cg;
52  }(),
53  []{ // (immediately invoked)
54  OpGraph og;
55  og.insert({"a", 32, OpCode::ADD});
56  og.insert({"b", 32, OpCode::ADD});
57  return finalFixup(std::move(og));
58  }(),
59  },
60  { "a->b", true,
61  []{ // (immediately invoked)
62  ConfigGraph cg;
63  cg.link(
64  cg.insert("a", {{"opcode","add"}}).first,
65  cg.insert("b", {{"opcode","add"}}).first,
66  {{"operand",Operands::BINARY_ANY}}
67  );
68  return cg;
69  }(),
70  [] { // (immediately invoked)
71  OpGraph og;
72  const auto a = og.insert({"a", 32, OpCode::ADD});
73  const auto b = og.insert(a, {"b", 32, OpCode::ADD}, Operands::BINARY_ANY).newOp;
74  (void)b;
75  return finalFixup(std::move(og));
76  }(),
77  },
78  { "const op", true,
79  [] { // (immediately invoked)
80  ConfigGraph cg;
81  cg.insert("a", {{"opcode", "const"}, {"constVal", "4"}});
82  return cg;
83  }(),
84  [] { // (immediately invoked)
85  OpGraph og;
86  og.insert({"a", 32, OpCode::CONST, 4});
87  return finalFixup(std::move(og));
88  }(),
89  },
90  };
91 
92  using std::begin; using std::end;
93  const auto& test_it = GENERATE_REF(range((std::ptrdiff_t)0, std::distance(begin(tests), end(tests))));
94  run_createOpGraphFromConfig_test(tests[test_it]);
95 }
96 
97 namespace {
98 
99 template<typename T>
100 OpGraph do_createOpGraphFromConfig(const T& input) {
101  try {
102  return createOpGraphFromConfig(input);
103  } catch (const std::exception& e) {
104  std::ostringstream s;
105  s << "Exception when creating from configuration: " << e.what()
106  << "\nInput was:\n" << input;
107  std::throw_with_nested(std::logic_error(s.str()));
108  }
109 }
110 
111 template<typename Test>
112 void run_createOpGraphFromConfig_test(const Test& test) {
113  GIVEN("A '" << test.input_description << "' configuration") {
114  WHEN("Creating an OpGraph from it") {
115  THEN("The result should " << (test.expect_to_parse ? "be as expected" : "fail to parse")) {
116  if (test.expect_to_parse) {
117  const auto actual = do_createOpGraphFromConfig(test.input);
118  CHECK(actual == test.expected);
119  } else {
120  REQUIRE_THROWS(do_createOpGraphFromConfig(test.input));
121  }
122  }
123  }
124  }
125 }
126 
127 } // end anon namespace
ConfigGraph.h
OpCode::CONST
@ CONST
OpGraphProcedures.h
begin
auto begin(const SingleItemImmutableSet< VertexID > &siis)
Definition: Collections.h:137
ConfigGraph
Definition: ConfigGraph.h:72
OpCode::ADD
@ ADD
TEST_CASE
TEST_CASE("createOpGraphFromConfig Tests")
Definition: createOpGraphFromConfig_tests.cpp:32
createOpGraphFromConfig
OpGraph createOpGraphFromConfig(const ConfigGraph &config)
Definition: OpGraphProcedures.cpp:187
OpGraphsForTesting.hpp
ConfigGraph::insert
std::pair< VertexID, bool > insert(std::string name, ConfigStore vertex_attrs={})
Definition: ConfigGraph.h:173
ConfigGraph::link
EdgeID link(VertexID source, VertexID target, ConfigStore edge_attrs={})
Definition: ConfigGraph.h:190
finalFixup
OpGraph finalFixup(OpGraph opgraph)
Definition: OpGraphsForTesting.cpp:5
end
auto end(const SingleItemImmutableSet< VertexID > &siis)
Definition: Collections.h:138
OpGraph::insert
OpDescriptor insert(OpGraphOp op)
Definition: OpGraph.cpp:338
Operands::BINARY_ANY
static constexpr auto & BINARY_ANY
Definition: OpGraph.h:91
OpGraph
Definition: OpGraph.h:215