/* Copyright (c) 2013 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener adiener@sacredsoftware.net */ #ifndef __TestSuite_H__ #define __TestSuite_H__ #ifdef __cplusplus extern "C" { #endif #include #include #include #include #include typedef struct TestSuite TestSuite; extern void (* g_unitTestFailureCallback)(const char * file, const char * function, int line, const char * format, ...) __attribute__((__noreturn__)) __attribute__((format(printf, 4, 5))); extern unsigned int g_unitTestSuccessfulAssertCount; struct TestSuite { char * description; unsigned int testCaseCount; void (** testCases)(); }; #define TestCase_assert(condition, ...) \ if (!(condition)) { \ (*g_unitTestFailureCallback)(__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__); \ } else { \ g_unitTestSuccessfulAssertCount++; \ } #ifndef __printfFormats_H__ #ifdef WIN32 #define SIZE_T_FORMAT "%Iu" #define INT64_FORMAT "%I64d" #define UINT64_FORMAT "%I64u" #else #define SIZE_T_FORMAT "%zu" #if defined(linux) && defined(_LP64) #define INT64_FORMAT "%ld" #define UINT64_FORMAT "%lu" #else #define INT64_FORMAT "%lld" #define UINT64_FORMAT "%llu" #endif #endif #define FIXED_16_16_FORMAT "0x%05X" #endif #define TestCase_assertTypedValueEqual(value, expectedValue, type, format) { \ type actualValue = value; \ TestCase_assert(actualValue == (type) (expectedValue), "Expected " format " but got " format, (type) (expectedValue), (type) (actualValue)); \ } #define TestCase_assertBoolFalse(value) TestCase_assert(!value, "Expected false but got true") #define TestCase_assertBoolTrue(value) TestCase_assert(value, "Expected true but got false") #define TestCase_assertBoolEqual(value, expectedValue) TestCase_assert((bool) (value) == (bool) (expectedValue), "Expected %s but got %s", (expectedValue) ? "true" : "false", (value) ? "true" : "false") #define TestCase_assertIntEqual(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, int, "%d") #define TestCase_assertInt64Equal(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, int64_t, INT64_FORMAT) #define TestCase_assertUIntEqual(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, unsigned int, "%u") #define TestCase_assertUInt64Equal(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, uint64_t, UINT64_FORMAT) #define TestCase_assertEnumEqual(value, expectedValue) TestCase_assert((int) (value) == (int) (expectedValue), "Expected " #expectedValue " (%d) but got %d", (int) (expectedValue), (int) (value)) #define TestCase_assertPointerNULL(value) { \ const void * actualValue = value; \ TestCase_assert(actualValue == NULL, "Expected NULL but got %p", actualValue); \ } #define TestCase_assertPointerNonNULL(value) TestCase_assert(value != NULL, "Expected non-NULL but got NULL") #define TestCase_assertPointerEqual(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, const void *, "%p") #define TestCase_assertPointerUnequal(value, expectedValue) TestCase_assert((const void *) (value) != (const void *) (expectedValue), "Expected pointers to differ, but they were the same (%p)", expectedValue) #define TestCase_assertStringEqual(value, expectedValue) TestCase_assert(!strcmp((const char *) (value), (const char *) (expectedValue)), "Expected \"%s\" but got \"%s\"", (const char *) (expectedValue), (const char *) (value)) #define TestCase_assertStringPointerEqual(value, expectedValue) TestCase_assert((const char *) (value) == (const char *) (expectedValue), "Expected \"%s\" (%p) but got \"%s\" (%p)", (const char *) (expectedValue), (const char *) (expectedValue), (const char *) (value), (const char *) (value)) #define TestCase_assertStringPointerNULL(value) TestCase_assert((value) == NULL, "Expected NULL but got \"%s\" (%p)", (value), (const char *) (value)) #define TestCase_assertFixed16_16Equal(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, fixed16_16, FIXED_16_16_FORMAT) #define TestCase_assertSizeEqual(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, size_t, SIZE_T_FORMAT) #define TestCase_assertFloatEqual(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, float, "%f") #define TestCase_assertDoubleEqual(value, expectedValue) TestCase_assertTypedValueEqual(value, expectedValue, double, "%f") #define TestCase_assertFloatApproximate(value, expectedValue, tolerance) { \ float actualValue = value; \ TestCase_assert(fabsf(actualValue - (expectedValue)) < (tolerance), "Expected ~%f but got %f (tolerance %f)", (expectedValue), actualValue, (tolerance)); \ } #define TestCase_assertDoubleApproximate(value, expectedValue, tolerance) { \ double actualValue = value; \ TestCase_assert(fabs(actualValue - (expectedValue)) < (tolerance), "Expected ~%f but got %f (tolerance %f)", (expectedValue), actualValue, (tolerance)); \ } #define TestCase_assertBytesEqual(value, expectedValue, length) \ if (memcmp((value), (expectedValue), (length))) { \ char expectedByteString[(length) * 3], actualByteString[(length) * 3]; \ TestCase_formatByteString((const uint8_t *) (expectedValue), (length), expectedByteString); \ TestCase_formatByteString((const uint8_t *) (value), (length), actualByteString); \ (*g_unitTestFailureCallback)(__FILE__, __FUNCTION__, __LINE__, "Nonmatching bytes. Expected:\n %s\n Actual:\n %s", expectedByteString, actualByteString); \ } else { \ g_unitTestSuccessfulAssertCount++; \ } static inline void TestCase_formatByteString(const uint8_t * bytes, size_t length, char * outString) { for (size_t byteIndex = 0; byteIndex < length; byteIndex++) { char nibble = '0' + (bytes[byteIndex] >> 4); if (nibble > '9') { nibble -= '9' - 'A' + 1; } outString[byteIndex * 3 + 0] = nibble; nibble = '0' + (bytes[byteIndex] & 0xF); if (nibble > '9') { nibble -= '9' - 'A' + 1; } outString[byteIndex * 3 + 1] = nibble; outString[byteIndex * 3 + 2] = ' '; } outString[length * 3 - 1] = 0; } #ifdef WIN32 #define DLLEXPORT __declspec(dllexport) #elif defined(__linux) #define DLLEXPORT __attribute__((visibility("default"))) #else #define DLLEXPORT #endif #define TEST_SUITE(name, ...) \ DLLEXPORT TestSuite * name##_suite() { \ return testSuite(#name, __VA_ARGS__, NULL); \ } static inline TestSuite * testSuite(const char * description, ...) __attribute__((sentinel)); static inline TestSuite * testSuite(const char * description, ...) { TestSuite * suite; va_list args; unsigned int testCaseIndex; suite = malloc(sizeof(*suite)); va_start(args, description); for (suite->testCaseCount = 0; va_arg(args, void (*)()) != NULL; suite->testCaseCount++); va_end(args); suite->description = malloc(strlen(description) + 1); strcpy(suite->description, description); suite->testCases = malloc(sizeof(*suite->testCases) * suite->testCaseCount); va_start(args, description); for (testCaseIndex = 0; testCaseIndex < suite->testCaseCount; testCaseIndex++) { suite->testCases[testCaseIndex] = va_arg(args, void (*)()); } va_end(args); return suite; } #ifdef __cplusplus } #endif #endif