summaryrefslogtreecommitdiff
path: root/cmockery_0_1_2/src/example/calculator_test.c
blob: 6feaf413aa30ec83a112d68aa981c3e4cd0d8106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include "cmockery.h"
#include <stdio.h>

#ifdef _WIN32
// Compatibility with the Windows standard C library.
#define vsnprintf _vsnprintf
#endif // _WIN32

#define array_length(x) (sizeof(x) / sizeof((x)[0]))

/* To simplify this code, these functions and data structures could have been 
 * separated out from the application example.c into a header shared with 
 * test application.  However, this example illustrates how it's possible to
 * test existing code with little modification. */

typedef int (*BinaryOperator)(int a, int b);

typedef struct OperatorFunction {
	const char* operator;
	BinaryOperator function;
} OperatorFunction;

extern int add(int a, int b);
extern int subtract(int a, int b);
extern int multiply(int a, int b);
extern int divide(int a, int b);
extern BinaryOperator find_operator_function_by_string(
        const size_t number_of_operator_functions,
        const OperatorFunction * const operator_functions,
        const char* const operator_string);
extern 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);
extern int example_main(int argc, char *argv[]);

/* A mock fprintf function that checks the value of strings printed to the 
 * standard error stream. */
int example_test_fprintf(FILE* const file, const char *format, ...) {
	int return_value;
	va_list args;
	char temporary_buffer[256];
	assert_true(file == stderr);
	va_start(args, format);
	return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer),
	                         format, args);
	check_expected(temporary_buffer);
	va_end(args);
	return return_value;
}

/* A mock printf function that checks the value of strings printed to the 
 * standard output stream. */
int example_test_printf(const char *format, ...) {
	int return_value;
	va_list args;
	char temporary_buffer[256];
	va_start(args, format);
	return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer),
	                         format, args);
	check_expected(temporary_buffer);
	va_end(args);
	return return_value;
}

// A mock binary operator function.
int binary_operator(int a, int b) {
	check_expected(a);
	check_expected(b);
	return (int)mock();
}


// Ensure add() adds two integers correctly.
void test_add(void **state) {
	assert_int_equal(add(3, 3), 6);
	assert_int_equal(add(3, -3), 0);
}

// Ensure subtract() subtracts two integers correctly.
void test_subtract(void **state) {
	assert_int_equal(subtract(3, 3), 0);
	assert_int_equal(subtract(3, -3), 6);
}

// Ensure multiple() mulitplies two integers correctly.
void test_multiply(void **state) {
	assert_int_equal(multiply(3, 3), 9);
	assert_int_equal(multiply(3, 0), 0);
}

// Ensure divide() divides one integer by another correctly.
void test_divide(void **state) {
	assert_int_equal(divide(10, 2), 5);
	assert_int_equal(divide(2, 10), 0);
}

// Ensure divide() asserts when trying to divide by zero.
void test_divide_by_zero(void **state) {
	expect_assert_failure(divide(100, 0));
}

/* Ensure find_operator_function_by_string() asserts when a NULL pointer is 
 * specified as the table to search. */
void test_find_operator_function_by_string_null_functions(void **state) {
	expect_assert_failure(find_operator_function_by_string(1, NULL, "test"));
}

/* Ensure find_operator_function_by_string() asserts when a NULL pointer is 
 * specified as the string to search for. */
void test_find_operator_function_by_string_null_string(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
	};
	expect_assert_failure(find_operator_function_by_string(
	    array_length(operator_functions), operator_functions, NULL));
}

/* Ensure find_operator_function_by_string() returns NULL when a NULL pointer 
 * is specified as the table to search when the table size is 0. */
void test_find_operator_function_by_string_valid_null_functions(void **state) {
  assert_int_equal((int)find_operator_function_by_string(0, NULL, "test"),
	                     (int)NULL);
}

/* Ensure find_operator_function_by_string() returns NULL when searching for
 * an operator string that isn't in the specified table. */
void test_find_operator_function_by_string_not_found(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
		{"-", binary_operator},
		{"/", binary_operator},
	};
	assert_int_equal((int)find_operator_function_by_string(
	        array_length(operator_functions), operator_functions, "test"),
	    (int)NULL);
}

