From 9d0e5fc35dfa724198e72f158b2feecf6aff5b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Elfstr=C3=B6m?= Date: Thu, 20 Aug 2015 17:08:29 +0200 Subject: [PATCH] Implement CHECK_ARRAY_CLOSE_DESCRIBED macro --- UnitTest++/CheckMacros.h | 4 ++ tests/TestCheckMacros.cpp | 81 +++++++++++++++++++++++++++++++++++++++ tests/TestUnitTestPP.cpp | 7 ++++ 3 files changed, 92 insertions(+) diff --git a/UnitTest++/CheckMacros.h b/UnitTest++/CheckMacros.h index 6ccb2c5..471c495 100644 --- a/UnitTest++/CheckMacros.h +++ b/UnitTest++/CheckMacros.h @@ -164,6 +164,10 @@ UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, \ UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__))) +#define CHECK_ARRAY_CLOSE_DESCRIBED(expected, actual, count, tolerance, description) \ + UNITTEST_CHECK_ARRAY_CLOSE(expected, actual, count, tolerance, \ + UNITTEST_CHECK_WITH_DESCRIPTION(UnitTest::ArrayAreClose(expected, actual, count, tolerance), description)) + #define UNITTEST_CHECK_ARRAY_CLOSE(expected, actual, count, tolerance, test_action) \ UNITTEST_MULTILINE_MACRO_BEGIN \ UT_TRY \ diff --git a/tests/TestCheckMacros.cpp b/tests/TestCheckMacros.cpp index 6e5a2fb..fb800bd 100644 --- a/tests/TestCheckMacros.cpp +++ b/tests/TestCheckMacros.cpp @@ -742,6 +742,87 @@ TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenFailing) CHECK_EQUAL(1, g_sideEffect); } +TEST(CheckArrayCloseDescribedSucceedsOnEqual) +{ + bool failure = true; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + const float data[4] = { 0, 1, 2, 3 }; + CHECK_ARRAY_CLOSE_DESCRIBED(data, data, 4, 0.01f, "description"); + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); +} + +TEST(CheckArrayCloseDescribedFailsOnNotEqual) +{ + bool failure = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_CLOSE_DESCRIBED(data1, data2, 4, 0.01f, "description"); + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); +} + +TEST(CheckArrayCloseDescribedFailureContainsCorrectDetails) +{ + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + CHECK_ARRAY_CLOSE_DESCRIBED(data1, data2, 4, 0.01f, "description " << hex << 0x1234); line = __LINE__; + } + + CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL("description 1234", reporter.lastFailedMessage); + CHECK_EQUAL(line, reporter.lastFailedLine); +} + +TEST(CheckArrayCloseDescribedDoesNotHaveSideEffectsWhenPassing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[] = { 0, 1, 2, 3 }; + CHECK_ARRAY_CLOSE_DESCRIBED(data, FunctionWithSideEffects2(), 4, 0.01f, "description"); + } + CHECK_EQUAL(1, g_sideEffect); +} + +TEST(CheckArrayCloseDescribedDoesNotHaveSideEffectsWhenFailing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[] = { 0, 1, 3, 3 }; + CHECK_ARRAY_CLOSE_DESCRIBED(data, FunctionWithSideEffects2(), 4, 0.01f, "description"); + } + + CHECK_EQUAL(1, g_sideEffect); +} + TEST(CheckArray2DCloseSucceedsOnEqual) { bool failure = true; diff --git a/tests/TestUnitTestPP.cpp b/tests/TestUnitTestPP.cpp index 996455c..a26736f 100644 --- a/tests/TestUnitTestPP.cpp +++ b/tests/TestUnitTestPP.cpp @@ -80,6 +80,13 @@ TEST(ArrayCloseSucceeds) CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f); } +TEST(CanUseCheckArrayCloseDescribedToGetCustomFailureMessage) +{ + float const x[] = {1, 2.01f, 3}; + float const expected[] = {1, 2, 3}; + CHECK_ARRAY_CLOSE_DESCRIBED(expected, x, 3, 0.01f, "x is not correct, have you checked the flux capacitor?"); +} + TEST(Array2dCloseSucceeds) { float const a1[][3] = {{1, 2, 3}, {4, 5, 6}};