Skip to content

Commit 97576dc

Browse files
committed
Fixed CHECK_EQUAL
1 parent bc512e8 commit 97576dc

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

include/CppUTest/UtestMacros.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,13 @@
116116
CHECK_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
117117

118118
#define CHECK_EQUAL_LOCATION(expected,actual, file, line)\
119-
{ UtestShell::getCurrent()->assertEquals(((expected) != (actual)), StringFrom(expected).asCharString(), StringFrom(actual).asCharString(), file, line); }
119+
{ if ((expected) != (actual)) { \
120+
if ((actual) != (actual)) \
121+
UtestShell::getCurrent()->print("WARNING:\n\tThe \"Actual Parameter\" parameter is evaluated multiple times resulting in different values.\n\tThus the value in the error message is probably incorrect.", file, line); \
122+
if ((expected) != (expected)) \
123+
UtestShell::getCurrent()->print("WARNING:\n\tThe \"Expected Parameter\" parameter is evaluated multiple times resulting in different values.\n\tThus the value in the error message is probably incorrect.", file, line); \
124+
UtestShell::getCurrent()->assertEquals(true, StringFrom(expected).asCharString(), StringFrom(actual).asCharString(), file, line); \
125+
} }
120126

121127
//This check checks for char* string equality using strcmp.
122128
//This makes up for the fact that CHECK_EQUAL only compares the pointers to char*'s

tests/TestUTestMacro.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,48 @@ TEST(UnitTestMacros, FailureWithCHECK_EQUAL)
191191
CHECK_TEST_FAILS_PROPER_WITH_TEXT("expected <1>");
192192
}
193193

194+
static int countInCountingMethod;
195+
static int _countingMethod()
196+
{
197+
return countInCountingMethod++;
198+
}
199+
200+
TEST(UnitTestMacros, passingCheckEqualWillNotBeEvaluatedMultipleTimesWithCHECK_EQUAL)
201+
{
202+
countInCountingMethod = 0;
203+
CHECK_EQUAL(0, _countingMethod());
204+
205+
LONGS_EQUAL(1, countInCountingMethod);
206+
}
207+
208+
static void _failing_CHECK_EQUAL_WithActualBeingEvaluatesMultipleTimesWillGiveAWarning()
209+
{
210+
CHECK_EQUAL(12345, _countingMethod());
211+
}
212+
213+
TEST(UnitTestMacros, failing_CHECK_EQUAL_WithActualBeingEvaluatesMultipleTimesWillGiveAWarning)
214+
{
215+
runTestWithMethod(_failing_CHECK_EQUAL_WithActualBeingEvaluatesMultipleTimesWillGiveAWarning);
216+
CHECK_TEST_FAILS_PROPER_WITH_TEXT("WARNING:\n\tThe \"Actual Parameter\" parameter is evaluated multiple times resulting in different values.\n\tThus the value in the error message is probably incorrect.");
217+
}
218+
219+
static void _failing_CHECK_EQUAL_WithExpectedBeingEvaluatesMultipleTimesWillGiveAWarning()
220+
{
221+
CHECK_EQUAL(_countingMethod(), 12345);
222+
}
223+
224+
TEST(UnitTestMacros, failing_CHECK_EQUAL_WithExpectedBeingEvaluatesMultipleTimesWillGiveAWarning)
225+
{
226+
runTestWithMethod(_failing_CHECK_EQUAL_WithExpectedBeingEvaluatesMultipleTimesWillGiveAWarning);
227+
CHECK_TEST_FAILS_PROPER_WITH_TEXT("WARNING:\n\tThe \"Expected Parameter\" parameter is evaluated multiple times resulting in different values.\n\tThus the value in the error message is probably incorrect.");
228+
}
229+
230+
TEST(UnitTestMacros, failing_CHECK_EQUAL_withParamatersThatDontChangeWillNotGiveAnyWarning)
231+
{
232+
runTestWithMethod(_failingTestMethodWithCHECK_EQUAL);
233+
CHECK( ! fixture.output_->getOutput().contains("WARNING"));
234+
}
235+
194236
TEST(UnitTestMacros, CHECK_EQUALBehavesAsProperMacro)
195237
{
196238
if (this == 0) CHECK_EQUAL(1, 2)

0 commit comments

Comments
 (0)