1 //- Copyright (c) 2010 James Grenning and Contributed to Unity Project
2 /* ==========================================
3 Unity Project - A Test Framework for C
4 Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
5 [Released under MIT License. Please refer to license.txt for details]
6 ========================================== */
8 #include "unity_fixture.h"
9 #include "unity_output_Spy.h"
13 extern UNITY_FIXTURE_T UnityFixture;
15 TEST_GROUP(UnityFixture);
17 TEST_SETUP(UnityFixture)
21 TEST_TEAR_DOWN(UnityFixture)
27 int* pointer2 = (int*)2;
28 int* pointer3 = (int*)3;
34 TEST(UnityFixture, PointerSetting)
36 TEST_ASSERT_POINTERS_EQUAL(pointer1, 0);
37 UT_PTR_SET(pointer1, &int1);
38 UT_PTR_SET(pointer2, &int2);
39 UT_PTR_SET(pointer3, &int3);
40 TEST_ASSERT_POINTERS_EQUAL(pointer1, &int1);
41 TEST_ASSERT_POINTERS_EQUAL(pointer2, &int2);
42 TEST_ASSERT_POINTERS_EQUAL(pointer3, &int3);
43 UT_PTR_SET(pointer1, &int4);
44 UnityPointer_UndoAllSets();
45 TEST_ASSERT_POINTERS_EQUAL(pointer1, 0);
46 TEST_ASSERT_POINTERS_EQUAL(pointer2, (int*)2);
47 TEST_ASSERT_POINTERS_EQUAL(pointer3, (int*)3);
50 TEST(UnityFixture, ForceMallocFail)
52 UnityMalloc_MakeMallocFailAfterCount(1);
55 void* mfails = malloc(10);
56 TEST_ASSERT_POINTERS_EQUAL(0, mfails);
60 TEST(UnityFixture, ReallocSmallerIsUnchanged)
62 void* m1 = malloc(10);
63 void* m2 = realloc(m1, 5);
64 TEST_ASSERT_POINTERS_EQUAL(m1, m2);
68 TEST(UnityFixture, ReallocSameIsUnchanged)
70 void* m1 = malloc(10);
71 void* m2 = realloc(m1, 10);
72 TEST_ASSERT_POINTERS_EQUAL(m1, m2);
76 TEST(UnityFixture, ReallocLargerNeeded)
78 void* m1 = malloc(10);
79 strcpy((char*)m1, "123456789");
80 void* m2 = realloc(m1, 15);
82 STRCMP_EQUAL("123456789", m2);
86 TEST(UnityFixture, ReallocNullPointerIsLikeMalloc)
88 void* m = realloc(0, 15);
93 TEST(UnityFixture, ReallocSizeZeroFreesMemAndReturnsNullPointer)
95 void* m1 = malloc(10);
96 void* m2 = realloc(m1, 0);
97 TEST_ASSERT_POINTERS_EQUAL(0, m2);
100 TEST(UnityFixture, CallocFillsWithZero)
102 void* m = calloc(3, sizeof(char));
104 TEST_ASSERT_BYTES_EQUAL(0, s[0]);
105 TEST_ASSERT_BYTES_EQUAL(0, s[1]);
106 TEST_ASSERT_BYTES_EQUAL(0, s[2]);
113 TEST(UnityFixture, PointerSet)
122 UnityPointer_Init(10);
123 UT_PTR_SET(p1, &newC1);
124 UT_PTR_SET(p2, &newC2);
125 TEST_ASSERT_POINTERS_EQUAL(&newC1, p1);
126 TEST_ASSERT_POINTERS_EQUAL(&newC2, p2);
127 UnityPointer_UndoAllSets();
128 TEST_ASSERT_POINTERS_EQUAL(&c1, p1);
129 TEST_ASSERT_POINTERS_EQUAL(&c2, p2);
132 //------------------------------------------------------------
134 TEST_GROUP(UnityCommandOptions);
138 const char* savedName;
139 const char* savedGroup;
141 TEST_SETUP(UnityCommandOptions)
143 savedVerbose = UnityFixture.Verbose;
144 savedRepeat = UnityFixture.RepeatCount;
145 savedName = UnityFixture.NameFilter;
146 savedGroup = UnityFixture.GroupFilter;
149 TEST_TEAR_DOWN(UnityCommandOptions)
151 UnityFixture.Verbose = savedVerbose;
152 UnityFixture.RepeatCount= savedRepeat;
153 UnityFixture.NameFilter = savedName;
154 UnityFixture.GroupFilter = savedGroup;
158 static char* noOptions[] = {
162 TEST(UnityCommandOptions, DefaultOptions)
164 UnityGetCommandLineOptions(1, noOptions);
165 TEST_ASSERT_EQUAL(0, UnityFixture.Verbose);
166 TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.GroupFilter);
167 TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.NameFilter);
168 TEST_ASSERT_EQUAL(1, UnityFixture.RepeatCount);
171 static char* verbose[] = {
176 TEST(UnityCommandOptions, OptionVerbose)
178 TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, verbose));
179 TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
182 static char* group[] = {
187 TEST(UnityCommandOptions, OptionSelectTestByGroup)
189 TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, group));
190 STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
193 static char* name[] = {
198 TEST(UnityCommandOptions, OptionSelectTestByName)
200 TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, name));
201 STRCMP_EQUAL("testname", UnityFixture.NameFilter);
204 static char* repeat[] = {
209 TEST(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount)
211 TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, repeat));
212 TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
215 TEST(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount)
217 TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, repeat));
218 TEST_ASSERT_EQUAL(99, UnityFixture.RepeatCount);
221 static char* multiple[] = {
229 TEST(UnityCommandOptions, MultipleOptions)
231 TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(8, multiple));
232 TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
233 STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
234 STRCMP_EQUAL("testname", UnityFixture.NameFilter);
235 TEST_ASSERT_EQUAL(98, UnityFixture.RepeatCount);
238 static char* dashRNotLast[] = {
246 TEST(UnityCommandOptions, MultipleOptionsDashRNotLastAndNoValueSpecified)
248 TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(7, dashRNotLast));
249 TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
250 STRCMP_EQUAL("gggg", UnityFixture.GroupFilter);
251 STRCMP_EQUAL("tttt", UnityFixture.NameFilter);
252 TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
256 //------------------------------------------------------------
258 TEST_GROUP(LeakDetection);
260 TEST_SETUP(LeakDetection)
262 UnityOutputCharSpy_Create(1000);
265 TEST_TEAR_DOWN(LeakDetection)
267 UnityOutputCharSpy_Destroy();
270 #define EXPECT_ABORT_BEGIN \
272 jmp_buf TestAbortFrame; \
273 memcpy(TestAbortFrame, Unity.AbortFrame, sizeof(jmp_buf)); \
274 if (TEST_PROTECT()) \
277 #define EXPECT_ABORT_END \
279 memcpy(Unity.AbortFrame, TestAbortFrame, sizeof(jmp_buf)); \
282 TEST(LeakDetection, DetectsLeak)
284 void* m = malloc(10);
285 UnityOutputCharSpy_Enable(1);
287 UnityMalloc_EndTest();
289 UnityOutputCharSpy_Enable(0);
290 CHECK(strstr(UnityOutputCharSpy_Get(), "This test leaks!"));
292 Unity.CurrentTestFailed = 0;
295 TEST(LeakDetection, BufferOverrunFoundDuringFree)
297 void* m = malloc(10);
300 UnityOutputCharSpy_Enable(1);
304 UnityOutputCharSpy_Enable(0);
305 CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during free()"));
306 Unity.CurrentTestFailed = 0;
309 TEST(LeakDetection, BufferOverrunFoundDuringRealloc)
311 void* m = malloc(10);
314 UnityOutputCharSpy_Enable(1);
318 UnityOutputCharSpy_Enable(0);
319 CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during realloc()"));
320 Unity.CurrentTestFailed = 0;