]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
add unity_fixture to the unity library
authorHarlan Stenn <stenn@ntp.org>
Mon, 1 Jun 2015 10:42:57 +0000 (10:42 +0000)
committerHarlan Stenn <stenn@ntp.org>
Mon, 1 Jun 2015 10:42:57 +0000 (10:42 +0000)
bk: 556c3731emFeHsNy5YxCxx-RjsuVFw

sntp/unity/Makefile.am
sntp/unity/unity_fixture.c [new file with mode: 0644]
sntp/unity/unity_fixture.h [new file with mode: 0644]
sntp/unity/unity_fixture_internals.h [new file with mode: 0644]
sntp/unity/unity_fixture_malloc_overrides.h [new file with mode: 0644]

index a5c2a5452915076d2fa58da802e07be4796f8188..5dfada7c1c3e7b7980c0dbae0ebfa3fca1be97fe 100644 (file)
@@ -5,11 +5,15 @@ CLEANFILES =
 
 noinst_LIBRARIES = libunity.a
 
-libunity_a_SOURCES =           \
-       ../libpkgver/colcomp.c  \
-       unity.c                 \
-       unity.h                 \
-       unity_internals.h       \
+libunity_a_SOURCES =                           \
+       ../libpkgver/colcomp.c                  \
+       unity.c                                 \
+       unity.h                                 \
+       unity_internals.h                       \
+       unity_fixture.c                         \
+       unity_fixture.h                         \
+       unity_fixture_internals.h               \
+       unity_fixture_malloc_overrides.h        \
        $(NULL)
 
 include $(top_srcdir)/depsver.mf
