128-bit AVX2 SIMD for AMD Ryzen
[gromacs.git] / src / gromacs / options / tests / filenameoptionmanager.cpp
blob9a67461c05ebf87065b4c9fbd8bec1fe28ac50b4
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,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 file name option implementation dependent on gmx::FileNameOptionManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
42 #include "gmxpre.h"
44 #include "gromacs/options/filenameoptionmanager.h"
46 #include <gtest/gtest.h>
48 #include "gromacs/fileio/filetypes.h"
49 #include "gromacs/options/filenameoption.h"
50 #include "gromacs/options/options.h"
51 #include "gromacs/options/optionsassigner.h"
52 #include "gromacs/utility/exceptions.h"
54 #include "testutils/testasserts.h"
55 #include "testutils/testfileredirector.h"
57 namespace
60 using gmx::FileNameOption;
62 class FileNameOptionManagerTest : public ::testing::Test
64 public:
65 FileNameOptionManagerTest()
67 manager_.setInputRedirector(&redirector_);
68 options_.addManager(&manager_);
71 void addExistingFile(const char *filename)
73 redirector_.addExistingFile(filename);
76 gmx::test::TestFileInputRedirector redirector_;
77 gmx::FileNameOptionManager manager_;
78 gmx::Options options_;
81 /********************************************************************
82 * Actual tests
85 TEST_F(FileNameOptionManagerTest, AddsMissingExtension)
87 std::string value;
88 ASSERT_NO_THROW_GMX(options_.addOption(
89 FileNameOption("f").store(&value)
90 .filetype(gmx::eftTrajectory).outputFile()));
92 gmx::OptionsAssigner assigner(&options_);
93 EXPECT_NO_THROW_GMX(assigner.start());
94 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
95 EXPECT_NO_THROW_GMX(assigner.appendValue("testfile"));
96 EXPECT_NO_THROW_GMX(assigner.finishOption());
97 EXPECT_NO_THROW_GMX(assigner.finish());
98 EXPECT_NO_THROW_GMX(options_.finish());
100 EXPECT_EQ("testfile.xtc", value);
103 TEST_F(FileNameOptionManagerTest, AddsMissingCustomDefaultExtension)
105 std::string value;
106 ASSERT_NO_THROW_GMX(options_.addOption(
107 FileNameOption("f").store(&value)
108 .filetype(gmx::eftTrajectory).outputFile()
109 .defaultType(efPDB)));
111 gmx::OptionsAssigner assigner(&options_);
112 EXPECT_NO_THROW_GMX(assigner.start());
113 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
114 EXPECT_NO_THROW_GMX(assigner.appendValue("testfile"));
115 EXPECT_NO_THROW_GMX(assigner.finishOption());
116 EXPECT_NO_THROW_GMX(assigner.finish());
117 EXPECT_NO_THROW_GMX(options_.finish());
119 EXPECT_EQ("testfile.pdb", value);
122 TEST_F(FileNameOptionManagerTest, GivesErrorOnMissingInputFile)
124 std::string value;
125 ASSERT_NO_THROW_GMX(options_.addOption(
126 FileNameOption("f").store(&value)
127 .filetype(gmx::eftIndex).inputFile()));
128 EXPECT_TRUE(value.empty());
130 gmx::OptionsAssigner assigner(&options_);
131 EXPECT_NO_THROW_GMX(assigner.start());
132 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
133 EXPECT_THROW_GMX(assigner.appendValue("missing.ndx"), gmx::InvalidInputError);
134 EXPECT_NO_THROW_GMX(assigner.finishOption());
135 EXPECT_NO_THROW_GMX(assigner.finish());
136 EXPECT_NO_THROW_GMX(options_.finish());
138 EXPECT_TRUE(value.empty());
141 TEST_F(FileNameOptionManagerTest, GivesErrorOnMissingGenericInputFile)
143 std::string value;
144 ASSERT_NO_THROW_GMX(options_.addOption(
145 FileNameOption("f").store(&value)
146 .filetype(gmx::eftTrajectory).inputFile()));
147 EXPECT_TRUE(value.empty());
149 gmx::OptionsAssigner assigner(&options_);
150 EXPECT_NO_THROW_GMX(assigner.start());
151 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
152 EXPECT_THROW_GMX(assigner.appendValue("missing.trr"), gmx::InvalidInputError);
153 EXPECT_NO_THROW_GMX(assigner.finishOption());
154 EXPECT_NO_THROW_GMX(assigner.finish());
155 EXPECT_NO_THROW_GMX(options_.finish());
157 EXPECT_TRUE(value.empty());
160 TEST_F(FileNameOptionManagerTest, GivesErrorOnMissingDefaultInputFile)
162 std::string value;
163 ASSERT_NO_THROW_GMX(options_.addOption(
164 FileNameOption("f").store(&value)
165 .filetype(gmx::eftIndex).inputFile()
166 .defaultBasename("missing")));
168 gmx::OptionsAssigner assigner(&options_);
169 EXPECT_NO_THROW_GMX(assigner.start());
170 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
171 EXPECT_NO_THROW_GMX(assigner.finishOption());
172 EXPECT_NO_THROW_GMX(assigner.finish());
173 EXPECT_THROW_GMX(options_.finish(), gmx::InvalidInputError);
176 TEST_F(FileNameOptionManagerTest, GivesErrorOnMissingRequiredInputFile)
178 std::string value;
179 ASSERT_NO_THROW_GMX(options_.addOption(
180 FileNameOption("f").store(&value).required()
181 .filetype(gmx::eftIndex).inputFile()
182 .defaultBasename("missing")));
183 EXPECT_EQ("missing.ndx", value);
185 gmx::OptionsAssigner assigner(&options_);
186 EXPECT_NO_THROW_GMX(assigner.start());
187 EXPECT_NO_THROW_GMX(assigner.finish());
188 EXPECT_THROW_GMX(options_.finish(), gmx::InvalidInputError);
191 TEST_F(FileNameOptionManagerTest, AcceptsMissingInputFileIfSpecified)
193 std::string value;
194 ASSERT_NO_THROW_GMX(options_.addOption(
195 FileNameOption("f").store(&value)
196 .filetype(gmx::eftIndex).inputFile()
197 .allowMissing()));
198 EXPECT_TRUE(value.empty());
200 gmx::OptionsAssigner assigner(&options_);
201 EXPECT_NO_THROW_GMX(assigner.start());
202 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
203 EXPECT_NO_THROW_GMX(assigner.appendValue("missing.ndx"));
204 EXPECT_NO_THROW_GMX(assigner.finishOption());
205 EXPECT_NO_THROW_GMX(assigner.finish());
206 EXPECT_NO_THROW_GMX(options_.finish());
208 EXPECT_EQ("missing.ndx", value);
211 TEST_F(FileNameOptionManagerTest, AcceptsMissingDefaultInputFileIfSpecified)
213 std::string value;
214 ASSERT_NO_THROW_GMX(options_.addOption(
215 FileNameOption("f").store(&value)
216 .filetype(gmx::eftIndex).inputFile()
217 .defaultBasename("missing")
218 .allowMissing()));
220 gmx::OptionsAssigner assigner(&options_);
221 EXPECT_NO_THROW_GMX(assigner.start());
222 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
223 EXPECT_NO_THROW_GMX(assigner.finishOption());
224 EXPECT_NO_THROW_GMX(assigner.finish());
225 EXPECT_NO_THROW_GMX(options_.finish());
227 EXPECT_EQ("missing.ndx", value);
230 TEST_F(FileNameOptionManagerTest, AcceptsMissingRequiredInputFileIfSpecified)
232 std::string value;
233 ASSERT_NO_THROW_GMX(options_.addOption(
234 FileNameOption("f").store(&value).required()
235 .filetype(gmx::eftIndex).inputFile()
236 .defaultBasename("missing")
237 .allowMissing()));
238 EXPECT_EQ("missing.ndx", value);
240 gmx::OptionsAssigner assigner(&options_);
241 EXPECT_NO_THROW_GMX(assigner.start());
242 EXPECT_NO_THROW_GMX(assigner.finish());
243 EXPECT_NO_THROW_GMX(options_.finish());
245 EXPECT_EQ("missing.ndx", value);
248 TEST_F(FileNameOptionManagerTest, AddsMissingExtensionBasedOnExistingFile)
250 addExistingFile("testfile.trr");
252 std::string value;
253 ASSERT_NO_THROW_GMX(options_.addOption(
254 FileNameOption("f").store(&value)
255 .filetype(gmx::eftTrajectory).inputFile()));
257 gmx::OptionsAssigner assigner(&options_);
258 EXPECT_NO_THROW_GMX(assigner.start());
259 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
260 EXPECT_NO_THROW_GMX(assigner.appendValue("testfile"));
261 EXPECT_NO_THROW_GMX(assigner.finishOption());
262 EXPECT_NO_THROW_GMX(assigner.finish());
263 EXPECT_NO_THROW_GMX(options_.finish());
265 EXPECT_EQ("testfile.trr", value);
268 TEST_F(FileNameOptionManagerTest,
269 AddsMissingExtensionForRequiredDefaultNameBasedOnExistingFile)
271 addExistingFile("testfile.trr");
273 std::string value;
274 ASSERT_NO_THROW_GMX(options_.addOption(
275 FileNameOption("f").store(&value).required()
276 .filetype(gmx::eftTrajectory).inputFile()
277 .defaultBasename("testfile")));
278 EXPECT_EQ("testfile.xtc", value);
280 gmx::OptionsAssigner assigner(&options_);
281 EXPECT_NO_THROW_GMX(assigner.start());
282 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
283 EXPECT_NO_THROW_GMX(assigner.finishOption());
284 EXPECT_NO_THROW_GMX(assigner.finish());
285 EXPECT_NO_THROW_GMX(options_.finish());
287 EXPECT_EQ("testfile.trr", value);
290 TEST_F(FileNameOptionManagerTest,
291 AddsMissingExtensionForOptionalDefaultNameBasedOnExistingFile)
293 addExistingFile("testfile.trr");
295 std::string value;
296 ASSERT_NO_THROW_GMX(options_.addOption(
297 FileNameOption("f").store(&value)
298 .filetype(gmx::eftTrajectory).inputFile()
299 .defaultBasename("testfile")));
301 gmx::OptionsAssigner assigner(&options_);
302 EXPECT_NO_THROW_GMX(assigner.start());
303 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
304 EXPECT_NO_THROW_GMX(assigner.finishOption());
305 EXPECT_NO_THROW_GMX(assigner.finish());
306 EXPECT_NO_THROW_GMX(options_.finish());
308 EXPECT_EQ("testfile.trr", value);
311 TEST_F(FileNameOptionManagerTest,
312 AddsMissingExtensionForRequiredFromDefaultNameOptionBasedOnExistingFile)
314 addExistingFile("testfile.trr");
316 std::string value;
317 ASSERT_NO_THROW_GMX(options_.addOption(
318 FileNameOption("f").store(&value).required()
319 .filetype(gmx::eftTrajectory).inputFile()
320 .defaultBasename("foo")));
321 ASSERT_NO_THROW_GMX(manager_.addDefaultFileNameOption(&options_, "deffnm"));
322 EXPECT_EQ("foo.xtc", value);
324 gmx::OptionsAssigner assigner(&options_);
325 EXPECT_NO_THROW_GMX(assigner.start());
326 EXPECT_NO_THROW_GMX(assigner.startOption("deffnm"));
327 EXPECT_NO_THROW_GMX(assigner.appendValue("testfile"));
328 EXPECT_NO_THROW_GMX(assigner.finishOption());
329 EXPECT_NO_THROW_GMX(assigner.finish());
330 EXPECT_NO_THROW_GMX(options_.finish());
332 EXPECT_EQ("testfile.trr", value);
335 TEST_F(FileNameOptionManagerTest,
336 AddsMissingExtensionForOptionalFromDefaultNameOptionBasedOnExistingFile)
338 addExistingFile("testfile.trr");
340 std::string value;
341 ASSERT_NO_THROW_GMX(options_.addOption(
342 FileNameOption("f").store(&value)
343 .filetype(gmx::eftTrajectory).inputFile()
344 .defaultBasename("foo")));
345 ASSERT_NO_THROW_GMX(manager_.addDefaultFileNameOption(&options_, "deffnm"));
347 gmx::OptionsAssigner assigner(&options_);
348 EXPECT_NO_THROW_GMX(assigner.start());
349 EXPECT_NO_THROW_GMX(assigner.startOption("deffnm"));
350 EXPECT_NO_THROW_GMX(assigner.appendValue("testfile"));
351 EXPECT_NO_THROW_GMX(assigner.finishOption());
352 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
353 EXPECT_NO_THROW_GMX(assigner.finishOption());
354 EXPECT_NO_THROW_GMX(assigner.finish());
355 EXPECT_NO_THROW_GMX(options_.finish());
357 EXPECT_EQ("testfile.trr", value);
360 TEST_F(FileNameOptionManagerTest, DefaultNameOptionWorksWithoutInputChecking)
362 std::string value;
363 ASSERT_NO_THROW_GMX(manager_.disableInputOptionChecking(true));
364 ASSERT_NO_THROW_GMX(options_.addOption(
365 FileNameOption("f").store(&value).required()
366 .filetype(gmx::eftIndex).inputFile()
367 .defaultBasename("default")
368 .allowMissing()));
369 ASSERT_NO_THROW_GMX(manager_.addDefaultFileNameOption(&options_, "deffnm"));
370 EXPECT_EQ("default.ndx", value);
372 gmx::OptionsAssigner assigner(&options_);
373 EXPECT_NO_THROW_GMX(assigner.start());
374 EXPECT_NO_THROW_GMX(assigner.startOption("deffnm"));
375 EXPECT_NO_THROW_GMX(assigner.appendValue("missing"));
376 EXPECT_NO_THROW_GMX(assigner.finishOption());
377 EXPECT_NO_THROW_GMX(assigner.finish());
378 EXPECT_NO_THROW_GMX(options_.finish());
380 EXPECT_EQ("missing.ndx", value);
383 } // namespace