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_sequencer_node.cpp
33 //! \brief Test for [flow_graph.sequencer_node] specification
34 
35 /*
36 TODO: implement missing conformance tests for sequencer_node:
37   - [ ] The copy constructor and copy assignment are called for the node's type template parameter.
38   - [ ] Explicit test that `Sequencer' requirements are necessary.
39   - [ ] Write tests for the constructors.
40   - [ ] Add CTAD test.
41   - [ ] Improve `test_buffering' by checking that additional `try_get()' does not receive the same
42     value.
43   - [ ] Add explicit test on the example from the specification.
44 */
45 
46 template<typename T>
47 void test_inheritance(){
48     using namespace oneapi::tbb::flow;
49 
50     CHECK_MESSAGE( (std::is_base_of<graph_node, sequencer_node<T>>::value), "sequencer_node should be derived from graph_node");
51     CHECK_MESSAGE( (std::is_base_of<receiver<T>, sequencer_node<T>>::value), "sequencer_node should be derived from receiver<T>");
52     CHECK_MESSAGE( (std::is_base_of<sender<T>, sequencer_node<T>>::value), "sequencer_node should be derived from sender<T>");
53 }
54 
55 template<typename T>
56 struct id_sequencer{
57     using input_type = T;
58 
59     std::size_t operator()(T v) {
60         return v;
61     }
62 };
63 
64 void test_copies(){
65     using namespace oneapi::tbb::flow;
66 
67     graph g;
68     id_sequencer<int> sequencer;
69 
70     sequencer_node<int> n(g, sequencer);
71     sequencer_node<int> n2(n);
72 }
73 
74 void test_buffering(){
75     oneapi::tbb::flow::graph g;
76 
77     id_sequencer<int> sequencer;
78 
79     oneapi::tbb::flow::sequencer_node<int> node(g, sequencer);
80     oneapi::tbb::flow::limiter_node<int> rejecter(g, 0);
81 
82     oneapi::tbb::flow::make_edge(node, rejecter);
83     node.try_put(1);
84     g.wait_for_all();
85 
86     int tmp = -1;
87     CHECK_MESSAGE( (node.try_get(tmp) == false), "try_get after rejection should not succeed");
88     CHECK_MESSAGE( (tmp == -1), "try_get after rejection should not set value");
89 }
90 
91 #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
92 void test_deduction_guides(){
93     // oneapi::tbb::flow::graph g;
94     // id_sequencer<int> sequ;
95     // oneapi::tbb::flow::sequencer_node node1(g, sequ);
96 }
97 #endif
98 
99 void test_forwarding(){
100     oneapi::tbb::flow::graph g;
101     id_sequencer<int> sequencer;
102 
103     oneapi::tbb::flow::sequencer_node<int> node1(g, sequencer);
104     test_push_receiver<int> node2(g);
105     test_push_receiver<int> node3(g);
106 
107     oneapi::tbb::flow::make_edge(node1, node2);
108     oneapi::tbb::flow::make_edge(node1, node3);
109 
110     node1.try_put(0);
111 
112     g.wait_for_all();
113 
114     int c2 = get_count(node2), c3 = get_count(node3);
115     CHECK_MESSAGE( (c2 != c3 ), "Only one descendant the node needs to receive");
116     CHECK_MESSAGE( (c2 + c3 == 1 ), "Messages need to be received");
117 }
118 
119 void test_sequencer(){
120     oneapi::tbb::flow::graph g;
121     id_sequencer<int> sequencer;
122 
123     oneapi::tbb::flow::sequencer_node<int> node(g, sequencer);
124 
125     node.try_put(1);
126     node.try_put(0);
127     node.try_put(1);
128     g.wait_for_all();
129 
130     int tmp = -1;
131     CHECK_MESSAGE((node.try_get(tmp) == true), "Getting from sequencer should succeed");
132     CHECK_MESSAGE((tmp == 0), "Received value should be correct");
133 
134     tmp = -1;
135     CHECK_MESSAGE((node.try_get(tmp) == true), "Getting from sequencer should succeed");
136     CHECK_MESSAGE((tmp == 1), "Received value should be correct");
137 
138     tmp = -1;
139     CHECK_MESSAGE((node.try_get(tmp) == false), "Getting from sequencer should not succeed");
140 
141 }
142 
143 //! Test function_node buffering
144 //! \brief \ref requirement
145 TEST_CASE("sequencer_node buffering"){
146     test_sequencer();
147 }
148 
149 //! Test function_node buffering
150 //! \brief \ref requirement
151 TEST_CASE("sequencer_node buffering"){
152     test_forwarding();
153 }
154 
155 //! Test deduction guides
156 //! \brief \ref interface \ref requirement
157 TEST_CASE("Deduction guides"){
158 #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
159     test_deduction_guides();
160 #endif
161 }
162 
163 //! Test priority_queue_node buffering
164 //! \brief \ref requirement
165 TEST_CASE("sequencer_node buffering"){
166     test_buffering();
167 }
168 
169 //! Test copy constructor
170 //! \brief \ref interface
171 TEST_CASE("sequencer_node copy constructor"){
172     test_copies();
173 }
174 
175 //! Test inheritance relations
176 //! \brief \ref interface
177 TEST_CASE("sequencer_node superclasses"){
178     test_inheritance<int>();
179     test_inheritance<void*>();
180 }
181 
182