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