128-bit AVX2 SIMD for AMD Ryzen
[gromacs.git] / src / gromacs / options / tests / timeunitmanager.cpp
blob044f81ee7fc80f33aed12ce937f1f747cafb01f9
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,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 handling of time units with gmx::TimeUnitManager and
38 * gmx::TimeUnitBehavior.
40 * Also related functionality in gmx::DoubleOptionStorage is tested.
42 * \author Teemu Murtola <teemu.murtola@gmail.com>
43 * \ingroup module_options
45 #include "gmxpre.h"
47 #include "gromacs/options/timeunitmanager.h"
49 #include <gtest/gtest.h>
51 #include "gromacs/options/basicoptions.h"
52 #include "gromacs/options/options.h"
53 #include "gromacs/options/optionsassigner.h"
55 #include "testutils/testasserts.h"
57 namespace
60 TEST(TimeUnitManagerTest, BasicOperations)
62 gmx::TimeUnitManager manager;
63 EXPECT_EQ(gmx::TimeUnit_ps, manager.timeUnit());
64 EXPECT_DOUBLE_EQ(1.0, manager.timeScaleFactor());
65 manager.setTimeUnit(gmx::TimeUnit_ns);
66 EXPECT_EQ(gmx::TimeUnit_ns, manager.timeUnit());
67 EXPECT_DOUBLE_EQ(1e3, manager.timeScaleFactor());
68 EXPECT_DOUBLE_EQ(1e-3, manager.inverseTimeScaleFactor());
71 TEST(TimeUnitBehaviorTest, ScalesAssignedOptionValue)
73 gmx::TimeUnitBehavior behavior;
75 gmx::Options options;
76 double value = 0.0;
77 using gmx::DoubleOption;
78 ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("p").store(&value).timeValue()));
80 gmx::OptionsAssigner assigner(&options);
81 EXPECT_NO_THROW_GMX(assigner.start());
82 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
83 ASSERT_NO_THROW_GMX(assigner.appendValue("1.5"));
84 EXPECT_NO_THROW_GMX(assigner.finishOption());
85 EXPECT_NO_THROW_GMX(assigner.finish());
87 EXPECT_DOUBLE_EQ(1.5, value);
88 behavior.setTimeUnit(gmx::TimeUnit_ns);
89 behavior.optionsFinishing(&options);
90 EXPECT_DOUBLE_EQ(1500, value);
92 EXPECT_NO_THROW_GMX(options.finish());
94 behavior.setTimeUnit(gmx::TimeUnit_us);
95 behavior.optionsFinishing(&options);
96 EXPECT_DOUBLE_EQ(1500000, value);
98 behavior.setTimeUnit(gmx::TimeUnit_fs);
99 behavior.optionsFinishing(&options);
100 EXPECT_DOUBLE_EQ(0.0015, value);
102 behavior.setTimeUnit(gmx::TimeUnit_ps);
103 behavior.optionsFinishing(&options);
104 EXPECT_DOUBLE_EQ(1.5, value);
107 TEST(TimeUnitBehaviorTest, DoesNotScaleDefaultValues)
109 gmx::TimeUnitBehavior behavior;
111 gmx::Options options;
112 double value = 1.5, value2 = 0.0;
113 using gmx::DoubleOption;
114 ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("p").store(&value).timeValue()));
115 ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("q").store(&value2).timeValue()
116 .defaultValueIfSet(2.5)));
118 gmx::OptionsAssigner assigner(&options);
119 EXPECT_NO_THROW_GMX(assigner.start());
120 ASSERT_NO_THROW_GMX(assigner.startOption("q"));
121 EXPECT_NO_THROW_GMX(assigner.finishOption());
122 EXPECT_NO_THROW_GMX(assigner.finish());
123 EXPECT_NO_THROW_GMX(options.finish());
125 EXPECT_DOUBLE_EQ(2.5, value2);
126 behavior.setTimeUnit(gmx::TimeUnit_ns);
127 behavior.optionsFinishing(&options);
128 EXPECT_DOUBLE_EQ(1.5, value);
129 EXPECT_DOUBLE_EQ(2.5, value2);
132 TEST(TimeUnitBehaviorTest, ScalesUserInputWithMultipleSources)
134 gmx::TimeUnitBehavior behavior;
136 gmx::Options options;
137 double value = 0.0;
138 using gmx::DoubleOption;
139 ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("p").store(&value).timeValue()));
141 gmx::OptionsAssigner assigner(&options);
142 EXPECT_NO_THROW_GMX(assigner.start());
143 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
144 ASSERT_NO_THROW_GMX(assigner.appendValue("1.5"));
145 EXPECT_NO_THROW_GMX(assigner.finishOption());
146 EXPECT_NO_THROW_GMX(assigner.finish());
147 gmx::OptionsAssigner assigner2(&options);
148 EXPECT_NO_THROW_GMX(assigner2.start());
149 EXPECT_NO_THROW_GMX(assigner2.finish());
150 EXPECT_NO_THROW_GMX(options.finish());
152 EXPECT_DOUBLE_EQ(1.5, value);
153 behavior.setTimeUnit(gmx::TimeUnit_ns);
154 behavior.optionsFinishing(&options);
155 EXPECT_DOUBLE_EQ(1500, value);
158 TEST(TimeUnitBehaviorTest, TimeUnitOptionWorks)
160 gmx::TimeUnitBehavior behavior;
162 gmx::Options options;
163 double value = 0.0;
164 using gmx::DoubleOption;
165 ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("p").store(&value).timeValue()));
166 ASSERT_NO_THROW_GMX(behavior.addTimeUnitOption(&options, "tu"));
168 gmx::OptionsAssigner assigner(&options);
169 EXPECT_NO_THROW_GMX(assigner.start());
170 ASSERT_NO_THROW_GMX(assigner.startOption("p"));
171 ASSERT_NO_THROW_GMX(assigner.appendValue("1.5"));
172 EXPECT_NO_THROW_GMX(assigner.finishOption());
173 ASSERT_NO_THROW_GMX(assigner.startOption("tu"));
174 ASSERT_NO_THROW_GMX(assigner.appendValue("ns"));
175 EXPECT_NO_THROW_GMX(assigner.finishOption());
176 EXPECT_NO_THROW_GMX(assigner.finish());
178 EXPECT_DOUBLE_EQ(1.5, value);
179 EXPECT_EQ(gmx::TimeUnit_ns, behavior.timeUnit());
180 behavior.optionsFinishing(&options);
181 EXPECT_DOUBLE_EQ(1500, value);
183 EXPECT_NO_THROW_GMX(options.finish());
186 } // namespace