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_broadcast_node.cpp
33 //! \brief Test for [flow_graph.broadcast_node] specification
34 
35 /*
36 TODO: implement missing conformance tests for broadcast_node:
37   - [ ] The copy constructor and copy assignment are called for the node's type template parameter.
38   - [ ] Improve test for constructors.
39 */
40 
41 template<typename T>
42 void test_inheritance(){
43     using namespace oneapi::tbb::flow;
44 
45     CHECK_MESSAGE( (std::is_base_of<graph_node, broadcast_node<T>>::value), "broadcast_node should be derived from graph_node");
46     CHECK_MESSAGE( (std::is_base_of<receiver<T>, broadcast_node<T>>::value), "broadcast_node should be derived from reciever<T>");
47     CHECK_MESSAGE( (std::is_base_of<sender<T>, broadcast_node<T>>::value), "broadcast_node should be derived from sender<T>");
48 }
49 
50 void test_copies(){
51     using namespace oneapi::tbb::flow;
52 
53     graph g;
54     broadcast_node<int> n(g);
55     broadcast_node<int> n2(n);
56 }
57 
58 void test_buffering(){
59     oneapi::tbb::flow::graph g;
60 
61     oneapi::tbb::flow::broadcast_node<int> node(g);
62     oneapi::tbb::flow::limiter_node<int> rejecter(g, 0);
63 
64     oneapi::tbb::flow::make_edge(node, rejecter);
65 
66     node.try_put(1);
67     g.wait_for_all();
68 
69     int tmp = -1;
70     CHECK_MESSAGE( (node.try_get(tmp) == false), "try_get after rejection should not succeed");
71     CHECK_MESSAGE( (tmp == -1), "try_get after rejection should not set value");
72 }
73 
74 void test_forwarding(){
75     oneapi::tbb::flow::graph g;
76 
77     oneapi::tbb::flow::broadcast_node<int> node1(g);
78     test_push_receiver<int> node2(g);
79     test_push_receiver<int> node3(g);
80 
81     oneapi::tbb::flow::make_edge(node1, node2);
82     oneapi::tbb::flow::make_edge(node1, node3);
83 
84     node1.try_put(1);
85     g.wait_for_all();
86 
87     int c2 = get_count(node2), c3 = get_count(node3);
88     CHECK_MESSAGE( ( c2 == 1), "Descendant of the node must receive one message.");
89     CHECK_MESSAGE( ( c3 == 1), "Descendant of the node must receive one message.");
90 }
91 
92 //! Test function_node broadcast
93 //! \brief \ref requirement
94 TEST_CASE("broadcast_node broadcasts"){
95     test_forwarding();
96 }
97 
98 //! Test broadcast_node buffering
99 //! \brief \ref requirement
100 TEST_CASE("broadcast_node buffering"){
101     test_buffering();
102 }
103 
104 //! Test copy constructor
105 //! \brief \ref interface
106 TEST_CASE("broadcast_node copy constructor"){
107     test_copies();
108 }
109 
110 //! Test inheritance relations
111 //! \brief \ref interface
112 TEST_CASE("broadcast_node superclasses"){
113     test_inheritance<int>();
114     test_inheritance<void*>();
115 }
116 
117