From 3631f1a203eb2e35810268d02437b86cc9f37302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Fri, 29 May 2015 19:32:58 +0200 Subject: [PATCH 1/8] Added support for memory buffer parameter checking in mocks. Now MockActualCall and MockExpectedCall both have new methods withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) and withParameter(const SimpleString& name, const unsigned char* value, size_t size), that allow checking that the buffer have the same length and contents. --- include/CppUTest/PlatformSpecificFunctions_c.h | 1 + include/CppUTest/SimpleString.h | 1 + include/CppUTestExt/MockActualCall.h | 2 ++ include/CppUTestExt/MockCheckedActualCall.h | 3 +++ include/CppUTestExt/MockCheckedExpectedCall.h | 3 +++ include/CppUTestExt/MockExpectedCall.h | 2 ++ include/CppUTestExt/MockNamedValue.h | 1 + src/CppUTest/SimpleString.cpp | 15 +++++++++++++++ src/CppUTestExt/MockActualCall.cpp | 16 ++++++++++++++++ src/CppUTestExt/MockExpectedCall.cpp | 16 ++++++++++++++++ src/CppUTestExt/MockNamedValue.cpp | 15 +++++++++++++++ src/Platforms/C2000/UtestPlatform.cpp | 6 ++++++ src/Platforms/Gcc/UtestPlatform.cpp | 1 + src/Platforms/GccNoStdC/UtestPlatform.cpp | 1 + src/Platforms/Iar/UtestPlatform.cpp | 5 +++++ src/Platforms/Symbian/UtestPlatform.cpp | 5 +++++ src/Platforms/VisualCpp/UtestPlatform.cpp | 1 + src/Platforms/armcc/UtestPlatform.cpp | 1 + 18 files changed, 95 insertions(+) diff --git a/include/CppUTest/PlatformSpecificFunctions_c.h b/include/CppUTest/PlatformSpecificFunctions_c.h index 732e48b1d..3a8155a5e 100644 --- a/include/CppUTest/PlatformSpecificFunctions_c.h +++ b/include/CppUTest/PlatformSpecificFunctions_c.h @@ -74,6 +74,7 @@ extern void* (*PlatformSpecificRealloc)(void* memory, size_t size); extern void (*PlatformSpecificFree)(void* memory); extern void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size); extern void* (*PlatformSpecificMemset)(void* mem, int c, size_t size); +extern int (*PlatformSpecificMemCmp)(const void* s1, const void* s2, size_t size); typedef void* PlatformSpecificMutex; extern PlatformSpecificMutex (*PlatformSpecificMutexCreate)(void); diff --git a/include/CppUTest/SimpleString.h b/include/CppUTest/SimpleString.h index 0c8ac3448..5eb29fdce 100644 --- a/include/CppUTest/SimpleString.h +++ b/include/CppUTest/SimpleString.h @@ -140,6 +140,7 @@ SimpleString StringFrom(int value); SimpleString StringFrom(unsigned int value); SimpleString StringFrom(long value); SimpleString StringFrom(unsigned long value); +SimpleString StringFrom(const unsigned char* value, size_t size); SimpleString HexStringFrom(long value); SimpleString HexStringFrom(unsigned long value); SimpleString HexStringFrom(const void* value); diff --git a/include/CppUTestExt/MockActualCall.h b/include/CppUTestExt/MockActualCall.h index 970a1cbb2..037f235d9 100644 --- a/include/CppUTestExt/MockActualCall.h +++ b/include/CppUTestExt/MockActualCall.h @@ -51,6 +51,7 @@ class MockActualCall MockActualCall& withParameter(const SimpleString& name, const char* value) { return withStringParameter(name, value); } MockActualCall& withParameter(const SimpleString& name, void* value) { return withPointerParameter(name, value); } MockActualCall& withParameter(const SimpleString& name, const void* value) { return withConstPointerParameter(name, value); } + MockActualCall& withParameter(const SimpleString& name, const unsigned char* value, size_t size) { return withMemoryBufferParameter(name, value, size); } virtual MockActualCall& withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value)=0; virtual MockActualCall& withOutputParameter(const SimpleString& name, void* output)=0; @@ -62,6 +63,7 @@ class MockActualCall virtual MockActualCall& withStringParameter(const SimpleString& name, const char* value)=0; virtual MockActualCall& withPointerParameter(const SimpleString& name, void* value)=0; virtual MockActualCall& withConstPointerParameter(const SimpleString& name, const void* value)=0; + virtual MockActualCall& withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size)=0; virtual bool hasReturnValue()=0; virtual MockNamedValue returnValue()=0; diff --git a/include/CppUTestExt/MockCheckedActualCall.h b/include/CppUTestExt/MockCheckedActualCall.h index cf1f70142..22c05d9fc 100644 --- a/include/CppUTestExt/MockCheckedActualCall.h +++ b/include/CppUTestExt/MockCheckedActualCall.h @@ -47,6 +47,7 @@ class MockCheckedActualCall : public MockActualCall virtual MockActualCall& withStringParameter(const SimpleString& name, const char* value) _override; virtual MockActualCall& withPointerParameter(const SimpleString& name, void* value) _override; virtual MockActualCall& withConstPointerParameter(const SimpleString& name, const void* value) _override; + virtual MockActualCall& withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) _override; virtual MockActualCall& withParameterOfType(const SimpleString& type, const SimpleString& name, const void* value) _override; virtual MockActualCall& withOutputParameter(const SimpleString& name, void* output) _override; @@ -147,6 +148,7 @@ class MockActualCallTrace : public MockActualCall virtual MockActualCall& withStringParameter(const SimpleString& name, const char* value) _override; virtual MockActualCall& withPointerParameter(const SimpleString& name, void* value) _override; virtual MockActualCall& withConstPointerParameter(const SimpleString& name, const void* value) _override; + virtual MockActualCall& withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) _override; virtual MockActualCall& withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value) _override; virtual MockActualCall& withOutputParameter(const SimpleString& name, void* output) _override; @@ -202,6 +204,7 @@ class MockIgnoredActualCall: public MockActualCall virtual MockActualCall& withStringParameter(const SimpleString&, const char*) _override { return *this; } virtual MockActualCall& withPointerParameter(const SimpleString& , void*) _override { return *this; } virtual MockActualCall& withConstPointerParameter(const SimpleString& , const void*) _override { return *this; } + virtual MockActualCall& withMemoryBufferParameter(const SimpleString&, const unsigned char*, size_t) _override { return *this; }; virtual MockActualCall& withParameterOfType(const SimpleString&, const SimpleString&, const void*) _override { return *this; } virtual MockActualCall& withOutputParameter(const SimpleString&, void*) _override { return *this; } diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index 4b6f6b2be..23a51e0a1 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -48,6 +48,7 @@ class MockCheckedExpectedCall : public MockExpectedCall virtual MockExpectedCall& withStringParameter(const SimpleString& name, const char* value) _override; virtual MockExpectedCall& withPointerParameter(const SimpleString& name, void* value) _override; virtual MockExpectedCall& withConstPointerParameter(const SimpleString& name, const void* value) _override; + virtual MockExpectedCall& withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) _override; virtual MockExpectedCall& withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value) _override; virtual MockExpectedCall& ignoreOtherParameters() _override; virtual MockExpectedCall& withOutputParameterReturning(const SimpleString& name, const void* value, size_t size) _override; @@ -144,6 +145,7 @@ class MockExpectedCallComposite : public MockExpectedCall virtual MockExpectedCall& withStringParameter(const SimpleString& name, const char* value) _override; virtual MockExpectedCall& withConstPointerParameter(const SimpleString& name, const void* value) _override; virtual MockExpectedCall& withPointerParameter(const SimpleString& name, void* value) _override; + virtual MockExpectedCall& withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) _override; virtual MockExpectedCall& withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value) _override; virtual MockExpectedCall& withOutputParameterReturning(const SimpleString& name, const void* value, size_t size) _override; virtual MockExpectedCall& ignoreOtherParameters() _override; @@ -179,6 +181,7 @@ class MockIgnoredExpectedCall: public MockExpectedCall virtual MockExpectedCall& withStringParameter(const SimpleString&, const char*) _override { return *this; } virtual MockExpectedCall& withPointerParameter(const SimpleString& , void*) _override { return *this; } virtual MockExpectedCall& withConstPointerParameter(const SimpleString& , const void*) _override { return *this; } + virtual MockExpectedCall& withMemoryBufferParameter(const SimpleString&, const unsigned char*, size_t) _override { return *this; }; virtual MockExpectedCall& withParameterOfType(const SimpleString&, const SimpleString&, const void*) _override { return *this; } virtual MockExpectedCall& withOutputParameterReturning(const SimpleString&, const void*, size_t) { return *this; } virtual MockExpectedCall& ignoreOtherParameters() { return *this;} diff --git a/include/CppUTestExt/MockExpectedCall.h b/include/CppUTestExt/MockExpectedCall.h index 919fbd35d..341ea425b 100644 --- a/include/CppUTestExt/MockExpectedCall.h +++ b/include/CppUTestExt/MockExpectedCall.h @@ -48,6 +48,7 @@ class MockExpectedCall MockExpectedCall& withParameter(const SimpleString& name, const char* value) { return withStringParameter(name, value); } MockExpectedCall& withParameter(const SimpleString& name, void* value) { return withPointerParameter(name, value); } MockExpectedCall& withParameter(const SimpleString& name, const void* value) { return withConstPointerParameter(name, value); } + MockExpectedCall& withParameter(const SimpleString& name, const unsigned char* value, size_t size) { return withMemoryBufferParameter(name, value, size); } virtual MockExpectedCall& withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value)=0; virtual MockExpectedCall& withOutputParameterReturning(const SimpleString& name, const void* value, size_t size)=0; virtual MockExpectedCall& ignoreOtherParameters()=0; @@ -60,6 +61,7 @@ class MockExpectedCall virtual MockExpectedCall& withStringParameter(const SimpleString& name, const char* value)=0; virtual MockExpectedCall& withPointerParameter(const SimpleString& name, void* value)=0; virtual MockExpectedCall& withConstPointerParameter(const SimpleString& name, const void* value)=0; + virtual MockExpectedCall& withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size)=0; virtual MockExpectedCall& andReturnValue(int value)=0; virtual MockExpectedCall& andReturnValue(unsigned int value)=0; virtual MockExpectedCall& andReturnValue(long int value)=0; diff --git a/include/CppUTestExt/MockNamedValue.h b/include/CppUTestExt/MockNamedValue.h index a0cae0d1f..cd242d6ab 100644 --- a/include/CppUTestExt/MockNamedValue.h +++ b/include/CppUTestExt/MockNamedValue.h @@ -82,6 +82,7 @@ class MockNamedValue virtual void setValue(void* value); virtual void setValue(const void* value); virtual void setValue(const char* value); + virtual void setValue(const unsigned char* value); virtual void setObjectPointer(const SimpleString& type, const void* objectPtr); virtual void setSize(size_t size); diff --git a/src/CppUTest/SimpleString.cpp b/src/CppUTest/SimpleString.cpp index 6aaeafa49..c1fe7a59f 100644 --- a/src/CppUTest/SimpleString.cpp +++ b/src/CppUTest/SimpleString.cpp @@ -490,6 +490,21 @@ SimpleString HexStringFrom(const void* value) return StringFromFormat("%lx", convertPointerToLongValue(value)); } +SimpleString StringFrom(const unsigned char* value, size_t size) +{ + SimpleString str = StringFromFormat("Len = %1u | HexContents =", size); + size_t displayedSize = ((size > 128) ? 128 : size); + for (size_t i = 0; i < displayedSize; i++) + { + str += StringFromFormat(" %02X", value[i]); + } + if( size > displayedSize ) + { + str += " ..."; + } + return str; +} + SimpleString StringFrom(double value, int precision) { return StringFromFormat("%.*g", precision, value); diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index d8fcb10d1..4190f34b3 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -222,6 +222,15 @@ MockActualCall& MockCheckedActualCall::withConstPointerParameter(const SimpleStr return *this; } +MockActualCall& MockCheckedActualCall::withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) +{ + MockNamedValue actualParameter(name); + actualParameter.setValue(value); + actualParameter.setSize(size); + checkInputParameter(actualParameter); + return *this; +} + MockActualCall& MockCheckedActualCall::withParameterOfType(const SimpleString& type, const SimpleString& name, const void* value) { MockNamedValue actualParameter(name); @@ -531,6 +540,13 @@ MockActualCall& MockActualCallTrace::withConstPointerParameter(const SimpleStrin return *this; } +MockActualCall& MockActualCallTrace::withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) +{ + addParameterName(name); + traceBuffer_ += StringFrom(value, size); + return *this; +} + MockActualCall& MockActualCallTrace::withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value) { traceBuffer_ += " "; diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index def97e566..e07650cd0 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -137,6 +137,15 @@ MockExpectedCall& MockCheckedExpectedCall::withConstPointerParameter(const Simpl return *this; } +MockExpectedCall& MockCheckedExpectedCall::withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) +{ + MockNamedValue* newParameter = new MockExpectedFunctionParameter(name); + inputParameters_->add(newParameter); + newParameter->setValue(value); + newParameter->setSize(size); + return *this; +} + MockExpectedCall& MockCheckedExpectedCall::withParameterOfType(const SimpleString& type, const SimpleString& name, const void* value) { MockNamedValue* newParameter = new MockExpectedFunctionParameter(name); @@ -551,6 +560,13 @@ MockExpectedCall& MockExpectedCallComposite::withConstPointerParameter(const Sim return *this; } +MockExpectedCall& MockExpectedCallComposite::withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) +{ + for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) + node->call_.withParameter(name, value, size); + return *this; +} + MockExpectedCall& MockExpectedCallComposite::withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value) { for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) diff --git a/src/CppUTestExt/MockNamedValue.cpp b/src/CppUTestExt/MockNamedValue.cpp index c272c37aa..c92d39608 100644 --- a/src/CppUTestExt/MockNamedValue.cpp +++ b/src/CppUTestExt/MockNamedValue.cpp @@ -94,6 +94,12 @@ void MockNamedValue::setValue(const char* value) value_.stringValue_ = value; } +void MockNamedValue::setValue(const unsigned char* value) +{ + type_ = "const unsigned char*"; + value_.stringValue_ = (const char*) value; +} + void MockNamedValue::setObjectPointer(const SimpleString& type, const void* objectPtr) { type_ = type; @@ -251,6 +257,13 @@ bool MockNamedValue::equals(const MockNamedValue& p) const return value_.constPointerValue_ == p.value_.constPointerValue_; else if (type_ == "double") return (doubles_equal(value_.doubleValue_, p.value_.doubleValue_, 0.005)); + else if (type_ == "const unsigned char*") + { + if (size_ != p.size_) { + return false; + } + return PlatformSpecificMemCmp(value_.stringValue_, p.value_.stringValue_, size_) == 0; + } if (comparator_) return comparator_->isEqual(value_.objectPointerValue_, p.value_.objectPointerValue_); @@ -276,6 +289,8 @@ SimpleString MockNamedValue::toString() const return StringFrom(value_.constPointerValue_); else if (type_ == "double") return StringFrom(value_.doubleValue_); + else if (type_ == "const unsigned char*") + return StringFrom((const unsigned char*) value_.stringValue_, size_); if (comparator_) return comparator_->valueToString(value_.objectPointerValue_); diff --git a/src/Platforms/C2000/UtestPlatform.cpp b/src/Platforms/C2000/UtestPlatform.cpp index 5fa63c3a9..2a2a9f77a 100644 --- a/src/Platforms/C2000/UtestPlatform.cpp +++ b/src/Platforms/C2000/UtestPlatform.cpp @@ -200,11 +200,17 @@ static void* C2000Memset(void* mem, int c, size_t size) return mem; } +static void* C2000MemCmp(void* s1, const void* s2, size_t size) +{ + return far_memcmp((long)s1, (long)s2, size); +} + void* (*PlatformSpecificMalloc)(size_t size) = C2000Malloc; void* (*PlatformSpecificRealloc)(void* memory, size_t size) = C2000Realloc; void (*PlatformSpecificFree)(void* memory) = C2000Free; void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = C2000MemCpy; void* (*PlatformSpecificMemset)(void* mem, int c, size_t size) = C2000Memset; +int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = C2000MemCmp; /* double PlatformSpecificFabs(double d) diff --git a/src/Platforms/Gcc/UtestPlatform.cpp b/src/Platforms/Gcc/UtestPlatform.cpp index 447b801d8..01d4cd88f 100644 --- a/src/Platforms/Gcc/UtestPlatform.cpp +++ b/src/Platforms/Gcc/UtestPlatform.cpp @@ -231,6 +231,7 @@ void* (*PlatformSpecificRealloc)(void*, size_t) = realloc; void (*PlatformSpecificFree)(void* memory) = free; void* (*PlatformSpecificMemCpy)(void*, const void*, size_t) = memcpy; void* (*PlatformSpecificMemset)(void*, int, size_t) = memset; +int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = memcmp; static int IsNanImplementation(double d) { diff --git a/src/Platforms/GccNoStdC/UtestPlatform.cpp b/src/Platforms/GccNoStdC/UtestPlatform.cpp index 678d58a0a..92a0f1980 100644 --- a/src/Platforms/GccNoStdC/UtestPlatform.cpp +++ b/src/Platforms/GccNoStdC/UtestPlatform.cpp @@ -51,6 +51,7 @@ void* (*PlatformSpecificRealloc)(void*, size_t) = NULL; void (*PlatformSpecificFree)(void*) = NULL; void* (*PlatformSpecificMemCpy)(void*, const void*, size_t) = NULL; void* (*PlatformSpecificMemset)(void*, int, size_t) = NULL; +int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = NULL; PlatformSpecificMutex (*PlatformSpecificMutexCreate)(void) = NULL; void (*PlatformSpecificMutexLock)(PlatformSpecificMutex mtx) = NULL; diff --git a/src/Platforms/Iar/UtestPlatform.cpp b/src/Platforms/Iar/UtestPlatform.cpp index 97d1c46d8..2d6c4313c 100644 --- a/src/Platforms/Iar/UtestPlatform.cpp +++ b/src/Platforms/Iar/UtestPlatform.cpp @@ -157,6 +157,11 @@ void* PlatformSpecificMemset(void* mem, int c, size_t size) return memset(mem, c, size); } +int PlatformSpecificMemCmp(const void* s1, const void* s2, size_t size) +{ + return memcmp(s1, s2, size); +} + double PlatformSpecificFabs(double d) { return fabs(d); diff --git a/src/Platforms/Symbian/UtestPlatform.cpp b/src/Platforms/Symbian/UtestPlatform.cpp index 7f0f6601b..955d7b1a6 100644 --- a/src/Platforms/Symbian/UtestPlatform.cpp +++ b/src/Platforms/Symbian/UtestPlatform.cpp @@ -125,6 +125,11 @@ void* PlatformSpecificMemset(void* mem, int c, size_t size) return memset(mem, c, size); } +int PlatformSpecificMemCmp(const void* s1, const void* s2, size_t size) +{ + return memcmp(s1, s2, size); +} + PlatformSpecificFile PlatformSpecificFOpen(const char* filename, const char* flag) { return fopen(filename, flag); } diff --git a/src/Platforms/VisualCpp/UtestPlatform.cpp b/src/Platforms/VisualCpp/UtestPlatform.cpp index bc78c7c3a..3289bc6a4 100644 --- a/src/Platforms/VisualCpp/UtestPlatform.cpp +++ b/src/Platforms/VisualCpp/UtestPlatform.cpp @@ -160,6 +160,7 @@ void* (*PlatformSpecificRealloc)(void* memory, size_t size) = realloc; void (*PlatformSpecificFree)(void* memory) = free; void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = memcpy; void* (*PlatformSpecificMemset)(void* mem, int c, size_t size) = memset; +int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = memcmp; double (*PlatformSpecificFabs)(double d) = fabs; extern "C" int (*PlatformSpecificIsNan)(double) = _isnan; diff --git a/src/Platforms/armcc/UtestPlatform.cpp b/src/Platforms/armcc/UtestPlatform.cpp index 3568e6f4c..c940f8915 100644 --- a/src/Platforms/armcc/UtestPlatform.cpp +++ b/src/Platforms/armcc/UtestPlatform.cpp @@ -150,6 +150,7 @@ extern "C" void* (*PlatformSpecificRealloc) (void*, size_t) = realloc; extern "C" void (*PlatformSpecificFree)(void*) = free; extern "C" void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = memcpy; extern "C" void* (*PlatformSpecificMemset)(void*, int, size_t) = memset; +extern "C" int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = memcmp; static int IsNanImplementation(double d) { From b92a90dea73be93ec4dcc49b2a9ac9e826626cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Fri, 29 May 2015 20:30:24 +0200 Subject: [PATCH 2/8] Added tests for the new memory buffer parameters in mocks and fixed minor errors --- include/CppUTestExt/MockNamedValue.h | 2 ++ src/CppUTestExt/MockNamedValue.cpp | 12 +++++++++--- tests/CppUTestExt/MockActualCallTest.cpp | 3 +++ tests/CppUTestExt/MockExpectedCallTest.cpp | 17 +++++++++++++++++ tests/SimpleStringTest.cpp | 7 +++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/include/CppUTestExt/MockNamedValue.h b/include/CppUTestExt/MockNamedValue.h index cd242d6ab..6567d5dfe 100644 --- a/include/CppUTestExt/MockNamedValue.h +++ b/include/CppUTestExt/MockNamedValue.h @@ -103,6 +103,7 @@ class MockNamedValue virtual const char* getStringValue() const; virtual void* getPointerValue() const; virtual const void* getConstPointerValue() const; + virtual const unsigned char* getMemBufferValue() const; virtual const void* getObjectPointer() const; virtual size_t getSize() const; virtual MockNamedValueComparator* getComparator() const; @@ -120,6 +121,7 @@ class MockNamedValue const char* stringValue_; void* pointerValue_; const void* constPointerValue_; + const unsigned char* memBufferValue_; const void* objectPointerValue_; const void* outputPointerValue_; } value_; diff --git a/src/CppUTestExt/MockNamedValue.cpp b/src/CppUTestExt/MockNamedValue.cpp index c92d39608..db1d1258c 100644 --- a/src/CppUTestExt/MockNamedValue.cpp +++ b/src/CppUTestExt/MockNamedValue.cpp @@ -97,7 +97,7 @@ void MockNamedValue::setValue(const char* value) void MockNamedValue::setValue(const unsigned char* value) { type_ = "const unsigned char*"; - value_.stringValue_ = (const char*) value; + value_.memBufferValue_ = value; } void MockNamedValue::setObjectPointer(const SimpleString& type, const void* objectPtr) @@ -197,6 +197,12 @@ const void* MockNamedValue::getConstPointerValue() const return value_.pointerValue_; } +const unsigned char* MockNamedValue::getMemBufferValue() const +{ + STRCMP_EQUAL("const unsigned char*", type_.asCharString()); + return value_.memBufferValue_; +} + const void* MockNamedValue::getObjectPointer() const { return value_.objectPointerValue_; @@ -262,7 +268,7 @@ bool MockNamedValue::equals(const MockNamedValue& p) const if (size_ != p.size_) { return false; } - return PlatformSpecificMemCmp(value_.stringValue_, p.value_.stringValue_, size_) == 0; + return PlatformSpecificMemCmp(value_.memBufferValue_, p.value_.memBufferValue_, size_) == 0; } if (comparator_) @@ -290,7 +296,7 @@ SimpleString MockNamedValue::toString() const else if (type_ == "double") return StringFrom(value_.doubleValue_); else if (type_ == "const unsigned char*") - return StringFrom((const unsigned char*) value_.stringValue_, size_); + return StringFrom(value_.memBufferValue_, size_); if (comparator_) return comparator_->valueToString(value_.objectPointerValue_); diff --git a/tests/CppUTestExt/MockActualCallTest.cpp b/tests/CppUTestExt/MockActualCallTest.cpp index bb5f205e5..94164cc3f 100644 --- a/tests/CppUTestExt/MockActualCallTest.cpp +++ b/tests/CppUTestExt/MockActualCallTest.cpp @@ -147,6 +147,7 @@ TEST(MockCheckedActualCall, remainderOfMockActualCallTraceWorksAsItShould) { int value; const int const_value = 1; + const unsigned char mem_buffer[] = { 0xFE, 0x15 }; MockActualCallTrace actual; actual.withName("func"); actual.withCallOrder(1); @@ -157,6 +158,7 @@ TEST(MockCheckedActualCall, remainderOfMockActualCallTraceWorksAsItShould) actual.withLongIntParameter("long_int", (long int) 1); actual.withPointerParameter("pointer", &value); actual.withConstPointerParameter("const_pointer", &const_value); + actual.withMemoryBufferParameter("mem_buffer", mem_buffer, sizeof(mem_buffer)); actual.withParameterOfType("int", "named_type", &const_value); SimpleString expectedString("\nFunction name:func"); @@ -170,6 +172,7 @@ TEST(MockCheckedActualCall, remainderOfMockActualCallTraceWorksAsItShould) expectedString += HexStringFrom(&value); expectedString += " const_pointer:0x"; expectedString += HexStringFrom(&const_value); + expectedString += " mem_buffer:Len = 2 | HexContents = FE 15"; expectedString += " int named_type:0x"; expectedString += HexStringFrom(&const_value); STRCMP_EQUAL(expectedString.asCharString(), actual.getTraceOutput()); diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index 4d8cffa26..3d5386c69 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -176,6 +176,15 @@ TEST(MockExpectedCall, callWithConstPointerParameter) POINTERS_EQUAL(ptr, call->getInputParameter("constPointer").getConstPointerValue()); } +TEST(MockExpectedCall, callWithMemoryBuffer) +{ + const unsigned char mem_buffer[] = { 0x12, 0xFE, 0xA1 }; + call->withParameter("memoryBuffer", mem_buffer, sizeof(mem_buffer)); + STRCMP_EQUAL("const unsigned char*", call->getInputParameterType("memoryBuffer").asCharString()); + POINTERS_EQUAL( (void*) mem_buffer, (void*) call->getInputParameter("memoryBuffer").getMemBufferValue() ); + LONGS_EQUAL(sizeof(mem_buffer), call->getInputParameter("memoryBuffer").getSize()); +} + TEST(MockExpectedCall, callWithObjectParameter) { void* ptr = (void*) 0x123; @@ -456,6 +465,13 @@ TEST(MockExpectedCallComposite, hasConstPointerParameter) STRCMP_EQUAL("name -> const void* param: <0x0>", call.callToString().asCharString()); } +TEST(MockExpectedCallComposite, hasMemoryBufferParameter) +{ + const unsigned char mem_buffer[] = { 0x89, 0xFE, 0x15 }; + composite.withParameter("param", mem_buffer, sizeof(mem_buffer)); + STRCMP_EQUAL("name -> const unsigned char* param: ", call.callToString().asCharString()); +} + TEST(MockExpectedCallComposite, hasParameterOfType) { composite.withParameterOfType("type", "param", (const void*) 0); @@ -566,6 +582,7 @@ TEST(MockIgnoredExpectedCall, worksAsItShould) ignored.withStringParameter("goo", "hello"); ignored.withPointerParameter("pie", (void*) 0); ignored.withConstPointerParameter("woo", (const void*) 0); + ignored.withMemoryBufferParameter("waa", (const unsigned char*) 0, 0); ignored.withParameterOfType("top", "mytype", (const void*) 0); ignored.withOutputParameterReturning("bar", (const void*) 0, 1); ignored.ignoreOtherParameters(); diff --git a/tests/SimpleStringTest.cpp b/tests/SimpleStringTest.cpp index 9e087fe2f..c1dbefa06 100644 --- a/tests/SimpleStringTest.cpp +++ b/tests/SimpleStringTest.cpp @@ -459,6 +459,13 @@ TEST(SimpleString, SmallDoubles) STRCMP_CONTAINS("1.2e", s.asCharString()); } +TEST(SimpleString, MemoryBuffers) +{ + const unsigned char mem_buffer[] = { 0x12, 0xFE, 0xA1 }; + SimpleString s( StringFrom( mem_buffer, sizeof(mem_buffer) ) ); + STRCMP_EQUAL("Len = 3 | HexContents = 12 FE A1", s.asCharString()); +} + TEST(SimpleString, Sizes) { size_t size = 10; From 05f21bdebd881424adf0f0f4c255d6359a260354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Fri, 29 May 2015 20:36:01 +0200 Subject: [PATCH 3/8] Fixed error in sprintf format --- src/CppUTest/SimpleString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CppUTest/SimpleString.cpp b/src/CppUTest/SimpleString.cpp index c1fe7a59f..a7c55239a 100644 --- a/src/CppUTest/SimpleString.cpp +++ b/src/CppUTest/SimpleString.cpp @@ -492,7 +492,7 @@ SimpleString HexStringFrom(const void* value) SimpleString StringFrom(const unsigned char* value, size_t size) { - SimpleString str = StringFromFormat("Len = %1u | HexContents =", size); + SimpleString str = StringFromFormat("Len = %lu | HexContents =", size); size_t displayedSize = ((size > 128) ? 128 : size); for (size_t i = 0; i < displayedSize; i++) { From 8f815c10ebe17ae37eb761f477eb466d9ef45061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Fri, 29 May 2015 22:01:04 +0200 Subject: [PATCH 4/8] Removed extra ';'s that keep Travis-CI naggin... --- include/CppUTestExt/MockCheckedActualCall.h | 2 +- include/CppUTestExt/MockCheckedExpectedCall.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/CppUTestExt/MockCheckedActualCall.h b/include/CppUTestExt/MockCheckedActualCall.h index 22c05d9fc..06920d624 100644 --- a/include/CppUTestExt/MockCheckedActualCall.h +++ b/include/CppUTestExt/MockCheckedActualCall.h @@ -204,7 +204,7 @@ class MockIgnoredActualCall: public MockActualCall virtual MockActualCall& withStringParameter(const SimpleString&, const char*) _override { return *this; } virtual MockActualCall& withPointerParameter(const SimpleString& , void*) _override { return *this; } virtual MockActualCall& withConstPointerParameter(const SimpleString& , const void*) _override { return *this; } - virtual MockActualCall& withMemoryBufferParameter(const SimpleString&, const unsigned char*, size_t) _override { return *this; }; + virtual MockActualCall& withMemoryBufferParameter(const SimpleString&, const unsigned char*, size_t) _override { return *this; } virtual MockActualCall& withParameterOfType(const SimpleString&, const SimpleString&, const void*) _override { return *this; } virtual MockActualCall& withOutputParameter(const SimpleString&, void*) _override { return *this; } diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index 23a51e0a1..81968fca8 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -181,7 +181,7 @@ class MockIgnoredExpectedCall: public MockExpectedCall virtual MockExpectedCall& withStringParameter(const SimpleString&, const char*) _override { return *this; } virtual MockExpectedCall& withPointerParameter(const SimpleString& , void*) _override { return *this; } virtual MockExpectedCall& withConstPointerParameter(const SimpleString& , const void*) _override { return *this; } - virtual MockExpectedCall& withMemoryBufferParameter(const SimpleString&, const unsigned char*, size_t) _override { return *this; }; + virtual MockExpectedCall& withMemoryBufferParameter(const SimpleString&, const unsigned char*, size_t) _override { return *this; } virtual MockExpectedCall& withParameterOfType(const SimpleString&, const SimpleString&, const void*) _override { return *this; } virtual MockExpectedCall& withOutputParameterReturning(const SimpleString&, const void*, size_t) { return *this; } virtual MockExpectedCall& ignoreOtherParameters() { return *this;} From 1ba3a57fa643976fb8aed6e8b4255bee99a229f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Wed, 3 Jun 2015 00:39:46 +0200 Subject: [PATCH 5/8] Added tests to check MockActualCall::withMemoryBufferParameter --- tests/CppUTestExt/MockSupportTest.cpp | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/CppUTestExt/MockSupportTest.cpp b/tests/CppUTestExt/MockSupportTest.cpp index c29ddbd89..b4b618c82 100644 --- a/tests/CppUTestExt/MockSupportTest.cpp +++ b/tests/CppUTestExt/MockSupportTest.cpp @@ -436,6 +436,50 @@ TEST(MockSupportTest, expectOneConstPointerParameterAndValue) CHECK_NO_MOCK_FAILURE(); } +TEST(MockSupportTest, expectOneMemBufferParameterAndValue) +{ + unsigned char memBuffer1[] = { 0x12, 0x15, 0xFF }; + unsigned char memBuffer2[] = { 0x12, 0x15, 0xFF }; + mock().expectOneCall("foo").withParameter("parameter", memBuffer1, sizeof(memBuffer1)); + mock().actualCall("foo").withParameter("parameter", memBuffer2, sizeof(memBuffer2)); + mock().checkExpectations(); + CHECK_NO_MOCK_FAILURE(); +} + +TEST(MockSupportTest, expectOneMemBufferParameterAndValueFailsDueToContents) +{ + unsigned char memBuffer1[] = { 0x12, 0x15, 0xFF }; + unsigned char memBuffer2[] = { 0x12, 0x05, 0xFF }; + + MockNamedValue parameter("parameter"); + parameter.setValue( memBuffer2 ); + parameter.setSize( sizeof(memBuffer2) ); + addFunctionToExpectationsList("foo")->withParameter("parameter", memBuffer1, sizeof(memBuffer1)); + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, *expectationsList); + + mock().expectOneCall("foo").withParameter("parameter", memBuffer1, sizeof(memBuffer1)); + mock().actualCall("foo").withParameter("parameter", memBuffer2, sizeof(memBuffer2)); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + +TEST(MockSupportTest, expectOneMemBufferParameterAndValueFailsDueToSize) +{ + unsigned char memBuffer1[] = { 0x12, 0x15, 0xFF }; + unsigned char memBuffer2[] = { 0x12, 0x15, 0xFF, 0x90 }; + + MockNamedValue parameter("parameter"); + parameter.setValue( memBuffer2 ); + parameter.setSize( sizeof(memBuffer2) ); + addFunctionToExpectationsList("foo")->withParameter("parameter", memBuffer1, sizeof(memBuffer1)); + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, *expectationsList); + + mock().expectOneCall("foo").withParameter("parameter", memBuffer1, sizeof(memBuffer1)); + mock().actualCall("foo").withParameter("parameter", memBuffer2, sizeof(memBuffer2)); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + TEST(MockSupportTest, expectOneStringParameterAndValueFails) { MockNamedValue parameter("parameter"); From 0793d75eb400c189555ea54a219f6087743e6f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 16 Jun 2015 02:35:07 +0200 Subject: [PATCH 6/8] Removed usage of platform-specific memcpy and modified MockNamedValue to have a differentiated setMemoryBuffer method (instead of setValue) --- include/CppUTest/PlatformSpecificFunctions_c.h | 1 - include/CppUTestExt/MockNamedValue.h | 2 +- src/CppUTestExt/MockActualCall.cpp | 3 +-- src/CppUTestExt/MockExpectedCall.cpp | 3 +-- src/CppUTestExt/MockNamedValue.cpp | 5 +++-- src/Platforms/C2000/UtestPlatform.cpp | 6 ------ src/Platforms/Gcc/UtestPlatform.cpp | 1 - src/Platforms/GccNoStdC/UtestPlatform.cpp | 1 - src/Platforms/Iar/UtestPlatform.cpp | 5 ----- src/Platforms/Symbian/UtestPlatform.cpp | 5 ----- src/Platforms/VisualCpp/UtestPlatform.cpp | 1 - src/Platforms/armcc/UtestPlatform.cpp | 1 - 12 files changed, 6 insertions(+), 28 deletions(-) diff --git a/include/CppUTest/PlatformSpecificFunctions_c.h b/include/CppUTest/PlatformSpecificFunctions_c.h index 3a8155a5e..732e48b1d 100644 --- a/include/CppUTest/PlatformSpecificFunctions_c.h +++ b/include/CppUTest/PlatformSpecificFunctions_c.h @@ -74,7 +74,6 @@ extern void* (*PlatformSpecificRealloc)(void* memory, size_t size); extern void (*PlatformSpecificFree)(void* memory); extern void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size); extern void* (*PlatformSpecificMemset)(void* mem, int c, size_t size); -extern int (*PlatformSpecificMemCmp)(const void* s1, const void* s2, size_t size); typedef void* PlatformSpecificMutex; extern PlatformSpecificMutex (*PlatformSpecificMutexCreate)(void); diff --git a/include/CppUTestExt/MockNamedValue.h b/include/CppUTestExt/MockNamedValue.h index 6567d5dfe..a23b75238 100644 --- a/include/CppUTestExt/MockNamedValue.h +++ b/include/CppUTestExt/MockNamedValue.h @@ -82,7 +82,7 @@ class MockNamedValue virtual void setValue(void* value); virtual void setValue(const void* value); virtual void setValue(const char* value); - virtual void setValue(const unsigned char* value); + virtual void setMemoryBuffer(const unsigned char* value, size_t size); virtual void setObjectPointer(const SimpleString& type, const void* objectPtr); virtual void setSize(size_t size); diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index 4190f34b3..f57fe5b70 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -225,8 +225,7 @@ MockActualCall& MockCheckedActualCall::withConstPointerParameter(const SimpleStr MockActualCall& MockCheckedActualCall::withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) { MockNamedValue actualParameter(name); - actualParameter.setValue(value); - actualParameter.setSize(size); + actualParameter.setMemoryBuffer(value, size); checkInputParameter(actualParameter); return *this; } diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index e07650cd0..401b93b2e 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -141,8 +141,7 @@ MockExpectedCall& MockCheckedExpectedCall::withMemoryBufferParameter(const Simpl { MockNamedValue* newParameter = new MockExpectedFunctionParameter(name); inputParameters_->add(newParameter); - newParameter->setValue(value); - newParameter->setSize(size); + newParameter->setMemoryBuffer(value, size); return *this; } diff --git a/src/CppUTestExt/MockNamedValue.cpp b/src/CppUTestExt/MockNamedValue.cpp index db1d1258c..9d64b192e 100644 --- a/src/CppUTestExt/MockNamedValue.cpp +++ b/src/CppUTestExt/MockNamedValue.cpp @@ -94,10 +94,11 @@ void MockNamedValue::setValue(const char* value) value_.stringValue_ = value; } -void MockNamedValue::setValue(const unsigned char* value) +void MockNamedValue::setMemoryBuffer(const unsigned char* value, size_t size) { type_ = "const unsigned char*"; value_.memBufferValue_ = value; + size_ = size; } void MockNamedValue::setObjectPointer(const SimpleString& type, const void* objectPtr) @@ -268,7 +269,7 @@ bool MockNamedValue::equals(const MockNamedValue& p) const if (size_ != p.size_) { return false; } - return PlatformSpecificMemCmp(value_.memBufferValue_, p.value_.memBufferValue_, size_) == 0; + return SimpleString::MemCmp(value_.memBufferValue_, p.value_.memBufferValue_, size_) == 0; } if (comparator_) diff --git a/src/Platforms/C2000/UtestPlatform.cpp b/src/Platforms/C2000/UtestPlatform.cpp index 2a2a9f77a..5fa63c3a9 100644 --- a/src/Platforms/C2000/UtestPlatform.cpp +++ b/src/Platforms/C2000/UtestPlatform.cpp @@ -200,17 +200,11 @@ static void* C2000Memset(void* mem, int c, size_t size) return mem; } -static void* C2000MemCmp(void* s1, const void* s2, size_t size) -{ - return far_memcmp((long)s1, (long)s2, size); -} - void* (*PlatformSpecificMalloc)(size_t size) = C2000Malloc; void* (*PlatformSpecificRealloc)(void* memory, size_t size) = C2000Realloc; void (*PlatformSpecificFree)(void* memory) = C2000Free; void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = C2000MemCpy; void* (*PlatformSpecificMemset)(void* mem, int c, size_t size) = C2000Memset; -int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = C2000MemCmp; /* double PlatformSpecificFabs(double d) diff --git a/src/Platforms/Gcc/UtestPlatform.cpp b/src/Platforms/Gcc/UtestPlatform.cpp index 01d4cd88f..447b801d8 100644 --- a/src/Platforms/Gcc/UtestPlatform.cpp +++ b/src/Platforms/Gcc/UtestPlatform.cpp @@ -231,7 +231,6 @@ void* (*PlatformSpecificRealloc)(void*, size_t) = realloc; void (*PlatformSpecificFree)(void* memory) = free; void* (*PlatformSpecificMemCpy)(void*, const void*, size_t) = memcpy; void* (*PlatformSpecificMemset)(void*, int, size_t) = memset; -int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = memcmp; static int IsNanImplementation(double d) { diff --git a/src/Platforms/GccNoStdC/UtestPlatform.cpp b/src/Platforms/GccNoStdC/UtestPlatform.cpp index 92a0f1980..678d58a0a 100644 --- a/src/Platforms/GccNoStdC/UtestPlatform.cpp +++ b/src/Platforms/GccNoStdC/UtestPlatform.cpp @@ -51,7 +51,6 @@ void* (*PlatformSpecificRealloc)(void*, size_t) = NULL; void (*PlatformSpecificFree)(void*) = NULL; void* (*PlatformSpecificMemCpy)(void*, const void*, size_t) = NULL; void* (*PlatformSpecificMemset)(void*, int, size_t) = NULL; -int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = NULL; PlatformSpecificMutex (*PlatformSpecificMutexCreate)(void) = NULL; void (*PlatformSpecificMutexLock)(PlatformSpecificMutex mtx) = NULL; diff --git a/src/Platforms/Iar/UtestPlatform.cpp b/src/Platforms/Iar/UtestPlatform.cpp index 2d6c4313c..97d1c46d8 100644 --- a/src/Platforms/Iar/UtestPlatform.cpp +++ b/src/Platforms/Iar/UtestPlatform.cpp @@ -157,11 +157,6 @@ void* PlatformSpecificMemset(void* mem, int c, size_t size) return memset(mem, c, size); } -int PlatformSpecificMemCmp(const void* s1, const void* s2, size_t size) -{ - return memcmp(s1, s2, size); -} - double PlatformSpecificFabs(double d) { return fabs(d); diff --git a/src/Platforms/Symbian/UtestPlatform.cpp b/src/Platforms/Symbian/UtestPlatform.cpp index 955d7b1a6..7f0f6601b 100644 --- a/src/Platforms/Symbian/UtestPlatform.cpp +++ b/src/Platforms/Symbian/UtestPlatform.cpp @@ -125,11 +125,6 @@ void* PlatformSpecificMemset(void* mem, int c, size_t size) return memset(mem, c, size); } -int PlatformSpecificMemCmp(const void* s1, const void* s2, size_t size) -{ - return memcmp(s1, s2, size); -} - PlatformSpecificFile PlatformSpecificFOpen(const char* filename, const char* flag) { return fopen(filename, flag); } diff --git a/src/Platforms/VisualCpp/UtestPlatform.cpp b/src/Platforms/VisualCpp/UtestPlatform.cpp index 3289bc6a4..bc78c7c3a 100644 --- a/src/Platforms/VisualCpp/UtestPlatform.cpp +++ b/src/Platforms/VisualCpp/UtestPlatform.cpp @@ -160,7 +160,6 @@ void* (*PlatformSpecificRealloc)(void* memory, size_t size) = realloc; void (*PlatformSpecificFree)(void* memory) = free; void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = memcpy; void* (*PlatformSpecificMemset)(void* mem, int c, size_t size) = memset; -int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = memcmp; double (*PlatformSpecificFabs)(double d) = fabs; extern "C" int (*PlatformSpecificIsNan)(double) = _isnan; diff --git a/src/Platforms/armcc/UtestPlatform.cpp b/src/Platforms/armcc/UtestPlatform.cpp index c940f8915..3568e6f4c 100644 --- a/src/Platforms/armcc/UtestPlatform.cpp +++ b/src/Platforms/armcc/UtestPlatform.cpp @@ -150,7 +150,6 @@ extern "C" void* (*PlatformSpecificRealloc) (void*, size_t) = realloc; extern "C" void (*PlatformSpecificFree)(void*) = free; extern "C" void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = memcpy; extern "C" void* (*PlatformSpecificMemset)(void*, int, size_t) = memset; -extern "C" int (*PlatformSpecificMemCmp)(const void*, const void*, size_t) = memcmp; static int IsNanImplementation(double d) { From e8e8a9fdbdea339f8bfa1d2531ce9c5c57f5207e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 16 Jun 2015 03:31:35 +0200 Subject: [PATCH 7/8] Fixed method nomenclature and a failing unit test --- include/CppUTestExt/MockNamedValue.h | 4 ++-- src/CppUTestExt/MockNamedValue.cpp | 12 ++++++------ tests/CppUTestExt/MockExpectedCallTest.cpp | 2 +- tests/CppUTestExt/MockSupportTest.cpp | 6 ++---- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/include/CppUTestExt/MockNamedValue.h b/include/CppUTestExt/MockNamedValue.h index a23b75238..7f8d58038 100644 --- a/include/CppUTestExt/MockNamedValue.h +++ b/include/CppUTestExt/MockNamedValue.h @@ -103,7 +103,7 @@ class MockNamedValue virtual const char* getStringValue() const; virtual void* getPointerValue() const; virtual const void* getConstPointerValue() const; - virtual const unsigned char* getMemBufferValue() const; + virtual const unsigned char* getMemoryBuffer() const; virtual const void* getObjectPointer() const; virtual size_t getSize() const; virtual MockNamedValueComparator* getComparator() const; @@ -121,7 +121,7 @@ class MockNamedValue const char* stringValue_; void* pointerValue_; const void* constPointerValue_; - const unsigned char* memBufferValue_; + const unsigned char* memoryBufferValue_; const void* objectPointerValue_; const void* outputPointerValue_; } value_; diff --git a/src/CppUTestExt/MockNamedValue.cpp b/src/CppUTestExt/MockNamedValue.cpp index 9d64b192e..d760ae31f 100644 --- a/src/CppUTestExt/MockNamedValue.cpp +++ b/src/CppUTestExt/MockNamedValue.cpp @@ -37,7 +37,7 @@ void MockNamedValue::setDefaultComparatorRepository(MockNamedValueComparatorRepo defaultRepository_ = repository; } -MockNamedValue::MockNamedValue(const SimpleString& name) : name_(name), type_("int"), comparator_(NULL) +MockNamedValue::MockNamedValue(const SimpleString& name) : name_(name), type_("int"), size_(0), comparator_(NULL) { value_.intValue_ = 0; } @@ -97,7 +97,7 @@ void MockNamedValue::setValue(const char* value) void MockNamedValue::setMemoryBuffer(const unsigned char* value, size_t size) { type_ = "const unsigned char*"; - value_.memBufferValue_ = value; + value_.memoryBufferValue_ = value; size_ = size; } @@ -198,10 +198,10 @@ const void* MockNamedValue::getConstPointerValue() const return value_.pointerValue_; } -const unsigned char* MockNamedValue::getMemBufferValue() const +const unsigned char* MockNamedValue::getMemoryBuffer() const { STRCMP_EQUAL("const unsigned char*", type_.asCharString()); - return value_.memBufferValue_; + return value_.memoryBufferValue_; } const void* MockNamedValue::getObjectPointer() const @@ -269,7 +269,7 @@ bool MockNamedValue::equals(const MockNamedValue& p) const if (size_ != p.size_) { return false; } - return SimpleString::MemCmp(value_.memBufferValue_, p.value_.memBufferValue_, size_) == 0; + return SimpleString::MemCmp(value_.memoryBufferValue_, p.value_.memoryBufferValue_, size_) == 0; } if (comparator_) @@ -297,7 +297,7 @@ SimpleString MockNamedValue::toString() const else if (type_ == "double") return StringFrom(value_.doubleValue_); else if (type_ == "const unsigned char*") - return StringFrom(value_.memBufferValue_, size_); + return StringFrom(value_.memoryBufferValue_, size_); if (comparator_) return comparator_->valueToString(value_.objectPointerValue_); diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index 6b14e6b84..df336f6a2 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -181,7 +181,7 @@ TEST(MockExpectedCall, callWithMemoryBuffer) const unsigned char mem_buffer[] = { 0x12, 0xFE, 0xA1 }; call->withParameter("memoryBuffer", mem_buffer, sizeof(mem_buffer)); STRCMP_EQUAL("const unsigned char*", call->getInputParameterType("memoryBuffer").asCharString()); - POINTERS_EQUAL( (void*) mem_buffer, (void*) call->getInputParameter("memoryBuffer").getMemBufferValue() ); + POINTERS_EQUAL( (void*) mem_buffer, (void*) call->getInputParameter("memoryBuffer").getMemoryBuffer() ); LONGS_EQUAL(sizeof(mem_buffer), call->getInputParameter("memoryBuffer").getSize()); } diff --git a/tests/CppUTestExt/MockSupportTest.cpp b/tests/CppUTestExt/MockSupportTest.cpp index b4b618c82..5d9cc4d04 100644 --- a/tests/CppUTestExt/MockSupportTest.cpp +++ b/tests/CppUTestExt/MockSupportTest.cpp @@ -452,8 +452,7 @@ TEST(MockSupportTest, expectOneMemBufferParameterAndValueFailsDueToContents) unsigned char memBuffer2[] = { 0x12, 0x05, 0xFF }; MockNamedValue parameter("parameter"); - parameter.setValue( memBuffer2 ); - parameter.setSize( sizeof(memBuffer2) ); + parameter.setMemoryBuffer( memBuffer2, sizeof(memBuffer2) ); addFunctionToExpectationsList("foo")->withParameter("parameter", memBuffer1, sizeof(memBuffer1)); MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, *expectationsList); @@ -469,8 +468,7 @@ TEST(MockSupportTest, expectOneMemBufferParameterAndValueFailsDueToSize) unsigned char memBuffer2[] = { 0x12, 0x15, 0xFF, 0x90 }; MockNamedValue parameter("parameter"); - parameter.setValue( memBuffer2 ); - parameter.setSize( sizeof(memBuffer2) ); + parameter.setMemoryBuffer( memBuffer2, sizeof(memBuffer2) ); addFunctionToExpectationsList("foo")->withParameter("parameter", memBuffer1, sizeof(memBuffer1)); MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, *expectationsList); From 07b8139b4c617f834e68bd77814741a10582ed28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Mon, 13 Jul 2015 20:14:31 +0200 Subject: [PATCH 8/8] Refactored code to use existing StringFromBinary function to generate hex dump of mocks memory buffers --- include/CppUTest/SimpleString.h | 3 +- src/CppUTest/SimpleString.cpp | 32 ++++++++++++---------- src/CppUTestExt/MockActualCall.cpp | 2 +- src/CppUTestExt/MockNamedValue.cpp | 2 +- tests/CppUTestExt/MockActualCallTest.cpp | 2 +- tests/CppUTestExt/MockExpectedCallTest.cpp | 2 +- tests/SimpleStringTest.cpp | 19 ++++++++----- 7 files changed, 35 insertions(+), 27 deletions(-) diff --git a/include/CppUTest/SimpleString.h b/include/CppUTest/SimpleString.h index 60e5eefea..f87138a0e 100644 --- a/include/CppUTest/SimpleString.h +++ b/include/CppUTest/SimpleString.h @@ -140,7 +140,6 @@ SimpleString StringFrom(int value); SimpleString StringFrom(unsigned int value); SimpleString StringFrom(long value); SimpleString StringFrom(unsigned long value); -SimpleString StringFrom(const unsigned char* value, size_t size); SimpleString HexStringFrom(long value); SimpleString HexStringFrom(unsigned long value); SimpleString HexStringFrom(const void* value); @@ -150,6 +149,8 @@ SimpleString StringFromFormat(const char* format, ...) __check_format__(printf, SimpleString VStringFromFormat(const char* format, va_list args); SimpleString StringFromBinary(const unsigned char* value, size_t size); SimpleString StringFromBinaryOrNull(const unsigned char* value, size_t size); +SimpleString StringFromBinaryWithSize(const unsigned char* value, size_t size); +SimpleString StringFromBinaryWithSizeOrNull(const unsigned char* value, size_t size); SimpleString StringFromMaskedBits(unsigned long value, unsigned long mask, size_t byteCount); #if CPPUTEST_USE_STD_CPP_LIB diff --git a/src/CppUTest/SimpleString.cpp b/src/CppUTest/SimpleString.cpp index ce2ef02b7..ea4a90e69 100644 --- a/src/CppUTest/SimpleString.cpp +++ b/src/CppUTest/SimpleString.cpp @@ -490,21 +490,6 @@ SimpleString HexStringFrom(const void* value) return StringFromFormat("%lx", convertPointerToLongValue(value)); } -SimpleString StringFrom(const unsigned char* value, size_t size) -{ - SimpleString str = StringFromFormat("Len = %lu | HexContents =", size); - size_t displayedSize = ((size > 128) ? 128 : size); - for (size_t i = 0; i < displayedSize; i++) - { - str += StringFromFormat(" %02X", value[i]); - } - if( size > displayedSize ) - { - str += " ..."; - } - return str; -} - SimpleString StringFrom(double value, int precision) { return StringFromFormat("%.*g", precision, value); @@ -601,6 +586,23 @@ SimpleString StringFromBinaryOrNull(const unsigned char* value, size_t size) return (value) ? StringFromBinary(value, size) : "(null)"; } +SimpleString StringFromBinaryWithSize(const unsigned char* value, size_t size) +{ + SimpleString result = StringFromFormat("Size = %lu | HexContents = ", size); + size_t displayedSize = ((size > 128) ? 128 : size); + result += StringFromBinaryOrNull(value, size); + if (size > displayedSize) + { + result += " ..."; + } + return result; +} + +SimpleString StringFromBinaryWithSizeOrNull(const unsigned char* value, size_t size) +{ + return (value) ? StringFromBinaryWithSize(value, size) : "(null)"; +} + SimpleString StringFromMaskedBits(unsigned long value, unsigned long mask, size_t byteCount) { SimpleString result; diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index f57fe5b70..4f79572a7 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -542,7 +542,7 @@ MockActualCall& MockActualCallTrace::withConstPointerParameter(const SimpleStrin MockActualCall& MockActualCallTrace::withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) { addParameterName(name); - traceBuffer_ += StringFrom(value, size); + traceBuffer_ += StringFromBinaryWithSizeOrNull(value, size); return *this; } diff --git a/src/CppUTestExt/MockNamedValue.cpp b/src/CppUTestExt/MockNamedValue.cpp index d760ae31f..2cfac2986 100644 --- a/src/CppUTestExt/MockNamedValue.cpp +++ b/src/CppUTestExt/MockNamedValue.cpp @@ -297,7 +297,7 @@ SimpleString MockNamedValue::toString() const else if (type_ == "double") return StringFrom(value_.doubleValue_); else if (type_ == "const unsigned char*") - return StringFrom(value_.memoryBufferValue_, size_); + return StringFromBinaryWithSizeOrNull(value_.memoryBufferValue_, size_); if (comparator_) return comparator_->valueToString(value_.objectPointerValue_); diff --git a/tests/CppUTestExt/MockActualCallTest.cpp b/tests/CppUTestExt/MockActualCallTest.cpp index 94164cc3f..19307452e 100644 --- a/tests/CppUTestExt/MockActualCallTest.cpp +++ b/tests/CppUTestExt/MockActualCallTest.cpp @@ -172,7 +172,7 @@ TEST(MockCheckedActualCall, remainderOfMockActualCallTraceWorksAsItShould) expectedString += HexStringFrom(&value); expectedString += " const_pointer:0x"; expectedString += HexStringFrom(&const_value); - expectedString += " mem_buffer:Len = 2 | HexContents = FE 15"; + expectedString += " mem_buffer:Size = 2 | HexContents = FE 15"; expectedString += " int named_type:0x"; expectedString += HexStringFrom(&const_value); STRCMP_EQUAL(expectedString.asCharString(), actual.getTraceOutput()); diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index df336f6a2..3fd6bccb1 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -469,7 +469,7 @@ TEST(MockExpectedCallComposite, hasMemoryBufferParameter) { const unsigned char mem_buffer[] = { 0x89, 0xFE, 0x15 }; composite.withParameter("param", mem_buffer, sizeof(mem_buffer)); - STRCMP_EQUAL("name -> const unsigned char* param: ", call.callToString().asCharString()); + STRCMP_EQUAL("name -> const unsigned char* param: ", call.callToString().asCharString()); } TEST(MockExpectedCallComposite, hasParameterOfType) diff --git a/tests/SimpleStringTest.cpp b/tests/SimpleStringTest.cpp index 1ccc863ae..e6491a83b 100644 --- a/tests/SimpleStringTest.cpp +++ b/tests/SimpleStringTest.cpp @@ -459,13 +459,6 @@ TEST(SimpleString, SmallDoubles) STRCMP_CONTAINS("1.2e", s.asCharString()); } -TEST(SimpleString, MemoryBuffers) -{ - const unsigned char mem_buffer[] = { 0x12, 0xFE, 0xA1 }; - SimpleString s( StringFrom( mem_buffer, sizeof(mem_buffer) ) ); - STRCMP_EQUAL("Len = 3 | HexContents = 12 FE A1", s.asCharString()); -} - TEST(SimpleString, Sizes) { size_t size = 10; @@ -812,6 +805,18 @@ TEST(SimpleString, Binary) STRCMP_EQUAL("(null)", StringFromBinaryOrNull(NULL, 0).asCharString()); } +TEST(SimpleString, BinaryWithSize) +{ + const unsigned char value[] = { 0x12, 0xFE, 0xA1 }; + const char expectedString[] = "Size = 3 | HexContents = 12 FE A1"; + + STRCMP_EQUAL(expectedString, StringFromBinaryWithSize(value, sizeof(value)).asCharString()); + STRCMP_EQUAL(expectedString, StringFromBinaryWithSizeOrNull(value, sizeof(value)).asCharString()); + STRCMP_EQUAL("Size = 0 | HexContents = ", StringFromBinaryWithSize(value, 0).asCharString()); + STRCMP_EQUAL("(null)", StringFromBinaryWithSizeOrNull(NULL, 0).asCharString()); +} + + TEST(SimpleString, MemCmp) { unsigned char smaller[] = { 0x00, 0x01, 0x2A, 0xFF };