Skip to content

Commit 53d55b6

Browse files
committed
Merge pull request #968 from arstrube/chars
Add interface to compare signed byte-sized data
2 parents e16155e + dd6902f commit 53d55b6

File tree

12 files changed

+174
-2
lines changed

12 files changed

+174
-2
lines changed

include/CppUTest/SimpleString.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ SimpleString StringFrom(long value);
146146
SimpleString StringFrom(unsigned long value);
147147
SimpleString StringFrom(cpputest_longlong value);
148148
SimpleString StringFrom(cpputest_ulonglong value);
149+
SimpleString HexStringFrom(signed char value);
149150
SimpleString HexStringFrom(long value);
150151
SimpleString HexStringFrom(unsigned long value);
151152
SimpleString HexStringFrom(cpputest_longlong value);

include/CppUTest/TestFailure.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ class UnsignedLongLongsEqualFailure : public TestFailure
147147
UnsignedLongLongsEqualFailure(UtestShell* test, const char* fileName, int lineNumber, cpputest_ulonglong expected, cpputest_ulonglong actual, const SimpleString& text);
148148
};
149149

150+
class SignedBytesEqualFailure : public TestFailure
151+
{
152+
public:
153+
SignedBytesEqualFailure (UtestShell* test, const char* fileName, int lineNumber, signed char expected, signed char actual, const SimpleString& text);
154+
};
155+
150156
class StringEqualFailure : public TestFailure
151157
{
152158
public:

include/CppUTest/TestHarness_c.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
#define CHECK_EQUAL_C_CHAR(expected,actual) \
4646
CHECK_EQUAL_C_CHAR_LOCATION(expected,actual,__FILE__,__LINE__)
4747

48+
#define CHECK_EQUAL_C_UBYTE(expected,actual) \
49+
CHECK_EQUAL_C_UBYTE_LOCATION(expected,actual,__FILE__,__LINE__)
50+
51+
#define CHECK_EQUAL_C_SBYTE(expected,actual) \
52+
CHECK_EQUAL_C_SBYTE_LOCATION(expected,actual,__FILE__,__LINE__)
53+
4854
#define CHECK_EQUAL_C_STRING(expected,actual) \
4955
CHECK_EQUAL_C_STRING_LOCATION(expected,actual,__FILE__,__LINE__)
5056

@@ -120,6 +126,10 @@ extern void CHECK_EQUAL_C_REAL_LOCATION(double expected, double actual,
120126
double threshold, const char* fileName, int lineNumber);
121127
extern void CHECK_EQUAL_C_CHAR_LOCATION(char expected, char actual,
122128
const char* fileName, int lineNumber);
129+
extern void CHECK_EQUAL_C_UBYTE_LOCATION(unsigned char expected, unsigned char actual,
130+
const char* fileName, int lineNumber);
131+
extern void CHECK_EQUAL_C_SBYTE_LOCATION(signed char expected, signed char actual,
132+
const char* fileName, int lineNumber);
123133
extern void CHECK_EQUAL_C_STRING_LOCATION(const char* expected,
124134
const char* actual, const char* fileName, int lineNumber);
125135
extern void CHECK_EQUAL_C_POINTER_LOCATION(const void* expected,

include/CppUTest/Utest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class UtestShell
115115
virtual void assertUnsignedLongsEqual(unsigned long expected, unsigned long actual, const char* text, const char *fileName, int lineNumber, const TestTerminator& testTerminator = NormalTestTerminator());
116116
virtual void assertLongLongsEqual(cpputest_longlong expected, cpputest_longlong actual, const char* text, const char *fileName, int lineNumber, const TestTerminator& testTerminator = NormalTestTerminator());
117117
virtual void assertUnsignedLongLongsEqual(cpputest_ulonglong expected, cpputest_ulonglong actual, const char* text, const char *fileName, int lineNumber, const TestTerminator& testTerminator = NormalTestTerminator());
118+
virtual void assertSignedBytesEqual(signed char expected, signed char actual, const char* text, const char *fileName, int lineNumber, const TestTerminator& testTerminator = NormalTestTerminator());
118119
virtual void assertPointersEqual(const void *expected, const void *actual, const char* text, const char *fileName, int lineNumber, const TestTerminator& testTerminator = NormalTestTerminator());
119120
virtual void assertFunctionPointersEqual(void (*expected)(), void (*actual)(), const char* text, const char* fileName, int lineNumber, const TestTerminator& testTerminator = NormalTestTerminator());
120121
virtual void assertDoublesEqual(double expected, double actual, double threshold, const char* text, const char *fileName, int lineNumber, const TestTerminator& testTerminator = NormalTestTerminator());

include/CppUTest/UtestMacros.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@
232232
#define BYTES_EQUAL_TEXT(expected, actual, text)\
233233
LONGS_EQUAL_TEXT((expected) & 0xff, (actual) & 0xff, text)
234234

235+
#define SIGNED_BYTES_EQUAL(expected, actual)\
236+
SIGNED_BYTES_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
237+
238+
#define SIGNED_BYTES_EQUAL_LOCATION(expected, actual, file, line) \
239+
{ UtestShell::getCurrent()->assertSignedBytesEqual(expected, actual, NULL, file, line); }
240+
241+
#define SIGNED_BYTES_EQUAL_TEXT(expected, actual, text)\
242+
SIGNED_BYTES_EQUAL_TEXT_LOCATION(expected, actual, text, __FILE__, __LINE__)
243+
244+
#define SIGNED_BYTES_EQUAL_TEXT_LOCATION(expected, actual, text, file, line) \
245+
{ UtestShell::getCurrent()->assertSignedBytesEqual(expected, actual, text, file, line); }
246+
235247
#define POINTERS_EQUAL(expected, actual)\
236248
POINTERS_EQUAL_LOCATION((expected), (actual), NULL, __FILE__, __LINE__)
237249

src/CppUTest/SimpleString.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,16 @@ SimpleString HexStringFrom(long value)
478478
return StringFromFormat("%lx", value);
479479
}
480480

481+
SimpleString HexStringFrom(signed char value)
482+
{
483+
SimpleString result = StringFromFormat("%x", value);
484+
if(value < 0) {
485+
size_t size = result.size();
486+
result = result.subString(size-(CPPUTEST_CHAR_BIT/4));
487+
}
488+
return result;
489+
}
490+
481491
SimpleString HexStringFrom(unsigned long value)
482492
{
483493
return StringFromFormat("%lx", value);

src/CppUTest/TestFailure.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,24 @@ UnsignedLongLongsEqualFailure::UnsignedLongLongsEqualFailure(UtestShell* test, c
308308
message_ += createButWasString(expectedReported, actualReported);
309309
}
310310

311+
SignedBytesEqualFailure::SignedBytesEqualFailure (UtestShell* test, const char* fileName, int lineNumber, signed char expected, signed char actual, const SimpleString& text)
312+
: TestFailure(test, fileName, lineNumber)
313+
{
314+
message_ = createUserText(text);
315+
316+
SimpleString aDecimal = StringFrom((int)actual);
317+
SimpleString aHex = HexStringFrom(actual);
318+
SimpleString eDecimal = StringFrom((int)expected);
319+
SimpleString eHex = HexStringFrom(expected);
320+
321+
SimpleString::padStringsToSameLength(aDecimal, eDecimal, ' ');
322+
SimpleString::padStringsToSameLength(aHex, eHex, '0');
323+
324+
SimpleString actualReported = aDecimal + " 0x" + aHex;
325+
SimpleString expectedReported = eDecimal + " 0x" + eHex;
326+
message_ += createButWasString(expectedReported, actualReported);
327+
}
328+
311329
StringEqualFailure::StringEqualFailure(UtestShell* test, const char* fileName, int lineNumber, const char* expected, const char* actual, const SimpleString& text)
312330
: TestFailure(test, fileName, lineNumber)
313331
{

src/CppUTest/TestHarness_c.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ void CHECK_EQUAL_C_CHAR_LOCATION(char expected, char actual, const char* fileNam
5050
UtestShell::getCurrent()->assertEquals(((expected) != (actual)), StringFrom(expected).asCharString(), StringFrom(actual).asCharString(), NULL, fileName, lineNumber, TestTerminatorWithoutExceptions());
5151
}
5252

53+
extern void CHECK_EQUAL_C_UBYTE_LOCATION(unsigned char expected, unsigned char actual, const char* fileName, int lineNumber)\
54+
{
55+
UtestShell::getCurrent()->assertEquals(((expected) != (actual)),StringFrom((int)expected).asCharString(), StringFrom((int) actual).asCharString(), NULL, fileName, lineNumber, TestTerminatorWithoutExceptions());
56+
}
57+
58+
void CHECK_EQUAL_C_SBYTE_LOCATION(char signed expected, signed char actual, const char* fileName, int lineNumber)
59+
{
60+
UtestShell::getCurrent()->assertEquals(((expected) != (actual)),StringFrom((int)expected).asCharString(), StringFrom((int) actual).asCharString(), NULL, fileName, lineNumber, TestTerminatorWithoutExceptions());
61+
}
62+
5363
void CHECK_EQUAL_C_STRING_LOCATION(const char* expected, const char* actual, const char* fileName, int lineNumber)
5464
{
5565
UtestShell::getCurrent()->assertCstrEqual(expected, actual, NULL, fileName, lineNumber, TestTerminatorWithoutExceptions());

src/CppUTest/Utest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ void UtestShell::assertUnsignedLongLongsEqual(cpputest_ulonglong expected, cpput
455455
#endif
456456
}
457457

458+
void UtestShell::assertSignedBytesEqual(signed char expected, signed char actual, const char* text, const char *fileName, int lineNumber, const TestTerminator& testTerminator)
459+
{
460+
getTestResult()->countCheck();
461+
if (expected != actual)
462+
failWith(SignedBytesEqualFailure (this, fileName, lineNumber, expected, actual, text), testTerminator);
463+
}
464+
458465
void UtestShell::assertPointersEqual(const void* expected, const void* actual, const char* text, const char* fileName, int lineNumber, const TestTerminator& testTerminator)
459466
{
460467
getTestResult()->countCheck();

tests/SimpleStringTest.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,17 @@ TEST(SimpleString, FunctionPointers)
482482
TEST(SimpleString, Characters)
483483
{
484484
SimpleString s(StringFrom('a'));
485-
SimpleString s2(StringFrom('a'));
486-
CHECK(s == s2);
485+
STRCMP_EQUAL("a", s.asCharString());
486+
}
487+
488+
TEST(SimpleString, NegativeSignedBytes)
489+
{
490+
STRCMP_EQUAL("-15", StringFrom((signed char)-15).asCharString());
491+
}
492+
493+
TEST(SimpleString, PositiveSignedBytes)
494+
{
495+
STRCMP_EQUAL("4", StringFrom(4).asCharString());
487496
}
488497

489498
TEST(SimpleString, LongInts)
@@ -554,6 +563,8 @@ TEST(SimpleString, Sizes)
554563

555564
TEST(SimpleString, HexStrings)
556565
{
566+
STRCMP_EQUAL("f3", HexStringFrom((signed char)-13).asCharString());
567+
557568
SimpleString h1 = HexStringFrom(0xffffL);
558569
STRCMP_EQUAL("ffff", h1.asCharString());
559570

0 commit comments

Comments
 (0)