clang-tidy: google tests applicable
[gromacs.git] / src / gromacs / taskassignment / tests / usergpuids.cpp
blob066336746cc863a5fec6c5bee131b1168ffd9a78
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017,2018, 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 for NonbondedOnGpuFromUser
39 * \author Mark Abraham <mark.j.abraham@gmail.com>
40 * \ingroup module_taskassignment
42 #include "gmxpre.h"
44 #include "gromacs/taskassignment/usergpuids.h"
46 #include <string>
47 #include <vector>
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
52 #include "gromacs/utility/exceptions.h"
54 namespace gmx
57 namespace test
59 namespace
62 TEST(GpuIdStringHandlingTest, ParsingAndReconstructionWork)
64 using ::testing::UnorderedElementsAreArray;
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>.
70 // Test simple assignments and back mappings
72 const char *strings[] = { "01", "0,1", "0,1," };
73 for (const auto &s : strings)
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));
83 // Test an input that could be a single large index, or two small indices; and back mappings
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));
92 // Test an input that must be a single large index; and back mappings
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));
101 // Test multiple large indices; and back mappings
103 const char *strings[] = { "11,12", "11,12," };
104 for (const auto &s : strings)
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));
116 TEST(GpuIdStringHandlingTest, EmptyStringCanBeValid)
118 using ::testing::IsEmpty;
120 auto assignment = parseUserGpuIds("");
121 EXPECT_THAT(assignment, IsEmpty());
122 EXPECT_EQ("", makeGpuIdString(assignment, 0));
125 TEST(GpuIdStringHandlingTest, InvalidInputsThrow)
128 const char *strings[] = {
129 "a", "0a", ",01", ",0,1", ",0,1,",
130 ":0", "0a:1b", "0:1:2",
131 ",", ";", ":", "-", "=",
133 for (const auto &s : strings)
135 EXPECT_THROW(parseUserGpuIds(s), InvalidInputError) << "for string " << s;
140 } // namespace
141 } // namespace test
142 } // namespace gmx