cf1470733382dd62f80c223e442d4c32d0487582
[alexxy/gromacs.git] / src / gromacs / options / tests / treesupport.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests option support for operations on KeyValueTree.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/options/treesupport.h"
45
46 #include <vector>
47
48 #include <gtest/gtest.h>
49
50 #include "gromacs/options/basicoptions.h"
51 #include "gromacs/options/options.h"
52 #include "gromacs/options/optionsection.h"
53 #include "gromacs/options/repeatingsection.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/keyvaluetree.h"
56 #include "gromacs/utility/keyvaluetreebuilder.h"
57
58 #include "testutils/refdata.h"
59 #include "testutils/testasserts.h"
60
61 namespace
62 {
63
64 /********************************************************************
65  * Tests for assignOptionsFromKeyValueTree()
66  */
67
68 TEST(TreeValueSupportAssignTest, AssignsFromTree)
69 {
70     int                      a0 = 0, a1 = 0;
71     std::string              b1;
72
73     gmx::Options             options;
74     options.addOption(gmx::IntegerOption("a").store(&a0));
75     auto                     sec = options.addSection(gmx::OptionSection("s"));
76     sec.addOption(gmx::IntegerOption("a").store(&a1));
77     sec.addOption(gmx::StringOption("b").store(&b1));
78
79     gmx::KeyValueTreeBuilder builder;
80     builder.rootObject().addValue<int>("a", 2);
81     auto                     obj = builder.rootObject().addObject("s");
82     obj.addValue<int>("a", 1);
83     obj.addValue<std::string>("b", "foo");
84     gmx::KeyValueTreeObject  tree = builder.build();
85
86     ASSERT_NO_THROW_GMX(gmx::assignOptionsFromKeyValueTree(&options, tree, nullptr));
87     EXPECT_NO_THROW_GMX(options.finish());
88
89     EXPECT_EQ(2, a0);
90     EXPECT_EQ(1, a1);
91     EXPECT_EQ("foo", b1);
92 }
93
94 struct SectionData
95 {
96     int a;
97 };
98
99 TEST(TreeValueSupportAssignTest, AssignsFromTreeWithArrays)
100 {
101     std::vector<int>         a0;
102     std::vector<SectionData> s;
103
104     gmx::Options             options;
105     options.addOption(gmx::IntegerOption("a").storeVector(&a0).multiValue());
106     auto                     sec = options.addSection(gmx::RepeatingOptionSection<SectionData>("s").storeVector(&s));
107     sec.addOption(gmx::IntegerOption("a").store(&sec.bind().a));
108
109     gmx::KeyValueTreeBuilder builder;
110     auto                     array = builder.rootObject().addUniformArray<int>("a");
111     array.addValue(1);
112     array.addValue(2);
113     auto                     objArray = builder.rootObject().addObjectArray("s");
114     auto                     obj1     = objArray.addObject();
115     obj1.addValue<int>("a", 3);
116     auto                     obj2 = objArray.addObject();
117     obj2.addValue<int>("a", 4);
118     gmx::KeyValueTreeObject  tree = builder.build();
119
120     ASSERT_NO_THROW_GMX(gmx::assignOptionsFromKeyValueTree(&options, tree, nullptr));
121     EXPECT_NO_THROW_GMX(options.finish());
122
123     ASSERT_EQ(2U, a0.size());
124     EXPECT_EQ(1, a0[0]);
125     EXPECT_EQ(2, a0[1]);
126     ASSERT_EQ(2U, s.size());
127     EXPECT_EQ(3, s[0].a);
128     EXPECT_EQ(4, s[1].a);
129 }
130
131 TEST(TreeValueSupportAssignErrorTest, HandlesInvalidValue)
132 {
133     int                      a1 = 0;
134
135     gmx::Options             options;
136     auto                     sec = options.addSection(gmx::OptionSection("s"));
137     sec.addOption(gmx::IntegerOption("a").store(&a1));
138
139     gmx::KeyValueTreeBuilder builder;
140     auto                     obj = builder.rootObject().addObject("s");
141     obj.addValue<std::string>("a", "foo");
142     gmx::KeyValueTreeObject  tree = builder.build();
143
144     EXPECT_THROW_GMX(gmx::assignOptionsFromKeyValueTree(&options, tree, nullptr),
145                      gmx::InvalidInputError);
146 }
147
148 /********************************************************************
149  * Tests for adjustKeyValueTreeFromOptions()
150  */
151
152 class TreeValueSupportAdjustTest : public ::testing::Test
153 {
154     public:
155         void runTest()
156         {
157             gmx::test::TestReferenceData    refdata;
158             gmx::test::TestReferenceChecker checker(refdata.rootChecker());
159             gmx::KeyValueTreeObject         tree(builder_.build());
160             checker.checkKeyValueTreeObject(tree, "Input");
161             ASSERT_NO_THROW_GMX(tree = gmx::adjustKeyValueTreeFromOptions(tree, options_));
162             checker.checkKeyValueTreeObject(tree, "Output");
163         }
164
165         gmx::Options              options_;
166         gmx::KeyValueTreeBuilder  builder_;
167 };
168
169 TEST_F(TreeValueSupportAdjustTest, FillsDefaultValues)
170 {
171     options_.addOption(gmx::IntegerOption("a").defaultValue(2));
172     runTest();
173 }
174
175 TEST_F(TreeValueSupportAdjustTest, FillsDefaultVectorValues)
176 {
177     int v[3] = {1, 2, 3};
178     options_.addOption(gmx::IntegerOption("a").store(v).vector());
179     runTest();
180 }
181
182 TEST_F(TreeValueSupportAdjustTest, FillsDefaultObjectValues)
183 {
184     auto sec1 = options_.addSection(gmx::OptionSection("s"));
185     sec1.addOption(gmx::IntegerOption("a").defaultValue(1));
186     auto sec2 = options_.addSection(gmx::OptionSection("r"));
187     sec2.addOption(gmx::IntegerOption("a").defaultValue(2));
188     options_.addOption(gmx::IntegerOption("a").defaultValue(3));
189     runTest();
190 }
191
192 TEST_F(TreeValueSupportAdjustTest, NormalizesValues)
193 {
194     options_.addOption(gmx::IntegerOption("a"));
195     builder_.rootObject().addValue<std::string>("a", "2");
196     runTest();
197 }
198
199 TEST_F(TreeValueSupportAdjustTest, MergesDefaultValues)
200 {
201     builder_.rootObject().addValue<int>("b", 1);
202     options_.addOption(gmx::IntegerOption("a").defaultValue(2));
203     options_.addOption(gmx::IntegerOption("b").defaultValue(3));
204     runTest();
205 }
206
207 TEST_F(TreeValueSupportAdjustTest, OrdersValues)
208 {
209     builder_.rootObject().addValue<int>("a", 1);
210     builder_.rootObject().addValue<int>("c", 1);
211     builder_.rootObject().addValue<int>("b", 1);
212     options_.addOption(gmx::IntegerOption("b").defaultValue(2));
213     options_.addOption(gmx::IntegerOption("a").defaultValue(1));
214     options_.addOption(gmx::IntegerOption("c").defaultValue(3));
215     // TODO: This does not actually test the correct ordering, since the
216     // reference data is not currently order-sensitive, but the order can be
217     // checked manually from the reference data.
218     runTest();
219 }
220
221 } // namespace