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_limiter_node.cpp
33 //! \brief Test for [flow_graph.limiter_node] specification
34 
35 /*
36 TODO: implement missing conformance tests for limiter_node:
37   - [ ] The copy constructor and copy assignment are called for the node's type template parameter.
38   - [ ] Add use of `decrement' member into the `test_limiting' and see how positive and negative
39     values sent to `decrement's' port affect node's internal threshold.
40   - [ ] Add test checking the node gets value from the predecessors when threshold decreases enough.
41   - [ ] Add test that `continue_msg' decreases the threshold by one.
42 */
43 
44 template<typename T>
45 void test_inheritance(){
46     using namespace oneapi::tbb::flow;
47 
48     CHECK_MESSAGE( (std::is_base_of<graph_node, limiter_node<T>>::value), "sequencer_node should be derived from graph_node");
49     CHECK_MESSAGE( (std::is_base_of<receiver<T>, limiter_node<T>>::value), "sequencer_node should be derived from receiver<T>");
50     CHECK_MESSAGE( (std::is_base_of<sender<T>, limiter_node<T>>::value), "sequencer_node should be derived from sender<T>");
51 }
52 
53 void test_copies(){
54     using namespace oneapi::tbb::flow;
55 
56     graph g;
57     limiter_node<int> n(g, 5);
58     limiter_node<int> n2(n);
59 
60 }
61 
62 void test_buffering(){
63     oneapi::tbb::flow::graph g;
64 
65     oneapi::tbb::flow::limiter_node<int> node(g, 5);
66     oneapi::tbb::flow::limiter_node<int> rejecter(g, 0);
67 
68     oneapi::tbb::flow::make_edge(node, rejecter);
69     node.try_put(1);
70     g.wait_for_all();
71 
72     int tmp = -1;
73     CHECK_MESSAGE( (node.try_get(tmp) == false), "try_get after rejection should not succeed");
74     CHECK_MESSAGE( (tmp == -1), "try_get after rejection should not set value");
75 }
76 
77 void test_forwarding(){
78     oneapi::tbb::flow::graph g;
79 
80     oneapi::tbb::flow::limiter_node<int> node1(g, 5);
81     test_push_receiver<int> node2(g);
82     test_push_receiver<int> node3(g);
83 
84     oneapi::tbb::flow::make_edge(node1, node2);
85     oneapi::tbb::flow::make_edge(node1, node3);
86 
87     node1.try_put(1);
88     g.wait_for_all();
89 
90     CHECK_MESSAGE( (get_count(node2) == 1), "Descendant of the node needs to be receive N messages");
91     CHECK_MESSAGE( (get_count(node3) == 1), "Descendant of the node must receive one message.");
92 }
93 
94 void test_limiting(){
95     oneapi::tbb::flow::graph g;
96 
97     oneapi::tbb::flow::limiter_node<int> node1(g, 5);
98     test_push_receiver<int> node2(g);
99 
100     oneapi::tbb::flow::make_edge(node1, node2);
101 
102     for(int i = 0; i < 10; ++i)
103         node1.try_put(1);
104     g.wait_for_all();
105 
106     CHECK_MESSAGE( (get_count(node2) == 5), "Descendant of the node needs be receive limited number of messages");
107 }
108 
109 //! Test limiter_node limiting
110 //! \brief \ref requirement
111 TEST_CASE("limiter_node limiting"){
112     test_limiting();
113 }
114 
115 //! Test function_node broadcast
116 //! \brief \ref requirement
117 TEST_CASE("limiter_node broadcast"){
118     test_forwarding();
119 }
120 
121 //! Test limiter_node buffering
122 //! \brief \ref requirement
123 TEST_CASE("limiter_node buffering"){
124     test_buffering();
125 }
126 
127 //! Test copy constructor
128 //! \brief \ref interface
129 TEST_CASE("limiter_node copy constructor"){
130     test_copies();
131 }
132 
133 //! Test inheritance relations
134 //! \brief \ref interface
135 TEST_CASE("limiter_node superclasses"){
136     test_inheritance<int>();
137     test_inheritance<void*>();
138 }
139 
140