From c5aa621e6447f7f37d00a1a085779b4f44877291 Mon Sep 17 00:00:00 2001 From: Alexis Denis Date: Tue, 22 Nov 2016 13:16:33 -0600 Subject: [PATCH] Catching and resolving the exception thrown when accessing an empty string from an ostrstream (MemoryOutStream GetString would throw if the stream was empty). Moving the exception disable to the test file instead of the main file so the workaround is located where it is needed. All tests pass!!! --- UnitTest++/MemoryOutStream.cpp | 9 +++++++-- tests/Main.cpp | 7 ------- tests/TestChecks.cpp | 10 ++++++++++ tests/TestMemoryOutStream.cpp | 2 -- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/UnitTest++/MemoryOutStream.cpp b/UnitTest++/MemoryOutStream.cpp index 98e52e6..41dd0f9 100644 --- a/UnitTest++/MemoryOutStream.cpp +++ b/UnitTest++/MemoryOutStream.cpp @@ -6,14 +6,19 @@ namespace UnitTest { char const* MemoryOutStream::GetText() const { - m_text = this->str(); + try + { + m_text = this->str(); + } + catch (...) { m_text = ""; } + return m_text.c_str(); } void MemoryOutStream::Clear() { this->str(std::string()); - m_text = this->str(); + m_text = std::string(); } #ifdef UNITTEST_COMPILER_IS_MSVC6 diff --git a/tests/Main.cpp b/tests/Main.cpp index ce9749a..0c369f0 100644 --- a/tests/Main.cpp +++ b/tests/Main.cpp @@ -1,16 +1,9 @@ #include "UnitTest++/UnitTestPP.h" #ifdef __BORLANDC__ #include - #include - #include #endif int main(int, char const *[]) { -#ifdef __BORLANDC__ - // This is to allow the check test to pass when using Nan (invalid float numbers), otherwise, they throw an exception - // and the test fails (CheckCloseWithNaNFails and CheckCloseWithNaNAgainstItselfFails) - _control87(MCW_EM, MCW_EM); -#endif return UnitTest::RunAllTests(); } diff --git a/tests/TestChecks.cpp b/tests/TestChecks.cpp index 2c0ac04..04cd150 100644 --- a/tests/TestChecks.cpp +++ b/tests/TestChecks.cpp @@ -3,6 +3,11 @@ #include +#ifdef __BORLANDC__ + #include + #include +#endif + using namespace UnitTest; @@ -148,6 +153,11 @@ namespace { TEST(CheckCloseWithNaNFails) { +#ifdef __BORLANDC__ + // This is to allow the check test to pass when using Nan (invalid float numbers), otherwise, they throw an exception + // and the test fails (CheckCloseWithNaNFails and CheckCloseWithNaNAgainstItselfFails) + _control87(MCW_EM, MCW_EM); +#endif const unsigned int bitpattern = 0xFFFFFFFF; float nan; UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern)); diff --git a/tests/TestMemoryOutStream.cpp b/tests/TestMemoryOutStream.cpp index a4bd958..4010c79 100644 --- a/tests/TestMemoryOutStream.cpp +++ b/tests/TestMemoryOutStream.cpp @@ -257,7 +257,6 @@ namespace { CHECK_EQUAL("53124", stream.GetText()); } -#ifndef __BORLANDC__ // Not sure why but this test fails with the Borland C++ 5.5 compiler. // It throws an unhandled exception: // unexpected NULL pointer in function: basic_string( const charT*,size_type,const Allocator&) @@ -268,7 +267,6 @@ namespace { stream.Clear(); CHECK_EQUAL("", stream.GetText()); } -#endif #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM