From a8e25da0ab447e691e9f542ba6ea4d60441d6974 Mon Sep 17 00:00:00 2001 From: Nick Thompson <4nt@ornl.gov> Date: Mon, 12 Apr 2021 12:54:38 -0400 Subject: [PATCH] Add __func__ to unit test metadata printed on failure. --- vtkm/cont/testing/Testing.h | 45 ++++++++++++++++++++----------- vtkm/testing/Testing.h | 43 +++++++++++++++++++---------- vtkm/testing/UnitTestGeometry.cxx | 6 ----- vtkm/testing/UnitTestMath.cxx | 7 +---- 4 files changed, 60 insertions(+), 41 deletions(-) diff --git a/vtkm/cont/testing/Testing.h b/vtkm/cont/testing/Testing.h index 05f9c2eea..60198a6d1 100644 --- a/vtkm/cont/testing/Testing.h +++ b/vtkm/cont/testing/Testing.h @@ -28,6 +28,21 @@ #include +#include + +#define VTKM_MATH_ASSERT(condition, message) \ + { \ + if (condition) \ + { \ + return; \ + } \ + std::stringstream ss; \ + ss << "Error at " << __FILE__ << ":" << __LINE__ << ":" << __func__ << "\n"; \ + ss << "\t" << message; \ + this->RaiseError(ss.str().c_str()); \ + } + + namespace opt = vtkm::cont::internal::option; namespace vtkm @@ -141,26 +156,25 @@ 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.GetFunc() << "\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:" << 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; @@ -179,24 +193,25 @@ 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.GetFunc() << "\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\t"; + << error.GetMessage() << "\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::cout << "Unidentified exception thrown.\n"; return 1; } return 0; diff --git a/vtkm/testing/Testing.h b/vtkm/testing/Testing.h index 3beaa69ae..5cb526a27 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* GetFunc() 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.GetFunc() << "\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..e87e683e6 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 @@ -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