Debian Codegraph @

src/vendor/azure-uamqp-c/deps/umock-c/tests/umockalloc_ut/umockalloc_ut.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifdef __cplusplus
5 #include <cstdlib>
6 #else
7 #include <stdlib.h>
8 #endif
9
10 #include "testrunnerswitcher.h"
11 #include "umock_c/umockalloc.h"
12 #include "umock_c/umock_log.h"
13
14 void UMOCK_LOG(const char* format, ...)
15 {
16 (void)format;
17 }
18
19 typedef struct test_malloc_CALL_TAG
20 {
21 size_t size;
22 } test_malloc_CALL;
23
24 static test_malloc_CALL* test_malloc_calls;
25 static size_t test_malloc_call_count;
26 static void* test_malloc_expected_result;
27
28 typedef struct test_calloc_CALL_TAG
29 {
30 size_t size;
31 size_t nmemb;
32 } test_calloc_CALL;
33
34 static test_calloc_CALL* test_calloc_calls;
35 static size_t test_calloc_call_count;
36 static void* test_calloc_expected_result;
37
38 typedef struct test_realloc_CALL_TAG
39 {
40 size_t size;
41 void* ptr;
42 } test_realloc_CALL;
43
44 static test_realloc_CALL* test_realloc_calls;
45 static size_t test_realloc_call_count;
46 static void* test_realloc_expected_result;
47
48 typedef struct test_free_CALL_TAG
49 {
50 void* ptr;
51 } test_free_CALL;
52
53 static test_free_CALL* test_free_calls;
54 static size_t test_free_call_count;
55
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59
60 void* mock_malloc(size_t size)
61 {
62 test_malloc_CALL* new_calls = (test_malloc_CALL*)realloc(test_malloc_calls, sizeof(test_malloc_CALL) * (test_malloc_call_count + 1));
63 if (new_calls != NULL)
64 {
65 test_malloc_calls = new_calls;
66 test_malloc_calls[test_malloc_call_count].size = size;
67 test_malloc_call_count++;
68 }
69
70 return test_malloc_expected_result;
71 }
72
73 void* mock_calloc(size_t nmemb, size_t size)
74 {
75 test_calloc_CALL* new_calls = (test_calloc_CALL*)realloc(test_calloc_calls, sizeof(test_calloc_CALL) * (test_calloc_call_count + 1));
76 if (new_calls != NULL)
77 {
78 test_calloc_calls = new_calls;
79 test_calloc_calls[test_calloc_call_count].nmemb = nmemb;
80 test_calloc_calls[test_calloc_call_count].size = size;
81 test_calloc_call_count++;
82 }
83
84 return test_calloc_expected_result;
85 }
86
87 void* mock_realloc(void* ptr, size_t size)
88 {
89 test_realloc_CALL* new_calls = (test_realloc_CALL*)realloc(test_realloc_calls, sizeof(test_realloc_CALL) * (test_realloc_call_count + 1));
90 if (new_calls != NULL)
91 {
92 test_realloc_calls = new_calls;
93 test_realloc_calls[test_realloc_call_count].ptr = ptr;
94 test_realloc_calls[test_realloc_call_count].size = size;
95 test_realloc_call_count++;
96 }
97
98 return test_realloc_expected_result;
99 }
100
101 void mock_free(void* ptr)
102 {
103 test_free_CALL* new_calls = (test_free_CALL*)realloc(test_free_calls, sizeof(test_free_CALL) * (test_free_call_count + 1));
104 if (new_calls != NULL)
105 {
106 test_free_calls = new_calls;
107 test_free_calls[test_free_call_count].ptr = ptr;
108 test_free_call_count++;
109 }
110 }
111
112 #ifdef __cplusplus
113 }
114 #endif
115
116 static TEST_MUTEX_HANDLE test_mutex;
117 static TEST_MUTEX_HANDLE global_mutex;
118
119 BEGIN_TEST_SUITE(umockalloc_unittests)
120
121 TEST_SUITE_INITIALIZE(suite_init)
122 {
123 test_mutex = TEST_MUTEX_CREATE();
124 ASSERT_IS_NOT_NULL(test_mutex);
125 }
126
127 TEST_SUITE_CLEANUP(suite_cleanup)
128 {
129 TEST_MUTEX_DESTROY(test_mutex);
130 }
131
132 TEST_FUNCTION_INITIALIZE(test_function_init)
133 {
134 int mutex_acquire_result = TEST_MUTEX_ACQUIRE(test_mutex);
135 ASSERT_ARE_EQUAL(int, 0, mutex_acquire_result);
136
137 test_free_calls = NULL;
138 test_free_call_count = 0;
139
140 test_malloc_calls = NULL;
141 test_malloc_call_count = 0;
142 test_malloc_expected_result = (void*)0x4242;
143
144 test_calloc_calls = NULL;
145 test_calloc_call_count = 0;
146 test_calloc_expected_result = (void*)0x4242;
147
148 test_realloc_calls = NULL;
149 test_realloc_call_count = 0;
150 test_realloc_expected_result = (void*)0x4243;
151 }
152
153 TEST_FUNCTION_CLEANUP(test_function_cleanup)
154 {
155 free(test_malloc_calls);
156 test_malloc_calls = NULL;
157 test_malloc_call_count = 0;
158
159 free(test_calloc_calls);
160 test_calloc_calls = NULL;
161 test_calloc_call_count = 0;
162
163 free(test_realloc_calls);
164 test_realloc_calls = NULL;
165 test_realloc_call_count = 0;
166
167 free(test_free_calls);
168 test_free_calls = NULL;
169 test_free_call_count = 0;
170
171 TEST_MUTEX_RELEASE(test_mutex);
172 }
173
174 /* umockalloc_malloc */
175
176 /* Tests_SRS_UMOCKALLOC_01_001: [ umockalloc_malloc shall call malloc, while passing the size argument to malloc. ] */
177 /* Tests_SRS_UMOCKALLOC_01_002: [ umockalloc_malloc shall return the result of malloc. ]*/
178 TEST_FUNCTION(umockalloc_malloc_calls_malloc)
179 {
180 // arrange
181 void* result;
182 test_malloc_expected_result = (void*)0x4242;
183
184 // act
185 result = umockalloc_malloc(42);
186
187 // assert
188 ASSERT_ARE_EQUAL(void_ptr, (void*)0x4242, result);
189 ASSERT_ARE_EQUAL(size_t, 1, test_malloc_call_count);
190 ASSERT_ARE_EQUAL(size_t, 42, test_malloc_calls[0].size);
191 }
192
193 /* Tests_SRS_UMOCKALLOC_01_001: [ umockalloc_malloc shall call malloc, while passing the size argument to malloc. ] */
194 /* Tests_SRS_UMOCKALLOC_01_002: [ umockalloc_malloc shall return the result of malloc. ]*/
195 TEST_FUNCTION(umockalloc_malloc_calls_malloc_other_value)
196 {
197 // arrange
198 void* result;
199 test_malloc_expected_result = (void*)0x5252;
200
201 // act
202 result = umockalloc_malloc(43);
203
204 // assert
205 ASSERT_ARE_EQUAL(void_ptr, (void*)0x5252, result);
206 ASSERT_ARE_EQUAL(size_t, 1, test_malloc_call_count);
207 ASSERT_ARE_EQUAL(size_t, 43, test_malloc_calls[0].size);
208 }
209
210 /* Tests_SRS_UMOCKALLOC_01_001: [ umockalloc_malloc shall call malloc, while passing the size argument to malloc. ] */
211 TEST_FUNCTION(when_malloc_returns_NULL_umockalloc_malloc_returns_NULL)
212 {
213 // arrange
214 void* result;
215 test_malloc_expected_result = NULL;
216
217 // act
218 result = umockalloc_malloc(43);
219
220 // assert
221 ASSERT_IS_NULL(result);
222 ASSERT_ARE_EQUAL(size_t, 1, test_malloc_call_count);
223 ASSERT_ARE_EQUAL(size_t, 43, test_malloc_calls[0].size);
224 }
225
226 /* umockalloc_calloc */
227
228 /* Tests_SRS_UMOCKALLOC_09_001: [ umockalloc_calloc shall call calloc, while passing the number of members and size arguments to calloc. ] */
229 /* Tests_SRS_UMOCKALLOC_09_002: [ umockalloc_calloc shall return the result of calloc. ]*/
230 TEST_FUNCTION(umockalloc_calloc_calls_calloc)
231 {
232 // arrange
233 void* result;
234 test_calloc_expected_result = (void*)0x4242;
235
236 // act
237 result = umockalloc_calloc(1, 42);
238
239 // assert
240 ASSERT_ARE_EQUAL(void_ptr, (void*)0x4242, result);
241 ASSERT_ARE_EQUAL(size_t, 1, test_calloc_call_count);
242 ASSERT_ARE_EQUAL(size_t, 42, test_calloc_calls[0].size);
243 ASSERT_ARE_EQUAL(size_t, 1, test_calloc_calls[0].nmemb);
244 }
245
246 /* Tests_SRS_UMOCKALLOC_09_001: [ umockalloc_calloc shall call calloc, while passing the number of members and size arguments to calloc. ] */
247 /* Tests_SRS_UMOCKALLOC_09_002: [ umockalloc_calloc shall return the result of calloc. ]*/
248 TEST_FUNCTION(umockalloc_calloc_calls_calloc_other_value)
249 {
250 // arrange
251 void* result;
252 test_calloc_expected_result = (void*)0x5252;
253
254 // act
255 result = umockalloc_calloc(1, 43);
256
257 // assert
258 ASSERT_ARE_EQUAL(void_ptr, (void*)0x5252, result);
259 ASSERT_ARE_EQUAL(size_t, 1, test_calloc_call_count);
260 ASSERT_ARE_EQUAL(size_t, 43, test_calloc_calls[0].size);
261 ASSERT_ARE_EQUAL(size_t, 1, test_calloc_calls[0].nmemb);
262 }
263
264 /* Tests_SRS_UMOCKALLOC_09_001: [ umockalloc_calloc shall call calloc, while passing the number of members and size arguments to calloc. ] */
265 /* Tests_SRS_UMOCKALLOC_09_002: [ umockalloc_calloc shall return the result of calloc. ]*/
266 TEST_FUNCTION(umockalloc_calloc_calls_calloc_2_members)
267 {
268 // arrange
269 void* result;
270 test_calloc_expected_result = (void*)0x4242;
271
272 // act
273 result = umockalloc_calloc(2, 40);
274
275 // assert
276 ASSERT_ARE_EQUAL(void_ptr, (void*)0x4242, result);
277 ASSERT_ARE_EQUAL(size_t, 1, test_calloc_call_count);
278 ASSERT_ARE_EQUAL(size_t, 40, test_calloc_calls[0].size);
279 ASSERT_ARE_EQUAL(size_t, 2, test_calloc_calls[0].nmemb);
280 }
281
282 /* Tests_SRS_UMOCKALLOC_09_001: [ umockalloc_calloc shall call calloc, while passing the number of members and size arguments to calloc. ] */
283 /* Tests_SRS_UMOCKALLOC_09_002: [ umockalloc_calloc shall return the result of calloc. ]*/
284 TEST_FUNCTION(umockalloc_calloc_calls_calloc_0_members)
285 {
286 // arrange
287 void* result;
288 test_calloc_expected_result = NULL;
289
290 // act
291 result = umockalloc_calloc(0, 40);
292
293 // assert
294 ASSERT_IS_NULL(result);
295 ASSERT_ARE_EQUAL(size_t, 1, test_calloc_call_count);
296 ASSERT_ARE_EQUAL(size_t, 40, test_calloc_calls[0].size);
297 ASSERT_ARE_EQUAL(size_t, 0, test_calloc_calls[0].nmemb);
298 }
299
300 /* Tests_SRS_UMOCKALLOC_09_001: [ umockalloc_malloc shall call malloc, while passing the size argument to malloc. ] */
301 TEST_FUNCTION(when_calloc_returns_NULL_umockalloc_calloc_returns_NULL)
302 {
303 // arrange
304 void* result;
305 test_calloc_expected_result = NULL;
306
307 // act
308 result = umockalloc_calloc(1, 43);
309
310 // assert
311 ASSERT_IS_NULL(result);
312 ASSERT_ARE_EQUAL(size_t, 1, test_calloc_call_count);
313 ASSERT_ARE_EQUAL(size_t, 43, test_calloc_calls[0].size);
314 ASSERT_ARE_EQUAL(size_t, 1, test_calloc_calls[0].nmemb);
315 }
316
317 /* umockalloc_realloc */
318
319 /* Tests_SRS_UMOCKALLOC_01_005: [ umockalloc_realloc shall call realloc, while passing the ptr and size arguments to realloc. ] */
320 /* Tests_SRS_UMOCKALLOC_01_006: [ umockalloc_realloc shall return the result of realloc. ]*/
321 TEST_FUNCTION(umockalloc_realloc_calls_realloc)
322 {
323 // arrange
324 void* result;
325 test_realloc_expected_result = (void*)0x4242;
326
327 // act
328 result = umockalloc_realloc((void*)0x2222, 42);
329
330 // assert
331 ASSERT_ARE_EQUAL(void_ptr, (void*)0x4242, result);
332 ASSERT_ARE_EQUAL(size_t, 1, test_realloc_call_count);
333 ASSERT_ARE_EQUAL(size_t, 42, test_realloc_calls[0].size);
334 ASSERT_ARE_EQUAL(void_ptr, (void*)0x2222, test_realloc_calls[0].ptr);
335 }
336
337 /* Tests_SRS_UMOCKALLOC_01_005: [ umockalloc_realloc shall call realloc, while passing the ptr and size arguments to realloc. ] */
338 /* Tests_SRS_UMOCKALLOC_01_006: [ umockalloc_realloc shall return the result of realloc. ]*/
339 TEST_FUNCTION(umockalloc_realloc_calls_realloc_other_value)
340 {
341 // arrange
342 void* result;
343 test_realloc_expected_result = (void*)0x5252;
344
345 // act
346 result = umockalloc_realloc((void*)0x3232, 43);
347
348 // assert
349 ASSERT_ARE_EQUAL(void_ptr, (void*)0x5252, result);
350 ASSERT_ARE_EQUAL(size_t, 1, test_realloc_call_count);
351 ASSERT_ARE_EQUAL(size_t, 43, test_realloc_calls[0].size);
352 ASSERT_ARE_EQUAL(void_ptr, (void*)0x3232, test_realloc_calls[0].ptr);
353 }
354
355 /* Tests_SRS_UMOCKALLOC_01_005: [ umockalloc_realloc shall call realloc, while passing the ptr and size arguments to realloc. ] */
356 /* Tests_SRS_UMOCKALLOC_01_006: [ umockalloc_realloc shall return the result of realloc. ]*/
357 TEST_FUNCTION(when_realloc_returns_NULL_umockalloc_realloc_returns_NULL)
358 {
359 // arrange
360 void* result;
361 test_realloc_expected_result = NULL;
362
363 // act
364 result = umockalloc_realloc((void*)0x3232, 43);
365
366 // assert
367 ASSERT_IS_NULL(result);
368 ASSERT_ARE_EQUAL(size_t, 1, test_realloc_call_count);
369 ASSERT_ARE_EQUAL(size_t, 43, test_realloc_calls[0].size);
370 ASSERT_ARE_EQUAL(void_ptr, (void*)0x3232, test_realloc_calls[0].ptr);
371 }
372
373 /* Tests_SRS_UMOCKALLOC_01_005: [ umockalloc_realloc shall call realloc, while passing the ptr and size arguments to realloc. ] */
374 /* Tests_SRS_UMOCKALLOC_01_006: [ umockalloc_realloc shall return the result of realloc. ]*/
375 TEST_FUNCTION(umockalloc_realloc_with_NULL_and_0_size_calls_the_underlying_realloc)
376 {
377 // arrange
378 void* result;
379 test_realloc_expected_result = (void*)0x4242;
380
381 // act
382 result = umockalloc_realloc(NULL, 0);
383
384 // assert
385 ASSERT_ARE_EQUAL(void_ptr, (void*)0x4242, result);
386 ASSERT_ARE_EQUAL(size_t, 1, test_realloc_call_count);
387 ASSERT_ARE_EQUAL(size_t, 0, test_realloc_calls[0].size);
388 ASSERT_ARE_EQUAL(void_ptr, NULL, test_realloc_calls[0].ptr);
389 }
390
391 /* umockalloc_free */
392
393 /* Tests_SRS_UMOCKALLOC_01_007: [ umockalloc_free shall call free, while passing the ptr argument to free. ]*/
394 TEST_FUNCTION(umockalloc_free_calls_free)
395 {
396 // arrange
397
398 // act
399 umockalloc_free((void*)0x4242);
400
401 // assert
402 ASSERT_ARE_EQUAL(size_t, 1, test_free_call_count);
403 ASSERT_ARE_EQUAL(void_ptr, (void*)0x4242, test_free_calls[0].ptr);
404 }
405
406 /* Tests_SRS_UMOCKALLOC_01_007: [ umockalloc_free shall call free, while passing the ptr argument to free. ]*/
407 TEST_FUNCTION(umockalloc_free_with_NULL_calls_free_with_NULL)
408 {
409 // arrange
410
411 // act
412 umockalloc_free(NULL);
413
414 // assert
415 ASSERT_ARE_EQUAL(size_t, 1, test_free_call_count);
416 ASSERT_ARE_EQUAL(void_ptr, NULL, test_free_calls[0].ptr);
417 }
418
419 END_TEST_SUITE(umockalloc_unittests)