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