2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017, 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 doInt64(gmx_int64_t
*value
)
70 checker_
.checkInt64(*value
, nullptr);
72 virtual void doFloat(float *value
)
74 checker_
.checkFloat(*value
, nullptr);
76 virtual void doDouble(double *value
)
78 checker_
.checkDouble(*value
, nullptr);
80 virtual void doString(std::string
*value
)
82 checker_
.checkString(*value
, nullptr);
86 gmx::test::TestReferenceChecker checker_
;
89 class RefDataReadSerializer
: public gmx::ISerializer
92 RefDataReadSerializer(gmx::test::TestReferenceChecker
*parentChecker
,
94 : checker_(parentChecker
->checkCompound("SerializedData", id
))
98 virtual bool reading() const { return true; }
100 virtual void doUChar(unsigned char *value
)
102 *value
= checker_
.readUChar(nullptr);
104 virtual void doInt(int *value
)
106 *value
= checker_
.readInteger(nullptr);
108 virtual void doInt64(gmx_int64_t
*value
)
110 *value
= checker_
.readInt64(nullptr);
112 virtual void doFloat(float *value
)
114 *value
= checker_
.readFloat(nullptr);
116 virtual void doDouble(double *value
)
118 *value
= checker_
.readDouble(nullptr);
120 virtual void doString(std::string
*value
)
122 *value
= checker_
.readString(nullptr);
126 gmx::test::TestReferenceChecker checker_
;
129 class KeyValueTreeSerializerTest
: public ::testing::Test
134 gmx::KeyValueTreeObject
input(builder_
.build());
135 gmx::test::TestReferenceData data
;
136 gmx::test::TestReferenceChecker
checker(data
.rootChecker());
137 checker
.checkKeyValueTreeObject(input
, "Input");
139 RefDataWriteSerializer
serializer(&checker
, "Stream");
140 gmx::serializeKeyValueTree(input
, &serializer
);
143 RefDataReadSerializer
serializer(&checker
, "Stream");
144 gmx::KeyValueTreeObject output
145 = gmx::deserializeKeyValueTree(&serializer
);
146 checker
.checkKeyValueTreeObject(output
, "Input");
150 gmx::KeyValueTreeBuilder builder_
;
153 TEST_F(KeyValueTreeSerializerTest
, EmptyTree
)
158 TEST_F(KeyValueTreeSerializerTest
, SimpleObject
)
160 builder_
.rootObject().addValue
<int>("foo", 1);
161 builder_
.rootObject().addValue
<std::string
>("bar", "a");
162 builder_
.rootObject().addValue
<float>("f", 1.5);
163 builder_
.rootObject().addValue
<double>("d", 2.5);
167 TEST_F(KeyValueTreeSerializerTest
, ObjectWithArrays
)
169 auto arr1
= builder_
.rootObject().addUniformArray
<int>("a");
172 auto arr2
= builder_
.rootObject().addUniformArray
<std::string
>("b");
173 arr2
.addValue("foo");
174 arr2
.addValue("bar");
178 TEST_F(KeyValueTreeSerializerTest
, ObjectWithObjects
)
180 auto obj1
= builder_
.rootObject().addObject("obj");
181 obj1
.addValue
<int>("a", 1);
182 obj1
.addValue
<std::string
>("b", "foo");
183 auto obj2
= builder_
.rootObject().addObject("obj2");
184 obj2
.addValue
<int>("c", 2);
185 obj2
.addValue
<std::string
>("d", "bar");
186 builder_
.rootObject().addValue
<int>("foo", 3);