Improve MessageStringCollector
[alexxy/gromacs.git] / src / gromacs / utility / tests / inmemoryserializer.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020,2021, 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 /*! \internal \file
36  * \brief
37  * Tests for gmx::InMemorySerializer and InMemoryDeserializer.
38  *
39  * \author Christian Blau <blau@kth.se>
40  * \ingroup module_utility
41  */
42
43 #include "gmxpre.h"
44
45 #include "gromacs/utility/inmemoryserializer.h"
46
47 #include <gmock/gmock.h>
48 #include <gtest/gtest.h>
49
50 namespace gmx
51 {
52 namespace test
53 {
54 namespace
55 {
56 union IntAndFloat32
57 {
58     std::int32_t int32Value_;
59     float        floatValue_;
60 };
61
62 union IntAndFloat64
63 {
64     std::int64_t int64Value_;
65     double       doubleValue_;
66 };
67
68 //! Constants used for testing endian swap operations
69 //! \{
70 constexpr std::int16_t c_int16Value        = static_cast<std::int16_t>(0x7A2B);
71 constexpr std::int16_t c_int16ValueSwapped = static_cast<std::int16_t>(0x2B7A);
72 constexpr std::int32_t c_int32Value        = static_cast<std::int32_t>(0x78ABCDEF);
73 constexpr std::int32_t c_int32ValueSwapped = static_cast<std::int32_t>(0xEFCDAB78);
74 constexpr std::int64_t c_int64Value        = static_cast<std::int64_t>(0x78ABCDEF12345678);
75 constexpr std::int64_t c_int64ValueSwapped = static_cast<std::int64_t>(0x78563412EFCDAB78);
76
77 constexpr const IntAndFloat32 c_intAndFloat32{ c_int32Value };
78 constexpr const IntAndFloat64 c_intAndFloat64{ c_int64Value };
79
80 constexpr const IntAndFloat32 c_intAndFloat32Swapped{ c_int32ValueSwapped };
81 constexpr const IntAndFloat64 c_intAndFloat64Swapped{ c_int64ValueSwapped };
82 //! \}
83
84 //! Return the integer used for testing, depending on the size of int.
85 constexpr int integerSizeDependentTestingValue()
86 {
87     return sizeof(int) == 4 ? c_int32Value : sizeof(int) == 8 ? c_int64Value : c_int16Value;
88 }
89
90 //! Return the endianess-swapped integer used for testing, depending on the size of int.
91 constexpr int integerSizeDependentTestingValueEndianessSwapped()
92 {
93     return sizeof(int) == 4   ? c_int32ValueSwapped
94            : sizeof(int) == 8 ? c_int64ValueSwapped
95                               : c_int16ValueSwapped;
96 }
97
98 class InMemorySerializerTest : public ::testing::Test
99 {
100 public:
101     struct SerializerValues
102     {
103         bool           boolValue_;
104         unsigned char  unsignedCharValue_;
105         char           charValue_;
106         unsigned short unsignedShortValue_;
107         std::int32_t   int32Value_;
108         float          floatValue_;
109         std::int64_t   int64Value_;
110         double         doubleValue_;
111         int            intValue_;
112         real           realValue_;
113     };
114
115     static void serialize(ISerializer* serializer, SerializerValues* values)
116     {
117         EXPECT_FALSE(serializer->reading());
118         doValues(serializer, values);
119     }
120
121     static SerializerValues deserialize(ISerializer* serializer)
122     {
123         EXPECT_TRUE(serializer->reading());
124         SerializerValues result;
125         doValues(serializer, &result);
126         return result;
127     }
128
129     static void checkSerializerValuesforEquality(const SerializerValues& lhs, const SerializerValues& rhs)
130     {
131         EXPECT_EQ(lhs.boolValue_, rhs.boolValue_);
132         EXPECT_EQ(lhs.unsignedCharValue_, rhs.unsignedCharValue_);
133         EXPECT_EQ(lhs.charValue_, rhs.charValue_);
134         EXPECT_EQ(lhs.intValue_, rhs.intValue_);
135         EXPECT_EQ(lhs.int32Value_, rhs.int32Value_);
136         EXPECT_EQ(lhs.int64Value_, rhs.int64Value_);
137         EXPECT_EQ(lhs.unsignedShortValue_, rhs.unsignedShortValue_);
138         EXPECT_EQ(lhs.realValue_, rhs.realValue_);
139         EXPECT_EQ(lhs.floatValue_, rhs.floatValue_);
140         EXPECT_EQ(lhs.doubleValue_, rhs.doubleValue_);
141     }
142
143 private:
144     static void doValues(ISerializer* serializer, SerializerValues* values)
145     {
146         serializer->doBool(&values->boolValue_);
147         serializer->doUChar(&values->unsignedCharValue_);
148         serializer->doChar(&values->charValue_);
149         serializer->doInt(&values->intValue_);
150         serializer->doInt32(&values->int32Value_);
151         serializer->doInt64(&values->int64Value_);
152         serializer->doUShort(&values->unsignedShortValue_);
153         serializer->doReal(&values->realValue_);
154         serializer->doFloat(&values->floatValue_);
155         serializer->doDouble(&values->doubleValue_);
156     }
157
158 protected:
159     SerializerValues defaultValues_ = { true,
160                                         0x78,
161                                         0x78,
162                                         static_cast<unsigned short>(c_int16Value),
163                                         c_int32Value,
164                                         c_intAndFloat32.floatValue_,
165                                         c_int64Value,
166                                         c_intAndFloat64.doubleValue_,
167                                         integerSizeDependentTestingValue(),
168                                         std::is_same_v<real, double>
169                                                 ? static_cast<real>(c_intAndFloat64.doubleValue_)
170                                                 : static_cast<real>(c_intAndFloat32.floatValue_) };
171
172     SerializerValues endianessSwappedValues_ = {
173         true,
174         0x78,
175         0x78,
176         static_cast<unsigned short>(c_int16ValueSwapped),
177         c_int32ValueSwapped,
178         c_intAndFloat32Swapped.floatValue_,
179         c_int64ValueSwapped,
180         c_intAndFloat64Swapped.doubleValue_,
181         integerSizeDependentTestingValueEndianessSwapped(),
182         std::is_same_v<real, float> ? static_cast<real>(c_intAndFloat32Swapped.floatValue_)
183                                     : static_cast<real>(c_intAndFloat64Swapped.doubleValue_)
184     };
185 };
186
187 TEST_F(InMemorySerializerTest, Roundtrip)
188 {
189     InMemorySerializer serializer;
190     SerializerValues   values = defaultValues_;
191     serialize(&serializer, &values);
192
193     auto buffer = serializer.finishAndGetBuffer();
194
195     InMemoryDeserializer deserializer(buffer, std::is_same_v<real, double>);
196
197     SerializerValues deserialisedValues = deserialize(&deserializer);
198
199     checkSerializerValuesforEquality(values, deserialisedValues);
200 }
201
202 TEST_F(InMemorySerializerTest, RoundtripWithEndianessSwap)
203 {
204     InMemorySerializer serializerWithSwap(EndianSwapBehavior::Swap);
205     SerializerValues   values = defaultValues_;
206     serialize(&serializerWithSwap, &values);
207
208     auto buffer = serializerWithSwap.finishAndGetBuffer();
209
210     InMemoryDeserializer deserializerWithSwap(
211             buffer, std::is_same_v<real, double>, EndianSwapBehavior::Swap);
212
213     SerializerValues deserialisedValues = deserialize(&deserializerWithSwap);
214
215     checkSerializerValuesforEquality(values, deserialisedValues);
216 }
217
218 TEST_F(InMemorySerializerTest, SerializerExplicitEndianessSwap)
219 {
220     InMemorySerializer serializerWithSwap(EndianSwapBehavior::Swap);
221     SerializerValues   values = defaultValues_;
222     serialize(&serializerWithSwap, &values);
223
224     auto buffer = serializerWithSwap.finishAndGetBuffer();
225
226     InMemoryDeserializer deserializerWithOutSwap(buffer, std::is_same_v<real, double>);
227
228     SerializerValues deserialisedValues = deserialize(&deserializerWithOutSwap);
229     checkSerializerValuesforEquality(endianessSwappedValues_, deserialisedValues);
230 }
231
232 TEST_F(InMemorySerializerTest, DeserializerExplicitEndianessSwap)
233 {
234     InMemorySerializer serializer;
235     SerializerValues   values = defaultValues_;
236     serialize(&serializer, &values);
237
238     auto buffer = serializer.finishAndGetBuffer();
239
240     InMemoryDeserializer deserializerWithSwap(
241             buffer, std::is_same_v<real, double>, EndianSwapBehavior::Swap);
242
243     SerializerValues deserialisedValues = deserialize(&deserializerWithSwap);
244     checkSerializerValuesforEquality(endianessSwappedValues_, deserialisedValues);
245 }
246
247 TEST_F(InMemorySerializerTest, SizeIsCorrect)
248 {
249     InMemorySerializer serializer;
250     // These types all have well-defined widths in bytes,
251     // which we can test below.
252     serializer.doBool(&defaultValues_.boolValue_);     // 1 bytes
253     serializer.doChar(&defaultValues_.charValue_);     // 1 bytes
254     serializer.doInt32(&defaultValues_.int32Value_);   // 4 bytes
255     serializer.doInt64(&defaultValues_.int64Value_);   // 8 bytes
256     serializer.doFloat(&defaultValues_.floatValue_);   // 4 bytes
257     serializer.doDouble(&defaultValues_.doubleValue_); // 8 bytes
258     std::vector<char> charBuffer = { 'a', 'b', 'c' };
259     serializer.doCharArray(charBuffer.data(), charBuffer.size()); // 3 bytes
260     serializer.doOpaque(charBuffer.data(), charBuffer.size());    // 3 bytes
261     std::vector<int32_t> int32Buffer = { 0x1BCDEF78, 0x654321FE };
262     serializer.doInt32Array(int32Buffer.data(), int32Buffer.size()); // 8 bytes
263     std::vector<int64_t> int64Buffer = { 0x1BCDEF78654321FE, 0x3726ABFEAB34716C };
264     serializer.doInt64Array(int64Buffer.data(), int64Buffer.size()); // 16 bytes
265     auto buffer = serializer.finishAndGetBuffer();
266     EXPECT_EQ(buffer.size(), 56);
267 }
268
269 } // namespace
270 } // namespace test
271 } // namespace gmx