/* Ensure find_operator_function_by_string() returns the correct function when
 * searching for an operator string that is in the specified table. */
void test_find_operator_function_by_string_found(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", (BinaryOperator)0x12345678},
		{"-", (BinaryOperator)0xDEADBEEF},
		{"/", (BinaryOperator)0xABADCAFE},
	};
	assert_int_equal((int)find_operator_function_by_string(
	        array_length(operator_functions), operator_functions, "-"),
	    0xDEADBEEF);
}

// Ensure perform_operation() asserts when a NULL arguments array is specified.
void test_perform_operation_null_args(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
	};
	int number_of_intermediate_values;
	int *intermediate_values;
	int error_occurred;
	expect_assert_failure(perform_operation(
	    1, NULL, array_length(operator_functions), operator_functions,
	    &number_of_intermediate_values, &intermediate_values, 
	    &error_occurred));
}

/* Ensure perform_operation() asserts when a NULL operator_functions array is 
 * specified. */
void test_perform_operation_null_operator_functions(void **state) {
	char *args[] = {
		"1", "+", "2", "*", "4"
	};
	int number_of_intermediate_values;
	int *intermediate_values;
	int error_occurred;
	expect_assert_failure(perform_operation(
	    array_length(args), args, 1, NULL, &number_of_intermediate_values, 
	    &intermediate_values, &error_occurred));
}

/* Ensure perform_operation() asserts when a NULL pointer is specified for 
 * number_of_intermediate_values. */
void test_perform_operation_null_number_of_intermediate_values(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
	};
	char *args[] = {
		"1", "+", "2", "*", "4"
	};
	int *intermediate_values;
	int error_occurred;	
	expect_assert_failure(perform_operation(
	    array_length(args), args, 1, operator_functions, NULL, 
	    &intermediate_values, &error_occurred));
}

/* Ensure perform_operation() asserts when a NULL pointer is specified for 
 * intermediate_values. */
void test_perform_operation_null_intermediate_values(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
	};
	char *args[] = {
		"1", "+", "2", "*", "4"
	};
	int number_of_intermediate_values;
	int error_occurred;	
	expect_assert_failure(perform_operation(
	    array_length(args), args, array_length(operator_functions),
	    operator_functions, &number_of_intermediate_values, NULL,
	    &error_occurred));
}

// Ensure perform_operation() returns 0 when no arguments are specified.
void test_perform_operation_no_arguments(void **state) {
	int number_of_intermediate_values;
	int *intermediate_values;
	int error_occurred;
	assert_int_equal(perform_operation(
	    0, NULL, 0, NULL, &number_of_intermediate_values, &intermediate_values,
	    &error_occurred), 0);
	assert_int_equal(error_occurred, 0);
}

/* Ensure perform_operation() returns an error if the first argument isn't
 * an integer string. */
void test_perform_operation_first_arg_not_integer(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
	};
	char *args[] = {
		"test", "+", "2", "*", "4"
	};
	int number_of_intermediate_values;
	int *intermediate_values;
	int error_occurred;

	expect_string(example_test_fprintf, temporary_buffer, 
	              "Unable to parse integer from argument test\n");

	assert_int_equal(perform_operation(
	    array_length(args), args, array_length(operator_functions),
	    operator_functions, &number_of_intermediate_values,
	    &intermediate_values, &error_occurred), 0);
	assert_int_equal(error_occurred, 1);
}

/* Ensure perform_operation() returns an error when parsing an unknown 
 * operator. */
void test_perform_operation_unknown_operator(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
	};
	char *args[] = {
		"1", "*", "2", "*", "4"
	};
	int number_of_intermediate_values;
	int *intermediate_values;
	int error_occurred;

	expect_string(example_test_fprintf, temporary_buffer, 
	              "Unknown operator *, argument 1\n");

	assert_int_equal(perform_operation(
	    array_length(args), args, array_length(operator_functions),
	    operator_functions, &number_of_intermediate_values,
	    &intermediate_values, &error_occurred), 0);
	assert_int_equal(error_occurred, 1);
}

/* Ensure perform_operation() returns an error when nothing follows an 
 * operator. */
