File: test_sid_dimension_to_tuple_like.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 (74 lines) | stat: -rw-r--r-- 2,771 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
/*
 * 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 <gtest/gtest.h>

#include <gridtools/common/array.hpp>
#include <gridtools/common/integral_constant.hpp>
#include <gridtools/common/tuple.hpp>
#include <gridtools/sid/dimension_to_tuple_like.hpp>

namespace gridtools {
    namespace {
        TEST(dimension_to_tuple_like, smoke) {
            double data[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
            auto testee = sid::dimension_to_tuple_like<integral_constant<int, 0>, 3>(data);
            static_assert(is_sid<decltype(testee)>::value);

            auto ptr = sid::get_origin(testee)();
            auto strides = sid::get_strides(testee);

            static_assert(tuple_util::size<decltype(strides)>{} == 1);

            EXPECT_EQ(data[1][0], tuple_util::get<1>(*ptr));
            EXPECT_EQ(&data[2][0], &tuple_util::get<2>(*ptr));

            sid::shift(ptr, tuple_util::get<0>(strides), 2);
            EXPECT_EQ(data[1][2], tuple_util::get<1>(*ptr));
            EXPECT_EQ(&data[2][2], &tuple_util::get<2>(*ptr));

            tuple_util::get<1>(*sid::shifted(ptr, tuple_util::get<0>(strides), 1)) = 42;
            EXPECT_EQ(42, data[1][3]);
        }

        TEST(dimension_to_tuple_like, assignable_from_tuple_like) {
            double data[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
            auto testee = sid::dimension_to_tuple_like<gridtools::integral_constant<int, 0>, 2>(data);
            static_assert(is_sid<decltype(testee)>::value);

            auto ptr = sid::get_origin(testee)();

            *ptr = tuple(2., 3.);
            EXPECT_EQ(tuple_util::get<0>(*ptr), 2.);
            EXPECT_EQ(data[0][0], 2.);
            EXPECT_EQ(tuple_util::get<1>(*ptr), 3.);
            EXPECT_EQ(data[1][0], 3.);
        }

        TEST(dimension_to_tuple_like, nested) {
            double data[2][3] = {0, 1, 2, 3, 4, 5};
            auto testee = sid::dimension_to_tuple_like<integral_constant<int, 1>, 3>(
                sid::dimension_to_tuple_like<integral_constant<int, 0>, 2>(data));
            static_assert(is_sid<decltype(testee)>::value);

            auto derefed_ptr = *sid::get_origin(testee)();

            auto first_level = tuple_util::get<2>(derefed_ptr);
            ASSERT_EQ(data[0][2], tuple_util::get<0>(first_level));
            ASSERT_EQ(data[1][2], tuple_util::get<1>(first_level));

            EXPECT_EQ(&data[1][2], &tuple_util::get<1>(tuple_util::get<2>(derefed_ptr)));

            derefed_ptr = array<array<double, 2>, 3>{10, 11, 12, 13, 14, 15};
            EXPECT_EQ(data[1][2], 15);
        }

    } // namespace
} // namespace gridtools