c4756770dd7b5fabed710260dc44dab661ff48ec
[alexxy/gromacs.git] / src / gromacs / taskassignment / tests / usergpuids.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017, 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 for NonbondedOnGpuFromUser
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \ingroup module_taskassignment
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/taskassignment/usergpuids.h"
45
46 #include <string>
47 #include <vector>
48
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
51
52 #include "gromacs/utility/exceptions.h"
53
54 namespace gmx
55 {
56
57 namespace test
58 {
59 namespace
60 {
61
62 TEST(GpuIdStringHandlingTest, ParsingAndReconstructionWork)
63 {
64     using ::testing::UnorderedElementsAreArray;
65
66     // TODO It would be nicer to use EXPECT_THAT(assignment,
67     // UnorderedElementsAreArray({0,1}) but MSVC 2015 does
68     // not deduce the template arguments for <int, 2>.
69
70     // Test simple assignments and back mappings
71     {
72         const char *strings[] = { "01", "0,1", "0,1," };
73         for (const auto &s : strings)
74         {
75             auto assignment = parseUserGpuIds(s);
76             auto matcher    = UnorderedElementsAreArray<int, 2>({0, 1});
77             EXPECT_THAT(assignment, matcher) << "for string " << s;
78             EXPECT_EQ("0",     makeGpuIdString(assignment, 1));
79             EXPECT_EQ("0,1",   makeGpuIdString(assignment, 2));
80             EXPECT_EQ("0,0,1", makeGpuIdString(assignment, 3));
81         }
82     }
83     // Test an input that could be a single large index, or two small indices; and back mappings
84     {
85         auto assignment = parseUserGpuIds("11");
86         auto matcher    = UnorderedElementsAreArray<int, 2>({1, 1});
87         EXPECT_THAT(assignment, matcher);
88         EXPECT_EQ("1",     makeGpuIdString(assignment, 1));
89         EXPECT_EQ("1,1",   makeGpuIdString(assignment, 2));
90         EXPECT_EQ("1,1,1", makeGpuIdString(assignment, 3));
91     }
92     // Test an input that must be a single large index; and back mappings
93     {
94         auto assignment = parseUserGpuIds("11,");
95         auto matcher    = UnorderedElementsAreArray<int, 1>({11});
96         EXPECT_THAT(assignment, matcher);
97         EXPECT_EQ("11",       makeGpuIdString(assignment, 1));
98         EXPECT_EQ("11,11",    makeGpuIdString(assignment, 2));
99         EXPECT_EQ("11,11,11", makeGpuIdString(assignment, 3));
100     }
101     // Test multiple large indices; and back mappings
102     {
103         const char *strings[] = { "11,12", "11,12," };
104         for (const auto &s : strings)
105         {
106             auto assignment = parseUserGpuIds(s);
107             auto matcher    = UnorderedElementsAreArray<int, 2>({11, 12});
108             EXPECT_THAT(assignment, matcher) << "for string " << s;
109             EXPECT_EQ("11",       makeGpuIdString(assignment, 1));
110             EXPECT_EQ("11,12",    makeGpuIdString(assignment, 2));
111             EXPECT_EQ("11,11,12", makeGpuIdString(assignment, 3));
112         }
113     }
114 }
115
116 TEST(GpuIdStringHandlingTest, EmptyStringCanBeValid)
117 {
118     using ::testing::IsEmpty;
119
120     auto assignment = parseUserGpuIds("");
121     EXPECT_THAT(assignment, IsEmpty());
122     EXPECT_EQ("", makeGpuIdString(assignment, 0));
123 }
124
125 TEST(GpuIdStringHandlingTest, InvalidInputsThrow)
126 {
127     {
128         const char *strings[] = {
129             "a", "0a", ",01", ",0,1", ",0,1,",
130             ":0", "0a:1b", "0:1:2",
131             ",", ";", ":", "-", "=",
132         };
133         for (const auto &s : strings)
134         {
135             EXPECT_THROW(parseUserGpuIds(s), InvalidInputError) << "for string " << s;
136         }
137     }
138 }
139
140 } // namespace
141 } // namespace
142 } // namespace