1 /*
2     Copyright (c) 2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 
18 #include "common/test.h"
19 
20 #include "common/utils.h"
21 #include "common/graph_utils.h"
22 
23 #include "oneapi/tbb/flow_graph.h"
24 #include "oneapi/tbb/task_arena.h"
25 #include "oneapi/tbb/global_control.h"
26 
27 #include "conformance_flowgraph.h"
28 
29 //! \file conformance_composite_node.cpp
30 //! \brief Test for [flow_graph.composite_node] specification
31 
32 /*
33 TODO: implement missing conformance tests for composite_node:
34   - [ ] Check that `input_ports_type' and `output_ports_type' are defined, accessible, and meet
35     their requirements, that is, each element is a reference to actual node, input or output
36     port respectively.
37   - [ ] Add tests for `composite_node' with only input and output ports.
38   - [ ] Make sure `input_ports()' and `output_ports()' are defined and accessible in respective
39     specializations.
40   - [ ] Check the size of input and output tuples is equal to the size of `input_ports_type' and
41     `output_ports_type'.
42 */
43 
44 using namespace oneapi::tbb::flow;
45 using namespace std;
46 
47 class adder : public composite_node<  tuple<int, int>,  tuple<int> > {
48     join_node< tuple<int,int>, queueing > j;
49     function_node< tuple<int,int>, int > f;
50     queue_node <int> qn;
51     typedef composite_node< tuple<int,int>, tuple<int> > base_type;
52 
53     struct f_body {
54         int operator()(const tuple<int,int> &t) {
55             int sum = get<0>(t) + get<1>(t);
56             return  sum;
57         }
58     };
59 
60 public:
61     adder(graph &g) : base_type(g), j(g), f(g, unlimited, f_body()), qn(g) {
62         make_edge(j, f);
63         make_edge(f, qn);
64 
65         base_type::input_ports_type input_tuple(input_port<0>(j), input_port<1>(j));
66         base_type::output_ports_type output_tuple(qn);
67         base_type::set_external_ports(input_tuple, output_tuple);
68     }
69 };
70 
71 void test_inheritance(){
72     using namespace oneapi::tbb::flow;
73 
74     CHECK_MESSAGE( (std::is_base_of<graph_node, adder>::value), "multifunction_node should be derived from graph_node");
75 
76 }
77 
78 //! Test inheritance relations
79 //! \brief \ref interface
80 TEST_CASE("composite_node superclasses"){
81     test_inheritance();
82 }
83 
84 //! Test inheritance relations
85 //! \brief \ref interface \ref requirement
86 TEST_CASE("Construction and message test"){
87     graph g;
88     split_node< tuple<int, int, int, int> > s(g);
89     adder a0(g);
90     adder a1(g);
91     adder a2(g);
92 
93     make_edge(output_port<0>(s), input_port<0>(a0));
94     make_edge(output_port<1>(s), input_port<1>(a0));
95 
96     make_edge(output_port<0>(a0),input_port<0>(a1));
97     make_edge(output_port<2>(s), input_port<1>(a1));
98 
99     make_edge(output_port<0>(a1), input_port<0>(a2));
100     make_edge(output_port<3>(s), input_port<1>(a2));
101 
102     s.try_put(std::make_tuple(1,3,5,7));
103     g.wait_for_all();
104 
105     int tmp = -1;
106     CHECK_MESSAGE((output_port<0>(a2).try_get(tmp) == true), "Composite node should produce a value");
107     CHECK_MESSAGE((tmp == 1+3+5+7), "Composite node should produce correct sum");
108 }
109