File: test_hymap.cpp

package info (click to toggle)
gridtools 2.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,480 kB
  • sloc: cpp: 228,792; python: 17,561; javascript: 9,164; ansic: 4,101; sh: 850; makefile: 231; f90: 201
file content (238 lines) | stat: -rw-r--r-- 8,073 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * GridTools
 *
 * Copyright (c) 2014-2023, ETH Zurich
 * All rights reserved.
 *
 * Please, refer to the LICENSE file in the root directory.
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <gridtools/common/hymap.hpp>

#include <tuple>
#include <type_traits>

#include <gtest/gtest.h>

#include <gridtools/common/integral_constant.hpp>
#include <gridtools/common/tuple_util.hpp>
#include <gridtools/meta.hpp>

namespace gridtools {
    namespace {

        struct a;
        struct b;
        struct c;

        static_assert(!is_hymap<void>::value);
        static_assert(is_hymap<std::array<int, 3>>::value);
        static_assert(is_hymap<tuple<int, double>>::value);
        static_assert(is_hymap<hymap::keys<>::values<>>::value);
        static_assert(is_hymap<hymap::keys<a>::values<int>>::value);
        static_assert(is_hymap<hymap::keys<a, b>::values<int, double>>::value);

        static_assert(std::is_same_v<get_keys<std::array<int, 2>>,
            meta::list<integral_constant<int, 0>, integral_constant<int, 1>>>);
        static_assert(std::is_same_v<get_keys<tuple<int, double>>,
            meta::list<integral_constant<int, 0>, integral_constant<int, 1>>>);
        static_assert(std::is_same_v<get_keys<hymap::keys<>::values<>>, hymap::keys<>>);
        static_assert(std::is_same_v<get_keys<hymap::keys<a>::values<int>>, hymap::keys<a>>);

        TEST(tuple_like, smoke) {
            using testee_t = hymap::keys<a, b, c>::values<int, double, void *>;

            static_assert(tuple_util::size<testee_t>::value == 3);

            static_assert(std::is_same_v<tuple_util::element<0, testee_t>, int>);
            static_assert(std::is_same_v<tuple_util::element<1, testee_t>, double>);
            static_assert(std::is_same_v<tuple_util::element<2, testee_t>, void *>);

            testee_t testee{42, 5.3, nullptr};
            EXPECT_EQ(42, tuple_util::get<0>(testee));
            EXPECT_EQ(5.3, tuple_util::get<1>(testee));
            EXPECT_EQ(nullptr, tuple_util::get<2>(testee));
        }

        TEST(at_key, smoke) {
            using testee_t = hymap::keys<a, b>::values<int, double>;
            testee_t testee{42, 5.3};

            static_assert(has_key<testee_t, a>::value);
            static_assert(has_key<testee_t, b>::value);
            static_assert(!has_key<testee_t, c>::value);

            EXPECT_EQ(42, at_key<a>(testee));
            EXPECT_EQ(5.3, at_key<b>(testee));
        }

        TEST(at_key, tuple_like) {
            using testee_t = std::tuple<int, double>;
            testee_t testee{42, 5.3};

            static_assert(has_key<testee_t, integral_constant<int, 0>>::value);
            static_assert(has_key<testee_t, integral_constant<int, 1>>::value);
            static_assert(!has_key<testee_t, integral_constant<int, 2>>::value);

            EXPECT_EQ(42, (at_key<integral_constant<int, 0>>(testee)));
            EXPECT_EQ(5.3, (at_key<integral_constant<int, 1>>(testee)));
        }

        struct add_2_f {
            template <class T>
            T operator()(T x) const {
                return x + 2;
            }
        };

        TEST(tuple_like, transform) {
            using testee_t = hymap::keys<a, b>::values<int, double>;

            testee_t src = {42, 5.3};
            auto dst = tuple_util::transform(add_2_f{}, src);

            EXPECT_EQ(44, at_key<a>(dst));
            EXPECT_EQ(7.3, at_key<b>(dst));
        }

#if !defined(__NVCC__)
        TEST(deduction, smoke) {
            auto testee = hymap::keys<a, b>::values(42, 5.3);

            EXPECT_EQ(42, at_key<a>(testee));
            EXPECT_EQ(5.3, at_key<b>(testee));

            EXPECT_EQ(42, at_key<a>(hymap::keys<a>::values(42)));
        }
#if defined(__clang__) || defined(__GNUC__) && __GNUC__ > 8
        TEST(deduction, empty) { hymap::keys<>::values(); }
#endif
#endif

