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.
36 * \brief Tests for gmx::FixedCapacityVector
38 * \author Berk Hess <hess@kth.se>
39 * \ingroup module_utility
43 #include "gromacs/utility/fixedcapacityvector.h"
45 #include <gtest/gtest.h>
47 #include "gromacs/utility/basedefinitions.h"
55 TEST(FixedCapacityVectorTest, IsEmpty)
57 FixedCapacityVector<int, 2> empty;
59 EXPECT_EQ(0U, empty.size());
60 EXPECT_TRUE(empty.empty());
63 TEST(FixedCapacityVectorTest, PushWorks)
65 FixedCapacityVector<int, 2> v;
68 EXPECT_EQ(1U, v.size());
72 EXPECT_EQ(2U, v.size());
77 TEST(FixedCapacityVectorTest, PopWorks)
79 FixedCapacityVector<int, 3> v;
82 EXPECT_EQ(1U, v.size());
85 EXPECT_EQ(0U, v.size());
86 EXPECT_TRUE(v.empty());
89 TEST(FixedCapacityVectorTest, ClearWorks)
91 FixedCapacityVector<int, 3> v;
94 EXPECT_EQ(1U, v.size());
97 EXPECT_EQ(0U, v.size());
98 EXPECT_TRUE(v.empty());
101 TEST(FixedCapacityVectorTest, EmplaceBackWorks)
103 FixedCapacityVector<std::vector<int>, 2> v;
105 const auto& elem = v.emplace_back(5);
106 EXPECT_EQ(1U, v.size());
107 EXPECT_EQ(5U, elem.size());
110 TEST(FixedCapacityVectorTest, AtThrows)
112 FixedCapacityVector<int, 3> v;
115 EXPECT_EQ(8, v.at(0));
117 EXPECT_THROW(v.at(1), std::out_of_range);
120 TEST(FixedCapacityVectorTest, IteratorWorks)
122 FixedCapacityVector<int, 4> v;
124 std::vector<int> ref = { 7, 4, 5 };
126 for (auto elem : ref)
130 EXPECT_EQ(3U, v.size());
132 std::vector<int> loopResult;
135 loopResult.push_back(elem);
137 EXPECT_EQ(3U, loopResult.size());
138 EXPECT_EQ(ref[0], loopResult[0]);
139 EXPECT_EQ(ref[1], loopResult[1]);
140 EXPECT_EQ(ref[2], loopResult[2]);
143 TEST(FixedCapacityVectorTest, ReverseIteratorWorks)
145 FixedCapacityVector<int, 4> v;
147 std::vector<int> ref = { 7, 4, 5 };
149 for (auto elem : ref)
153 EXPECT_EQ(3U, v.size());
155 std::vector<int> loopResult;
156 for (auto it = v.rbegin(); it != v.rend(); ++it)
158 loopResult.push_back(*it);
160 EXPECT_EQ(3U, loopResult.size());
161 EXPECT_EQ(ref[0], loopResult[2]);
162 EXPECT_EQ(ref[1], loopResult[1]);
163 EXPECT_EQ(ref[2], loopResult[0]);
166 TEST(FixedCapacityVectorTest, ZeroCapacityWorks)
168 FixedCapacityVector<int, 0> v;
170 EXPECT_EQ(0U, v.size());
171 EXPECT_TRUE(v.empty());