Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / mdspan / tests / extents.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 /*
36  * This file is a modified version of original work of Sandia Corporation.
37  * In the spirit of the original code, this particular file can be distributed
38  * on the terms of Sandia Corporation.
39  */
40 /*
41  *                         Kokkos v. 2.0
42  *               Copyright (2014) Sandia Corporation
43  *
44  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
45  * the U.S. Government retains certain rights in this software.
46  *
47  * Kokkos is licensed under 3-clause BSD terms of use:
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions are
51  * met:
52  *
53  * 1. Redistributions of source code must retain the above copyright
54  * notice, this list of conditions and the following disclaimer.
55  *
56  * 2. Redistributions in binary form must reproduce the above copyright
57  * notice, this list of conditions and the following disclaimer in the
58  * documentation and/or other materials provided with the distribution.
59  *
60  * 3. Neither the name of the Corporation nor the names of the
61  * contributors may be used to endorse or promote products derived from
62  * this software without specific prior written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
65  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
68  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
69  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
70  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
71  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
72  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
74  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75  *
76  * Questions? Contact Christian R. Trott (crtrott@sandia.gov)
77  */
78 /*! \internal \file
79  * \brief Testing gmx::extents.
80  *
81  * \author Christian Trott <crtrott@sandia.gov>
82  * \author Carter Edwards <hedwards@nvidia.com>
83  * \author David Hollman <dshollm@sandia.gov>
84  * \author Christian Blau <cblau@gwdg.de>
85  */
86 #include "gmxpre.h"
87
88 #include "gromacs/mdspan/extents.h"
89
90 #include <gtest/gtest.h>
91
92 namespace gmx
93 {
94
95 template<ptrdiff_t... E_STATIC>
96 class ExtentsTest
97 {
98 public:
99     using extents_type = gmx::extents<E_STATIC...>;
100
101     extents_type my_extents_explicit;
102     extents_type my_extents_array;
103     extents_type my_extents_copy;
104
105     ExtentsTest()
106     {
107         my_extents_explicit = extents<E_STATIC...>();
108         my_extents_array    = extents<E_STATIC...>(std::array<ptrdiff_t, 0>());
109         my_extents_copy     = extents<E_STATIC...>(my_extents_explicit);
110     }
111
112     template<class... E>
113     ExtentsTest(E... e)
114     {
115         my_extents_explicit = extents<E_STATIC...>(e...);
116         my_extents_array    = extents<E_STATIC...>(std::array<ptrdiff_t, 2>({ { e... } }));
117         my_extents_copy     = extents<E_STATIC...>(my_extents_explicit);
118     }
119
120     void check_rank(ptrdiff_t r)
121     {
122         EXPECT_EQ(my_extents_explicit.rank(), r);
123         EXPECT_EQ(my_extents_array.rank(), r);
124         EXPECT_EQ(my_extents_copy.rank(), r);
125     }
126     void check_rank_dynamic(ptrdiff_t r)
127     {
128         EXPECT_EQ(my_extents_explicit.rank_dynamic(), r);
129         EXPECT_EQ(my_extents_array.rank_dynamic(), r);
130         EXPECT_EQ(my_extents_copy.rank_dynamic(), r);
131     }
132     template<class... E>
133     void check_extents(E... e)
134     {
135         std::array<ptrdiff_t, extents_type::rank()> s = { { E_STATIC... } };
136         std::array<ptrdiff_t, extents_type::rank()> a = { { e... } };
137         for (size_t r = 0; r < extents_type::rank(); r++)
138         {
139             EXPECT_EQ(my_extents_explicit.static_extent(r), s[r]);
140             EXPECT_EQ(my_extents_explicit.extent(r), a[r]);
141
142             EXPECT_EQ(my_extents_array.static_extent(r), s[r]);
143             EXPECT_EQ(my_extents_array.extent(r), a[r]);
144
145             EXPECT_EQ(my_extents_copy.static_extent(r), s[r]);
146             EXPECT_EQ(my_extents_copy.extent(r), a[r]);
147         }
148         EXPECT_EQ(my_extents_explicit.static_extent(extents_type::rank() + 1), 1);
149         EXPECT_EQ(my_extents_explicit.extent(extents_type::rank() + 1), 1);
150
151         EXPECT_EQ(my_extents_array.static_extent(extents_type::rank() + 1), 1);
152         EXPECT_EQ(my_extents_array.extent(extents_type::rank() + 1), 1);
153
154         EXPECT_EQ(my_extents_copy.static_extent(extents_type::rank() + 1), 1);
155         EXPECT_EQ(my_extents_copy.extent(extents_type::rank() + 1), 1);
156     }
157 };
158
159 TEST(ExtentsTest, Construction)
160 {
161
162     // setting two dynamic extents
163     ExtentsTest<5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
164
165     test.check_rank(5);
166     test.check_rank_dynamic(2);
167     test.check_extents(5, 4, 3, 2, 1);
168 }
169
170 TEST(ExtentsTest, PurelyStatic)
171 {
172     ExtentsTest<5, 4, 3> test;
173     test.check_rank(3);
174     test.check_rank_dynamic(0);
175     test.check_extents(5, 4, 3);
176 }
177
178 TEST(ExtentsTest, RankNought)
179 {
180     // Can construct extents of rank nought
181     ExtentsTest<> test;
182     test.check_rank(0);
183     test.check_rank_dynamic(0);
184 }
185 TEST(ExtentsTest, Assignment)
186 {
187     extents<5, dynamic_extent, 3, dynamic_extent, 1> e1(4, 2);
188     extents<5, 4, 3, 2, 1>                           e2;
189     e2 = e1;
190     for (size_t r = 0; r < 5; r++)
191     {
192         EXPECT_EQ(e2.extent(r), e1.extent(r));
193     }
194     extents<dynamic_extent, dynamic_extent, dynamic_extent, dynamic_extent, dynamic_extent> e3(
195             9, 8, 7, 6, 5);
196     for (int r = 0; r < 5; r++)
197     {
198         EXPECT_EQ(e3.extent(r), 9 - r);
199     }
200     e3 = e1;
201     for (int r = 0; r < 5; r++)
202     {
203         EXPECT_EQ(e3.extent(r), e1.extent(r));
204     }
205 }
206 } // namespace gmx