2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
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 * Implements tests for xvg file operations
39 * \author Joe Jordan <ejjordan@kth.se>
45 #include <gtest/gtest.h>
47 #include "gromacs/fileio/xvgr.h"
48 #include "gromacs/utility/smalloc.h"
49 #include "gromacs/utility/textwriter.h"
51 #include "testutils/testfilemanager.h"
52 #include "testutils/testoptions.h"
59 class XvgioTest : public ::testing::Test
62 XvgioTest() { referenceFilename_ = fileManager_.getTemporaryFilePath("ref.xvg"); }
64 const std::string& referenceFilename() const { return referenceFilename_; }
66 const std::string& referenceContents() const { return referenceContents_; }
68 void useStringAsXvgFile(const std::string& xvgString) { referenceContents_ = xvgString; }
72 gmx::TextWriter::writeFileFromString(referenceFilename(), referenceContents());
75 static void compareValues(basic_mdspan<const double, dynamicExtents2D> ref,
76 basic_mdspan<const double, dynamicExtents2D> test)
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));
85 for (std::ptrdiff_t i = 0; i < ref.extent(0); i++)
87 for (std::ptrdiff_t j = 0; j < ref.extent(1); j++)
89 EXPECT_DOUBLE_EQ(ref[i][j], test[j][i]);
95 gmx::test::TestFileManager fileManager_;
96 std::string referenceFilename_;
97 std::string referenceContents_;
100 TEST_F(XvgioTest, readXvgIntWorks)
106 MultiDimArray<std::vector<double>, dynamicExtents2D> xvgTestData = readXvgData(referenceFilename());
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);
113 compareValues(xvgRefData.asConstView(), xvgTestData.asConstView());
116 TEST_F(XvgioTest, readXvgRealWorks)
123 MultiDimArray<std::vector<double>, dynamicExtents2D> xvgTestData = readXvgData(referenceFilename());
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 {
132 compareValues(xvgRefData.asConstView(), xvgTestData.asConstView());
135 TEST_F(XvgioTest, readXvgIgnoreCommentLineWorks)
143 MultiDimArray<std::vector<double>, dynamicExtents2D> xvgTestData = readXvgData(referenceFilename());
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);
150 compareValues(xvgRefData.asConstView(), xvgTestData.asConstView());
153 // TODO Remove this test once all calls to read_xvg have been ported to readXvgData
154 TEST_F(XvgioTest, readXvgDeprecatedWorks)
160 std::vector<std::vector<double>> xvgData = { { 1, 4 }, { 2, 5 }, { 3, 6 } };
162 double** xvgTestData = nullptr;
164 int testNumRows = read_xvg(referenceFilename().c_str(), &xvgTestData, &testNumColumns);
166 double** xvgRefData = nullptr;
167 int refNumColumns = 3;
170 EXPECT_EQ(refNumColumns, testNumColumns);
171 EXPECT_EQ(refNumRows, testNumRows);
173 // Set the reference data
174 snew(xvgRefData, refNumColumns);
175 for (int column = 0; column < refNumColumns; column++)
177 snew(xvgRefData[column], refNumRows);
178 for (int row = 0; row < refNumRows; row++)
180 xvgRefData[column][row] = xvgData[column][row];
184 // Check that the reference and test data match
185 for (int column = 0; column < refNumColumns; column++)
187 for (int row = 0; row < refNumRows; row++)
189 EXPECT_EQ(xvgRefData[column][row], xvgTestData[column][row]);
193 // Free the reference and test data memory
194 for (int column = 0; column < refNumColumns; column++)
196 sfree(xvgRefData[column]);
197 sfree(xvgTestData[column]);