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_queue_node.cpp
33 //! \brief Test for [flow_graph.queue_node] specification
34 
35 /*
36 TODO: implement missing conformance tests for queue_node:
37   - [ ] The copy constructor and copy assignment are called for the node's type template parameter.
38   - [ ] Improve `test_forwarding()'.
39   - [ ] Improve tests of the constructors.
40   - [ ] Improve `test_buffering' by checking that additional `try_get()' does not receive the same
41     value.
42 - [ ] Based on the decision about the details for `try_put()' and `try_get()' write corresponding
43     tests.
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, queue_node<T>>::value), "queue_node should be derived from graph_node");
51     CHECK_MESSAGE( (std::is_base_of<receiver<T>, queue_node<T>>::value), "queue_node should be derived from receiver<T>");
52     CHECK_MESSAGE( (std::is_base_of<sender<T>, queue_node<T>>::value), "queue_node should be derived from sender<T>");
53 }
54 
55 void test_copies(){
56     using namespace oneapi::tbb::flow;
57 
58     graph g;
59     queue_node<int> n(g);
60     queue_node<int> n2(n);
61 
62 }
63 
64 void test_buffering(){
65     oneapi::tbb::flow::graph g;
66 
67     oneapi::tbb::flow::queue_node<int> node(g);
68     oneapi::tbb::flow::limiter_node<int> rejecter(g, 0);
69 
70     oneapi::tbb::flow::make_edge(node, rejecter);
71     node.try_put(1);
72     g.wait_for_all();
73 
74     int tmp = -1;
75     CHECK_MESSAGE( (node.try_get(tmp) == true), "try_get after rejection should succeed");
76     CHECK_MESSAGE( (tmp == 1), "try_get after rejection should set value");
77 }
78 
79 void test_forwarding(){
80     oneapi::tbb::flow::graph g;
81 
82     oneapi::tbb::flow::queue_node<int> node1(g);
83     test_push_receiver<int> node2(g);
84     test_push_receiver<int> node3(g);
85 
86     oneapi::tbb::flow::make_edge(node1, node2);
87     oneapi::tbb::flow::make_edge(node1, node3);
88 
89     node1.try_put(1);
90     g.wait_for_all();
91 
92     int c2 = get_count(node2), c3 = get_count(node3);
93     CHECK_MESSAGE( (c2 != c3 ), "Only one descendant the node needs to receive");
94     CHECK_MESSAGE( (c2 + c3 == 1 ), "All messages need to be received");
95 }
96 
97 void test_queue_node(){
98     oneapi::tbb::flow::graph g;
99 
100     oneapi::tbb::flow::queue_node<int> node(g);
101     node.try_put(1);
102     node.try_put(2);
103     g.wait_for_all();
104 
105     int tmp = -1;
106     CHECK_MESSAGE( (node.try_get(tmp) == true), "try_get should succeed");
107     CHECK_MESSAGE( (tmp == 1), "try_get should set correct value");
108 
109     tmp = -1;
110     CHECK_MESSAGE( (node.try_get(tmp) == true), "try_get should succeed");
111     CHECK_MESSAGE( (tmp == 2), "try_get should set correct value");
112 }
113 
114 void test_double_reserve(){
115     oneapi::tbb::flow::graph g;
116 
117     oneapi::tbb::flow::queue_node<int> node(g);
118 
119     int tmp = -1;
120     node.try_reserve(tmp);
121     CHECK_MESSAGE((tmp == -1), "Should not be delivered");
122     node.try_reserve(tmp);
123     CHECK_MESSAGE((tmp == -1), "Should not be delivered");
124 
125     g.reset();
126 
127     node.try_reserve(tmp);
128     CHECK_MESSAGE((tmp == -1), "Should not be delivered");
129     node.try_reserve(tmp);
130     CHECK_MESSAGE((tmp == -1), "Should not be delivered");
131 }
132 
133 //! Test multiple reserves
134 //! \brief \ref error_guessing
135 TEST_CASE("queue_node double reserve"){
136     test_double_reserve();
137 }
138 
139 //! Test message logic
140 //! \brief \ref requirement
141 TEST_CASE("queue_node messages"){
142     test_queue_node();
143 }
144 
145 //! Test single-push
146 //! \brief \ref requirement
147 TEST_CASE("queue_node buffering"){
148     test_forwarding();
149 }
150 
151 //! Test buffering
152 //! \brief \ref requirement
153 TEST_CASE("queue_node buffering"){
154     test_buffering();
155 }
156 
157 //! Test copy constructor
158 //! \brief \ref interface
159 TEST_CASE("queue_node copy constructor"){
160     test_copies();
161 }
162 
163 //! Test inheritance relations
164 //! \brief \ref interface
165 TEST_CASE("queue_node superclasses"){
166     test_inheritance<int>();
167     test_inheritance<void*>();
168 }
169 
170