        TEST(convert_hymap, smoke) {
            auto src = hymap::keys<a, b>::make_values(42, 5.3);

            auto dst = tuple_util::convert_to<hymap::keys<b, c>::values>(src);

            EXPECT_EQ(42, at_key<b>(dst));
            EXPECT_EQ(5.3, at_key<c>(dst));
        }

        TEST(to_meta_map, smoke) {
            using src_t = hymap::keys<a, b>::values<int, double>;
            using dst_t = hymap::to_meta_map<src_t>;

            static_assert(std::is_same_v<meta::second<meta::mp_find<dst_t, a>>, int>);
            static_assert(std::is_same_v<meta::second<meta::mp_find<dst_t, b>>, double>);
        }

        TEST(from_meta_map, smoke) {
            using src_t = meta::list<meta::list<a, int>, meta::list<b, double>>;
            using dst_t = hymap::from_meta_map<src_t>;

            static_assert(std::is_same_v<typename std::decay<decltype(at_key<a>(std::declval<dst_t>()))>::type, int>);
            static_assert(
                std::is_same_v<typename std::decay<decltype(at_key<b>(std::declval<dst_t>()))>::type, double>);
        }

        TEST(from_meta_map, empty) {
            using src_t = meta::list<>;
            using dst_t = hymap::from_meta_map<src_t>;

            static_assert(tuple_util::size<dst_t>() == 0);
        }

        struct add_3_f {
            template <class Key, class Value>
            Value operator()(Value x) const {
                return x + 3;
            }
        };

        TEST(transform, smoke) {
            hymap::keys<a, b>::values<int, double> src = {42, 5.3};

            auto dst = hymap::transform(add_3_f{}, src);

            EXPECT_EQ(45, at_key<a>(dst));
            EXPECT_EQ(8.3, at_key<b>(dst));
        }

        struct acc_f {
            double &m_val;
            template <class Key, class Value>
            void operator()(Value x) const {
                m_val += x;
            }
        };

        TEST(for_each, smoke) {
            hymap::keys<a, b>::values<int, double> src = {42, 5.3};

            double acc = 0;
            hymap::for_each(acc_f{acc}, src);

            EXPECT_EQ(47.3, acc);
        }

        TEST(canonicalize_and_remove_key, smoke) {
            hymap::keys<a, b>::values<int, double> src = {42, 37.5};
            auto res = hymap::canonicalize_and_remove_key<a>(src);

            static_assert(!has_key<decltype(res), a>());
            static_assert(has_key<decltype(res), b>());

            EXPECT_EQ(37.5, at_key<b>(res));
        }

        TEST(merge, smoke) {
            auto m1 = hymap::keys<a, b>::make_values(1, 2);
            auto m2 = hymap::keys<b, c>::make_values(3.5, 16);
            auto testee = hymap::merge(m1, m2);

            EXPECT_EQ(1, at_key<a>(testee));
            EXPECT_EQ(2, at_key<b>(testee));
            EXPECT_EQ(16, at_key<c>(testee));
        }

        TEST(concat, smoke) {
            auto m1 = hymap::keys<a, b>::make_values(1, 2);
            auto m2 = hymap::keys<c>::make_values(3.5);
            auto testee = hymap::concat(m1, m2);

            EXPECT_EQ(1, at_key<a>(testee));
            EXPECT_EQ(2, at_key<b>(testee));
            EXPECT_EQ(3.5, at_key<c>(testee));
        }

        TEST(concat, empty) {
            auto m1 = hymap::keys<>::make_values();
            auto m2 = hymap::keys<a>::make_values(42);
            auto testee = hymap::concat(m1, m2);

            EXPECT_EQ(42, at_key<a>(testee));
        }

        TEST(assignment, smoke) {
            hymap::keys<a, b>::values<double, double> testee;
            auto src = hymap::keys<b, a, c>::make_values(88, 3.5, 16);
            testee = src;
            EXPECT_EQ(3.5, at_key<a>(testee));
            EXPECT_EQ(88, at_key<b>(testee));
        }

        TEST(assignment, references) {
            double testee_a = 0.;
            double testee_b = 0.;
            hymap::keys<a, b>::values<double &, double &> testee{testee_a, testee_b};
            double src_a = 3.5;
            double src_b = 88;
            hymap::keys<a, b>::values<double &, double &> src{src_a, src_b};
            testee = src;
            EXPECT_EQ(3.5, at_key<a>(testee));
            EXPECT_EQ(88, at_key<b>(testee));
        }

    } // namespace
} // namespace gridtools