128-bit AVX2 SIMD for AMD Ryzen
[gromacs.git] / src / gromacs / options / tests / repeatingsection.cpp
blobfaf16c2e7282af58b9bd53dfa757355daf2393cd
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016, 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 functionality related to repeating sections.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
42 #include "gmxpre.h"
44 #include "gromacs/options/repeatingsection.h"
46 #include <vector>
48 #include <gtest/gtest.h>
50 #include "gromacs/options/basicoptions.h"
51 #include "gromacs/options/options.h"
52 #include "gromacs/options/optionsassigner.h"
54 #include "testutils/testasserts.h"
56 namespace
59 using gmx::RepeatingOptionSection;
61 struct SectionData
63 int value;
66 TEST(RepeatingOptionSectionTest, HandlesNoInstance)
68 std::vector<SectionData> values;
69 gmx::Options options;
70 auto sec = options.addSection(
71 RepeatingOptionSection<SectionData>("section")
72 .storeVector(&values));
73 using gmx::IntegerOption;
74 ASSERT_NO_THROW_GMX(sec.addOption(IntegerOption("p").store(&sec.bind().value)));
76 gmx::OptionsAssigner assigner(&options);
77 EXPECT_NO_THROW_GMX(assigner.start());
78 EXPECT_NO_THROW_GMX(assigner.finish());
79 EXPECT_NO_THROW_GMX(options.finish());
81 EXPECT_EQ(0U, values.size());
84 TEST(RepeatingOptionSectionTest, HandlesNoInstanceWithRequiredOption)
86 std::vector<SectionData> values;
87 gmx::Options options;
88 auto sec = options.addSection(
89 RepeatingOptionSection<SectionData>("section")
90 .storeVector(&values));
91 using gmx::IntegerOption;
92 ASSERT_NO_THROW_GMX(sec.addOption(IntegerOption("p").store(&sec.bind().value)
93 .required()));
95 gmx::OptionsAssigner assigner(&options);
96 EXPECT_NO_THROW_GMX(assigner.start());
97 EXPECT_NO_THROW_GMX(assigner.finish());
98 EXPECT_NO_THROW_GMX(options.finish());
100 EXPECT_EQ(0U, values.size());
103 TEST(RepeatingOptionSectionTest, HandlesSingleInstance)
105 std::vector<SectionData> values;
106 gmx::Options options;
107 auto sec = options.addSection(
108 RepeatingOptionSection<SectionData>("section")
109 .storeVector(&values));
110 using gmx::IntegerOption;
111 ASSERT_NO_THROW_GMX(sec.addOption(IntegerOption("p").store(&sec.bind().value)));
113 gmx::OptionsAssigner assigner(&options);
114 EXPECT_NO_THROW_GMX(assigner.start());
115 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
116 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
117 EXPECT_NO_THROW_GMX(assigner.appendValue("4"));
118 EXPECT_NO_THROW_GMX(assigner.finishOption());
119 EXPECT_NO_THROW_GMX(assigner.finishSection());
120 EXPECT_NO_THROW_GMX(assigner.finish());
121 EXPECT_NO_THROW_GMX(options.finish());
123 ASSERT_EQ(1U, values.size());
124 EXPECT_EQ(4, values[0].value);
127 TEST(RepeatingOptionSectionTest, HandlesDefaultValue)
129 std::vector<SectionData> values;
130 gmx::Options options;
131 auto sec = options.addSection(
132 RepeatingOptionSection<SectionData>("section")
133 .storeVector(&values));
134 using gmx::IntegerOption;
135 ASSERT_NO_THROW_GMX(sec.addOption(IntegerOption("p").store(&sec.bind().value)
136 .defaultValue(3)));
138 gmx::OptionsAssigner assigner(&options);
139 EXPECT_NO_THROW_GMX(assigner.start());
140 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
141 EXPECT_NO_THROW_GMX(assigner.finishSection());
142 EXPECT_NO_THROW_GMX(assigner.finish());
143 EXPECT_NO_THROW_GMX(options.finish());
145 ASSERT_EQ(1U, values.size());
146 EXPECT_EQ(3, values[0].value);
149 TEST(RepeatingOptionSectionTest, HandlesTwoInstances)
151 std::vector<SectionData> values;
152 gmx::Options options;
153 auto sec = options.addSection(
154 RepeatingOptionSection<SectionData>("section")
155 .storeVector(&values));
156 using gmx::IntegerOption;
157 ASSERT_NO_THROW_GMX(sec.addOption(IntegerOption("p").store(&sec.bind().value)));
159 gmx::OptionsAssigner assigner(&options);
160 EXPECT_NO_THROW_GMX(assigner.start());
161 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
162 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
163 EXPECT_NO_THROW_GMX(assigner.appendValue("4"));
164 EXPECT_NO_THROW_GMX(assigner.finishOption());
165 EXPECT_NO_THROW_GMX(assigner.finishSection());
166 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
167 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
168 EXPECT_NO_THROW_GMX(assigner.appendValue("5"));
169 EXPECT_NO_THROW_GMX(assigner.finishOption());
170 EXPECT_NO_THROW_GMX(assigner.finishSection());
171 EXPECT_NO_THROW_GMX(assigner.finish());
172 EXPECT_NO_THROW_GMX(options.finish());
174 ASSERT_EQ(2U, values.size());
175 EXPECT_EQ(4, values[0].value);
176 EXPECT_EQ(5, values[1].value);
179 TEST(RepeatingOptionSectionTest, HandlesUnsetOptionWithImplicitDefault)
181 std::vector<SectionData> values;
182 gmx::Options options;
183 auto sec = options.addSection(
184 RepeatingOptionSection<SectionData>("section")
185 .storeVector(&values));
186 using gmx::IntegerOption;
187 ASSERT_NO_THROW_GMX(sec.addOption(IntegerOption("p").store(&sec.bind().value)));
189 gmx::OptionsAssigner assigner(&options);
190 EXPECT_NO_THROW_GMX(assigner.start());
191 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
192 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
193 EXPECT_NO_THROW_GMX(assigner.appendValue("4"));
194 EXPECT_NO_THROW_GMX(assigner.finishOption());
195 EXPECT_NO_THROW_GMX(assigner.finishSection());
196 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
197 EXPECT_NO_THROW_GMX(assigner.finishSection());
198 EXPECT_NO_THROW_GMX(assigner.finish());
199 EXPECT_NO_THROW_GMX(options.finish());
201 ASSERT_EQ(2U, values.size());
202 EXPECT_EQ(4, values[0].value);
203 EXPECT_EQ(0, values[1].value);
206 TEST(RepeatingOptionSectionTest, HandlesUnsetOptionWithExplicitDefault)
208 std::vector<SectionData> values;
209 gmx::Options options;
210 auto sec = options.addSection(
211 RepeatingOptionSection<SectionData>("section")
212 .storeVector(&values));
213 using gmx::IntegerOption;
214 ASSERT_NO_THROW_GMX(sec.addOption(IntegerOption("p").store(&sec.bind().value)
215 .defaultValue(1)));
217 gmx::OptionsAssigner assigner(&options);
218 EXPECT_NO_THROW_GMX(assigner.start());
219 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
220 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
221 EXPECT_NO_THROW_GMX(assigner.appendValue("4"));
222 EXPECT_NO_THROW_GMX(assigner.finishOption());
223 EXPECT_NO_THROW_GMX(assigner.finishSection());
224 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
225 EXPECT_NO_THROW_GMX(assigner.finishSection());
226 EXPECT_NO_THROW_GMX(assigner.finish());
227 EXPECT_NO_THROW_GMX(options.finish());
229 ASSERT_EQ(2U, values.size());
230 EXPECT_EQ(4, values[0].value);
231 EXPECT_EQ(1, values[1].value);
234 struct NestedSectionData
236 int value;
237 std::vector<SectionData> subsec;
240 TEST(RepeatingOptionSectionTest, HandlesNestedSections)
242 std::vector<NestedSectionData> values;
243 gmx::Options options;
244 auto sec = options.addSection(
245 RepeatingOptionSection<NestedSectionData>("section")
246 .storeVector(&values));
247 auto subsec = sec.addSection(
248 RepeatingOptionSection<SectionData>("subsec")
249 .storeVector(&sec.bind().subsec));
250 using gmx::IntegerOption;
251 ASSERT_NO_THROW_GMX(sec.addOption(IntegerOption("p").store(&sec.bind().value)));
252 ASSERT_NO_THROW_GMX(subsec.addOption(IntegerOption("p").store(&subsec.bind().value)));
254 gmx::OptionsAssigner assigner(&options);
255 EXPECT_NO_THROW_GMX(assigner.start());
256 ASSERT_NO_THROW_GMX(assigner.startSection("section"));
257 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
258 EXPECT_NO_THROW_GMX(assigner.appendValue("4"));
259 EXPECT_NO_THROW_GMX(assigner.finishOption());
260 ASSERT_NO_THROW_GMX(assigner.startSection("subsec"));
261 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
262 EXPECT_NO_THROW_GMX(assigner.appendValue("5"));
263 EXPECT_NO_THROW_GMX(assigner.finishOption());
264 EXPECT_NO_THROW_GMX(assigner.finishSection());
265 EXPECT_NO_THROW_GMX(assigner.finishSection());
266 EXPECT_NO_THROW_GMX(assigner.finish());
267 EXPECT_NO_THROW_GMX(options.finish());
269 ASSERT_EQ(1U, values.size());
270 EXPECT_EQ(4, values[0].value);
271 ASSERT_EQ(1U, values[0].subsec.size());
272 EXPECT_EQ(5, values[0].subsec[0].value);
275 } // namespace