1 // Copyright (c) 2015 Hartmut Kaiser
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <hpx/hpx_init.hpp>
7 #include <hpx/util/tagged_pair.hpp>
8 #include <hpx/util/tagged_tuple.hpp>
9 #include <hpx/util/lightweight_test.hpp>
10
11 #include <utility>
12 #include <type_traits>
13
14 struct A
15 {
AA16 A(int val = 0) : value_(val) {}
17 int value_;
18 };
19
20 struct B
21 {
BB22 B(int val = 0) : value_(val) {}
23 int value_;
24 };
25
26 struct C
27 {
CC28 C(int val = 0) : value_(val) {}
29 int value_;
30 };
31
32 HPX_DEFINE_TAG_SPECIFIER(tag1) // defines tag::tag1
HPX_DEFINE_TAG_SPECIFIER(tag2)33 HPX_DEFINE_TAG_SPECIFIER(tag2) // defines tag::tag2
34 HPX_DEFINE_TAG_SPECIFIER(tag3) // defines tag::tag3
35
36 void tagged_pair_test()
37 {
38 typedef hpx::util::tagged_pair<tag::tag1(A), tag::tag2(B)> pair;
39
40 static_assert(std::is_same<typename pair::first_type, A>::value, "");
41 static_assert(std::is_same<typename pair::second_type, B>::value, "");
42
43 {
44 pair p;
45
46 static_assert(std::is_same<decltype(p.first), A>::value, "");
47 static_assert(std::is_same<decltype(p.second), B>::value, "");
48
49 static_assert(std::is_same<
50 typename hpx::util::decay<decltype(p.tag1())>::type, A
51 >::value, "");
52 static_assert(std::is_same<
53 typename hpx::util::decay<decltype(p.tag2())>::type, B
54 >::value, "");
55 }
56
57 {
58 pair p(42, 43);
59
60 HPX_TEST_EQ(p.tag1().value_, 42);
61 HPX_TEST_EQ(p.tag2().value_, 43);
62 }
63
64 {
65 pair p(42, 43);
66
67 HPX_TEST_EQ(hpx::util::get<0>(p).value_, 42);
68 HPX_TEST_EQ(hpx::util::get<1>(p).value_, 43);
69 }
70
71 {
72 pair p = hpx::util::make_tagged_pair<tag::tag1, tag::tag2>(42, 43);
73
74 HPX_TEST_EQ(p.tag1().value_, 42);
75 HPX_TEST_EQ(p.tag2().value_, 43);
76 }
77
78 {
79 pair p = hpx::util::make_tagged_pair<tag::tag1, tag::tag2>(
80 std::make_pair(42, 43));
81
82 HPX_TEST_EQ(p.tag1().value_, 42);
83 HPX_TEST_EQ(p.tag2().value_, 43);
84 }
85 }
86
tagged_tuple_test()87 void tagged_tuple_test()
88 {
89 typedef hpx::util::tagged_tuple<tag::tag1(A), tag::tag2(B), tag::tag3(C)>
90 tuple;
91
92 {
93 tuple t;
94
95 static_assert(std::is_same<
96 typename hpx::util::tuple_element<0, tuple>::type, A
97 >::value, "");
98 static_assert(std::is_same<
99 typename hpx::util::tuple_element<1, tuple>::type, B
100 >::value, "");
101 static_assert(std::is_same<
102 typename hpx::util::tuple_element<2, tuple>::type, C
103 >::value, "");
104
105 static_assert(std::is_same<
106 typename hpx::util::decay<decltype(t.tag1())>::type, A
107 >::value, "");
108 static_assert(std::is_same<
109 typename hpx::util::decay<decltype(t.tag2())>::type, B
110 >::value, "");
111 static_assert(std::is_same<
112 typename hpx::util::decay<decltype(t.tag3())>::type, C
113 >::value, "");
114 }
115
116 {
117 tuple t(42, 43, 44);
118
119 HPX_TEST_EQ(t.tag1().value_, 42);
120 HPX_TEST_EQ(t.tag2().value_, 43);
121 HPX_TEST_EQ(t.tag3().value_, 44);
122 }
123
124 {
125 tuple t(42, 43, 44);
126
127 HPX_TEST_EQ(hpx::util::get<0>(t).value_, 42);
128 HPX_TEST_EQ(hpx::util::get<1>(t).value_, 43);
129 HPX_TEST_EQ(hpx::util::get<2>(t).value_, 44);
130 }
131
132 {
133 using hpx::util::make_tagged_tuple;
134 tuple t = make_tagged_tuple<tag::tag1, tag::tag2, tag::tag3>(42, 43, 44);
135
136 HPX_TEST_EQ(t.tag1().value_, 42);
137 HPX_TEST_EQ(t.tag2().value_, 43);
138 HPX_TEST_EQ(t.tag3().value_, 44);
139 }
140
141 {
142 using hpx::util::make_tagged_tuple;
143 tuple t = make_tagged_tuple<tag::tag1, tag::tag2, tag::tag3>(
144 hpx::util::make_tuple(42, 43, 44));
145
146 HPX_TEST_EQ(t.tag1().value_, 42);
147 HPX_TEST_EQ(t.tag2().value_, 43);
148 HPX_TEST_EQ(t.tag3().value_, 44);
149 }
150 }
151
main(int argc,char * argv[])152 int main(int argc, char* argv[])
153 {
154 tagged_pair_test();
155 tagged_tuple_test();
156
157 return hpx::util::report_errors();
158 }
159