/* 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 */ #define _GNU_SOURCE #include #include #include #include #include #include #include #if defined(WIN32) #include #else #include #endif #include "unittest/TestSuite.h" extern const char * UnitTest_suiteNameList[1]; void (* g_unitTestFailureCallback)(const char * file, const char * function, int line, const char * format, ...) __attribute__((__noreturn__)) __attribute__((format(printf, 4, 5))); unsigned int g_unitTestSuccessfulAssertCount = 0; static unsigned int failureCount = 0; static bool suiteFailed; static jmp_buf jmpEnv; static int lengthOfSharedPrefix(const char * string1, const char * string2) { int charIndex; for (charIndex = 0; string1[charIndex] != '\x00' && string2[charIndex] != '\x00'; charIndex++) { if (string1[charIndex] != string2[charIndex]) { break; } } return charIndex; } static void assertFailureCallback(const char * file, const char * function, int line, const char * format, ...) __attribute__((__noreturn__)) __attribute__((format(printf, 4, 5))); static void assertFailureCallback(const char * file, const char * function, int line, const char * format, ...) { va_list args; static char * sourceRoot; if (!suiteFailed) { suiteFailed = true; printf(" Failed:\n"); } if (sourceRoot == NULL) { sourceRoot = strdup(__FILE__); char * substring = strstr(sourceRoot, "unittest/unittest_main.c"); if (substring != NULL) { *substring = 0; } } file += lengthOfSharedPrefix(sourceRoot, file); printf(" FAILURE in %s (%s:%d):\n ", function, file, line); va_start(args, format); vprintf(format, args); va_end(args); putchar('\n'); failureCount++; longjmp(jmpEnv, 1); } int main(int argc, char ** argv) { #if defined(WIN32) HMODULE moduleHandle = GetModuleHandle(NULL); #endif if (argc > 1) { chdir(argv[1]); } g_unitTestFailureCallback = assertFailureCallback; unsigned int suiteCount; for (suiteCount = 0; UnitTest_suiteNameList[suiteCount] != NULL; suiteCount++); putchar('\n'); unsigned int successfulAssertsTotal = 0, testCaseCountTotal = 0; for (unsigned int suiteIndex = 0; suiteIndex < suiteCount; suiteIndex++) { suiteFailed = false; char suiteFunctionName[256]; snprintf(suiteFunctionName, 256, "%s_suite", UnitTest_suiteNameList[suiteIndex]); struct TestSuite * (* suiteFunction)(); #if defined(WIN32) suiteFunction = (struct TestSuite * (*)()) GetProcAddress(moduleHandle, suiteFunctionName); #else suiteFunction = (struct TestSuite * (*)()) dlsym(RTLD_DEFAULT, suiteFunctionName); #endif if (suiteFunction == NULL) { fprintf(stderr, "Couldn't load test suite %s (no symbol named %s found)\n", UnitTest_suiteNameList[suiteIndex], suiteFunctionName); abort(); } struct TestSuite * testSuite = suiteFunction(); printf("%s (%d/%d) running %d test%s...", testSuite->description, suiteIndex + 1, suiteCount, testSuite->testCaseCount, testSuite->testCaseCount == 1 ? "" : "s"); g_unitTestSuccessfulAssertCount = 0; unsigned int successfulTestCaseCount = 0; for (unsigned int testCaseIndex = 0; testCaseIndex < testSuite->testCaseCount; testCaseIndex++) { if (setjmp(jmpEnv) != 0) { continue; } testSuite->testCases[testCaseIndex](); successfulTestCaseCount++; } successfulAssertsTotal += g_unitTestSuccessfulAssertCount; testCaseCountTotal += testSuite->testCaseCount; if (suiteFailed) { printf(" %u test case%s passed, %u failed (%u successful asserts total)\n", successfulTestCaseCount, successfulTestCaseCount == 1 ? "" : "s", testSuite->testCaseCount - successfulTestCaseCount, g_unitTestSuccessfulAssertCount); } else { printf(" %u test case%s succeeded with %u asserts\n", testSuite->testCaseCount, testSuite->testCaseCount == 1 ? "" : "s", g_unitTestSuccessfulAssertCount); } } printf("\nTests completed (%u successful assert%s; %u test case%s run, %u failure%s)\n\n", successfulAssertsTotal, successfulAssertsTotal == 1 ? "" : "s", testCaseCountTotal, testCaseCountTotal == 1 ? "" : "s", failureCount, failureCount == 1 ? "" : "s"); return failureCount > 0 ? EXIT_FAILURE : EXIT_SUCCESS; }