#ifdef printf
#undef printf
#endif // printf
+extern int example_test_printf(const char *format, ...);
#define printf example_test_printf
extern void print_message(const char *format, ...);
const char* file, const int line);
void _test_free(void* const ptr, const char* file, const int line);
+int example_main(int argc, char *argv[]);
/* main is defined in the unit test so redefine name of the the main function
* here. */
#define main example_main
} OperatorFunction;
+BinaryOperator find_operator_function_by_string(
+ const size_t number_of_operator_functions,
+ const OperatorFunction * const operator_functions,
+ const char* const operator_string);
+
+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);
+
static int add(int a, int b);
static int subtract(int a, int b);
static int multiply(int a, int b);
* associated with the specified operator_string. This function returns the
* function associated with operator_string if successful, NULL otherwise.
*/
-static BinaryOperator find_operator_function_by_string(
+BinaryOperator find_operator_function_by_string(
const size_t number_of_operator_functions,
const OperatorFunction * const operator_functions,
const char* const operator_string) {
size_t i;
assert(!number_of_operator_functions || operator_functions);
- assert(operator_string);
+ assert(operator_string != NULL);
for (i = 0; i < number_of_operator_functions; i++) {
const OperatorFunction *const operator_function =
* If an error occurs while performing the operation error_occurred is set to
* 1, the operation is aborted and 0 is returned.
*/
-static int perform_operation(
+int perform_operation(
int number_of_arguments, char *arguments[],
const size_t number_of_operator_functions,
const OperatorFunction * const operator_functions,
int ** const intermediate_values, int * const error_occurred) {
char *end_of_integer;
int value;
- unsigned int i;
+ int i;
assert(!number_of_arguments || arguments);
assert(!number_of_operator_functions || operator_functions);
- assert(error_occurred);
- assert(number_of_intermediate_values);
- assert(intermediate_values);
+ assert(error_occurred != NULL);
+ assert(number_of_intermediate_values != NULL);
+ assert(intermediate_values != NULL);
*error_occurred = 0;
*number_of_intermediate_values = 0;
// If no errors occurred display the result.
if (!return_value && argc > 1) {
- unsigned int i;
- unsigned int intermediate_value_index = 0;
+ int i;
+ int intermediate_value_index = 0;
printf("%s\n", argv[1]);
for (i = 2; i < argc; i += 2) {
assert(intermediate_value_index < number_of_intermediate_values);
int ** const intermediate_values, int * const error_occurred);
extern int example_main(int argc, char *argv[]);
+int example_test_fprintf(FILE* const file, const char *format, ...);
+int example_test_printf(const char *format, ...);
+
/* 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, ...) {
}
// A mock binary operator function.
-int binary_operator(int a, int b) {
+static 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) {
+static 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) {
+static 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) {
+static 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) {
+static 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) {
+static 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) {
+static 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) {
+static void test_find_operator_function_by_string_null_string(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
/* 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) {
+static void test_find_operator_function_by_string_valid_null_functions(void **state) {
assert_int_equal(find_operator_function_by_string(0, NULL, "test"), 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) {
+static void test_find_operator_function_by_string_not_found(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
{"-", binary_operator},
/* 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) {
+static void test_find_operator_function_by_string_found(void **state) {
const OperatorFunction operator_functions[] = {
{"+", (BinaryOperator)0x12345678},
{"-", (BinaryOperator)0xDEADBEEF},
}
// Ensure perform_operation() asserts when a NULL arguments array is specified.
-void test_perform_operation_null_args(void **state) {
+static void test_perform_operation_null_args(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
/* Ensure perform_operation() asserts when a NULL operator_functions array is
* specified. */
-void test_perform_operation_null_operator_functions(void **state) {
+static void test_perform_operation_null_operator_functions(void **state) {
char *args[] = {
"1", "+", "2", "*", "4"
};
/* 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) {
+static void test_perform_operation_null_number_of_intermediate_values(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
/* Ensure perform_operation() asserts when a NULL pointer is specified for
* intermediate_values. */
-void test_perform_operation_null_intermediate_values(void **state) {
+static void test_perform_operation_null_intermediate_values(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
/* 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) {
+static void test_perform_operation_first_arg_not_integer(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
/* Ensure perform_operation() returns an error when parsing an unknown
* operator. */
-void test_perform_operation_unknown_operator(void **state) {
+static void test_perform_operation_unknown_operator(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
/* Ensure perform_operation() returns an error when nothing follows an
* operator. */
-void test_perform_operation_missing_argument(void **state) {
+static void test_perform_operation_missing_argument(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
/* Ensure perform_operation() returns an error when an integer doesn't follow
* an operator. */
-void test_perform_operation_no_integer_after_operator(void **state) {
+static void test_perform_operation_no_integer_after_operator(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
// Ensure perform_operation() succeeds given valid input parameters.
-void test_perform_operation(void **state) {
+static void test_perform_operation(void **state) {
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
{"*", binary_operator},
// Ensure main() in example.c succeeds given no arguments.
-void test_example_main_no_args(void **state) {
+static void test_example_main_no_args(void **state) {
char *args[] = {
"example",
};
// Ensure main() in example.c succeeds given valid input arguments.
-void test_example_main(void **state) {
+static void test_example_main(void **state) {
char *args[] = {
"example", "1", "+", "3", "*", "10",
};