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.
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.
42 * Copyright (2014) Sandia Corporation
44 * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
45 * the U.S. Government retains certain rights in this software.
47 * Kokkos is licensed under 3-clause BSD terms of use:
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions are
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
76 * Questions? Contact Christian R. Trott (crtrott@sandia.gov)
79 * \brief Testing gmx::extents.
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>
88 #include "gromacs/mdspan/extents.h"
90 #include <gtest/gtest.h>
95 template<ptrdiff_t... E_STATIC>
99 using extents_type = gmx::extents<E_STATIC...>;
101 extents_type my_extents_explicit;
102 extents_type my_extents_array;
103 extents_type my_extents_copy;
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);
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);
120 void check_rank(ptrdiff_t r)
122 EXPECT_EQ(my_extents_explicit.rank(), r);
123 EXPECT_EQ(my_extents_array.rank(), r);
124 EXPECT_EQ(my_extents_copy.rank(), r);
126 void check_rank_dynamic(ptrdiff_t r)
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);
133 void check_extents(E... e)
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++)
139 EXPECT_EQ(my_extents_explicit.static_extent(r), s[r]);
140 EXPECT_EQ(my_extents_explicit.extent(r), a[r]);
142 EXPECT_EQ(my_extents_array.static_extent(r), s[r]);
143 EXPECT_EQ(my_extents_array.extent(r), a[r]);
145 EXPECT_EQ(my_extents_copy.static_extent(r), s[r]);
146 EXPECT_EQ(my_extents_copy.extent(r), a[r]);
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);
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);
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);
159 TEST(ExtentsTest, Construction)
162 // setting two dynamic extents
163 ExtentsTest<5, dynamic_extent, 3, dynamic_extent, 1> test(4, 2);
166 test.check_rank_dynamic(2);
167 test.check_extents(5, 4, 3, 2, 1);
170 TEST(ExtentsTest, PurelyStatic)
172 ExtentsTest<5, 4, 3> test;
174 test.check_rank_dynamic(0);
175 test.check_extents(5, 4, 3);
178 TEST(ExtentsTest, RankNought)
180 // Can construct extents of rank nought
183 test.check_rank_dynamic(0);
185 TEST(ExtentsTest, Assignment)
187 extents<5, dynamic_extent, 3, dynamic_extent, 1> e1(4, 2);
188 extents<5, 4, 3, 2, 1> e2;
190 for (size_t r = 0; r < 5; r++)
192 EXPECT_EQ(e2.extent(r), e1.extent(r));
194 extents<dynamic_extent, dynamic_extent, dynamic_extent, dynamic_extent, dynamic_extent> e3(
196 for (int r = 0; r < 5; r++)
198 EXPECT_EQ(e3.extent(r), 9 - r);
201 for (int r = 0; r < 5; r++)
203 EXPECT_EQ(e3.extent(r), e1.extent(r));