| 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_H |
| 5 |
#define UMOCK_C_H |
| 6 |
|
| 7 |
#ifdef __cplusplus |
| 8 |
#include <cstdlib> |
| 9 |
#else |
| 10 |
#include <stdlib.h> |
| 11 |
#endif |
| 12 |
|
| 13 |
#include "azure_macro_utils/macro_utils.h" |
| 14 |
#include "umock_c/umockcallrecorder.h" |
| 15 |
|
| 16 |
/* Define UMOCK_STATIC to static to make mocks private to compilation unit */ |
| 17 |
#ifndef UMOCK_STATIC |
| 18 |
#define UMOCK_STATIC |
| 19 |
#endif |
| 20 |
|
| 21 |
#define UMOCK_C_ERROR_CODE_VALUES \ |
| 22 |
UMOCK_C_ARG_INDEX_OUT_OF_RANGE, \ |
| 23 |
UMOCK_C_MALLOC_ERROR, \ |
| 24 |
UMOCK_C_INVALID_ARGUMENT_BUFFER, \ |
| 25 |
UMOCK_C_COMPARE_CALL_ERROR, \ |
| 26 |
UMOCK_C_RESET_CALLS_ERROR, \ |
| 27 |
UMOCK_C_CAPTURE_RETURN_ALREADY_USED, \ |
| 28 |
UMOCK_C_NULL_ARGUMENT, \ |
| 29 |
UMOCK_C_INVALID_PAIRED_CALLS, \ |
| 30 |
UMOCK_C_REGISTER_TYPE_FAILED, \ |
| 31 |
UMOCK_C_ERROR |
| 32 |
|
| 33 |
MU_DEFINE_ENUM(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES) |
| 34 |
|
| 35 |
/* This is the type for the error callback by which umock_c shall indicate errors to the user */ |
| 36 |
typedef void(*ON_UMOCK_C_ERROR)(UMOCK_C_ERROR_CODE error_code); |
| 37 |
|
| 38 |
#define IGNORED_PTR_ARG (NULL) |
| 39 |
#define IGNORED_NUM_ARG (0) |
| 40 |
|
| 41 |
#define REGISTER_GLOBAL_MOCK_HOOK(mock_function, mock_hook_function) \ |
| 42 |
MU_C2(set_global_mock_hook_,mock_function)(mock_hook_function); |
| 43 |
|
| 44 |
#define REGISTER_GLOBAL_MOCK_RETURN(mock_function, return_value) \ |
| 45 |
MU_C2(set_global_mock_return_,mock_function)(return_value); |
| 46 |
|
| 47 |
#define REGISTER_GLOBAL_MOCK_FAIL_RETURN(mock_function, fail_return_value) \ |
| 48 |
MU_C2(set_global_mock_fail_return_,mock_function)(fail_return_value); |
| 49 |
|
| 50 |
#define REGISTER_GLOBAL_MOCK_RETURNS(mock_function, return_value, fail_return_value) \ |
| 51 |
MU_C2(set_global_mock_returns_,mock_function)(return_value, fail_return_value); |
| 52 |
|
| 53 |
/* Codes_SRS_UMOCK_C_LIB_01_013: [STRICT_EXPECTED_CALL shall record that a certain call is expected.] */ |
| 54 |
#define STRICT_EXPECTED_CALL(call) \ |
| 55 |
MU_C2(get_auto_ignore_args_function_, call)(MU_C2(umock_c_strict_expected_,call), #call) |
| 56 |
|
| 57 |
#define EXPECTED_CALL(call) \ |
| 58 |
MU_C2(umock_c_expected_,call) |
| 59 |
|
| 60 |
#define DECLARE_UMOCK_POINTER_TYPE_FOR_TYPE(value_type, alias) \ |
| 61 |
char* MU_C3(stringify_func_,alias,ptr)(const value_type** value) \ |
| 62 |
{ \ |
| 63 |
char temp_buffer[32]; \ |
| 64 |
char* result; \ |
| 65 |
size_t length = sprintf(temp_buffer, "%p", (void*)*value); \ |
| 66 |
if (length < 0) \ |
| 67 |
{ \ |
| 68 |
result = NULL; \ |
| 69 |
} \ |
| 70 |
else \ |
| 71 |
{ \ |
| 72 |
result = (char*)malloc(length + 1); \ |
| 73 |
if (result != NULL) \ |
| 74 |
{ \ |
| 75 |
(void)memcpy(result, temp_buffer, length + 1); \ |
| 76 |
} \ |
| 77 |
} \ |
| 78 |
return result; \ |
| 79 |
} \ |
| 80 |
int MU_C3(are_equal_func_,alias,ptr)(const value_type** left, const value_type** right) \ |
| 81 |
{ \ |
| 82 |
return *left == *right; \ |
| 83 |
} \ |
| 84 |
int MU_C3(copy_func_,alias,ptr)(value_type** destination, const value_type** source) \ |
| 85 |
{ \ |
| 86 |
*destination = (value_type*)*source; \ |
| 87 |
return 0; \ |
| 88 |
} \ |
| 89 |
void MU_C3(free_func_,alias,ptr)(value_type** value) \ |
| 90 |
{ \ |
| 91 |
(void)value; \ |
| 92 |
} \ |
| 93 |
|
| 94 |
#ifdef __cplusplus |
| 95 |
extern "C" { |
| 96 |
#endif |
| 97 |
|
| 98 |
void umock_c_indicate_error(UMOCK_C_ERROR_CODE error_code); |
| 99 |
int umock_c_init(ON_UMOCK_C_ERROR on_umock_c_error); |
| 100 |
void umock_c_deinit(void); |
| 101 |
void umock_c_reset_all_calls(void); |
| 102 |
const char* umock_c_get_actual_calls(void); |
| 103 |
const char* umock_c_get_expected_calls(void); |
| 104 |
UMOCKCALLRECORDER_HANDLE umock_c_get_call_recorder(void); |
| 105 |
int umock_c_set_call_recorder(UMOCKCALLRECORDER_HANDLE umockc_call_recorder); |
| 106 |
|
| 107 |
#ifdef __cplusplus |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
/* Codes_SRS_UMOCK_C_LIB_01_065: [REGISTER_UMOCK_VALUE_TYPE shall register the type identified by value_type to be usable by umock_c for argument and return types and instruct umock_c which functions to use for getting the stringify, are_equal, copy and free.]*/ |
| 112 |
/* Codes_SRS_UMOCK_C_LIB_01_197: [ If REGISTER_UMOCK_VALUE_TYPE fails, the on_error callback shall be called with UMOCK_C_REGISTER_TYPE_FAILED. ]*/ |
| 113 |
#define REGISTER_UMOCK_VALUE_TYPE_ALL(value_type, stringify_func, are_equal_func, copy_func, free_func) \ |
| 114 |
{ \ |
| 115 |
if (umocktypes_register_type(MU_TOSTRING(value_type), (UMOCKTYPE_STRINGIFY_FUNC)stringify_func, (UMOCKTYPE_ARE_EQUAL_FUNC)are_equal_func, (UMOCKTYPE_COPY_FUNC)copy_func, (UMOCKTYPE_FREE_FUNC)free_func) != 0) \ |
| 116 |
{ \ |
| 117 |
umock_c_indicate_error(UMOCK_C_REGISTER_TYPE_FAILED); \ |
| 118 |
} \ |
| 119 |
} |
| 120 |
|
| 121 |
/* Codes_SRS_UMOCK_C_LIB_01_066: [If only the value_type is specified in the macro invocation then the stringify, are_equal, copy and free function names shall be automatically derived from the type as: umockvalue_stringify_value_type, umockvalue_are_equal_value_type, umockvalue_copy_value_type, umockvalue_free_value_type.]*/ |
| 122 |
#define REGISTER_UMOCK_VALUE_TYPE_ONLY_TYPE(value_type) \ |
| 123 |
REGISTER_UMOCK_VALUE_TYPE_ALL (value_type, MU_C2(umock_stringify_,value_type), MU_C2(umock_are_equal_,value_type), MU_C2(umock_copy_,value_type), MU_C2(umock_free_,value_type)) |
| 124 |
|
| 125 |
#if _MSC_VER |
| 126 |
#define REGISTER_UMOCK_VALUE_TYPE(...) \ |
| 127 |
MU_IF(MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)), REGISTER_UMOCK_VALUE_TYPE_ALL, REGISTER_UMOCK_VALUE_TYPE_ONLY_TYPE) MU_LPAREN __VA_ARGS__) |
| 128 |
#else |
| 129 |
#define REGISTER_UMOCK_VALUE_TYPE(...) \ |
| 130 |
MU_IF(MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)), REGISTER_UMOCK_VALUE_TYPE_ALL, REGISTER_UMOCK_VALUE_TYPE_ONLY_TYPE) (__VA_ARGS__) |
| 131 |
#endif |
| 132 |
|
| 133 |
/* Codes_SRS_UMOCK_C_LIB_01_149: [ REGISTER_UMOCK_ALIAS_TYPE registers a new alias type for another type. ]*/ |
| 134 |
/* Codes_SRS_UMOCK_C_LIB_02_001: [ If the types do not have the same size the on_error callback shall be called with UMOCK_C_REGISTER_TYPE_FAILED. ]*/ |
| 135 |
/* Codes_SRS_UMOCK_C_LIB_01_198: [ If REGISTER_UMOCK_ALIAS_TYPE fails, the on_error callback shall be called with UMOCK_C_REGISTER_TYPE_FAILED. ]*/ |
| 136 |
#define REGISTER_UMOCK_ALIAS_TYPE(value_type, is_value_type) \ |
| 137 |
if ((sizeof(value_type)!=sizeof(is_value_type)) || umocktypes_register_alias_type(MU_TOSTRING(value_type), MU_TOSTRING(is_value_type)) != 0) \ |
| 138 |
{ \ |
| 139 |
umock_c_indicate_error(UMOCK_C_REGISTER_TYPE_FAILED); \ |
| 140 |
} \ |
| 141 |
|
| 142 |
#define UMOCK_TYPE(value_type) \ |
| 143 |
(const char*)(const void*)(const value_type*)(const void*)MU_TOSTRING(value_type) |
| 144 |
|
| 145 |
#include "umock_c/umock_c_internal.h" |
| 146 |
#include "umock_c/umock_c_prod.h" |
| 147 |
|
| 148 |
#endif /* UMOCK_C_H */ |