| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include "gtest/gtest.h" |
| 36 |
#include "Kokkos_Core.hpp" |
| 37 |
#include "stk_util/util/ReportHandler.hpp" // for set_assert_handler, ThrowRequireMsg, set_erro... |
| 38 |
#include "stk_util/ngp/NgpSpaces.hpp" |
| 39 |
#include <iostream> // for basic_ostream::operator<<, operator<<, ostrin... |
| 40 |
#include <stdexcept> // for logic_error, runtime_error, invalid_argument |
| 41 |
#include <string> // for string |
| 42 |
|
| 43 |
|
| 44 |
namespace { |
| 45 |
|
| 46 |
bool test_assert_handler_called = false; |
| 47 |
bool test_error_handler_called = false; |
| 48 |
bool test_invarg_handler_called = false; |
| 49 |
|
| 50 |
void |
| 51 |
test_assert_handler(const char* /*expr*/, |
| 52 |
const std::string& /*location*/, |
| 53 |
std::ostringstream& /*message*/) |
| 54 |
{ |
| 55 |
test_assert_handler_called = true; |
| 56 |
} |
| 57 |
|
| 58 |
void |
| 59 |
test_error_handler(const char* /*expr*/, |
| 60 |
const std::string& /*location*/, |
| 61 |
std::ostringstream& /*message*/) |
| 62 |
{ |
| 63 |
test_error_handler_called = true; |
| 64 |
} |
| 65 |
|
| 66 |
void |
| 67 |
test_invarg_handler(const char* /*expr*/, |
| 68 |
const std::string& /*location*/, |
| 69 |
std::ostringstream& /*message*/) |
| 70 |
{ |
| 71 |
test_invarg_handler_called = true; |
| 72 |
} |
| 73 |
|
| 74 |
void force_throw_require_trigger(bool msg = true) |
| 75 |
{ |
| 76 |
if (msg) { |
| 77 |
STK_ThrowRequireMsg(false, "Will always fail, this is for testing"); |
| 78 |
} |
| 79 |
else { |
| 80 |
STK_ThrowRequire(false); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void force_throw_error_trigger(bool msg = true) |
| 85 |
{ |
| 86 |
if (msg) { |
| 87 |
STK_ThrowErrorMsgIf(true, "Will always fail, this is for testing"); |
| 88 |
} |
| 89 |
else { |
| 90 |
STK_ThrowErrorIf(true); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void force_throw_invarg_trigger(bool msg = true) |
| 95 |
{ |
| 96 |
if (msg) { |
| 97 |
STK_ThrowInvalidArgMsgIf(true, "Will always fail, this is for testing"); |
| 98 |
} |
| 99 |
else { |
| 100 |
STK_ThrowInvalidArgIf(true); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void check_interaction_with_if(bool msg = true) |
| 105 |
{ |
| 106 |
if (msg) { |
| 107 |
if (true) |
| 108 |
STK_ThrowRequireMsg(false, "Will always fail, this is for testing"); |
| 109 |
} |
| 110 |
else { |
| 111 |
if (true) |
| 112 |
STK_ThrowRequire(false); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void force_throw_assert() |
| 117 |
{ |
| 118 |
STK_ThrowAssert(false); |
| 119 |
} |
| 120 |
|
| 121 |
void test_no_expr_error() |
| 122 |
{ |
| 123 |
STK_ThrowErrorMsg("message"); |
| 124 |
} |
| 125 |
|
| 126 |
} // namespace <empty> |
| 127 |
|
| 128 |
TEST(UnitTestingOfThrowMacros, testUnit) |
| 129 |
{ |
| 130 |
// Setting assert handler to NULL should cause exception |
| 131 |
ASSERT_THROW(stk::set_assert_handler(0), std::runtime_error); |
| 132 |
|
| 133 |
// Check that Throw*Msg works |
| 134 |
ASSERT_THROW(force_throw_require_trigger(), std::logic_error); |
| 135 |
ASSERT_THROW(force_throw_error_trigger(), std::runtime_error); |
| 136 |
ASSERT_THROW(force_throw_invarg_trigger(), std::invalid_argument); |
| 137 |
|
| 138 |
// Check that Throw* works |
| 139 |
ASSERT_THROW(force_throw_require_trigger(false), std::logic_error); |
| 140 |
ASSERT_THROW(force_throw_error_trigger(false), std::runtime_error); |
| 141 |
ASSERT_THROW(force_throw_invarg_trigger(false), std::invalid_argument); |
| 142 |
|
| 143 |
// Check that macro interacts appropriately with if statements |
| 144 |
ASSERT_THROW(check_interaction_with_if(), std::logic_error); |
| 145 |
ASSERT_THROW(check_interaction_with_if(false), std::logic_error); |
| 146 |
|
| 147 |
// Check that usage of ThrowRequireMsg/ThrowAssertMsg does not change program |
| 148 |
// semantics. Code blocks that are not contained within braces seem to be |
| 149 |
// the most likely to be problematic. |
| 150 |
|
| 151 |
bool expected_execution_path = false; |
| 152 |
if (false) |
| 153 |
STK_ThrowRequireMsg(false, "test"); |
| 154 |
else |
| 155 |
expected_execution_path = true; |
| 156 |
ASSERT_TRUE(expected_execution_path); |
| 157 |
|
| 158 |
expected_execution_path = false; |
| 159 |
if (false) |
| 160 |
STK_ThrowAssertMsg(false, "test"); |
| 161 |
else |
| 162 |
expected_execution_path = true; |
| 163 |
ASSERT_TRUE(expected_execution_path); |
| 164 |
|
| 165 |
expected_execution_path = false; |
| 166 |
if (false) |
| 167 |
STK_ThrowErrorMsg("test"); |
| 168 |
else |
| 169 |
expected_execution_path = true; |
| 170 |
ASSERT_TRUE(expected_execution_path); |
| 171 |
|
| 172 |
// These next four statements are to check compilation success |
| 173 |
|
| 174 |
if (false) |
| 175 |
STK_ThrowRequireMsg(false, "test"); |
| 176 |
|
| 177 |
if (false) |
| 178 |
STK_ThrowAssertMsg(false, "test"); |
| 179 |
|
| 180 |
if (false) |
| 181 |
STK_ThrowRequire(false); |
| 182 |
|
| 183 |
if (false) |
| 184 |
STK_ThrowAssert(false); |
| 185 |
|
| 186 |
// Check that do-while still works, again, we are mostly checking compilation |
| 187 |
// success here. |
| 188 |
|
| 189 |
do STK_ThrowRequireMsg(true, "test"); |
| 190 |
while (false); |
| 191 |
|
| 192 |
do STK_ThrowAssertMsg(true, "test"); |
| 193 |
while (false); |
| 194 |
|
| 195 |
// Check that message with put-tos compiles |
| 196 |
|
| 197 |
int temp = 0; |
| 198 |
STK_ThrowRequireMsg(true, "test: " << temp << " blah"); |
| 199 |
STK_ThrowAssertMsg(true, "test: " << temp << " blah"); |
| 200 |
|
| 201 |
// Check that assert behaves as expected (throws in debug, not in opt) |
| 202 |
#ifdef NDEBUG |
| 203 |
force_throw_assert(); |
| 204 |
#else |
| 205 |
ASSERT_THROW(force_throw_assert(), std::logic_error); |
| 206 |
#endif |
| 207 |
|
| 208 |
// Check that ThrowErrorMsg works |
| 209 |
|
| 210 |
ASSERT_THROW(test_no_expr_error(), std::runtime_error); |
| 211 |
|
| 212 |
// Check that setting handler for asserts works. |
| 213 |
|
| 214 |
stk::ErrorHandler orig = stk::set_assert_handler(test_assert_handler); |
| 215 |
|
| 216 |
STK_ThrowRequireMsg(false, "test"); |
| 217 |
|
| 218 |
ASSERT_TRUE(test_assert_handler_called); |
| 219 |
|
| 220 |
stk::set_assert_handler(orig); |
| 221 |
|
| 222 |
ASSERT_THROW(force_throw_require_trigger(), std::logic_error); |
| 223 |
|
| 224 |
// Check that setting handler for errors works. |
| 225 |
|
| 226 |
orig = stk::set_error_handler(test_error_handler); |
| 227 |
|
| 228 |
STK_ThrowErrorMsgIf(true, "test"); |
| 229 |
|
| 230 |
ASSERT_TRUE(test_error_handler_called); |
| 231 |
|
| 232 |
stk::set_error_handler(orig); |
| 233 |
|
| 234 |
ASSERT_THROW(force_throw_error_trigger(), std::runtime_error); |
| 235 |
|
| 236 |
// Check that setting handler for invalid args works. |
| 237 |
|
| 238 |
orig = stk::set_invalid_arg_handler(test_invarg_handler); |
| 239 |
|
| 240 |
STK_ThrowInvalidArgMsgIf(true, "test"); |
| 241 |
|
| 242 |
ASSERT_TRUE(test_invarg_handler_called); |
| 243 |
|
| 244 |
stk::set_invalid_arg_handler(orig); |
| 245 |
|
| 246 |
ASSERT_THROW(force_throw_invarg_trigger(), std::invalid_argument); |
| 247 |
} |
| 248 |
|
| 249 |
void testNGPThrowRequireMsg() |
| 250 |
{ |
| 251 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(const int & /*i*/){ |
| 252 |
bool test = false; |
| 253 |
STK_NGP_ThrowRequireMsg(test == true, "Error testing whatever"); |
| 254 |
}); |
| 255 |
} |
| 256 |
|
| 257 |
#if (defined(__GNUC__) && (__GNUC__ > 4)) |
| 258 |
#define NEW_ENOUGH_GCC |
| 259 |
#endif |
| 260 |
|
| 261 |
TEST(UnitTestingOfThrowMacros, NGP_ThrowRequireMsg) |
| 262 |
{ |
| 263 |
#if defined(STK_ENABLE_GPU) || defined(_OPENMP) |
| 264 |
// Unable to test a device-side abort, as this eventually results in a throw |
| 265 |
// inside Kokkos::finalize(). |
| 266 |
// Also, OpenMP seems to produce an abort (in adddition to a throw?). |
| 267 |
// |
| 268 |
// testNGPThrowRequireMsg(); |
| 269 |
std::cout<<"NGP_ThrowRequireMsg: #if cuda, openmp or hip"<<std::endl; |
| 270 |
#else |
| 271 |
#ifdef NEW_ENOUGH_GCC |
| 272 |
std::cout<<"NGP_ThrowRequireMsg: #ifdef new-enough-gcc"<<std::endl; |
| 273 |
//For now, only test this on gcc compilers more recent than major version 4. |
| 274 |
//A Trilinos pre-push test platform, 4.8.4 seems to produce an abort instead |
| 275 |
//of a throw for this test. |
| 276 |
try { |
| 277 |
testNGPThrowRequireMsg(); |
| 278 |
} |
| 279 |
catch (std::exception & ex) { |
| 280 |
std::cerr<<"ex.what(): "<<ex.what()<<std::endl; |
| 281 |
std::string expectedMsg1 = "Requirement( test == true ) FAILED\n" |
| 282 |
"Error occurred at: stk_unit_tests/stk_util/util/UnitTestThrowMacros.cpp:"; |
| 283 |
std::string expectedMsg2 = "Error: Error testing whatever\n"; |
| 284 |
std::string message = ex.what(); |
| 285 |
std::cout<<"NGP_ThrowRequireMsg: caught, comparing strings"<<std::endl; |
| 286 |
EXPECT_NE(message.find(expectedMsg1), std::string::npos); |
| 287 |
EXPECT_NE(message.find(expectedMsg2), std::string::npos); |
| 288 |
} |
| 289 |
catch (...) { |
| 290 |
std::cerr<<"Unexpected exception-type thrown."<<std::endl; |
| 291 |
} |
| 292 |
#endif |
| 293 |
#endif |
| 294 |
} |
| 295 |
|
| 296 |
void testNGPThrowRequire() |
| 297 |
{ |
| 298 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(const int & /*i*/){ |
| 299 |
bool test = false; |
| 300 |
STK_NGP_ThrowRequire(test == true); |
| 301 |
}); |
| 302 |
} |
| 303 |
|
| 304 |
TEST(UnitTestingOfThrowMacros, NGP_ThrowRequire) |
| 305 |
{ |
| 306 |
#if defined(STK_ENABLE_GPU) || defined(_OPENMP) |
| 307 |
// Unable to test a device-side abort, as this eventually results in a throw |
| 308 |
// inside Kokkos::finalize(). |
| 309 |
// |
| 310 |
// testNGPThrowRequire(); |
| 311 |
std::cout<<"NGP_ThrowRequire: #if cuda, openmp or hip"<<std::endl; |
| 312 |
#else |
| 313 |
#ifdef NEW_ENOUGH_GCC |
| 314 |
std::cout<<"NGP_ThrowRequire: #ifdef new-enough-gcc"<<std::endl; |
| 315 |
try { |
| 316 |
testNGPThrowRequire(); |
| 317 |
} |
| 318 |
catch (std::exception & ex) { |
| 319 |
const char * expectedMsg = "Requirement( test == true ) FAILED\n" |
| 320 |
"Error occurred at: stk_unit_tests/stk_util/util/UnitTestThrowMacros.cpp:"; |
| 321 |
std::string message = ex.what(); |
| 322 |
std::cout<<"NGP_ThrowRequire: caught, comparing strings"<<std::endl; |
| 323 |
EXPECT_NE(message.find(expectedMsg), std::string::npos); |
| 324 |
} |
| 325 |
#endif |
| 326 |
#endif |
| 327 |
} |
| 328 |
|
| 329 |
// Debug testing |
| 330 |
#ifndef NDEBUG |
| 331 |
void testNGPThrowAssertMsg() |
| 332 |
{ |
| 333 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(const int & i){ |
| 334 |
bool test = false; |
| 335 |
STK_NGP_ThrowAssertMsg(test == true, "Error testing whatever"); |
| 336 |
}); |
| 337 |
} |
| 338 |
|
| 339 |
TEST(UnitTestingOfThrowMacros, NGP_ThrowAssertMsg_debug) |
| 340 |
{ |
| 341 |
#if defined(STK_ENABLE_GPU) || defined(_OPENMP) |
| 342 |
// Unable to test a device-side abort, as this eventually results in a throw |
| 343 |
// inside Kokkos::finalize(). |
| 344 |
// |
| 345 |
// testNGPThrowAssertMsg(); |
| 346 |
#else |
| 347 |
#ifdef NEW_ENOUGH_GCC |
| 348 |
try { |
| 349 |
testNGPThrowAssertMsg(); |
| 350 |
} |
| 351 |
catch (std::exception & ex) { |
| 352 |
const char * expectedMsg1 = "Requirement( test == true ) FAILED\n" |
| 353 |
"Error occurred at: stk_unit_tests/stk_util/util/UnitTestThrowMacros.cpp:"; |
| 354 |
const char * expectedMsg2 = "Error: Error testing whatever\n"; |
| 355 |
std::string message = ex.what(); |
| 356 |
EXPECT_NE(message.find(expectedMsg1), std::string::npos); |
| 357 |
EXPECT_NE(message.find(expectedMsg2), std::string::npos); |
| 358 |
} |
| 359 |
#endif |
| 360 |
#endif |
| 361 |
} |
| 362 |
#endif |
| 363 |
|
| 364 |
// Release testing |
| 365 |
#ifdef NDEBUG |
| 366 |
void testNGPThrowAssertMsg() |
| 367 |
{ |
| 368 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(const int & /*i*/){ |
| 369 |
STK_NGP_ThrowAssertMsg(false, "Error testing whatever"); |
| 370 |
}); |
| 371 |
} |
| 372 |
|
| 373 |
TEST(UnitTestingOfThrowMacros, NGP_ThrowAssertMsg_release) |
| 374 |
{ |
| 375 |
#if defined(STK_ENABLE_GPU) || defined(_OPENMP) |
| 376 |
// Unable to test a device-side abort, as this eventually results in a throw |
| 377 |
// inside Kokkos::finalize(). |
| 378 |
// |
| 379 |
// testNGPThrowAssertMsg(); |
| 380 |
#else |
| 381 |
#ifdef NEW_ENOUGH_GCC |
| 382 |
EXPECT_NO_THROW(testNGPThrowAssertMsg()); |
| 383 |
#endif |
| 384 |
#endif |
| 385 |
} |
| 386 |
#endif |
| 387 |
|
| 388 |
void testNGPThrowErrorMsgIf() |
| 389 |
{ |
| 390 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(const int & /*i*/){ |
| 391 |
bool test = true; |
| 392 |
STK_NGP_ThrowErrorMsgIf(test == true, "Error testing whatever"); |
| 393 |
}); |
| 394 |
} |
| 395 |
|
| 396 |
TEST(UnitTestingOfThrowMacros, NGP_ThrowErrorMsgIf) |
| 397 |
{ |
| 398 |
#if defined(STK_ENABLE_GPU) || defined(_OPENMP) |
| 399 |
// Unable to test a device-side abort, as this eventually results in a throw |
| 400 |
// inside Kokkos::finalize(). |
| 401 |
// |
| 402 |
// testNGPThrowErrorMsgIf(); |
| 403 |
#else |
| 404 |
#ifdef NEW_ENOUGH_GCC |
| 405 |
try { |
| 406 |
testNGPThrowErrorMsgIf(); |
| 407 |
} |
| 408 |
catch (std::exception & ex) { |
| 409 |
const char * expectedMsg1 = "Requirement( !(test == true) ) FAILED\n" |
| 410 |
"Error occurred at: stk_unit_tests/stk_util/util/UnitTestThrowMacros.cpp:"; |
| 411 |
const char * expectedMsg2 = "Error: Error testing whatever\n"; |
| 412 |
std::string message = ex.what(); |
| 413 |
EXPECT_NE(message.find(expectedMsg1), std::string::npos); |
| 414 |
EXPECT_NE(message.find(expectedMsg2), std::string::npos); |
| 415 |
} |
| 416 |
#endif |
| 417 |
#endif |
| 418 |
} |
| 419 |
|
| 420 |
void testNGPThrowErrorIf() |
| 421 |
{ |
| 422 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(const int & /*i*/){ |
| 423 |
bool test = true; |
| 424 |
STK_NGP_ThrowErrorIf(test == true); |
| 425 |
}); |
| 426 |
} |
| 427 |
|
| 428 |
TEST(UnitTestingOfThrowMacros, NGP_ThrowErrorIf) |
| 429 |
{ |
| 430 |
#if defined(STK_ENABLE_GPU) || defined(_OPENMP) |
| 431 |
// Unable to test a device-side abort, as this eventually results in a throw |
| 432 |
// inside Kokkos::finalize(). |
| 433 |
// |
| 434 |
// testNGPThrowErrorIf(); |
| 435 |
#else |
| 436 |
#ifdef NEW_ENOUGH_GCC |
| 437 |
try { |
| 438 |
testNGPThrowErrorIf(); |
| 439 |
} |
| 440 |
catch (std::exception & ex) { |
| 441 |
const char * expectedMsg = "Requirement( !(test == true) ) FAILED\n" |
| 442 |
"Error occurred at: stk_unit_tests/stk_util/util/UnitTestThrowMacros.cpp:"; |
| 443 |
std::string message = ex.what(); |
| 444 |
EXPECT_NE(message.find(expectedMsg), std::string::npos); |
| 445 |
} |
| 446 |
#endif |
| 447 |
#endif |
| 448 |
} |
| 449 |
|
| 450 |
void testNGPThrowErrorMsg() |
| 451 |
{ |
| 452 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(const int & /*i*/){ |
| 453 |
STK_NGP_ThrowErrorMsg("Error testing whatever"); |
| 454 |
}); |
| 455 |
} |
| 456 |
|
| 457 |
TEST(UnitTestingOfThrowMacros, NGP_ThrowErrorMsg) |
| 458 |
{ |
| 459 |
#if defined(STK_ENABLE_GPU) || defined(_OPENMP) |
| 460 |
// Unable to test a device-side abort, as this eventually results in a throw |
| 461 |
// inside Kokkos::finalize(). |
| 462 |
// |
| 463 |
// testNGPThrowErrorMsg(); |
| 464 |
#else |
| 465 |
#ifdef NEW_ENOUGH_GCC |
| 466 |
try { |
| 467 |
testNGPThrowErrorMsg(); |
| 468 |
} |
| 469 |
catch (std::exception & ex) { |
| 470 |
const char * expectedMsg1 = "Error occurred at: stk_unit_tests/stk_util/util/UnitTestThrowMacros.cpp:"; |
| 471 |
const char * expectedMsg2 = "Error: Error testing whatever\n"; |
| 472 |
std::string message = ex.what(); |
| 473 |
EXPECT_NE(message.find(expectedMsg1), std::string::npos); |
| 474 |
EXPECT_NE(message.find(expectedMsg2), std::string::npos); |
| 475 |
} |
| 476 |
#endif |
| 477 |
#endif |
| 478 |
} |
| 479 |
|
| 480 |
#if !defined(NDEBUG) |
| 481 |
// clang-format off |
| 482 |
#define TEST_THROW_MACROS_IN_BLOCKS_ARG(testThrow, cond) \ |
| 483 |
{ \ |
| 484 |
for (int i = 0; i < 1; ++i) { \ |
| 485 |
testThrow(++i == -1 || cond); \ |
| 486 |
} \ |
| 487 |
for (int i = 0; i < 1; ++i) \ |
| 488 |
testThrow(++i == -1 || cond); \ |
| 489 |
if (int i = 1) { \ |
| 490 |
testThrow(++i == -1 || cond); \ |
| 491 |
} \ |
| 492 |
if (int i = 1) \ |
| 493 |
testThrow(++i == -1 || cond); \ |
| 494 |
} |
| 495 |
|
| 496 |
#define TEST_THROW_MSG_MACROS_IN_BLOCKS_ARG(testThrowMsg, cond) \ |
| 497 |
{ \ |
| 498 |
for (int i = 0; i < 1; ++i) { \ |
| 499 |
testThrowMsg(++i == -1 || cond, ""); \ |
| 500 |
} \ |
| 501 |
for (int i = 0; i < 1; ++i) \ |
| 502 |
testThrowMsg(++i == -1 || cond, ""); \ |
| 503 |
if (int i = 1) { \ |
| 504 |
testThrowMsg(++i == -1 || cond, ""); \ |
| 505 |
} \ |
| 506 |
if (int i = 1) \ |
| 507 |
testThrowMsg(++i == -1 || cond, ""); \ |
| 508 |
} |
| 509 |
// clang-format on |
| 510 |
|
| 511 |
#define TEST_THROW_MACROS_IN_BLOCKS(testThrow) TEST_THROW_MACROS_IN_BLOCKS_ARG(testThrow, false) |
| 512 |
#define TEST_THROW_IF_MACROS_IN_BLOCKS(testThrow) TEST_THROW_MACROS_IN_BLOCKS_ARG(testThrow, true) |
| 513 |
#define TEST_THROW_MSG_MACROS_IN_BLOCKS(testThrow) TEST_THROW_MSG_MACROS_IN_BLOCKS_ARG(testThrow, false) |
| 514 |
#define TEST_THROW_MSG_IF_MACROS_IN_BLOCKS(testThrow) TEST_THROW_MSG_MACROS_IN_BLOCKS_ARG(testThrow, true) |
| 515 |
|
| 516 |
#define TEST_NGP_THROW_MACROS_IN_BLOCKS(testThrow) TEST_THROW_MACROS_IN_BLOCKS_ARG(testThrow, true) |
| 517 |
#define TEST_NGP_THROW_IF_MACROS_IN_BLOCKS(testThrow) TEST_THROW_MACROS_IN_BLOCKS_ARG(testThrow, false) |
| 518 |
#define TEST_NGP_THROW_MSG_MACROS_IN_BLOCKS(testThrow) TEST_THROW_MSG_MACROS_IN_BLOCKS_ARG(testThrow, true) |
| 519 |
#define TEST_NGP_THROW_MSG_IF_MACROS_IN_BLOCKS(testThrow) TEST_THROW_MSG_MACROS_IN_BLOCKS_ARG(testThrow, false) |
| 520 |
|
| 521 |
TEST(UnitTestingOfThrowMacros, STK_ThrowsInSingleStatementBlock) |
| 522 |
{ |
| 523 |
EXPECT_ANY_THROW(TEST_THROW_MACROS_IN_BLOCKS(STK_ThrowRequireWithSierraHelpMsg)); |
| 524 |
EXPECT_ANY_THROW(TEST_THROW_MACROS_IN_BLOCKS(STK_ThrowRequire)); |
| 525 |
EXPECT_ANY_THROW(TEST_THROW_IF_MACROS_IN_BLOCKS(STK_ThrowErrorIf)); |
| 526 |
EXPECT_ANY_THROW(TEST_THROW_IF_MACROS_IN_BLOCKS(STK_ThrowInvalidArgIf)); |
| 527 |
|
| 528 |
EXPECT_ANY_THROW(TEST_THROW_MSG_MACROS_IN_BLOCKS(STK_ThrowRequireMsg)); |
| 529 |
EXPECT_ANY_THROW(TEST_THROW_MSG_IF_MACROS_IN_BLOCKS(STK_ThrowErrorMsgIf)); |
| 530 |
EXPECT_ANY_THROW(TEST_THROW_MSG_IF_MACROS_IN_BLOCKS(STK_ThrowInvalidArgMsgIf)); |
| 531 |
|
| 532 |
#ifdef NDEBUG |
| 533 |
EXPECT_NO_THROW(TEST_THROW_MACROS_IN_BLOCKS(STK_ThrowAssert)); |
| 534 |
EXPECT_NO_THROW(TEST_THROW_MSG_MACROS_IN_BLOCKS(STK_ThrowAssertMsg)); |
| 535 |
#else |
| 536 |
EXPECT_ANY_THROW(TEST_THROW_MACROS_IN_BLOCKS(STK_ThrowAssert)); |
| 537 |
EXPECT_ANY_THROW(TEST_THROW_MSG_MACROS_IN_BLOCKS(STK_ThrowAssertMsg)); |
| 538 |
#endif |
| 539 |
} |
| 540 |
|
| 541 |
template <typename Lambda> |
| 542 |
void run_ngp_macros_test(Lambda lambda) { |
| 543 |
EXPECT_NO_THROW( Kokkos::parallel_for(1, lambda) ); |
| 544 |
} |
| 545 |
|
| 546 |
template <typename... Lambdas> |
| 547 |
void run_ngp_macros_in_single_statement_block(Lambdas... lambdas) { |
| 548 |
(run_ngp_macros_test(lambdas), ...); |
| 549 |
} |
| 550 |
|
| 551 |
void test_stk_ngp_macros_in_single_statement_block() { |
| 552 |
auto ngpThrowRequire = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MACROS_IN_BLOCKS(STK_NGP_ThrowRequire); }; |
| 553 |
auto ngpThrowAssert = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MACROS_IN_BLOCKS(STK_NGP_ThrowAssert); }; |
| 554 |
auto ngpThrowErrorIf = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_IF_MACROS_IN_BLOCKS(STK_NGP_ThrowErrorIf); }; |
| 555 |
|
| 556 |
auto ngpThrowRequireMsg = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MSG_MACROS_IN_BLOCKS(STK_NGP_ThrowRequireMsg); }; |
| 557 |
auto ngpThrowAssertMsg = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MSG_MACROS_IN_BLOCKS(STK_NGP_ThrowAssertMsg); }; |
| 558 |
auto ngpThrowErrorMsgIf = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MSG_IF_MACROS_IN_BLOCKS(STK_NGP_ThrowErrorMsgIf); }; |
| 559 |
|
| 560 |
run_ngp_macros_in_single_statement_block(ngpThrowRequire, ngpThrowAssert, ngpThrowErrorIf, ngpThrowRequireMsg, |
| 561 |
ngpThrowRequire, ngpThrowAssertMsg, ngpThrowErrorMsgIf); |
| 562 |
} |
| 563 |
|
| 564 |
TEST(UnitTestingOfThrowMacros, STK_NGPThrowsInSingleStatementBlock) |
| 565 |
{ |
| 566 |
test_stk_ngp_macros_in_single_statement_block(); |
| 567 |
} |
| 568 |
|
| 569 |
#endif |