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