1 /*
2 * This file is part of the GROMACS molecular simulation package.
3 *
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.
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 #include "gmxpre.h"
36
37 #include "gromacs/mdlib/ebin.h"
38
39 #include <cstdio>
40
41 #include <gtest/gtest.h>
42
43 #include "gromacs/utility/textreader.h"
44 #include "gromacs/utility/unique_cptr.h"
45
46 #include "testutils/refdata.h"
47 #include "testutils/testasserts.h"
48 #include "testutils/testfilemanager.h"
49
50 namespace gmx
51 {
52 namespace test
53 {
54 namespace
55 {
56
57 //! Wraps fclose to discard the return value to use it as a deleter with gmx::unique_cptr.
fcloseWrapper(FILE * fp)58 void fcloseWrapper(FILE* fp)
59 {
60 fclose(fp);
61 }
62
63 class PrEbinTest : public ::testing::Test
64 {
65 public:
66 TestFileManager fileManager_;
67
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
71 // running tests.
72 std::string logFilename_;
73 FILE* log_;
74 unique_cptr<FILE, fcloseWrapper> logFileGuard_;
75
76 TestReferenceData refData_;
77 TestReferenceChecker checker_;
78
PrEbinTest()79 PrEbinTest() :
80 logFilename_(fileManager_.getTemporaryFilePath(".log")),
81 log_(std::fopen(logFilename_.c_str(), "w")),
82 logFileGuard_(log_),
83 checker_(refData_.rootChecker())
84 {
85 }
86 };
87
TEST_F(PrEbinTest,HandlesAverages)88 TEST_F(PrEbinTest, HandlesAverages)
89 {
90 ASSERT_NE(log_, nullptr);
91
92 t_ebin* ebin = mk_ebin();
93 unique_cptr<t_ebin, done_ebin> ebinGuard(ebin);
94
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);
100
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)
105 {
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);
109 }
110
111 // Test pr_ebin
112 pr_ebin(log_, ebin, 0, 2, 5, eprAVER, true);
113
114 // We need to close the file before the contents are available.
115 logFileGuard_.reset(nullptr);
116
117 checker_.checkInteger(ebin->nener, "Number of Energy Terms");
118 checker_.checkString(TextReader::readFileToString(logFilename_), "log");
119 }
120
TEST_F(PrEbinTest,HandlesEmptyAverages)121 TEST_F(PrEbinTest, HandlesEmptyAverages)
122 {
123 ASSERT_NE(log_, nullptr);
124
125 t_ebin* ebin = mk_ebin();
126 unique_cptr<t_ebin, done_ebin> ebinGuard(ebin);
127
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);
133
134 // Test pr_ebin
135 pr_ebin(log_, ebin, 0, 2, 5, eprAVER, true);
136
137 // We need to close the file before the contents are available.
138 logFileGuard_.reset(nullptr);
139
140 checker_.checkInteger(ebin->nener, "Number of Energy Terms");
141 checker_.checkString(TextReader::readFileToString(logFilename_), "log");
142 }
143
144 } // namespace
145 } // namespace test
146 } // namespace gmx
147