2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019, 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 #include "gromacs/mdlib/ebin.h"
41 #include <gtest/gtest.h>
43 #include "gromacs/utility/textreader.h"
44 #include "gromacs/utility/unique_cptr.h"
46 #include "testutils/refdata.h"
47 #include "testutils/testasserts.h"
48 #include "testutils/testfilemanager.h"
57 //! Wraps fclose to discard the return value to use it as a deleter with gmx::unique_cptr.
58 void fcloseWrapper(FILE* fp)
63 class PrEbinTest : public ::testing::Test
66 TestFileManager fileManager_;
68 // TODO This will be more elegant (and run faster) when we
69 // refactor the output routines to write to a stream
70 // interface, which can already be handled in-memory when
72 std::string logFilename_;
74 unique_cptr<FILE, fcloseWrapper> logFileGuard_;
76 TestReferenceData refData_;
77 TestReferenceChecker checker_;
80 logFilename_(fileManager_.getTemporaryFilePath(".log")),
81 log_(std::fopen(logFilename_.c_str(), "w")),
83 checker_(refData_.rootChecker())
88 TEST_F(PrEbinTest, HandlesAverages)
90 ASSERT_NE(log_, nullptr);
92 t_ebin* ebin = mk_ebin();
93 unique_cptr<t_ebin, done_ebin> ebinGuard(ebin);
95 // Set up the energy entries
96 const char* firstName[] = { "first" };
97 const char* secondName[] = { "second" };
98 int first = get_ebin_space(ebin, 1, firstName, nullptr);
99 int second = get_ebin_space(ebin, 1, secondName, nullptr);
101 // Put some data into the energy entries
102 const real timevalues[2][2] = { { 1.0, 20.0 }, { 2.0, 40.0 } };
103 gmx_bool bSum = true;
104 for (const auto& values : timevalues)
106 add_ebin(ebin, first, 1, &values[0], bSum);
107 add_ebin(ebin, second, 1, &values[1], bSum);
108 ebin_increase_count(1, ebin, bSum);
112 pr_ebin(log_, ebin, 0, 2, 5, eprAVER, true);
114 // We need to close the file before the contents are available.
115 logFileGuard_.reset(nullptr);
117 checker_.checkInteger(ebin->nener, "Number of Energy Terms");
118 checker_.checkString(TextReader::readFileToString(logFilename_), "log");
121 TEST_F(PrEbinTest, HandlesEmptyAverages)
123 ASSERT_NE(log_, nullptr);
125 t_ebin* ebin = mk_ebin();
126 unique_cptr<t_ebin, done_ebin> ebinGuard(ebin);
128 // Set up the energy entries
129 const char* firstName[] = { "first" };
130 const char* secondName[] = { "second" };
131 get_ebin_space(ebin, 1, firstName, nullptr);
132 get_ebin_space(ebin, 1, secondName, nullptr);
135 pr_ebin(log_, ebin, 0, 2, 5, eprAVER, true);
137 // We need to close the file before the contents are available.
138 logFileGuard_.reset(nullptr);
140 checker_.checkInteger(ebin->nener, "Number of Energy Terms");
141 checker_.checkString(TextReader::readFileToString(logFilename_), "log");