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 "conformance_flowgraph.h"
22 
23 //! \file conformance_split_node.cpp
24 //! \brief Test for [flow_graph.split_node] specification
25 
26 using input_msg = conformance::message</*default_ctor*/true, /*copy_ctor*/true, /*copy_assign*/true/*enable for queue_node successor*/>;
27 using my_input_tuple = std::tuple<int, float, input_msg>;
28 using my_split_type = oneapi::tbb::flow::split_node<my_input_tuple>;
29 
30 //! Test node not buffered unsuccessful message, and try_get after rejection should not succeed.
31 //! \brief \ref requirement
32 TEST_CASE("split_node buffering") {
33     oneapi::tbb::flow::graph g;
34 
35     my_split_type testing_node(g);
36 
37     oneapi::tbb::flow::limiter_node<int> rejecter1(g,0);
38     oneapi::tbb::flow::limiter_node<float> rejecter2(g,0);
39     oneapi::tbb::flow::limiter_node<input_msg> rejecter3(g,0);
40 
41     oneapi::tbb::flow::make_edge(oneapi::tbb::flow::output_port<0>(testing_node), rejecter1);
42     oneapi::tbb::flow::make_edge(oneapi::tbb::flow::output_port<1>(testing_node), rejecter2);
43     oneapi::tbb::flow::make_edge(oneapi::tbb::flow::output_port<2>(testing_node), rejecter3);
44 
45     my_input_tuple my_tuple(1, 1.5f, input_msg(2));
46     testing_node.try_put(my_tuple);
47     g.wait_for_all();
48 
49     int tmp1 = -1;
50     float tmp2 = -1;
51     input_msg tmp3(-1);
52     CHECK_MESSAGE((oneapi::tbb::flow::output_port<0>(testing_node).try_get(tmp1) == false
53                     && tmp1 == -1), "Value should be discarded after rejection");
54     CHECK_MESSAGE((oneapi::tbb::flow::output_port<1>(testing_node).try_get(tmp2) == false
55                     && tmp2 == -1.f), "Value should be discarded after rejection");
56     CHECK_MESSAGE((oneapi::tbb::flow::output_port<2>(testing_node).try_get(tmp3) == false
57                     && tmp3 == -1), "Value should be discarded after rejection");
58 }
59 
60 //! Test node broadcast messages to successors and splitting them in correct order
61 //! \brief \ref requirement
62 TEST_CASE("split_node broadcast and splitting"){
63     using namespace oneapi::tbb::flow;
64     oneapi::tbb::flow::graph g;
65 
66     my_split_type testing_node(g);
67     conformance::test_push_receiver<int> node2(g);
68     conformance::test_push_receiver<float> node3(g);
69     conformance::test_push_receiver<input_msg> node4(g);
70 
71     oneapi::tbb::flow::make_edge(output_port<0>(testing_node), node2);
72     oneapi::tbb::flow::make_edge(output_port<1>(testing_node), node3);
73     oneapi::tbb::flow::make_edge(output_port<2>(testing_node), node4);
74 
75     my_input_tuple my_tuple(1, 1.5f, input_msg(2));
76 
77     CHECK_MESSAGE((testing_node.try_put(my_tuple)), "`try_put()' must always returns `true'");
78     g.wait_for_all();
79     auto values1 = conformance::get_values(node2);
80     auto values2 = conformance::get_values(node3);
81     auto values3 = conformance::get_values(node4);
82 
83     CHECK_MESSAGE((values1.size() == 1), "Descendant of the node must receive one message.");
84     CHECK_MESSAGE((values2.size() == 1), "Descendant of the node must receive one message.");
85     CHECK_MESSAGE((values3.size() == 1), "Descendant of the node must receive one message.");
86     CHECK_MESSAGE((values1[0] == 1), "Descendant of the node needs to be receive N messages");
87     CHECK_MESSAGE((values2[0] == 1.5f), "Descendant of the node must receive one message.");
88     CHECK_MESSAGE((values3[0] == 2), "Descendant of the node must receive one message.");
89 }
90 
91 //! The node that is constructed has a reference to the same graph object as src.
92 //! The predecessors and successors of src are not copied.
93 //! \brief \ref interface
94 TEST_CASE("split_node copy constructor"){
95     oneapi::tbb::flow::graph g;
96     oneapi::tbb::flow::continue_node<std::tuple<int>> node0( g,
__anoned94b1790102(oneapi::tbb::flow::continue_msg) 97                                 [](oneapi::tbb::flow::continue_msg) { return std::tuple<int>(1); } );
98 
99     oneapi::tbb::flow::split_node<std::tuple<int>> node1(g);
100     conformance::test_push_receiver<int> node2(g);
101     conformance::test_push_receiver<int> node3(g);
102 
103     oneapi::tbb::flow::make_edge(node0, node1);
104     oneapi::tbb::flow::make_edge(oneapi::tbb::flow::output_port<0>(node1), node2);
105 
106     oneapi::tbb::flow::split_node<std::tuple<int>> node_copy(node1);
107 
108     oneapi::tbb::flow::make_edge(oneapi::tbb::flow::output_port<0>(node_copy), node3);
109 
110     node_copy.try_put(std::tuple<int>(1));
111     g.wait_for_all();
112 
113     CHECK_MESSAGE((conformance::get_values(node2).size() == 0 && conformance::get_values(node3).size() == 1), "Copied node doesn`t copy successor");
114 
115     node0.try_put(oneapi::tbb::flow::continue_msg());
116     g.wait_for_all();
117 
118     CHECK_MESSAGE((conformance::get_values(node2).size() == 1 && conformance::get_values(node3).size() == 0), "Copied node doesn`t copy predecessor");
119 }
120 
121 //! Test copy constructor
122 //! \brief \ref interface
123 TEST_CASE("split_node superclasses") {
124     CHECK_MESSAGE((std::is_base_of<oneapi::tbb::flow::graph_node, my_split_type>::value), "split_node should be derived from graph_node");
125     CHECK_MESSAGE((std::is_base_of<oneapi::tbb::flow::receiver<my_input_tuple>, my_split_type>::value), "split_node should be derived from receiver<T>");
126 }
127 
128 //! Test split_node output_ports() returns a tuple of output ports.
129 //! \brief \ref interface \ref requirement
130 TEST_CASE("split_node output_ports") {
131     oneapi::tbb::flow::graph g;
132     my_split_type node(g);
133 
134     CHECK_MESSAGE((std::is_same<my_split_type::output_ports_type&,
135         decltype(node.output_ports())>::value), "split_node output_ports should returns a tuple of output ports");
136 }
137