Simplify compiling GPU code for tests
[gromacs.git] / src / gromacs / mdspan / tests / layouts.cpp
blobc7f5be2c6fa9a8b8d4e95e22695458f928642515
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \internal \file
36 * \brief Testing gmx::extents.
38 * \author Christian Trott <crtrott@sandia.gov>
39 * \author David Hollman <dshollm@sandia.gov>
40 * \author Christian Blau <cblau@gwdg.de>
42 #include "gmxpre.h"
44 #include "gromacs/mdspan/layouts.h"
46 #include <gtest/gtest.h>
48 #include "gromacs/mdspan/extents.h"
50 namespace gmx
53 template<class Layout, ptrdiff_t... E_STATIC>
54 struct LayoutTests
57 typedef Layout layout_type;
58 typedef extents<E_STATIC...> extents_type;
59 typedef typename Layout::template mapping<extents_type> mapping_type;
61 mapping_type my_mapping_explicit, my_mapping_copy;
63 template<class... E>
64 LayoutTests(E... e)
66 my_mapping_explicit = mapping_type(extents_type(e...));
67 my_mapping_copy = mapping_type(my_mapping_explicit);
70 void check_rank(ptrdiff_t r)
72 EXPECT_EQ(my_mapping_explicit.extents().rank(), r);
73 EXPECT_EQ(my_mapping_copy.extents().rank(), r);
75 void check_rank_dynamic(ptrdiff_t r)
77 EXPECT_EQ(my_mapping_explicit.extents().rank_dynamic(), r);
78 EXPECT_EQ(my_mapping_copy.extents().rank_dynamic(), r);
80 template<class... E>
81 void check_extents(E... e)
83 std::array<ptrdiff_t, extents_type::rank()> a = { { e... } };
84 for (size_t r = 0; r < extents_type::rank(); r++)
86 EXPECT_EQ(my_mapping_explicit.extents().extent(r), a[r]);
87 EXPECT_EQ(my_mapping_copy.extents().extent(r), a[r]);
90 template<class... E>
91 void check_strides(E... e)
93 std::array<ptrdiff_t, extents_type::rank()> a = { { e... } };
94 for (size_t r = 0; r < extents_type::rank(); r++)
96 EXPECT_EQ(my_mapping_explicit.stride(r), a[r]);
97 EXPECT_EQ(my_mapping_copy.stride(r), a[r]);
100 void check_required_span_size(ptrdiff_t size)
102 EXPECT_EQ(my_mapping_explicit.required_span_size(), size);
103 EXPECT_EQ(my_mapping_copy.required_span_size(), size);
106 void check_properties(bool always_unique,
107 bool always_contiguous,
108 bool always_strided,
109 bool unique,
110 bool contiguous,
111 bool strided)
113 EXPECT_EQ(my_mapping_explicit.is_always_unique() ? 1 : 0, always_unique ? 1 : 0);
114 EXPECT_EQ(my_mapping_explicit.is_always_contiguous() ? 1 : 0, always_contiguous ? 1 : 0);
115 EXPECT_EQ(my_mapping_explicit.is_always_strided() ? 1 : 0, always_strided ? 1 : 0);
116 EXPECT_EQ(my_mapping_explicit.is_unique() ? 1 : 0, unique ? 1 : 0);
117 EXPECT_EQ(my_mapping_explicit.is_contiguous() ? 1 : 0, contiguous ? 1 : 0);
118 EXPECT_EQ(my_mapping_explicit.is_strided() ? 1 : 0, strided ? 1 : 0);
119 EXPECT_EQ(my_mapping_copy.is_always_unique() ? 1 : 0, always_unique ? 1 : 0);
120 EXPECT_EQ(my_mapping_copy.is_always_contiguous() ? 1 : 0, always_contiguous ? 1 : 0);
121 EXPECT_EQ(my_mapping_copy.is_always_strided() ? 1 : 0, always_strided ? 1 : 0);
122 EXPECT_EQ(my_mapping_copy.is_unique() ? 1 : 0, unique ? 1 : 0);
123 EXPECT_EQ(my_mapping_copy.is_contiguous() ? 1 : 0, contiguous ? 1 : 0);
124 EXPECT_EQ(my_mapping_copy.is_strided() ? 1 : 0, strided ? 1 : 0);
127 template<class... E>
128 void check_operator(ptrdiff_t offset, E... e)
130 EXPECT_EQ(my_mapping_explicit(e...), offset);
131 EXPECT_EQ(my_mapping_copy(e...), offset);
135 TEST(LayoutTests, LayoutRightConstruction)
137 LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
139 test.check_rank(5);
140 test.check_rank_dynamic(2);
141 test.check_extents(5, 4, 3, 2, 1);
142 test.check_strides(24, 6, 2, 1, 1);
145 TEST(LayoutTests, LayoutRightProperties)
147 LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
149 test.check_properties(true, true, true, true, true, true);
152 TEST(LayoutTests, LayoutRightOperator)
154 LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
156 test.check_operator(107, 4, 1, 2, 1, 0);
157 test.check_operator(0, 0, 0, 0, 0, 0);
160 } // namespace gmx