Debian Codegraph @

src/vendor/azure-uamqp-c/deps/umock-c/tests/umockcallpairs_ut/umockcallpairs_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/umockcallpairs.h"
12 #include "umock_c/umockcall.h"
13 #include "umock_c/umock_log.h"
14
15 void UMOCK_LOG(const char* format, ...)
16 {
17 (void)format;
18 }
19
20 typedef struct umocktypes_are_equal_CALL_TAG
21 {
22 char* type;
23 const void* left;
24 const void* right;
25 } umocktypes_are_equal_CALL;
26
27 static umocktypes_are_equal_CALL* umocktypes_are_equal_calls;
28 static size_t umocktypes_are_equal_call_count;
29 static size_t when_shall_umocktypes_are_equal_call_fail;
30 static int umocktypes_are_equal_call_result;
31 static int umocktypes_are_equal_fail_call_result;
32
33 typedef struct umocktypes_copy_CALL_TAG
34 {
35 char* type;
36 void* destination;
37 const void* source;
38 } umocktypes_copy_CALL;
39
40 static umocktypes_copy_CALL* umocktypes_copy_calls;
41 static size_t umocktypes_copy_call_count;
42 static size_t when_shall_umocktypes_copy_call_fail;
43 static int umocktypes_copy_call_result;
44 static int umocktypes_copy_fail_call_result;
45
46 typedef struct umocktypes_free_CALL_TAG
47 {
48 char* type;
49 const void* value;
50 } umocktypes_free_CALL;
51
52 static umocktypes_free_CALL* umocktypes_free_calls;
53 static size_t umocktypes_free_call_count;
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 int umocktypes_are_equal(const char* type, const void* left, const void* right)
60 {
61 int result;
62
63 umocktypes_are_equal_CALL* new_calls = (umocktypes_are_equal_CALL*)realloc(umocktypes_are_equal_calls, sizeof(umocktypes_are_equal_CALL) * (umocktypes_are_equal_call_count + 1));
64 if (new_calls != NULL)
65 {
66 size_t typename_length = strlen(type);
67 umocktypes_are_equal_calls = new_calls;
68 umocktypes_are_equal_calls[umocktypes_are_equal_call_count].type = (char*)malloc(typename_length + 1);
69 (void)memcpy(umocktypes_are_equal_calls[umocktypes_are_equal_call_count].type, type, typename_length + 1);
70 umocktypes_are_equal_calls[umocktypes_are_equal_call_count].left = left;
71 umocktypes_are_equal_calls[umocktypes_are_equal_call_count].right = right;
72 umocktypes_are_equal_call_count++;
73 }
74
75 if (when_shall_umocktypes_are_equal_call_fail == umocktypes_are_equal_call_count)
76 {
77 result = umocktypes_are_equal_fail_call_result;
78 }
79 else
80 {
81 result = umocktypes_are_equal_call_result;
82 }
83
84 return result;
85 }
86
87 int umocktypes_copy(const char* type, void* destination, const void* source)
88 {
89 int result;
90
91 umocktypes_copy_CALL* new_calls = (umocktypes_copy_CALL*)realloc(umocktypes_copy_calls, sizeof(umocktypes_copy_CALL) * (umocktypes_copy_call_count + 1));
92 if (new_calls != NULL)
93 {
94 size_t typename_length = strlen(type);
95 umocktypes_copy_calls = new_calls;
96 umocktypes_copy_calls[umocktypes_copy_call_count].type = (char*)malloc(typename_length + 1);
97 (void)memcpy(umocktypes_copy_calls[umocktypes_copy_call_count].type, type, typename_length + 1);
98 umocktypes_copy_calls[umocktypes_copy_call_count].destination = destination;
99 umocktypes_copy_calls[umocktypes_copy_call_count].source = source;
100 umocktypes_copy_call_count++;
101 }
102
103 if (when_shall_umocktypes_copy_call_fail == umocktypes_copy_call_count)
104 {
105 result = umocktypes_copy_fail_call_result;
106 }
107 else
108 {
109 result = umocktypes_copy_call_result;
110 }
111
112 return result;
113 }
114
115 void umocktypes_free(const char* type, const void* value)
116 {
117 umocktypes_free_CALL* new_calls = (umocktypes_free_CALL*)realloc(umocktypes_free_calls, sizeof(umocktypes_free_CALL) * (umocktypes_free_call_count + 1));
118 if (new_calls != NULL)
119 {
120 size_t typename_length = strlen(type);
121 umocktypes_free_calls = new_calls;
122 umocktypes_free_calls[umocktypes_free_call_count].type = (char*)malloc(typename_length + 1);
123 (void)memcpy(umocktypes_free_calls[umocktypes_free_call_count].type, type, typename_length + 1);
124 umocktypes_free_calls[umocktypes_free_call_count].value = value;
125 umocktypes_free_call_count++;
126 }
127 }
128
129 void reset_umocktypes_are_equal_calls(void)
130 {
131 if (umocktypes_are_equal_calls != NULL)
132 {
133 size_t i;
134 for (i = 0; i < umocktypes_are_equal_call_count; i++)
135 {
136 free(umocktypes_are_equal_calls[i].type);
137 }
138
139 free(umocktypes_are_equal_calls);
140 umocktypes_are_equal_calls = NULL;
141 }
142 umocktypes_are_equal_call_count = 0;
143 when_shall_umocktypes_are_equal_call_fail = 0;
144 }
145
146 void reset_umocktypes_copy_calls(void)
147 {
148 if (umocktypes_copy_calls != NULL)
149 {
150 size_t i;
151 for (i = 0; i < umocktypes_copy_call_count; i++)
152 {
153 free(umocktypes_copy_calls[i].type);
154 }
155
156 free(umocktypes_copy_calls);
157 umocktypes_copy_calls = NULL;
158 }
159 umocktypes_copy_call_count = 0;
160 when_shall_umocktypes_copy_call_fail = 0;
161 }
162
163 void reset_umocktypes_free_calls(void)
164 {
165 if (umocktypes_free_calls != NULL)
166 {
167 size_t i;
168 for (i = 0; i < umocktypes_free_call_count; i++)
169 {
170 free(umocktypes_free_calls[i].type);
171 }
172
173 free(umocktypes_free_calls);
174 umocktypes_free_calls = NULL;
175 }
176 umocktypes_free_call_count = 0;
177 }
178
179 static size_t malloc_call_count;
180 static size_t realloc_call_count;
181 static size_t free_call_count;
182
183 static size_t when_shall_malloc_fail;
184 static size_t when_shall_realloc_fail;
185
186 void* mock_malloc(size_t size)
187 {
188 void* result;
189 malloc_call_count++;
190 if (malloc_call_count == when_shall_malloc_fail)
191 {
192 result = NULL;
193 }
194 else
195 {
196 result = malloc(size);
197 }
198 return result;
199 }
200
201 void* mock_realloc(void* ptr, size_t size)
202 {
203 void* result;
204 realloc_call_count++;
205 if (realloc_call_count == when_shall_realloc_fail)
206 {
207 result = NULL;
208 }
209 else
210 {
211 result = realloc(ptr, size);
212 }
213 return result;
214 }
215
216 void mock_free(void* ptr)
217 {
218 free_call_count++;
219 free(ptr);
220 }
221
222 #ifdef __cplusplus
223 }
224 #endif
225
226 void reset_malloc_calls(void)
227 {
228 malloc_call_count = 0;
229 when_shall_malloc_fail = 0;
230 realloc_call_count = 0;
231 when_shall_realloc_fail = 0;
232 free_call_count = 0;
233 }
234
235 static void reset_all_calls(void)
236 {
237 reset_malloc_calls();
238 reset_umocktypes_are_equal_calls();
239 reset_umocktypes_copy_calls();
240 reset_umocktypes_free_calls();
241 }
242
243 static TEST_MUTEX_HANDLE test_mutex;
244 static TEST_MUTEX_HANDLE global_mutex;
245
246 BEGIN_TEST_SUITE(umockcallpairs_unittests)
247
248 TEST_SUITE_INITIALIZE(suite_init)
249 {
250 test_mutex = TEST_MUTEX_CREATE();
251 ASSERT_IS_NOT_NULL(test_mutex);
252 }
253
254 TEST_SUITE_CLEANUP(suite_cleanup)
255 {
256 TEST_MUTEX_DESTROY(test_mutex);
257 }
258
259 TEST_FUNCTION_INITIALIZE(test_function_init)
260 {
261 int mutex_acquire_result = TEST_MUTEX_ACQUIRE(test_mutex);
262 ASSERT_ARE_EQUAL(int, 0, mutex_acquire_result);
263
264 reset_all_calls();
265 umocktypes_copy_call_result = 0;
266 umocktypes_are_equal_call_result = 1;
267 }
268
269 TEST_FUNCTION_CLEANUP(test_function_cleanup)
270 {
271 TEST_MUTEX_RELEASE(test_mutex);
272
273 reset_all_calls();
274 }
275
276 /* umockcallpairs_track_create_paired_call */
277
278 /* Tests_SRS_UMOCKCALLPAIRS_01_001: [ umockcallpairs_track_create_paired_call shall add a new entry to the PAIRED_HANDLES array and on success it shall return 0. ]*/
279 /* Tests_SRS_UMOCKCALLPAIRS_01_002: [ umockcallpairs_track_create_paired_call shall copy the handle_value to the handle_value member of the new entry. ] */
280 /* Tests_SRS_UMOCKCALLPAIRS_01_003: [ umockcallpairs_track_create_paired_call shall allocate a memory block and store a pointer to it in the memory field of the new entry. ]*/
281 /* Tests_SRS_UMOCKCALLPAIRS_01_006: [ The handle value shall be copied by using umocktypes_copy. ]*/
282 TEST_FUNCTION(umockcallpairs_track_create_paired_call_succeeds)
283 {
284 // arrange
285 PAIRED_HANDLES paired_handles = { NULL, 0 };
286 void* handle = (void*)0x4242;
287
288 // act
289 int result = umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
290
291 // assert
292 ASSERT_ARE_EQUAL(int, 0, result);
293 ASSERT_ARE_EQUAL(size_t, 2, malloc_call_count);
294 ASSERT_ARE_EQUAL(size_t, 1, realloc_call_count);
295 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_copy_call_count);
296 ASSERT_ARE_EQUAL(char_ptr, "void*", umocktypes_copy_calls[0].type);
297 ASSERT_ARE_EQUAL(void_ptr, &handle, umocktypes_copy_calls[0].source);
298
299 // cleanup
300 free(paired_handles.paired_handles[0].handle_type);
301 free(paired_handles.paired_handles[0].handle_value);
302 free(paired_handles.paired_handles);
303 }
304
305 /* Tests_SRS_UMOCKCALLPAIRS_01_004: [ If any of the arguments paired_handles, handle or handle_type is NULL, umockcallpairs_track_create_paired_call shallfail and return a non-zero value. ]*/
306 TEST_FUNCTION(when_paired_handles_is_NULL_umockcallpairs_track_create_paired_call_fails)
307 {
308 // arrange
309 void* handle = (void*)0x4242;
310
311 // act
312 int result = umockcallpairs_track_create_paired_call(NULL, &handle, "void*", sizeof(handle));
313
314 // assert
315 ASSERT_ARE_NOT_EQUAL(int, 0, result);
316 ASSERT_ARE_EQUAL(size_t, 0, malloc_call_count);
317 ASSERT_ARE_EQUAL(size_t, 0, realloc_call_count);
318 }
319
320 /* Tests_SRS_UMOCKCALLPAIRS_01_004: [ If any of the arguments paired_handles, handle or handle_type is NULL, umockcallpairs_track_create_paired_call shallfail and return a non-zero value. ]*/
321 TEST_FUNCTION(when_handle_is_NULL_umockcallpairs_track_create_paired_call_fails)
322 {
323 // arrange
324 PAIRED_HANDLES paired_handles = { NULL, 0 };
325
326 // act
327 int result = umockcallpairs_track_create_paired_call(&paired_handles, NULL, "void*", sizeof(void*));
328
329 // assert
330 ASSERT_ARE_NOT_EQUAL(int, 0, result);
331 ASSERT_ARE_EQUAL(size_t, 0, malloc_call_count);
332 ASSERT_ARE_EQUAL(size_t, 0, realloc_call_count);
333 }
334
335 /* Tests_SRS_UMOCKCALLPAIRS_01_004: [ If any of the arguments paired_handles, handle or handle_type is NULL, umockcallpairs_track_create_paired_call shallfail and return a non-zero value. ]*/
336 TEST_FUNCTION(when_handle_type_is_NULL_umockcallpairs_track_create_paired_call_fails)
337 {
338 // arrange
339 PAIRED_HANDLES paired_handles = { NULL, 0 };
340 void* handle = (void*)0x4242;
341
342 // act
343 int result = umockcallpairs_track_create_paired_call(&paired_handles, &handle, NULL, sizeof(handle));
344
345 // assert
346 ASSERT_ARE_NOT_EQUAL(int, 0, result);
347 ASSERT_ARE_EQUAL(size_t, 0, malloc_call_count);
348 ASSERT_ARE_EQUAL(size_t, 0, realloc_call_count);
349 }
350
351 /* Tests_SRS_UMOCKCALLPAIRS_01_005: [ If allocating memory fails, umockcallpairs_track_create_paired_call shall fail and return a non-zero value. ]*/
352 TEST_FUNCTION(when_reallocating_the_entire_paired_handles_array_fails_umockcallpairs_track_create_paired_call_fails)
353 {
354 // arrange
355 int result;
356 PAIRED_HANDLES paired_handles = { NULL, 0 };
357 void* handle = (void*)0x4242;
358
359 when_shall_realloc_fail = 1;
360
361 // act
362 result = umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
363
364 // assert
365 ASSERT_ARE_NOT_EQUAL(int, 0, result);
366 ASSERT_ARE_EQUAL(size_t, 1, realloc_call_count);
367 ASSERT_ARE_EQUAL(size_t, 0, malloc_call_count);
368 }
369
370 /* Tests_SRS_UMOCKCALLPAIRS_01_005: [ If allocating memory fails, umockcallpairs_track_create_paired_call shall fail and return a non-zero value. ]*/
371 TEST_FUNCTION(when_allocating_the_handle_value_memory_block_fails_umockcallpairs_track_create_paired_call_fails)
372 {
373 // arrange
374 int result;
375 PAIRED_HANDLES paired_handles = { NULL, 0 };
376 void* handle = (void*)0x4242;
377
378 when_shall_malloc_fail = 1;
379
380 // act
381 result = umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
382
383 // assert
384 ASSERT_ARE_NOT_EQUAL(int, 0, result);
385 ASSERT_ARE_EQUAL(size_t, 1, malloc_call_count);
386 ASSERT_ARE_EQUAL(size_t, 1, realloc_call_count);
387 ASSERT_ARE_EQUAL(size_t, 1, free_call_count);
388 }
389
390 /* Tests_SRS_UMOCKCALLPAIRS_01_005: [ If allocating memory fails, umockcallpairs_track_create_paired_call shall fail and return a non-zero value. ]*/
391 TEST_FUNCTION(when_allocating_the_handle_type_block_fails_umockcallpairs_track_create_paired_call_fails)
392 {
393 // arrange
394 int result;
395 PAIRED_HANDLES paired_handles = { NULL, 0 };
396 void* handle = (void*)0x4242;
397
398 when_shall_malloc_fail = 2;
399
400 // act
401 result = umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
402
403 // assert
404 ASSERT_ARE_NOT_EQUAL(int, 0, result);
405 ASSERT_ARE_EQUAL(size_t, 2, malloc_call_count);
406 ASSERT_ARE_EQUAL(size_t, 1, realloc_call_count);
407 ASSERT_ARE_EQUAL(size_t, 2, free_call_count);
408 }
409
410 /* Tests_SRS_UMOCKCALLPAIRS_01_007: [ If umocktypes_copy fails, umockcallpairs_track_create_paired_call shall fail and return a non-zero value. ]*/
411 TEST_FUNCTION(when_copying_the_handle_fails_umockcallpairs_track_create_paired_call_fails)
412 {
413 // arrange
414 int result;
415 PAIRED_HANDLES paired_handles = { NULL, 0 };
416 void* handle = (void*)0x4242;
417
418 when_shall_umocktypes_copy_call_fail = 1;
419 umocktypes_copy_fail_call_result = -1;
420
421 // act
422 result = umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
423
424 // assert
425 ASSERT_ARE_NOT_EQUAL(int, 0, result);
426 ASSERT_ARE_EQUAL(size_t, 1, realloc_call_count);
427 ASSERT_ARE_EQUAL(size_t, 3, free_call_count);
428 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_copy_call_count);
429 ASSERT_ARE_EQUAL(size_t, 2, malloc_call_count);
430 ASSERT_ARE_EQUAL(char_ptr, "void*", umocktypes_copy_calls[0].type);
431 ASSERT_ARE_EQUAL(void_ptr, &handle, umocktypes_copy_calls[0].source);
432 }
433
434 TEST_FUNCTION(when_realloc_fails_a_subsequent_create_and_destroy_succeeds)
435 {
436 // arrange
437 int result;
438 PAIRED_HANDLES paired_handles = { NULL, 0 };
439 void* handle = (void*)0x4242;
440 void* copied_handle;
441 when_shall_realloc_fail = 1;
442 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
443 reset_all_calls();
444 when_shall_realloc_fail = 0;
445
446 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
447 copied_handle = umocktypes_copy_calls[0].destination;
448 reset_all_calls();
449
450 // act
451 result = umockcallpairs_track_destroy_paired_call(&paired_handles, &handle);
452
453 // assert
454 ASSERT_ARE_EQUAL(int, 0, result);
455 ASSERT_ARE_EQUAL(size_t, 3, free_call_count);
456 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_are_equal_call_count);
457 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_free_call_count);
458 ASSERT_ARE_EQUAL(char_ptr, "void*", umocktypes_free_calls[0].type);
459 ASSERT_ARE_EQUAL(void_ptr, copied_handle, umocktypes_free_calls[0].value);
460 }
461
462 /* umockcallpairs_track_destroy_paired_call */
463
464 /* Tests_SRS_UMOCKCALLPAIRS_01_008: [ umockcallpairs_track_destroy_paired_call shall remove from the paired handles array pointed by the paired_handles field the entry that is associated with the handle passed in the handle argument. ]*/
465 /* Tests_SRS_UMOCKCALLPAIRS_01_009: [ On success umockcallpairs_track_destroy_paired_call shall return 0. ]*/
466 /* Tests_SRS_UMOCKCALLPAIRS_01_011: [ umockcallpairs_track_destroy_paired_call shall free the memory pointed by the memory field in the PAIRED_HANDLES array entry associated with handle. ]*/
467 /* Tests_SRS_UMOCKCALLPAIRS_01_012: [ If the array paired handles array is empty after removing the entry, the paired_handles field shall be freed and set to NULL. ]*/
468 /* Tests_SRS_UMOCKCALLPAIRS_01_013: [ When looking up which entry to remove, the comparison of the handle values shall be done by calling umocktypes_are_equal. ]*/
469 TEST_FUNCTION(umockcallpairs_track_destroy_paired_call_removes_a_tracked_handle)
470 {
471 // arrange
472 int result;
473 PAIRED_HANDLES paired_handles = { NULL, 0 };
474 void* handle = (void*)0x4242;
475 void* copied_handle;
476 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
477 copied_handle = umocktypes_copy_calls[0].destination;
478 reset_all_calls();
479
480 // act
481 result = umockcallpairs_track_destroy_paired_call(&paired_handles, &handle);
482
483 // assert
484 ASSERT_ARE_EQUAL(int, 0, result);
485 ASSERT_ARE_EQUAL(size_t, 3, free_call_count);
486 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_are_equal_call_count);
487 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_free_call_count);
488 ASSERT_ARE_EQUAL(char_ptr, "void*", umocktypes_free_calls[0].type);
489 ASSERT_ARE_EQUAL(void_ptr, copied_handle, umocktypes_free_calls[0].value);
490 }
491
492 /* Tests_SRS_UMOCKCALLPAIRS_01_008: [ umockcallpairs_track_destroy_paired_call shall remove from the paired handles array pointed by the paired_handles field the entry that is associated with the handle passed in the handle argument. ]*/
493 /* Tests_SRS_UMOCKCALLPAIRS_01_009: [ On success umockcallpairs_track_destroy_paired_call shall return 0. ]*/
494 /* Tests_SRS_UMOCKCALLPAIRS_01_011: [ umockcallpairs_track_destroy_paired_call shall free the memory pointed by the memory field in the PAIRED_HANDLES array entry associated with handle. ]*/
495 /* Tests_SRS_UMOCKCALLPAIRS_01_012: [ If the array paired handles array is empty after removing the entry, the paired_handles field shall be freed and set to NULL. ]*/
496 /* Tests_SRS_UMOCKCALLPAIRS_01_013: [ When looking up which entry to remove, the comparison of the handle values shall be done by calling umocktypes_are_equal. ]*/
497 TEST_FUNCTION(umockcallpairs_track_destroy_paired_call_with_2_creates_removes_the_tracked_handle)
498 {
499 // arrange
500 int result;
501 PAIRED_HANDLES paired_handles = { NULL, 0 };
502 void* handle1 = (void*)0x4242;
503 void* handle2 = (void*)0x4243;
504 void* first_copied_handle;
505 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle1, "void*", sizeof(handle1));
506 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle2, "void*", sizeof(handle2));
507 first_copied_handle = umocktypes_copy_calls[0].destination;
508 reset_all_calls();
509
510 // act
511 result = umockcallpairs_track_destroy_paired_call(&paired_handles, &handle1);
512
513 // assert
514 ASSERT_ARE_EQUAL(int, 0, result);
515 ASSERT_ARE_EQUAL(size_t, 2, free_call_count);
516 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_are_equal_call_count);
517 ASSERT_ARE_EQUAL(char_ptr, "void*", umocktypes_are_equal_calls[0].type);
518 ASSERT_ARE_EQUAL(void_ptr, first_copied_handle, umocktypes_are_equal_calls[0].left);
519 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_free_call_count);
520 ASSERT_ARE_EQUAL(char_ptr, "void*", umocktypes_free_calls[0].type);
521
522 // cleanup
523 free(paired_handles.paired_handles[0].handle_type);
524 free(paired_handles.paired_handles[0].handle_value);
525 free(paired_handles.paired_handles);
526 }
527
528 /* Tests_SRS_UMOCKCALLPAIRS_01_008: [ umockcallpairs_track_destroy_paired_call shall remove from the paired handles array pointed by the paired_handles field the entry that is associated with the handle passed in the handle argument. ]*/
529 /* Tests_SRS_UMOCKCALLPAIRS_01_009: [ On success umockcallpairs_track_destroy_paired_call shall return 0. ]*/
530 /* Tests_SRS_UMOCKCALLPAIRS_01_011: [ umockcallpairs_track_destroy_paired_call shall free the memory pointed by the memory field in the PAIRED_HANDLES array entry associated with handle. ]*/
531 /* Tests_SRS_UMOCKCALLPAIRS_01_013: [ When looking up which entry to remove, the comparison of the handle values shall be done by calling umocktypes_are_equal. ]*/
532 TEST_FUNCTION(when_the_handle_is_found_at_the_second_index_umockcallpairs_track_destroy_paired_call_succeeds)
533 {
534 // arrange
535 int result;
536 PAIRED_HANDLES paired_handles = { NULL, 0 };
537 void* handle1 = (void*)0x4242;
538 void* handle2 = (void*)0x4243;
539 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle1, "void*", sizeof(handle1));
540 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle2, "void*", sizeof(handle2));
541 reset_all_calls();
542
543 when_shall_umocktypes_are_equal_call_fail = 1;
544 umocktypes_are_equal_fail_call_result = 0;
545
546 // act
547 result = umockcallpairs_track_destroy_paired_call(&paired_handles, &handle2);
548
549 // assert
550 ASSERT_ARE_EQUAL(int, 0, result);
551 ASSERT_ARE_EQUAL(size_t, 1, umocktypes_free_call_count);
552 ASSERT_ARE_EQUAL(char_ptr, "void*", umocktypes_free_calls[0].type);
553
554 // cleanup
555 free(paired_handles.paired_handles[0].handle_type);
556 free(paired_handles.paired_handles[0].handle_value);
557 free(paired_handles.paired_handles);
558 }
559
560 /* Tests_SRS_UMOCKCALLPAIRS_01_010: [ If any of the arguments is NULL, umockcallpairs_track_destroy_paired_call shall fail and return a non-zero value. ]*/
561 TEST_FUNCTION(umockcallpairs_track_destroy_paired_call_with_NULL_paired_handles_fails)
562 {
563 // arrange
564 void* handle = (void*)0x4242;
565
566 // act
567 int result = umockcallpairs_track_destroy_paired_call(NULL, &handle);
568
569 // assert
570 ASSERT_ARE_NOT_EQUAL(int, 0, result);
571 ASSERT_ARE_EQUAL(size_t, 0, free_call_count);
572 ASSERT_ARE_EQUAL(size_t, 0, umocktypes_are_equal_call_count);
573 ASSERT_ARE_EQUAL(size_t, 0, umocktypes_free_call_count);
574 }
575
576 /* Tests_SRS_UMOCKCALLPAIRS_01_010: [ If any of the arguments is NULL, umockcallpairs_track_destroy_paired_call shall fail and return a non-zero value. ]*/
577 TEST_FUNCTION(umockcallpairs_track_destroy_paired_call_with_NULL_handle_fails)
578 {
579 // arrange
580 int result;
581 PAIRED_HANDLES paired_handles = { NULL, 0 };
582 void* handle = (void*)0x4242;
583 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
584 reset_all_calls();
585
586 // act
587 result = umockcallpairs_track_destroy_paired_call(&paired_handles, NULL);
588
589 // assert
590 ASSERT_ARE_NOT_EQUAL(int, 0, result);
591 ASSERT_ARE_EQUAL(size_t, 0, free_call_count);
592 ASSERT_ARE_EQUAL(size_t, 0, umocktypes_are_equal_call_count);
593 ASSERT_ARE_EQUAL(size_t, 0, umocktypes_free_call_count);
594
595 // cleanup
596 free(paired_handles.paired_handles[0].handle_type);
597 free(paired_handles.paired_handles[0].handle_value);
598 free(paired_handles.paired_handles);
599 }
600
601 /* Tests_SRS_UMOCKCALLPAIRS_01_014: [ If umocktypes_are_equal fails, umockcallpairs_track_destroy_paired_call shall fail and return a non-zero value. ]*/
602 TEST_FUNCTION(when_are_equal_fails_umockcallpairs_track_destroy_paired_call_fails)
603 {
604 // arrange
605 int result;
606 PAIRED_HANDLES paired_handles = { NULL, 0 };
607 void* handle = (void*)0x4242;
608 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle, "void*", sizeof(handle));
609 reset_all_calls();
610
611 when_shall_umocktypes_are_equal_call_fail = 1;
612 umocktypes_are_equal_fail_call_result = -1;
613
614 // act
615 result = umockcallpairs_track_destroy_paired_call(&paired_handles, &handle);
616
617 // assert
618 ASSERT_ARE_NOT_EQUAL(int, 0, result);
619
620 // cleanup
621 free(paired_handles.paired_handles[0].handle_type);
622 free(paired_handles.paired_handles[0].handle_value);
623 free(paired_handles.paired_handles);
624 }
625
626 /* Tests_SRS_UMOCKCALLPAIRS_01_015: [ If the handle is not found in the array then umockcallpairs_track_destroy_paired_call shall fail and return a non-zero value. ]*/
627 TEST_FUNCTION(when_the_handle_is_not_found_umockcallpairs_track_destroy_paired_call_fails)
628 {
629 // arrange
630 int result;
631 PAIRED_HANDLES paired_handles = { NULL, 0 };
632 void* handle1 = (void*)0x4242;
633 void* handle2 = (void*)0x4243;
634 (void)umockcallpairs_track_create_paired_call(&paired_handles, &handle1, "void*", sizeof(handle2));
635 reset_all_calls();
636
637 when_shall_umocktypes_are_equal_call_fail = 1;
638 umocktypes_are_equal_fail_call_result = 0;
639
640 // act
641 result = umockcallpairs_track_destroy_paired_call(&paired_handles, &handle2);
642
643 // assert
644 ASSERT_ARE_NOT_EQUAL(int, 0, result);
645
646 // cleanup
647 free(paired_handles.paired_handles[0].handle_type);
648 free(paired_handles.paired_handles[0].handle_value);
649 free(paired_handles.paired_handles);
650 }
651
652 END_TEST_SUITE(umockcallpairs_unittests)