Update instructions in containers.rst
[gromacs.git] / src / gromacs / mdspan / tests / extensions.cpp
blob5ace1e7f590d941f36f446a58d692ad17648d7d6
1 /*
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.
35 /*! \internal \file
36 * \brief
37 * Tests gromacs extensions to mdspan proposal
39 * \author Christian Blau <cblau@gwdg.de>
40 * \ingroup module_mdspan
42 #include "gmxpre.h"
44 #include "gromacs/mdspan/extensions.h"
46 #include <string>
47 #include <vector>
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
52 #include "gromacs/mdspan/mdspan.h"
54 #include "testutils/testasserts.h"
56 namespace gmx
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{ data.data() };
63 EXPECT_EQ(span[0][0], 1);
64 EXPECT_EQ(span[0][1], 2);
66 EXPECT_EQ(span[1][0], 3);
67 EXPECT_EQ(span[1][1], 4);
69 EXPECT_EQ(span[2][0], 5);
70 EXPECT_EQ(span[2][1], 6);
73 TEST(MdSpanExtension, SlicingDynamic)
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 };
78 EXPECT_EQ(span[0][0], 1);
79 EXPECT_EQ(span[0][1], 2);
81 EXPECT_EQ(span[1][0], 3);
82 EXPECT_EQ(span[1][1], 4);
84 EXPECT_EQ(span[2][0], 5);
85 EXPECT_EQ(span[2][1], 6);
88 TEST(MdSpanExtension, SlicingAllStatic3D)
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);
95 EXPECT_EQ(span[0][1][0], 3);
96 EXPECT_EQ(span[0][1][1], 4);
98 EXPECT_EQ(span[1][0][0], 5);
99 EXPECT_EQ(span[1][0][1], 6);
101 EXPECT_EQ(span[1][1][0], 7);
102 EXPECT_EQ(span[1][1][1], 8);
105 TEST(MdSpanExtension, SlicingEqualsView3D)
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));
119 TEST(MdSpanExtension, additionWorks)
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() };
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);
132 TEST(MdSpanExtension, subtractionWorks)
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() };
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);
145 TEST(MdSpanExtension, multiplicationWorks)
147 std::array<int, 2 * 2 * 2> arr1 = { { -4, -3, -2, -1, 0, 1, 2, 3 } };
148 std::array<int, 2 * 2 * 2> arr2 = { {
156 } };
157 basic_mdspan<int, extents<2, 2, 2>> span1{ arr1.data() };
158 basic_mdspan<int, extents<2, 2, 2>> span2{ arr2.data() };
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);
166 TEST(MdSpanExtension, divisionWorks)
168 std::array<float, 2 * 2 * 2> arr1 = { { -4, -3, -2, -1, 0, 1, 2, 3 } };
169 std::array<float, 2 * 2 * 2> arr2 = { {
178 } };
179 basic_mdspan<float, extents<2, 2, 2>> span1{ arr1.data() };
180 basic_mdspan<float, extents<2, 2, 2>> span2{ arr2.data() };
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);
187 } // namespace gmx