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.
37 #include "gromacs/utility/keyvaluetreeserializer.h"
39 #include <gtest/gtest.h>
41 #include "gromacs/utility/iserializer.h"
42 #include "gromacs/utility/keyvaluetreebuilder.h"
44 #include "testutils/refdata.h"
49 class RefDataWriteSerializer : public gmx::ISerializer
52 RefDataWriteSerializer(gmx::test::TestReferenceChecker *parentChecker,
54 : checker_(parentChecker->checkCompound("SerializedData", id))
58 virtual bool reading() const { return false; }
60 virtual void doUChar(unsigned char *value)
62 checker_.checkUChar(*value, nullptr);
64 virtual void doInt(int *value)
66 checker_.checkInteger(*value, nullptr);
68 virtual void doFloat(float *value)
70 checker_.checkFloat(*value, nullptr);
72 virtual void doDouble(double *value)
74 checker_.checkDouble(*value, nullptr);
76 virtual void doString(std::string *value)
78 checker_.checkString(*value, nullptr);
82 gmx::test::TestReferenceChecker checker_;
85 class RefDataReadSerializer : public gmx::ISerializer
88 RefDataReadSerializer(gmx::test::TestReferenceChecker *parentChecker,
90 : checker_(parentChecker->checkCompound("SerializedData", id))
94 virtual bool reading() const { return true; }
96 virtual void doUChar(unsigned char *value)
98 *value = checker_.readUChar(nullptr);
100 virtual void doInt(int *value)
102 *value = checker_.readInteger(nullptr);
104 virtual void doFloat(float *value)
106 *value = checker_.readFloat(nullptr);
108 virtual void doDouble(double *value)
110 *value = checker_.readDouble(nullptr);
112 virtual void doString(std::string *value)
114 *value = checker_.readString(nullptr);
118 gmx::test::TestReferenceChecker checker_;
121 class KeyValueTreeSerializerTest : public ::testing::Test
126 gmx::KeyValueTreeObject input(builder_.build());
127 gmx::test::TestReferenceData data;
128 gmx::test::TestReferenceChecker checker(data.rootChecker());
129 checker.checkKeyValueTreeObject(input, "Input");
131 RefDataWriteSerializer serializer(&checker, "Stream");
132 gmx::serializeKeyValueTree(input, &serializer);
135 RefDataReadSerializer serializer(&checker, "Stream");
136 gmx::KeyValueTreeObject output
137 = gmx::deserializeKeyValueTree(&serializer);
138 checker.checkKeyValueTreeObject(output, "Input");
142 gmx::KeyValueTreeBuilder builder_;
145 TEST_F(KeyValueTreeSerializerTest, EmptyTree)
150 TEST_F(KeyValueTreeSerializerTest, SimpleObject)
152 builder_.rootObject().addValue<int>("foo", 1);
153 builder_.rootObject().addValue<std::string>("bar", "a");
154 builder_.rootObject().addValue<float>("f", 1.5);
155 builder_.rootObject().addValue<double>("d", 2.5);
159 TEST_F(KeyValueTreeSerializerTest, ObjectWithArrays)
161 auto arr1 = builder_.rootObject().addUniformArray<int>("a");
164 auto arr2 = builder_.rootObject().addUniformArray<std::string>("b");
165 arr2.addValue("foo");
166 arr2.addValue("bar");
170 TEST_F(KeyValueTreeSerializerTest, ObjectWithObjects)
172 auto obj1 = builder_.rootObject().addObject("obj");
173 obj1.addValue<int>("a", 1);
174 obj1.addValue<std::string>("b", "foo");
175 auto obj2 = builder_.rootObject().addObject("obj2");
176 obj2.addValue<int>("c", 2);
177 obj2.addValue<std::string>("d", "bar");
178 builder_.rootObject().addValue<int>("foo", 3);