2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019,2020, 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.
37 * Tests option support for operations on KeyValueTree.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
44 #include "gromacs/options/treesupport.h"
49 #include <gtest/gtest.h>
51 #include "gromacs/options/basicoptions.h"
52 #include "gromacs/options/options.h"
53 #include "gromacs/options/optionsection.h"
54 #include "gromacs/options/repeatingsection.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/inmemoryserializer.h"
57 #include "gromacs/utility/keyvaluetree.h"
58 #include "gromacs/utility/keyvaluetreebuilder.h"
59 #include "gromacs/utility/keyvaluetreeserializer.h"
60 #include "gromacs/utility/stringstream.h"
61 #include "gromacs/utility/stringutil.h"
62 #include "gromacs/utility/textwriter.h"
64 #include "testutils/refdata.h"
65 #include "testutils/testasserts.h"
70 /********************************************************************
71 * Tests for assignOptionsFromKeyValueTree()
74 TEST(TreeValueSupportAssignTest, AssignsFromTree)
80 options.addOption(gmx::IntegerOption("a").store(&a0));
81 auto sec = options.addSection(gmx::OptionSection("s"));
82 sec.addOption(gmx::IntegerOption("a").store(&a1));
83 sec.addOption(gmx::StringOption("b").store(&b1));
85 gmx::KeyValueTreeBuilder builder;
86 builder.rootObject().addValue<int>("a", 2);
87 auto obj = builder.rootObject().addObject("s");
88 obj.addValue<int>("a", 1);
89 obj.addValue<std::string>("b", "foo");
90 gmx::KeyValueTreeObject tree = builder.build();
92 ASSERT_NO_THROW_GMX(gmx::assignOptionsFromKeyValueTree(&options, tree, nullptr));
93 EXPECT_NO_THROW_GMX(options.finish());
105 TEST(TreeValueSupportAssignTest, AssignsFromTreeWithArrays)
108 std::vector<SectionData> s;
110 gmx::Options options;
111 options.addOption(gmx::IntegerOption("a").storeVector(&a0).multiValue());
112 auto sec = options.addSection(gmx::RepeatingOptionSection<SectionData>("s").storeVector(&s));
113 sec.addOption(gmx::IntegerOption("a").store(&sec.bind().a));
115 gmx::KeyValueTreeBuilder builder;
116 auto array = builder.rootObject().addUniformArray<int>("a");
119 auto objArray = builder.rootObject().addObjectArray("s");
120 auto obj1 = objArray.addObject();
121 obj1.addValue<int>("a", 3);
122 auto obj2 = objArray.addObject();
123 obj2.addValue<int>("a", 4);
124 gmx::KeyValueTreeObject tree = builder.build();
126 ASSERT_NO_THROW_GMX(gmx::assignOptionsFromKeyValueTree(&options, tree, nullptr));
127 EXPECT_NO_THROW_GMX(options.finish());
129 ASSERT_EQ(2U, a0.size());
132 ASSERT_EQ(2U, s.size());
133 EXPECT_EQ(3, s[0].a);
134 EXPECT_EQ(4, s[1].a);
137 TEST(TreeValueSupportAssignErrorTest, HandlesInvalidValue)
141 gmx::Options options;
142 auto sec = options.addSection(gmx::OptionSection("s"));
143 sec.addOption(gmx::IntegerOption("a").store(&a1));
145 gmx::KeyValueTreeBuilder builder;
146 auto obj = builder.rootObject().addObject("s");
147 obj.addValue<std::string>("a", "foo");
148 gmx::KeyValueTreeObject tree = builder.build();
150 EXPECT_THROW_GMX(gmx::assignOptionsFromKeyValueTree(&options, tree, nullptr), gmx::InvalidInputError);
153 /********************************************************************
154 * Tests for checkForUnknownOptionsInKeyValueTree()
157 class TreeValueSupportCheckTest : public ::testing::Test
160 TreeValueSupportCheckTest()
162 auto sec1 = options_.addSection(gmx::OptionSection("s"));
163 auto sec2 = options_.addSection(gmx::OptionSection("r"));
164 options_.addOption(gmx::IntegerOption("a"));
165 sec1.addOption(gmx::IntegerOption("a"));
166 sec1.addOption(gmx::IntegerOption("b"));
167 sec2.addOption(gmx::IntegerOption("b"));
170 gmx::Options options_;
171 gmx::KeyValueTreeBuilder builder_;
174 TEST_F(TreeValueSupportCheckTest, HandlesEmpty)
176 EXPECT_NO_THROW_GMX(gmx::checkForUnknownOptionsInKeyValueTree(builder_.build(), options_));
179 TEST_F(TreeValueSupportCheckTest, HandlesMatchingTree)
181 auto root = builder_.rootObject();
182 root.addValue<int>("a", 1);
183 auto obj1 = root.addObject("s");
184 obj1.addValue<int>("a", 1);
185 obj1.addValue<int>("b", 2);
186 auto obj2 = root.addObject("r");
187 obj2.addValue<int>("b", 3);
189 EXPECT_NO_THROW_GMX(gmx::checkForUnknownOptionsInKeyValueTree(builder_.build(), options_));
192 TEST_F(TreeValueSupportCheckTest, HandlesSmallerTree1)
194 auto root = builder_.rootObject();
195 root.addValue<int>("a", 1);
196 auto obj1 = root.addObject("s");
197 obj1.addValue<int>("b", 2);
199 EXPECT_NO_THROW_GMX(gmx::checkForUnknownOptionsInKeyValueTree(builder_.build(), options_));
202 TEST_F(TreeValueSupportCheckTest, HandlesSmallerTree2)
204 auto root = builder_.rootObject();
205 auto obj1 = root.addObject("s");
206 obj1.addValue<int>("a", 1);
207 obj1.addValue<int>("b", 2);
209 EXPECT_NO_THROW_GMX(gmx::checkForUnknownOptionsInKeyValueTree(builder_.build(), options_));
212 TEST_F(TreeValueSupportCheckTest, DetectsExtraValue)
214 auto root = builder_.rootObject();
215 auto obj2 = root.addObject("r");
216 obj2.addValue<int>("a", 1);
217 obj2.addValue<int>("b", 3);
219 EXPECT_THROW_GMX(gmx::checkForUnknownOptionsInKeyValueTree(builder_.build(), options_),
220 gmx::InvalidInputError);
223 /********************************************************************
224 * Tests for adjustKeyValueTreeFromOptions()
227 class TreeValueSupportAdjustTest : public ::testing::Test
232 gmx::test::TestReferenceData refdata;
233 gmx::test::TestReferenceChecker checker(refdata.rootChecker());
234 gmx::KeyValueTreeObject tree(builder_.build());
235 checker.checkKeyValueTreeObject(tree, "Input");
236 ASSERT_NO_THROW_GMX(tree = gmx::adjustKeyValueTreeFromOptions(tree, options_));
237 checker.checkKeyValueTreeObject(tree, "Output");
240 gmx::Options options_;
241 gmx::KeyValueTreeBuilder builder_;
244 TEST_F(TreeValueSupportAdjustTest, FillsDefaultValues)
246 options_.addOption(gmx::IntegerOption("a").defaultValue(2));
250 TEST_F(TreeValueSupportAdjustTest, FillsDefaultVectorValues)
252 int v[3] = { 1, 2, 3 };
253 options_.addOption(gmx::IntegerOption("a").store(v).vector());
257 TEST_F(TreeValueSupportAdjustTest, FillsDefaultObjectValues)
259 auto sec1 = options_.addSection(gmx::OptionSection("s"));
260 sec1.addOption(gmx::IntegerOption("a").defaultValue(1));
261 auto sec2 = options_.addSection(gmx::OptionSection("r"));
262 sec2.addOption(gmx::IntegerOption("a").defaultValue(2));
263 options_.addOption(gmx::IntegerOption("a").defaultValue(3));
267 TEST_F(TreeValueSupportAdjustTest, NormalizesValues)
269 options_.addOption(gmx::IntegerOption("a"));
270 builder_.rootObject().addValue<std::string>("a", "2");
274 TEST_F(TreeValueSupportAdjustTest, MergesDefaultValues)
276 builder_.rootObject().addValue<int>("b", 1);
277 options_.addOption(gmx::IntegerOption("a").defaultValue(2));
278 options_.addOption(gmx::IntegerOption("b").defaultValue(3));
282 TEST_F(TreeValueSupportAdjustTest, OrdersValues)
284 builder_.rootObject().addValue<int>("a", 1);
285 builder_.rootObject().addValue<int>("c", 1);
286 builder_.rootObject().addValue<int>("b", 1);
287 options_.addOption(gmx::IntegerOption("b").defaultValue(2));
288 options_.addOption(gmx::IntegerOption("a").defaultValue(1));
289 options_.addOption(gmx::IntegerOption("c").defaultValue(3));
290 // TODO: This does not actually test the correct ordering, since the
291 // reference data is not currently order-sensitive, but the order can be
292 // checked manually from the reference data.
296 /********************************************************************
297 * Support for different option types
300 class TreeValueSupportTest : public ::testing::Test
305 gmx::test::TestReferenceData refdata;
306 gmx::test::TestReferenceChecker checker(refdata.rootChecker());
307 gmx::KeyValueTreeObject tree(builder_.build());
308 checker.checkKeyValueTreeObject(tree, "Input");
309 // Check that adjustment works.
310 ASSERT_NO_THROW_GMX(tree = gmx::adjustKeyValueTreeFromOptions(tree, options_));
311 checker.checkKeyValueTreeObject(tree, "Adjusted");
312 // Check that assignment works.
313 ASSERT_NO_THROW_GMX(gmx::assignOptionsFromKeyValueTree(&options_, tree, nullptr));
314 // Check that serialization works.
316 std::vector<char> buffer = serializeTree(tree);
317 gmx::InMemoryDeserializer deserializer(buffer, false);
318 gmx::KeyValueTreeObject output = gmx::deserializeKeyValueTree(&deserializer);
319 SCOPED_TRACE("After serialization/deserialization\n Buffer: " + formatBuffer(buffer));
320 checker.checkKeyValueTreeObject(output, "Adjusted");
322 // Check that dumping works.
324 gmx::StringOutputStream stream;
325 gmx::TextWriter writer(&stream);
326 ASSERT_NO_THROW_GMX(gmx::dumpKeyValueTree(&writer, tree));
327 checker.checkTextBlock(stream.toString(), "Dumped");
329 // Check that comparison works.
331 gmx::StringOutputStream stream;
332 gmx::TextWriter writer(&stream);
333 ASSERT_NO_THROW_GMX(gmx::compareKeyValueTrees(&writer, tree, tree, 0.0, 0.0));
334 checker.checkTextBlock(stream.toString(), "Compared");
336 // Check that comparison works against an empty tree.
338 gmx::StringOutputStream stream;
339 gmx::TextWriter writer(&stream);
340 gmx::KeyValueTreeObject empty;
341 ASSERT_NO_THROW_GMX(gmx::compareKeyValueTrees(&writer, tree, empty, 0.0, 0.0));
342 checker.checkTextBlock(stream.toString(), "ComparedAgainstEmpty");
346 gmx::Options options_;
347 gmx::KeyValueTreeBuilder builder_;
350 std::vector<char> serializeTree(const gmx::KeyValueTreeObject& tree)
352 gmx::InMemorySerializer serializer;
353 gmx::serializeKeyValueTree(tree, &serializer);
354 return serializer.finishAndGetBuffer();
357 std::string formatBuffer(const std::vector<char>& buffer)
359 return gmx::formatAndJoin(buffer, " ", [](char c) {
360 return gmx::formatString("%02x", static_cast<unsigned char>(c));
365 TEST_F(TreeValueSupportTest, SupportsBooleanOption)
367 options_.addOption(gmx::BooleanOption("a").defaultValue(true));
371 TEST_F(TreeValueSupportTest, SupportsIntegerOption)
373 options_.addOption(gmx::IntegerOption("a").defaultValue(2));
377 TEST_F(TreeValueSupportTest, SupportsInt64Option)
379 options_.addOption(gmx::Int64Option("a").defaultValue(2));
383 TEST_F(TreeValueSupportTest, SupportsStringOption)
385 options_.addOption(gmx::StringOption("a").defaultValue("s"));
389 TEST_F(TreeValueSupportTest, SupportsFloatOption)
391 options_.addOption(gmx::FloatOption("a").defaultValue(1.5));
395 TEST_F(TreeValueSupportTest, SupportsDoubleOption)
397 options_.addOption(gmx::DoubleOption("a").defaultValue(1.5));
401 //! Enum for testing EnumOption.
402 enum class TestEnum : int
408 TEST_F(TreeValueSupportTest, SupportsEnumOption)
410 enum class TestEnum : int
416 const gmx::EnumerationArray<TestEnum, const char*> testEnumNames = { { "foo", "bar" } };
417 options_.addOption(gmx::EnumOption<TestEnum>("a").enumValue(testEnumNames).defaultValue(TestEnum::Foo));