Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / mdspan / tests / layouts.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Testing gmx::extents.
37  *
38  * \author Christian Trott <crtrott@sandia.gov>
39  * \author David Hollman <dshollm@sandia.gov>
40  * \author Christian Blau <cblau@gwdg.de>
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/mdspan/layouts.h"
45
46 #include <gtest/gtest.h>
47
48 #include "gromacs/mdspan/extents.h"
49
50 namespace gmx
51 {
52
53 template<class Layout, ptrdiff_t... E_STATIC>
54 struct LayoutTests
55 {
56
57     typedef Layout                                          layout_type;
58     typedef extents<E_STATIC...>                            extents_type;
59     typedef typename Layout::template mapping<extents_type> mapping_type;
60
61     mapping_type my_mapping_explicit, my_mapping_copy;
62
63     template<class... E>
64     LayoutTests(E... e)
65     {
66         my_mapping_explicit = mapping_type(extents_type(e...));
67         my_mapping_copy     = mapping_type(my_mapping_explicit);
68     }
69
70     void check_rank(ptrdiff_t r)
71     {
72         EXPECT_EQ(my_mapping_explicit.extents().rank(), r);
73         EXPECT_EQ(my_mapping_copy.extents().rank(), r);
74     }
75     void check_rank_dynamic(ptrdiff_t r)
76     {
77         EXPECT_EQ(my_mapping_explicit.extents().rank_dynamic(), r);
78         EXPECT_EQ(my_mapping_copy.extents().rank_dynamic(), r);
79     }
80     template<class... E>
81     void check_extents(E... e)
82     {
83         std::array<ptrdiff_t, extents_type::rank()> a = { { e... } };
84         for (size_t r = 0; r < extents_type::rank(); r++)
85         {
86             EXPECT_EQ(my_mapping_explicit.extents().extent(r), a[r]);
87             EXPECT_EQ(my_mapping_copy.extents().extent(r), a[r]);
88         }
89     }
90     template<class... E>
91     void check_strides(E... e)
92     {
93         std::array<ptrdiff_t, extents_type::rank()> a = { { e... } };
94         for (size_t r = 0; r < extents_type::rank(); r++)
95         {
96             EXPECT_EQ(my_mapping_explicit.stride(r), a[r]);
97             EXPECT_EQ(my_mapping_copy.stride(r), a[r]);
98         }
99     }
100     void check_required_span_size(ptrdiff_t size)
101     {
102         EXPECT_EQ(my_mapping_explicit.required_span_size(), size);
103         EXPECT_EQ(my_mapping_copy.required_span_size(), size);
104     }
105
106     void check_properties(bool always_unique,
107                           bool always_contiguous,
108                           bool always_strided,
109                           bool unique,
110                           bool contiguous,
111                           bool strided)
112     {
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);
125     }
126
127     template<class... E>
128     void check_operator(ptrdiff_t offset, E... e)
129     {
130         EXPECT_EQ(my_mapping_explicit(e...), offset);
131         EXPECT_EQ(my_mapping_copy(e...), offset);
132     }
133 };
134
135 TEST(LayoutTests, LayoutRightConstruction)
136 {
137     LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
138
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);
143 }
144
145 TEST(LayoutTests, LayoutRightProperties)
146 {
147     LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
148
149     test.check_properties(true, true, true, true, true, true);
150 }
151
152 TEST(LayoutTests, LayoutRightOperator)
153 {
154     LayoutTests<layout_right, 5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
155
156     test.check_operator(107, 4, 1, 2, 1, 0);
157     test.check_operator(0, 0, 0, 0, 0, 0);
158 }
159
160 } // namespace gmx