OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestBCHandler.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2#include <mpi.h>
3#include <memory>
4#include <sstream>
5
6#include "Ippl.h"
8
9class BCHandlerTest : public ::testing::Test {
10protected:
11 static void SetUpTestSuite() {
12 int argc = 0;
13 char** argv = nullptr;
14 ippl::initialize(argc, argv);
15 }
16
17 static void TearDownTestSuite() { ippl::finalize(); }
18
19 void SetUp() override {
20 // Nothing special needed for BCHandler tests
21 }
22
23 void TearDown() override {
24 // Nothing special needed for BCHandler tests
25 }
26};
27
28// ============================================================================
29// Tests for BCType enum and strToBCType
30// ============================================================================
31
32TEST_F(BCHandlerTest, EnumValues) {
33 EXPECT_EQ(BCHandler<3>::OPEN, 0);
34 EXPECT_EQ(BCHandler<3>::PERIODIC, 1);
35 EXPECT_EQ(BCHandler<3>::DIRICHLET, 2);
36}
37
38TEST_F(BCHandlerTest, strToBCTypeValidOPEN) {
39 auto bc = BCHandler<3>::strToBCType("OPEN");
40 EXPECT_EQ(bc, BCHandler<3>::OPEN);
41}
42
43TEST_F(BCHandlerTest, strToBCTypeValidPERIODIC) {
44 auto bc = BCHandler<3>::strToBCType("PERIODIC");
45 EXPECT_EQ(bc, BCHandler<3>::PERIODIC);
46}
47
48TEST_F(BCHandlerTest, strToBCTypeValidDIRICHLET) {
49 auto bc = BCHandler<3>::strToBCType("DIRICHLET");
50 EXPECT_EQ(bc, BCHandler<3>::DIRICHLET);
51}
52
53TEST_F(BCHandlerTest, strToBCTypeInvalid) {
54 EXPECT_THROW(BCHandler<3>::strToBCType("INVALID"), OpalException);
55 EXPECT_THROW(BCHandler<3>::strToBCType("periodic"), OpalException);
56 EXPECT_THROW(BCHandler<3>::strToBCType("Open"), OpalException);
58}
59
60// ============================================================================
61// Tests for Constructors
62// ============================================================================
63
64TEST_F(BCHandlerTest, VariadicConstructor3D_AllOPEN) {
66
67 EXPECT_TRUE(handler.isAll(BCHandler<3>::OPEN));
68 EXPECT_FALSE(handler.isAll(BCHandler<3>::PERIODIC));
69}
70
71TEST_F(BCHandlerTest, VariadicConstructor3D_AllPERIODIC) {
73
74 EXPECT_TRUE(handler.isAll(BCHandler<3>::PERIODIC));
75 EXPECT_FALSE(handler.isAll(BCHandler<3>::OPEN));
76}
77
78TEST_F(BCHandlerTest, VariadicConstructor3D_Mixed) {
80
81 EXPECT_FALSE(handler.isAll(BCHandler<3>::OPEN));
82 EXPECT_FALSE(handler.isAll(BCHandler<3>::PERIODIC));
83 EXPECT_FALSE(handler.isAll(BCHandler<3>::DIRICHLET));
84}
85
86TEST_F(BCHandlerTest, VariadicConstructor1D) {
88 EXPECT_TRUE(handler.isAll(BCHandler<1>::PERIODIC));
89}
90
91TEST_F(BCHandlerTest, VariadicConstructor2D) {
93 EXPECT_FALSE(handler.isAll(BCHandler<2>::OPEN));
94 EXPECT_FALSE(handler.isAll(BCHandler<2>::PERIODIC));
95}
96
97TEST_F(BCHandlerTest, VariadicConstructorWrongNumberOfArgs) {
98 // 3D handler requires exactly 3 BCs
99 // Note: Passing wrong number of args is a compile-time error in the constructor,
100 // so we only test with 2 args (1D handler expects 1, 2D expects 2, etc.).
101 // The compile-time check is adequate for this validation.
102}
103
104TEST_F(BCHandlerTest, CopyConstructor) {
106
107 BCHandler<3> handler2 = handler1;
108
109 EXPECT_TRUE(handler2.isAll(BCHandler<3>::OPEN) == false);
110 EXPECT_TRUE(handler1.isAll(BCHandler<3>::OPEN) == handler2.isAll(BCHandler<3>::OPEN));
111}
112
113// ============================================================================
114// Tests for isAll method
115// ============================================================================
116
117TEST_F(BCHandlerTest, isAllReturnsTrueWhenAllMatch) {
119
120 EXPECT_TRUE(handler.isAll(BCHandler<3>::OPEN));
121}
122
123TEST_F(BCHandlerTest, isAllReturnsFalseWhenNotAllMatch) {
125
126 EXPECT_FALSE(handler.isAll(BCHandler<3>::OPEN));
127 EXPECT_FALSE(handler.isAll(BCHandler<3>::PERIODIC));
128 EXPECT_FALSE(handler.isAll(BCHandler<3>::DIRICHLET));
129}
130
131TEST_F(BCHandlerTest, isAllForMultipleDimensions) {
133 EXPECT_TRUE(handler1.isAll(BCHandler<1>::PERIODIC));
134
136 EXPECT_TRUE(handler2.isAll(BCHandler<2>::PERIODIC));
137
139 EXPECT_TRUE(handler3.isAll(BCHandler<3>::PERIODIC));
140}
141
142// ============================================================================
143// Tests for isAllEqual method
144// ============================================================================
145
146TEST_F(BCHandlerTest, isAllEqualReturnsTrueWhenAllEqual) {
148
149 EXPECT_TRUE(handler.isAllEqual());
150}
151
152TEST_F(BCHandlerTest, isAllEqualReturnsFalseWhenNotAllEqual) {
154
155 EXPECT_FALSE(handler.isAllEqual());
156}
157
158TEST_F(BCHandlerTest, isAllEqualWithDifferentFirstElement) {
160
161 EXPECT_FALSE(handler.isAllEqual());
162}
163
164TEST_F(BCHandlerTest, isAllEqualWithDifferentLastElement) {
166
167 EXPECT_FALSE(handler.isAllEqual());
168}
169
170TEST_F(BCHandlerTest, isAllEqualForDifferentDimensions) {
172 EXPECT_TRUE(handler1.isAllEqual());
173
175 EXPECT_TRUE(handler2.isAllEqual());
176
178 EXPECT_FALSE(handler3.isAllEqual());
179}
180
181// ============================================================================
182// Tests for Stream Output Operator
183// ============================================================================
184
185TEST_F(BCHandlerTest, StreamOutputAllOPEN) {
187
188 std::ostringstream oss;
189 oss << handler;
190 std::string output = oss.str();
191
192 EXPECT_NE(output.find("BCHandler<3>"), std::string::npos);
193 EXPECT_NE(output.find("OPEN"), std::string::npos);
194}
195
196TEST_F(BCHandlerTest, StreamOutputMixed) {
198
199 std::ostringstream oss;
200 oss << handler;
201 std::string output = oss.str();
202
203 EXPECT_NE(output.find("BCHandler<3>"), std::string::npos);
204 EXPECT_NE(output.find("OPEN"), std::string::npos);
205 EXPECT_NE(output.find("PERIODIC"), std::string::npos);
206 EXPECT_NE(output.find("DIRICHLET"), std::string::npos);
207}
208
209TEST_F(BCHandlerTest, StreamOutputFormat) {
211
212 std::ostringstream oss;
213 oss << handler;
214 std::string output = oss.str();
215
216 EXPECT_NE(output.find("["), std::string::npos);
217 EXPECT_NE(output.find("]"), std::string::npos);
218}
219
220TEST_F(BCHandlerTest, StreamOutput1D) {
222
223 std::ostringstream oss;
224 oss << handler;
225 std::string output = oss.str();
226
227 EXPECT_NE(output.find("BCHandler<1>"), std::string::npos);
228}
229
230TEST_F(BCHandlerTest, StreamOutput2D) {
232
233 std::ostringstream oss;
234 oss << handler;
235 std::string output = oss.str();
236
237 EXPECT_NE(output.find("BCHandler<2>"), std::string::npos);
238}
239
240// ============================================================================
241// Note on toIPPLBConds method testing
242// ============================================================================
243// The toIPPLBConds() method is inherently tied to IPPL's Field type system.
244// Testing this method properly requires instantiating a full IPPL Field with
245// Mesh and Centering types, which is beyond the scope of unit testing BCHandler
246// in isolation. In integration/system testing, the method is validated through
247// FieldContainer and field solver tests that exercise the full BC pipeline.
248
249TEST_F(BCHandlerTest, ToIPPLBCondsIntegrationNote) {
250 SUCCEED() << "toIPPLBConds() is hopefully tested indirectly through"
251 " FieldContainer and FieldSolver integration tests.";
252}
253
254// ============================================================================
255// Integration Tests
256// ============================================================================
257
258TEST_F(BCHandlerTest, ConsistencyCheckFromString) {
259 auto bc = BCHandler<3>::strToBCType("PERIODIC");
260 BCHandler<3> handler(bc, bc, bc);
261
262 EXPECT_TRUE(handler.isAll(BCHandler<3>::PERIODIC));
263 EXPECT_TRUE(handler.isAllEqual());
264}
265
266TEST_F(BCHandlerTest, CopyConstructorPreservesState) {
268
269 BCHandler<3> copied = original;
270
271 // Both should have the same string representation
272 std::ostringstream oss1, oss2;
273 oss1 << original;
274 oss2 << copied;
275
276 EXPECT_EQ(oss1.str(), oss2.str());
277}
278
279TEST_F(BCHandlerTest, MultiDimensionalConstructors) {
280 // Test 1D, 2D, 3D all work correctly
282 EXPECT_TRUE(h1.isAllEqual());
283
285 EXPECT_TRUE(h2.isAllEqual());
286
288 EXPECT_TRUE(h3.isAllEqual());
289}
290
291TEST_F(BCHandlerTest, ExceptionMessageQuality) {
292 try {
293 BCHandler<3>::strToBCType("UNKNOWN_BC");
294 FAIL() << "Expected OpalException to be thrown";
295 } catch (const OpalException& e) {
296 std::string msg = e.what();
297 EXPECT_NE(msg.find("Unknown boundary condition type"), std::string::npos);
298 }
299}
TEST_F(BCHandlerTest, EnumValues)
static void TearDownTestSuite()
static void SetUpTestSuite()
void SetUp() override
void TearDown() override
Handler for boundary conditions per spatial dimension.
Definition BCHandler.hpp:29
bool isAllEqual() const
Check whether all stored BCs are equal (all the same value).
static BCType strToBCType(const std::string &str)
Convert a textual boundary-condition name to BCType enum.
Definition BCHandler.hpp:55
bool isAll(BCType bc_type) const
Return true if every stored BC equals bc_type.
Definition BCHandler.hpp:98