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