OpenVPN
calculator_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <stdarg.h>
17 #include <stddef.h>
18 #include <setjmp.h>
19 #include "cmocka.h"
20 #include <stdio.h>
21 
22 #ifdef _WIN32
23 /* Compatibility with the Windows standard C library. */
24 #define vsnprintf _vsnprintf
25 #endif /* _WIN32 */
26 
27 #define array_length(x) (sizeof(x) / sizeof((x)[0]))
28 
29 /* To simplify this code, these functions and data structures could have been
30  * separated out from the application example.c into a header shared with
31  * test application. However, this example illustrates how it's possible to
32  * test existing code with little modification. */
33 
34 typedef int (*BinaryOperator)(int a, int b);
35 
36 typedef struct OperatorFunction {
37  const char* operator;
38  BinaryOperator function;
40 
41 extern int add(int a, int b);
42 extern int subtract(int a, int b);
43 extern int multiply(int a, int b);
44 extern int divide(int a, int b);
46  const size_t number_of_operator_functions,
47  const OperatorFunction * const operator_functions,
48  const char* const operator_string);
49 extern int perform_operation(
50  int number_of_arguments, char *arguments[],
51  const size_t number_of_operator_functions,
52  const OperatorFunction * const operator_functions,
53  int * const number_of_intermediate_values,
54  int ** const intermediate_values, int * const error_occurred);
55 extern int example_main(int argc, char *argv[]);
56 
57 int example_test_fprintf(FILE* const file, const char *format, ...) CMOCKA_PRINTF_ATTRIBUTE(2, 3);
58 int example_test_printf(const char *format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
59 
60 static char temporary_buffer[256];
61 
62 /* A mock fprintf function that checks the value of strings printed to the
63  * standard error stream. */
64 int example_test_fprintf(FILE* const file, const char *format, ...) {
65  int return_value;
66  va_list args;
67  assert_true(file == stderr);
68  va_start(args, format);
69  return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer),
70  format, args);
71  check_expected_ptr(temporary_buffer);
72  va_end(args);
73  return return_value;
74 }
75 
76 /* A mock printf function that checks the value of strings printed to the
77  * standard output stream. */
78 int example_test_printf(const char *format, ...) {
79  int return_value;
80  va_list args;
81  va_start(args, format);
82  return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer),
83  format, args);
85  va_end(args);
86  return return_value;
87 }
88 
89 /* A mock binary operator function. */
90 static int binary_operator(int a, int b) {
91  check_expected(a);
92  check_expected(b);
93  return (int)mock();
94 }
95 
96 
97 /* Ensure add() adds two integers correctly. */
98 static void test_add(void **state) {
99  (void) state; /* unused */
100 
101  assert_int_equal(add(3, 3), 6);
102  assert_int_equal(add(3, -3), 0);
103 }
104 
105 /* Ensure subtract() subtracts two integers correctly. */
106 static void test_subtract(void **state) {
107  (void) state; /* unused */
108 
109  assert_int_equal(subtract(3, 3), 0);
110  assert_int_equal(subtract(3, -3), 6);
111 }
112 
113 /* Ensure multiple() mulitplies two integers correctly. */
114 static void test_multiply(void **state) {
115  (void) state; /* unused */
116 
117  assert_int_equal(multiply(3, 3), 9);
118  assert_int_equal(multiply(3, 0), 0);
119 }
120 
121 /* Ensure divide() divides one integer by another correctly. */
122 static void test_divide(void **state) {
123  (void) state; /* unused */
124 
125  assert_int_equal(divide(10, 2), 5);
126  assert_int_equal(divide(2, 10), 0);
127 }
128 
129 /* Ensure divide() asserts when trying to divide by zero. */
130 static void test_divide_by_zero(void **state) {
131  (void) state; /* unused */
132 
133  expect_assert_failure(divide(100, 0));
134 }
135 
136 /* Ensure find_operator_function_by_string() asserts when a NULL pointer is
137  * specified as the table to search. */
139  (void) state; /* unused */
140 
142 }
143 
144 /* Ensure find_operator_function_by_string() asserts when a NULL pointer is
145  * specified as the string to search for. */
147  const OperatorFunction operator_functions[] = {
148  {"+", binary_operator},
149  };
150 
151  (void) state; /* unused */
152 
154  array_length(operator_functions), operator_functions, NULL));
155 }
156 
157 /* Ensure find_operator_function_by_string() returns NULL when a NULL pointer
158  * is specified as the table to search when the table size is 0. */
160  (void) state; /* unused */
161 
163 }
164 
165 /* Ensure find_operator_function_by_string() returns NULL when searching for
166  * an operator string that isn't in the specified table. */
168  const OperatorFunction operator_functions[] = {
169  {"+", binary_operator},
170  {"-", binary_operator},
171  {"/", binary_operator},
172  };
173 
174  (void) state; /* unused */
175 
177  array_length(operator_functions), operator_functions, "test"));
178 }
179 
180 /* Ensure find_operator_function_by_string() returns the correct function when
181  * searching for an operator string that is in the specified table. */
183  const OperatorFunction operator_functions[] = {
184  {"+", (BinaryOperator)0x12345678},
185  {"-", (BinaryOperator)0xDEADBEEF},
186  {"/", (BinaryOperator)0xABADCAFE},
187  };
188 
189  (void) state; /* unused */
190 
194  operator_functions,
195  "-")),
196  0xDEADBEEF);
197 }
198 
199 /* Ensure perform_operation() asserts when a NULL arguments array is specified. */
200 static void test_perform_operation_null_args(void **state) {
201  const OperatorFunction operator_functions[] = {
202  {"+", binary_operator},
203  };
204  int number_of_intermediate_values;
205  int *intermediate_values;
206  int error_occurred;
207 
208  (void) state; /* unused */
209 
211  1, NULL, array_length(operator_functions), operator_functions,
212  &number_of_intermediate_values, &intermediate_values,
213  &error_occurred));
214 }
215 
216 /* Ensure perform_operation() asserts when a NULL operator_functions array is
217  * specified. */
219  const char *args[] = {
220  "1", "+", "2", "*", "4"
221  };
222  int number_of_intermediate_values;
223  int *intermediate_values;
224  int error_occurred;
225 
226  (void) state; /* unused */
227 
229  array_length(args), (char **) args, 1, NULL, &number_of_intermediate_values,
230  &intermediate_values, &error_occurred));
231 }
232 
233 /* Ensure perform_operation() asserts when a NULL pointer is specified for
234  * number_of_intermediate_values. */
236  const OperatorFunction operator_functions[] = {
237  {"+", binary_operator},
238  };
239  const char *args[] = {
240  "1", "+", "2", "*", "4"
241  };
242  int *intermediate_values;
243  int error_occurred;
244 
245  (void) state; /* unused */
246 
248  array_length(args), (char **) args, 1, operator_functions, NULL,
249  &intermediate_values, &error_occurred));
250 }
251 
252 /* Ensure perform_operation() asserts when a NULL pointer is specified for
253  * intermediate_values. */
255  const OperatorFunction operator_functions[] = {
256  {"+", binary_operator},
257  };
258  const char *args[] = {
259  "1", "+", "2", "*", "4"
260  };
261  int number_of_intermediate_values;
262  int error_occurred;
263 
264  (void) state; /* unused */
265 
267  array_length(args), (char **) args, array_length(operator_functions),
268  operator_functions, &number_of_intermediate_values, NULL,
269  &error_occurred));
270 }
271 
272 /* Ensure perform_operation() returns 0 when no arguments are specified. */
273 static void test_perform_operation_no_arguments(void **state) {
274  int number_of_intermediate_values;
275  int *intermediate_values;
276  int error_occurred;
277 
278  (void) state; /* unused */
279 
281  0, NULL, 0, NULL, &number_of_intermediate_values, &intermediate_values,
282  &error_occurred), 0);
283  assert_int_equal(error_occurred, 0);
284 }
285 
286 /* Ensure perform_operation() returns an error if the first argument isn't
287  * an integer string. */
289  const OperatorFunction operator_functions[] = {
290  {"+", binary_operator},
291  };
292  const char *args[] = {
293  "test", "+", "2", "*", "4"
294  };
295  int number_of_intermediate_values;
296  int *intermediate_values;
297  int error_occurred;
298 
299  (void) state; /* unused */
300 
302  "Unable to parse integer from argument test\n");
303 
305  array_length(args), (char **) args, array_length(operator_functions),
306  operator_functions, &number_of_intermediate_values,
307  &intermediate_values, &error_occurred), 0);
308  assert_int_equal(error_occurred, 1);
309 }
310 
311 /* Ensure perform_operation() returns an error when parsing an unknown
312  * operator. */
313 static void test_perform_operation_unknown_operator(void **state) {
314  const OperatorFunction operator_functions[] = {
315  {"+", binary_operator},
316  };
317  const char *args[] = {
318  "1", "*", "2", "*", "4"
319  };
320  int number_of_intermediate_values;
321  int *intermediate_values;
322  int error_occurred;
323 
324  (void) state; /* unused */
325 
327  "Unknown operator *, argument 1\n");
328 
330  array_length(args), (char **) args, array_length(operator_functions),
331  operator_functions, &number_of_intermediate_values,
332  &intermediate_values, &error_occurred), 0);
333  assert_int_equal(error_occurred, 1);
334 }
335 
336 /* Ensure perform_operation() returns an error when nothing follows an
337  * operator. */
338 static void test_perform_operation_missing_argument(void **state) {
339  const OperatorFunction operator_functions[] = {
340  {"+", binary_operator},
341  };
342  const char *args[] = {
343  "1", "+",
344  };
345  int number_of_intermediate_values;
346  int *intermediate_values;
347  int error_occurred;
348 
349  (void) state; /* unused */
350 
352  "Binary operator + missing argument\n");
353 
355  array_length(args), (char **) args, array_length(operator_functions),
356  operator_functions, &number_of_intermediate_values,
357  &intermediate_values, &error_occurred), 0);
358  assert_int_equal(error_occurred, 1);
359 }
360 
361 /* Ensure perform_operation() returns an error when an integer doesn't follow
362  * an operator. */
364  const OperatorFunction operator_functions[] = {
365  {"+", binary_operator},
366  };
367  const char *args[] = {
368  "1", "+", "test",
369  };
370  int number_of_intermediate_values;
371  int *intermediate_values;
372  int error_occurred;
373 
374  (void) state; /* unused */
375 
377  "Unable to parse integer test of argument 2\n");
378 
380  array_length(args), (char **) args, array_length(operator_functions),
381  operator_functions, &number_of_intermediate_values,
382  &intermediate_values, &error_occurred), 0);
383  assert_int_equal(error_occurred, 1);
384 }
385 
386 
387 /* Ensure perform_operation() succeeds given valid input parameters. */
388 static void test_perform_operation(void **state) {
389  const OperatorFunction operator_functions[] = {
390  {"+", binary_operator},
391  {"*", binary_operator},
392  };
393  const char *args[] = {
394  "1", "+", "3", "*", "10",
395  };
396  int number_of_intermediate_values;
397  int *intermediate_values = NULL;
398  int error_occurred;
399 
400  (void) state; /* unused */
401 
402  /* Setup return values of mock operator functions. */
403  /* Addition. */
407 
408  /* Multiplication. */
412 
414  array_length(args), (char **) args, array_length(operator_functions),
415  operator_functions, &number_of_intermediate_values,
416  &intermediate_values, &error_occurred), 40);
417  assert_int_equal(error_occurred, 0);
418 
419  assert_non_null(intermediate_values);
420  assert_int_equal(intermediate_values[0], 4);
421  assert_int_equal(intermediate_values[1], 40);
422  test_free(intermediate_values);
423 }
424 
425 
426 /* Ensure main() in example.c succeeds given no arguments. */
427 static void test_example_main_no_args(void **state) {
428  const char *args[] = {
429  "example",
430  };
431 
432  (void) state; /* unused */
433 
434  assert_int_equal(example_main(array_length(args), (char **) args), 0);
435 }
436 
437 
438 
439 /* Ensure main() in example.c succeeds given valid input arguments. */
440 static void test_example_main(void **state) {
441  const char *args[] = {
442  "example", "1", "+", "3", "*", "10",
443  };
444 
445  (void) state; /* unused */
446 
451 
452  assert_int_equal(example_main(array_length(args), (char **) args), 0);
453 }
454 
455 
456 int main(void) {
457  const struct CMUnitTest tests[] = {
480  };
481  return cmocka_run_group_tests(tests, NULL, NULL);
482 }
#define assert_true(c)
Definition: cmocka.h:1045
#define assert_non_null(c)
Definition: cmocka.h:1102
#define cmocka_unit_test(f)
Initializes a CMUnitTest structure.
Definition: cmocka.h:1653
static void test_perform_operation_no_integer_after_operator(void **state)
static void test_find_operator_function_by_string_not_found(void **state)
static int binary_operator(int a, int b)
static void test_find_operator_function_by_string_null_string(void **state)
static void test_add(void **state)
static void test_perform_operation_unknown_operator(void **state)
static void test_example_main(void **state)
int example_main(int argc, char *argv[])
int int example_test_printf(const char *format,...) CMOCKA_PRINTF_ATTRIBUTE(1
#define test_free(ptr)
Definition: cmocka.h:1920
static void test_perform_operation_null_args(void **state)
static void test_perform_operation_null_number_of_intermediate_values(void **state)
int add(int a, int b)
static void test_perform_operation_first_arg_not_integer(void **state)
#define expect_value(function, parameter, value)
Definition: cmocka.h:662
#define CMOCKA_PRINTF_ATTRIBUTE(a, b)
Definition: cmocka.h:141
static void test_perform_operation_no_arguments(void **state)
static void test_perform_operation(void **state)
static void test_find_operator_function_by_string_null_functions(void **state)
int example_test_fprintf(FILE *const file, const char *format,...) CMOCKA_PRINTF_ATTRIBUTE(2
static void test_multiply(void **state)
int divide(int a, int b)
#define vsnprintf
int(* BinaryOperator)(int a, int b)
int multiply(int a, int b)
#define array_length(x)
int subtract(int a, int b)
static void test_divide_by_zero(void **state)
#define will_return(function, value)
Definition: cmocka.h:294
static void test_divide(void **state)
#define cast_ptr_to_largest_integral_type(value)
Definition: cmocka.h:133
static void test_perform_operation_null_intermediate_values(void **state)
static void test_find_operator_function_by_string_valid_null_functions(void **state)
#define check_expected(parameter)
Definition: cmocka.h:986
#define mock()
Definition: cmocka.h:210
static void test_subtract(void **state)
#define cmocka_run_group_tests(group_tests, group_setup, group_teardown)
Definition: cmocka.h:1749
static void test_perform_operation_missing_argument(void **state)
static void test_perform_operation_null_operator_functions(void **state)
int int static char temporary_buffer[256]
BinaryOperator find_operator_function_by_string(const size_t number_of_operator_functions, const OperatorFunction *const operator_functions, const char *const operator_string)
Definition: calculator.c:143
#define assert_null(c)
Definition: cmocka.h:1119
static void test_example_main_no_args(void **state)
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)
Definition: calculator.c:175
static void test_find_operator_function_by_string_found(void **state)
#define check_expected_ptr(parameter)
Definition: cmocka.h:1004
Definition: argv.h:35
struct OperatorFunction OperatorFunction
int main(void)
#define expect_assert_failure(function_call)
Definition: cmocka.h:2012
#define expect_string(function, parameter, string)
Definition: cmocka.h:753
#define assert_int_equal(a, b)
Definition: cmocka.h:1174