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_overwrite_node.cpp
30 //! \brief Test for [flow_graph.overwrite_node] specification
31 
32 /*
33 TODO: implement missing conformance tests for overwrite_node:
34   - [ ] The copy constructor and copy assignment are called for the node's type template parameter.
35   - [ ] Improve copy constructor test and general constructor tests.
36   - [ ] Write test checking the value is initially invalid.
37   - [ ] Write test checking that the gets from the node are non-destructive, but the first `try_get'
38     fails.
39   - [ ] Test that first `try_get' fails.
40   - [ ] Add test on `overwrite_node::is_valid()' method.
41   - [ ] Add test on `overwrite_node::clear()' method.
42   - [ ] Add test with reserving `join_node' as node's successor. Use example from the spec.
43 */
44 
45 template<typename T>
46 void test_inheritance(){
47     using namespace tbb::flow;
48 
49     CHECK_MESSAGE( (std::is_base_of<graph_node, overwrite_node<T>>::value), "overwrite_node should be derived from graph_node");
50     CHECK_MESSAGE( (std::is_base_of<receiver<T>, overwrite_node<T>>::value), "overwrite_node should be derived from receiver<T>");
51     CHECK_MESSAGE( (std::is_base_of<sender<T>, overwrite_node<T>>::value), "overwrite_node should be derived from sender<T>");
52 }
53 
54 void test_copies(){
55     using namespace tbb::flow;
56 
57     graph g;
58     overwrite_node<int> fn(g);
59     overwrite_node<int> fn2(fn);
60 
61 }
62 
63 void test_buffering(){
64     tbb::flow::graph g;
65 
66     tbb::flow::overwrite_node<int> node(g);
67     tbb::flow::limiter_node<int> rejecter(g, 0);
68 
69     tbb::flow::make_edge(node, rejecter);
70     node.try_put(1);
71     g.wait_for_all();
72 
73     int 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 }
77 
78 void test_forwarding(){
79     tbb::flow::graph g;
80 
81     tbb::flow::overwrite_node<int> node1(g);
82     test_push_receiver<int> node2(g);
83     test_push_receiver<int> node3(g);
84 
85     tbb::flow::make_edge(node1, node2);
86     tbb::flow::make_edge(node1, node3);
87 
88     node1.try_put(1);
89     g.wait_for_all();
90 
91     CHECK_MESSAGE( (get_count(node2) == 1), "Descendant of the node needs to be receive N messages");
92     CHECK_MESSAGE( (get_count(node3) == 1), "Descendant of the node must receive one message.");
93 }
94 
95 void test_overwriting(){
96     tbb::flow::graph g;
97 
98     tbb::flow::overwrite_node<int> node1(g);
99 
100     int tmp = -1;
101     node1.try_put(1);
102     g.wait_for_all();
103 
104     CHECK_MESSAGE( (node1.try_get(tmp) == true), "Descendant needs to receive a message");
105     CHECK_MESSAGE( (tmp == 1), "Descendant needs to receive a correct value");
106 
107     node1.try_put(2);
108     g.wait_for_all();
109 
110     CHECK_MESSAGE( (node1.try_get(tmp) == true), "Descendant needs to receive a message");
111     CHECK_MESSAGE( (tmp == 2), "Descendant needs to receive a correct value");
112 }
113 
114 //! Test overwrite_node behavior
115 //! \brief \ref requirement
116 TEST_CASE("overwrite_node messages"){
117     test_overwriting();
118 }
119 
120 //! Test function_node broadcast
121 //! \brief \ref requirement
122 TEST_CASE("overwrite_node broadcast"){
123     test_forwarding();
124 }
125 
126 //! Test function_node buffering
127 //! \brief \ref requirement
128 TEST_CASE("overwrite_node buffering"){
129     test_buffering();
130 }
131 
132 //! Test copy constructor
133 //! \brief \ref interface
134 TEST_CASE("overwrite_node copy constructor"){
135     test_copies();
136 }
137 
138 //! Test inheritance relations
139 //! \brief \ref interface
140 TEST_CASE("overwrite_node superclasses"){
141     test_inheritance<int>();
142     test_inheritance<void*>();
143 }
144