00156143a9b29f0a11376d5304fb184c558b229b
[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, 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 doFloat(float *value)
69         {
70             checker_.checkFloat(*value, nullptr);
71         }
72         virtual void doDouble(double *value)
73         {
74             checker_.checkDouble(*value, nullptr);
75         }
76         virtual void doString(std::string *value)
77         {
78             checker_.checkString(*value, nullptr);
79         }
80
81     private:
82         gmx::test::TestReferenceChecker checker_;
83 };
84
85 class RefDataReadSerializer : public gmx::ISerializer
86 {
87     public:
88         RefDataReadSerializer(gmx::test::TestReferenceChecker *parentChecker,
89                               const char                      *id)
90             : checker_(parentChecker->checkCompound("SerializedData", id))
91         {
92         }
93
94         virtual bool reading() const { return true; }
95
96         virtual void doUChar(unsigned char *value)
97         {
98             *value = checker_.readUChar(nullptr);
99         }
100         virtual void doInt(int *value)
101         {
102             *value = checker_.readInteger(nullptr);
103         }
104         virtual void doFloat(float *value)
105         {
106             *value = checker_.readFloat(nullptr);
107         }
108         virtual void doDouble(double *value)
109         {
110             *value = checker_.readDouble(nullptr);
111         }
112         virtual void doString(std::string *value)
113         {
114             *value = checker_.readString(nullptr);
115         }
116
117     private:
118         gmx::test::TestReferenceChecker checker_;
119 };
120
121 class KeyValueTreeSerializerTest : public ::testing::Test
122 {
123     public:
124         void runTest()
125         {
126             gmx::KeyValueTreeObject           input(builder_.build());
127             gmx::test::TestReferenceData      data;
128             gmx::test::TestReferenceChecker   checker(data.rootChecker());
129             checker.checkKeyValueTreeObject(input, "Input");
130             {
131                 RefDataWriteSerializer        serializer(&checker, "Stream");
132                 gmx::serializeKeyValueTree(input, &serializer);
133             }
134             {
135                 RefDataReadSerializer         serializer(&checker, "Stream");
136                 gmx::KeyValueTreeObject       output
137                     = gmx::deserializeKeyValueTree(&serializer);
138                 checker.checkKeyValueTreeObject(output, "Input");
139             }
140         }
141
142         gmx::KeyValueTreeBuilder builder_;
143 };
144
145 TEST_F(KeyValueTreeSerializerTest, EmptyTree)
146 {
147     runTest();
148 }
149
150 TEST_F(KeyValueTreeSerializerTest, SimpleObject)
151 {
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);
156     runTest();
157 }
158
159 TEST_F(KeyValueTreeSerializerTest, ObjectWithArrays)
160 {
161     auto arr1 = builder_.rootObject().addUniformArray<int>("a");
162     arr1.addValue(1);
163     arr1.addValue(2);
164     auto arr2 = builder_.rootObject().addUniformArray<std::string>("b");
165     arr2.addValue("foo");
166     arr2.addValue("bar");
167     runTest();
168 }
169
170 TEST_F(KeyValueTreeSerializerTest, ObjectWithObjects)
171 {
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);
179     runTest();
180 }
181
182 } // namespace