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 "oneapi/tbb/flow_graph.h"
24 #include "oneapi/tbb/task_arena.h"
25 #include "oneapi/tbb/global_control.h"
26 
27 //! \file conformance_indexer_node.cpp
28 //! \brief Test for [flow_graph.indexer_node] specification
29 
30 /*
31 TODO: implement missing conformance tests for buffer_node:
32   - [ ] The copy constructor is called for the node's type template parameter.
33   - [ ] Improve `test_forwarding' by checking that the value passed is the actual one received.
34   - [ ] Improve `test_buffering' by checking that additional `try_get()' does not receive the same value.
35   - [ ] Improve tests of the constructors.
36   - [ ] Based on the decision about the details for `try_put()' and `try_get()' write corresponding tests.
37   - [ ] Fix description in `TEST_CASEs'.
38 */
39 using namespace oneapi::tbb::flow;
40 using namespace std;
41 
42 template<typename I1, typename I2>
43 void test_inheritance(){
44     using namespace oneapi::tbb::flow;
45 
46     CHECK_MESSAGE( (std::is_base_of<graph_node, indexer_node<I1, I2>>::value), "indexer_node should be derived from graph_node");
47 }
48 
49 void test_copies(){
50     using namespace oneapi::tbb::flow;
51 
52     graph g;
53     indexer_node<int, int> fn(g);
54 
55     indexer_node<int, int> f2(fn);
56 }
57 
58 //! Test body copying and copy_body logic
59 //! \brief \ref interface
60 TEST_CASE("indexer_node and body copying"){
61     test_copies();
62 }
63 
64 void test_broadcasting(){
65     oneapi::tbb::flow::graph g;
66 
67     typedef indexer_node<int,float> my_indexer_type;
68     typedef my_indexer_type::output_type my_output_type;
69 
70     my_indexer_type o(g);
71 
72     my_indexer_type node1(g);
73     queue_node<my_output_type> node2(g);
74     queue_node<my_output_type> node3(g);
75 
76     oneapi::tbb::flow::make_edge(node1, node2);
77     oneapi::tbb::flow::make_edge(node1, node3);
78 
79     input_port<0>(node1).try_put(6);
80     input_port<1>(node1).try_put(1.5);
81     g.wait_for_all();
82 
83     my_output_type tmp;
84     CHECK_MESSAGE( (node2.try_get(tmp)), "Descendant of the node needs to receive message once");
85     CHECK_MESSAGE( (node3.try_get(tmp)), "Descendant of the node needs to receive message once");
86 }
87 
88 //! Test broadcasting property
89 //! \brief \ref requirement
90 TEST_CASE("indexer_node broadcasts"){
91     test_broadcasting();
92 }
93 
94 //! Test inheritance relations
95 //! \brief \ref interface
96 TEST_CASE("indexer_node superclasses"){
97     test_inheritance<int, int>();
98 }
99 
100 //! Test discarding property
101 //! \brief \ref requirement
102 TEST_CASE("indexer_node discarding") {
103   graph g;
104 
105   typedef indexer_node<int,float> my_indexer_type;
106   my_indexer_type o(g);
107 
108   limiter_node< my_indexer_type::output_type > rejecter( g,0);
109   make_edge( o, rejecter );
110 
111   input_port<0>(o).try_put(6);
112   input_port<1>(o).try_put(1.5);
113 
114   my_indexer_type::output_type tmp;
115   CHECK_MESSAGE((o.try_get(tmp) == false), "Value should be discarded after rejection");
116   g.wait_for_all();
117 }
118 
119 //! Test indexer body
120 //! \brief \ref requirement
121 TEST_CASE("indexer_node body") {
122   graph g;
123   function_node<int,int> f1( g, unlimited,
124                                [](const int &i) { return 2*i; } );
125   function_node<float,float> f2( g, unlimited,
126                                [](const float &f) { return f/2; } );
127 
128   typedef indexer_node<int,float> my_indexer_type;
129   my_indexer_type o(g);
130 
131   function_node< my_indexer_type::output_type >
132     f3( g, unlimited,
133         []( const my_indexer_type::output_type &v ) {
134             if (v.tag() == 0) {
135                 CHECK_MESSAGE( (cast_to<int>(v) == 6), "Expected to receive 6" );
136             } else {
137                 CHECK_MESSAGE( (cast_to<float>(v) == 1.5), "Expected to receive 1.5" );
138            }
139         }
140     );
141   make_edge( f1, input_port<0>(o) );
142   make_edge( f2, input_port<1>(o) );
143   make_edge( o, f3 );
144 
145   f1.try_put( 3 );
146   f2.try_put( 3 );
147   g.wait_for_all();
148 }
149