// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering // Solutions of Sandia, LLC (NTESS). Under the terms of Contract // DE-NA0003525 with NTESS, the U.S. Government retains certain rights // in this software. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // // * Neither the name of NTESS nor the names of its contributors // may be used to endorse or promote products derived from this // software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // #include "gtest/gtest.h" #include "Kokkos_Core.hpp" #include "stk_util/util/ReportHandler.hpp" // for set_assert_handler, ThrowRequireMsg, set_erro... #include "stk_util/ngp/NgpSpaces.hpp" #include // for basic_ostream::operator<<, operator<<, ostrin... #include // for logic_error, runtime_error, invalid_argument #include // for string namespace { bool test_assert_handler_called = false; bool test_error_handler_called = false; bool test_invarg_handler_called = false; void test_assert_handler(const char* /*expr*/, const std::string& /*location*/, std::ostringstream& /*message*/) { test_assert_handler_called = true; } void test_error_handler(const char* /*expr*/, const std::string& /*location*/, std::ostringstream& /*message*/) { test_error_handler_called = true; } void test_invarg_handler(const char* /*expr*/, const std::string& /*location*/, std::ostringstream& /*message*/) { test_invarg_handler_called = true; } void force_throw_require_trigger(bool msg = true) { if (msg) { STK_ThrowRequireMsg(false, "Will always fail, this is for testing"); } else { STK_ThrowRequire(false); } } void force_throw_error_trigger(bool msg = true) { if (msg) { STK_ThrowErrorMsgIf(true, "Will always fail, this is for testing"); } else { STK_ThrowErrorIf(true); } } void force_throw_invarg_trigger(bool msg = true) { if (msg) { STK_ThrowInvalidArgMsgIf(true, "Will always fail, this is for testing"); } else { STK_ThrowInvalidArgIf(true); } } void check_interaction_with_if(bool msg = true) { if (msg) { if (true) STK_ThrowRequireMsg(false, "Will always fail, this is for testing"); } else { if (true) STK_ThrowRequire(false); } } void force_throw_assert() { STK_ThrowAssert(false); } void test_no_expr_error() { STK_ThrowErrorMsg("message"); } } // namespace TEST(UnitTestingOfThrowMacros, testUnit) { // Setting assert handler to NULL should cause exception ASSERT_THROW(stk::set_assert_handler(0), std::runtime_error); // Check that Throw*Msg works ASSERT_THROW(force_throw_require_trigger(), std::logic_error); ASSERT_THROW(force_throw_error_trigger(), std::runtime_error); ASSERT_THROW(force_throw_invarg_trigger(), std::invalid_argument); // Check that Throw* works ASSERT_THROW(force_throw_require_trigger(false), std::logic_error); ASSERT_THROW(force_throw_error_trigger(false), std::runtime_error); ASSERT_THROW(force_throw_invarg_trigger(false), std::invalid_argument); // Check that macro interacts appropriately with if statements ASSERT_THROW(check_interaction_with_if(), std::logic_error); ASSERT_THROW(check_interaction_with_if(false), std::logic_error); // Check that usage of ThrowRequireMsg/ThrowAssertMsg does not change program // semantics. Code blocks that are not contained within braces seem to be // the most likely to be problematic. bool expected_execution_path = false; if (false) STK_ThrowRequireMsg(false, "test"); else expected_execution_path = true; ASSERT_TRUE(expected_execution_path); expected_execution_path = false; if (false) STK_ThrowAssertMsg(false, "test"); else expected_execution_path = true; ASSERT_TRUE(expected_execution_path); expected_execution_path = false; if (false) STK_ThrowErrorMsg("test"); else expected_execution_path = true; ASSERT_TRUE(expected_execution_path); // These next four statements are to check compilation success if (false) STK_ThrowRequireMsg(false, "test"); if (false) STK_ThrowAssertMsg(false, "test"); if (false) STK_ThrowRequire(false); if (false) STK_ThrowAssert(false); // Check that do-while still works, again, we are mostly checking compilation // success here. do STK_ThrowRequireMsg(true, "test"); while (false); do STK_ThrowAssertMsg(true, "test"); while (false); // Check that message with put-tos compiles int temp = 0; STK_ThrowRequireMsg(true, "test: " << temp << " blah"); STK_ThrowAssertMsg(true, "test: " << temp << " blah"); // Check that assert behaves as expected (throws in debug, not in opt) #ifdef NDEBUG force_throw_assert(); #else ASSERT_THROW(force_throw_assert(), std::logic_error); #endif // Check that ThrowErrorMsg works ASSERT_THROW(test_no_expr_error(), std::runtime_error); // Check that setting handler for asserts works. stk::ErrorHandler orig = stk::set_assert_handler(test_assert_handler); STK_ThrowRequireMsg(false, "test"); ASSERT_TRUE(test_assert_handler_called); stk::set_assert_handler(orig); ASSERT_THROW(force_throw_require_trigger(), std::logic_error); // Check that setting handler for errors works. orig = stk::set_error_handler(test_error_handler); STK_ThrowErrorMsgIf(true, "test"); ASSERT_TRUE(test_error_handler_called); stk::set_error_handler(orig); ASSERT_THROW(force_throw_error_trigger(), std::runtime_error); // Check that setting handler for invalid args works. orig = stk::set_invalid_arg_handler(test_invarg_handler); STK_ThrowInvalidArgMsgIf(true, "test"); ASSERT_TRUE(test_invarg_handler_called); stk::set_invalid_arg_handler(orig); ASSERT_THROW(force_throw_invarg_trigger(), std::invalid_argument); } void testNGPThrowRequireMsg() { Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(const int & /*i*/){ bool test = false; STK_NGP_ThrowRequireMsg(test == true, "Error testing whatever"); }); } #if (defined(__GNUC__) && (__GNUC__ > 4)) #define NEW_ENOUGH_GCC #endif TEST(UnitTestingOfThrowMacros, NGP_ThrowRequireMsg) { #if defined(STK_ENABLE_GPU) || defined(_OPENMP) // Unable to test a device-side abort, as this eventually results in a throw // inside Kokkos::finalize(). // Also, OpenMP seems to produce an abort (in adddition to a throw?). // // testNGPThrowRequireMsg(); std::cout<<"NGP_ThrowRequireMsg: #if cuda, openmp or hip"< void run_ngp_macros_test(Lambda lambda) { EXPECT_NO_THROW( Kokkos::parallel_for(1, lambda) ); } template void run_ngp_macros_in_single_statement_block(Lambdas... lambdas) { (run_ngp_macros_test(lambdas), ...); } void test_stk_ngp_macros_in_single_statement_block() { auto ngpThrowRequire = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MACROS_IN_BLOCKS(STK_NGP_ThrowRequire); }; auto ngpThrowAssert = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MACROS_IN_BLOCKS(STK_NGP_ThrowAssert); }; auto ngpThrowErrorIf = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_IF_MACROS_IN_BLOCKS(STK_NGP_ThrowErrorIf); }; auto ngpThrowRequireMsg = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MSG_MACROS_IN_BLOCKS(STK_NGP_ThrowRequireMsg); }; auto ngpThrowAssertMsg = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MSG_MACROS_IN_BLOCKS(STK_NGP_ThrowAssertMsg); }; auto ngpThrowErrorMsgIf = KOKKOS_LAMBDA(const int) { TEST_NGP_THROW_MSG_IF_MACROS_IN_BLOCKS(STK_NGP_ThrowErrorMsgIf); }; run_ngp_macros_in_single_statement_block(ngpThrowRequire, ngpThrowAssert, ngpThrowErrorIf, ngpThrowRequireMsg, ngpThrowRequire, ngpThrowAssertMsg, ngpThrowErrorMsgIf); } TEST(UnitTestingOfThrowMacros, STK_NGPThrowsInSingleStatementBlock) { test_stk_ngp_macros_in_single_statement_block(); } #endif