1 /*
2     Copyright (c) 2020-2021 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 #if __INTEL_COMPILER && _MSC_VER
18 #pragma warning(disable : 2586) // decorated name length exceeded, name was truncated
19 #endif
20 
21 #include "common/test.h"
22 
23 #include "common/utils.h"
24 #include "common/graph_utils.h"
25 
26 #include "oneapi/tbb/flow_graph.h"
27 #include "oneapi/tbb/task_arena.h"
28 #include "oneapi/tbb/global_control.h"
29 
30 #include "conformance_flowgraph.h"
31 
32 //! \file conformance_composite_node.cpp
33 //! \brief Test for [flow_graph.composite_node] specification
34 
35 /*
36 TODO: implement missing conformance tests for composite_node:
37   - [ ] Check that `input_ports_type' and `output_ports_type' are defined, accessible, and meet
38     their requirements, that is, each element is a reference to actual node, input or output
39     port respectively.
40   - [ ] Add tests for `composite_node' with only input and output ports.
41   - [ ] Make sure `input_ports()' and `output_ports()' are defined and accessible in respective
42     specializations.
43   - [ ] Check the size of input and output tuples is equal to the size of `input_ports_type' and
44     `output_ports_type'.
45 */
46 
47 using namespace oneapi::tbb::flow;
48 using namespace std;
49 
50 class adder : public composite_node<  tuple<int, int>,  tuple<int> > {
51     join_node< tuple<int,int>, queueing > j;
52     function_node< tuple<int,int>, int > f;
53     queue_node <int> qn;
54     typedef composite_node< tuple<int,int>, tuple<int> > base_type;
55 
56     struct f_body {
57         int operator()(const tuple<int,int> &t) {
58             int sum = get<0>(t) + get<1>(t);
59             return  sum;
60         }
61     };
62 
63 public:
64     adder(graph &g) : base_type(g), j(g), f(g, unlimited, f_body()), qn(g) {
65         make_edge(j, f);
66         make_edge(f, qn);
67 
68         base_type::input_ports_type input_tuple(input_port<0>(j), input_port<1>(j));
69         base_type::output_ports_type output_tuple(qn);
70         base_type::set_external_ports(input_tuple, output_tuple);
71     }
72 };
73 
74 void test_inheritance(){
75     using namespace oneapi::tbb::flow;
76 
77     CHECK_MESSAGE( (std::is_base_of<graph_node, adder>::value), "multifunction_node should be derived from graph_node");
78 
79 }
80 
81 //! Test inheritance relations
82 //! \brief \ref interface
83 TEST_CASE("composite_node superclasses"){
84     test_inheritance();
85 }
86 
87 //! Test inheritance relations
88 //! \brief \ref interface \ref requirement
89 TEST_CASE("Construction and message test"){
90     graph g;
91     split_node< tuple<int, int, int, int> > s(g);
92     adder a0(g);
93     adder a1(g);
94     adder a2(g);
95 
96     make_edge(output_port<0>(s), input_port<0>(a0));
97     make_edge(output_port<1>(s), input_port<1>(a0));
98 
99     make_edge(output_port<0>(a0),input_port<0>(a1));
100     make_edge(output_port<2>(s), input_port<1>(a1));
101 
102     make_edge(output_port<0>(a1), input_port<0>(a2));
103     make_edge(output_port<3>(s), input_port<1>(a2));
104 
105     s.try_put(std::make_tuple(1,3,5,7));
106     g.wait_for_all();
107 
108     int tmp = -1;
109     CHECK_MESSAGE((output_port<0>(a2).try_get(tmp) == true), "Composite node should produce a value");
110     CHECK_MESSAGE((tmp == 1+3+5+7), "Composite node should produce correct sum");
111 }
112