2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
37 * Tests gromacs extensions to mdspan proposal
39 * \author Christian Blau <cblau@gwdg.de>
40 * \ingroup module_mdspan
44 #include "gromacs/mdspan/extensions.h"
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
52 #include "gromacs/mdspan/mdspan.h"
54 #include "testutils/testasserts.h"
59 TEST(MdSpanExtension
, SlicingAllStatic
)
61 std::array
<int, 2*3> data
= {1, 2, 3, 4, 5, 6};
62 basic_mdspan
<int, extents
<3, 2> > span
{
65 EXPECT_EQ(span
[0][0], 1);
66 EXPECT_EQ(span
[0][1], 2);
68 EXPECT_EQ(span
[1][0], 3);
69 EXPECT_EQ(span
[1][1], 4);
71 EXPECT_EQ(span
[2][0], 5);
72 EXPECT_EQ(span
[2][1], 6);
75 TEST(MdSpanExtension
, SlicingDynamic
)
77 std::array
<int, 2 * 3> data
= {1, 2, 3, 4, 5, 6};
78 basic_mdspan
<int, extents
<dynamic_extent
, dynamic_extent
> > span
{
82 EXPECT_EQ(span
[0][0], 1);
83 EXPECT_EQ(span
[0][1], 2);
85 EXPECT_EQ(span
[1][0], 3);
86 EXPECT_EQ(span
[1][1], 4);
88 EXPECT_EQ(span
[2][0], 5);
89 EXPECT_EQ(span
[2][1], 6);
92 TEST(MdSpanExtension
, SlicingAllStatic3D
)
94 std::array
<int, 2 * 2* 2> data
= {1, 2, 3, 4, 5, 6, 7, 8 };
95 basic_mdspan
<int, extents
<2, 2, 2> >
99 EXPECT_EQ(span
[0][0][0], 1);
100 EXPECT_EQ(span
[0][0][1], 2);
102 EXPECT_EQ(span
[0][1][0], 3);
103 EXPECT_EQ(span
[0][1][1], 4);
105 EXPECT_EQ(span
[1][0][0], 5);
106 EXPECT_EQ(span
[1][0][1], 6);
108 EXPECT_EQ(span
[1][1][0], 7);
109 EXPECT_EQ(span
[1][1][1], 8);
113 TEST(MdSpanExtension
, SlicingEqualsView3D
)
115 std::array
<int, 2 * 2 * 2> data
= {1, 2, 3, 4, 5, 6, 7, 8};
116 basic_mdspan
<int, extents
<2, 2, 2> > span
{
119 EXPECT_EQ(span
[0][0][0], span(0, 0, 0));
120 EXPECT_EQ(span
[0][0][1], span(0, 0, 1));
121 EXPECT_EQ(span
[0][1][0], span(0, 1, 0));
122 EXPECT_EQ(span
[0][1][1], span(0, 1, 1));
123 EXPECT_EQ(span
[1][0][0], span(1, 0, 0));
124 EXPECT_EQ(span
[1][0][1], span(1, 0, 1));
125 EXPECT_EQ(span
[1][1][0], span(1, 1, 0));
126 EXPECT_EQ(span
[1][1][1], span(1, 1, 1));
129 TEST(MdSpanExtension
, additionWorks
)
131 std::array
<int, 2 * 2 * 2> arr1
= {{-4, -3, -2, -1, 0, 1, 2, 3}};
132 std::array
<int, 2 * 2 * 2> arr2
= {{1, 1, 1, 1, 1, 1, 1, 1}};
133 basic_mdspan
<int, extents
<2, 2, 2> > span1
{
136 basic_mdspan
<int, extents
<2, 2, 2> > span2
{
140 auto result
= addElementwise(span1
, span2
);
141 EXPECT_EQ(result
[0][0][1], -2);
142 EXPECT_EQ(result
[0][1][0], -1);
143 EXPECT_EQ(result
[1][0][0], 1);
146 TEST(MdSpanExtension
, subtractionWorks
)
148 std::array
<int, 2 * 2 * 2> arr1
= {{-4, -3, -2, -1, 0, 1, 2, 3}};
149 std::array
<int, 2 * 2 * 2> arr2
= {{1, 1, 1, 1, 1, 1, 1, 1}};
150 basic_mdspan
<int, extents
<2, 2, 2> > span1
{
153 basic_mdspan
<int, extents
<2, 2, 2> > span2
{
157 auto result
= subtractElementwise(span1
, span2
);
158 EXPECT_EQ(result
[0][0][1], -4);
159 EXPECT_EQ(result
[0][1][0], -3);
160 EXPECT_EQ(result
[1][0][0], -1);
163 TEST(MdSpanExtension
, multiplicationWorks
)
165 std::array
<int, 2 * 2 * 2> arr1
= {{-4, -3, -2, -1, 0, 1, 2, 3}};
166 std::array
<int, 2 * 2 * 2> arr2
= {{2, 2, 2, 2, 2, 2, 2, }};
167 basic_mdspan
<int, extents
<2, 2, 2> > span1
{
170 basic_mdspan
<int, extents
<2, 2, 2> > span2
{
174 auto result
= multiplyElementwise(span1
, span2
);
175 EXPECT_EQ(result
[0][0][1], -6);
176 EXPECT_EQ(result
[0][1][0], -4);
177 EXPECT_EQ(result
[1][0][0], 0);
180 TEST(MdSpanExtension
, divisionWorks
)
182 std::array
<float, 2 * 2 * 2> arr1
= {{-4, -3, -2, -1, 0, 1, 2, 3}};
183 std::array
<float, 2 * 2 * 2> arr2
= {{2, 2, 2, 2, 2, 2, 2, 2, }};
184 basic_mdspan
<float, extents
<2, 2, 2> > span1
{
187 basic_mdspan
<float, extents
<2, 2, 2> > span2
{
191 auto result
= divideElementwise(span1
, span2
);
192 EXPECT_EQ(result
[0][0][1], -1.5);
193 EXPECT_EQ(result
[0][1][0], -1);
194 EXPECT_EQ(result
[1][0][0], 0);