39 #define printf example_test_printf 48 #define fprintf example_test_fprintf 56 #define assert(expression) \ 57 mock_assert((int)(expression), #expression, __FILE__, __LINE__) 58 void mock_assert(
const int result,
const char* expression,
const char *file,
66 #define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) 70 #define free(ptr) _test_free(ptr, __FILE__, __LINE__) 71 void*
_test_calloc(
const size_t number_of_elements,
const size_t size,
72 const char* file,
const int line);
73 void _test_free(
void*
const ptr,
const char* file,
const int line);
78 #define main example_main 98 const size_t number_of_operator_functions,
100 const char*
const operator_string);
103 int number_of_arguments,
char *arguments[],
104 const size_t number_of_operator_functions,
106 int *
const number_of_intermediate_values,
107 int **
const intermediate_values,
int *
const error_occurred);
109 static int add(
int a,
int b);
112 static int divide(
int a,
int b);
122 static int add(
int a,
int b) {
144 const size_t number_of_operator_functions,
146 const char*
const operator_string) {
148 assert(!number_of_operator_functions || operator_functions);
149 assert(operator_string != NULL);
151 for (i = 0; i < number_of_operator_functions; i++) {
153 &operator_functions[i];
154 if (strcmp(operator_function->
operator, operator_string) == 0) {
176 int number_of_arguments,
char *arguments[],
177 const size_t number_of_operator_functions,
179 int *
const number_of_intermediate_values,
180 int **
const intermediate_values,
int *
const error_occurred) {
181 char *end_of_integer;
184 assert(!number_of_arguments || arguments);
185 assert(!number_of_operator_functions || operator_functions);
186 assert(error_occurred != NULL);
187 assert(number_of_intermediate_values != NULL);
188 assert(intermediate_values != NULL);
191 *number_of_intermediate_values = 0;
192 *intermediate_values = NULL;
193 if (!number_of_arguments)
197 value = (int)strtol(arguments[0], &end_of_integer, 10);
198 if (end_of_integer == arguments[0]) {
200 fprintf(stderr,
"Unable to parse integer from argument %s\n",
207 *intermediate_values = calloc(((number_of_arguments - 1) / 2),
208 sizeof(**intermediate_values));
211 while (i < number_of_arguments) {
213 const char*
const operator_string = arguments[i];
215 number_of_operator_functions, operator_functions, operator_string);
216 int *
const intermediate_value =
217 &((*intermediate_values)[*number_of_intermediate_values]);
218 (*number_of_intermediate_values) ++;
221 fprintf(stderr,
"Unknown operator %s, argument %d\n",
228 if (i == number_of_arguments) {
229 fprintf(stderr,
"Binary operator %s missing argument\n",
235 other_value = (int)strtol(arguments[i], &end_of_integer, 10);
236 if (end_of_integer == arguments[i]) {
238 fprintf(stderr,
"Unable to parse integer %s of argument %d\n",
246 *intermediate_value =
function(value, other_value);
247 value = *intermediate_value;
249 if (*error_occurred) {
250 free(*intermediate_values);
251 *intermediate_values = NULL;
252 *number_of_intermediate_values = 0;
260 int number_of_intermediate_values;
261 int *intermediate_values;
265 sizeof(operator_function_map) /
sizeof(operator_function_map[0]),
266 operator_function_map, &number_of_intermediate_values,
267 &intermediate_values, &return_value);
270 if (!return_value && argc > 1) {
272 int intermediate_value_index = 0;
273 printf(
"%s\n", argv[1]);
274 for (i = 2; i < argc; i += 2) {
275 assert(intermediate_value_index < number_of_intermediate_values);
276 printf(
" %s %s = %d\n", argv[i], argv[i + 1],
277 intermediate_values[intermediate_value_index++]);
279 printf(
"= %d\n", result);
281 if (intermediate_values) {
282 free(intermediate_values);
int example_main(int argc, char *argv[])
int int example_test_printf(const char *format,...) CMOCKA_PRINTF_ATTRIBUTE(1
int perform_operation(int number_of_arguments, char *arguments[], const size_t number_of_operator_functions, const OperatorFunction *const operator_functions, int *const number_of_intermediate_values, int **const intermediate_values, int *const error_occurred)
void mock_assert(const int result, const char *const expression, const char *const file, const int line)
Function to replace assert(3) in tested code.
static int add(int a, int b)
static OperatorFunction operator_function_map[]
void _test_free(void *const ptr, const char *file, const int line)
static int divide(int a, int b)
int example_test_fprintf(FILE *const file, const char *format,...) CMOCKA_PRINTF_ATTRIBUTE(2
int(* BinaryOperator)(int a, int b)
void print_message(const char *const format,...) CMOCKA_PRINTF_ATTRIBUTE(1
static int subtract(int a, int b)
void * _test_calloc(const size_t number_of_elements, const size_t size, const char *file, const int line)
static int multiply(int a, int b)
struct OperatorFunction OperatorFunction
int main(int argc, char *argv[])
BinaryOperator find_operator_function_by_string(const size_t number_of_operator_functions, const OperatorFunction *const operator_functions, const char *const operator_string)