1 /*
2     Copyright (c) 2022 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 #define TBB_PREVIEW_FLOW_GRAPH_FEATURES 1
18 
19 #include "common/config.h"
20 
21 #include "test_join_node.h"
22 #include "common/test_join_node_multiple_predecessors.h"
23 #include "common/test_follows_and_precedes_api.h"
24 
25 #include <array>
26 #include <vector>
27 
28 //! \file test_join_node_preview.cpp
29 //! \brief Test for [preview] functionality
30 
31 void jn_follows_and_precedes() {
32     using msg_t = tbb::flow::continue_msg;
33     using JoinOutputType = std::tuple<msg_t, msg_t, msg_t>;
34 
35     std::array<msg_t, 3> messages_for_follows = { {msg_t(), msg_t(), msg_t()} };
36     std::vector<msg_t> messages_for_precedes = {msg_t(), msg_t(), msg_t()};
37 
38     follows_and_precedes_testing::test_follows
39         <msg_t, tbb::flow::join_node<JoinOutputType>, tbb::flow::buffer_node<msg_t>>(messages_for_follows);
40     follows_and_precedes_testing::test_follows
41         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::queueing>>(messages_for_follows);
42     follows_and_precedes_testing::test_follows
43         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::reserving>, tbb::flow::buffer_node<msg_t>>(messages_for_follows);
44     auto b = [](msg_t) { return msg_t(); };
45     class hash_compare {
46     public:
47         std::size_t hash(msg_t) const { return 0; }
48         bool equal(msg_t, msg_t) const { return true; }
49     };
50     follows_and_precedes_testing::test_follows
51         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::key_matching<msg_t, hash_compare>>, tbb::flow::buffer_node<msg_t>>
52         (messages_for_follows, b, b, b);
53 
54     follows_and_precedes_testing::test_precedes
55         <msg_t, tbb::flow::join_node<JoinOutputType>>(messages_for_precedes);
56     follows_and_precedes_testing::test_precedes
57         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::queueing>>(messages_for_precedes);
58     follows_and_precedes_testing::test_precedes
59         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::reserving>>(messages_for_precedes);
60     follows_and_precedes_testing::test_precedes
61         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::key_matching<msg_t, hash_compare>>>
62         (messages_for_precedes, b, b, b);
63 }
64 
65 void jn_msg_key_matching_follows_and_precedes() {
66     using msg_t = MyMessageKeyWithoutKey<int, int>;
67     using JoinOutputType = std::tuple<msg_t, msg_t, msg_t>;
68 
69     std::array<msg_t, 3> messages_for_follows = { {msg_t(), msg_t(), msg_t()} };
70     std::vector<msg_t> messages_for_precedes = { msg_t(), msg_t(), msg_t() };
71 
72     follows_and_precedes_testing::test_follows
73         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::key_matching<std::size_t>>, tbb::flow::buffer_node<msg_t>>
74         (messages_for_follows);
75     follows_and_precedes_testing::test_precedes
76         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::key_matching<std::size_t>>>
77         (messages_for_precedes);
78 }
79 
80 void test_follows_and_precedes_api() {
81     jn_follows_and_precedes();
82     jn_msg_key_matching_follows_and_precedes();
83 }
84 
85 //! Test follows and precedes API
86 //! \brief \ref error_guessing
87 TEST_CASE("Test follows and preceedes API"){
88     test_follows_and_precedes_api();
89 }
90 
91 // TODO: Look deeper into this test to see if it has the right name
92 // and if it actually tests some kind of regression. It is possible
93 // that `connect_join_via_follows` and `connect_join_via_precedes`
94 // functions are redundant.
95 
96 //! Test maintaining correct count of ports without input
97 //! \brief \ref error_guessing
98 TEST_CASE("Test removal of the predecessor while having none") {
99     using namespace multiple_predecessors;
100 
101     test(connect_join_via_follows);
102     test(connect_join_via_precedes);
103 }
104