clang-tidy: google tests applicable
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / mdebin.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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/mdebin.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.
58 void fcloseWrapper(FILE *fp)
59 {
60     fclose(fp);
61 }
62
63 class PrintEbin : public ::testing::Test
64 {
65     public:
66         t_ebin                         ebin_;
67         unique_cptr<t_ebin, done_ebin> ebinGuard_;
68         t_mdebin                       mdebin_;
69
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
74         // running tests.
75         std::string logFilename_;
76         FILE       *log_;
77         unique_cptr<FILE, fcloseWrapper> logFileGuard_;
78
79         TestReferenceData                refData_;
80         TestReferenceChecker             checker_;
81
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())
86         {
87             mdebin_.ebin = &ebin_;
88         }
89 };
90
91 TEST_F(PrintEbin, print_ebin_HandlesEmptyAverages)
92 {
93     ASSERT_NE(log_, nullptr);
94
95     // Test print_ebin
96     print_ebin(nullptr, false, false, false, log_,
97                0, 0, eprAVER,
98                &mdebin_, nullptr, nullptr, nullptr, nullptr);
99
100     // We need to close the file before the contents are available.
101     logFileGuard_.reset(nullptr);
102
103     checker_.checkString(TextReader::readFileToString(logFilename_), "log");
104 }
105
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.
108
109 TEST_F(PrintEbin, pr_ebin_HandlesAverages)
110 {
111     ASSERT_NE(log_, nullptr);
112
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);
118
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)
123     {
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);
127     }
128
129     // Test pr_ebin
130     pr_ebin(log_, &ebin_, 0, 2, 5, eprAVER, true);
131
132     // We need to close the file before the contents are available.
133     logFileGuard_.reset(nullptr);
134
135     checker_.checkString(TextReader::readFileToString(logFilename_), "log");
136 }
137
138 TEST_F(PrintEbin, pr_ebin_HandlesEmptyAverages)
139 {
140     ASSERT_NE(log_, nullptr);
141
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);
147
148     // Test pr_ebin
149     pr_ebin(log_, &ebin_, 0, 2, 5, eprAVER, true);
150
151     // We need to close the file before the contents are available.
152     logFileGuard_.reset(nullptr);
153
154     checker_.checkString(TextReader::readFileToString(logFilename_), "log");
155 }
156
157 }  // namespace
158 }  // namespace test
159 }  // namespace gmx