2 * This file is part of the GROMACS molecular simulation package.
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.
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 * Tests for gmx::InMemorySerializer and InMemoryDeserializer.
39 * \author Christian Blau <blau@kth.se>
40 * \ingroup module_utility
45 #include "gromacs/utility/inmemoryserializer.h"
47 #include <gmock/gmock.h>
48 #include <gtest/gtest.h>
58 std::int32_t int32Value_;
64 std::int64_t int64Value_;
68 //! Constants used for testing endian swap operations
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);
77 constexpr const IntAndFloat32 c_intAndFloat32{ c_int32Value };
78 constexpr const IntAndFloat64 c_intAndFloat64{ c_int64Value };
80 constexpr const IntAndFloat32 c_intAndFloat32Swapped{ c_int32ValueSwapped };
81 constexpr const IntAndFloat64 c_intAndFloat64Swapped{ c_int64ValueSwapped };
84 //! Return the integer used for testing, depending on the size of int.
85 constexpr int integerSizeDependentTestingValue()
87 return sizeof(int) == 4 ? c_int32Value : sizeof(int) == 8 ? c_int64Value : c_int16Value;
90 //! Return the endianess-swapped integer used for testing, depending on the size of int.
91 constexpr int integerSizeDependentTestingValueEndianessSwapped()
93 return sizeof(int) == 4 ? c_int32ValueSwapped
94 : sizeof(int) == 8 ? c_int64ValueSwapped
95 : c_int16ValueSwapped;
98 class InMemorySerializerTest : public ::testing::Test
101 struct SerializerValues
104 unsigned char unsignedCharValue_;
106 unsigned short unsignedShortValue_;
107 std::int32_t int32Value_;
109 std::int64_t int64Value_;
115 static void serialize(ISerializer* serializer, SerializerValues* values)
117 EXPECT_FALSE(serializer->reading());
118 doValues(serializer, values);
121 static SerializerValues deserialize(ISerializer* serializer)
123 EXPECT_TRUE(serializer->reading());
124 SerializerValues result;
125 doValues(serializer, &result);
129 static void checkSerializerValuesforEquality(const SerializerValues& lhs, const SerializerValues& rhs)
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_);
144 static void doValues(ISerializer* serializer, SerializerValues* values)
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_);
159 SerializerValues defaultValues_ = { true,
162 static_cast<unsigned short>(c_int16Value),
164 c_intAndFloat32.floatValue_,
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_) };
172 SerializerValues endianessSwappedValues_ = {
176 static_cast<unsigned short>(c_int16ValueSwapped),
178 c_intAndFloat32Swapped.floatValue_,
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_)
187 TEST_F(InMemorySerializerTest, Roundtrip)
189 InMemorySerializer serializer;
190 SerializerValues values = defaultValues_;
191 serialize(&serializer, &values);
193 auto buffer = serializer.finishAndGetBuffer();
195 InMemoryDeserializer deserializer(buffer, std::is_same_v<real, double>);
197 SerializerValues deserialisedValues = deserialize(&deserializer);
199 checkSerializerValuesforEquality(values, deserialisedValues);
202 TEST_F(InMemorySerializerTest, RoundtripWithEndianessSwap)
204 InMemorySerializer serializerWithSwap(EndianSwapBehavior::Swap);
205 SerializerValues values = defaultValues_;
206 serialize(&serializerWithSwap, &values);
208 auto buffer = serializerWithSwap.finishAndGetBuffer();
210 InMemoryDeserializer deserializerWithSwap(
211 buffer, std::is_same_v<real, double>, EndianSwapBehavior::Swap);
213 SerializerValues deserialisedValues = deserialize(&deserializerWithSwap);
215 checkSerializerValuesforEquality(values, deserialisedValues);
218 TEST_F(InMemorySerializerTest, SerializerExplicitEndianessSwap)
220 InMemorySerializer serializerWithSwap(EndianSwapBehavior::Swap);
221 SerializerValues values = defaultValues_;
222 serialize(&serializerWithSwap, &values);
224 auto buffer = serializerWithSwap.finishAndGetBuffer();
226 InMemoryDeserializer deserializerWithOutSwap(buffer, std::is_same_v<real, double>);
228 SerializerValues deserialisedValues = deserialize(&deserializerWithOutSwap);
229 checkSerializerValuesforEquality(endianessSwappedValues_, deserialisedValues);
232 TEST_F(InMemorySerializerTest, DeserializerExplicitEndianessSwap)
234 InMemorySerializer serializer;
235 SerializerValues values = defaultValues_;
236 serialize(&serializer, &values);
238 auto buffer = serializer.finishAndGetBuffer();
240 InMemoryDeserializer deserializerWithSwap(
241 buffer, std::is_same_v<real, double>, EndianSwapBehavior::Swap);
243 SerializerValues deserialisedValues = deserialize(&deserializerWithSwap);
244 checkSerializerValuesforEquality(endianessSwappedValues_, deserialisedValues);
247 TEST_F(InMemorySerializerTest, SizeIsCorrect)
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);