Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / mdspan / tests / extensions.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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
37  * Tests gromacs extensions to mdspan proposal
38  *
39  * \author Christian Blau <cblau@gwdg.de>
40  * \ingroup module_mdspan
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/mdspan/extensions.h"
45
46 #include <string>
47 #include <vector>
48
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
51
52 #include "gromacs/mdspan/mdspan.h"
53
54 #include "testutils/testasserts.h"
55
56 namespace gmx
57 {
58
59 TEST(MdSpanExtension, SlicingAllStatic)
60 {
61     std::array<int, 2 * 3>           data = { 1, 2, 3, 4, 5, 6 };
62     basic_mdspan<int, extents<3, 2>> span{ data.data() };
63     EXPECT_EQ(span[0][0], 1);
64     EXPECT_EQ(span[0][1], 2);
65
66     EXPECT_EQ(span[1][0], 3);
67     EXPECT_EQ(span[1][1], 4);
68
69     EXPECT_EQ(span[2][0], 5);
70     EXPECT_EQ(span[2][1], 6);
71 }
72
73 TEST(MdSpanExtension, SlicingDynamic)
74 {
75     std::array<int, 2 * 3>                                     data = { 1, 2, 3, 4, 5, 6 };
76     basic_mdspan<int, extents<dynamic_extent, dynamic_extent>> span{ data.data(), 2, 2 };
77
78     EXPECT_EQ(span[0][0], 1);
79     EXPECT_EQ(span[0][1], 2);
80
81     EXPECT_EQ(span[1][0], 3);
82     EXPECT_EQ(span[1][1], 4);
83
84     EXPECT_EQ(span[2][0], 5);
85     EXPECT_EQ(span[2][1], 6);
86 }
87
88 TEST(MdSpanExtension, SlicingAllStatic3D)
89 {
90     std::array<int, 2 * 2 * 2>          data = { 1, 2, 3, 4, 5, 6, 7, 8 };
91     basic_mdspan<int, extents<2, 2, 2>> span{ data.data() };
92     EXPECT_EQ(span[0][0][0], 1);
93     EXPECT_EQ(span[0][0][1], 2);
94
95     EXPECT_EQ(span[0][1][0], 3);
96     EXPECT_EQ(span[0][1][1], 4);
97
98     EXPECT_EQ(span[1][0][0], 5);
99     EXPECT_EQ(span[1][0][1], 6);
100
101     EXPECT_EQ(span[1][1][0], 7);
102     EXPECT_EQ(span[1][1][1], 8);
103 }
104
105 TEST(MdSpanExtension, SlicingEqualsView3D)
106 {
107     std::array<int, 2 * 2 * 2>          data = { 1, 2, 3, 4, 5, 6, 7, 8 };
108     basic_mdspan<int, extents<2, 2, 2>> span{ data.data() };
109     EXPECT_EQ(span[0][0][0], span(0, 0, 0));
110     EXPECT_EQ(span[0][0][1], span(0, 0, 1));
111     EXPECT_EQ(span[0][1][0], span(0, 1, 0));
112     EXPECT_EQ(span[0][1][1], span(0, 1, 1));
113     EXPECT_EQ(span[1][0][0], span(1, 0, 0));
114     EXPECT_EQ(span[1][0][1], span(1, 0, 1));
115     EXPECT_EQ(span[1][1][0], span(1, 1, 0));
116     EXPECT_EQ(span[1][1][1], span(1, 1, 1));
117 }
118
119 TEST(MdSpanExtension, additionWorks)
120 {
121     std::array<int, 2 * 2 * 2>          arr1 = { { -4, -3, -2, -1, 0, 1, 2, 3 } };
122     std::array<int, 2 * 2 * 2>          arr2 = { { 1, 1, 1, 1, 1, 1, 1, 1 } };
123     basic_mdspan<int, extents<2, 2, 2>> span1{ arr1.data() };
124     basic_mdspan<int, extents<2, 2, 2>> span2{ arr2.data() };
125
126     auto result = addElementwise(span1, span2);
127     EXPECT_EQ(result[0][0][1], -2);
128     EXPECT_EQ(result[0][1][0], -1);
129     EXPECT_EQ(result[1][0][0], 1);
130 }
131
132 TEST(MdSpanExtension, subtractionWorks)
133 {
134     std::array<int, 2 * 2 * 2>          arr1 = { { -4, -3, -2, -1, 0, 1, 2, 3 } };
135     std::array<int, 2 * 2 * 2>          arr2 = { { 1, 1, 1, 1, 1, 1, 1, 1 } };
136     basic_mdspan<int, extents<2, 2, 2>> span1{ arr1.data() };
137     basic_mdspan<int, extents<2, 2, 2>> span2{ arr2.data() };
138
139     auto result = subtractElementwise(span1, span2);
140     EXPECT_EQ(result[0][0][1], -4);
141     EXPECT_EQ(result[0][1][0], -3);
142     EXPECT_EQ(result[1][0][0], -1);
143 }
144
145 TEST(MdSpanExtension, multiplicationWorks)
146 {
147     std::array<int, 2 * 2 * 2>          arr1 = { { -4, -3, -2, -1, 0, 1, 2, 3 } };
148     std::array<int, 2 * 2 * 2>          arr2 = { {
149             2,
150             2,
151             2,
152             2,
153             2,
154             2,
155             2,
156     } };
157     basic_mdspan<int, extents<2, 2, 2>> span1{ arr1.data() };
158     basic_mdspan<int, extents<2, 2, 2>> span2{ arr2.data() };
159
160     auto result = multiplyElementwise(span1, span2);
161     EXPECT_EQ(result[0][0][1], -6);
162     EXPECT_EQ(result[0][1][0], -4);
163     EXPECT_EQ(result[1][0][0], 0);
164 }
165
166 TEST(MdSpanExtension, divisionWorks)
167 {
168     std::array<float, 2 * 2 * 2>          arr1 = { { -4, -3, -2, -1, 0, 1, 2, 3 } };
169     std::array<float, 2 * 2 * 2>          arr2 = { {
170             2,
171             2,
172             2,
173             2,
174             2,
175             2,
176             2,
177             2,
178     } };
179     basic_mdspan<float, extents<2, 2, 2>> span1{ arr1.data() };
180     basic_mdspan<float, extents<2, 2, 2>> span2{ arr2.data() };
181
182     auto result = divideElementwise(span1, span2);
183     EXPECT_EQ(result[0][0][1], -1.5);
184     EXPECT_EQ(result[0][1][0], -1);
185     EXPECT_EQ(result[1][0][0], 0);
186 }
187 } // namespace gmx