Add replacements for pbc enumerations
[gromacs.git] / src / gromacs / mdspan / tests / layouts.cpp
blobbf1c0c6d77818a3b6a6373c1e5d52a29ee05e31c
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018, 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 {
56 typedef Layout layout_type;
57 typedef extents<E_STATIC...> extents_type;
58 typedef typename Layout::template mapping<extents_type> mapping_type;
60 mapping_type my_mapping_explicit, my_mapping_copy;
62 template<class ... E>
63 LayoutTests(E ... e)
65 my_mapping_explicit = mapping_type(extents_type(e ...));
66 my_mapping_copy = mapping_type(my_mapping_explicit);
69 void check_rank(ptrdiff_t r)
71 EXPECT_EQ(my_mapping_explicit.extents().rank(), r);
72 EXPECT_EQ(my_mapping_copy.extents().rank(), r);
74 void check_rank_dynamic(ptrdiff_t r)
76 EXPECT_EQ(my_mapping_explicit.extents().rank_dynamic(), r);
77 EXPECT_EQ(my_mapping_copy.extents().rank_dynamic(), r);
79 template<class ... E>
80 void check_extents(E ... e)
82 std::array<ptrdiff_t, extents_type::rank()> a = {{e ...}};
83 for (size_t r = 0; r < extents_type::rank(); r++)
85 EXPECT_EQ(my_mapping_explicit.extents().extent(r), a[r]);
86 EXPECT_EQ(my_mapping_copy.extents().extent(r), a[r]);
89 template<class ... E>
90 void check_strides(E ... e)
92 std::array<ptrdiff_t, extents_type::rank()> a = {{e ...}};
93 for (size_t r = 0; r < extents_type::rank(); r++)
95 EXPECT_EQ(my_mapping_explicit.stride(r), a[r]);
96 EXPECT_EQ(my_mapping_copy.stride(r), a[r]);
99 void check_required_span_size(ptrdiff_t size)
101 EXPECT_EQ(my_mapping_explicit.required_span_size(), size);
102 EXPECT_EQ(my_mapping_copy.required_span_size(), size);
105 void check_properties(bool always_unique, bool always_contiguous, bool always_strided,
106 bool unique, bool contiguous, bool strided)
108 EXPECT_EQ(my_mapping_explicit.is_always_unique() ? 1 : 0, always_unique ? 1 : 0);
109 EXPECT_EQ(my_mapping_explicit.is_always_contiguous() ? 1 : 0, always_contiguous ? 1 : 0);
110 EXPECT_EQ(my_mapping_explicit.is_always_strided() ? 1 : 0, always_strided ? 1 : 0);
111 EXPECT_EQ(my_mapping_explicit.is_unique() ? 1 : 0, unique ? 1 : 0);
112 EXPECT_EQ(my_mapping_explicit.is_contiguous() ? 1 : 0, contiguous ? 1 : 0);
113 EXPECT_EQ(my_mapping_explicit.is_strided() ? 1 : 0, strided ? 1 : 0);
114 EXPECT_EQ(my_mapping_copy.is_always_unique() ? 1 : 0, always_unique ? 1 : 0);
115 EXPECT_EQ(my_mapping_copy.is_always_contiguous() ? 1 : 0, always_contiguous ? 1 : 0);
116 EXPECT_EQ(my_mapping_copy.is_always_strided() ? 1 : 0, always_strided ? 1 : 0);
117 EXPECT_EQ(my_mapping_copy.is_unique() ? 1 : 0, unique ? 1 : 0);
118 EXPECT_EQ(my_mapping_copy.is_contiguous() ? 1 : 0, contiguous ? 1 : 0);
119 EXPECT_EQ(my_mapping_copy.is_strided() ? 1 : 0, strided ? 1 : 0);
122 template<class ... E>
123 void check_operator(ptrdiff_t offset, E ... e)
125 EXPECT_EQ(my_mapping_explicit(e ...), offset);
126 EXPECT_EQ(my_mapping_copy(e ...), offset);
131 TEST(LayoutTests, LayoutRightConstruction) {
132 LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
134 test.check_rank(5);
135 test.check_rank_dynamic(2);
136 test.check_extents(5, 4, 3, 2, 1);
137 test.check_strides(24, 6, 2, 1, 1);
140 TEST(LayoutTests, LayoutRightProperties) {
141 LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
143 test.check_properties(true, true, true, true, true, true);
146 TEST(LayoutTests, LayoutRightOperator) {
147 LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
149 test.check_operator(107, 4, 1, 2, 1, 0);
150 test.check_operator(0, 0, 0, 0, 0, 0);
153 } // namespace gmx