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_buffer_node.cpp
33 //! \brief Test for [flow_graph.buffer_node] specification
34 
35 /*
36 TODO: implement missing conformance tests for buffer_node:
37   - [ ] The copy constructor is called for the node's type template parameter.
38   - [ ] Improve `test_forwarding' by checking that the value passed is the actual one received.
39   - [ ] Improve `test_buffering' by checking that additional `try_get()' does not receive the same
40     value.
41   - [ ] Improve tests of the constructors.
42   - [ ] Based on the decision about the details for `try_put()' and `try_get()' write corresponding
43     tests.
44   - [ ] Fix description in `TEST_CASEs'.*/
45 template<typename T>
46 void test_inheritance(){
47     using namespace oneapi::tbb::flow;
48 
49     CHECK_MESSAGE( (std::is_base_of<graph_node, buffer_node<T>>::value), "buffer_node should be derived from graph_node");
50     CHECK_MESSAGE( (std::is_base_of<receiver<T>, buffer_node<T>>::value), "buffer_node should be derived from receiver<T>");
51     CHECK_MESSAGE( (std::is_base_of<sender<T>, buffer_node<T>>::value), "buffer_node should be derived from sender<T>");
52 }
53 
54 void test_copies(){
55     using namespace oneapi::tbb::flow;
56 
57     graph g;
58     buffer_node<int> n(g);
59     buffer_node<int> n2(n);
60 
61 }
62 
63 void test_buffering(){
64     oneapi::tbb::flow::graph g;
65 
66     oneapi::tbb::flow::buffer_node<int> node(g);
67     oneapi::tbb::flow::limiter_node<int> rejecter(g, 0);
68 
69     oneapi::tbb::flow::make_edge(node, rejecter);
70 
71     int tmp = -1;
72     CHECK_MESSAGE( (node.try_get(tmp) == false), "try_get before placemnt should not succeed");
73 
74     node.try_put(1);
75 
76     tmp = -1;
77     CHECK_MESSAGE( (node.try_get(tmp) == true), "try_get after rejection should succeed");
78     CHECK_MESSAGE( (tmp == 1), "try_get after rejection should set value");
79     g.wait_for_all();
80 }
81 
82 void test_forwarding(){
83     oneapi::tbb::flow::graph g;
84 
85     oneapi::tbb::flow::buffer_node<int> node1(g);
86     test_push_receiver<int> node2(g);
87     test_push_receiver<int> node3(g);
88 
89     oneapi::tbb::flow::make_edge(node1, node2);
90     oneapi::tbb::flow::make_edge(node1, node3);
91 
92     node1.try_put(1);
93     g.wait_for_all();
94 
95     int c2 = get_count(node2), c3 = get_count(node3);
96     CHECK_MESSAGE( (c2 != c3 ), "Only one descendant the node needs to receive");
97     CHECK_MESSAGE( (c2 + c3 == 1 ), "All messages need to be received");
98 }
99 
100 //! Test function_node buffering
101 //! \brief \ref requirement
102 TEST_CASE("buffer_node buffering"){
103     test_forwarding();
104 }
105 
106 //! Test function_node buffering
107 //! \brief \ref requirement
108 TEST_CASE("buffer_node buffering"){
109     test_buffering();
110 }
111 
112 //! Test copy constructor
113 //! \brief \ref interface
114 TEST_CASE("buffer_node copy constructor"){
115     test_copies();
116 }
117 
118 //! Test inheritance relations
119 //! \brief \ref interface
120 TEST_CASE("buffer_node superclasses"){
121     test_inheritance<int>();
122     test_inheritance<void*>();
123 }
124 
125