8b68b114e00982f34dbc7d403348a30ab24a64e6
[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 {
63         data.data()
64     };
65     EXPECT_EQ(span[0][0], 1);
66     EXPECT_EQ(span[0][1], 2);
67
68     EXPECT_EQ(span[1][0], 3);
69     EXPECT_EQ(span[1][1], 4);
70
71     EXPECT_EQ(span[2][0], 5);
72     EXPECT_EQ(span[2][1], 6);
73 }
74
75 TEST(MdSpanExtension, SlicingDynamic)
76 {
77     std::array<int, 2 * 3> data = {1, 2, 3, 4, 5, 6};
78     basic_mdspan<int, extents<dynamic_extent, dynamic_extent> > span {
79         data.data(), 2, 2
80     };
81
82     EXPECT_EQ(span[0][0], 1);
83     EXPECT_EQ(span[0][1], 2);
84
85     EXPECT_EQ(span[1][0], 3);
86     EXPECT_EQ(span[1][1], 4);
87
88     EXPECT_EQ(span[2][0], 5);
89     EXPECT_EQ(span[2][1], 6);
90 }
91
92 TEST(MdSpanExtension, SlicingAllStatic3D)
93 {
94     std::array<int, 2 * 2* 2> data = {1, 2, 3, 4, 5, 6, 7, 8 };
95     basic_mdspan<int, extents<2, 2, 2> >
96     span {
97         data.data()
98     };
99     EXPECT_EQ(span[0][0][0], 1);
100     EXPECT_EQ(span[0][0][1], 2);
101
102     EXPECT_EQ(span[0][1][0], 3);
103     EXPECT_EQ(span[0][1][1], 4);
104
105     EXPECT_EQ(span[1][0][0], 5);
106     EXPECT_EQ(span[1][0][1], 6);
107
108     EXPECT_EQ(span[1][1][0], 7);
109     EXPECT_EQ(span[1][1][1], 8);
110
111 }
112
113 TEST(MdSpanExtension, SlicingEqualsView3D)
114 {
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 {
117         data.data()
118     };
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));
127 }
128
129 } // namespace gmx