summaryrefslogtreecommitdiff
path: root/lab_6/unity/extras/fixture/test
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-02-26 10:24:00 -0800
committerFuwn <[email protected]>2026-02-26 10:31:15 -0800
commita14f361a60f55299e0057c419cc59e5194a77854 (patch)
treeedf54089f5849ee4dfdac48bd04bd8c271cfb62a /lab_6/unity/extras/fixture/test
parentfeat(homework_2): Add implementation (diff)
downloadcst456-a14f361a60f55299e0057c419cc59e5194a77854.tar.xz
cst456-a14f361a60f55299e0057c419cc59e5194a77854.zip
feat(lab_6): Add initial files
Diffstat (limited to 'lab_6/unity/extras/fixture/test')
-rw-r--r--lab_6/unity/extras/fixture/test/main/AllTests.c21
-rw-r--r--lab_6/unity/extras/fixture/test/testunity_fixture.c39
-rw-r--r--lab_6/unity/extras/fixture/test/unity_fixture_Test.c341
-rw-r--r--lab_6/unity/extras/fixture/test/unity_fixture_TestRunner.c41
-rw-r--r--lab_6/unity/extras/fixture/test/unity_output_Spy.c56
-rw-r--r--lab_6/unity/extras/fixture/test/unity_output_Spy.h17
6 files changed, 515 insertions, 0 deletions
diff --git a/lab_6/unity/extras/fixture/test/main/AllTests.c b/lab_6/unity/extras/fixture/test/main/AllTests.c
new file mode 100644
index 0000000..7d0577e
--- /dev/null
+++ b/lab_6/unity/extras/fixture/test/main/AllTests.c
@@ -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]
+========================================== */
+
+#include "unity_fixture.h"
+
+static void runAllTests(void)
+{
+ RUN_TEST_GROUP(UnityFixture);
+ RUN_TEST_GROUP(UnityCommandOptions);
+ RUN_TEST_GROUP(LeakDetection)
+}
+
+int main(int argc, const char* argv[])
+{
+ return UnityMain(argc, argv, runAllTests);
+}
+
diff --git a/lab_6/unity/extras/fixture/test/testunity_fixture.c b/lab_6/unity/extras/fixture/test/testunity_fixture.c
new file mode 100644
index 0000000..de0c04c
--- /dev/null
+++ b/lab_6/unity/extras/fixture/test/testunity_fixture.c
@@ -0,0 +1,39 @@
+//- 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 "unity_fixture.h"
+
+static int data = -1;
+
+TEST_GROUP(mygroup);
+
+TEST_SETUP(mygroup)
+{
+ data = 0;
+}
+
+TEST_TEAR_DOWN(mygroup)
+{
+ data = -1;
+}
+
+TEST(mygroup, test1)
+{
+ TEST_ASSERT_EQUAL_INT(0, data);
+}
+
+TEST(mygroup, test2)
+{
+ TEST_ASSERT_EQUAL_INT(0, data);
+ data = 5;
+}
+
+TEST(mygroup, test3)
+{
+ data = 7;
+ TEST_ASSERT_EQUAL_INT(7, data);
+}
diff --git a/lab_6/unity/extras/fixture/test/unity_fixture_Test.c b/lab_6/unity/extras/fixture/test/unity_fixture_Test.c
new file mode 100644
index 0000000..1f209e9
--- /dev/null
+++ b/lab_6/unity/extras/fixture/test/unity_fixture_Test.c
@@ -0,0 +1,341 @@
+//- 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 "unity_fixture.h"
+#include "unity_output_Spy.h"
+#include <stdlib.h>
+#include <string.h>
+
+extern UNITY_FIXTURE_T UnityFixture;
+
+TEST_GROUP(UnityFixture);
+
+TEST_SETUP(UnityFixture)
+{
+}
+
+TEST_TEAR_DOWN(UnityFixture)
+{
+}
+
+int my_int;
+int* pointer1 = 0;
+int* pointer2 = (int*)2;
+int* pointer3 = (int*)3;
+int int1;
+int int2;
+int int3;
+int int4;
+
+TEST(UnityFixture, PointerSetting)
+{
+ TEST_ASSERT_POINTERS_EQUAL(pointer1, 0);
+ UT_PTR_SET(pointer1, &int1);
+ UT_PTR_SET(pointer2, &int2);
+ UT_PTR_SET(pointer3, &int3);
+ TEST_ASSERT_POINTERS_EQUAL(pointer1, &int1);
+ TEST_ASSERT_POINTERS_EQUAL(pointer2, &int2);
+ TEST_ASSERT_POINTERS_EQUAL(pointer3, &int3);
+ UT_PTR_SET(pointer1, &int4);
+ UnityPointer_UndoAllSets();
+ TEST_ASSERT_POINTERS_EQUAL(pointer1, 0);
+ TEST_ASSERT_POINTERS_EQUAL(pointer2, (int*)2);
+ TEST_ASSERT_POINTERS_EQUAL(pointer3, (int*)3);
+}
+
+TEST(UnityFixture, ForceMallocFail)
+{
+ void* m;
+ void* mfails;
+ UnityMalloc_MakeMallocFailAfterCount(1);
+ m = malloc(10);
+ CHECK(m);
+ mfails = malloc(10);
+ TEST_ASSERT_POINTERS_EQUAL(0, mfails);
+ free(m);
+}
+
+TEST(UnityFixture, ReallocSmallerIsUnchanged)
+{
+ void* m1 = malloc(10);
+ void* m2 = realloc(m1, 5);
+ TEST_ASSERT_POINTERS_EQUAL(m1, m2);
+ free(m2);
+}
+
+TEST(UnityFixture, ReallocSameIsUnchanged)
+{
+ void* m1 = malloc(10);
+ void* m2 = realloc(m1, 10);
+ TEST_ASSERT_POINTERS_EQUAL(m1, m2);
+ free(m2);
+}
+
+TEST(UnityFixture, ReallocLargerNeeded)
+{
+ void* m1 = malloc(10);
+ void* m2;
+ strcpy((char*)m1, "123456789");
+ m2 = realloc(m1, 15);
+ CHECK(m1 != m2);
+ STRCMP_EQUAL("123456789", m2);
+ free(m2);
+}
+
+TEST(UnityFixture, ReallocNullPointerIsLikeMalloc)
+{
+ void* m = realloc(0, 15);
+ CHECK(m != 0);
+ free(m);
+}
+
+TEST(UnityFixture, ReallocSizeZeroFreesMemAndReturnsNullPointer)
+{
+ void* m1 = malloc(10);
+ void* m2 = realloc(m1, 0);
+ TEST_ASSERT_POINTERS_EQUAL(0, m2);
+}
+
+TEST(UnityFixture, CallocFillsWithZero)
+{
+ void* m = calloc(3, sizeof(char));
+ char* s = (char*)m;
+ TEST_ASSERT_BYTES_EQUAL(0, s[0]);
+ TEST_ASSERT_BYTES_EQUAL(0, s[1]);
+ TEST_ASSERT_BYTES_EQUAL(0, s[2]);
+ free(m);
+}
+
+char *p1;
+char *p2;
+
+TEST(UnityFixture, PointerSet)
+{
+ char c1;
+ char c2;
+ char newC1;
+ char newC2;
+ p1 = &c1;
+ p2 = &c2;
+
+ UnityPointer_Init();
+ UT_PTR_SET(p1, &newC1);
+ UT_PTR_SET(p2, &newC2);
+ TEST_ASSERT_POINTERS_EQUAL(&newC1, p1);
+ TEST_ASSERT_POINTERS_EQUAL(&newC2, p2);
+ UnityPointer_UndoAllSets();
+ TEST_ASSERT_POINTERS_EQUAL(&c1, p1);
+ TEST_ASSERT_POINTERS_EQUAL(&c2, p2);
+}
+
+//------------------------------------------------------------
+
+TEST_GROUP(UnityCommandOptions);
+
+int savedVerbose;
+int savedRepeat;
+const char* savedName;
+const char* savedGroup;
+
+TEST_SETUP(UnityCommandOptions)
+{
+ savedVerbose = UnityFixture.Verbose;
+ savedRepeat = UnityFixture.RepeatCount;
+ savedName = UnityFixture.NameFilter;
+ savedGroup = UnityFixture.GroupFilter;
+}
+
+TEST_TEAR_DOWN(UnityCommandOptions)
+{
+ UnityFixture.Verbose = savedVerbose;
+ UnityFixture.RepeatCount= savedRepeat;
+ UnityFixture.NameFilter = savedName;
+ UnityFixture.GroupFilter = savedGroup;
+}
+
+
+static const char* noOptions[] = {
+ "testrunner.exe"
+};
+
+TEST(UnityCommandOptions, DefaultOptions)
+{
+ UnityGetCommandLineOptions(1, noOptions);
+ TEST_ASSERT_EQUAL(0, UnityFixture.Verbose);
+ TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.GroupFilter);
+ TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.NameFilter);
+ TEST_ASSERT_EQUAL(1, UnityFixture.RepeatCount);
+}
+
+static const char* verbose[] = {
+ "testrunner.exe",
+ "-v"
+};
+
+TEST(UnityCommandOptions, OptionVerbose)
+{
+ TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, verbose));
+ TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
+}
+
+static const char* group[] = {
+ "testrunner.exe",
+ "-g", "groupname"
+};
+
+TEST(UnityCommandOptions, OptionSelectTestByGroup)
+{
+ TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, group));
+ STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
+}
+
+static const char* name[] = {
+ "testrunner.exe",
+ "-n", "testname"
+};
+
+TEST(UnityCommandOptions, OptionSelectTestByName)
+{
+ TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, name));
+ STRCMP_EQUAL("testname", UnityFixture.NameFilter);
+}
+
+static const char* repeat[] = {
+ "testrunner.exe",
+ "-r", "99"
+};
+
+TEST(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount)
+{
+ TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, repeat));
+ TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
+}
+
+TEST(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount)
+{
+ TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, repeat));
+ TEST_ASSERT_EQUAL(99, UnityFixture.RepeatCount);
+}
+
+static const char* multiple[] = {
+ "testrunner.exe",
+ "-v",
+ "-g", "groupname",
+ "-n", "testname",
+ "-r", "98"
+};
+
+TEST(UnityCommandOptions, MultipleOptions)
+{
+ TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(8, multiple));
+ TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
+ STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
+ STRCMP_EQUAL("testname", UnityFixture.NameFilter);
+ TEST_ASSERT_EQUAL(98, UnityFixture.RepeatCount);
+}
+
+static const char* dashRNotLast[] = {
+ "testrunner.exe",
+ "-v",
+ "-g", "gggg",
+ "-r",
+ "-n", "tttt",
+};
+
+TEST(UnityCommandOptions, MultipleOptionsDashRNotLastAndNoValueSpecified)
+{
+ TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(7, dashRNotLast));
+ TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
+ STRCMP_EQUAL("gggg", UnityFixture.GroupFilter);
+ STRCMP_EQUAL("tttt", UnityFixture.NameFilter);
+ TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
+}
+
+static const char* unknownCommand[] = {
+ "testrunner.exe",
+ "-v",
+ "-g", "groupname",
+ "-n", "testname",
+ "-r", "98",
+ "-z"
+};
+TEST(UnityCommandOptions, UnknownCommandIsIgnored)
+{
+ TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(9, unknownCommand));
+ TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
+ STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
+ STRCMP_EQUAL("testname", UnityFixture.NameFilter);
+ TEST_ASSERT_EQUAL(98, UnityFixture.RepeatCount);
+}
+
+
+//------------------------------------------------------------
+
+TEST_GROUP(LeakDetection);
+
+TEST_SETUP(LeakDetection)
+{
+ UnityOutputCharSpy_Create(1000);
+}
+
+TEST_TEAR_DOWN(LeakDetection)
+{
+ UnityOutputCharSpy_Destroy();
+}
+
+#define EXPECT_ABORT_BEGIN \
+ { \
+ jmp_buf TestAbortFrame; \
+ memcpy(TestAbortFrame, Unity.AbortFrame, sizeof(jmp_buf)); \
+ if (TEST_PROTECT()) \
+ {
+
+#define EXPECT_ABORT_END \
+ } \
+ memcpy(Unity.AbortFrame, TestAbortFrame, sizeof(jmp_buf)); \
+ }
+
+TEST(LeakDetection, DetectsLeak)
+{
+ void* m = malloc(10);
+ UnityOutputCharSpy_Enable(1);
+ EXPECT_ABORT_BEGIN
+ UnityMalloc_EndTest();
+ EXPECT_ABORT_END
+ UnityOutputCharSpy_Enable(0);
+ CHECK(strstr(UnityOutputCharSpy_Get(), "This test leaks!"));
+ free(m);
+ Unity.CurrentTestFailed = 0;
+}
+
+TEST(LeakDetection, BufferOverrunFoundDuringFree)
+{
+ void* m = malloc(10);
+ char* s = (char*)m;
+ s[10] = (char)0xFF;
+ UnityOutputCharSpy_Enable(1);
+ EXPECT_ABORT_BEGIN
+ free(m);
+ EXPECT_ABORT_END
+ UnityOutputCharSpy_Enable(0);
+ CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during free()"));
+ Unity.CurrentTestFailed = 0;
+}
+
+TEST(LeakDetection, BufferOverrunFoundDuringRealloc)
+{
+ void* m = malloc(10);
+ char* s = (char*)m;
+ s[10] = (char)0xFF;
+ UnityOutputCharSpy_Enable(1);
+ EXPECT_ABORT_BEGIN
+ m = realloc(m, 100);
+ EXPECT_ABORT_END
+ UnityOutputCharSpy_Enable(0);
+ CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during realloc()"));
+ Unity.CurrentTestFailed = 0;
+}
diff --git a/lab_6/unity/extras/fixture/test/unity_fixture_TestRunner.c b/lab_6/unity/extras/fixture/test/unity_fixture_TestRunner.c
new file mode 100644
index 0000000..6fb0602
--- /dev/null
+++ b/lab_6/unity/extras/fixture/test/unity_fixture_TestRunner.c
@@ -0,0 +1,41 @@
+//- 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 "unity_fixture.h"
+
+TEST_GROUP_RUNNER(UnityFixture)
+{
+ RUN_TEST_CASE(UnityFixture, PointerSetting);
+ RUN_TEST_CASE(UnityFixture, ForceMallocFail);
+ RUN_TEST_CASE(UnityFixture, ReallocSmallerIsUnchanged);
+ RUN_TEST_CASE(UnityFixture, ReallocSameIsUnchanged);
+ RUN_TEST_CASE(UnityFixture, ReallocLargerNeeded);
+ RUN_TEST_CASE(UnityFixture, ReallocNullPointerIsLikeMalloc);
+ RUN_TEST_CASE(UnityFixture, ReallocSizeZeroFreesMemAndReturnsNullPointer);
+ RUN_TEST_CASE(UnityFixture, CallocFillsWithZero);
+ RUN_TEST_CASE(UnityFixture, PointerSet);
+}
+
+TEST_GROUP_RUNNER(UnityCommandOptions)
+{
+ RUN_TEST_CASE(UnityCommandOptions, DefaultOptions);
+ RUN_TEST_CASE(UnityCommandOptions, OptionVerbose);
+ RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByGroup);
+ RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByName);
+ RUN_TEST_CASE(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount);
+ RUN_TEST_CASE(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount);
+ RUN_TEST_CASE(UnityCommandOptions, MultipleOptions);
+ RUN_TEST_CASE(UnityCommandOptions, MultipleOptionsDashRNotLastAndNoValueSpecified);
+ RUN_TEST_CASE(UnityCommandOptions, UnknownCommandIsIgnored);
+}
+
+TEST_GROUP_RUNNER(LeakDetection)
+{
+ RUN_TEST_CASE(LeakDetection, DetectsLeak);
+ RUN_TEST_CASE(LeakDetection, BufferOverrunFoundDuringFree);
+ RUN_TEST_CASE(LeakDetection, BufferOverrunFoundDuringRealloc);
+}
diff --git a/lab_6/unity/extras/fixture/test/unity_output_Spy.c b/lab_6/unity/extras/fixture/test/unity_output_Spy.c
new file mode 100644
index 0000000..0a0ad04
--- /dev/null
+++ b/lab_6/unity/extras/fixture/test/unity_output_Spy.c
@@ -0,0 +1,56 @@
+//- 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 "unity_output_Spy.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int size;
+static int count;
+static char* buffer;
+static int spy_enable;
+
+void UnityOutputCharSpy_Create(int s)
+{
+ size = s;
+ count = 0;
+ spy_enable = 0;
+ buffer = malloc(size);
+ memset(buffer, 0, size);
+}
+
+void UnityOutputCharSpy_Destroy(void)
+{
+ size = 0;
+ free(buffer);
+}
+
+int UnityOutputCharSpy_OutputChar(int c)
+{
+ if (spy_enable)
+ {
+ if (count < (size-1))
+ buffer[count++] = c;
+ }
+ else
+ {
+ putchar(c);
+ }
+ return c;
+}
+
+const char * UnityOutputCharSpy_Get(void)
+{
+ return buffer;
+}
+
+void UnityOutputCharSpy_Enable(int enable)
+{
+ spy_enable = enable;
+}
diff --git a/lab_6/unity/extras/fixture/test/unity_output_Spy.h b/lab_6/unity/extras/fixture/test/unity_output_Spy.h
new file mode 100644
index 0000000..791f6a5
--- /dev/null
+++ b/lab_6/unity/extras/fixture/test/unity_output_Spy.h
@@ -0,0 +1,17 @@
+//- 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 D_unity_output_Spy_H
+#define D_unity_output_Spy_H
+
+void UnityOutputCharSpy_Create(int s);
+void UnityOutputCharSpy_Destroy(void);
+int UnityOutputCharSpy_OutputChar(int c);
+const char * UnityOutputCharSpy_Get(void);
+void UnityOutputCharSpy_Enable(int enable);
+
+#endif