diff --git a/sntp/unity/unity_fixture.c b/sntp/unity/unity_fixture.c
new file mode 100644 (file)
index 0000000..6bdbe5b
--- /dev/null
@@ -0,0 +1,398 @@
+//- Copyright (c) 2010 James Grenning and Contributed to Unity Project
+/* ==========================================
+    Unity Project - A Test Framework for C
+    Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
+    [Released under MIT License. Please refer to license.txt for details]
+========================================== */
+
+#include <string.h>
+#include <stdio.h>
+#include "unity_fixture.h"
+#include "unity_internals.h"
+
+UNITY_FIXTURE_T UnityFixture;
+
+//If you decide to use the function pointer approach.
+int (*outputChar)(int) = putchar;
+
+int verbose = 0;
+
+void setUp(void);
+void tearDown(void);
+void setUp(void)    { /*does nothing*/ }
+void tearDown(void) { /*does nothing*/ }
+
+static void announceTestRun(unsigned int runNumber)
+{
+    UnityPrint("Unity test run ");
+    UnityPrintNumber(runNumber+1);
+    UnityPrint(" of ");
+    UnityPrintNumber(UnityFixture.RepeatCount);
+    UNITY_OUTPUT_CHAR('\n');
+}
+
+int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
+{
+    int result = UnityGetCommandLineOptions(argc, argv);
+    unsigned int r;
+    if (result != 0)
+        return result;
+
+    for (r = 0; r < UnityFixture.RepeatCount; r++)
+    {
+        UnityBegin(argv[0]);
+        announceTestRun(r);
+        runAllTests();
+        UNITY_OUTPUT_CHAR('\n');
+        UnityEnd();
+    }
+
+    return UnityFailureCount();
+}
+
+static int selected(const char * filter, const char * name)
+{
+    if (filter == 0)
+        return 1;
+    return strstr(name, filter) ? 1 : 0;
+}
+
+static int testSelected(const char* test)
+{
+    return selected(UnityFixture.NameFilter, test);
+}
+
+static int groupSelected(const char* group)
+{
+    return selected(UnityFixture.GroupFilter, group);
+}
+
+static void runTestCase(void)
+{
+
+}
+
+void UnityTestRunner(unityfunction* setup,
+        unityfunction* testBody,
+        unityfunction* teardown,
+        const char * printableName,
+        const char * group,
+        const char * name,
+        const char * file, int line)
+{
+    if (testSelected(name) && groupSelected(group))
+    {
+        Unity.CurrentTestFailed = 0;
+        Unity.TestFile = file;
+        Unity.CurrentTestName = printableName;
+        Unity.CurrentTestLineNumber = line;
+        if (!UnityFixture.Verbose)
+            UNITY_OUTPUT_CHAR('.');
+        else
+            UnityPrint(printableName);
+
+        Unity.NumberOfTests++;
+        UnityMalloc_StartTest();
+        UnityPointer_Init();
+
+        runTestCase();
+        if (TEST_PROTECT())
+        {
+            setup();
+            testBody();
+        }
+        if (TEST_PROTECT())
+        {
+            teardown();
+        }
+        if (TEST_PROTECT())
+        {
+            UnityPointer_UndoAllSets();
+            if (!Unity.CurrentTestFailed)
+                UnityMalloc_EndTest();
+        }
+        UnityConcludeFixtureTest();
+    }
+}
+
+void UnityIgnoreTest(const char * printableName)
+{
+    Unity.NumberOfTests++;
+    Unity.CurrentTestIgnored = 1;
+    if (!UnityFixture.Verbose)
+        UNITY_OUTPUT_CHAR('!');
+    else
+        UnityPrint(printableName);
+    UnityConcludeFixtureTest();
+}
+
+
+//-------------------------------------------------
+//Malloc and free stuff
+//
+#define MALLOC_DONT_FAIL -1
+static int malloc_count;
+static int malloc_fail_countdown = MALLOC_DONT_FAIL;
+
+void UnityMalloc_StartTest(void)
+{
+    malloc_count = 0;
+    malloc_fail_countdown = MALLOC_DONT_FAIL;
+}
+
+void UnityMalloc_EndTest(void)
+{
+    malloc_fail_countdown = MALLOC_DONT_FAIL;
+    if (malloc_count != 0)
+    {
+        TEST_FAIL_MESSAGE("This test leaks!");
+    }
+}
+
+void UnityMalloc_MakeMallocFailAfterCount(int countdown)
+{
+    malloc_fail_countdown = countdown;
+}
+
+#ifdef malloc
+#undef malloc
+#endif
+
+#ifdef free
+#undef free
+#endif
+
+#ifdef calloc
+#undef calloc
+#endif
+
+#ifdef realloc
+#undef realloc
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct GuardBytes
+{
+    size_t size;
+    char guard[sizeof(size_t)];
+} Guard;
+
+
+static const char * end = "END";
+
+void * unity_malloc(size_t size)
+{
+    char* mem;
+    Guard* guard;
+
+    if (malloc_fail_countdown != MALLOC_DONT_FAIL)
+    {
+        if (malloc_fail_countdown == 0)
+            return 0;
+        malloc_fail_countdown--;
+    }
+
+    malloc_count++;
+
+    guard = (Guard*)malloc(size + sizeof(Guard) + 4);
+    guard->size = size;
+    mem = (char*)&(guard[1]);
+    memcpy(&mem[size], end, strlen(end) + 1);
+
+    return (void*)mem;
+}
+
+static int isOverrun(void * mem)
+{
+    Guard* guard = (Guard*)mem;
+    char* memAsChar = (char*)mem;
+    guard--;
+
+    return strcmp(&memAsChar[guard->size], end) != 0;
+}
+
+static void release_memory(void * mem)
+{
+    Guard* guard = (Guard*)mem;
+    guard--;
+
+    malloc_count--;
+    free(guard);
+}
+
+void unity_free(void * mem)
+{
+    int overrun = isOverrun(mem);//strcmp(&memAsChar[guard->size], end) != 0;
+    release_memory(mem);
+    if (overrun)
+    {
+        TEST_FAIL_MESSAGE("Buffer overrun detected during free()");
+    }
+}
+
+void* unity_calloc(size_t num, size_t size)
+{
+    void* mem = unity_malloc(num * size);
+    memset(mem, 0, num*size);
+    return mem;
+}
+
+void* unity_realloc(void * oldMem, size_t size)
+{
+    Guard* guard = (Guard*)oldMem;
+//    char* memAsChar = (char*)oldMem;
+    void* newMem;
+
+    if (oldMem == 0)
+        return unity_malloc(size);
+
+    guard--;
+    if (isOverrun(oldMem))
+    {
+        release_memory(oldMem);
+        TEST_FAIL_MESSAGE("Buffer overrun detected during realloc()");
+    }
+
+    if (size == 0)
+    {
+        release_memory(oldMem);
+        return 0;
+    }
+
+    if (guard->size >= size)
+        return oldMem;
+
+    newMem = unity_malloc(size);
+    memcpy(newMem, oldMem, guard->size);
+    unity_free(oldMem);
+    return newMem;
+}
+
+
+//--------------------------------------------------------
+//Automatic pointer restoration functions
+typedef struct _PointerPair
+{
+    struct _PointerPair * next;
+    void ** pointer;
+    void * old_value;
+} PointerPair;
+
+enum {MAX_POINTERS=50};
+static PointerPair pointer_store[MAX_POINTERS];
+static int pointer_index = 0;
+
+void UnityPointer_Init(void)
+{
+    pointer_index = 0;
+}
+
+void UnityPointer_Set(void ** pointer, void * newValue)
+{
+    if (pointer_index >= MAX_POINTERS)
+        TEST_FAIL_MESSAGE("Too many pointers set");
+
+    pointer_store[pointer_index].pointer = pointer;
+    pointer_store[pointer_index].old_value = *pointer;
+    *pointer = newValue;
+    pointer_index++;
+}
+
+void UnityPointer_UndoAllSets(void)
+{
+    while (pointer_index > 0)
+    {
+        pointer_index--;
+        *(pointer_store[pointer_index].pointer) =
+        pointer_store[pointer_index].old_value;
+
+    }
+}
+
+int UnityFailureCount(void)
+{
+    return Unity.TestFailures;
+}
+
+int UnityGetCommandLineOptions(int argc, const char* argv[])
+{
+    int i;
+    UnityFixture.Verbose = 0;
+    UnityFixture.GroupFilter = 0;
+    UnityFixture.NameFilter = 0;
+    UnityFixture.RepeatCount = 1;
+
+    if (argc == 1)
+        return 0;
+
+    for (i = 1; i < argc; )
+    {
+        if (strcmp(argv[i], "-v") == 0)
+        {
+            UnityFixture.Verbose = 1;
+            i++;
+        }
+        else if (strcmp(argv[i], "-g") == 0)
+        {
+            i++;
+            if (i >= argc)
+                return 1;
+            UnityFixture.GroupFilter = argv[i];
+            i++;
+        }
+        else if (strcmp(argv[i], "-n") == 0)
+        {
+            i++;
+            if (i >= argc)
+                return 1;
+            UnityFixture.NameFilter = argv[i];
+            i++;
+        }
+        else if (strcmp(argv[i], "-r") == 0)
+        {
+            UnityFixture.RepeatCount = 2;
+            i++;
+            if (i < argc)
+            {
+                if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
+                {
+                    UnityFixture.RepeatCount = atoi(argv[i]);
+                    i++;
+                }
+            }
+        } else {
+            // ignore unknown parameter
+            i++;
+        }
+    }
+    return 0;
+}
+
+void UnityConcludeFixtureTest(void)
+{
+    if (Unity.CurrentTestIgnored)
+    {
+        if (UnityFixture.Verbose)
+        {
+            UNITY_OUTPUT_CHAR('\n');
+        }
+        Unity.TestIgnores++;
+    }
+    else if (!Unity.CurrentTestFailed)
+    {
+        if (UnityFixture.Verbose)
+        {
+            UnityPrint(" PASS");
+            UNITY_OUTPUT_CHAR('\n');
+        }
+    }
+    else if (Unity.CurrentTestFailed)
+    {
+        Unity.TestFailures++;
+    }
+
+    Unity.CurrentTestFailed = 0;
+    Unity.CurrentTestIgnored = 0;
+}
diff --git a/sntp/unity/unity_fixture.h b/sntp/unity/unity_fixture.h
new file mode 100644 (file)
index 0000000..e176bbd
--- /dev/null
@@ -0,0 +1,86 @@
+//- Copyright (c) 2010 James Grenning and Contributed to Unity Project
+/* ==========================================
+    Unity Project - A Test Framework for C
+    Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
+    [Released under MIT License. Please refer to license.txt for details]
+========================================== */
+
+#ifndef UNITY_FIXTURE_H_
+#define UNITY_FIXTURE_H_
+
+#include "unity.h"
+#include "unity_internals.h"
+#include "unity_fixture_malloc_overrides.h"
+#include "unity_fixture_internals.h"
+
+int UnityMain(int argc, const char* argv[], void (*runAllTests)(void));
+
+
+#define TEST_GROUP(group)\
+    static const char* TEST_GROUP_##group = #group
+
+#define TEST_SETUP(group) void TEST_##group##_SETUP(void);\
+    void TEST_##group##_SETUP(void)
+
+#define TEST_TEAR_DOWN(group) void TEST_##group##_TEAR_DOWN(void);\
+    void TEST_##group##_TEAR_DOWN(void)
+
+
+#define TEST(group, name) \
+    void TEST_##group##_##name##_(void);\
+    void TEST_##group##_##name##_run(void);\
+    void TEST_##group##_##name##_run(void)\
+    {\
+        UnityTestRunner(TEST_##group##_SETUP,\
+            TEST_##group##_##name##_,\
+            TEST_##group##_TEAR_DOWN,\
+            "TEST(" #group ", " #name ")",\
+            TEST_GROUP_##group, #name,\
+            __FILE__, __LINE__);\
+    }\
+    void  TEST_##group##_##name##_(void)
+
+#define IGNORE_TEST(group, name) \
+    void TEST_##group##_##name##_(void);\
+    void TEST_##group##_##name##_run(void);\
+    void TEST_##group##_##name##_run(void)\
+    {\
+        UnityIgnoreTest("IGNORE_TEST(" #group ", " #name ")");\
+    }\
+    void TEST_##group##_##name##_(void)
+
+#define DECLARE_TEST_CASE(group, name) \
+    void TEST_##group##_##name##_run(void)
+
+#define RUN_TEST_CASE(group, name) \
+    { DECLARE_TEST_CASE(group, name);\
+      TEST_##group##_##name##_run(); }
+
+//This goes at the bottom of each test file or in a separate c file
+#define TEST_GROUP_RUNNER(group)\
+    void TEST_##group##_GROUP_RUNNER_runAll(void);\
+    void TEST_##group##_GROUP_RUNNER(void);\
+    void TEST_##group##_GROUP_RUNNER(void)\
+    {\
+        TEST_##group##_GROUP_RUNNER_runAll();\
+    }\
+    void TEST_##group##_GROUP_RUNNER_runAll(void)
+
+//Call this from main
+#define RUN_TEST_GROUP(group)\
+    { void TEST_##group##_GROUP_RUNNER(void);\
+      TEST_##group##_GROUP_RUNNER(); }
+
+//CppUTest Compatibility Macros
+#define UT_PTR_SET(ptr, newPointerValue)               UnityPointer_Set((void**)&ptr, (void*)newPointerValue)
+#define TEST_ASSERT_POINTERS_EQUAL(expected, actual)   TEST_ASSERT_EQUAL_PTR(expected, actual)
+#define TEST_ASSERT_BYTES_EQUAL(expected, actual)      TEST_ASSERT_EQUAL_HEX8(0xff & (expected), 0xff & (actual))
+#define FAIL(message)                                  TEST_FAIL((message))
+#define CHECK(condition)                               TEST_ASSERT_TRUE((condition))
+#define LONGS_EQUAL(expected, actual)                  TEST_ASSERT_EQUAL_INT((expected), (actual))
+#define STRCMP_EQUAL(expected, actual)                 TEST_ASSERT_EQUAL_STRING((expected), (actual))
+#define DOUBLES_EQUAL(expected, actual, delta)         TEST_ASSERT_FLOAT_WITHIN(((expected), (actual), (delta))
+
+void UnityMalloc_MakeMallocFailAfterCount(int count);
+
+#endif /* UNITY_FIXTURE_H_ */
diff --git a/sntp/unity/unity_fixture_internals.h b/sntp/unity/unity_fixture_internals.h
new file mode 100644 (file)
index 0000000..c4099fa
--- /dev/null
@@ -0,0 +1,44 @@
+//- Copyright (c) 2010 James Grenning and Contributed to Unity Project
+/* ==========================================
+    Unity Project - A Test Framework for C
+    Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
+    [Released under MIT License. Please refer to license.txt for details]
+========================================== */
+
+#ifndef UNITY_FIXTURE_INTERNALS_H_
+#define UNITY_FIXTURE_INTERNALS_H_
+
+typedef struct _UNITY_FIXTURE_T
+{
+    int Verbose;
+    unsigned int RepeatCount;
+    const char* NameFilter;
+    const char* GroupFilter;
+} UNITY_FIXTURE_T;
+
+typedef void unityfunction(void);
+void UnityTestRunner(unityfunction * setup,
+        unityfunction * body,
+        unityfunction * teardown,
+        const char * printableName,
+        const char * group,
+        const char * name,
+        const char * file, int line);
+
+void UnityIgnoreTest(const char * printableName);
+void UnityMalloc_StartTest(void);
+void UnityMalloc_EndTest(void);
+int UnityFailureCount(void);
+int UnityGetCommandLineOptions(int argc, const char* argv[]);
+void UnityConcludeFixtureTest(void);
+
+void UnityPointer_Set(void ** ptr, void * newValue);
+void UnityPointer_UndoAllSets(void);
+void UnityPointer_Init(void);
+
+void UnityAssertEqualPointer(const void * expected,
+                            const void * actual,
+                            const char* msg,
+                            const UNITY_LINE_TYPE lineNumber);
+
+#endif /* UNITY_FIXTURE_INTERNALS_H_ */
diff --git a/sntp/unity/unity_fixture_malloc_overrides.h b/sntp/unity/unity_fixture_malloc_overrides.h
new file mode 100644 (file)
index 0000000..1e10014
--- /dev/null
@@ -0,0 +1,21 @@
+//- Copyright (c) 2010 James Grenning and Contributed to Unity Project
+/* ==========================================
+    Unity Project - A Test Framework for C
+    Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
+    [Released under MIT License. Please refer to license.txt for details]
+========================================== */
+
+#ifndef UNITY_FIXTURE_MALLOC_OVERRIDES_H_
+#define UNITY_FIXTURE_MALLOC_OVERRIDES_H_
+
+#define malloc  unity_malloc
+#define calloc  unity_calloc
+#define realloc unity_realloc
+#define free    unity_free
+
+void* unity_malloc(size_t size);
+void* unity_calloc(size_t num, size_t size);
+void* unity_realloc(void * oldMem, size_t size);
+void unity_free(void * mem);
+
+#endif /* UNITY_FIXTURE_MALLOC_OVERRIDES_H_ */