]> git.mdlowis.com Git - projs/libcds.git/blob
b8b4524cce9649fe0dde93f3639a890d8682d077
[projs/libcds.git] /
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 ========================================== */
7
8 #include "unity_fixture.h"
9 #include "unity_output_Spy.h"
10 #include <stdlib.h>
11 #include <string.h>
12
13 extern UNITY_FIXTURE_T UnityFixture;
14
15 TEST_GROUP(UnityFixture);
16
17 TEST_SETUP(UnityFixture)
18 {
19 }
20
21 TEST_TEAR_DOWN(UnityFixture)
22 {
23 }
24
25 int my_int;
26 int* pointer1 = 0;
27 int* pointer2 = (int*)2;
28 int* pointer3 = (int*)3;
29 int int1;
30 int int2;
31 int int3;
32 int int4;
33
34 TEST(UnityFixture, PointerSetting)
35 {
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);
48 }
49
50 TEST(UnityFixture, ForceMallocFail)
51 {
52     UnityMalloc_MakeMallocFailAfterCount(1);
53     void* m = malloc(10);
54     CHECK(m);
55     void* mfails = malloc(10);
56     TEST_ASSERT_POINTERS_EQUAL(0, mfails);
57     free(m);
58 }
59
60 TEST(UnityFixture, ReallocSmallerIsUnchanged)
61 {
62     void* m1 = malloc(10);
63     void* m2 = realloc(m1, 5);
64     TEST_ASSERT_POINTERS_EQUAL(m1, m2);
65     free(m2);
66 }
67
68 TEST(UnityFixture, ReallocSameIsUnchanged)
69 {
70     void* m1 = malloc(10);
71     void* m2 = realloc(m1, 10);
72     TEST_ASSERT_POINTERS_EQUAL(m1, m2);
73     free(m2);
74 }
75
76 TEST(UnityFixture, ReallocLargerNeeded)
77 {
78     void* m1 = malloc(10);
79     strcpy((char*)m1, "123456789");
80     void* m2 = realloc(m1, 15);
81     CHECK(m1 != m2);
82     STRCMP_EQUAL("123456789", m2);
83     free(m2);
84 }
85
86 TEST(UnityFixture, ReallocNullPointerIsLikeMalloc)
87 {
88     void* m = realloc(0, 15);
89     CHECK(m != 0);
90     free(m);
91 }
92
93 TEST(UnityFixture, ReallocSizeZeroFreesMemAndReturnsNullPointer)
94 {
95     void* m1 = malloc(10);
96     void* m2 = realloc(m1, 0);
97     TEST_ASSERT_POINTERS_EQUAL(0, m2);
98 }
99
100 TEST(UnityFixture, CallocFillsWithZero)
101 {
102     void* m = calloc(3, sizeof(char));
103     char* s = (char*)m;
104     TEST_ASSERT_BYTES_EQUAL(0, s[0]);
105     TEST_ASSERT_BYTES_EQUAL(0, s[1]);
106     TEST_ASSERT_BYTES_EQUAL(0, s[2]);
107     free(m);
108 }
109
110 char *p1;
111 char *p2;
112
113 TEST(UnityFixture, PointerSet)
114 {
115     char c1;
116     char c2;
117     char newC1;
118     char newC2;
119     p1 = &c1;
120     p2 = &c2;
121
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);
130 }
131
132 //------------------------------------------------------------
133
134 TEST_GROUP(UnityCommandOptions);
135
136 int savedVerbose;
137 int savedRepeat;
138 const char* savedName;
139 const char* savedGroup;
140
141 TEST_SETUP(UnityCommandOptions)
142 {
143     savedVerbose = UnityFixture.Verbose;
144     savedRepeat = UnityFixture.RepeatCount;
145     savedName = UnityFixture.NameFilter;
146     savedGroup = UnityFixture.GroupFilter;
147 }
148
149 TEST_TEAR_DOWN(UnityCommandOptions)
150 {
151     UnityFixture.Verbose = savedVerbose;
152     UnityFixture.RepeatCount= savedRepeat;
153     UnityFixture.NameFilter = savedName;
154     UnityFixture.GroupFilter = savedGroup;
155 }
156
157
158 static char* noOptions[] = {
159         "testrunner.exe"
160 };
161
162 TEST(UnityCommandOptions, DefaultOptions)
163 {
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);
169 }
170
171 static char* verbose[] = {
172         "testrunner.exe",
173         "-v"
174 };
175
176 TEST(UnityCommandOptions, OptionVerbose)
177 {
178     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, verbose));
179     TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
180 }
181
182 static char* group[] = {
183         "testrunner.exe",
184         "-g", "groupname"
185 };
186
187 TEST(UnityCommandOptions, OptionSelectTestByGroup)
188 {
189     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, group));
190     STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
191 }
192
193 static char* name[] = {
194         "testrunner.exe",
195         "-n", "testname"
196 };
197
198 TEST(UnityCommandOptions, OptionSelectTestByName)
199 {
200     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, name));
201     STRCMP_EQUAL("testname", UnityFixture.NameFilter);
202 }
203
204 static char* repeat[] = {
205         "testrunner.exe",
206         "-r", "99"
207 };
208
209 TEST(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount)
210 {
211     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, repeat));
212     TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
213 }
214
215 TEST(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount)
216 {
217     TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, repeat));
218     TEST_ASSERT_EQUAL(99, UnityFixture.RepeatCount);
219 }
220
221 static char* multiple[] = {
222         "testrunner.exe",
223         "-v",
224         "-g", "groupname",
225         "-n", "testname",
226         "-r", "98"
227 };
228
229 TEST(UnityCommandOptions, MultipleOptions)
230 {
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);
236 }
237
238 static char* dashRNotLast[] = {
239         "testrunner.exe",
240         "-v",
241         "-g", "gggg",
242         "-r",
243         "-n", "tttt",
244 };
245
246 TEST(UnityCommandOptions, MultipleOptionsDashRNotLastAndNoValueSpecified)
247 {
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);
253 }
254
255
256 //------------------------------------------------------------
257
258 TEST_GROUP(LeakDetection);
259
260 TEST_SETUP(LeakDetection)
261 {
262     UnityOutputCharSpy_Create(1000);
263 }
264
265 TEST_TEAR_DOWN(LeakDetection)
266 {
267     UnityOutputCharSpy_Destroy();
268 }
269
270 #define EXPECT_ABORT_BEGIN \
271   { \
272     jmp_buf TestAbortFrame;   \
273     memcpy(TestAbortFrame, Unity.AbortFrame, sizeof(jmp_buf)); \
274     if (TEST_PROTECT()) \
275     {
276
277 #define EXPECT_ABORT_END \
278     } \
279     memcpy(Unity.AbortFrame, TestAbortFrame, sizeof(jmp_buf)); \
280   }
281
282 TEST(LeakDetection, DetectsLeak)
283 {
284     void* m = malloc(10);
285     UnityOutputCharSpy_Enable(1);
286     EXPECT_ABORT_BEGIN
287     UnityMalloc_EndTest();
288     EXPECT_ABORT_END
289     UnityOutputCharSpy_Enable(0);
290     CHECK(strstr(UnityOutputCharSpy_Get(), "This test leaks!"));
291     free(m);
292     Unity.CurrentTestFailed = 0;
293 }
294
295 TEST(LeakDetection, BufferOverrunFoundDuringFree)
296 {
297     void* m = malloc(10);
298     char* s = (char*)m;
299     s[10] = (char)0xFF;
300     UnityOutputCharSpy_Enable(1);
301     EXPECT_ABORT_BEGIN
302     free(m);
303     EXPECT_ABORT_END
304     UnityOutputCharSpy_Enable(0);
305     CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during free()"));
306     Unity.CurrentTestFailed = 0;
307 }
308
309 TEST(LeakDetection, BufferOverrunFoundDuringRealloc)
310 {
311     void* m = malloc(10);
312     char* s = (char*)m;
313     s[10] = (char)0xFF;
314     UnityOutputCharSpy_Enable(1);
315     EXPECT_ABORT_BEGIN
316     m = realloc(m, 100);
317     EXPECT_ABORT_END
318     UnityOutputCharSpy_Enable(0);
319     CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during realloc()"));
320     Unity.CurrentTestFailed = 0;
321 }