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