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, ...);
+int example_test_fprintf(FILE* const file, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
+int example_test_printf(const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
char temporary_buffer[256];
// Ensure add() adds two integers correctly.
static void test_add(void **state) {
+ (void) state; /* unused */
+
assert_int_equal(add(3, 3), 6);
assert_int_equal(add(3, -3), 0);
}
// Ensure subtract() subtracts two integers correctly.
static void test_subtract(void **state) {
+ (void) state; /* unused */
+
assert_int_equal(subtract(3, 3), 0);
assert_int_equal(subtract(3, -3), 6);
}
// Ensure multiple() mulitplies two integers correctly.
static void test_multiply(void **state) {
+ (void) state; /* unused */
+
assert_int_equal(multiply(3, 3), 9);
assert_int_equal(multiply(3, 0), 0);
}
// Ensure divide() divides one integer by another correctly.
static void test_divide(void **state) {
+ (void) state; /* unused */
+
assert_int_equal(divide(10, 2), 5);
assert_int_equal(divide(2, 10), 0);
}
// Ensure divide() asserts when trying to divide by zero.
static void test_divide_by_zero(void **state) {
+ (void) state; /* unused */
+
expect_assert_failure(divide(100, 0));
}
/* Ensure find_operator_function_by_string() asserts when a NULL pointer is
* specified as the table to search. */
static void test_find_operator_function_by_string_null_functions(void **state) {
+ (void) state; /* unused */
+
expect_assert_failure(find_operator_function_by_string(1, NULL, "test"));
}
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
+
+ (void) state; /* unused */
+
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. */
static void test_find_operator_function_by_string_valid_null_functions(void **state) {
+ (void) state; /* unused */
+
assert_int_equal(find_operator_function_by_string(0, NULL, "test"), NULL);
}
{"-", binary_operator},
{"/", binary_operator},
};
+
+ (void) state; /* unused */
+
assert_int_equal(find_operator_function_by_string(
array_length(operator_functions), operator_functions, "test"),
NULL);
{"-", (BinaryOperator)0xDEADBEEF},
{"/", (BinaryOperator)0xABADCAFE},
};
+
+ (void) state; /* unused */
+
assert_int_equal(find_operator_function_by_string(
array_length(operator_functions), operator_functions, "-"),
0xDEADBEEF);
int number_of_intermediate_values;
int *intermediate_values;
int error_occurred;
+
+ (void) state; /* unused */
+
expect_assert_failure(perform_operation(
1, NULL, array_length(operator_functions), operator_functions,
&number_of_intermediate_values, &intermediate_values,
/* Ensure perform_operation() asserts when a NULL operator_functions array is
* specified. */
static void test_perform_operation_null_operator_functions(void **state) {
- char *args[] = {
+ const char *args[] = {
"1", "+", "2", "*", "4"
};
int number_of_intermediate_values;
int *intermediate_values;
int error_occurred;
+
+ (void) state; /* unused */
+
expect_assert_failure(perform_operation(
- array_length(args), args, 1, NULL, &number_of_intermediate_values,
+ array_length(args), (char **) args, 1, NULL, &number_of_intermediate_values,
&intermediate_values, &error_occurred));
}
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
- char *args[] = {
+ const char *args[] = {
"1", "+", "2", "*", "4"
};
int *intermediate_values;
int error_occurred;
+
+ (void) state; /* unused */
+
expect_assert_failure(perform_operation(
- array_length(args), args, 1, operator_functions, NULL,
+ array_length(args), (char **) args, 1, operator_functions, NULL,
&intermediate_values, &error_occurred));
}
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
- char *args[] = {
+ const char *args[] = {
"1", "+", "2", "*", "4"
};
int number_of_intermediate_values;
int error_occurred;
+
+ (void) state; /* unused */
+
expect_assert_failure(perform_operation(
- array_length(args), args, array_length(operator_functions),
+ array_length(args), (char **) 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) {
+static void test_perform_operation_no_arguments(void **state) {
int number_of_intermediate_values;
int *intermediate_values;
int error_occurred;
+
+ (void) state; /* unused */
+
assert_int_equal(perform_operation(
0, NULL, 0, NULL, &number_of_intermediate_values, &intermediate_values,
&error_occurred), 0);
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
- char *args[] = {
+ const char *args[] = {
"test", "+", "2", "*", "4"
};
int number_of_intermediate_values;
int *intermediate_values;
int error_occurred;
+ (void) state; /* unused */
+
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),
+ array_length(args), (char **) args, array_length(operator_functions),
operator_functions, &number_of_intermediate_values,
&intermediate_values, &error_occurred), 0);
assert_int_equal(error_occurred, 1);
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
- char *args[] = {
+ const char *args[] = {
"1", "*", "2", "*", "4"
};
int number_of_intermediate_values;
int *intermediate_values;
int error_occurred;
+ (void) state; /* unused */
+
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),
+ array_length(args), (char **) args, array_length(operator_functions),
operator_functions, &number_of_intermediate_values,
&intermediate_values, &error_occurred), 0);
assert_int_equal(error_occurred, 1);
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
- char *args[] = {
+ const char *args[] = {
"1", "+",
};
int number_of_intermediate_values;
int *intermediate_values;
int error_occurred;
+ (void) state; /* unused */
+
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),
+ array_length(args), (char **) args, array_length(operator_functions),
operator_functions, &number_of_intermediate_values,
&intermediate_values, &error_occurred), 0);
assert_int_equal(error_occurred, 1);
const OperatorFunction operator_functions[] = {
{"+", binary_operator},
};
- char *args[] = {
+ const char *args[] = {
"1", "+", "test",
};
int number_of_intermediate_values;
int *intermediate_values;
int error_occurred;
+ (void) state; /* unused */
+
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),
+ array_length(args), (char **) args, array_length(operator_functions),
operator_functions, &number_of_intermediate_values,
&intermediate_values, &error_occurred), 0);
assert_int_equal(error_occurred, 1);
{"+", binary_operator},
{"*", binary_operator},
};
- char *args[] = {
+ const char *args[] = {
"1", "+", "3", "*", "10",
};
int number_of_intermediate_values;
int *intermediate_values;
int error_occurred;
+ (void) state; /* unused */
+
// Setup return values of mock operator functions.
// Addition.
expect_value(binary_operator, a, 1);
will_return(binary_operator, 40);
assert_int_equal(perform_operation(
- array_length(args), args, array_length(operator_functions),
+ array_length(args), (char **) args, array_length(operator_functions),
operator_functions, &number_of_intermediate_values,
&intermediate_values, &error_occurred), 40);
assert_int_equal(error_occurred, 0);
// Ensure main() in example.c succeeds given no arguments.
static void test_example_main_no_args(void **state) {
- char *args[] = {
+ const char *args[] = {
"example",
};
- assert_int_equal(example_main(array_length(args), args), 0);
+
+ (void) state; /* unused */
+
+ assert_int_equal(example_main(array_length(args), (char **) args), 0);
}
// Ensure main() in example.c succeeds given valid input arguments.
static void test_example_main(void **state) {
- char *args[] = {
+ const char *args[] = {
"example", "1", "+", "3", "*", "10",
};
+ (void) state; /* unused */
+
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);
+ assert_int_equal(example_main(array_length(args), (char **) args), 0);
}
-int main(int argc, char* argv[]) {
+int main(void) {
UnitTest tests[] = {
unit_test(test_add),
unit_test(test_subtract),