Code beautification with uncrustify
[alexxy/gromacs.git] / src / testutils / datatest.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implements classes in datatest.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_testutils
37  */
38 #include "datatest.h"
39
40 #include <gmock/gmock.h>
41 #include <gtest/gtest.h>
42
43 #include "gromacs/analysisdata/analysisdata.h"
44 #include "gromacs/analysisdata/paralleloptions.h"
45 #include "gromacs/utility/gmxassert.h"
46 #include "gromacs/utility/stringutil.h"
47
48 #include "testutils/mock_datamodule.h"
49 #include "testutils/refdata.h"
50
51 namespace gmx
52 {
53
54 namespace test
55 {
56
57 /********************************************************************
58  * AnalysisDataTestInputPointSet
59  */
60
61 AnalysisDataTestInputPointSet::AnalysisDataTestInputPointSet()
62 {
63 }
64
65
66 /********************************************************************
67  * AnalysisDataTestInputFrame
68  */
69
70 AnalysisDataTestInputFrame::AnalysisDataTestInputFrame(int index, real x)
71     : index_(index), x_(x)
72 {
73 }
74
75
76 /********************************************************************
77  * AnalysisDataTestInput
78  */
79
80 void AnalysisDataTestInput::initFromArray(const real *data, size_t count)
81 {
82     size_t columns = 0;
83
84     for (size_t i = 0; i < count; ++i)
85     {
86         if (data[i] == MPSTOP)
87         {
88             bMultipoint_ = true;
89             break;
90         }
91     }
92     for (size_t i = 0; i < count; )
93     {
94         frames_.push_back(AnalysisDataTestInputFrame(frames_.size(), data[i]));
95         AnalysisDataTestInputFrame &frame = frames_.back();
96         GMX_RELEASE_ASSERT(data[i] != END_OF_FRAME && data[i] != MPSTOP,
97                            "Empty data frame");
98         while (data[i] != END_OF_FRAME)
99         {
100             ++i;
101             frame.points_.push_back(AnalysisDataTestInputPointSet());
102             AnalysisDataTestInputPointSet &points = frame.points_.back();
103             while (data[i] != MPSTOP && data[i] != END_OF_FRAME)
104             {
105                 GMX_RELEASE_ASSERT(i < count,
106                                    "Premature end of data");
107                 points.y_.push_back(data[i]);
108                 ++i;
109             }
110             size_t frameColumns = points.y_.size();
111             GMX_RELEASE_ASSERT(frameColumns > 0U, "Empty data point set");
112             GMX_RELEASE_ASSERT(!(!bMultipoint_ && columns > 0U && columns != frameColumns),
113                                "Different frames have different number of columns");
114             if (columns < frameColumns)
115             {
116                 columns = frameColumns;
117             }
118         }
119         ++i;
120     }
121     GMX_RELEASE_ASSERT(!frames_.empty(), "Empty data");
122     columnCount_ = columns;
123 }
124
125
126 AnalysisDataTestInput::~AnalysisDataTestInput()
127 {
128 }
129
130
131 const AnalysisDataTestInputFrame &AnalysisDataTestInput::frame(int index) const
132 {
133     GMX_RELEASE_ASSERT(index >= 0 && index < frameCount(),
134                        "Out-of-range frame index");
135     return frames_[index];
136 }
137
138
139 /********************************************************************
140  * AnalysisDataTest
141  */
142
143 AnalysisDataTestFixture::AnalysisDataTestFixture()
144 {
145 }
146
147
148 void AnalysisDataTestFixture::presentAllData(const AnalysisDataTestInput &input,
149                                              AnalysisData                *data)
150 {
151     gmx::AnalysisDataParallelOptions options;
152     gmx::AnalysisDataHandle          handle = data->startData(options);
153     for (int row = 0; row < input.frameCount(); ++row)
154     {
155         presentDataFrame(input, row, handle);
156         EXPECT_EQ(row + 1, data->frameCount());
157     }
158     handle.finishData();
159 }
160
161
162 void AnalysisDataTestFixture::presentDataFrame(const AnalysisDataTestInput &input,
163                                                int row, AnalysisDataHandle handle)
164 {
165     const AnalysisDataTestInputFrame &frame = input.frame(row);
166     handle.startFrame(row, frame.x(), frame.dx());
167     for (int i = 0; i < frame.pointSetCount(); ++i)
168     {
169         const AnalysisDataTestInputPointSet &points = frame.points(i);
170         for (int j = 0; j < points.size(); ++j)
171         {
172             handle.setPoint(j, points.y(j), points.dy(j), points.present(j));
173         }
174         if (input.isMultipoint())
175         {
176             handle.finishPointSet();
177         }
178     }
179     handle.finishFrame();
180 }
181
182
183 void
184 AnalysisDataTestFixture::addStaticCheckerModule(const AnalysisDataTestInput &data,
185                                                 AbstractAnalysisData        *source)
186 {
187     MockAnalysisDataModulePointer module(new MockAnalysisDataModule(0));
188     module->setupStaticCheck(data, source);
189     source->addModule(module);
190 }
191
192
193 void
194 AnalysisDataTestFixture::addStaticColumnCheckerModule(const AnalysisDataTestInput &data,
195                                                       int firstcol, int n,
196                                                       AbstractAnalysisData *source)
197 {
198     MockAnalysisDataModulePointer module(new MockAnalysisDataModule(0));
199     module->setupStaticColumnCheck(data, firstcol, n, source);
200     source->addColumnModule(firstcol, n, module);
201 }
202
203
204 void
205 AnalysisDataTestFixture::addStaticStorageCheckerModule(const AnalysisDataTestInput &data,
206                                                        int                          storageCount,
207                                                        AbstractAnalysisData        *source)
208 {
209     MockAnalysisDataModulePointer module(new MockAnalysisDataModule(0));
210     module->setupStaticStorageCheck(data, storageCount, source);
211     source->addModule(module);
212 }
213
214
215 void
216 AnalysisDataTestFixture::addReferenceCheckerModule(TestReferenceChecker  checker,
217                                                    const char           *id,
218                                                    AbstractAnalysisData *source)
219 {
220     MockAnalysisDataModulePointer module(new MockAnalysisDataModule(0));
221     module->setupReferenceCheck(checker.checkCompound("AnalysisData", id), source);
222     source->addModule(module);
223 }
224
225
226 void
227 AnalysisDataTestFixture::addReferenceCheckerModule(const char           *id,
228                                                    AbstractAnalysisData *source)
229 {
230     addReferenceCheckerModule(data_.rootChecker(), id, source);
231 }
232
233 } // namespace test
234 } // namespace gmx