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