diff --git a/vtkm/cont/testing/Testing.h b/vtkm/cont/testing/Testing.h index 05f9c2eea..c4b3702f1 100644 --- a/vtkm/cont/testing/Testing.h +++ b/vtkm/cont/testing/Testing.h @@ -26,8 +26,33 @@ #include +#include #include +// We could, conceivably, use CUDA or Kokkos specific print statements here. +// But we cannot use std::stringstream on device, so for now, we'll just accept +// that on CUDA and Kokkos we print less actionalble information. +#if defined(VTKM_ENABLE_CUDA) || defined(VTKM_ENABLE_KOKKOS) +#define VTKM_MATH_ASSERT(condition, message) \ + { \ + if (!(condition)) \ + { \ + this->RaiseError(message); \ + } \ + } +#else +#define VTKM_MATH_ASSERT(condition, message) \ + { \ + if (!(condition)) \ + { \ + std::stringstream ss; \ + ss << "\n\tError at " << __FILE__ << ":" << __LINE__ << ":" << __func__ << "\n\t" << message \ + << "\n"; \ + this->RaiseError(ss.str().c_str()); \ + } \ + } +#endif + namespace opt = vtkm::cont::internal::option; namespace vtkm @@ -141,26 +166,26 @@ public: { function(); } - catch (vtkm::testing::Testing::TestFailure& error) + catch (vtkm::testing::Testing::TestFailure const& error) { - std::cout << "***** Test failed @ " << error.GetFile() << ":" << error.GetLine() << std::endl - << error.GetMessage() << std::endl; + std::cerr << "Error at " << error.GetFile() << ":" << error.GetLine() << ":" + << error.GetFunction() << "\n\t" << error.GetMessage() << "\n"; return 1; } - catch (vtkm::cont::Error& error) + catch (vtkm::cont::Error const& error) { - std::cout << "***** Uncaught VTKm exception thrown." << std::endl - << error.GetMessage() << std::endl; + std::cerr << "Uncaught VTKm exception thrown.\n" << error.GetMessage() << "\n"; + std::cerr << "Stacktrace:\n" << error.GetStackTrace() << "\n"; return 1; } - catch (std::exception& error) + catch (std::exception const& error) { - std::cout << "***** STL exception throw." << std::endl << error.what() << std::endl; + std::cerr << "STL exception throw.\n" << error.what() << "\n"; return 1; } catch (...) { - std::cout << "***** Unidentified exception thrown." << std::endl; + std::cerr << "Unidentified exception thrown.\n"; return 1; } return 0; @@ -179,24 +204,24 @@ public: } catch (vtkm::testing::Testing::TestFailure& error) { - std::cout << "***** Test failed @ " << error.GetFile() << ":" << error.GetLine() << std::endl - << error.GetMessage() << std::endl; + std::cerr << "Error at " << error.GetFile() << ":" << error.GetLine() << ":" + << error.GetFunction() << "\n\t" << error.GetMessage() << "\n"; return 1; } catch (vtkm::cont::Error& error) { - std::cout << "***** Uncaught VTKm exception thrown." << std::endl - << error.GetMessage() << std::endl; + std::cerr << "Uncaught VTKm exception thrown.\n" << error.GetMessage() << "\n"; + std::cerr << "Stacktrace:\n" << error.GetStackTrace() << "\n"; return 1; } catch (std::exception& error) { - std::cout << "***** STL exception throw." << std::endl << error.what() << std::endl; + std::cerr << "STL exception throw.\n\t" << error.what() << "\n"; return 1; } catch (...) { - std::cout << "***** Unidentified exception thrown." << std::endl; + std::cerr << "Unidentified exception thrown.\n"; return 1; } return 0; diff --git a/vtkm/testing/Testing.h b/vtkm/testing/Testing.h index 3beaa69ae..b6d023e5b 100644 --- a/vtkm/testing/Testing.h +++ b/vtkm/testing/Testing.h @@ -66,13 +66,14 @@ #define VTKM_TEST_ASSERT(...) \ ::vtkm::testing::Testing::Assert( \ - VTKM_STRINGIFY_FIRST(__VA_ARGS__), __FILE__, __LINE__, __VA_ARGS__) + VTKM_STRINGIFY_FIRST(__VA_ARGS__), __FILE__, __LINE__, __func__, __VA_ARGS__) /// \def VTKM_TEST_FAIL(messages..) /// /// Causes a test to fail with the given \a messages. At least one argument must be given. -#define VTKM_TEST_FAIL(...) ::vtkm::testing::Testing::TestFail(__FILE__, __LINE__, __VA_ARGS__) +#define VTKM_TEST_FAIL(...) \ + ::vtkm::testing::Testing::TestFail(__FILE__, __LINE__, __func__, __VA_ARGS__) class TestEqualResult { @@ -295,9 +296,13 @@ public: { public: template - VTKM_CONT TestFailure(const std::string& file, vtkm::Id line, Ts&&... messages) + VTKM_CONT TestFailure(const std::string& file, + vtkm::Id line, + const char* func, + Ts&&... messages) : File(file) , Line(line) + , Func(func) { std::stringstream messageStream; this->AppendMessages(messageStream, std::forward(messages)...); @@ -306,6 +311,7 @@ public: VTKM_CONT const std::string& GetFile() const { return this->File; } VTKM_CONT vtkm::Id GetLine() const { return this->Line; } + VTKM_CONT const char* GetFunction() const { return this->Func; } VTKM_CONT const std::string& GetMessage() const { return this->Message; } private: @@ -347,6 +353,7 @@ public: std::string File; vtkm::Id Line; + const char* Func; std::string Message; }; @@ -354,6 +361,7 @@ public: static VTKM_CONT void Assert(const std::string& conditionString, const std::string& file, vtkm::Id line, + const char* func, bool condition, Ts&&... messages) { @@ -363,30 +371,36 @@ public: } else { - throw TestFailure(file, line, std::forward(messages)..., " (", conditionString, ")"); + throw TestFailure( + file, line, func, std::forward(messages)..., " (", conditionString, ")"); } } static VTKM_CONT void Assert(const std::string& conditionString, const std::string& file, + const char* func, vtkm::Id line, bool condition) { - Assert(conditionString, file, line, condition, "Test assertion failed"); + Assert(conditionString, file, line, func, condition, "Test assertion failed"); } static VTKM_CONT void Assert(const std::string& conditionString, const std::string& file, + const char* func, vtkm::Id line, const TestEqualResult& result) { - Assert(conditionString, file, line, static_cast(result), result.GetMergedMessage()); + Assert(conditionString, file, line, func, static_cast(result), result.GetMergedMessage()); } template - static VTKM_CONT void TestFail(const std::string& file, vtkm::Id line, Ts&&... messages) + static VTKM_CONT void TestFail(const std::string& file, + vtkm::Id line, + const char* func, + Ts&&... messages) { - throw TestFailure(file, line, std::forward(messages)...); + throw TestFailure(file, line, func, std::forward(messages)...); } #ifndef VTKM_TESTING_IN_CONT @@ -433,20 +447,21 @@ public: { function(); } - catch (TestFailure& error) + catch (TestFailure const& error) { - std::cout << "***** Test failed @ " << error.GetFile() << ":" << error.GetLine() << std::endl - << error.GetMessage() << std::endl; + std::cerr << "***** Test failed @ " << error.GetFile() << ":" << error.GetLine() << ":" + << error.GetFunction() << "\n" + << error.GetMessage() << "\n"; return 1; } - catch (std::exception& error) + catch (std::exception const& error) { - std::cout << "***** STL exception throw." << std::endl << error.what() << std::endl; + std::cerr << "***** STL exception throw.\n" << error.what() << "\n"; return 1; } catch (...) { - std::cout << "***** Unidentified exception thrown." << std::endl; + std::cerr << "***** Unidentified exception thrown.\n"; return 1; } return 0; diff --git a/vtkm/testing/UnitTestGeometry.cxx b/vtkm/testing/UnitTestGeometry.cxx index fedaff757..9e4a0a068 100644 --- a/vtkm/testing/UnitTestGeometry.cxx +++ b/vtkm/testing/UnitTestGeometry.cxx @@ -20,12 +20,6 @@ #include -#define VTKM_MATH_ASSERT(condition, message) \ - if (!(condition)) \ - { \ - this->RaiseError(message); \ - } - //----------------------------------------------------------------------------- namespace { diff --git a/vtkm/testing/UnitTestMath.cxx b/vtkm/testing/UnitTestMath.cxx index bab96785f..c314a854b 100644 --- a/vtkm/testing/UnitTestMath.cxx +++ b/vtkm/testing/UnitTestMath.cxx @@ -20,11 +20,6 @@ #include -#define VTKM_MATH_ASSERT(condition, message) \ - if (!(condition)) \ - { \ - this->RaiseError(message); \ - } //----------------------------------------------------------------------------- namespace UnitTestMathNamespace @@ -921,7 +916,7 @@ struct ScalarVectorFieldTests : public vtkm::exec::FunctorBase "Float distance for difference of products is which exceeds 1.5; this is in violation of a " "theorem " "proved by Jeannerod in doi.org/10.1090/S0025-5718-2013-02679-8. Is your build compiled " - "with fma's enabled?"); + "with FMAs enabled?"); #endif } @@ -1088,7 +1083,7 @@ struct BitOpTests : public vtkm::exec::FunctorBase VTKM_MATH_ASSERT(test_equal(vtkm::CountSetBits(word), this->DumbCountBits(word)), "CountBits returned wrong value."); VTKM_MATH_ASSERT(test_equal(vtkm::FindFirstSetBit(word), this->DumbFindFirstSetBit(word)), - "FindFirstSetBit returned wrong value.") + "FindFirstSetBit returned wrong value."); } VTKM_EXEC vtkm::Int32 DumbCountBits(T word) const