Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/gballoc_ut/gballoc_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 #if defined(GB_MEASURE_MEMORY_FOR_THIS)
5 #undef GB_MEASURE_MEMORY_FOR_THIS
6 #endif
7
8 #ifdef __cplusplus
9 #include <cstdlib>
10 #else
11 #include <stdlib.h>
12 #endif
13
14 #include "azure_macro_utils/macro_utils.h"
15 #include "azure_c_shared_utility/optimize_size.h"
16 #include "azure_c_shared_utility/gballoc.h"
17 #include "testrunnerswitcher.h"
18 #include "azure_c_shared_utility/lock.h"
19
20 #ifndef SIZE_MAX
21 #define SIZE_MAX ((size_t)~(size_t)0)
22 #endif
23
24 static TEST_MUTEX_HANDLE g_testByTest;
25
26 static void* TEST_ALLOC_PTR1 = (void*)0x4242;
27 static void* TEST_ALLOC_PTR2 = (void*)0x4243;
28 static void* TEST_REALLOC_PTR = (void*)0x4245;
29
30 #define OVERHEAD_SIZE 4096
31 static const LOCK_HANDLE TEST_LOCK_HANDLE = (LOCK_HANDLE)0x4244;
32
33 #define ENABLE_MOCKS
34
35 #include "umock_c/umock_c.h"
36 #include "umock_c/umock_c_prod.h"
37
38 IMPLEMENT_UMOCK_C_ENUM_TYPE(LOCK_RESULT, LOCK_RESULT_VALUES);
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 MOCKABLE_FUNCTION(, void*, mock_malloc, size_t, size);
44 MOCKABLE_FUNCTION(, void*, mock_calloc, size_t, nmemb, size_t, size);
45 MOCKABLE_FUNCTION(, void*, mock_realloc, void*, ptr, size_t, size);
46 MOCKABLE_FUNCTION(, void, mock_free, void*, ptr);
47
48 MOCKABLE_FUNCTION(, LOCK_HANDLE, Lock_Init);
49 MOCKABLE_FUNCTION(, LOCK_RESULT, Lock_Deinit, LOCK_HANDLE, handle);
50 MOCKABLE_FUNCTION(, LOCK_RESULT, Lock, LOCK_HANDLE, handle);
51 MOCKABLE_FUNCTION(, LOCK_RESULT, Unlock, LOCK_HANDLE, handle);
52 #ifdef __cplusplus
53 }
54 #endif
55
56 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
57
58 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
59 {
60 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
61 }
62
63 BEGIN_TEST_SUITE(GBAlloc_UnitTests)
64
65 TEST_SUITE_INITIALIZE(TestClassInitialize)
66 {
67 int result;
68
69 g_testByTest = TEST_MUTEX_CREATE();
70 ASSERT_IS_NOT_NULL(g_testByTest);
71
72 result = umock_c_init(on_umock_c_error);
73 ASSERT_ARE_EQUAL(int, 0, result);
74
75 REGISTER_UMOCK_ALIAS_TYPE(LOCK_HANDLE, void*);
76 REGISTER_TYPE(LOCK_RESULT, LOCK_RESULT);
77
78 REGISTER_GLOBAL_MOCK_RETURN(mock_malloc, TEST_ALLOC_PTR1);
79 REGISTER_GLOBAL_MOCK_RETURN(mock_realloc, TEST_ALLOC_PTR1);
80 REGISTER_GLOBAL_MOCK_RETURN(mock_calloc, TEST_ALLOC_PTR1);
81
82 REGISTER_GLOBAL_MOCK_RETURN(Lock_Init, TEST_LOCK_HANDLE);
83 REGISTER_GLOBAL_MOCK_RETURN(Lock, LOCK_OK);
84 REGISTER_GLOBAL_MOCK_RETURN(Unlock, LOCK_OK);
85 }
86
87 TEST_SUITE_CLEANUP(TestClassCleanup)
88 {
89 umock_c_deinit();
90 TEST_MUTEX_DESTROY(g_testByTest);
91 }
92
93 TEST_FUNCTION_INITIALIZE(TestMethodInitialize)
94 {
95 if (TEST_MUTEX_ACQUIRE(g_testByTest))
96 {
97 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
98 }
99
100 umock_c_reset_all_calls();
101 }
102
103 TEST_FUNCTION_CLEANUP(TestMethodCleanup)
104 {
105 gballoc_deinit();
106
107 TEST_MUTEX_RELEASE(g_testByTest);
108 }
109
110 /* gballoc_init */
111
112 /* Tests_SRS_GBALLOC_01_002: [Upon initialization the total memory used and maximum total memory used tracked by the module shall be set to 0.] */
113 TEST_FUNCTION(gballoc_init_resets_memory_used)
114 {
115 //arrange
116 void* allocation;
117 gballoc_init();
118 allocation = malloc(OVERHEAD_SIZE);
119
120 EXPECTED_CALL(mock_calloc(1, 0))
121 .SetReturn(allocation);
122 gballoc_free(gballoc_malloc(1));
123
124 gballoc_deinit();
125
126 // act
127 gballoc_init();
128
129 // assert
130 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
131 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getCurrentMemoryUsed());
132
133 ///cleanup
134 free(allocation);
135 }
136
137 /* Tests_SRS_GBALLOC_01_024: [gballoc_init shall initialize the gballoc module and return 0 upon success.] */
138 /* Tests_SRS_GBALLOC_01_026: [gballoc_Init shall create a lock handle that will be used to make the other gballoc APIs thread-safe.] */
139 TEST_FUNCTION(when_gballoc_init_calls_lock_init_and_it_succeeds_then_gballoc_init_succeeds)
140 {
141 // arrange
142 int result;
143 STRICT_EXPECTED_CALL(Lock_Init());
144
145 // act
146 result = gballoc_init();
147
148 // assert
149 ASSERT_ARE_EQUAL(int, 0, result);
150 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
151 }
152
153 /* Tests_SRS_GBALLOC_01_027: [If the Lock creation fails, gballoc_init shall return a non-zero value.] */
154 TEST_FUNCTION(when_lock_init_fails_gballoc_init_fails)
155 {
156 // arrange
157 int result;
158 STRICT_EXPECTED_CALL(Lock_Init())
159 .SetReturn((LOCK_HANDLE)NULL);
160
161 // act
162 result = gballoc_init();
163
164 // assert
165 ASSERT_ARE_NOT_EQUAL(int, 0, result);
166 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
167 }
168
169 /* Tests_SRS_GBALLOC_01_025: [Init after Init shall fail and return a non-zero value.] */
170 TEST_FUNCTION(gballoc_init_after_gballoc_init_fails)
171 {
172 // arrange
173 int result;
174 STRICT_EXPECTED_CALL(Lock_Init());
175 gballoc_init();
176
177 //act
178 result = gballoc_init();
179
180 // assert
181 ASSERT_ARE_NOT_EQUAL(int, 0, result);
182 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
183 }
184
185 /* gballoc_deinit */
186
187 /* Tests_SRS_GBALLOC_01_028: [gballoc_deinit shall free all resources allocated by gballoc_init.] */
188 TEST_FUNCTION(gballoc_deinit_frees_the_lock_when_the_module_was_initialized)
189 {
190 // arrange
191 gballoc_init();
192 umock_c_reset_all_calls();
193
194 STRICT_EXPECTED_CALL(Lock_Deinit(TEST_LOCK_HANDLE));
195
196 // act
197 gballoc_deinit();
198
199 // assert
200 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
201 }
202
203 /* Tests_SRS_GBALLOC_01_029: [if gballoc is not initialized gballoc_deinit shall do nothing.] */
204 TEST_FUNCTION(gballoc_deinit_after_gballoc_deinit_doesnot_free_lock)
205 {
206 //arrange
207
208 // act
209 gballoc_deinit();
210
211 // assert
212 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
213 }
214
215 /* gballoc_malloc */
216
217 /* Tests_SRS_GBALLOC_01_048: [If acquiring the lock fails, gballoc_malloc shall return NULL.] */
218 TEST_FUNCTION(when_acquiring_the_lock_fails_gballoc_malloc_fails)
219 {
220 // arrange
221 void* result;
222 gballoc_init();
223 umock_c_reset_all_calls();
224
225 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE))
226 .SetReturn(LOCK_ERROR);
227
228 // act
229 result = gballoc_malloc(1);
230
231 // assert
232 ASSERT_IS_NULL(result);
233 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
234 }
235
236 /* Tests_SRS_GBALLOC_01_003: [gballoc_malloc shall call the C99 malloc function and return its result.] */
237 /* Tests_SRS_GBALLOC_01_004: [If the underlying malloc call is successful, gballoc_malloc shall increment the total memory used with the amount indicated by size.] */
238 /* Tests_SRS_GBALLOC_01_030: [gballoc_malloc shall ensure thread safety by using the lock created by gballoc_Init.] */
239 TEST_FUNCTION(gballoc_malloc_with_0_Size_Calls_Underlying_malloc)
240 {
241 // arrange
242 void* result;
243 void* allocation;
244 gballoc_init();
245 umock_c_reset_all_calls();
246 allocation = malloc(OVERHEAD_SIZE);
247
248 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
249 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
250 EXPECTED_CALL(mock_calloc(1, 0))
251 .SetReturn(allocation);
252 /* This is the call to the underlying malloc with the size we want to allocate */
253 STRICT_EXPECTED_CALL(mock_malloc(0));
254 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
255
256 // act
257 result = gballoc_malloc(0);
258
259 // assert
260 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
261 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
262
263 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
264
265 // cleanup
266 gballoc_free(result);
267 free(allocation);
268 }
269
270 /* Tests_SRS_GBALLOC_01_003: [gballoc_malloc shall call the C99 malloc function and return its result.] */
271 /* Tests_SRS_GBALLOC_01_004: [If the underlying malloc call is successful, gballoc_malloc shall increment the total memory used with the amount indicated by size.] */
272 TEST_FUNCTION(gballoc_malloc_with_1_Size_Calls_Underlying_malloc_And_Increases_Max_Used)
273 {
274 // arrange
275 void* result;
276 void* allocation;
277 gballoc_init();
278 umock_c_reset_all_calls();
279 allocation = malloc(OVERHEAD_SIZE);
280
281 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
282 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
283 EXPECTED_CALL(mock_calloc(1, 0))
284 .SetReturn(allocation);
285 /* This is the call to the underlying malloc with the size we want to allocate */
286 STRICT_EXPECTED_CALL(mock_malloc(1));
287 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
288
289 // act
290 result = gballoc_malloc(1);
291
292 // assert
293 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
294 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
295 ASSERT_ARE_EQUAL(size_t, 1, gballoc_getMaximumMemoryUsed());
296
297 // cleanup
298 gballoc_free(result);
299 free(allocation);
300 }
301
302 /* Tests_SRS_GBALLOC_01_012: [When the underlying malloc call fails, gballoc_malloc shall return NULL and size should not be counted towards total memory used.] */
303 TEST_FUNCTION(When_malloc_Fails_Then_gballoc_malloc_fails_too)
304 {
305 // arrange
306 void* result;
307 void* allocation;
308 gballoc_init();
309 umock_c_reset_all_calls();
310 allocation = malloc(OVERHEAD_SIZE);
311
312 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
313 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
314 EXPECTED_CALL(mock_calloc(1, 0))
315 .SetReturn(allocation);
316 /* This is the call to the underlying malloc with the size we want to allocate */
317 STRICT_EXPECTED_CALL(mock_malloc(1))
318 .SetReturn((void*)NULL);
319 STRICT_EXPECTED_CALL(mock_free(allocation));
320 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
321
322 // act
323 result = gballoc_malloc(1);
324
325 // assert
326 ASSERT_IS_NULL(result);
327 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
328 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
329
330 // cleanup
331 free(allocation);
332 }
333
334 /* Tests_SRS_GBALLOC_01_013: [When gballoc_malloc fails allocating memory for its internal use, gballoc_malloc shall return NULL.] */
335 TEST_FUNCTION(When_allocating_memory_for_tracking_information_fails_Then_gballoc_malloc_fails_too)
336 {
337 // arrange
338 void* result;
339 gballoc_init();
340 umock_c_reset_all_calls();
341
342 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
343 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
344 EXPECTED_CALL(mock_calloc(1, 0))
345 .SetReturn((void*)NULL);
346 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
347
348 // act
349 result = gballoc_malloc(1);
350
351 // assert
352 ASSERT_IS_NULL(result);
353 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
354 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
355 }
356
357 /* Tests_SRS_GBALLOC_01_039: [If gballoc was not initialized gballoc_malloc shall simply call malloc without any memory tracking being performed.] */
358 TEST_FUNCTION(gballoc_malloc_after_deinit_calls_crt_malloc)
359 {
360 // arrange
361 void* result;
362 STRICT_EXPECTED_CALL(mock_malloc(1));
363
364 //act
365 result = gballoc_malloc(1);
366
367 //assert
368 ASSERT_IS_NOT_NULL(result);
369 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
370 }
371
372 /* gballoc_calloc */
373
374 /* Tests_SRS_GBALLOC_01_046: [If acquiring the lock fails, gballoc_calloc shall return NULL.] */
375 TEST_FUNCTION(when_acquiring_the_lock_fails_gballoc_calloc_fails)
376 {
377 // arrange
378 void* result;
379 gballoc_init();
380 umock_c_reset_all_calls();
381
382 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE))
383 .SetReturn(LOCK_ERROR);
384
385 // act
386 result = gballoc_calloc(1,1);
387
388 // assert
389 ASSERT_IS_NULL(result);
390 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
391 }
392
393 /* Tests_SRS_GBALLOC_01_020: [gballoc_calloc shall call the C99 calloc function and return its result.] */
394 /* Tests_SRS_GBALLOC_01_021: [If the underlying calloc call is successful, gballoc_calloc shall increment the total memory used with nmemb*size.] */
395 /* Tests_SRS_GBALLOC_01_031: [gballoc_calloc shall ensure thread safety by using the lock created by gballoc_Init] */
396 TEST_FUNCTION(gballoc_calloc_with_0_Size_And_ItemCount_Calls_Underlying_calloc)
397 {
398 // arrange
399 void* result;
400 void* allocation;
401 gballoc_init();
402 umock_c_reset_all_calls();
403 allocation = malloc(OVERHEAD_SIZE);
404
405 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
406 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
407 EXPECTED_CALL(mock_calloc(1, 0))
408 .SetReturn(allocation);
409 /* This is the call to the underlying malloc with the size we want to allocate */
410 STRICT_EXPECTED_CALL(mock_calloc(0, 0));
411 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
412
413 // act
414 result = gballoc_calloc(0, 0);
415
416 // assert
417 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
418 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
419 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
420
421 // cleanup
422 gballoc_free(result);
423 free(allocation);
424 }
425
426 /* Tests_SRS_GBALLOC_01_020: [gballoc_calloc shall call the C99 calloc function and return its result.] */
427 /* Tests_SRS_GBALLOC_01_021: [If the underlying calloc call is successful, gballoc_calloc shall increment the total memory used with nmemb*size.] */
428 TEST_FUNCTION(gballoc_calloc_with_1_Item_Of_1_Size_Calls_Underlying_malloc_And_Increases_Max_Used)
429 {
430 // arrange
431 void* result;
432 void* allocation;
433 gballoc_init();
434 umock_c_reset_all_calls();
435 allocation = malloc(OVERHEAD_SIZE);
436
437 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
438 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
439 EXPECTED_CALL(mock_calloc(1, 0))
440 .SetReturn(allocation);
441 /* This is the call to the underlying malloc with the size we want to allocate */
442 STRICT_EXPECTED_CALL(mock_calloc(1, 1));
443 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
444
445 // act
446 result = gballoc_calloc(1, 1);
447
448 // assert
449 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
450 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
451 ASSERT_ARE_EQUAL(size_t, 1, gballoc_getMaximumMemoryUsed());
452
453 // cleanup
454 gballoc_free(result);
455 free(allocation);
456 }
457
458 /* Tests_SRS_GBALLOC_01_020: [gballoc_calloc shall call the C99 calloc function and return its result.] */
459 /* Tests_SRS_GBALLOC_01_021: [If the underlying calloc call is successful, gballoc_calloc shall increment the total memory used with nmemb*size.] */
460 TEST_FUNCTION(gballoc_calloc_with_1_Item_Of_0_Size_Calls_Underlying_malloc_And_Does_Not_Increase_Max_Used)
461 {
462 // arrange
463 void* result;
464 void* allocation;
465 gballoc_init();
466 umock_c_reset_all_calls();
467 allocation = malloc(OVERHEAD_SIZE);
468
469 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
470 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
471 EXPECTED_CALL(mock_calloc(1, 0))
472 .SetReturn(allocation);
473 /* This is the call to the underlying malloc with the size we want to allocate */
474 STRICT_EXPECTED_CALL(mock_calloc(1, 0));
475 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
476
477 // act
478 result = gballoc_calloc(1, 0);
479
480 // assert
481 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
482 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
483 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
484
485 // cleanup
486 gballoc_free(result);
487 free(allocation);
488 }
489
490 /* Tests_SRS_GBALLOC_01_020: [gballoc_calloc shall call the C99 calloc function and return its result.] */
491 /* Tests_SRS_GBALLOC_01_021: [If the underlying calloc call is successful, gballoc_calloc shall increment the total memory used with nmemb*size.] */
492 TEST_FUNCTION(gballoc_calloc_with_0_Items_Of_1_Size_Calls_Underlying_malloc_And_Does_Not_Increase_Max_Used)
493 {
494 // arrange
495 void* result;
496 void* allocation;
497 gballoc_init();
498 umock_c_reset_all_calls();
499 allocation = malloc(OVERHEAD_SIZE);
500
501 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
502 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
503 EXPECTED_CALL(mock_calloc(1, 0))
504 .SetReturn(allocation);
505 /* This is the call to the underlying malloc with the size we want to allocate */
506 STRICT_EXPECTED_CALL(mock_calloc(0, 1));
507 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
508
509 // act
510 result = gballoc_calloc(0, 1);
511
512 // assert
513 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
514 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
515 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
516
517 // cleanup
518 gballoc_free(result);
519 free(allocation);
520 }
521
522 /* Tests_SRS_GBALLOC_01_020: [gballoc_calloc shall call the C99 calloc function and return its result.] */
523 /* Tests_SRS_GBALLOC_01_021: [If the underlying calloc call is successful, gballoc_calloc shall increment the total memory used with nmemb*size.] */
524 TEST_FUNCTION(gballoc_calloc_with_42_Items_Of_2_Size_Calls_Underlying_malloc_And_Increases_Max_Size)
525 {
526 // arrange
527 void* result;
528 void* allocation;
529 gballoc_init();
530 umock_c_reset_all_calls();
531 allocation = malloc(OVERHEAD_SIZE);
532
533 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
534 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
535 EXPECTED_CALL(mock_calloc(1, 0))
536 .SetReturn(allocation);
537 /* This is the call to the underlying malloc with the size we want to allocate */
538 STRICT_EXPECTED_CALL(mock_calloc(42, 2));
539 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
540
541 // act
542 result = gballoc_calloc(42, 2);
543
544 // assert
545 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
546 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
547 ASSERT_ARE_EQUAL(size_t, 84, gballoc_getMaximumMemoryUsed());
548
549 // cleanup
550 gballoc_free(result);
551 free(allocation);
552 }
553
554 /* Tests_SRS_GBALLOC_01_022: [When the underlying calloc call fails, gballoc_calloc shall return NULL and size should not be counted towards total memory used.] */
555 TEST_FUNCTION(When_calloc_Fails_Then_gballoc_calloc_fails_too)
556 {
557 // arrange
558 void* result;
559 void* allocation;
560 gballoc_init();
561 umock_c_reset_all_calls();
562 allocation = malloc(OVERHEAD_SIZE);
563
564 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
565 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
566 EXPECTED_CALL(mock_calloc(1, 0))
567 .SetReturn(allocation);
568 /* This is the call to the underlying malloc with the size we want to allocate */
569 STRICT_EXPECTED_CALL(mock_calloc(1, 1))
570 .SetReturn((void*)NULL);
571 STRICT_EXPECTED_CALL(mock_free(allocation));
572 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
573
574 // act
575 result = gballoc_calloc(1, 1);
576
577 // assert
578 ASSERT_IS_NULL(result);
579 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
580 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
581
582 // cleanup
583 free(allocation);
584 }
585
586 /* Tests_SRS_GBALLOC_01_023: [When gballoc_calloc fails allocating memory for its internal use, gballoc_calloc shall return NULL.] */
587 TEST_FUNCTION(When_allocating_memory_for_tracking_information_fails_Then_gballoc_calloc_fails_too)
588 {
589 // arrange
590 void* result;
591 gballoc_init();
592 umock_c_reset_all_calls();
593
594 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
595 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
596 EXPECTED_CALL(mock_calloc(1, 0))
597 .SetReturn((void*)NULL);
598 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
599
600 // act
601 result = gballoc_calloc(1, 1);
602
603 // assert
604 ASSERT_IS_NULL(result);
605 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
606 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
607 }
608
609 /* Tests_SRS_GBALLOC_01_040: [If gballoc was not initialized gballoc_calloc shall simply call calloc without any memory tracking being performed.] */
610 TEST_FUNCTION(gballoc_calloc_after_deinit_calls_crt_calloc)
611 {
612 // arrange
613 void* result;
614 STRICT_EXPECTED_CALL(mock_calloc(1, 1));
615
616 // act
617 result = gballoc_calloc(1, 1);
618
619 // assert
620 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
621 ASSERT_IS_NOT_NULL(result);
622 }
623
624
625 /* gballoc_realloc */
626
627 /* Tests_SRS_GBALLOC_01_047: [If acquiring the lock fails, gballoc_realloc shall return NULL.]*/
628 TEST_FUNCTION(when_acquiring_the_lock_fails_gballoc_realloc_fails)
629 {
630 // arrange
631 void* result;
632 gballoc_init();
633 umock_c_reset_all_calls();
634
635 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE))
636 .SetReturn(LOCK_ERROR);
637
638 // act
639 result = gballoc_realloc(NULL, 1);
640
641 // assert
642 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
643 ASSERT_IS_NULL(result);
644 }
645
646 /* Tests_SRS_GBALLOC_01_041: [If gballoc was not initialized gballoc_realloc shall shall simply call realloc without any memory tracking being performed.] */
647 TEST_FUNCTION(gballoc_realloc_after_deinit_fails)
648 {
649 // arrange
650 void* result;
651 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1));
652
653 // act
654 result = gballoc_realloc(NULL, 1);
655
656 // assert
657 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
658 ASSERT_IS_NOT_NULL(result);
659 }
660
661 /* Tests_SRS_GBALLOC_01_005: [gballoc_realloc shall call the C99 realloc function and return its result.] */
662 /* Tests_SRS_GBALLOC_01_017: [When ptr is NULL, gballoc_realloc shall call the underlying realloc with ptr being NULL and the realloc result shall be tracked by gballoc.] */
663 /* Tests_SRS_GBALLOC_01_032: [gballoc_realloc shall ensure thread safety by using the lock created by gballoc_Init.] */
664 TEST_FUNCTION(gballoc_realloc_with_NULL_Arg_And_0_Size_Calls_Underlying_realloc)
665 {
666 // arrange
667 void* result;
668 void* allocation;
669 gballoc_init();
670 umock_c_reset_all_calls();
671 allocation = malloc(OVERHEAD_SIZE);
672
673 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
674 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
675 EXPECTED_CALL(mock_calloc(1, 0))
676 .SetReturn(allocation);
677 /* This is the call to the underlying malloc with the size we want to allocate */
678 STRICT_EXPECTED_CALL(mock_realloc(NULL, 0));
679 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
680
681 // act
682 result = gballoc_realloc(NULL, 0);
683
684 // assert
685 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
686 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
687 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
688
689 // cleanup
690 gballoc_free(result);
691 free(allocation);
692 }
693
694 /* Tests_SRS_GBALLOC_01_005: [gballoc_realloc shall call the C99 realloc function and return its result.] */
695 /* Tests_SRS_GBALLOC_01_017: [When ptr is NULL, gballoc_realloc shall call the underlying realloc with ptr being NULL and the realloc result shall be tracked by gballoc.] */
696 TEST_FUNCTION(gballoc_realloc_with_NULL_Arg_And_1_Size_Calls_Underlying_realloc)
697 {
698 // arrange
699 void* result;
700 void* allocation;
701 gballoc_init();
702 umock_c_reset_all_calls();
703 allocation = malloc(OVERHEAD_SIZE);
704
705 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
706 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
707 EXPECTED_CALL(mock_calloc(1, 0))
708 .SetReturn(allocation);
709 /* This is the call to the underlying malloc with the size we want to allocate */
710 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1));
711 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
712
713 // act
714 result = gballoc_realloc(NULL, 1);
715
716 // assert
717 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR1, result);
718 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
719 ASSERT_ARE_EQUAL(size_t, 1, gballoc_getMaximumMemoryUsed());
720
721 // cleanup
722 gballoc_free(result);
723 free(allocation);
724 }
725
726 /* Tests_SRS_GBALLOC_01_005: [gballoc_realloc shall call the C99 realloc function and return its result.] */
727 /* Tests_SRS_GBALLOC_01_006: [If the underlying realloc call is successful, gballoc_realloc shall look up the size associated with the pointer ptr and decrease the total memory used with that size.] */
728 TEST_FUNCTION(gballoc_realloc_with_Previous_1_Byte_Block_Ptr_And_2_Size_Calls_Underlying_realloc_And_Increases_Max_Used_Memory)
729 {
730 // arrange
731 void* result;
732 void* allocation;
733 gballoc_init();
734 umock_c_reset_all_calls();
735 allocation = malloc(OVERHEAD_SIZE);
736
737 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
738 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
739 EXPECTED_CALL(mock_calloc(1, 0))
740 .SetReturn(allocation);
741 /* This is the call to the underlying malloc with the size we want to allocate */
742 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1));
743 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
744 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
745 STRICT_EXPECTED_CALL(mock_realloc(TEST_ALLOC_PTR1, 2))
746 .SetReturn(TEST_ALLOC_PTR2);
747 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
748
749 result = gballoc_realloc(NULL, 1);
750
751 // act
752 result = gballoc_realloc(result, 2);
753
754 // assert
755 ASSERT_ARE_EQUAL(void_ptr, TEST_ALLOC_PTR2, result);
756 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
757 ASSERT_ARE_EQUAL(size_t, 2, gballoc_getMaximumMemoryUsed());
758
759 // cleanup
760 gballoc_free(result);
761 free(allocation);
762 }
763
764 /* Tests_SRS_GBALLOC_01_014: [When the underlying realloc call fails, gballoc_realloc shall return NULL and no change should be made to the counted total memory usage.] */
765 TEST_FUNCTION(When_realloc_fails_then_gballoc_realloc_Fails_Too_And_No_Change_Is_Made_To_Memory_Counters)
766 {
767 // arrange
768 void* result;
769 void* allocation;
770 gballoc_init();
771 umock_c_reset_all_calls();
772 allocation = malloc(OVERHEAD_SIZE);
773
774 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
775 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
776 EXPECTED_CALL(mock_calloc(1, 0))
777 .SetReturn(allocation);
778 /* This is the call to the underlying malloc with the size we want to allocate */
779 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1));
780 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
781 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
782 STRICT_EXPECTED_CALL(mock_realloc(TEST_ALLOC_PTR1, 2))
783 .SetReturn((void*)NULL);
784 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
785
786 result = gballoc_realloc(NULL, 1);
787
788 // act
789 result = gballoc_realloc(result, 2);
790
791 // assert
792 ASSERT_IS_NULL(result);
793 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
794 ASSERT_ARE_EQUAL(size_t, 1, gballoc_getMaximumMemoryUsed());
795
796 // cleanup
797 gballoc_free(TEST_ALLOC_PTR1);
798 free(allocation);
799 }
800
801 /* Tests_SRS_GBALLOC_01_015: [When allocating memory used for tracking by gballoc_realloc fails, gballoc_realloc shall return NULL and no change should be made to the counted total memory usage.] */
802 TEST_FUNCTION(When_Allocating_Memory_For_tracking_fails_gballoc_realloc_fails)
803 {
804 // arrange
805 void* result;
806 gballoc_init();
807 umock_c_reset_all_calls();
808
809 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
810 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
811 EXPECTED_CALL(mock_calloc(1, 0))
812 .SetReturn((void*)NULL);
813 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
814
815 // act
816 result = gballoc_realloc(NULL, 1);
817
818 // assert
819 ASSERT_IS_NULL(result);
820 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
821 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
822 }
823
824 /* Tests_SRS_GBALLOC_01_016: [When the ptr pointer cannot be found in the pointers tracked by gballoc, gballoc_realloc shall return NULL and the underlying realloc shall not be called.] */
825 TEST_FUNCTION(When_The_Pointer_Is_Not_Tracked_gballoc_realloc_Returns_NULL)
826 {
827 // arrange
828 void* result1;
829 void* result2;
830 void* allocation;
831 gballoc_init();
832 umock_c_reset_all_calls();
833 allocation = malloc(OVERHEAD_SIZE);
834
835 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
836 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
837 EXPECTED_CALL(mock_calloc(1, 0))
838 .SetReturn(allocation);
839 /* This is the call to the underlying malloc with the size we want to allocate */
840 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1));
841 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
842 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
843 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
844
845 result1 = gballoc_realloc(NULL, 1);
846
847 // act
848 result2 = gballoc_realloc(TEST_REALLOC_PTR, 2);
849
850 // assert
851 ASSERT_IS_NULL(result2);
852 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
853 ASSERT_ARE_EQUAL(size_t, 1, gballoc_getMaximumMemoryUsed());
854
855 // cleanup
856 gballoc_free(result1);
857 free(allocation);
858 }
859
860 /* Tests_SRS_GBALLOC_01_014: [When the underlying realloc call fails, gballoc_realloc shall return NULL and no change should be made to the counted total memory usage.] */
861 TEST_FUNCTION(When_ptr_is_null_and_the_underlying_realloc_fails_then_the_memory_used_for_tracking_is_freed)
862 {
863 // arrange
864 void* result;
865 void* allocation;
866 gballoc_init();
867 umock_c_reset_all_calls();
868 allocation = malloc(OVERHEAD_SIZE);
869
870 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
871 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
872 EXPECTED_CALL(mock_calloc(1, 0))
873 .SetReturn(allocation);
874 /* This is the call to the underlying malloc with the size we want to allocate */
875 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1))
876 .SetReturn((void*)NULL);
877 STRICT_EXPECTED_CALL(mock_free(allocation));
878 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
879
880 // act
881 result = gballoc_realloc(NULL, 1);
882
883 // assert
884 ASSERT_IS_NULL(result);
885 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
886 ASSERT_ARE_EQUAL(size_t, 0, gballoc_getMaximumMemoryUsed());
887
888 // cleanup
889 free(allocation);
890 }
891
892 /* gballoc_free */
893
894 /* Tests_SRS_GBALLOC_01_042: [If gballoc was not initialized gballoc_free shall shall simply call free.] */
895 TEST_FUNCTION(gballoc_free_after_deinit_calls_crt_free)
896 {
897 // arrange
898 STRICT_EXPECTED_CALL(mock_free((void*)0x4242));
899
900 // act
901 gballoc_free((void*)0x4242);
902
903 // assert
904 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
905 }
906
907 /* Tests_SRS_GBALLOC_01_049: [If acquiring the lock fails, gballoc_free shall do nothing.] */
908 TEST_FUNCTION(when_acquiring_the_lock_fails_then_gballoc_free_does_nothing)
909 {
910 // arrange
911 void* block;
912 void* allocation;
913 gballoc_init();
914 umock_c_reset_all_calls();
915 allocation = malloc(OVERHEAD_SIZE);
916
917 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
918 EXPECTED_CALL(mock_calloc(1, 0))
919 .SetReturn(allocation);
920 /* This is the call to the underlying malloc with the size we want to allocate */
921 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1));
922 block = gballoc_realloc(NULL, 1);
923 umock_c_reset_all_calls();
924
925 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE))
926 .SetReturn(LOCK_ERROR);
927
928 // act
929 gballoc_free(block);
930
931 // assert
932 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
933
934 // cleanup
935 gballoc_free(block);
936 free(allocation);
937 }
938
939 /* Tests_SRS_GBALLOC_01_008: [gballoc_free shall call the C99 free function.] */
940 /* Tests_SRS_GBALLOC_01_009: [gballoc_free shall also look up the size associated with the ptr pointer and decrease the total memory used with the associated size amount.] */
941 /* Tests_SRS_GBALLOC_01_010: [gballoc_getMaximumMemoryUsed shall return the maximum amount of total memory used recorded since the module initialization.] */
942 /* Tests_SRS_GBALLOC_01_011: [The maximum total memory used shall be the maximum of the total memory used at any point.] */
943 /* Tests_SRS_GBALLOC_01_033: [gballoc_free shall ensure thread safety by using the lock created by gballoc_Init.] */
944 TEST_FUNCTION(gballoc_free_calls_the_underlying_free)
945 {
946 // arrange
947 void* block;
948 void* allocation;
949 gballoc_init();
950 umock_c_reset_all_calls();
951 allocation = malloc(OVERHEAD_SIZE);
952
953 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
954 EXPECTED_CALL(mock_calloc(1, 0))
955 .SetReturn(allocation);
956 /* This is the call to the underlying malloc with the size we want to allocate */
957 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1));
958 block = gballoc_realloc(NULL, 1);
959 umock_c_reset_all_calls();
960
961 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
962 STRICT_EXPECTED_CALL(mock_free(TEST_ALLOC_PTR1));
963 STRICT_EXPECTED_CALL(mock_free(allocation));
964 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
965
966 // act
967 gballoc_free(block);
968
969 // assert
970 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
971 ASSERT_ARE_EQUAL(size_t, 1, gballoc_getMaximumMemoryUsed());
972
973 // cleanup
974 free(allocation);
975 }
976
977 /* Tests_SRS_GBALLOC_01_008: [gballoc_free shall call the C99 free function.] */
978 /* Tests_SRS_GBALLOC_01_009: [gballoc_free shall also look up the size associated with the ptr pointer and decrease the total memory used with the associated size amount.] */
979 /* Tests_SRS_GBALLOC_01_010: [gballoc_getMaximumMemoryUsed shall return the maximum amount of total memory used recorded since the module initialization.] */
980 /* Tests_SRS_GBALLOC_01_010: [gballoc_getMaximumMemoryUsed shall return the maximum amount of total memory used recorded since the module initialization.] */
981 /* Tests_SRS_GBALLOC_01_011: [The maximum total memory used shall be the maximum of the total memory used at any point.] */
982 TEST_FUNCTION(gballoc_malloc_free_2_times_with_1_byte_yields_1_byte_as_max)
983 {
984 // arrange
985 void* block;
986 void* allocation;
987 gballoc_init();
988 umock_c_reset_all_calls();
989 allocation = malloc(OVERHEAD_SIZE);
990
991 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
992 EXPECTED_CALL(mock_calloc(1, 0))
993 .SetReturn(allocation);
994 STRICT_EXPECTED_CALL(mock_realloc(NULL, 1));
995 block = gballoc_realloc(NULL, 1);
996 gballoc_free(block);
997
998 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
999 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1000 EXPECTED_CALL(mock_calloc(1, 0))
1001 .SetReturn(allocation);
1002 block = gballoc_realloc(NULL, 1);
1003 umock_c_reset_all_calls();
1004
1005 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1006 STRICT_EXPECTED_CALL(mock_free(TEST_ALLOC_PTR1));
1007 STRICT_EXPECTED_CALL(mock_free(allocation));
1008 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1009
1010 // act
1011 gballoc_free(block);
1012
1013 // assert
1014 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1015 ASSERT_ARE_EQUAL(size_t, 1, gballoc_getMaximumMemoryUsed());
1016
1017 // cleanup
1018 free(allocation);
1019 }
1020
1021 /* Tests_SRS_GBALLOC_01_019:[When the ptr pointer cannot be found in the pointers tracked by gballoc, gballoc_free shall not free any memory.] */
1022 TEST_FUNCTION(gballoc_free_with_an_untracked_pointer_does_not_alter_total_memory_used)
1023 {
1024 // arrange
1025 void* allocation;
1026 gballoc_init();
1027 umock_c_reset_all_calls();
1028 allocation = malloc(OVERHEAD_SIZE);
1029
1030 /* don't quite like this, but I'm unsure I want to invest more in this memory counting */
1031 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1032 EXPECTED_CALL(mock_calloc(1, 0))
1033 .SetReturn(allocation);
1034
1035 gballoc_realloc(NULL, 1);
1036 umock_c_reset_all_calls();
1037
1038 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1039 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1040
1041 // act
1042 gballoc_free(TEST_REALLOC_PTR);
1043
1044 // assert
1045 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1046 gballoc_realloc(TEST_ALLOC_PTR1, 2);
1047 ASSERT_ARE_EQUAL(size_t, 2, gballoc_getMaximumMemoryUsed());
1048
1049 // cleanup
1050 gballoc_free(TEST_ALLOC_PTR1);
1051 free(allocation);
1052 }
1053
1054 /* gballoc_getMaximumMemoryUsed */
1055
1056
1057 /* Tests_SRS_GBALLOC_01_034: [gballoc_getMaximumMemoryUsed shall ensure thread safety by using the lock created by gballoc_Init.] */
1058 TEST_FUNCTION(when_gballoc_getMaximumMemoryUsed_called_It_Shall_Lock_And_Unlock)
1059 {
1060 // arrange
1061 gballoc_init();
1062 umock_c_reset_all_calls();
1063
1064 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1065 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1066 // act
1067 (void)gballoc_getMaximumMemoryUsed();
1068
1069 // assert
1070 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1071 }
1072
1073 /* Tests_SRS_GBALLOC_01_050: [If the lock cannot be acquired, gballoc_getMaximumMemoryUsed shall return SIZE_MAX.] */
1074 TEST_FUNCTION(when_acquiring_the_lock_fails_then_gballoc_getMaximumMemoryUsed_fails)
1075 {
1076 // arrange
1077 size_t result;
1078 gballoc_init();
1079 umock_c_reset_all_calls();
1080
1081 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE))
1082 .SetReturn(LOCK_ERROR);
1083
1084 // act
1085 result = gballoc_getMaximumMemoryUsed();
1086
1087 // assert
1088 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1089 ASSERT_ARE_EQUAL(size_t, SIZE_MAX, result);
1090 }
1091
1092 /* Tests_SRS_GBALLOC_01_038: [If gballoc was not initialized gballoc_getMaximumMemoryUsed shall return MAX_INT_SIZE.] */
1093 TEST_FUNCTION(gballoc_getMaximumMemoryUsed_after_deinit_fails)
1094 {
1095 // arrange
1096
1097 // act
1098 size_t result = gballoc_getMaximumMemoryUsed();
1099
1100 // assert
1101 ASSERT_ARE_EQUAL(size_t, SIZE_MAX, result);
1102 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1103 }
1104
1105 /* gballoc_getCurrentMemoryUsed */
1106
1107 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1108 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_1_byte_malloc_returns_1)
1109 {
1110 // arrange
1111 void* allocation;
1112 void* toBeFreed;
1113 size_t result;
1114 gballoc_init();
1115 umock_c_reset_all_calls();
1116
1117 allocation = malloc(OVERHEAD_SIZE);
1118
1119 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1120 EXPECTED_CALL(mock_calloc(1, 0))
1121 .SetReturn(allocation);
1122 STRICT_EXPECTED_CALL(mock_malloc(1));
1123 toBeFreed = gballoc_malloc(1);
1124 umock_c_reset_all_calls();
1125 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1126 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1127
1128 // act
1129 result = gballoc_getCurrentMemoryUsed();
1130
1131 // assert
1132 ASSERT_ARE_EQUAL(size_t, 1, result);
1133 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1134
1135 ///cleanup
1136 gballoc_free(toBeFreed);
1137 free(allocation);
1138 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1139 }
1140
1141 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1142 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_2x3_byte_calloc_returns_6)
1143 {
1144 // arrange
1145 void* allocation;
1146 void* toBeFreed;
1147 size_t result;
1148 gballoc_init();
1149 umock_c_reset_all_calls();
1150 allocation = malloc(OVERHEAD_SIZE);
1151
1152 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1153 EXPECTED_CALL(mock_calloc(1, 0))
1154 .SetReturn(allocation);
1155 toBeFreed = gballoc_calloc(2, 3);
1156 umock_c_reset_all_calls();
1157 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1158 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1159
1160 // act
1161 result = gballoc_getCurrentMemoryUsed();
1162
1163 // assert
1164 ASSERT_ARE_EQUAL(size_t, 6, result);
1165 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1166
1167 ///cleanup
1168 gballoc_free(toBeFreed);
1169 free(allocation);
1170 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1171 }
1172
1173 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1174 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_1_byte_malloc_and_3_bytes_realloc_returns_3)
1175 {
1176 // arrange
1177 void* allocation;
1178 void* toBeFreed;
1179 size_t result;
1180 gballoc_init();
1181 umock_c_reset_all_calls();
1182 allocation = malloc(OVERHEAD_SIZE);
1183
1184 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1185 EXPECTED_CALL(mock_calloc(1, 0))
1186 .SetReturn(allocation);
1187 STRICT_EXPECTED_CALL(mock_malloc(1));
1188 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1189
1190 toBeFreed = gballoc_malloc(1);
1191 toBeFreed = gballoc_realloc(toBeFreed, 3);
1192 umock_c_reset_all_calls();
1193 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1194 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1195
1196 // act
1197 result = gballoc_getCurrentMemoryUsed();
1198
1199 // assert
1200 ASSERT_ARE_EQUAL(size_t, 3, result);
1201 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1202
1203 ///cleanup
1204 gballoc_free(toBeFreed);
1205 free(allocation);
1206 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1207 }
1208
1209 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1210 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_1_byte_malloc_and_free_returns_0)
1211 {
1212 // arrange
1213 void* allocation;
1214 void* toBeFreed;
1215 size_t result;
1216 gballoc_init();
1217 umock_c_reset_all_calls();
1218 allocation = malloc(OVERHEAD_SIZE);
1219
1220 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1221 EXPECTED_CALL(mock_calloc(1, 0))
1222 .SetReturn(allocation);
1223 STRICT_EXPECTED_CALL(mock_malloc(1));
1224 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1225
1226 toBeFreed = gballoc_malloc(1);
1227 gballoc_free(toBeFreed);
1228 umock_c_reset_all_calls();
1229 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1230 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1231
1232 // act
1233 result = gballoc_getCurrentMemoryUsed();
1234
1235 // assert
1236 ASSERT_ARE_EQUAL(size_t, 0, result);
1237 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1238
1239 ///cleanup
1240 free(allocation);
1241 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1242 }
1243
1244 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1245 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_2x3_byte_calloc_and_free_returns_0)
1246 {
1247 // arrange
1248 void* allocation;
1249 void* toBeFreed;
1250 size_t result;
1251 gballoc_init();
1252 umock_c_reset_all_calls();
1253 allocation = malloc(OVERHEAD_SIZE);
1254
1255 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1256 EXPECTED_CALL(mock_calloc(1, 0))
1257 .SetReturn(allocation);
1258 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1259
1260 toBeFreed = gballoc_calloc(2, 3);
1261 gballoc_free(toBeFreed);
1262 umock_c_reset_all_calls();
1263 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1264 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1265
1266 // act
1267 result = gballoc_getCurrentMemoryUsed();
1268
1269 // assert
1270 ASSERT_ARE_EQUAL(size_t, 0, result);
1271 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1272
1273 ///cleanup
1274 free(allocation);
1275 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1276 }
1277
1278 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1279 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_realloc_and_free_returns_0)
1280 {
1281 // arrange
1282 void* allocation;
1283 void* toBeFreed;
1284 size_t result;
1285 gballoc_init();
1286 umock_c_reset_all_calls();
1287 allocation = malloc(OVERHEAD_SIZE);
1288
1289 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1290 EXPECTED_CALL(mock_calloc(1, 0))
1291 .SetReturn(allocation);
1292 STRICT_EXPECTED_CALL(mock_malloc(1));
1293 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1294
1295 toBeFreed = gballoc_malloc(1);
1296 toBeFreed = gballoc_realloc(toBeFreed, 3);
1297 gballoc_free(toBeFreed);
1298 umock_c_reset_all_calls();
1299 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1300 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1301
1302 // act
1303 result = gballoc_getCurrentMemoryUsed();
1304
1305 // assert
1306 ASSERT_ARE_EQUAL(size_t, 0, result);
1307 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1308
1309 ///cleanup
1310 free(allocation);
1311 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1312 }
1313
1314 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1315 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_1_byte_malloc_and_1_byte_malloc_returns_2)
1316 {
1317 // arrange
1318 void* allocation1;
1319 void* allocation2;
1320 void* toBeFreed1;
1321 void* toBeFreed2;
1322 size_t result;
1323 gballoc_init();
1324 umock_c_reset_all_calls();
1325
1326 allocation1 = malloc(OVERHEAD_SIZE);
1327 allocation2 = malloc(OVERHEAD_SIZE);
1328
1329 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1330 EXPECTED_CALL(mock_calloc(1, 0))
1331 .SetReturn(allocation1);
1332 STRICT_EXPECTED_CALL(mock_malloc(1));
1333 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1334 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1335 EXPECTED_CALL(mock_calloc(1, 0))
1336 .SetReturn(allocation2);
1337 STRICT_EXPECTED_CALL(mock_malloc(1));
1338 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1339
1340 toBeFreed1 = gballoc_malloc(1);
1341 toBeFreed2 = gballoc_malloc(1);
1342 umock_c_reset_all_calls();
1343 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1344 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1345
1346 // act
1347 result = gballoc_getCurrentMemoryUsed();
1348
1349 // assert
1350 ASSERT_ARE_EQUAL(size_t, 2, result);
1351 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1352
1353 ///cleanup
1354 gballoc_free(toBeFreed1);
1355 gballoc_free(toBeFreed2);
1356 free(allocation1);
1357 free(allocation2);
1358 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1359 }
1360
1361 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1362 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_1_byte_malloc_and_1_byte_malloc_and_3_realloc_returns_4)
1363 {
1364 // arrange
1365 void* allocation1;
1366 void* allocation2;
1367 void* toBeFreed1;
1368 void* toBeFreed2;
1369 size_t result;
1370 gballoc_init();
1371 umock_c_reset_all_calls();
1372
1373 allocation1 = malloc(OVERHEAD_SIZE);
1374 allocation2 = malloc(OVERHEAD_SIZE);
1375
1376 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1377 EXPECTED_CALL(mock_calloc(1, 0))
1378 .SetReturn(allocation1);
1379 STRICT_EXPECTED_CALL(mock_malloc(1));
1380 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1381 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1382 EXPECTED_CALL(mock_calloc(1, 0))
1383 .SetReturn(allocation2);
1384 STRICT_EXPECTED_CALL(mock_malloc(1));
1385 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1386
1387 toBeFreed1 = gballoc_malloc(1);
1388 toBeFreed2 = gballoc_malloc(1);
1389 toBeFreed2 = gballoc_realloc(toBeFreed2, 3);
1390 umock_c_reset_all_calls();
1391 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1392 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1393
1394 // act
1395 result = gballoc_getCurrentMemoryUsed();
1396
1397 // assert
1398 ASSERT_ARE_EQUAL(size_t, 4, result);
1399 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1400
1401 ///cleanup
1402 gballoc_free(toBeFreed1);
1403 gballoc_free(toBeFreed2);
1404 free(allocation1);
1405 free(allocation2);
1406 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1407 }
1408
1409 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1410 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_1_byte_malloc_and_6_byte_calloc_returns_7)
1411 {
1412 // arrange
1413 void* allocation1;
1414 void* allocation2;
1415 void* toBeFreed1;
1416 void* toBeFreed2;
1417 size_t result;
1418 gballoc_init();
1419 umock_c_reset_all_calls();
1420
1421 allocation1 = malloc(OVERHEAD_SIZE);
1422 allocation2 = malloc(OVERHEAD_SIZE);
1423
1424 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1425 EXPECTED_CALL(mock_calloc(1, 0))
1426 .SetReturn(allocation1);
1427 STRICT_EXPECTED_CALL(mock_malloc(1));
1428 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1429 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1430 EXPECTED_CALL(mock_calloc(1, 0))
1431 .SetReturn(allocation2);
1432 STRICT_EXPECTED_CALL(mock_calloc(2, 3));
1433 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1434
1435 toBeFreed1 = gballoc_malloc(1);
1436 toBeFreed2 = gballoc_calloc(2, 3);
1437 umock_c_reset_all_calls();
1438 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1439 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1440
1441 // act
1442 result = gballoc_getCurrentMemoryUsed();
1443
1444 // assert
1445 ASSERT_ARE_EQUAL(size_t, 7, result);
1446 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1447
1448 ///cleanup
1449 gballoc_free(toBeFreed1);
1450 gballoc_free(toBeFreed2);
1451 free(allocation1);
1452 free(allocation2);
1453 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1454 }
1455
1456 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1457 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_1_byte_malloc_and_6_byte_calloc_and_free_returns_6)
1458 {
1459 // arrange
1460 void* allocation1;
1461 void* allocation2;
1462 void* toBeFreed1;
1463 void* toBeFreed2;
1464 size_t result;
1465 gballoc_init();
1466 umock_c_reset_all_calls();
1467 allocation1 = malloc(OVERHEAD_SIZE);
1468 allocation2 = malloc(OVERHEAD_SIZE);
1469
1470 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1471 EXPECTED_CALL(mock_calloc(1, 0))
1472 .SetReturn(allocation1);
1473 STRICT_EXPECTED_CALL(mock_malloc(1))
1474 .SetReturn((char*)allocation1 + OVERHEAD_SIZE / 2); /*somewhere in the middle of allocation*/
1475 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1476 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1477 EXPECTED_CALL(mock_calloc(1, 0))
1478 .SetReturn(allocation2);
1479 STRICT_EXPECTED_CALL(mock_calloc(2, 3))
1480 .SetReturn((char*)allocation2 + OVERHEAD_SIZE / 2);
1481 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1482
1483 toBeFreed1 = gballoc_malloc(1);
1484 toBeFreed2 = gballoc_calloc(2, 3);
1485 gballoc_free(toBeFreed1);
1486 umock_c_reset_all_calls();
1487 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1488 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1489
1490 // act
1491 result = gballoc_getCurrentMemoryUsed();
1492
1493 // assert
1494 ASSERT_ARE_EQUAL(size_t, 6, result);
1495 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1496
1497 ///cleanup
1498 gballoc_free(toBeFreed2);
1499 free(allocation1);
1500 free(allocation2);
1501 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1502 }
1503
1504 /*Tests_SRS_GBALLOC_02_001: [gballoc_getCurrentMemoryUsed shall return the currently used memory size.] */
1505 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_1_byte_malloc_and_1_byte_malloc_and_3_realloc_and_free_returns_3)
1506 {
1507 // arrange
1508 void* allocation1;
1509 void* allocation2;
1510 void* toBeFreed1;
1511 void* toBeFreed2;
1512 size_t result;
1513 gballoc_init();
1514 umock_c_reset_all_calls();
1515 allocation1 = malloc(OVERHEAD_SIZE);
1516 allocation2 = malloc(OVERHEAD_SIZE);
1517
1518 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1519 EXPECTED_CALL(mock_calloc(1, 0))
1520 .SetReturn(allocation1);
1521 STRICT_EXPECTED_CALL(mock_malloc(1))
1522 .SetReturn((char*)allocation1 + OVERHEAD_SIZE / 2); /*somewhere in the middle of allocation*/
1523 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1524 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1525 EXPECTED_CALL(mock_calloc(1, 0))
1526 .SetReturn(allocation2);
1527 STRICT_EXPECTED_CALL(mock_realloc(IGNORED_PTR_ARG, 3))
1528 .IgnoreArgument(1)
1529 .SetReturn((char*)allocation2 + OVERHEAD_SIZE / 2); /*somewhere in the middle of allocation*/
1530 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1531
1532 toBeFreed1 = gballoc_malloc(1);
1533 toBeFreed2 = gballoc_malloc(1);
1534 toBeFreed2 = gballoc_realloc(toBeFreed2, 3);
1535 gballoc_free(toBeFreed1);
1536 umock_c_reset_all_calls();
1537 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1538 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1539
1540 // act
1541 result = gballoc_getCurrentMemoryUsed();
1542
1543 // assert
1544 ASSERT_ARE_EQUAL(size_t, 3, result);
1545 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1546
1547 ///cleanup
1548 gballoc_free(toBeFreed2);
1549 free(allocation1);
1550 free(allocation2);
1551 umock_c_reset_all_calls(); //this is just for mathematics, not for functionality
1552 }
1553
1554 /* Tests_SRS_GBALLOC_01_036: [gballoc_getCurrentMemoryUsed shall ensure thread safety by using the lock created by gballoc_Init.] */
1555 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_locks_and_unlocks)
1556 {
1557 // assert
1558 size_t result;
1559 gballoc_init();
1560 umock_c_reset_all_calls();
1561
1562 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1563 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1564
1565 // act
1566 result = gballoc_getCurrentMemoryUsed();
1567
1568 // assert
1569 ASSERT_ARE_EQUAL(size_t, 0, result);
1570 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1571 }
1572
1573 /* Tests_SRS_GBALLOC_01_044: [If gballoc was not initialized gballoc_getCurrentMemoryUsed shall return SIZE_MAX.] */
1574 TEST_FUNCTION(gballoc_getCurrentMemoryUsed_after_deinit_fails)
1575 {
1576 // arrange
1577
1578 // act
1579 size_t result = gballoc_getCurrentMemoryUsed();
1580
1581 // assert
1582 ASSERT_ARE_EQUAL(size_t, SIZE_MAX, result);
1583 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1584 }
1585
1586 /* Tests_SRS_GBALLOC_01_051: [If the lock cannot be acquired, gballoc_getCurrentMemoryUsed shall return SIZE_MAX.] */
1587 TEST_FUNCTION(when_acquiring_the_lock_fails_gballoc_getCurrentMemoryUsed_fails)
1588 {
1589 // arrange
1590 size_t result;
1591 gballoc_init();
1592 umock_c_reset_all_calls();
1593
1594 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE))
1595 .SetReturn(LOCK_ERROR);
1596
1597 // act
1598 result = gballoc_getCurrentMemoryUsed();
1599
1600 // assert
1601 ASSERT_ARE_EQUAL(size_t, SIZE_MAX, result);
1602 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1603 }
1604
1605 /* Tests_SRS_GBALLOC_07_001: [ If gballoc was not initialized gballoc_getAllocationCount shall return SIZE_MAX. ] */
1606 TEST_FUNCTION(gballoc_getAllocationCount_without_init_fail)
1607 {
1608 // arrange
1609 size_t result;
1610
1611 // act
1612 result = gballoc_getAllocationCount();
1613
1614 // assert
1615 ASSERT_ARE_EQUAL(size_t, 0, result);
1616 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1617 }
1618
1619 /* Tests_SRS_GBALLOC_07_003: [ If the lock cannot be acquired, gballoc_getAllocationCount shall return SIZE_MAX. ] */
1620 TEST_FUNCTION(gballoc_getAllocationCount_lock_fail)
1621 {
1622 // arrange
1623 size_t result;
1624 gballoc_init();
1625 umock_c_reset_all_calls();
1626
1627 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE))
1628 .SetReturn(LOCK_ERROR);
1629
1630 // act
1631 result = gballoc_getAllocationCount();
1632
1633 // assert
1634 ASSERT_ARE_EQUAL(size_t, 0, result);
1635 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1636 }
1637
1638 /* Tests_SRS_GBALLOC_07_004: [ gballoc_getAllocationCount shall return the currently number of allocations. ] */
1639 /* Tests_SRS_GBALLOC_07_002: [ gballoc_getAllocationCount shall ensure thread safety by using the lock created by gballoc_Init ] */
1640 TEST_FUNCTION(gballoc_getAllocationCount_success)
1641 {
1642 // arrange
1643 void* allocation;
1644 void* toBeFreed;
1645 size_t result;
1646 gballoc_init();
1647 umock_c_reset_all_calls();
1648
1649 allocation = malloc(OVERHEAD_SIZE);
1650
1651 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1652 EXPECTED_CALL(mock_calloc(1, 0))
1653 .SetReturn(allocation);
1654 STRICT_EXPECTED_CALL(mock_malloc(1));
1655 toBeFreed = gballoc_malloc(1);
1656 umock_c_reset_all_calls();
1657 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1658 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1659
1660 // act
1661 result = gballoc_getAllocationCount();
1662
1663 // assert
1664 ASSERT_ARE_EQUAL(size_t, 1, result);
1665 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1666
1667 // Cleanup
1668 gballoc_free(toBeFreed);
1669 free(allocation);
1670 }
1671
1672 TEST_FUNCTION(gballoc_resetMetrics_without_init_fail)
1673 {
1674 // arrange
1675
1676 // act
1677 gballoc_resetMetrics();
1678
1679 // assert
1680 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1681 }
1682
1683 /* Tests_SRS_GBALLOC_07_003: [ If the lock cannot be acquired, gballoc_getAllocationCount shall return SIZE_MAX. ] */
1684 TEST_FUNCTION(gballoc_resetMetrics_lock_fail)
1685 {
1686 // arrange
1687 gballoc_init();
1688 umock_c_reset_all_calls();
1689
1690 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE))
1691 .SetReturn(LOCK_ERROR);
1692
1693 // act
1694 gballoc_resetMetrics();
1695
1696 // assert
1697 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1698 }
1699
1700 /* Tests_SRS_GBALLOC_07_004: [ gballoc_getAllocationCount shall return the currently number of allocations. ] */
1701 /* Tests_SRS_GBALLOC_07_002: [ gballoc_getAllocationCount shall ensure thread safety by using the lock created by gballoc_Init ] */
1702 TEST_FUNCTION(gballoc_resetMetrics_success)
1703 {
1704 // arrange
1705 void* allocation;
1706 void* toBeFreed;
1707 size_t mem_used;
1708 size_t alloc_count;
1709 gballoc_init();
1710 umock_c_reset_all_calls();
1711
1712 allocation = malloc(OVERHEAD_SIZE);
1713
1714 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1715 EXPECTED_CALL(mock_calloc(1, 0))
1716 .SetReturn(allocation);
1717 STRICT_EXPECTED_CALL(mock_malloc(1));
1718 toBeFreed = gballoc_malloc(1);
1719 umock_c_reset_all_calls();
1720 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1721 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1722
1723 alloc_count = gballoc_getAllocationCount();
1724 ASSERT_ARE_EQUAL(size_t, 1, alloc_count);
1725 umock_c_reset_all_calls();
1726
1727 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1728 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1729 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1730 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1731 STRICT_EXPECTED_CALL(Lock(TEST_LOCK_HANDLE));
1732 STRICT_EXPECTED_CALL(Unlock(TEST_LOCK_HANDLE));
1733
1734 // act
1735 gballoc_resetMetrics();
1736 mem_used = gballoc_getCurrentMemoryUsed();
1737 alloc_count = gballoc_getAllocationCount();
1738
1739 // assert
1740 ASSERT_ARE_EQUAL(size_t, 0, mem_used);
1741 ASSERT_ARE_EQUAL(size_t, 0, alloc_count);
1742 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1743
1744 // Cleanup
1745 gballoc_free(toBeFreed);
1746 free(allocation);
1747 }
1748
1749 END_TEST_SUITE(GBAlloc_UnitTests)