f94866c8a6766a42086265227a2df0e284ac7c5b
[alexxy/gromacs.git] / src / gromacs / utility / tests / keyvaluetreeserializer.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 #include "gmxpre.h"
36
37 #include "gromacs/utility/keyvaluetreeserializer.h"
38
39 #include <gtest/gtest.h>
40
41 #include "gromacs/utility/iserializer.h"
42 #include "gromacs/utility/keyvaluetreebuilder.h"
43
44 #include "testutils/refdata.h"
45
46 namespace
47 {
48
49 class RefDataWriteSerializer : public gmx::ISerializer
50 {
51     public:
52         RefDataWriteSerializer(gmx::test::TestReferenceChecker *parentChecker,
53                                const char                      *id)
54             : checker_(parentChecker->checkCompound("SerializedData", id))
55         {
56         }
57
58         virtual bool reading() const { return false; }
59
60         virtual void doUChar(unsigned char *value)
61         {
62             checker_.checkUChar(*value, nullptr);
63         }
64         virtual void doInt(int *value)
65         {
66             checker_.checkInteger(*value, nullptr);
67         }
68         virtual void doInt64(gmx_int64_t *value)
69         {
70             checker_.checkInt64(*value, nullptr);
71         }
72         virtual void doFloat(float *value)
73         {
74             checker_.checkFloat(*value, nullptr);
75         }
76         virtual void doDouble(double *value)
77         {
78             checker_.checkDouble(*value, nullptr);
79         }
80         virtual void doString(std::string *value)
81         {
82             checker_.checkString(*value, nullptr);
83         }
84
85     private:
86         gmx::test::TestReferenceChecker checker_;
87 };
88
89 class RefDataReadSerializer : public gmx::ISerializer
90 {
91     public:
92         RefDataReadSerializer(gmx::test::TestReferenceChecker *parentChecker,
93                               const char                      *id)
94             : checker_(parentChecker->checkCompound("SerializedData", id))
95         {
96         }
97
98         virtual bool reading() const { return true; }
99
100         virtual void doUChar(unsigned char *value)
101         {
102             *value = checker_.readUChar(nullptr);
103         }
104         virtual void doInt(int *value)
105         {
106             *value = checker_.readInteger(nullptr);
107         }
108         virtual void doInt64(gmx_int64_t *value)
109         {
110             *value = checker_.readInt64(nullptr);
111         }
112         virtual void doFloat(float *value)
113         {
114             *value = checker_.readFloat(nullptr);
115         }
116         virtual void doDouble(double *value)
117         {
118             *value = checker_.readDouble(nullptr);
119         }
120         virtual void doString(std::string *value)
121         {
122             *value = checker_.readString(nullptr);
123         }
124
125     private:
126         gmx::test::TestReferenceChecker checker_;
127 };
128
129 class KeyValueTreeSerializerTest : public ::testing::Test
130 {
131     public:
132         void runTest()
133         {
134             gmx::KeyValueTreeObject           input(builder_.build());
135             gmx::test::TestReferenceData      data;
136             gmx::test::TestReferenceChecker   checker(data.rootChecker());
137             checker.checkKeyValueTreeObject(input, "Input");
138             {
139                 RefDataWriteSerializer        serializer(&checker, "Stream");
140                 gmx::serializeKeyValueTree(input, &serializer);
141             }
142             {
143                 RefDataReadSerializer         serializer(&checker, "Stream");
144                 gmx::KeyValueTreeObject       output
145                     = gmx::deserializeKeyValueTree(&serializer);
146                 checker.checkKeyValueTreeObject(output, "Input");
147             }
148         }
149
150         gmx::KeyValueTreeBuilder builder_;
151 };
152
153 TEST_F(KeyValueTreeSerializerTest, EmptyTree)
154 {
155     runTest();
156 }
157
158 TEST_F(KeyValueTreeSerializerTest, SimpleObject)
159 {
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);
164     runTest();
165 }
166
167 TEST_F(KeyValueTreeSerializerTest, ObjectWithArrays)
168 {
169     auto arr1 = builder_.rootObject().addUniformArray<int>("a");
170     arr1.addValue(1);
171     arr1.addValue(2);
172     auto arr2 = builder_.rootObject().addUniformArray<std::string>("b");
173     arr2.addValue("foo");
174     arr2.addValue("bar");
175     runTest();
176 }
177
178 TEST_F(KeyValueTreeSerializerTest, ObjectWithObjects)
179 {
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);
187     runTest();
188 }
189
190 } // namespace