include: Fix pointer casting and add check_expected_ptr()
authorAndreas Schneider <asn@cryptomilk.org>
Wed, 11 Feb 2015 06:55:47 +0000 (07:55 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Wed, 11 Feb 2015 16:08:56 +0000 (17:08 +0100)
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
example/calculator_test.c
example/chef_wrap/waiter_test_wrap.c
example/customer_database_test.c
example/product_database_test.c
include/cmocka.h

index a1c9334..a2dd21d 100644 (file)
@@ -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);
index b618795..4146818 100644 (file)
@@ -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 */
index 9cd1140..2f78b05 100644 (file)
@@ -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");
index e0ade53..e09eeab 100644 (file)
@@ -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());
 }
index b699652..d1c057b 100644 (file)
@@ -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
+
 /** @} */
 
 /**