void test_perform_operation_missing_argument(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
	};
	char *args[] = {
		"1", "+", 
	};
	int number_of_intermediate_values;
	int *intermediate_values;
	int error_occurred;

	expect_string(example_test_fprintf, temporary_buffer, 
	              "Binary operator + missing argument\n");

	assert_int_equal(perform_operation(
	    array_length(args), args, array_length(operator_functions),
	    operator_functions, &number_of_intermediate_values,
	    &intermediate_values, &error_occurred), 0);
	assert_int_equal(error_occurred, 1);
}

/* Ensure perform_operation() returns an error when an integer doesn't follow
 * an operator. */
void test_perform_operation_no_integer_after_operator(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
	};
	char *args[] = {
		"1", "+", "test",
	};
	int number_of_intermediate_values;
	int *intermediate_values;
	int error_occurred;

	expect_string(example_test_fprintf, temporary_buffer, 
	              "Unable to parse integer test of argument 2\n");

	assert_int_equal(perform_operation(
	    array_length(args), args, array_length(operator_functions),
	    operator_functions, &number_of_intermediate_values,
	    &intermediate_values, &error_occurred), 0);
	assert_int_equal(error_occurred, 1);	                    
}


// Ensure perform_operation() succeeds given valid input parameters.
void test_perform_operation(void **state) {
	const OperatorFunction operator_functions[] = {
		{"+", binary_operator},
		{"*", binary_operator},
	};
	char *args[] = {
		"1", "+", "3", "*", "10",
	};
	int number_of_intermediate_values;
	int *intermediate_values;
	int error_occurred;

	// Setup return values of mock operator functions.
	// Addition.
	expect_value(binary_operator, a, 1);
	expect_value(binary_operator, b, 3);
	will_return(binary_operator, 4);

	// Multiplication.
	expect_value(binary_operator, a, 4);
	expect_value(binary_operator, b, 10);
	will_return(binary_operator, 40);

	assert_int_equal(perform_operation(
	    array_length(args), args, array_length(operator_functions),
	    operator_functions, &number_of_intermediate_values,
	    &intermediate_values, &error_occurred), 40);
	assert_int_equal(error_occurred, 0);

	assert_true(intermediate_values);
	assert_int_equal(intermediate_values[0], 4);
	assert_int_equal(intermediate_values[1], 40);
	test_free(intermediate_values);
}


// Ensure main() in example.c succeeds given no arguments.
void test_example_main_no_args(void **state) {
	char *args[] = {
		"example",
	};
	assert_int_equal(example_main(array_length(args), args), 0);
}



// Ensure main() in example.c succeeds given valid input arguments.
void test_example_main(void **state) {
	char *args[] = {
		"example", "1", "+", "3", "*", "10",
	};

	expect_string(example_test_printf, temporary_buffer, "1\n");
	expect_string(example_test_printf, temporary_buffer, "  + 3 = 4\n");
	expect_string(example_test_printf, temporary_buffer, "  * 10 = 40\n");
	expect_string(example_test_printf, temporary_buffer, "= 40\n");

	assert_int_equal(example_main(array_length(args), args), 0);
}


int main(int argc, char* argv[]) {
	UnitTest tests[] = {
		unit_test(test_add),
		unit_test(test_subtract),
		unit_test(test_multiply),
		unit_test(test_divide),
		unit_test(test_divide_by_zero),
		unit_test(test_find_operator_function_by_string_null_functions),
		unit_test(test_find_operator_function_by_string_null_string),
		unit_test(test_find_operator_function_by_string_valid_null_functions),
		unit_test(test_find_operator_function_by_string_not_found),
		unit_test(test_find_operator_function_by_string_found),
		unit_test(test_perform_operation_null_args),
		unit_test(test_perform_operation_null_operator_functions),
		unit_test(test_perform_operation_null_number_of_intermediate_values),
		unit_test(test_perform_operation_null_intermediate_values),
		unit_test(test_perform_operation_no_arguments),
		unit_test(test_perform_operation_first_arg_not_integer),
		unit_test(test_perform_operation_unknown_operator),
		unit_test(test_perform_operation_missing_argument),
		unit_test(test_perform_operation_no_integer_after_operator),
		unit_test(test_perform_operation),
		unit_test(test_example_main_no_args),
		unit_test(test_example_main),
	};
	return run_tests(tests);
}