diff options
author | Andreas Schneider <asn@cryptomilk.org> | 2015-02-11 07:55:47 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2015-02-11 17:08:56 +0100 |
commit | f02f7357187e9b80da1dd7539333bc0c3a134987 (patch) | |
tree | 5da55c29608d90e1ebab4a6a4012b9f4dec537e0 | |
parent | c1332c257e73cb4c8f07158a4d6b639b6d6fb291 (diff) | |
download | cmocka-f02f7357187e9b80da1dd7539333bc0c3a134987.tar.gz cmocka-f02f7357187e9b80da1dd7539333bc0c3a134987.tar.bz2 cmocka-f02f7357187e9b80da1dd7539333bc0c3a134987.zip |
include: Fix pointer casting and add check_expected_ptr()
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r-- | example/calculator_test.c | 29 | ||||
-rw-r--r-- | example/chef_wrap/waiter_test_wrap.c | 6 | ||||
-rw-r--r-- | example/customer_database_test.c | 3 | ||||
-rw-r--r-- | example/product_database_test.c | 2 | ||||
-rw-r--r-- | include/cmocka.h | 20 |
5 files changed, 44 insertions, 16 deletions
diff --git a/example/calculator_test.c b/example/calculator_test.c index a1c9334..a2dd21d 100644 --- a/example/calculator_test.c +++ b/example/calculator_test.c @@ -68,7 +68,7 @@ int example_test_fprintf(FILE* const file, const char *format, ...) { va_start(args, format); return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer), format, args); - check_expected(temporary_buffer); + check_expected_ptr(temporary_buffer); va_end(args); return return_value; } @@ -81,7 +81,7 @@ int example_test_printf(const char *format, ...) { va_start(args, format); return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer), format, args); - check_expected(temporary_buffer); + check_expected_ptr(temporary_buffer); va_end(args); return return_value; } @@ -159,7 +159,10 @@ static void test_find_operator_function_by_string_null_string(void **state) { 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); + assert_int_equal( + cast_ptr_to_largest_integral_type( + find_operator_function_by_string(0, NULL, "test")), + 0); } /* Ensure find_operator_function_by_string() returns NULL when searching for @@ -173,9 +176,12 @@ static void test_find_operator_function_by_string_not_found(void **state) { (void) state; /* unused */ - assert_int_equal(find_operator_function_by_string( - array_length(operator_functions), operator_functions, "test"), - NULL); + assert_int_equal( + cast_ptr_to_largest_integral_type( + find_operator_function_by_string(array_length(operator_functions), + operator_functions, + "test")), + 0); } /* Ensure find_operator_function_by_string() returns the correct function when @@ -189,8 +195,11 @@ static void test_find_operator_function_by_string_found(void **state) { (void) state; /* unused */ - assert_int_equal(find_operator_function_by_string( - array_length(operator_functions), operator_functions, "-"), + assert_int_equal( + cast_ptr_to_largest_integral_type( + find_operator_function_by_string(array_length(operator_functions), + operator_functions, + "-")), 0xDEADBEEF); } @@ -392,7 +401,7 @@ static void test_perform_operation(void **state) { "1", "+", "3", "*", "10", }; int number_of_intermediate_values; - int *intermediate_values; + int *intermediate_values = NULL; int error_occurred; (void) state; /* unused */ @@ -414,7 +423,7 @@ static void test_perform_operation(void **state) { &intermediate_values, &error_occurred), 40); assert_int_equal(error_occurred, 0); - assert_true(intermediate_values); + assert_non_null(intermediate_values); assert_int_equal(intermediate_values[0], 4); assert_int_equal(intermediate_values[1], 40); test_free(intermediate_values); diff --git a/example/chef_wrap/waiter_test_wrap.c b/example/chef_wrap/waiter_test_wrap.c index b618795..4146818 100644 --- a/example/chef_wrap/waiter_test_wrap.c +++ b/example/chef_wrap/waiter_test_wrap.c @@ -60,7 +60,7 @@ int __wrap_chef_cook(const char *order, char **dish_out) bool knows_dish; char *dish; - check_expected(order); + check_expected_ptr(order); knows_dish = mock_type(bool); if (knows_dish == false) { @@ -120,7 +120,7 @@ static void test_order_hotdog(void **state) will_return(__wrap_chef_cook, true); will_return(__wrap_chef_cook, true); /* The result will be a hotdog and the cooking process will succeed */ - will_return(__wrap_chef_cook, "hotdog"); + will_return(__wrap_chef_cook, cast_ptr_to_largest_integral_type("hotdog")); will_return(__wrap_chef_cook, 0); /* Test the waiter */ @@ -153,7 +153,7 @@ static void test_bad_dish(void **state) * We expect the waiter to handle the bad dish and return an error * code */ - will_return(__wrap_chef_cook, "burger"); + will_return(__wrap_chef_cook, cast_ptr_to_largest_integral_type("burger")); will_return(__wrap_chef_cook, 0); /* Test the waiter */ diff --git a/example/customer_database_test.c b/example/customer_database_test.c index 9cd1140..2f78b05 100644 --- a/example/customer_database_test.c +++ b/example/customer_database_test.c @@ -72,7 +72,8 @@ static void test_get_customer_id_by_name(void **state) { (void) state; /* unused */ - will_return(mock_query_database, &customer_ids); + will_return(mock_query_database, + cast_ptr_to_largest_integral_type(&customer_ids)); will_return(mock_query_database, 1); rc = get_customer_id_by_name(&connection, "john doe"); diff --git a/example/product_database_test.c b/example/product_database_test.c index e0ade53..e09eeab 100644 --- a/example/product_database_test.c +++ b/example/product_database_test.c @@ -26,7 +26,7 @@ extern DatabaseConnection* connect_to_product_database(void); * that use the imaginary database.h module. */ DatabaseConnection* connect_to_database(const char * const url, const unsigned int port) { - check_expected(url); + check_expected_ptr(url); check_expected(port); return (DatabaseConnection*)((size_t)mock()); } diff --git a/include/cmocka.h b/include/cmocka.h index b699652..d1c057b 100644 --- a/include/cmocka.h +++ b/include/cmocka.h @@ -116,7 +116,7 @@ int __stdcall IsDebuggerPresent(); /* Perform an unsigned cast to uintptr_t. */ #define cast_to_pointer_integral_type(value) \ - ((uintptr_t)(value)) + ((uintptr_t)((size_t)(value))) /* Perform a cast of a pointer to uintmax_t */ #define cast_ptr_to_largest_integral_type(value) \ @@ -948,6 +948,24 @@ void check_expected(#parameter); cast_to_largest_integral_type(parameter)) #endif +#ifdef DOXYGEN +/** + * @brief Determine whether a function parameter is correct. + * + * This ensures the next value queued by one of the expect_*() macros matches + * the specified variable. + * + * This function needs to be called in the mock object. + * + * @param[in] #parameter The pointer to check. + */ +void check_expected_ptr(#parameter); +#else +#define check_expected_ptr(parameter) \ + _check_expected(__func__, #parameter, __FILE__, __LINE__, \ + cast_ptr_to_largest_integral_type(parameter)) +#endif + /** @} */ /** |