Apply clang-11 fixes for fileio
[alexxy/gromacs.git] / src / gromacs / fileio / tests / xvgio.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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  * Implements tests for xvg file operations
38  *
39  * \author Joe Jordan <ejjordan@kth.se>
40  */
41 #include "gmxpre.h"
42
43 #include <numeric>
44
45 #include <gtest/gtest.h>
46
47 #include "gromacs/fileio/xvgr.h"
48 #include "gromacs/utility/smalloc.h"
49 #include "gromacs/utility/textwriter.h"
50
51 #include "testutils/testfilemanager.h"
52 #include "testutils/testoptions.h"
53
54 namespace gmx
55 {
56 namespace test
57 {
58
59 class XvgioTest : public ::testing::Test
60 {
61 public:
62     XvgioTest() { referenceFilename_ = fileManager_.getTemporaryFilePath("ref.xvg"); }
63
64     const std::string& referenceFilename() const { return referenceFilename_; }
65
66     const std::string& referenceContents() const { return referenceContents_; }
67
68     void useStringAsXvgFile(const std::string& xvgString) { referenceContents_ = xvgString; }
69
70     void writeXvgFile() const
71     {
72         gmx::TextWriter::writeFileFromString(referenceFilename(), referenceContents());
73     }
74
75     static void compareValues(basic_mdspan<const double, dynamicExtents2D> ref,
76                               basic_mdspan<const double, dynamicExtents2D> test)
77     {
78         // The xvg reading routines use a column-major layout, while we would
79         // like to enforce row major behaviour everywhere else. This requires
80         // this test to swap the orders between reference data and test data.
81         // Hence, we compare extent(0) with extent(1) and [i][j] with [j][i].
82         EXPECT_EQ(ref.extent(0), test.extent(1));
83         EXPECT_EQ(ref.extent(1), test.extent(0));
84
85         for (std::ptrdiff_t i = 0; i < ref.extent(0); i++)
86         {
87             for (std::ptrdiff_t j = 0; j < ref.extent(1); j++)
88             {
89                 EXPECT_DOUBLE_EQ(ref[i][j], test[j][i]);
90             }
91         }
92     }
93
94 private:
95     gmx::test::TestFileManager fileManager_;
96     std::string                referenceFilename_;
97     std::string                referenceContents_;
98 };
99
100 TEST_F(XvgioTest, readXvgIntWorks)
101 {
102     useStringAsXvgFile(
103             "1 2 3\n"
104             "4 5 6\n");
105     writeXvgFile();
106     MultiDimArray<std::vector<double>, dynamicExtents2D> xvgTestData = readXvgData(referenceFilename());
107
108     const int                                            numRows    = 2;
109     const int                                            numColumns = 3;
110     MultiDimArray<std::vector<double>, dynamicExtents2D> xvgRefData(numRows, numColumns);
111     std::iota(begin(xvgRefData), end(xvgRefData), 1);
112
113     compareValues(xvgRefData.asConstView(), xvgTestData.asConstView());
114 }
115
116 TEST_F(XvgioTest, readXvgRealWorks)
117 {
118     useStringAsXvgFile(
119             "1.1 2.2\n"
120             "3.3 4.4\n"
121             "5.5 6.6\n");
122     writeXvgFile();
123     MultiDimArray<std::vector<double>, dynamicExtents2D> xvgTestData = readXvgData(referenceFilename());
124
125     const int                                            numRows    = 3;
126     const int                                            numColumns = 2;
127     MultiDimArray<std::vector<double>, dynamicExtents2D> xvgRefData(numRows, numColumns);
128     std::generate(begin(xvgRefData), end(xvgRefData), [n = 0.0]() mutable {
129         n += 1.1;
130         return n;
131     });
132     compareValues(xvgRefData.asConstView(), xvgTestData.asConstView());
133 }
134
135 TEST_F(XvgioTest, readXvgIgnoreCommentLineWorks)
136 {
137     useStringAsXvgFile(
138             "1 2 3\n"
139             "#comment\n"
140             "4 5 6\n");
141     writeXvgFile();
142
143     MultiDimArray<std::vector<double>, dynamicExtents2D> xvgTestData = readXvgData(referenceFilename());
144
145     const int                                            numRows    = 2;
146     const int                                            numColumns = 3;
147     MultiDimArray<std::vector<double>, dynamicExtents2D> xvgRefData(numRows, numColumns);
148     std::iota(begin(xvgRefData), end(xvgRefData), 1);
149
150     compareValues(xvgRefData.asConstView(), xvgTestData.asConstView());
151 }
152
153 // TODO Remove this test once all calls to read_xvg have been ported to readXvgData
154 TEST_F(XvgioTest, readXvgDeprecatedWorks)
155 {
156     useStringAsXvgFile(
157             "1 2 3\n"
158             "4 5 6\n");
159     writeXvgFile();
160     std::vector<std::vector<double>> xvgData = { { 1, 4 }, { 2, 5 }, { 3, 6 } };
161
162     double** xvgTestData = nullptr;
163     int      testNumColumns;
164     int      testNumRows = read_xvg(referenceFilename().c_str(), &xvgTestData, &testNumColumns);
165
166     double** xvgRefData    = nullptr;
167     int      refNumColumns = 3;
168     int      refNumRows    = 2;
169
170     EXPECT_EQ(refNumColumns, testNumColumns);
171     EXPECT_EQ(refNumRows, testNumRows);
172
173     // Set the reference data
174     snew(xvgRefData, refNumColumns);
175     for (int column = 0; column < refNumColumns; column++)
176     {
177         snew(xvgRefData[column], refNumRows);
178         for (int row = 0; row < refNumRows; row++)
179         {
180             xvgRefData[column][row] = xvgData[column][row];
181         }
182     }
183
184     // Check that the reference and test data match
185     for (int column = 0; column < refNumColumns; column++)
186     {
187         for (int row = 0; row < refNumRows; row++)
188         {
189             EXPECT_EQ(xvgRefData[column][row], xvgTestData[column][row]);
190         }
191     }
192
193     // Free the reference and test data memory
194     for (int column = 0; column < refNumColumns; column++)
195     {
196         sfree(xvgRefData[column]);
197         sfree(xvgTestData[column]);
198     }
199     sfree(xvgRefData);
200     sfree(xvgTestData);
201 }
202
203 } // namespace test
204 } // namespace gmx