File: test_sid_delegate.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 (87 lines) | stat: -rw-r--r-- 2,710 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
/*
 * 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/sid/delegate.hpp>

#include <functional>

#include <gtest/gtest.h>

#include <gridtools/common/integral_constant.hpp>
#include <gridtools/common/tuple_util.hpp>
#include <gridtools/sid/concept.hpp>
#include <gridtools/sid/simple_ptr_holder.hpp>
#include <gridtools/sid/synthetic.hpp>

namespace gridtools {
    namespace {
        using namespace literals;

        template <class Sid>
        struct i_shifted : sid::delegate<Sid> {
            friend sid::ptr_holder_type<Sid> sid_get_origin(i_shifted &obj) {
                auto &&impl = obj.m_impl;
                sid::ptr_diff_type<Sid> offset{};
                sid::shift(offset, sid::get_stride<integral_constant<int, 0>>(sid::get_strides(impl)), 1_c);
                return sid::get_origin(impl) + offset;
            }
            using sid::delegate<Sid>::delegate;
        };

        template <class Sid>
        i_shifted<Sid> i_shift(Sid &&sid) {
            return {std::forward<Sid>(sid)};
        }

        using sid::property;
        namespace tu = tuple_util;

        TEST(delegate, smoke) {
            double data[3][5];
            auto src = sid::synthetic()
                           .set<property::origin>(sid::host_device::simple_ptr_holder(&data[0][0]))
                           .set<property::strides>(tuple(5_c, 1_c));
            EXPECT_EQ(&data[0][0], sid::get_origin(src)());

            auto testee = i_shift(std::move(src));

            static_assert(is_sid<decltype(testee)>());

            EXPECT_EQ(&data[1][0], sid::get_origin(testee)());
        }

        TEST(delegate, array) {
            double src[3][5];
            auto testee = i_shift(src);

            static_assert(is_sid<decltype(testee)>());

            EXPECT_EQ(&src[1][0], sid::get_origin(testee)());
            auto strides = sid::get_strides(testee);
            EXPECT_EQ(tu::get<0>(strides), 5);
            EXPECT_EQ(tu::get<1>(strides), 1);
        }

        template <class Sid>
        sid::delegate<Sid> just_delegate(Sid &&s) {
            return {std::forward<Sid>(s)};
        }

        TEST(delegate, do_nothing) {
            double data[3][5];
            auto src = sid::synthetic()
                           .set<property::origin>(sid::host_device::simple_ptr_holder(&data[0][0]))
                           .set<property::strides>(tuple(5_c, 1_c));
            auto testee = just_delegate(src);
            static_assert(is_sid<decltype(testee)>());
        }

    } // namespace
} // namespace gridtools