Merge branch origin/release-2020 into master
[alexxy/gromacs.git] / src / gromacs / fileio / tests / fileioxdrserializer.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020, 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::FileIOXdrSerializer.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \ingroup module_fileio
41  */
42
43 #include "gmxpre.h"
44
45 #include <gtest/gtest.h>
46
47 #include "gromacs/fileio/gmxfio.h"
48 #include "gromacs/fileio/gmxfio_xdr.h"
49 #include "gromacs/utility/futil.h"
50
51 #include "testutils/testfilemanager.h"
52
53 namespace gmx
54 {
55 namespace test
56 {
57 namespace
58 {
59 union IntAndFloat32 {
60     std::int32_t int32Value_;
61     float        floatValue_;
62 };
63
64 union IntAndFloat64 {
65     std::int64_t int64Value_;
66     double       doubleValue_;
67 };
68
69 //! Constants used for testing endian swap operations
70 //! \{
71 constexpr std::int16_t c_int16Value = static_cast<std::int16_t>(0x7A2B);
72 constexpr std::int32_t c_int32Value = static_cast<std::int32_t>(0x78ABCDEF);
73 constexpr std::int64_t c_int64Value = static_cast<std::int64_t>(0x78ABCDEF12345678);
74
75 constexpr const IntAndFloat32 c_intAndFloat32{ c_int32Value };
76 constexpr const IntAndFloat64 c_intAndFloat64{ c_int64Value };
77 //! \}
78
79 //! Return the integer used for testing, depending on the size of int.
80 constexpr int integerSizeDependentTestingValue()
81 {
82     return sizeof(int) == 4 ? c_int32Value : sizeof(int) == 8 ? c_int64Value : c_int16Value;
83 }
84
85 class FileIOXdrSerializerTest : public ::testing::Test
86 {
87 public:
88     ~FileIOXdrSerializerTest() override
89     {
90         if (file_)
91         {
92             gmx_fio_close(file_);
93         }
94     }
95     struct SerializerValues
96     {
97         bool           boolValue_          = true;
98         unsigned char  unsignedCharValue_  = 0x78;
99         char           charValue_          = 0x78;
100         unsigned short unsignedShortValue_ = static_cast<unsigned short>(c_int16Value);
101         std::int32_t   int32Value_         = c_int32Value;
102         float          floatValue_         = c_intAndFloat32.floatValue_;
103         std::int64_t   int64Value_         = c_int64Value;
104         double         doubleValue_        = c_intAndFloat64.doubleValue_;
105         int            intValue_           = integerSizeDependentTestingValue();
106         real           realValue_          = std::is_same<real, double>::value
107                                   ? static_cast<real>(c_intAndFloat64.doubleValue_)
108                                   : static_cast<real>(c_intAndFloat32.floatValue_);
109     } defaultValues_;
110
111     TestFileManager fileManager_;
112     // Make sure the file extension is one that gmx_fio_open will
113     // recognize to open as binary, even though we're just abusing it
114     // to write arbitrary XDR output.
115     std::string filename_ = fileManager_.getTemporaryFilePath("data.edr");
116     t_fileio*   file_     = nullptr;
117 };
118
119 TEST_F(FileIOXdrSerializerTest, SizeIsCorrect)
120 {
121     file_ = gmx_fio_open(filename_.c_str(), "w");
122     FileIOXdrSerializer serializer(file_);
123     // These types all have well-defined widths in bytes AFTER XDR serialization,
124     // which we can test below.
125     serializer.doBool(&defaultValues_.boolValue_);     // 4 bytes
126     serializer.doChar(&defaultValues_.charValue_);     // 4 bytes
127     serializer.doInt32(&defaultValues_.int32Value_);   // 4 bytes
128     serializer.doInt64(&defaultValues_.int64Value_);   // 8 bytes
129     serializer.doFloat(&defaultValues_.floatValue_);   // 4 bytes
130     serializer.doDouble(&defaultValues_.doubleValue_); // 8 bytes
131     std::vector<char> charBuffer = { 'a', 'b', 'c' };
132     serializer.doCharArray(charBuffer.data(), charBuffer.size()); // 12 bytes
133     serializer.doOpaque(charBuffer.data(), charBuffer.size());    // 4 bytes
134     std::vector<int32_t> int32Buffer = { 0x1BCDEF78, 0x654321FE };
135     serializer.doInt32Array(int32Buffer.data(), int32Buffer.size()); // 8 bytes
136     std::vector<int64_t> int64Buffer = { 0x1BCDEF78654321FE, 0x3726ABFEAB34716C };
137     serializer.doInt64Array(int64Buffer.data(), int64Buffer.size()); // 16 bytes
138     gmx_fio_close(file_);
139
140     file_ = gmx_fio_open(filename_.c_str(), "r");
141
142     // Determine file size
143     gmx_fseek(gmx_fio_getfp(file_), 0, SEEK_END);
144     gmx_off_t fileSize = gmx_fio_ftell(file_);
145     EXPECT_EQ(fileSize, 72);
146 }
147
148 } // namespace
149 } // namespace test
150 } // namespace gmx