Debian Codegraph @

src/vendor/azure-uamqp-c/deps/umock-c/inc/umock_c/umock_c_internal.h

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 #ifndef UMOCK_C_INTERNAL_H
5 #define UMOCK_C_INTERNAL_H
6
7 #ifdef __cplusplus
8 #include <cstdlib>
9 #include <cstdio>
10 #else
11 #include <stdlib.h>
12 #include <stdio.h>
13 #endif
14
15 #include "azure_macro_utils/macro_utils.h"
16 #include "umock_c/umocktypes.h"
17 #include "umock_c/umockcall.h"
18 #include "umock_c/umockcallrecorder.h"
19 #include "umock_c/umock_c.h"
20 #include "umock_c/umock_log.h"
21 #include "umock_c/umockalloc.h"
22 #include "umock_c/umockcallpairs.h"
23 #include "umock_c/umockstring.h"
24 #include "umock_c/umockautoignoreargs.h"
25
26 #ifdef _MSC_VER
27 #define UMOCK_C_WEAK
28 #elif __GNUC__
29 // GCC needs weak for the reals
30 #define UMOCK_C_WEAK __attribute__ ((weak))
31 #else
32 // at the mercy of that compiler
33 #define UMOCK_C_WEAK
34 #endif
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 void umock_c_indicate_error(UMOCK_C_ERROR_CODE error_code);
41 UMOCKCALL_HANDLE umock_c_get_last_expected_call(void);
42 int umock_c_add_expected_call(UMOCKCALL_HANDLE mock_call);
43 int umock_c_add_actual_call(UMOCKCALL_HANDLE mock_call, UMOCKCALL_HANDLE* matched_call);
44
45 typedef struct ARG_BUFFER_TAG
46 {
47 void* bytes;
48 size_t length;
49 } ARG_BUFFER;
50
51 typedef int(*TRACK_CREATE_FUNC_TYPE)(PAIRED_HANDLES* paired_handles, const void* handle, const char* handle_type, size_t handle_type_size);
52 typedef int(*TRACK_DESTROY_FUNC_TYPE)(PAIRED_HANDLES* paired_handles, const void* handle);
53
54 #define MOCK_ENABLED 0
55 #define MOCK_DISABLED 1
56
57 #define ARG_IS_IGNORED -1
58 #define ARG_IS_NOT_IGNORED 0
59
60 #define FAIL_RETURN_VALUE_SET -1
61 #define FAIL_RETURN_VALUE_NOT_SET 0
62
63 #define RETURN_VALUE_SET -1
64 #define RETURN_VALUE_NOT_SET 0
65
66 #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
67
68 #define GET_USED_ARGUMENT_TYPE(mock_call, arg_name, arg_type) \
69 mock_call->MU_C2(override_argument_type_,arg_name) != NULL ? mock_call->MU_C2(override_argument_type_,arg_name) : #arg_type
70
71 #define COPY_ARG_TO_MOCK_STRUCT(arg_type, arg_name) umocktypes_copy(#arg_type, (void*)&mock_call_data->arg_name, (void*)&arg_name);
72 #define DECLARE_MOCK_CALL_STRUCT_STACK(arg_type, arg_name) arg_type arg_name;
73 #define MARK_ARG_AS_NOT_IGNORED(arg_type, arg_name) mock_call_data->MU_C2(is_ignored_, arg_name) = ARG_IS_NOT_IGNORED;
74 #define CLEAR_VALIDATE_ARG_VALUE(arg_type, arg_name) mock_call_data->MU_C2(validate_arg_value_pointer_, arg_name) = NULL;
75 #define CLEAR_OVERRIDE_ARGUMENT_TYPE(arg_type, arg_name) mock_call_data->MU_C2(override_argument_type_, arg_name) = NULL;
76 #define CLEAR_CAPTURE_ARG_VALUE(arg_type, arg_name) mock_call_data->MU_C2(capture_arg_value_pointer_, arg_name) = NULL;
77 #define MARK_ARG_AS_IGNORED(arg_type, arg_name) mock_call_data->MU_C2(is_ignored_, arg_name) = ARG_IS_IGNORED;
78 #define CLEAR_OUT_ARG_BUFFERS(count, arg_type, arg_name) \
79 MU_C2(mock_call_data->out_arg_buffer_,arg_name).bytes = NULL; \
80 mock_call_data->out_arg_buffers[COUNT_OF(mock_call_data->out_arg_buffers) - MU_DIV2(count)] = &MU_C2(mock_call_data->out_arg_buffer_,arg_name);
81 #define CLEAR_VALIDATE_ARG_BUFFERS(count, arg_type, arg_name) mock_call_data->validate_arg_buffers[COUNT_OF(mock_call_data->validate_arg_buffers) - MU_DIV2(count)].bytes = NULL;
82 #define FREE_ARG_VALUE(count, arg_type, arg_name) umocktypes_free(GET_USED_ARGUMENT_TYPE(typed_mock_call_data, arg_name, arg_type), (void*)&typed_mock_call_data->arg_name);
83 #define FREE_OUT_ARG_BUFFERS(count, arg_type, arg_name) umockalloc_free(typed_mock_call_data->out_arg_buffers[COUNT_OF(typed_mock_call_data->out_arg_buffers) - MU_DIV2(count)]->bytes);
84 #define FREE_VALIDATE_ARG_BUFFERS(count, arg_type, arg_name) umockalloc_free(typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->validate_arg_buffers) - MU_DIV2(count)].bytes);
85 #define FREE_OVERRIDE_ARGUMENT_TYPE(count, arg_type, arg_name) umockalloc_free(typed_mock_call_data->MU_C2(override_argument_type_, arg_name));
86
87 #define COPY_IGNORE_ARG(arg_type, arg_name) \
88 result->MU_C2(is_ignored_, arg_name) = typed_mock_call_data->MU_C2(is_ignored_, arg_name);
89
90 #define COPY_VALIDATE_ARG_VALUE(arg_type, arg_name) \
91 result->MU_C2(validate_arg_value_pointer_, arg_name) = typed_mock_call_data->MU_C2(validate_arg_value_pointer_, arg_name);
92
93 #define COPY_OVERRIDE_ARGUMENT_TYPE(arg_type, arg_name) \
94 result->MU_C2(override_argument_type_, arg_name) = (typed_mock_call_data->MU_C2(override_argument_type_, arg_name) == NULL) ? NULL : umockstring_clone(typed_mock_call_data->MU_C2(override_argument_type_, arg_name));
95
96 #define COPY_CAPTURE_ARG_VALUE(arg_type, arg_name) \
97 result->MU_C2(capture_arg_value_pointer_, arg_name) = typed_mock_call_data->MU_C2(capture_arg_value_pointer_, arg_name);
98
99 #define COPY_ARG_VALUE(arg_type, arg_name) umocktypes_copy(GET_USED_ARGUMENT_TYPE(typed_mock_call_data, arg_name, arg_type), (void*)&result->arg_name, (void*)&typed_mock_call_data->arg_name);
100 #define COPY_OUT_ARG_BUFFERS(count, arg_type, arg_name) \
101 result->out_arg_buffers[COUNT_OF(result->out_arg_buffers) - MU_DIV2(count)] = &result->MU_C2(out_arg_buffer_,arg_name); \
102 result->out_arg_buffers[COUNT_OF(result->out_arg_buffers) - MU_DIV2(count)]->length = typed_mock_call_data->out_arg_buffers[COUNT_OF(typed_mock_call_data->out_arg_buffers) - MU_DIV2(count)]->length; \
103 if (typed_mock_call_data->out_arg_buffers[COUNT_OF(typed_mock_call_data->out_arg_buffers) - MU_DIV2(count)]->bytes != NULL) \
104 { \
105 result->out_arg_buffers[COUNT_OF(result->out_arg_buffers) - MU_DIV2(count)]->bytes = umockalloc_malloc(typed_mock_call_data->out_arg_buffers[COUNT_OF(typed_mock_call_data->out_arg_buffers) - MU_DIV2(count)]->length); \
106 (void)memcpy(result->out_arg_buffers[COUNT_OF(result->out_arg_buffers) - MU_DIV2(count)]->bytes, typed_mock_call_data->out_arg_buffers[COUNT_OF(typed_mock_call_data->out_arg_buffers) - MU_DIV2(count)]->bytes, typed_mock_call_data->out_arg_buffers[COUNT_OF(typed_mock_call_data->out_arg_buffers) - MU_DIV2(count)]->length); \
107 } \
108 else \
109 { \
110 result->out_arg_buffers[COUNT_OF(result->out_arg_buffers) - MU_DIV2(count)]->bytes = NULL; \
111 }
112
113 #define COPY_VALIDATE_ARG_BUFFERS(count, arg_type, arg_name) \
114 result->validate_arg_buffers[COUNT_OF(result->validate_arg_buffers) - MU_DIV2(count)].length = typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->validate_arg_buffers) - MU_DIV2(count)].length; \
115 if (typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->validate_arg_buffers) - MU_DIV2(count)].bytes != NULL) \
116 { \
117 result->validate_arg_buffers[COUNT_OF(result->validate_arg_buffers) - MU_DIV2(count)].bytes = umockalloc_malloc(typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->validate_arg_buffers) - MU_DIV2(count)].length); \
118 (void)memcpy(result->validate_arg_buffers[COUNT_OF(result->validate_arg_buffers) - MU_DIV2(count)].bytes, typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->validate_arg_buffers) - MU_DIV2(count)].bytes, typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->validate_arg_buffers) - MU_DIV2(count)].length); \
119 } \
120 else \
121 { \
122 result->validate_arg_buffers[COUNT_OF(result->validate_arg_buffers) - MU_DIV2(count)].bytes = NULL; \
123 }
124
125 #define ONLY_FIRST_ARG(arg0type, arg0name, ...) arg0name
126 #define FILL_ARG_IN_METADATA(arg_type, arg_name) {MU_TOSTRING(arg_type), MU_TOSTRING(arg_name) },
127 #define ARG_IN_SIGNATURE(count, arg_type, arg_name) arg_type arg_name MU_IFCOMMA(count)
128 #define ARG_NAME_ONLY_IN_CALL(count, arg_type, arg_name) arg_name MU_IFCOMMA(count)
129 #define ONLY_FIRST_ARG_NAME_IN_CALL(count, arg_type, arg_name) count
130 #define ARG_ASSIGN_IN_ARRAY(arg_type, arg_name) arg_name_local
131 #define DECLARE_VALIDATE_ARG_VALUE(arg_type, arg_name) void* MU_C2(validate_arg_value_pointer_,arg_name);
132 #define DECLARE_CAPTURE_ARG_VALUE(arg_type, arg_name) void* MU_C2(capture_arg_value_pointer_,arg_name);
133 #define DECLARE_IGNORE_FLAG_FOR_ARG(arg_type, arg_name) int MU_C2(is_ignored_,arg_name) : 1;
134 #define DECLARE_OVERRIDE_ARGUMENT_TYPE_FOR_ARG(arg_type, arg_name) char* MU_C2(override_argument_type_,arg_name);
135 #define DECLARE_OUT_ARG_BUFFER_FOR_ARG(arg_type, arg_name) ARG_BUFFER MU_C2(out_arg_buffer_,arg_name);
136 #define COPY_IGNORE_ARG_BY_NAME_TO_MODIFIER(name, arg_type, arg_name) MU_C2(mock_call_modifier->IgnoreArgument_,arg_name) = MU_C4(ignore_argument_func_,name,_,arg_name);
137 #define COPY_VALIDATE_ARG_BY_NAME_TO_MODIFIER(name, arg_type, arg_name) MU_C2(mock_call_modifier->ValidateArgument_,arg_name) = MU_C4(validate_argument_func_,name,_,arg_name);
138 #define COPY_COPY_OUT_ARGUMENT_BUFFER_BY_NAME_TO_MODIFIER(name, arg_type, arg_name) MU_C2(mock_call_modifier->CopyOutArgumentBuffer_,arg_name) = MU_C4(copy_out_argument_buffer_func_,name,_,arg_name);
139 #define COPY_VALIDATE_ARGUMENT_VALUE_BY_NAME_TO_MODIFIER(name, arg_type, arg_name) MU_C2(mock_call_modifier->ValidateArgumentValue_,arg_name) = MU_C4(validate_argument_value_func_,name,_,arg_name);
140 #define COPY_VALIDATE_ARGUMENT_VALUE_AS_TYPE_BY_NAME_TO_MODIFIER(name, arg_type, arg_name) MU_C3(mock_call_modifier->ValidateArgumentValue_,arg_name,_AsType) = MU_C4(validate_argument_value_as_type_func_,name,_,arg_name);
141 #define COPY_CAPTURE_ARGUMENT_VALUE_BY_NAME_TO_MODIFIER(name, arg_type, arg_name) MU_C2(mock_call_modifier->CaptureArgumentValue_,arg_name) = MU_C4(capture_argument_value_func_,name,_,arg_name);
142 #define COPY_OUT_ARG_VALUE_FROM_MATCHED_CALL(count, arg_type, arg_name) \
143 if (matched_call_data->out_arg_buffers[COUNT_OF(matched_call_data->out_arg_buffers) - MU_DIV2(count)]->bytes != NULL) \
144 { \
145 (void)memcpy(*((void**)(&arg_name)), matched_call_data->out_arg_buffers[COUNT_OF(matched_call_data->out_arg_buffers) - MU_DIV2(count)]->bytes, matched_call_data->out_arg_buffers[COUNT_OF(matched_call_data->out_arg_buffers) - MU_DIV2(count)]->length); \
146 } \
147
148 #define STRINGIFY_ARGS_DECLARE_RESULT_VAR(count, arg_type, arg_name) \
149 char* MU_C2(arg_name,_stringified) \
150 = (MU_C2(typed_mock_call_data->validate_arg_value_pointer_, arg_name) != NULL) ? \
151 umocktypes_stringify(MU_TOSTRING(arg_type), (void*)MU_C2(typed_mock_call_data->validate_arg_value_pointer_, arg_name)) : \
152 ((typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->out_arg_buffers) - MU_DIV2(count)].bytes != NULL) ? \
153 umockc_stringify_buffer(typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->validate_arg_buffers) - MU_DIV2(count)].bytes, typed_mock_call_data->validate_arg_buffers[COUNT_OF(typed_mock_call_data->validate_arg_buffers) - MU_DIV2(count)].length) : \
154 umocktypes_stringify(MU_TOSTRING(arg_type), (void*)&typed_mock_call_data->arg_name));
155
156 #define STRINGIFY_ARGS_CHECK_ARG_STRINGIFY_SUCCESS(arg_type, arg_name) if (MU_C2(arg_name,_stringified) == NULL) is_error = 1;
157 #define STRINGIFY_ARGS_DECLARE_ARG_STRING_LENGTH(arg_type, arg_name) size_t MU_C2(arg_name,_stringified_length) = strlen(MU_C2(arg_name,_stringified));
158 #define STRINGIFY_ARGS_COUNT_LENGTH(arg_type, arg_name) args_string_length += MU_C2(arg_name,_stringified_length);
159 #define STRINGIFY_ARGS_FREE_STRINGIFIED_ARG(arg_type, arg_name) umockalloc_free(MU_C2(arg_name,_stringified));
160 #define STRINGIFY_ARGS_COPY_ARG_STRING(arg_type, arg_name) \
161 if (arg_index > 0) \
162 { \
163 result[current_pos] = ','; \
164 current_pos++; \
165 } \
166 (void)memcpy(result + current_pos, MU_C2(arg_name,_stringified), MU_C2(arg_name,_stringified_length) + 1); \
167 current_pos += MU_C2(arg_name, _stringified_length); \
168 arg_index++;
169
170 /* Codes_SRS_UMOCK_C_LIB_01_096: [If the content of the code under test buffer and the buffer supplied to ValidateArgumentBuffer does not match then this should be treated as a mismatch in argument comparison for that argument.]*/
171 #define ARE_EQUAL_FOR_ARG(count, arg_type, arg_name) \
172 if (result && \
173 (((typed_left->validate_arg_buffers[COUNT_OF(typed_left->validate_arg_buffers) - MU_DIV2(count)].bytes != NULL) && (memcmp(*((void**)&typed_right->arg_name), typed_left->validate_arg_buffers[COUNT_OF(typed_left->validate_arg_buffers) - MU_DIV2(count)].bytes, typed_left->validate_arg_buffers[COUNT_OF(typed_left->validate_arg_buffers) - MU_DIV2(count)].length) != 0)) \
174 || ((typed_right->validate_arg_buffers[COUNT_OF(typed_right->validate_arg_buffers) - MU_DIV2(count)].bytes != NULL) && (memcmp(*((void**)&typed_left->arg_name), typed_right->validate_arg_buffers[COUNT_OF(typed_right->validate_arg_buffers) - MU_DIV2(count)].bytes, typed_right->validate_arg_buffers[COUNT_OF(typed_right->validate_arg_buffers) - MU_DIV2(count)].length) != 0))) \
175 ) \
176 { \
177 result = 0; \
178 } \
179 if ((result == 1) && (MU_C2(typed_left->is_ignored_, arg_name) == ARG_IS_NOT_IGNORED) \
180 && (MU_C2(typed_right->is_ignored_, arg_name) == ARG_IS_NOT_IGNORED)) \
181 { \
182 void* left_value; \
183 void* right_value; \
184 if (MU_C2(typed_left->validate_arg_value_pointer_, arg_name) != NULL) \
185 { \
186 left_value = (void*)MU_C2(typed_left->validate_arg_value_pointer_, arg_name); \
187 } \
188 else \
189 { \
190 left_value = (void*)&typed_left->arg_name; \
191 } \
192 if (MU_C2(typed_right->validate_arg_value_pointer_, arg_name) != NULL) \
193 { \
194 right_value = (void*)MU_C2(typed_right->validate_arg_value_pointer_, arg_name); \
195 } \
196 else \
197 { \
198 right_value = (void*)&typed_right->arg_name; \
199 } \
200 result = umocktypes_are_equal(GET_USED_ARGUMENT_TYPE(typed_left, arg_name, arg_type), left_value, right_value); \
201 }
202
203 #define DECLARE_MOCK_CALL_MODIFIER(name, ...) \
204 MU_C2(mock_call_modifier_,name) mock_call_modifier; \
205 MU_C2(fill_mock_call_modifier_,name)(&mock_call_modifier);
206
207 #define DECLARE_IGNORE_ARGUMENT_FUNCTION_PROTOTYPE(name, arg_type, arg_name) \
208 static MU_C2(mock_call_modifier_,name) MU_C4(ignore_argument_func_,name,_,arg_name)(void);
209
210 #define DECLARE_VALIDATE_ARGUMENT_FUNCTION_PROTOTYPE(name, arg_type, arg_name) \
211 static MU_C2(mock_call_modifier_,name) MU_C4(validate_argument_func_,name,_,arg_name)(void);
212
213 #define DECLARE_COPY_OUT_ARGUMENT_BUFFER_FUNCTION_PROTOTYPE(name, arg_type, arg_name) \
214 static MU_C2(mock_call_modifier_,name) MU_C4(copy_out_argument_buffer_func_,name,_,arg_name)(const void* bytes, size_t length);
215
216 #define DECLARE_VALIDATE_ARGUMENT_VALUE_FUNCTION_PROTOTYPE(name, arg_type, arg_name) \
217 static MU_C2(mock_call_modifier_,name) MU_C4(validate_argument_value_func_,name,_,arg_name)(arg_type* arg_value);
218
219 #define DECLARE_VALIDATE_ARGUMENT_VALUE_AS_TYPE_FUNCTION_PROTOTYPE(name, arg_type, arg_name) \
220 static MU_C2(mock_call_modifier_,name) MU_C4(validate_argument_value_as_type_func_,name,_,arg_name)(const char* type_name);
221
222 #define DECLARE_CAPTURE_ARGUMENT_VALUE_FUNCTION_PROTOTYPE(name, arg_type, arg_name) \
223 static MU_C2(mock_call_modifier_,name) MU_C4(capture_argument_value_func_,name,_,arg_name)(arg_type* arg_value);
224
225 #define IGNORE_ARGUMENT_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
226 MU_C4(ignore_argument_func_type_,name,_,arg_name) MU_C2(IgnoreArgument_,arg_name);
227
228 #define VALIDATE_ARGUMENT_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
229 MU_C4(validate_argument_func_type_,name,_,arg_name) MU_C2(ValidateArgument_,arg_name);
230
231 #define COPY_OUT_ARGUMENT_BUFFER_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
232 MU_C4(copy_out_argument_buffer_func_type_,name,_,arg_name) MU_C2(CopyOutArgumentBuffer_,arg_name);
233
234 #define VALIDATE_ARGUMENT_VALUE_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
235 MU_C4(validate_argument_value_func_type_,name,_,arg_name) MU_C2(ValidateArgumentValue_,arg_name);
236
237 #define CAPTURE_ARGUMENT_VALUE_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
238 MU_C4(capture_argument_value_func_type_,name,_,arg_name) MU_C2(CaptureArgumentValue_,arg_name);
239
240 #define VALIDATE_ARGUMENT_VALUE_AS_TYPE_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
241 MU_C4(validate_argument_value_as_type_func_type_,name,_,arg_name) MU_C3(ValidateArgumentValue_,arg_name,_AsType);
242
243 #define ARG_RELATED_FUNCTIONS_IN_MODIFIERS(name, arg_type, arg_name) \
244 IGNORE_ARGUMENT_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
245 VALIDATE_ARGUMENT_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
246 COPY_OUT_ARGUMENT_BUFFER_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
247 VALIDATE_ARGUMENT_VALUE_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
248 VALIDATE_ARGUMENT_VALUE_AS_TYPE_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
249 CAPTURE_ARGUMENT_VALUE_FUNCTION_IN_MODIFIERS(name, arg_type, arg_name) \
250
251 #define DECLARE_IGNORE_ARGUMENT_FUNCTION_TYPE(name, arg_type, arg_name) \
252 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C4(ignore_argument_func_type_,name,_,arg_name))(void);
253
254 #define DECLARE_VALIDATE_ARGUMENT_FUNCTION_TYPE(name, arg_type, arg_name) \
255 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C4(validate_argument_func_type_,name,_,arg_name))(void);
256
257 #define DECLARE_COPY_OUT_ARGUMENT_BUFFER_FUNCTION_TYPE(name, arg_type, arg_name) \
258 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C4(copy_out_argument_buffer_func_type_,name,_,arg_name))(const void* bytes, size_t length);
259
260 #define DECLARE_VALIDATE_ARGUMENT_VALUE_FUNCTION_TYPE(name, arg_type, arg_name) \
261 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C4(validate_argument_value_func_type_,name,_,arg_name))(arg_type* arg_value);
262
263 #define DECLARE_VALIDATE_ARGUMENT_AS_TYPE_VALUE_FUNCTION_TYPE(name, arg_type, arg_name) \
264 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C4(validate_argument_value_as_type_func_type_,name,_,arg_name))(const char* type_name);
265
266 #define DECLARE_CAPTURE_ARGUMENT_VALUE_FUNCTION_TYPE(name, arg_type, arg_name) \
267 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C4(capture_argument_value_func_type_,name,_,arg_name))(arg_type* arg_value);
268
269 #define DECLARE_ARG_RELATED_FUNCTIONS(name, arg_type, arg_name) \
270 DECLARE_IGNORE_ARGUMENT_FUNCTION_TYPE(name, arg_type, arg_name) \
271 DECLARE_VALIDATE_ARGUMENT_FUNCTION_TYPE(name, arg_type, arg_name) \
272 DECLARE_COPY_OUT_ARGUMENT_BUFFER_FUNCTION_TYPE(name, arg_type, arg_name) \
273 DECLARE_VALIDATE_ARGUMENT_VALUE_FUNCTION_TYPE(name, arg_type, arg_name) \
274 DECLARE_VALIDATE_ARGUMENT_AS_TYPE_VALUE_FUNCTION_TYPE(name, arg_type, arg_name) \
275 DECLARE_CAPTURE_ARGUMENT_VALUE_FUNCTION_TYPE(name, arg_type, arg_name)
276
277 #define IGNORE_ARGUMENT_FUNCTION_IN_ARRAY(name, arg_type, arg_name) \
278 &MU_C4(ignore_argument_func_,name,_,arg_name),
279
280 #define VALIDATE_ARGUMENT_FUNCTION_IN_ARRAY(name, arg_type, arg_name) \
281 &MU_C4(validate_argument_func_,name,_,arg_name),
282
283 /* These 2 macros are used to check if a type is "void" or not */
284 #define TEST_void 0
285 #define IS_NOT_VOID(x) \
286 MU_IF(MU_C2(TEST_,x), 1, 0)
287
288 /* Codes_SRS_UMOCK_C_LIB_01_076: [The IgnoreAllArguments call modifier shall record that for that specific call all arguments will be ignored for that specific call.] */
289 #define IMPLEMENT_IGNORE_ALL_ARGUMENTS_FUNCTION(return_type, name, ...) \
290 static MU_C2(mock_call_modifier_,name) MU_C2(ignore_all_arguments_func_,name)(void) \
291 { \
292 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
293 DECLARE_MOCK_CALL_MODIFIER(name) \
294 MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_FOR_EACH_2(MARK_ARG_AS_IGNORED, __VA_ARGS__),) \
295 return mock_call_modifier; \
296 } \
297
298 /* Codes_SRS_UMOCK_C_LIB_01_077: [The ValidateAllArguments call modifier shall record that for that specific call all arguments will be validated.] */
299 #define IMPLEMENT_VALIDATE_ALL_ARGUMENTS_FUNCTION(return_type, name, ...) \
300 static MU_C2(mock_call_modifier_,name) MU_C2(validate_all_arguments_func_,name)(void) \
301 { \
302 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
303 DECLARE_MOCK_CALL_MODIFIER(name) \
304 if (mock_call_data == NULL) \
305 { \
306 UMOCK_LOG("ValidateAllArguments called without having an expected call."); \
307 umock_c_indicate_error(UMOCK_C_ERROR); \
308 } \
309 else \
310 { \
311 MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_FOR_EACH_2(MARK_ARG_AS_NOT_IGNORED, __VA_ARGS__), ) \
312 } \
313 return mock_call_modifier; \
314 } \
315
316 /* Codes_SRS_UMOCK_C_LIB_01_078: [The IgnoreArgument_{arg_name} call modifier shall record that the argument identified by arg_name will be ignored for that specific call.] */
317 #define IMPLEMENT_IGNORE_ARGUMENT_BY_NAME_FUNCTION(name, arg_type, arg_name) \
318 static MU_C2(mock_call_modifier_,name) MU_C4(ignore_argument_func_,name,_,arg_name)(void) \
319 { \
320 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
321 DECLARE_MOCK_CALL_MODIFIER(name) \
322 if (mock_call_data == NULL) \
323 { \
324 UMOCK_LOG("IgnoreArgument_%s called without having an expected call.", MU_TOSTRING(arg_name)); \
325 umock_c_indicate_error(UMOCK_C_ERROR); \
326 } \
327 else \
328 { \
329 MU_C2(mock_call_data->is_ignored_,arg_name) = ARG_IS_IGNORED; \
330 } \
331 return mock_call_modifier; \
332 } \
333
334 /* Codes_SRS_UMOCK_C_LIB_01_079: [The ValidateArgument_{arg_name} call modifier shall record that the argument identified by arg_name will be validated for that specific call.]*/
335 #define IMPLEMENT_VALIDATE_ARGUMENT_BY_NAME_FUNCTION(name, arg_type, arg_name) \
336 static MU_C2(mock_call_modifier_,name) MU_C4(validate_argument_func_,name,_,arg_name)(void) \
337 { \
338 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
339 DECLARE_MOCK_CALL_MODIFIER(name) \
340 if (mock_call_data == NULL) \
341 { \
342 UMOCK_LOG("ValidateArgument_%s called without having an expected call.", MU_TOSTRING(arg_name)); \
343 umock_c_indicate_error(UMOCK_C_ERROR); \
344 } \
345 else \
346 { \
347 MU_C2(mock_call_data->is_ignored_,arg_name) = ARG_IS_NOT_IGNORED; \
348 } \
349 return mock_call_modifier; \
350 } \
351
352 /* Codes_SRS_UMOCK_C_LIB_01_080: [The IgnoreArgument call modifier shall record that the indexth argument will be ignored for that specific call.]*/
353 /* Codes_SRS_UMOCK_C_LIB_01_081: [If the index is out of range umock_c shall raise an error with the code UMOCK_C_ARG_INDEX_OUT_OF_RANGE.] */
354 #define IMPLEMENT_IGNORE_ARGUMENT_FUNCTION(return_type, name, ...) \
355 static MU_C2(mock_call_modifier_,name) MU_C2(ignore_argument_func_,name)(size_t arg_index) \
356 { \
357 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
358 DECLARE_MOCK_CALL_MODIFIER(name) \
359 if (mock_call_data == NULL) \
360 { \
361 UMOCK_LOG("IgnoreArgument called without having an expected call."); \
362 umock_c_indicate_error(UMOCK_C_ERROR); \
363 } \
364 else \
365 { \
366 MU_IF(MU_COUNT_ARG(__VA_ARGS__), \
367 if ((arg_index < 1) || (arg_index > (sizeof(MU_C2(ignore_one_argument_array_,name)) / sizeof(MU_C2(ignore_one_argument_array_,name)[0])))) \
368 { \
369 UMOCK_LOG("Bad argument index in call to IgnoreArgument %zu.", arg_index); \
370 umock_c_indicate_error(UMOCK_C_ARG_INDEX_OUT_OF_RANGE); \
371 } \
372 else \
373 { \
374 MU_C2(ignore_one_argument_array_,name)[arg_index - 1](); \
375 }, \
376 ) \
377 } \
378 return mock_call_modifier; \
379 } \
380
381 /* Codes_SRS_UMOCK_C_LIB_01_082: [The ValidateArgument call modifier shall record that the indexth argument will be validated for that specific call.]*/
382 /* Codes_SRS_UMOCK_C_LIB_01_083: [If the index is out of range umock_c shall raise an error with the code UMOCK_C_ARG_INDEX_OUT_OF_RANGE.]*/
383 #define IMPLEMENT_VALIDATE_ARGUMENT_FUNCTION(return_type, name, ...) \
384 static MU_C2(mock_call_modifier_,name) MU_C2(validate_argument_func_,name)(size_t arg_index) \
385 { \
386 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
387 DECLARE_MOCK_CALL_MODIFIER(name) \
388 if (mock_call_data == NULL) \
389 { \
390 UMOCK_LOG("ValidateArgument called without having an expected call."); \
391 umock_c_indicate_error(UMOCK_C_ERROR); \
392 } \
393 else \
394 { \
395 MU_IF(MU_COUNT_ARG(__VA_ARGS__), \
396 if ((arg_index < 1) || (arg_index > (sizeof(MU_C2(validate_one_argument_array_,name)) / sizeof(MU_C2(validate_one_argument_array_,name)[0])))) \
397 { \
398 UMOCK_LOG("Bad argument index in call to ValidateArgument %zu.", arg_index); \
399 umock_c_indicate_error(UMOCK_C_ARG_INDEX_OUT_OF_RANGE); \
400 } \
401 else \
402 { \
403 MU_C2(validate_one_argument_array_,name)[arg_index - 1](); \
404 }, \
405 ) \
406 } \
407 return mock_call_modifier; \
408 } \
409
410 /* Codes_SRS_UMOCK_C_LIB_01_084: [The SetReturn call modifier shall record that when an actual call is matched with the specific expected call, it shall return the result value to the code under test.] */
411 #define IMPLEMENT_SET_RETURN_FUNCTION(return_type, name, ...) \
412 static MU_C2(mock_call_modifier_,name) MU_C2(set_return_func_,name)(return_type return_value) \
413 { \
414 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
415 DECLARE_MOCK_CALL_MODIFIER(name) \
416 if (mock_call_data == NULL) \
417 { \
418 UMOCK_LOG("SetReturn called without having an expected call."); \
419 umock_c_indicate_error(UMOCK_C_ERROR); \
420 } \
421 else \
422 { \
423 mock_call_data->return_value_set = RETURN_VALUE_SET; \
424 if (umocktypes_copy(#return_type, (void*)&mock_call_data->return_value, (void*)&return_value) != 0) \
425 { \
426 UMOCK_LOG("Could not copy return value of type %s.", MU_TOSTRING(return_type)); \
427 umock_c_indicate_error(UMOCK_C_ERROR); \
428 } \
429 } \
430 return mock_call_modifier; \
431 }
432
433 /* Codes_SRS_UMOCK_C_LIB_01_085: [The SetFailReturn call modifier shall record a fail return value.]*/
434 #define IMPLEMENT_SET_FAIL_RETURN_FUNCTION(return_type, name, ...) \
435 static MU_C2(mock_call_modifier_,name) MU_C2(set_fail_return_func_,name)(return_type return_value) \
436 { \
437 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
438 DECLARE_MOCK_CALL_MODIFIER(name) \
439 if (mock_call_data == NULL) \
440 { \
441 UMOCK_LOG("SetFailReturn called without having an expected call."); \
442 umock_c_indicate_error(UMOCK_C_ERROR); \
443 } \
444 else \
445 { \
446 mock_call_data->fail_return_value_set = FAIL_RETURN_VALUE_SET; \
447 if (umocktypes_copy(#return_type, (void*)&mock_call_data->fail_return_value, (void*)&return_value) != 0) \
448 { \
449 UMOCK_LOG("Could not copy fail return value of type %s.", MU_TOSTRING(return_type)); \
450 umock_c_indicate_error(UMOCK_C_ERROR); \
451 } \
452 } \
453 return mock_call_modifier; \
454 }
455
456 /* Codes_SRS_UMOCK_C_LIB_01_116: [ The argument targetted by CopyOutArgument shall also be marked as ignored. ] */
457 /* Codes_SRS_UMOCK_C_LIB_01_088: [The memory shall be copied.]*/
458 /* Codes_SRS_UMOCK_C_LIB_01_091: [If the index is out of range umock_c shall raise an error with the code UMOCK_C_ARG_INDEX_OUT_OF_RANGE.]*/
459 /* Codes_SRS_UMOCK_C_LIB_01_117: [ If any memory allocation error occurs, umock_c shall raise an error with the code UMOCK_C_MALLOC_ERROR. ]*/
460 /* Codes_SRS_UMOCK_C_LIB_01_118: [ If any other error occurs, umock_c shall raise an error with the code UMOCK_C_ERROR. ]*/
461 /* Codes_SRS_UMOCK_C_LIB_01_092: [If bytes is NULL or length is 0, umock_c shall raise an error with the code UMOCK_C_INVALID_ARGUMENT_BUFFER.] */
462 /* Codes_SRS_UMOCK_C_LIB_01_089: [The buffers for previous CopyOutArgumentBuffer calls shall be freed.]*/
463 /* Codes_SRS_UMOCK_C_LIB_01_133: [ If several calls to CopyOutArgumentBuffer are made, only the last buffer shall be kept. ]*/
464 #define IMPLEMENT_COPY_OUT_ARGUMENT_BUFFER_FUNCTION(return_type, name, ...) \
465 static MU_C2(mock_call_modifier_, name) MU_C2(copy_out_argument_buffer_func_, name)(size_t index, const void* bytes, size_t length) \
466 { \
467 DECLARE_MOCK_CALL_MODIFIER(name) \
468 if ((index < 1) || (index > MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)))) \
469 { \
470 UMOCK_LOG("Bad argument index in CopyOutArgumentBuffer: %zu.", index); \
471 umock_c_indicate_error(UMOCK_C_ARG_INDEX_OUT_OF_RANGE); \
472 } \
473 else if ((bytes == NULL) || (length == 0)) \
474 { \
475 UMOCK_LOG("Bad arguments to CopyOutArgumentBuffer: bytes = %p, length = %zu.", bytes, length); \
476 umock_c_indicate_error(UMOCK_C_INVALID_ARGUMENT_BUFFER); \
477 } \
478 else \
479 { \
480 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
481 if (mock_call_data == NULL) \
482 { \
483 UMOCK_LOG("CopyOutArgumentBuffer called without having an expected call."); \
484 umock_c_indicate_error(UMOCK_C_ERROR); \
485 } \
486 else \
487 { \
488 ARG_BUFFER* arg_buffer = mock_call_data->out_arg_buffers[index - 1]; \
489 umockalloc_free(arg_buffer->bytes); \
490 arg_buffer->bytes = umockalloc_malloc(length); \
491 if (arg_buffer->bytes == NULL) \
492 { \
493 UMOCK_LOG("Could not allocate memory for out argument buffers."); \
494 umock_c_indicate_error(UMOCK_C_MALLOC_ERROR); \
495 } \
496 else \
497 { \
498 (void)memcpy(arg_buffer->bytes, bytes, length); \
499 arg_buffer->length = length; \
500 mock_call_modifier.IgnoreArgument(index); \
501 } \
502 } \
503 } \
504 return mock_call_modifier; \
505 } \
506
507 /* Codes_SRS_UMOCK_C_LIB_01_154: [ The CopyOutArgumentBuffer_{arg_name} call modifier shall copy the memory pointed to by bytes and being length bytes so that it is later injected as an out argument when the code under test calls the mock function. ] */
508 /* Codes_SRS_UMOCK_C_LIB_01_163: [ The buffers for previous CopyOutArgumentBuffer calls shall be freed. ]*/
509 /* Codes_SRS_UMOCK_C_LIB_01_156: [ If several calls to CopyOutArgumentBuffer are made, only the last buffer shall be kept. ]*/
510 /* Codes_SRS_UMOCK_C_LIB_01_155: [ The memory shall be copied. ]*/
511 /* Codes_SRS_UMOCK_C_LIB_01_158: [ If bytes is NULL or length is 0, umock_c shall raise an error with the code UMOCK_C_INVALID_ARGUMENT_BUFFER. ] */
512 #define IMPLEMENT_COPY_OUT_ARGUMENT_BUFFER_BY_NAME_FUNCTION(name, arg_type, arg_name) \
513 static MU_C2(mock_call_modifier_,name) MU_C4(copy_out_argument_buffer_func_,name,_,arg_name)(const void* bytes, size_t length) \
514 { \
515 DECLARE_MOCK_CALL_MODIFIER(name) \
516 if ((bytes == NULL) || (length == 0)) \
517 { \
518 UMOCK_LOG("Bad arguments to CopyOutArgumentBuffer: bytes = %p, length = %zu.", bytes, length); \
519 umock_c_indicate_error(UMOCK_C_INVALID_ARGUMENT_BUFFER); \
520 } \
521 else \
522 { \
523 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
524 if (mock_call_data == NULL) \
525 { \
526 UMOCK_LOG("CopyOutArgumentBuffer called without having an expected call."); \
527 umock_c_indicate_error(UMOCK_C_ERROR); \
528 } \
529 else \
530 { \
531 ARG_BUFFER* arg_buffer = &MU_C2(mock_call_data->out_arg_buffer_, arg_name); \
532 umockalloc_free(arg_buffer->bytes); \
533 arg_buffer->bytes = umockalloc_malloc(length); \
534 if (arg_buffer->bytes == NULL) \
535 { \
536 UMOCK_LOG("Could not allocate memory for out argument buffers."); \
537 umock_c_indicate_error(UMOCK_C_MALLOC_ERROR); \
538 } \
539 else \
540 { \
541 (void)memcpy(arg_buffer->bytes, bytes, length); \
542 arg_buffer->length = length; \
543 MU_C2(mock_call_modifier.IgnoreArgument_, arg_name)(); \
544 } \
545 } \
546 } \
547 return mock_call_modifier; \
548 } \
549
550 #define IMPLEMENT_COPY_OUT_ARGUMENT_FUNCTION(return_type, name, ...) \
551 static MU_C2(mock_call_modifier_,name) MU_C2(copy_out_argument_func_,name)(size_t arg_index, void* value) \
552 { \
553 DECLARE_MOCK_CALL_MODIFIER(name) \
554 (void)value; \
555 (void)arg_index; \
556 return mock_call_modifier; \
557 } \
558
559 /* Codes_SRS_UMOCK_C_LIB_01_095: [The ValidateArgumentBuffer call modifier shall copy the memory pointed to by bytes and being length bytes so that it is later compared against a pointer type argument when the code under test calls the mock function.] */
560 /* Codes_SRS_UMOCK_C_LIB_01_097: [ValidateArgumentBuffer shall implicitly perform an IgnoreArgument on the indexth argument.]*/
561 /* Codes_SRS_UMOCK_C_LIB_01_099: [If the index is out of range umock_c shall raise an error with the code UMOCK_C_ARG_INDEX_OUT_OF_RANGE.]*/
562 /* Codes_SRS_UMOCK_C_LIB_01_100: [If bytes is NULL or length is 0, umock_c shall raise an error with the code UMOCK_C_INVALID_ARGUMENT_BUFFER.] */
563 /* Codes_SRS_UMOCK_C_LIB_01_131: [ The memory pointed by bytes shall be copied. ]*/
564 /* Codes_SRS_UMOCK_C_LIB_01_132: [ If several calls to ValidateArgumentBuffer are made, only the last buffer shall be kept. ]*/
565 /* Codes_SRS_UMOCK_C_LIB_01_130: [ The buffers for previous ValidateArgumentBuffer calls shall be freed. ]*/
566 #define IMPLEMENT_VALIDATE_ARGUMENT_BUFFER_FUNCTION(return_type, name, ...) \
567 static MU_C2(mock_call_modifier_,name) MU_C2(validate_argument_buffer_func_,name)(size_t index, const void* bytes, size_t length) \
568 { \
569 DECLARE_MOCK_CALL_MODIFIER(name) \
570 if ((index < 1) || (index > MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)))) \
571 { \
572 UMOCK_LOG("Bad argument index in ValidateArgumentBuffer: %zu.", index); \
573 umock_c_indicate_error(UMOCK_C_ARG_INDEX_OUT_OF_RANGE); \
574 } \
575 else if ((bytes == NULL) || (length == 0)) \
576 { \
577 UMOCK_LOG("Bad arguments to ValidateArgumentBuffer: bytes = %p, length = %zu.", bytes, length); \
578 umock_c_indicate_error(UMOCK_C_INVALID_ARGUMENT_BUFFER); \
579 } \
580 else \
581 { \
582 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
583 if (mock_call_data == NULL) \
584 { \
585 UMOCK_LOG("ValidateArgumentBuffer called without having an expected call."); \
586 umock_c_indicate_error(UMOCK_C_ERROR); \
587 } \
588 else \
589 { \
590 umockalloc_free(mock_call_data->validate_arg_buffers[index - 1].bytes); \
591 mock_call_data->validate_arg_buffers[index - 1].bytes = umockalloc_malloc(length); \
592 if (mock_call_data->validate_arg_buffers[index - 1].bytes == NULL) \
593 { \
594 UMOCK_LOG("Could not allocate memory for validating argument buffers."); \
595 umock_c_indicate_error(UMOCK_C_MALLOC_ERROR); \
596 } \
597 else \
598 { \
599 (void)memcpy(mock_call_data->validate_arg_buffers[index - 1].bytes, bytes, length); \
600 mock_call_data->validate_arg_buffers[index - 1].length = length; \
601 mock_call_modifier.IgnoreArgument(index); \
602 } \
603 } \
604 } \
605 return mock_call_modifier; \
606 } \
607
608 /* Codes_SRS_UMOCK_C_LIB_01_101: [The IgnoreAllCalls call modifier shall record that all calls matching the expected call shall be ignored. If no matching call occurs no missing call shall be reported.]*/
609 /* Codes_SRS_UMOCK_C_LIB_01_208: [ If no matching call occurs no missing call shall be reported. ]*/
610 #define IMPLEMENT_IGNORE_ALL_CALLS_FUNCTION(return_type, name, ...) \
611 static MU_C2(mock_call_modifier_,name) MU_C2(ignore_all_calls_func_,name)(void) \
612 { \
613 UMOCKCALL_HANDLE last_expected_call = umock_c_get_last_expected_call(); \
614 DECLARE_MOCK_CALL_MODIFIER(name) \
615 if (last_expected_call == NULL) \
616 { \
617 UMOCK_LOG("Cannot get last expected call."); \
618 umock_c_indicate_error(UMOCK_C_ERROR); \
619 } \
620 else \
621 { \
622 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(last_expected_call); \
623 if (mock_call_data == NULL) \
624 { \
625 UMOCK_LOG("ValidateArgumentBuffer called without having an expected call."); \
626 umock_c_indicate_error(UMOCK_C_ERROR); \
627 } \
628 else \
629 { \
630 if (umockcall_set_ignore_all_calls(last_expected_call, 1) != 0) \
631 { \
632 UMOCK_LOG("Cannot set the ignore_all_calls value on the last expected call."); \
633 umock_c_indicate_error(UMOCK_C_ERROR); \
634 } \
635 } \
636 } \
637 return mock_call_modifier; \
638 } \
639
640 /* Codes_SRS_UMOCK_C_LIB_01_179: [ The CaptureReturn call modifier shall copy the return value that is being returned to the code under test when an actual call is matched with the expected call. ]*/
641 /* Codes_SRS_UMOCK_C_LIB_01_180: [ If CaptureReturn is called multiple times for the same call, an error shall be indicated with the code UMOCK_C_CAPTURE_RETURN_ALREADY_USED. ]*/
642 /* Codes_SRS_UMOCK_C_LIB_01_182: [ If captured_return_value is NULL, umock_c shall raise an error with the code UMOCK_C_NULL_ARGUMENT. ]*/
643 #define IMPLEMENT_CAPTURE_RETURN_FUNCTION(return_type, name, ...) \
644 static MU_C2(mock_call_modifier_,name) MU_C2(capture_return_func_,name)(return_type* captured_return_value) \
645 { \
646 DECLARE_MOCK_CALL_MODIFIER(name) \
647 if (captured_return_value == NULL) \
648 { \
649 UMOCK_LOG("NULL captured_return_value."); \
650 umock_c_indicate_error(UMOCK_C_NULL_ARGUMENT); \
651 } \
652 else \
653 { \
654 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
655 if (mock_call_data == NULL) \
656 { \
657 UMOCK_LOG("CaptureReturn called without having an expected call."); \
658 umock_c_indicate_error(UMOCK_C_ERROR); \
659 } \
660 else \
661 { \
662 if (mock_call_data->captured_return_value != NULL) \
663 { \
664 UMOCK_LOG("CaptureReturn called multiple times."); \
665 umock_c_indicate_error(UMOCK_C_CAPTURE_RETURN_ALREADY_USED); \
666 } \
667 else \
668 { \
669 mock_call_data->captured_return_value = captured_return_value; \
670 } \
671 } \
672 } \
673 return mock_call_modifier; \
674 }
675
676 /* Codes_SRS_UMOCK_C_LIB_01_183: [ The ValidateArgumentValue_{arg_name} shall validate that the value of an argument matches the value pointed by arg_value. ]*/
677 /* Codes_SRS_UMOCK_C_LIB_01_184: [ If arg_value is NULL, umock_c shall raise an error with the code UMOCK_C_NULL_ARGUMENT. ]*/
678 /* Codes_SRS_UMOCK_C_LIB_01_185: [ The ValidateArgumentValue_{arg_name} modifier shall inhibit comparing with any value passed directly as an argument in the expected call. ]*/
679 /* Codes_SRS_UMOCK_C_LIB_01_186: [ The ValidateArgumentValue_{arg_name} shall implicitly do a ValidateArgument for the arg_name argument, making sure the argument is not ignored. ]*/
680 #define IMPLEMENT_VALIDATE_ARGUMENT_VALUE_BY_NAME_FUNCTION(name, arg_type, arg_name) \
681 static MU_C2(mock_call_modifier_,name) MU_C4(validate_argument_value_func_,name,_,arg_name)(arg_type* arg_value) \
682 { \
683 DECLARE_MOCK_CALL_MODIFIER(name) \
684 if (arg_value == NULL) \
685 { \
686 UMOCK_LOG("NULL argument to ValidateArgumentValue_%s.", MU_TOSTRING(arg_name)); \
687 umock_c_indicate_error(UMOCK_C_NULL_ARGUMENT); \
688 } \
689 else \
690 { \
691 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
692 if (mock_call_data == NULL) \
693 { \
694 UMOCK_LOG("ValidateArgumentValue_%s called without having an expected call.", MU_TOSTRING(arg_name)); \
695 umock_c_indicate_error(UMOCK_C_ERROR); \
696 } \
697 else \
698 { \
699 mock_call_data->MU_C2(is_ignored_, arg_name) = ARG_IS_NOT_IGNORED; \
700 mock_call_data->MU_C2(validate_arg_value_pointer_, arg_name) = (void*)arg_value; \
701 } \
702 } \
703 return mock_call_modifier; \
704 }
705
706 /* Codes_SRS_UMOCK_C_LIB_01_199: [ ValidateArgumentValue_{arg_name}_AsType shall ensure that validation of the argument arg_name is done as if the argument is of type type_name. ]*/
707 /* Codes_SRS_UMOCK_C_LIB_01_200: [ If type_name is NULL, umock_c shall raise an error with the code UMOCK_C_NULL_ARGUMENT. ]*/
708 /* Codes_SRS_UMOCK_C_LIB_01_202: [ If storing the argument value as the new type fails, umock_c shall raise an error with the code UMOCK_C_COPY_ARGUMENT_ERROR. ]*/
709 #define IMPLEMENT_VALIDATE_ARGUMENT_VALUE_AS_TYPE_BY_NAME_FUNCTION(name, arg_type, arg_name) \
710 static MU_C2(mock_call_modifier_,name) MU_C4(validate_argument_value_as_type_func_,name,_,arg_name)(const char* type_name) \
711 { \
712 DECLARE_MOCK_CALL_MODIFIER(name) \
713 if (type_name == NULL) \
714 { \
715 UMOCK_LOG("NULL argument to ValidateArgumentValue_%s_AsType.", MU_TOSTRING(arg_name)); \
716 umock_c_indicate_error(UMOCK_C_NULL_ARGUMENT); \
717 } \
718 else \
719 { \
720 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
721 if (mock_call_data == NULL) \
722 { \
723 UMOCK_LOG("ValidateArgumentValue_%s called without having an expected call.", MU_TOSTRING(arg_name)); \
724 umock_c_indicate_error(UMOCK_C_ERROR); \
725 } \
726 else \
727 { \
728 char* cloned_type_name = umockstring_clone(type_name); \
729 if (cloned_type_name == NULL) \
730 { \
731 umockalloc_free(cloned_type_name); \
732 UMOCK_LOG("Cannot allocate memory to copy type_name in ValidateArgumentValue_%s_AsType for type name %s.", MU_TOSTRING(arg_name), type_name); \
733 umock_c_indicate_error(UMOCK_C_MALLOC_ERROR); \
734 } \
735 else \
736 { \
737 void* temp = umockalloc_malloc(sizeof(arg_type)); \
738 if (temp == NULL) \
739 { \
740 umockalloc_free(cloned_type_name); \
741 UMOCK_LOG("Cannot allocate memory for the temporary argument value in ValidateArgumentValue_%s.", MU_TOSTRING(arg_name)); \
742 umock_c_indicate_error(UMOCK_C_ERROR); \
743 } \
744 else \
745 { \
746 if (umocktypes_copy(GET_USED_ARGUMENT_TYPE(mock_call_data, arg_name, arg_type), (void*)temp, (void*)&mock_call_data->arg_name) != 0) \
747 { \
748 umockalloc_free(cloned_type_name); \
749 UMOCK_LOG("Cannot copy argument in ValidateArgumentValue_%s.", MU_TOSTRING(arg_name)); \
750 umock_c_indicate_error(UMOCK_C_ERROR); \
751 } \
752 else \
753 { \
754 umocktypes_free(GET_USED_ARGUMENT_TYPE(mock_call_data, arg_name, arg_type), (void*)&mock_call_data->arg_name); \
755 if (umocktypes_copy(type_name, (void*)&mock_call_data->arg_name, (void*)temp) != 0) \
756 { \
757 umockalloc_free(cloned_type_name); \
758 UMOCK_LOG("Cannot copy argument as new type in ValidateArgumentValue_%s.", MU_TOSTRING(arg_name)); \
759 umock_c_indicate_error(UMOCK_C_ERROR); \
760 } \
761 else \
762 { \
763 umocktypes_free(GET_USED_ARGUMENT_TYPE(mock_call_data, arg_name, arg_type), (void*)temp); \
764 umockalloc_free(mock_call_data->MU_C2(override_argument_type_, arg_name)); \
765 mock_call_data->MU_C2(override_argument_type_, arg_name) = cloned_type_name; \
766 } \
767 } \
768 umockalloc_free(temp); \
769 } \
770 } \
771 } \
772 } \
773 return mock_call_modifier; \
774 }
775
776 /* Codes_SRS_UMOCK_C_LIB_01_209: [ The CaptureArgumentValue_{arg_name} shall copy the value of the argument at the time of the call to arg_value. ]*/
777 /* Codes_SRS_UMOCK_C_LIB_01_211: [ The CaptureArgumentValue_{arg_name} shall not change the how the argument is validated. ]*/
778 #define IMPLEMENT_CAPTURE_ARGUMENT_VALUE_BY_NAME_FUNCTION(name, arg_type, arg_name) \
779 static MU_C2(mock_call_modifier_,name) MU_C4(capture_argument_value_func_,name,_,arg_name)(arg_type* arg_value) \
780 { \
781 DECLARE_MOCK_CALL_MODIFIER(name) \
782 if (arg_value == NULL) \
783 { \
784 UMOCK_LOG("NULL argument to CaptureArgumentValue_%s.", MU_TOSTRING(arg_name)); \
785 umock_c_indicate_error(UMOCK_C_NULL_ARGUMENT); \
786 } \
787 else \
788 { \
789 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(umock_c_get_last_expected_call()); \
790 if (mock_call_data == NULL) \
791 { \
792 UMOCK_LOG("CaptureArgumentValue_%s called without having an expected call.", MU_TOSTRING(arg_name)); \
793 umock_c_indicate_error(UMOCK_C_ERROR); \
794 } \
795 else \
796 { \
797 mock_call_data->MU_C2(capture_arg_value_pointer_, arg_name) = (void*)arg_value; \
798 } \
799 } \
800 return mock_call_modifier; \
801 }
802
803 /* Codes_SRS_UMOCK_C_LIB_31_209: [ call_cannot_fail_func__{name} call modifier shall record that when performing failure case run, this call should be skipped. ]*/
804 /* Codes_SRS_UMOCK_C_LIB_31_210: [ If recording that the call cannot fail is unsuccessful, umock shall raise with the error code UMOCK_C_ERROR. ]*/
805 #define IMPLEMENT_SET_CALL_CANNOT_FAIL(return_type, name, ...) \
806 static MU_C2(mock_call_modifier_,name) MU_C2(call_cannot_fail_func_,name)(void) \
807 { \
808 UMOCKCALL_HANDLE last_expected_call = umock_c_get_last_expected_call(); \
809 DECLARE_MOCK_CALL_MODIFIER(name) \
810 if (last_expected_call == NULL) \
811 { \
812 UMOCK_LOG("Cannot get last expected call."); \
813 umock_c_indicate_error(UMOCK_C_ERROR); \
814 } \
815 else \
816 { \
817 MU_C2(mock_call_, name)* mock_call_data = (MU_C2(mock_call_, name)*)umockcall_get_call_data(last_expected_call); \
818 if (mock_call_data == NULL) \
819 { \
820 UMOCK_LOG("call_cannot_fail called without having an expected call."); \
821 umock_c_indicate_error(UMOCK_C_ERROR); \
822 } \
823 else \
824 { \
825 if (umockcall_set_call_can_fail(last_expected_call, 0) != 0) \
826 { \
827 UMOCK_LOG("Cannot set call can fail value on the last expected call."); \
828 umock_c_indicate_error(UMOCK_C_ERROR); \
829 } \
830 } \
831 } \
832 return mock_call_modifier; \
833 } \
834
835
836 #define IMPLEMENT_MOCK_FUNCTION(function_prefix, args_ignored, return_type, name, ...) \
837 MU_C2(mock_call_modifier_,name) UMOCK_STATIC MU_C2(function_prefix,name)(MU_IF(MU_COUNT_ARG(__VA_ARGS__),,void) MU_FOR_EACH_2_COUNTED(ARG_IN_SIGNATURE, __VA_ARGS__)) \
838 { \
839 UMOCKCALL_HANDLE mock_call; \
840 MU_C2(mock_call_,name)* mock_call_data = (MU_C2(mock_call_,name)*)umockalloc_malloc(sizeof(MU_C2(mock_call_,name))); \
841 DECLARE_MOCK_CALL_MODIFIER(name) \
842 MU_FOR_EACH_2(COPY_ARG_TO_MOCK_STRUCT, __VA_ARGS__) \
843 MU_IF(args_ignored, MU_FOR_EACH_2(MARK_ARG_AS_IGNORED, __VA_ARGS__), MU_FOR_EACH_2(MARK_ARG_AS_NOT_IGNORED, __VA_ARGS__)) \
844 MU_FOR_EACH_2_COUNTED(CLEAR_OUT_ARG_BUFFERS, __VA_ARGS__) \
845 MU_FOR_EACH_2_COUNTED(CLEAR_VALIDATE_ARG_BUFFERS, __VA_ARGS__) \
846 MU_FOR_EACH_2(CLEAR_VALIDATE_ARG_VALUE, __VA_ARGS__) \
847 MU_FOR_EACH_2(CLEAR_CAPTURE_ARG_VALUE, __VA_ARGS__) \
848 MU_FOR_EACH_2(CLEAR_OVERRIDE_ARGUMENT_TYPE, __VA_ARGS__) \
849 MU_IF(IS_NOT_VOID(return_type), \
850 mock_call_data->return_value_set = RETURN_VALUE_NOT_SET; \
851 mock_call_data->captured_return_value = NULL; \
852 mock_call_data->fail_return_value_set = FAIL_RETURN_VALUE_NOT_SET; \
853 ,) \
854 mock_call = umockcall_create(#name, mock_call_data, MU_C2(mock_call_data_copy_func_,name), MU_C2(mock_call_data_free_func_,name), MU_C2(mock_call_data_stringify_,name), MU_C2(mock_call_data_are_equal_,name)); \
855 if (mock_call == NULL) \
856 { \
857 UMOCK_LOG("Failed creating mock call."); \
858 umock_c_indicate_error(UMOCK_C_ERROR); \
859 } \
860 else \
861 { \
862 (void)umock_c_add_expected_call(mock_call); \
863 (void)umockcall_set_call_can_fail(mock_call, MU_IF(IS_NOT_VOID(return_type), 1, 0)); \
864 } \
865 return mock_call_modifier; \
866 } \
867
868 #define IMPLEMENT_STRICT_EXPECTED_MOCK(return_type, name, ...) \
869 IMPLEMENT_MOCK_FUNCTION(umock_c_strict_expected_, 0, return_type, name, __VA_ARGS__)
870
871 #define IMPLEMENT_EXPECTED_MOCK(return_type, name, ...) \
872 IMPLEMENT_MOCK_FUNCTION(umock_c_expected_, 1, return_type, name, __VA_ARGS__)
873
874 /* Codes_SRS_UMOCK_C_LIB_01_104: [The REGISTER_GLOBAL_MOCK_HOOK shall register a mock hook to be called every time the mocked function is called by production code.]*/
875 /* Codes_SRS_UMOCK_C_LIB_01_107: [If there are multiple invocations of REGISTER_GLOBAL_MOCK_HOOK, the last one shall take effect over the previous ones.] */
876 /* Codes_SRS_UMOCK_C_LIB_01_134: [ REGISTER_GLOBAL_MOCK_HOOK called with a NULL hook unregisters a previously registered hook. ]*/
877 #define IMPLEMENT_REGISTER_GLOBAL_MOCK_HOOK(return_type, name, ...) \
878 UMOCK_STATIC void MU_C2(set_global_mock_hook_,name)(MU_C2(mock_hook_func_type_, name) mock_return_hook) \
879 { \
880 MU_C2(mock_hook_,name) = mock_return_hook; \
881 } \
882
883 /* Codes_SRS_UMOCK_C_LIB_01_108: [The REGISTER_GLOBAL_MOCK_RETURN shall register a return value to always be returned by a mock function.]*/
884 /* Codes_SRS_UMOCK_C_LIB_01_109: [If there are multiple invocations of REGISTER_GLOBAL_MOCK_RETURN, the last one shall take effect over the previous ones.]*/
885 /* Codes_SRS_UMOCK_C_LIB_01_141: [ If any error occurs during REGISTER_GLOBAL_MOCK_RETURN, umock_c shall raise an error with the code UMOCK_C_ERROR. ]*/
886 #define IMPLEMENT_REGISTER_GLOBAL_MOCK_RETURN(return_type, name, ...) \
887 MU_IF(IS_NOT_VOID(return_type), UMOCK_STATIC void MU_C2(set_global_mock_return_, name)(return_type return_value) \
888 { \
889 MU_C2(get_mock_call_return_values_,name)()->success_value = return_value; \
890 }, ) \
891
892 /* Codes_SRS_UMOCK_C_LIB_01_111: [The REGISTER_GLOBAL_MOCK_FAIL_RETURN shall register a fail return value to be returned by a mock function when marked as failed in the expected calls.]*/
893 /* Codes_SRS_UMOCK_C_LIB_01_112: [If there are multiple invocations of REGISTER_GLOBAL_FAIL_MOCK_RETURN, the last one shall take effect over the previous ones.]*/
894 /* Codes_SRS_UMOCK_C_LIB_01_142: [ If any error occurs during REGISTER_GLOBAL_MOCK_FAIL_RETURN, umock_c shall raise an error with the code UMOCK_C_ERROR. ]*/
895 #define IMPLEMENT_REGISTER_GLOBAL_MOCK_FAIL_RETURN(return_type, name, ...) \
896 MU_IF(IS_NOT_VOID(return_type), UMOCK_STATIC void MU_C2(set_global_mock_fail_return_, name)(return_type fail_return_value) \
897 { \
898 MU_C2(get_mock_call_return_values_,name)()->failure_value = fail_return_value; \
899 }, ) \
900
901 /* Codes_SRS_UMOCK_C_LIB_01_113: [The REGISTER_GLOBAL_MOCK_RETURNS shall register both a success and a fail return value associated with a mock function.]*/
902 /* Codes_SRS_UMOCK_C_LIB_01_114: [If there are multiple invocations of REGISTER_GLOBAL_MOCK_RETURNS, the last one shall take effect over the previous ones.]*/
903 /* Codes_SRS_UMOCK_C_LIB_01_143: [ If any error occurs during REGISTER_GLOBAL_MOCK_RETURNS, umock_c shall raise an error with the code UMOCK_C_ERROR. ]*/
904 #define IMPLEMENT_REGISTER_GLOBAL_MOCK_RETURNS(return_type, name, ...) \
905 MU_IF(IS_NOT_VOID(return_type), UMOCK_STATIC void MU_C2(set_global_mock_returns_, name)(return_type return_value, return_type fail_return_value) \
906 { \
907 MU_C2(get_mock_call_return_values_,name)()->success_value = return_value; \
908 MU_C2(get_mock_call_return_values_,name)()->failure_value = fail_return_value; \
909 }, ) \
910
911 #define DECLARE_VALIDATE_ONE_ARGUMENT_FUNC_TYPE(name) \
912 typedef struct MU_C2(_mock_call_modifier_, name) (*MU_C2(validate_one_argument_func_type_, name))(void);
913
914 #define COPY_RETURN_VALUE(return_type, name) \
915 result_C8417226_7442_49B4_BBB9_9CA816A21EB7 = MU_C2(get_mock_call_return_values_, name)()->success_value;
916
917 typedef struct MOCK_CALL_ARG_METADATA_TAG
918 {
919 const char* type;
920 const char* name;
921 } MOCK_CALL_ARG_METADATA;
922
923 typedef struct MOCK_CALL_METADATA_TAG
924 {
925 const char* return_type;
926 const char* name;
927 size_t arg_count;
928 const MOCK_CALL_ARG_METADATA* args;
929 } MOCK_CALL_METADATA;
930
931 #define UNUSED_ARG(arg_type, arg_name) \
932 (void)arg_name;
933
934 /* Codes_SRS_UMOCK_C_LIB_01_205: [ If IGNORED_PTR_ARG or IGNORED_NUM_ARG is used as an argument value with STRICT_EXPECTED_CALL, the argument shall be automatically ignored. ]*/
935 /* Codes_SRS_UMOCK_C_LIB_01_206: [ IGNORED_PTR_ARG shall be defined as NULL so that it can be used for pointer type arguments. ]*/
936 /* Codes_SRS_UMOCK_C_LIB_01_207: [ IGNORED_NUM_ARG shall be defined to 0 so that it can be used for numeric type arguments. ]*/
937 #define AUTO_IGNORE_ARG(arg_type, arg_name) \
938 if (umockautoignoreargs_is_call_argument_ignored(call_as_string, arg_index++, &is_ignored) != 0) \
939 { \
940 UMOCK_LOG("Failed parsing argument %s value from the call.", MU_TOSTRING(arg_name)); \
941 } \
942 else \
943 { \
944 if (is_ignored) \
945 { \
946 result.MU_C2(IgnoreArgument_, arg_name)(); \
947 } \
948 } \
949
950 #define COPY_CAPTURED_ARGUMENT_VALUE(arg_type, arg_name) \
951 if (matched_call_data->MU_C2(capture_arg_value_pointer_,arg_name) != NULL) \
952 { \
953 if (umocktypes_copy(#arg_type, (void*)matched_call_data->MU_C2(capture_arg_value_pointer_,arg_name), (void*)&arg_name) != 0) \
954 { \
955 UMOCK_LOG("Could not copy captured argument value of type %s.", MU_TOSTRING(arg_type)); \
956 umock_c_indicate_error(UMOCK_C_ERROR); \
957 } \
958 } \
959
960 /* Codes_SRS_UMOCK_C_LIB_01_004: [If ENABLE_MOCKS is defined, MOCKABLE_FUNCTION shall generate the declaration of the function and code for the mocked function, thus allowing setting up of expectations in test functions.] */
961 /* Codes_SRS_UMOCK_C_LIB_01_014: [For each argument the argument value shall be stored for later comparison with actual calls.] */
962 /* Codes_SRS_UMOCK_C_LIB_01_017: [No arguments shall be saved by default, unless other modifiers state it.]*/
963 /* Codes_SRS_UMOCK_C_LIB_01_074: [When an expected call is recorded a call modifier interface in the form of a structure containing function pointers shall be returned to the caller.] */
964 /* Codes_SRS_UMOCK_C_LIB_01_075: [The last modifier in a chain overrides previous modifiers if any collision occurs.]*/
965 /* Codes_SRS_UMOCK_C_LIB_01_127: [ IgnoreAllArguments shall only be available for mock functions that have arguments. ]*/
966 /* Codes_SRS_UMOCK_C_LIB_01_126: [ ValidateAllArguments shall only be available for mock functions that have arguments. ]*/
967 /* Codes_SRS_UMOCK_C_LIB_01_125: [ IgnoreArgument_{arg_name} shall only be available for mock functions that have arguments. ]*/
968 /* Codes_SRS_UMOCK_C_LIB_01_124: [ ValidateArgument_{arg_name} shall only be available for mock functions that have arguments. **]*/
969 /* Codes_SRS_UMOCK_C_LIB_01_123: [ IgnoreArgument shall only be available for mock functions that have arguments. ]*/
970 /* Codes_SRS_UMOCK_C_LIB_01_122: [ ValidateArgument shall only be available for mock functions that have arguments. ]*/
971 /* Codes_SRS_UMOCK_C_LIB_01_121: [ SetReturn shall only be available if the return type is not void. ]*/
972 /* Codes_SRS_UMOCK_C_LIB_01_120: [ SetFailReturn shall only be available if the return type is not void. ]*/
973 /* Codes_SRS_UMOCK_C_LIB_01_119: [ CopyOutArgumentBuffer shall only be available for mock functions that have arguments. ]*/
974 /* Codes_SRS_UMOCK_C_LIB_01_128: [ CopyOutArgument shall only be available for mock functions that have arguments. ]*/
975 /* Codes_SRS_UMOCK_C_LIB_01_129: [ ValidateArgumentBuffer shall only be available for mock functions that have arguments. ]*/
976 /* Codes_SRS_UMOCK_C_LIB_01_105: [The hook\92s result shall be returned by the mock to the production code.]*/
977 /* Codes_SRS_UMOCK_C_LIB_01_106: [The signature for the hook shall be assumed to have exactly the same arguments and return as the mocked function.]*/
978 /* Codes_SRS_UMOCK_C_LIB_01_135: [ All parameters passed to the mock shall be passed down to the mock hook. ]*/
979 /* Codes_SRS_UMOCK_C_LIB_01_148: [ If call comparison fails an error shall be indicated by calling the error callback with UMOCK_C_COMPARE_CALL_ERROR. ]*/
980 /* Codes_SRS_UMOCK_C_LIB_01_136: [ When multiple return values are set for a mock function by using different means (such as SetReturn), the following order shall be in effect: ]*/
981 /* Codes_SRS_UMOCK_C_LIB_01_137: [ - If a return value has been specified for an expected call then that value shall be returned. ]*/
982 /* Codes_SRS_UMOCK_C_LIB_01_138: [ - If a global mock hook has been specified then it shall be called and its result returned. ]*/
983 /* Codes_SRS_UMOCK_C_LIB_01_139: [ - If a global return value has been specified then it shall be returned. ]*/
984 /* Codes_SRS_UMOCK_C_LIB_01_140: [ - Otherwise the value of a static variable of the same type as the return type shall be returned. ]*/
985 /* Codes_SRS_UMOCK_C_LIB_01_188: [ The create call shall have a non-void return type. ]*/
986 #define MOCKABLE_FUNCTION_UMOCK_INTERNAL_WITH_MOCK_NO_CODE(do_returns, return_type, name, ...) \
987 typedef return_type (*MU_C2(mock_hook_func_type_, name))(MU_IF(MU_COUNT_ARG(__VA_ARGS__),,void) MU_FOR_EACH_2_COUNTED(ARG_IN_SIGNATURE, __VA_ARGS__)); \
988 return_type MU_C2(real_, name)(MU_IF(MU_COUNT_ARG(__VA_ARGS__),,void) MU_FOR_EACH_2_COUNTED(ARG_IN_SIGNATURE, __VA_ARGS__)) UMOCK_C_WEAK; \
989 static MU_C2(mock_hook_func_type_,name) MU_C2(mock_hook_,name) = NULL; \
990 static TRACK_CREATE_FUNC_TYPE MU_C2(track_create_destroy_pair_malloc_,name) = NULL; \
991 static TRACK_DESTROY_FUNC_TYPE MU_C2(track_create_destroy_pair_free_,name) = NULL; \
992 static PAIRED_HANDLES MU_C2(paired_handles_,name); \
993 static PAIRED_HANDLES* MU_C2(used_paired_handles_,name) = NULL; \
994 static const MOCK_CALL_ARG_METADATA MU_C2(mock_call_args_metadata_,name)[MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)), 1)] \
995 = { MU_IF(MU_COUNT_ARG(__VA_ARGS__),,NULL) MU_FOR_EACH_2(FILL_ARG_IN_METADATA, __VA_ARGS__) }; \
996 static const MOCK_CALL_METADATA MU_C2(mock_call_metadata_,name) = {MU_TOSTRING(return_type), MU_TOSTRING(name), MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)), \
997 MU_C2(mock_call_args_metadata_,name) }; \
998 struct MU_C2(_mock_call_modifier_,name); \
999 MU_IF(IS_NOT_VOID(return_type), \
1000 typedef struct MU_C3(UMOCK_RETURNS_VALUES_STRUCT_, name, _TAG) \
1001 { \
1002 return_type success_value; \
1003 return_type failure_value; \
1004 int initialized : 1; \
1005 } MU_C2(UMOCK_RETURNS_VALUES_STRUCT_, name); \
1006 static MU_C2(UMOCK_RETURNS_VALUES_STRUCT_, name) MU_C2(mock_call_return_values_, name); \
1007 static MU_C2(UMOCK_RETURNS_VALUES_STRUCT_, name)* MU_C2(get_mock_call_return_values_, name)(void); \
1008 MU_IF(IS_NOT_VOID(return_type), \
1009 MU_IF(do_returns,, \
1010 static MU_C2(UMOCK_RETURNS_VALUES_STRUCT_, name)* MU_C2(get_mock_call_return_values_, name)(void) \
1011 { \
1012 MU_C2(UMOCK_RETURNS_VALUES_STRUCT_, name)* result = &MU_C2(mock_call_return_values_, name); \
1013 return result; \
1014 } \
1015 ) \
1016 ,) \
1017 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(set_return_func_type_,name))(return_type return_value); \
1018 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(set_fail_return_func_type_,name))(return_type return_value); \
1019 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(call_cannot_fail_func_type_,name))(void); \
1020 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(capture_return_func_type_,name))(return_type* captured_return_value);,) \
1021 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(ignore_all_calls_func_type_,name))(void); \
1022 MU_IF(MU_COUNT_ARG(__VA_ARGS__),typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(ignore_all_arguments_func_type_,name))(void); \
1023 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(validate_all_arguments_func_type_,name))(void); \
1024 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(copy_out_argument_func_type_,name))(size_t arg_index, void* value); \
1025 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(ignore_argument_func_type_,name))(size_t arg_index); \
1026 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(validate_argument_func_type_,name))(size_t arg_index); \
1027 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(validate_argument_buffer_func_type_,name))(size_t index, const void* bytes, size_t length); \
1028 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(copy_out_argument_buffer_func_type_,name))(size_t index, const void* bytes, size_t length); \
1029 MU_FOR_EACH_2_KEEP_1(DECLARE_ARG_RELATED_FUNCTIONS, name, __VA_ARGS__),) \
1030 typedef struct MU_C2(_mock_call_modifier_,name) \
1031 { \
1032 MU_C2(ignore_all_calls_func_type_,name) IgnoreAllCalls; \
1033 MU_IF(IS_NOT_VOID(return_type),MU_C2(set_return_func_type_,name) SetReturn; \
1034 MU_C2(set_fail_return_func_type_,name) SetFailReturn; \
1035 MU_C2(call_cannot_fail_func_type_,name) CallCannotFail; \
1036 MU_C2(capture_return_func_type_,name) CaptureReturn;,) \
1037 MU_IF(MU_COUNT_ARG(__VA_ARGS__),MU_C2(ignore_all_arguments_func_type_,name) IgnoreAllArguments; \
1038 MU_C2(validate_all_arguments_func_type_,name) ValidateAllArguments; \
1039 MU_C2(copy_out_argument_func_type_,name) CopyOutArgument; \
1040 MU_C2(ignore_argument_func_type_,name) IgnoreArgument; \
1041 MU_C2(validate_argument_func_type_,name) ValidateArgument; \
1042 MU_C2(validate_argument_buffer_func_type_,name) ValidateArgumentBuffer; \
1043 MU_C2(copy_out_argument_buffer_func_type_,name) CopyOutArgumentBuffer; \
1044 MU_FOR_EACH_2_KEEP_1(ARG_RELATED_FUNCTIONS_IN_MODIFIERS, name, __VA_ARGS__),) \
1045 } MU_C2(mock_call_modifier_,name); \
1046 static MU_C2(mock_call_modifier_,name) MU_C2(ignore_all_calls_func_,name)(void); \
1047 MU_IF(IS_NOT_VOID(return_type),static MU_C2(mock_call_modifier_,name) MU_C2(set_return_func_,name)(return_type return_value); \
1048 static MU_C2(mock_call_modifier_,name) MU_C2(set_fail_return_func_,name)(return_type return_value); \
1049 static MU_C2(mock_call_modifier_,name) MU_C2(call_cannot_fail_func_,name)(void); \
1050 static MU_C2(mock_call_modifier_,name) MU_C2(capture_return_func_,name)(return_type* captured_return_value);,) \
1051 MU_IF(MU_COUNT_ARG(__VA_ARGS__),static MU_C2(mock_call_modifier_,name) MU_C2(ignore_all_arguments_func_,name)(void); \
1052 static MU_C2(mock_call_modifier_,name) MU_C2(validate_all_arguments_func_,name)(void); \
1053 static MU_C2(mock_call_modifier_,name) MU_C2(copy_out_argument_func_,name)(size_t arg_index, void* value); \
1054 static MU_C2(mock_call_modifier_,name) MU_C2(ignore_argument_func_,name)(size_t arg_index); \
1055 static MU_C2(mock_call_modifier_,name) MU_C2(validate_argument_func_,name)(size_t arg_index); \
1056 static MU_C2(mock_call_modifier_,name) MU_C2(validate_argument_buffer_func_,name)(size_t index, const void* bytes, size_t length); \
1057 static MU_C2(mock_call_modifier_,name) MU_C2(copy_out_argument_buffer_func_,name)(size_t index, const void* bytes, size_t length); \
1058 MU_FOR_EACH_2_KEEP_1(DECLARE_IGNORE_ARGUMENT_FUNCTION_PROTOTYPE, name, __VA_ARGS__) \
1059 MU_FOR_EACH_2_KEEP_1(DECLARE_VALIDATE_ARGUMENT_FUNCTION_PROTOTYPE, name, __VA_ARGS__) \
1060 MU_FOR_EACH_2_KEEP_1(DECLARE_COPY_OUT_ARGUMENT_BUFFER_FUNCTION_PROTOTYPE, name, __VA_ARGS__) \
1061 MU_FOR_EACH_2_KEEP_1(DECLARE_VALIDATE_ARGUMENT_VALUE_FUNCTION_PROTOTYPE, name, __VA_ARGS__) \
1062 MU_FOR_EACH_2_KEEP_1(DECLARE_VALIDATE_ARGUMENT_VALUE_AS_TYPE_FUNCTION_PROTOTYPE, name, __VA_ARGS__) \
1063 MU_FOR_EACH_2_KEEP_1(DECLARE_CAPTURE_ARGUMENT_VALUE_FUNCTION_PROTOTYPE, name, __VA_ARGS__) \
1064 typedef struct MU_C2(_mock_call_modifier_,name) (*MU_C2(ignore_one_argument_func_type_,name))(void);,) \
1065 MU_IF(MU_COUNT_ARG(__VA_ARGS__), static const MU_C2(ignore_one_argument_func_type_,name) MU_C2(ignore_one_argument_array_,name)[] = \
1066 {,) \
1067 MU_FOR_EACH_2_KEEP_1(IGNORE_ARGUMENT_FUNCTION_IN_ARRAY, name, __VA_ARGS__) \
1068 MU_IF(MU_COUNT_ARG(__VA_ARGS__), }; \
1069 DECLARE_VALIDATE_ONE_ARGUMENT_FUNC_TYPE(name) \
1070 static const MU_C2(validate_one_argument_func_type_,name) MU_C2(validate_one_argument_array_,name)[] = \
1071 {,) \
1072 MU_FOR_EACH_2_KEEP_1(VALIDATE_ARGUMENT_FUNCTION_IN_ARRAY, name, __VA_ARGS__) \
1073 MU_IF(MU_COUNT_ARG(__VA_ARGS__),};,) \
1074 static void MU_C2(fill_mock_call_modifier_,name)(MU_C2(mock_call_modifier_,name)* mock_call_modifier) \
1075 { \
1076 MU_IF(IS_NOT_VOID(return_type),mock_call_modifier->SetReturn = MU_C2(set_return_func_,name); \
1077 mock_call_modifier->SetFailReturn = MU_C2(set_fail_return_func_,name); \
1078 mock_call_modifier->CallCannotFail = MU_C2(call_cannot_fail_func_,name); \
1079 mock_call_modifier->CaptureReturn = MU_C2(capture_return_func_,name);,) \
1080 MU_IF(MU_COUNT_ARG(__VA_ARGS__),mock_call_modifier->IgnoreAllArguments = MU_C2(ignore_all_arguments_func_,name); \
1081 mock_call_modifier->ValidateAllArguments = MU_C2(validate_all_arguments_func_,name); \
1082 mock_call_modifier->CopyOutArgument = MU_C2(copy_out_argument_func_,name); \
1083 mock_call_modifier->IgnoreArgument = MU_C2(ignore_argument_func_,name); \
1084 mock_call_modifier->ValidateArgument = MU_C2(validate_argument_func_,name); \
1085 mock_call_modifier->ValidateArgumentBuffer = MU_C2(validate_argument_buffer_func_,name); \
1086 mock_call_modifier->CopyOutArgumentBuffer = MU_C2(copy_out_argument_buffer_func_,name); \
1087 MU_FOR_EACH_2_KEEP_1(COPY_IGNORE_ARG_BY_NAME_TO_MODIFIER, name, __VA_ARGS__) \
1088 MU_FOR_EACH_2_KEEP_1(COPY_VALIDATE_ARG_BY_NAME_TO_MODIFIER, name, __VA_ARGS__) \
1089 MU_FOR_EACH_2_KEEP_1(COPY_COPY_OUT_ARGUMENT_BUFFER_BY_NAME_TO_MODIFIER, name, __VA_ARGS__) \
1090 MU_FOR_EACH_2_KEEP_1(COPY_VALIDATE_ARGUMENT_VALUE_BY_NAME_TO_MODIFIER, name, __VA_ARGS__) \
1091 MU_FOR_EACH_2_KEEP_1(COPY_VALIDATE_ARGUMENT_VALUE_AS_TYPE_BY_NAME_TO_MODIFIER, name, __VA_ARGS__),) \
1092 MU_FOR_EACH_2_KEEP_1(COPY_CAPTURE_ARGUMENT_VALUE_BY_NAME_TO_MODIFIER, name, __VA_ARGS__) \
1093 mock_call_modifier->IgnoreAllCalls = MU_C2(ignore_all_calls_func_,name); \
1094 } \
1095 typedef struct MU_C2(_mock_call_,name) \
1096 { \
1097 MU_IF(IS_NOT_VOID(return_type),return_type return_value; \
1098 return_type fail_return_value; \
1099 return_type* captured_return_value;,) \
1100 MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_FOR_EACH_2(DECLARE_MOCK_CALL_STRUCT_STACK, __VA_ARGS__) \
1101 MU_FOR_EACH_2(DECLARE_OUT_ARG_BUFFER_FOR_ARG, __VA_ARGS__) \
1102 ARG_BUFFER* out_arg_buffers[MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)),1)]; \
1103 ARG_BUFFER validate_arg_buffers[MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)),1)]; \
1104 MU_FOR_EACH_2(DECLARE_VALIDATE_ARG_VALUE, __VA_ARGS__) \
1105 MU_FOR_EACH_2(DECLARE_CAPTURE_ARG_VALUE, __VA_ARGS__) \
1106 MU_FOR_EACH_2(DECLARE_IGNORE_FLAG_FOR_ARG, __VA_ARGS__) \
1107 MU_FOR_EACH_2(DECLARE_OVERRIDE_ARGUMENT_TYPE_FOR_ARG, __VA_ARGS__),) \
1108 MU_IF(IS_NOT_VOID(return_type),int fail_return_value_set : 1; \
1109 int return_value_set : 1;,) \
1110 MU_IF(MU_COUNT_ARG(__VA_ARGS__), , MU_IF(IS_NOT_VOID(return_type),, int dummy : 1;)) \
1111 } MU_C2(mock_call_,name); \
1112 typedef MU_C2(mock_call_modifier_,name) (*MU_C3(auto_ignore_args_function_,name,_type))(MU_C2(mock_call_modifier_,name) call_modifier, const char* call_as_string); \
1113 MU_C2(mock_call_modifier_,name) UMOCK_STATIC MU_C2(auto_ignore_args_function_,name)(MU_C2(mock_call_modifier_,name) call_modifier, const char* call_as_string) \
1114 { \
1115 MU_C2(mock_call_modifier_,name) result = call_modifier; \
1116 MU_IF(MU_COUNT_ARG(__VA_ARGS__), \
1117 int is_ignored; \
1118 int arg_index = 1; ,) \
1119 (void)call_as_string; \
1120 MU_FOR_EACH_2(AUTO_IGNORE_ARG, __VA_ARGS__) \
1121 return result; \
1122 } \
1123 MU_C3(auto_ignore_args_function_,name,_type) UMOCK_STATIC MU_C2(get_auto_ignore_args_function_,name)(MU_IF(MU_COUNT_ARG(__VA_ARGS__),,void) MU_FOR_EACH_2_COUNTED(ARG_IN_SIGNATURE, __VA_ARGS__)) \
1124 { \
1125 MU_FOR_EACH_2(UNUSED_ARG, __VA_ARGS__) \
1126 return MU_C2(auto_ignore_args_function_,name); \
1127 } \
1128 UMOCK_STATIC char* MU_C2(mock_call_data_stringify_,name)(void* mock_call_data) \
1129 { \
1130 char* result; \
1131 MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_C2(mock_call_,name)* typed_mock_call_data = (MU_C2(mock_call_,name)*)mock_call_data;,) \
1132 int is_error = 0; \
1133 size_t args_string_length = 0; \
1134 MU_FOR_EACH_2_COUNTED(STRINGIFY_ARGS_DECLARE_RESULT_VAR, __VA_ARGS__) \
1135 MU_IF(MU_COUNT_ARG(__VA_ARGS__),,(void)mock_call_data;) \
1136 MU_FOR_EACH_2(STRINGIFY_ARGS_CHECK_ARG_STRINGIFY_SUCCESS, __VA_ARGS__) \
1137 (void)mock_call_data; \
1138 if (is_error != 0) \
1139 { \
1140 result = NULL; \
1141 } \
1142 else \
1143 { \
1144 MU_FOR_EACH_2(STRINGIFY_ARGS_DECLARE_ARG_STRING_LENGTH, __VA_ARGS__) \
1145 MU_FOR_EACH_2(STRINGIFY_ARGS_COUNT_LENGTH, __VA_ARGS__) \
1146 MU_IF(MU_COUNT_ARG(__VA_ARGS__), args_string_length += MU_COUNT_ARG(__VA_ARGS__) - 1;,) \
1147 result = (char*)umockalloc_malloc(args_string_length + 1); \
1148 if (result != NULL) \
1149 { \
1150 if (args_string_length == 0) \
1151 { \
1152 result[0] = '\0'; \
1153 } \
1154 else \
1155 { \
1156 MU_IF(MU_COUNT_ARG(__VA_ARGS__), \
1157 size_t current_pos = 0; \
1158 size_t arg_index = 0; \
1159 MU_FOR_EACH_2(STRINGIFY_ARGS_COPY_ARG_STRING, __VA_ARGS__), ) \
1160 } \
1161 } \
1162 } \
1163 MU_FOR_EACH_2(STRINGIFY_ARGS_FREE_STRINGIFIED_ARG, __VA_ARGS__) \
1164 return result; \
1165 } \
1166 UMOCK_STATIC int MU_C2(mock_call_data_are_equal_,name)(void* left, void* right) \
1167 { \
1168 int result; \
1169 if (left == right) \
1170 { \
1171 result = 1; \
1172 } \
1173 else if ((left == NULL) || (right == NULL)) \
1174 { \
1175 result = 0; \
1176 } \
1177 else \
1178 { \
1179 result = 1; \
1180 MU_IF(MU_COUNT_ARG(__VA_ARGS__), \
1181 { \
1182 MU_C2(mock_call_,name)* typed_left = (MU_C2(mock_call_,name)*)left; \
1183 MU_C2(mock_call_,name)* typed_right = (MU_C2(mock_call_,name)*)right; \
1184 MU_FOR_EACH_2_COUNTED(ARE_EQUAL_FOR_ARG, __VA_ARGS__) \
1185 }, ) \
1186 } \
1187 return result; \
1188 } \
1189 UMOCK_STATIC void MU_C2(mock_call_data_free_func_,name)(void* mock_call_data) \
1190 { \
1191 MU_C2(mock_call_,name)* typed_mock_call_data = (MU_C2(mock_call_,name)*)mock_call_data; \
1192 MU_FOR_EACH_2_COUNTED(FREE_ARG_VALUE, __VA_ARGS__) \
1193 MU_FOR_EACH_2_COUNTED(FREE_OUT_ARG_BUFFERS, __VA_ARGS__) \
1194 MU_FOR_EACH_2_COUNTED(FREE_VALIDATE_ARG_BUFFERS, __VA_ARGS__) \
1195 MU_FOR_EACH_2_COUNTED(FREE_OVERRIDE_ARGUMENT_TYPE, __VA_ARGS__) \
1196 MU_IF(IS_NOT_VOID(return_type),if (typed_mock_call_data->return_value_set == RETURN_VALUE_SET) \
1197 { \
1198 umocktypes_free(MU_TOSTRING(return_type), (void*)&typed_mock_call_data->return_value); \
1199 } \
1200 if (typed_mock_call_data->fail_return_value_set == FAIL_RETURN_VALUE_SET) \
1201 { \
1202 umocktypes_free(MU_TOSTRING(return_type), (void*)&typed_mock_call_data->fail_return_value); \
1203 },) \
1204 umockalloc_free(typed_mock_call_data); \
1205 } \
1206 UMOCK_STATIC void* MU_C2(mock_call_data_copy_func_,name)(void* mock_call_data) \
1207 { \
1208 MU_C2(mock_call_,name)* result = (MU_C2(mock_call_,name)*)umockalloc_malloc(sizeof(MU_C2(mock_call_,name))); \
1209 MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_C2(mock_call_,name)* typed_mock_call_data = (MU_C2(mock_call_,name)*)mock_call_data;,) \
1210 MU_IF(IS_NOT_VOID(return_type), MU_C2(mock_call_,name)* typed_mock_call_data_result = (MU_C2(mock_call_,name)*)mock_call_data;,) \
1211 (void)mock_call_data; \
1212 MU_FOR_EACH_2(COPY_IGNORE_ARG, __VA_ARGS__) \
1213 MU_FOR_EACH_2(COPY_ARG_VALUE, __VA_ARGS__) \
1214 MU_FOR_EACH_2_COUNTED(COPY_OUT_ARG_BUFFERS, __VA_ARGS__) \
1215 MU_FOR_EACH_2_COUNTED(COPY_VALIDATE_ARG_BUFFERS, __VA_ARGS__) \
1216 MU_FOR_EACH_2(COPY_VALIDATE_ARG_VALUE, __VA_ARGS__) \
1217 MU_FOR_EACH_2(COPY_OVERRIDE_ARGUMENT_TYPE, __VA_ARGS__) \
1218 MU_FOR_EACH_2(COPY_CAPTURE_ARG_VALUE, __VA_ARGS__) \
1219 MU_IF(IS_NOT_VOID(return_type), \
1220 result->return_value_set = typed_mock_call_data_result->return_value_set; \
1221 result->fail_return_value_set = typed_mock_call_data_result->fail_return_value_set; \
1222 if (typed_mock_call_data_result->return_value_set == RETURN_VALUE_SET) \
1223 { \
1224 umocktypes_copy(MU_TOSTRING(return_type), (void*)&result->return_value, (void*)&typed_mock_call_data_result->return_value); \
1225 } \
1226 result->captured_return_value = typed_mock_call_data_result->captured_return_value; \
1227 if (typed_mock_call_data_result->fail_return_value_set == FAIL_RETURN_VALUE_SET) \
1228 { \
1229 umocktypes_copy(MU_TOSTRING(return_type), (void*)&result->fail_return_value, (void*)&typed_mock_call_data_result->fail_return_value); \
1230 },) \
1231 return result; \
1232 } \
1233 MU_IF(IS_NOT_VOID(return_type), \
1234 IMPLEMENT_SET_RETURN_FUNCTION(return_type, name, __VA_ARGS__) \
1235 IMPLEMENT_SET_FAIL_RETURN_FUNCTION(return_type, name, __VA_ARGS__) \
1236 IMPLEMENT_SET_CALL_CANNOT_FAIL(return_type, name, __VA_ARGS__) \
1237 IMPLEMENT_CAPTURE_RETURN_FUNCTION(return_type, name, __VA_ARGS__),) \
1238 MU_IF(MU_COUNT_ARG(__VA_ARGS__),IMPLEMENT_IGNORE_ALL_ARGUMENTS_FUNCTION(return_type, name, __VA_ARGS__) \
1239 IMPLEMENT_VALIDATE_ALL_ARGUMENTS_FUNCTION(return_type, name, __VA_ARGS__) \
1240 MU_FOR_EACH_2_KEEP_1(IMPLEMENT_IGNORE_ARGUMENT_BY_NAME_FUNCTION, name, __VA_ARGS__) \
1241 MU_FOR_EACH_2_KEEP_1(IMPLEMENT_VALIDATE_ARGUMENT_BY_NAME_FUNCTION, name, __VA_ARGS__) \
1242 IMPLEMENT_IGNORE_ARGUMENT_FUNCTION(return_type, name, __VA_ARGS__) \
1243 IMPLEMENT_VALIDATE_ARGUMENT_FUNCTION(return_type, name, __VA_ARGS__) \
1244 IMPLEMENT_COPY_OUT_ARGUMENT_BUFFER_FUNCTION(return_type, name, __VA_ARGS__) \
1245 MU_FOR_EACH_2_KEEP_1(IMPLEMENT_COPY_OUT_ARGUMENT_BUFFER_BY_NAME_FUNCTION, name, __VA_ARGS__) \
1246 IMPLEMENT_COPY_OUT_ARGUMENT_FUNCTION(return_type, name, __VA_ARGS__) \
1247 IMPLEMENT_VALIDATE_ARGUMENT_BUFFER_FUNCTION(return_type, name, __VA_ARGS__) \
1248 MU_FOR_EACH_2_KEEP_1(IMPLEMENT_VALIDATE_ARGUMENT_VALUE_BY_NAME_FUNCTION, name, __VA_ARGS__) \
1249 MU_FOR_EACH_2_KEEP_1(IMPLEMENT_VALIDATE_ARGUMENT_VALUE_AS_TYPE_BY_NAME_FUNCTION, name, __VA_ARGS__),) \
1250 MU_FOR_EACH_2_KEEP_1(IMPLEMENT_CAPTURE_ARGUMENT_VALUE_BY_NAME_FUNCTION, name, __VA_ARGS__) \
1251 IMPLEMENT_IGNORE_ALL_CALLS_FUNCTION(return_type, name, __VA_ARGS__) \
1252 IMPLEMENT_REGISTER_GLOBAL_MOCK_HOOK(return_type, name, __VA_ARGS__) \
1253 IMPLEMENT_REGISTER_GLOBAL_MOCK_RETURN(return_type, name, __VA_ARGS__) \
1254 IMPLEMENT_REGISTER_GLOBAL_MOCK_FAIL_RETURN(return_type, name, __VA_ARGS__) \
1255 IMPLEMENT_REGISTER_GLOBAL_MOCK_RETURNS(return_type, name, __VA_ARGS__) \
1256 IMPLEMENT_STRICT_EXPECTED_MOCK(return_type, name, __VA_ARGS__) \
1257 IMPLEMENT_EXPECTED_MOCK(return_type, name, __VA_ARGS__) \
1258
1259 /* Codes_SRS_UMOCK_C_LIB_01_193: [ When a destroy_call happens the memory block associated with the argument passed to it shall be freed. ] */
1260 /* Codes_SRS_UMOCK_C_LIB_01_195: [ If any error occurs during the destroy_call related then umock_c shall raise an error with the code UMOCK_C_ERROR. ]*/
1261 /* Codes_SRS_UMOCK_C_LIB_01_194: [ If the first argument passed to destroy_call is not found in the list of tracked handles (returned by create_call) then umock_c shall raise an error with the code UMOCK_C_INVALID_PAIRED_CALLS. ]*/
1262 #define MOCKABLE_FUNCTION_BODY_WITHOUT_RETURN(modifiers, return_type, name, ...) \
1263 UMOCK_STATIC return_type modifiers name(MU_IF(MU_COUNT_ARG(__VA_ARGS__),,void) MU_FOR_EACH_2_COUNTED(ARG_IN_SIGNATURE, __VA_ARGS__)) \
1264 { \
1265 UMOCKCALL_HANDLE mock_call; \
1266 UMOCKCALL_HANDLE matched_call; \
1267 MU_IF(IS_NOT_VOID(return_type), \
1268 unsigned int result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 = 0; \
1269 unsigned int fail_result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 = 0; \
1270 void* captured_return_value_C8417226_7442_49B4_BBB9_9CA816A21EB7 = NULL;,) \
1271 MU_IF(IS_NOT_VOID(return_type),TRACK_CREATE_FUNC_TYPE track_create_destroy_pair_malloc_local_C8417226_7442_49B4_BBB9_9CA816A21EB7 = MU_C2(track_create_destroy_pair_malloc_,name); \
1272 PAIRED_HANDLES* used_paired_handles_local_C8417226_7442_49B4_BBB9_9CA816A21EB7 = MU_C2(used_paired_handles_,name); \
1273 const char* return_type_string_C8417226_7442_49B4_BBB9_9CA816A21EB7 = MU_TOSTRING(return_type); \
1274 (void)return_type_string_C8417226_7442_49B4_BBB9_9CA816A21EB7; \
1275 (void)track_create_destroy_pair_malloc_local_C8417226_7442_49B4_BBB9_9CA816A21EB7; \
1276 (void)used_paired_handles_local_C8417226_7442_49B4_BBB9_9CA816A21EB7;,) \
1277 MU_IF(IS_NOT_VOID(return_type),return_type result_C8417226_7442_49B4_BBB9_9CA816A21EB7 = MU_C2(get_mock_call_return_values_,name)()->success_value;,) \
1278 MU_C2(mock_call_,name)* matched_call_data; \
1279 MU_C2(mock_call_,name)* mock_call_data = (MU_C2(mock_call_,name)*)umockalloc_malloc(sizeof(MU_C2(mock_call_,name))); \
1280 MU_FOR_EACH_2(COPY_ARG_TO_MOCK_STRUCT, __VA_ARGS__) \
1281 MU_FOR_EACH_2(MARK_ARG_AS_NOT_IGNORED, __VA_ARGS__) \
1282 MU_FOR_EACH_2_COUNTED(CLEAR_OUT_ARG_BUFFERS, __VA_ARGS__) \
1283 MU_FOR_EACH_2_COUNTED(CLEAR_VALIDATE_ARG_BUFFERS, __VA_ARGS__) \
1284 MU_FOR_EACH_2(CLEAR_VALIDATE_ARG_VALUE, __VA_ARGS__) \
1285 MU_FOR_EACH_2(CLEAR_CAPTURE_ARG_VALUE, __VA_ARGS__) \
1286 MU_FOR_EACH_2(CLEAR_OVERRIDE_ARGUMENT_TYPE, __VA_ARGS__) \
1287 MU_IF(IS_NOT_VOID(return_type),mock_call_data->return_value_set = RETURN_VALUE_NOT_SET; \
1288 mock_call_data->captured_return_value = NULL; \
1289 mock_call_data->fail_return_value_set = FAIL_RETURN_VALUE_NOT_SET;,) \
1290 mock_call = umockcall_create(#name, mock_call_data, MU_C2(mock_call_data_copy_func_,name), MU_C2(mock_call_data_free_func_,name), MU_C2(mock_call_data_stringify_,name), MU_C2(mock_call_data_are_equal_,name)); \
1291 if (mock_call == NULL) \
1292 { \
1293 MU_IF(IS_NOT_VOID(return_type),COPY_RETURN_VALUE(return_type, name),) \
1294 UMOCK_LOG("Could not create a mock call in the actual call for %s.", MU_TOSTRING(name)); \
1295 umock_c_indicate_error(UMOCK_C_ERROR); \
1296 } \
1297 else \
1298 { \
1299 if (umock_c_add_actual_call(mock_call, &matched_call) != 0) \
1300 { \
1301 umockcall_destroy(mock_call); \
1302 UMOCK_LOG("Could not add an actual call for %s.", MU_TOSTRING(name)); \
1303 umock_c_indicate_error(UMOCK_C_COMPARE_CALL_ERROR); \
1304 } \
1305 if (matched_call != NULL) \
1306 { \
1307 matched_call_data = (MU_C2(mock_call_,name)*)umockcall_get_call_data(matched_call); \
1308 if (matched_call_data != NULL) \
1309 { \
1310 MU_FOR_EACH_2(COPY_CAPTURED_ARGUMENT_VALUE, __VA_ARGS__) \
1311 } \
1312 MU_IF(IS_NOT_VOID(return_type),if (matched_call_data != NULL) \
1313 { \
1314 captured_return_value_C8417226_7442_49B4_BBB9_9CA816A21EB7 = (void*)matched_call_data->captured_return_value; \
1315 if (umockcall_get_fail_call(matched_call)) \
1316 { \
1317 if (matched_call_data->fail_return_value_set == FAIL_RETURN_VALUE_SET) \
1318 { \
1319 result_C8417226_7442_49B4_BBB9_9CA816A21EB7 = matched_call_data->fail_return_value; \
1320 } \
1321 else \
1322 { \
1323 result_C8417226_7442_49B4_BBB9_9CA816A21EB7 = MU_C2(get_mock_call_return_values_, name)()->failure_value; \
1324 } \
1325 result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 = 1; \
1326 fail_result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 = 1; \
1327 } \
1328 else if (matched_call_data->return_value_set == RETURN_VALUE_SET) \
1329 { \
1330 result_C8417226_7442_49B4_BBB9_9CA816A21EB7 = matched_call_data->return_value; \
1331 result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 = 1; \
1332 } \
1333 else \
1334 { \
1335 if (MU_C2(mock_hook_, name) != NULL) \
1336 { \
1337 MU_IF(IS_NOT_VOID(return_type),result_C8417226_7442_49B4_BBB9_9CA816A21EB7 =,) MU_C2(mock_hook_, name)(MU_FOR_EACH_2_COUNTED(ARG_NAME_ONLY_IN_CALL, __VA_ARGS__)); \
1338 MU_IF(IS_NOT_VOID(return_type),result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 = 1;,) \
1339 } \
1340 } \
1341 }, \
1342 if (MU_C2(mock_hook_, name) != NULL) \
1343 { \
1344 MU_C2(mock_hook_, name)(MU_FOR_EACH_2_COUNTED(ARG_NAME_ONLY_IN_CALL, __VA_ARGS__)); \
1345 } \
1346 ) \
1347 MU_IF(MU_COUNT_ARG(__VA_ARGS__), MU_FOR_EACH_2_COUNTED(COPY_OUT_ARG_VALUE_FROM_MATCHED_CALL, __VA_ARGS__),) \
1348 } \
1349 else \
1350 { \
1351 if (MU_C2(mock_hook_, name) != NULL) \
1352 { \
1353 MU_IF(IS_NOT_VOID(return_type),result_C8417226_7442_49B4_BBB9_9CA816A21EB7 =,) MU_C2(mock_hook_, name)(MU_FOR_EACH_2_COUNTED(ARG_NAME_ONLY_IN_CALL, __VA_ARGS__)); \
1354 MU_IF(IS_NOT_VOID(return_type),result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 = 1;,) \
1355 } \
1356 } \
1357 } \
1358 MU_IF(MU_COUNT_ARG(__VA_ARGS__), if (MU_C2(track_create_destroy_pair_free_, name) != NULL) \
1359 { \
1360 if (MU_C2(track_create_destroy_pair_free_, name)(MU_C2(used_paired_handles_, name), (void*)&ONLY_FIRST_ARG(__VA_ARGS__, 1)) != 0) \
1361 { \
1362 UMOCK_LOG("Could not track the destroy call for %s.", MU_TOSTRING(name)); \
1363 umock_c_indicate_error(UMOCK_C_ERROR); \
1364 } \
1365 },) \
1366 { \
1367
1368 #define UMOCK_GENERATE_DEFAULT_RETURNS(success_return_value, failure_return_value) \
1369 = { success_return_value, failure_return_value }; \
1370 if (!result->initialized) \
1371 { \
1372 result->success_value = temp.success_value; \
1373 result->failure_value = temp.failure_value; \
1374 } \
1375 return result; \
1376 } \
1377
1378 /* Codes_SRS_UMOCK_C_LIB_01_188: [ The create call shall have a non-void return type. ]*/
1379 /* Codes_SRS_UMOCK_C_LIB_01_191: [ At each create_call a memory block shall be allocated so that it can be reported as a leak by any memory checker. ]*/
1380 /* Codes_SRS_UMOCK_C_LIB_01_192: [ If any error occurs during the create_call related then umock_c shall raise an error with the code UMOCK_C_ERROR. ]*/
1381 /* Codes_SRS_UMOCK_C_LIB_01_204: [ Tracking of paired calls shall not be done if the actual call to the create_call is using the SetFailReturn call modifier. ]*/
1382 #define MOCKABLE_FUNCTION_UMOCK_INTERNAL_WITH_MOCK(do_returns, modifiers, return_type, name, ...) \
1383 MOCKABLE_FUNCTION_UMOCK_INTERNAL_WITH_MOCK_NO_CODE(do_returns, return_type, name, __VA_ARGS__) \
1384 MOCKABLE_FUNCTION_BODY_WITHOUT_RETURN(modifiers, return_type, name, __VA_ARGS__) \
1385 MU_IF(IS_NOT_VOID(return_type), \
1386 if (result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 == 0) \
1387 { \
1388 COPY_RETURN_VALUE(return_type, name) \
1389 }; \
1390 if (captured_return_value_C8417226_7442_49B4_BBB9_9CA816A21EB7 != NULL) \
1391 { \
1392 (void)memcpy(captured_return_value_C8417226_7442_49B4_BBB9_9CA816A21EB7, (void*)&result_C8417226_7442_49B4_BBB9_9CA816A21EB7, sizeof(result_C8417226_7442_49B4_BBB9_9CA816A21EB7)); \
1393 } \
1394 if ((track_create_destroy_pair_malloc_local_C8417226_7442_49B4_BBB9_9CA816A21EB7 != NULL) && (fail_result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 == 0)) \
1395 { \
1396 if (track_create_destroy_pair_malloc_local_C8417226_7442_49B4_BBB9_9CA816A21EB7(used_paired_handles_local_C8417226_7442_49B4_BBB9_9CA816A21EB7, (const void*)&result_C8417226_7442_49B4_BBB9_9CA816A21EB7, return_type_string_C8417226_7442_49B4_BBB9_9CA816A21EB7, sizeof(result_C8417226_7442_49B4_BBB9_9CA816A21EB7)) != 0) \
1397 { \
1398 UMOCK_LOG("Could not track the create call for %s.", MU_TOSTRING(name)); \
1399 umock_c_indicate_error(UMOCK_C_ERROR); \
1400 } \
1401 } \
1402 return result_C8417226_7442_49B4_BBB9_9CA816A21EB7;,) \
1403 } \
1404 } \
1405 MU_IF(IS_NOT_VOID(return_type), \
1406 MU_IF(do_returns, \
1407 static MU_C2(UMOCK_RETURNS_VALUES_STRUCT_, name)* MU_C2(get_mock_call_return_values_, name)(void) \
1408 { \
1409 MU_C2(UMOCK_RETURNS_VALUES_STRUCT_, name)* result = &MU_C2(mock_call_return_values_, name); \
1410 const MU_C2(UMOCK_RETURNS_VALUES_STRUCT_, name) temp \
1411 UMOCK_GENERATE_DEFAULT_RETURNS,) \
1412 ,) \
1413
1414 /* Codes_SRS_UMOCK_C_LIB_01_150: [ MOCK_FUNCTION_WITH_CODE shall define a mock function and allow the user to embed code between this define and a MOCK_FUNCTION_END call. ]*/
1415 #define MOCK_FUNCTION_WITH_CODE(modifiers, return_type, name, ...) \
1416 MOCKABLE_FUNCTION_UMOCK_INTERNAL_WITH_MOCK_NO_CODE(0, return_type, name, __VA_ARGS__) \
1417 MOCKABLE_FUNCTION_BODY_WITHOUT_RETURN(modifiers, return_type, name, __VA_ARGS__) \
1418
1419 #define MOCKABLE_FUNCTION_INTERNAL_WITH_CODE(modifiers, return_type, name, ...) \
1420 MOCKABLE_FUNCTION_UMOCK_INTERNAL_WITH_MOCK_NO_CODE(0, return_type, name, __VA_ARGS__) \
1421 MOCKABLE_FUNCTION_BODY_WITHOUT_RETURN(modifiers, return_type, name, __VA_ARGS__) \
1422 MU_IF(IS_NOT_VOID(return_type), if (result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 == 0),) \
1423 { \
1424
1425 /* Codes_SRS_UMOCK_C_LIB_01_188: [ The create call shall have a non-void return type. ]*/
1426 /* Codes_SRS_UMOCK_C_LIB_01_191: [ At each create_call a memory block shall be allocated so that it can be reported as a leak by any memory checker. ]*/
1427 /* Codes_SRS_UMOCK_C_LIB_01_192: [ If any error occurs during the create_call related then umock_c shall raise an error with the code UMOCK_C_ERROR. ]*/
1428 /* Codes_SRS_UMOCK_C_LIB_01_204: [ Tracking of paired calls shall not be done if the actual call to the create_call is using the SetFailReturn call modifier. ]*/
1429 #define MOCK_FUNCTION_END(...) \
1430 MU_IF(MU_COUNT_ARG(__VA_ARGS__), if (result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 == 0) \
1431 { \
1432 result_C8417226_7442_49B4_BBB9_9CA816A21EB7 = __VA_ARGS__; \
1433 }; \
1434 if (captured_return_value_C8417226_7442_49B4_BBB9_9CA816A21EB7 != NULL) \
1435 { \
1436 (void)memcpy(captured_return_value_C8417226_7442_49B4_BBB9_9CA816A21EB7, (void*)&result_C8417226_7442_49B4_BBB9_9CA816A21EB7, sizeof(result_C8417226_7442_49B4_BBB9_9CA816A21EB7)); \
1437 } \
1438 if ((track_create_destroy_pair_malloc_local_C8417226_7442_49B4_BBB9_9CA816A21EB7 != NULL) && (fail_result_value_set_C8417226_7442_49B4_BBB9_9CA816A21EB7 == 0)) \
1439 { \
1440 if (track_create_destroy_pair_malloc_local_C8417226_7442_49B4_BBB9_9CA816A21EB7(used_paired_handles_local_C8417226_7442_49B4_BBB9_9CA816A21EB7, (const void*)&result_C8417226_7442_49B4_BBB9_9CA816A21EB7, return_type_string_C8417226_7442_49B4_BBB9_9CA816A21EB7, sizeof(result_C8417226_7442_49B4_BBB9_9CA816A21EB7)) != 0) \
1441 { \
1442 UMOCK_LOG("Could not track the create call for %s.", MU_TOSTRING(name)); \
1443 umock_c_indicate_error(UMOCK_C_ERROR); \
1444 } \
1445 } \
1446 return result_C8417226_7442_49B4_BBB9_9CA816A21EB7;,) \
1447 } \
1448 }
1449
1450 #define MOCKABLE_FUNCTION_END(...) \
1451 } \
1452 MOCK_FUNCTION_END(__VA_ARGS__)
1453
1454 /* Codes_SRS_UMOCK_C_LIB_01_187: [ REGISTER_UMOCKC_PAIRED_CREATE_DESTROY_CALLS shall register with umock two calls that are expected to be paired. ]*/
1455 /* Codes_SRS_UMOCK_C_LIB_01_190: [ If create_call or destroy_call do not obey these rules, at the time of calling REGISTER_UMOCKC_PAIRED_CREATE_DESTROY_CALLS umock_c shall raise an error with the code UMOCK_C_INVALID_PAIRED_CALLS. ]*/
1456 /* Codes_SRS_UMOCK_C_LIB_01_189: [ The destroy call shall take as argument at least one argument. The type of the first argument shall be of the same type as the return type for the create_call. ]*/
1457 #define REGISTER_UMOCKC_PAIRED_CREATE_DESTROY_CALLS(create_call, destroy_call) \
1458 if ((strcmp(MU_C2(mock_call_metadata_,create_call).return_type, "void") == 0) || \
1459 (MU_C2(mock_call_metadata_,destroy_call).arg_count == 0) || \
1460 (strcmp(MU_C2(mock_call_metadata_,create_call).return_type, MU_C2(mock_call_metadata_, destroy_call).args[0].type) != 0)) \
1461 { \
1462 umock_c_indicate_error(UMOCK_C_INVALID_PAIRED_CALLS); \
1463 } \
1464 else \
1465 { \
1466 MU_C2(track_create_destroy_pair_malloc_, create_call) = umockcallpairs_track_create_paired_call; \
1467 MU_C2(track_create_destroy_pair_free_, destroy_call) = umockcallpairs_track_destroy_paired_call; \
1468 MU_C2(used_paired_handles_, create_call) = &MU_C2(paired_handles_, create_call); \
1469 MU_C2(used_paired_handles_, destroy_call) = &MU_C2(paired_handles_, create_call); \
1470 }
1471
1472 /* Codes_SRS_UMOCK_C_LIB_01_219: [ REGISTER_GLOBAL_INTERFACE_HOOKS shall register as mock hooks the real functions for all the functions in a mockable interface. ]*/
1473 #define REGISTER_GLOBAL_INTERFACE_HOOKS(interface_name) \
1474 MU_C2(register_reals_, interface_name)()
1475
1476 /* Codes_SRS_UMOCK_C_LIB_01_220: [ UMOCK_REAL shall produce the name of the real function generated by umock. ]*/
1477 #define UMOCK_REAL(function_name) \
1478 MU_C2(real_, function_name)
1479
1480 #ifdef __cplusplus
1481 }
1482 #endif
1483
1484 #endif /* UMOCK_C_INTERNAL_H */