OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestBinningCmd.cpp
Go to the documentation of this file.
1
26#include <gtest/gtest.h>
27
28#include "Ippl.h"
29
33
34#include <memory>
35#include <string>
36
37// Test-only helper exposing minimal setters for internal attributes.
39public:
40 void setMaxBins(int value) {
41 Attributes::setReal(itsAttr[BINNING::MAXBINS], static_cast<double>(value));
42 }
43
44 void setDesiredWidth(double value) {
46 }
47
48 void setBinningAlpha(double value) {
50 }
51
53
57
58 void setParameterString(const std::string& value) {
59 // PARAMETER is declared as a predefined string; this helper mirrors normal usage.
61 }
62
63 void setDumpBinsFile(const std::string& value) {
65 }
66
67 void setDumpBinsFreq(double value) {
69 }
70
74};
75
76class BinningCmdTest : public ::testing::Test {
77protected:
78 static void SetUpTestSuite() {
79 int argc = 0;
80 char** argv = nullptr;
81 ippl::initialize(argc, argv);
82 }
83
84 static void TearDownTestSuite() { ippl::finalize(); }
85};
86
87// UpdateAppendsJsonExtensionAndValidatesFreq:
88// If DUMPBINSFILE is set without .json, update() appends the extension and
89// enforces a valid DUMPBINSFREQ.
90TEST_F(BinningCmdTest, UpdateAppendsJsonExtensionAndValidatesFreq) {
91 TestableBinningCmd cmd;
92
93 // Enable dumping with a filename missing the .json suffix.
94 cmd.setDumpBinsFile("bins_output");
95 cmd.setDumpBinsFreq(5.0);
96
97 cmd.update();
98
99 EXPECT_TRUE(cmd.dumpBinsToFile());
100 EXPECT_EQ(cmd.getDumpBinsFileName(), "bins_output.json");
101 EXPECT_EQ(cmd.getDumpBinsFrequency(), 5);
102}
103
104// DumpBinsToFileRespectsNoneAndEmpty:
105// dumpBinsToFile() should be false for "NONE" or empty, true otherwise.
106TEST_F(BinningCmdTest, DumpBinsToFileRespectsNoneAndEmpty) {
107 TestableBinningCmd cmd;
108
109 // Default is "NONE".
110 EXPECT_FALSE(cmd.dumpBinsToFile());
111
112 cmd.setDumpBinsFile("");
113 EXPECT_FALSE(cmd.dumpBinsToFile());
114
115 cmd.setDumpBinsFile("bins.json");
116 EXPECT_TRUE(cmd.dumpBinsToFile());
117}
118
119// InvalidDumpFreqThrowsWhenDumpingEnabled:
120// A non-positive DUMPBINSFREQ with an active DUMPBINSFILE must raise.
121TEST_F(BinningCmdTest, InvalidDumpFreqThrowsWhenDumpingEnabled) {
122 TestableBinningCmd cmd;
123
124 cmd.setDumpBinsFile("bins.json");
125 cmd.setDumpBinsFreq(0.0);
126
127 EXPECT_THROW(cmd.update(), OpalException);
128}
129
130// InvalidTablePrintFrequencyThrowsWhenNegative:
131// Negative TABLEPRINTFREQ values are rejected by update().
132TEST_F(BinningCmdTest, InvalidTablePrintFrequencyThrowsWhenNegative) {
133 TestableBinningCmd cmd;
134
135 cmd.setTablePrintFrequency(-1.0);
136 EXPECT_THROW(cmd.update(), OpalException);
137}
138
139// ConstructionDefaults:
140// Verify that a freshly constructed exemplar has the documented defaults.
141TEST_F(BinningCmdTest, ConstructionDefaults) {
142 BinningCmd cmd;
143
144 EXPECT_EQ(cmd.getMaxBins(), 128);
145 EXPECT_DOUBLE_EQ(cmd.getDesiredWidth(), 0.1);
146 EXPECT_DOUBLE_EQ(cmd.getBinningAlpha(), 1.0);
147 EXPECT_DOUBLE_EQ(cmd.getBinningBeta(), 1.5);
148 EXPECT_TRUE(cmd.getAdaptiveBinning());
149
150 EXPECT_EQ(cmd.getParameter(), "VELOCITYZ");
152
153 EXPECT_EQ(cmd.getTablePrintFrequency(), 10);
154}
155
156// GettersReflectAttributes:
157// Changing the underlying attributes via the helper should be reflected by the getters.
158TEST_F(BinningCmdTest, GettersReflectAttributes) {
159 TestableBinningCmd cmd;
160
161 cmd.setMaxBins(42);
162 cmd.setDesiredWidth(0.25);
163 cmd.setBinningAlpha(0.5);
164 cmd.setBinningBeta(1.75);
165 cmd.setAdaptiveBinning(false);
166 cmd.setParameterString("PZ");
167 cmd.setTablePrintFrequency(3.0);
168
169 // update() refreshes the internal cached parameter string.
170 cmd.update();
171
172 EXPECT_EQ(cmd.getMaxBins(), 42);
173 EXPECT_DOUBLE_EQ(cmd.getDesiredWidth(), 0.25);
174 EXPECT_DOUBLE_EQ(cmd.getBinningAlpha(), 0.5);
175 EXPECT_DOUBLE_EQ(cmd.getBinningBeta(), 1.75);
176 EXPECT_FALSE(cmd.getAdaptiveBinning());
177
178 EXPECT_EQ(cmd.getParameter(), "PZ");
179
180 EXPECT_EQ(cmd.getTablePrintFrequency(), 3);
181}
182
183// ExecuteMapsKnownParameters:
184// For each supported PARAMETER option, execute() should set the correct enum.
185TEST_F(BinningCmdTest, ExecuteMapsKnownParameters) {
186 struct Case {
187 const char* name;
188 BinningParameter expected;
189 } cases[] = {
190 {"VELOCITYZ", BinningParameter::VELOCITYZ},
191 {"POSITIONZ", BinningParameter::POSITIONZ},
192 {"PZ", BinningParameter::PZ},
193 {"GAMMAZ", BinningParameter::GAMMAZ}};
194
195 for (const auto& c : cases) {
196 TestableBinningCmd cmd;
197 cmd.setParameterString(c.name);
198
199 // execute() calls setParameterType() and then update().
200 cmd.execute();
201
202 EXPECT_EQ(cmd.getParameter(), std::string(c.name));
203 EXPECT_EQ(cmd.getParameterType(), c.expected);
204 }
205}
206
207// ExecuteThrowsOnUnknownParameter:
208// An unsupported PARAMETER string should surface as an OpalException
209// when setParameterType() (via execute()) cannot map it.
210TEST_F(BinningCmdTest, ExecuteThrowsOnUnknownParameter) {
211 TestableBinningCmd cmd;
212
213 // This value is not present in the BinningParameter mapping table.
214 cmd.setParameterString("FOO");
215
216 EXPECT_THROW(cmd.execute(), OpalException);
217}
218
219// CloneCopiesState:
220// clone(name) should copy attribute state and cached parameter type/name.
221TEST_F(BinningCmdTest, CloneCopiesState) {
222 TestableBinningCmd original;
223 original.setMaxBins(7);
224 original.setDesiredWidth(0.3);
225 original.setBinningAlpha(0.8);
226 original.setBinningBeta(1.2);
227 original.setAdaptiveBinning(false);
228 original.setParameterString("POSITIONZ");
229
230 original.execute(); // ensure parameterType_m is updated
231
232 std::unique_ptr<BinningCmd> copy(original.clone("BINNING_COPY"));
233
234 ASSERT_NE(copy, nullptr);
235 EXPECT_EQ(copy->getOpalName(), "BINNING_COPY");
236
237 EXPECT_EQ(copy->getMaxBins(), original.getMaxBins());
238 EXPECT_DOUBLE_EQ(copy->getDesiredWidth(), original.getDesiredWidth());
239 EXPECT_DOUBLE_EQ(copy->getBinningAlpha(), original.getBinningAlpha());
240 EXPECT_DOUBLE_EQ(copy->getBinningBeta(), original.getBinningBeta());
241 EXPECT_EQ(copy->getAdaptiveBinning(), original.getAdaptiveBinning());
242
243 EXPECT_EQ(copy->getParameter(), original.getParameter());
244 EXPECT_EQ(copy->getParameterType(), original.getParameterType());
245}
246
247// PrintInfoDoesNotThrow:
248// Smoke test that printInfo can be called without throwing.
249TEST_F(BinningCmdTest, PrintInfoDoesNotThrow) {
250 BinningCmd cmd;
251
252 // Inform is defined in Ippl; using a simple instance here is sufficient
253 // to exercise the printing path.
254 Inform os("BinningCmd::printInfo");
255
256 EXPECT_NO_THROW({ cmd.printInfo(os); });
257}
BinningParameter
The parameter that is used for binning.
Definition BinningCmd.h:35
TEST_F(BinningCmdTest, UpdateAppendsJsonExtensionAndValidatesFreq)
static void TearDownTestSuite()
static void SetUpTestSuite()
double getDesiredWidth() const
double getBinningAlpha() const
int getTablePrintFrequency() const
int getMaxBins() const
bool getAdaptiveBinning() const
BinningParameter getParameterType() const
std::string getParameter()
double getBinningBeta() const
Inform & printInfo(Inform &os) const
std::vector< Attribute > itsAttr
The object attributes.
Definition Object.h:210
void setMaxBins(int value)
void setTablePrintFrequency(double value)
void setDumpBinsFile(const std::string &value)
void setBinningBeta(double value)
void setDumpBinsFreq(double value)
void setParameterString(const std::string &value)
void setBinningAlpha(double value)
void setAdaptiveBinning(bool value)
void setDesiredWidth(double value)
void setBool(Attribute &attr, bool val)
Set logical value.
void setString(Attribute &attr, const std::string &val)
Set string value.
void setReal(Attribute &attr, double val)
Set real value.
void setPredefinedString(Attribute &attr, const std::string &val)
Set predefined string value.
@ DESIREDWIDTH
Definition BinningCmd.h:41
@ BINNINGBETA
Definition BinningCmd.h:43
@ ADAPTIVEBINNING
Definition BinningCmd.h:45
@ BINNINGALPHA
Definition BinningCmd.h:42
@ DUMPBINSFILE
Definition BinningCmd.h:46
@ TABLEPRINTFREQ
Definition BinningCmd.h:48
@ PARAMETER
Definition BinningCmd.h:44
@ DUMPBINSFREQ
Definition BinningCmd.h:47