2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018, 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/mdebin.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 PrintEbin : public ::testing::Test
67 unique_cptr<t_ebin, done_ebin> ebinGuard_;
70 TestFileManager fileManager_;
71 // TODO This will be more elegant (and run faster) when we
72 // refactor the output routines to write to a stream
73 // interface, which can already be handled in-memory when
75 std::string logFilename_;
77 unique_cptr<FILE, fcloseWrapper> logFileGuard_;
79 TestReferenceData refData_;
80 TestReferenceChecker checker_;
82 PrintEbin() : ebin_ {0}, ebinGuard_(&ebin_), mdebin_ {0},
83 logFilename_(fileManager_.getTemporaryFilePath(".log")),
84 log_(std::fopen(logFilename_.c_str(), "w")), logFileGuard_(log_),
85 checker_(refData_.rootChecker())
87 mdebin_.ebin = &ebin_;
91 TEST_F(PrintEbin, print_ebin_HandlesEmptyAverages)
93 ASSERT_NE(log_, nullptr);
96 print_ebin(nullptr, false, false, false, log_,
98 &mdebin_, nullptr, nullptr, nullptr, nullptr);
100 // We need to close the file before the contents are available.
101 logFileGuard_.reset(nullptr);
103 checker_.checkString(TextReader::readFileToString(logFilename_), "log");
106 // TODO Ideally we'd test for proper output of a normal t_mdebin, but
107 // building one is a lot of work for limited value.
109 TEST_F(PrintEbin, pr_ebin_HandlesAverages)
111 ASSERT_NE(log_, nullptr);
113 // Set up the energy entries
114 const char *firstName[] = {"first"};
115 const char *secondName[] = {"second"};
116 int first = get_ebin_space(mdebin_.ebin, 1, firstName, nullptr);
117 int second = get_ebin_space(mdebin_.ebin, 1, secondName, nullptr);
119 // Put some data into the energy entries
120 const real timevalues[2][2] = { { 1.0, 10.0}, {2.0, 20.0} };
121 gmx_bool bSum = true;
122 for (const auto &values : timevalues)
124 add_ebin(mdebin_.ebin, first, 1, &values[0], bSum);
125 add_ebin(mdebin_.ebin, second, 1, &values[1], bSum);
126 ebin_increase_count(mdebin_.ebin, bSum);
130 pr_ebin(log_, &ebin_, 0, 2, 5, eprAVER, true);
132 // We need to close the file before the contents are available.
133 logFileGuard_.reset(nullptr);
135 checker_.checkString(TextReader::readFileToString(logFilename_), "log");
138 TEST_F(PrintEbin, pr_ebin_HandlesEmptyAverages)
140 ASSERT_NE(log_, nullptr);
142 // Set up the energy entries
143 const char *firstName[] = {"first"};
144 const char *secondName[] = {"second"};
145 get_ebin_space(mdebin_.ebin, 1, firstName, nullptr);
146 get_ebin_space(mdebin_.ebin, 1, secondName, nullptr);
149 pr_ebin(log_, &ebin_, 0, 2, 5, eprAVER, true);
151 // We need to close the file before the contents are available.
152 logFileGuard_.reset(nullptr);
154 checker_.checkString(TextReader::readFileToString(logFilename_), "log");