1 /*
2     Copyright (c) 2005-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 // Message based key matching is a preview feature
18 #define TBB_PREVIEW_FLOW_GRAPH_FEATURES 1
19 
20 #define MAX_TUPLE_TEST_SIZE 10
21 
22 #include "common/config.h"
23 
24 #include "test_join_node.h"
25 
26 //! \file test_join_node_msg_key_matching.cpp
27 //! \brief Test for [preview] functionality
28 
29 #if __TBB_PREVIEW_FLOW_GRAPH_NODE_SET
30 #include <array>
31 #include <vector>
32 void test_follows_and_precedes_api() {
33     using msg_t = MyMessageKeyWithoutKey<int, int>;
34     using JoinOutputType = std::tuple<msg_t, msg_t, msg_t>;
35 
36     std::array<msg_t, 3> messages_for_follows = { {msg_t(), msg_t(), msg_t()} };
37     std::vector<msg_t> messages_for_precedes = { msg_t(), msg_t(), msg_t() };
38 
39     follows_and_precedes_testing::test_follows
40         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::key_matching<std::size_t>>, tbb::flow::buffer_node<msg_t>>
41         (messages_for_follows);
42     follows_and_precedes_testing::test_precedes
43         <msg_t, tbb::flow::join_node<JoinOutputType, tbb::flow::key_matching<std::size_t>>>
44         (messages_for_precedes);
45 }
46 #endif
47 
48 #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
49 struct message_key {
50     int my_key;
51     double my_value;
52 
53     int key() const { return my_key; }
54 
55     operator size_t() const { return my_key; }
56 
57     bool operator==(const message_key& rhs) { return my_value == rhs.my_value; }
58 };
59 
60 void test_deduction_guides() {
61     using namespace tbb::flow;
62     using tuple_type = std::tuple<message_key, message_key>;
63 
64     graph g;
65     broadcast_node<message_key> bm1(g), bm2(g);
66     broadcast_node<tuple_type> bm3(g);
67     join_node<tuple_type, key_matching<int> > j0(g);
68     join_node j3(j0);
69     static_assert(std::is_same_v<decltype(j3), join_node<tuple_type, key_matching<int>>>);
70 }
71 #endif
72 
73 template <typename T1, typename T2>
74 using make_tuple = decltype(std::tuple_cat(T1(), std::tuple<T2>()));
75 using T1 = std::tuple<MyMessageKeyWithoutKeyMethod<std::string, double>>;
76 using T2 = make_tuple<T1, MyMessageKeyWithBrokenKey<std::string, int>>;
77 using T3 = make_tuple < T2, MyMessageKeyWithoutKey<std::string, int>>;
78 using T4 = make_tuple < T3, MyMessageKeyWithoutKeyMethod<std::string, size_t>>;
79 using T5 = make_tuple < T4, MyMessageKeyWithBrokenKey<std::string, int>>;
80 using T6 = make_tuple < T5, MyMessageKeyWithoutKeyMethod<std::string, short>>;
81 using T7 = make_tuple < T6, MyMessageKeyWithoutKeyMethod<std::string, threebyte>>;
82 using T8 = make_tuple < T7, MyMessageKeyWithBrokenKey<std::string, int>>;
83 using T9 = make_tuple < T8, MyMessageKeyWithoutKeyMethod<std::string, threebyte>>;
84 using T10 = make_tuple < T9, MyMessageKeyWithBrokenKey<std::string, size_t>>;
85 
86 #if TBB_TEST_LOW_WORKLOAD && TBB_USE_DEBUG
87 // the compiler might generate huge object file in debug (>64M)
88 #define TEST_CASE_TEMPLATE_N_ARGS(dec) TEST_CASE_TEMPLATE(dec, T, T2, T3, T4, T5, T6, T7, T8, T9, T10)
89 #else
90 #define TEST_CASE_TEMPLATE_N_ARGS(dec) TEST_CASE_TEMPLATE(dec, T, T2, T10)
91 #endif
92 
93 //! Serial test with different tuple sizes
94 //! \brief \ref error_guessing
95 TEST_CASE_TEMPLATE_N_ARGS("Serial N tests") {
96     generate_test<serial_test, T, message_based_key_matching<std::string&> >::do_test();
97 }
98 
99 //! Parallel test with different tuple sizes
100 //! \brief \ref error_guessing
101 TEST_CASE_TEMPLATE_N_ARGS("Parallel N tests") {
102     generate_test<parallel_test, T, message_based_key_matching<std::string&> >::do_test();
103 }
104 
105 //! Serial test with matching policies
106 //! \brief \ref error_guessing
107 TEST_CASE("Serial test") {
108     generate_test<serial_test, std::tuple<MyMessageKeyWithBrokenKey<int, double>, MyMessageKeyWithoutKey<int, float> >, message_based_key_matching<int> >::do_test();
109     generate_test<serial_test, std::tuple<MyMessageKeyWithoutKeyMethod<std::string, double>, MyMessageKeyWithBrokenKey<std::string, float> >, message_based_key_matching<std::string> >::do_test();
110 }
111 
112 //! Parallel test with special key types
113 //! \brief \ref error_guessing
114 TEST_CASE("Parallel test"){
115     generate_test<parallel_test, std::tuple<MyMessageKeyWithBrokenKey<int, double>, MyMessageKeyWithoutKey<int, float> >, message_based_key_matching<int> >::do_test();
116     generate_test<parallel_test, std::tuple<MyMessageKeyWithoutKeyMethod<int, double>, MyMessageKeyWithBrokenKey<int, float> >, message_based_key_matching<int&> >::do_test();
117     generate_test<parallel_test, std::tuple<MyMessageKeyWithoutKey<std::string, double>, MyMessageKeyWithoutKeyMethod<std::string, float> >, message_based_key_matching<std::string&> >::do_test();
118 }
119 
120 #if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
121 //! Test deduction guides
122 //! \brief \ref requirement
123 TEST_CASE("Deduction guides test"){
124     test_deduction_guides();
125 }
126 #endif
127