]> granicus.if.org Git - check/commitdiff
add new ck_assert_ptr_(eq|ne) functions for pointer comparisons
authorbrarcher <brarcher@64e312b2-a51f-0410-8e61-82d0ca0eb02a>
Thu, 8 Nov 2012 17:05:31 +0000 (17:05 +0000)
committerbrarcher <brarcher@64e312b2-a51f-0410-8e61-82d0ca0eb02a>
Thu, 8 Nov 2012 17:05:31 +0000 (17:05 +0000)
git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@668 64e312b2-a51f-0410-8e61-82d0ca0eb02a

NEWS
doc/check.texi
src/check.h.in
tests/check_check_master.c
tests/check_check_sub.c

diff --git a/NEWS b/NEWS
index c87533e5a2695a5480abb1d2330c313ccb36bca9..6bf139741af7a4993dabb5ad848e60222ca26c16 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -19,6 +19,9 @@ In development:
 * Fix implementation of unsettenv in check's libcompat for systems that do
   not provide it. 
 
+* Improvements to the new Check API: new comparison functions for pointers:
+  ck_assert_ptr_(eq|ne).
+
 Mon, Oct 22, 2012: Released Check 0.9.9
   based on r637 (2012-10-22 13:54:14 +0200)
 
index 96377c325eaddd3e3830dfabe7964eb6640b8b1e..b267e7b7e01923594697c7bf2e4892ddc97b0097 100644 (file)
@@ -869,6 +869,15 @@ operator is again indicated by last two letters of the function name.
 @code{ck_assert_str_lt(a, b)} will pass if the unsigned numerical value
 of the character string @code{a} is less than that of @code{b}.
 
+@item ck_assert_ptr_eq
+@itemx ck_assert_ptr_ne
+
+Compares two pointers and displays predefined message with
+condition and values of both input parameters on failure.  The operator
+used for comparison is different for each function and is indicated by
+the last two letters of the function name.  The abbreviations @code{eq} and
+@code{ne} correspond to @code{==} and @code{!=} respectively.
+
 @end ftable
 
 @node Running Multiple Cases, No Fork Mode, Convenience Test Functions, Advanced Features
index 2fbb9a5f0cc926cd8da3b4ff09ed7b0a05203a99..fe9465994409614ba3050fa5b1efd14b0bd045ee 100644 (file)
@@ -59,7 +59,9 @@
          ck_assert_str_le
          ck_assert_str_gt
          ck_assert_str_ge
-
+      Check the relationship between two pointers:
+         ck_assert_ptr_eq
+         ck_assert_ptr_ne
 
    Test cases are created with tcase_create, unit tests are added
    with tcase_add_test
@@ -303,6 +305,15 @@ void CK_EXPORT _ck_assert_msg (int result, const char *file,
 #define ck_assert_str_gt(X, Y) _ck_assert_str(X, >, Y)
 #define ck_assert_str_ge(X, Y) _ck_assert_str(X, >=, Y)
 
+/* Pointer comparsion macros with improved output compared to ck_assert(). */
+/* OP may only be == or !=  */
+#define _ck_assert_ptr(X, OP, Y) do { \
+  void* _ck_x = (X); \
+  void* _ck_y = (Y); \
+  ck_assert_msg(_ck_x OP _ck_y, "Assertion '"#X#OP#Y"' failed: "#X"==%p, "#Y"==%p", _ck_x, _ck_y); \
+} while (0)
+#define ck_assert_ptr_eq(X, Y) _ck_assert_ptr(X, ==, Y)
+#define ck_assert_ptr_ne(X, Y) _ck_assert_ptr(X, !=, Y)
 
 /* Mark the last point reached in a unit test
    (useful for tracking down where a segfault, etc. occurs)
index 5e7c1c946be560d2bc78ab9d0c30fed0735bfb7c..a5427dd88d60b9ccbd0b500b31743e1b0221577d 100644 (file)
@@ -59,7 +59,9 @@ static master_test_t master_tests[] = {
   { "Simple Tests", CK_FAILURE, "Assertion 't>t' failed: t==\"test2\", t==\"test2\"" },
   { "Simple Tests", CK_FAILURE, "Assertion 's>=t' failed: s==\"test1\", t==\"test2\"" },
   { "Simple Tests", CK_PASS,    "Passed" },
-
+  { "Simple Tests", CK_FAILURE, "Assertion 'x==y' failed: x==0x1, y==0x2" },
+  { "Simple Tests", CK_FAILURE, "Assertion 'x!=z' failed: x==0x1, z==0x1" },
+  
   { "Signal Tests", CK_ERROR,   signal_11_str },
   { "Signal Tests", CK_PASS,    "Passed" },
   { "Signal Tests", CK_ERROR,   signal_11_8_str },
index 5a14dfaf65d57000ebe654fcdeabf8ac3e73b0b4..f3a6326e7ea021ce9c490a801881979cee29ce32 100644 (file)
@@ -302,6 +302,30 @@ START_TEST(test_ck_assert_str_expr)
 }
 END_TEST
 
+START_TEST(test_ck_assert_ptr_eq)
+{
+  int * x = (int*)0x1;
+  int * y = (int*)0x2;
+  ck_assert_ptr_eq(NULL, NULL);
+  ck_assert_ptr_eq(x,    x);
+  ck_assert_ptr_eq(x,    y);
+  #define LINENO_ck_assert_ptr_eq _STR(__LINE__)
+}
+END_TEST
+
+START_TEST(test_ck_assert_ptr_ne)
+{
+  int * x = (int*)0x1;
+  int * y = (int*)0x2;
+  int * z = x;
+  ck_assert_ptr_ne(x,    y);
+  ck_assert_ptr_ne(x,    NULL);
+  ck_assert_ptr_ne(NULL, y);
+  ck_assert_ptr_ne(x,    z);
+  #define LINENO_ck_assert_ptr_ne _STR(__LINE__)
+}
+END_TEST
+
 START_TEST(test_segv)
   #define LINENO_segv _STR(__LINE__)
 {
@@ -582,6 +606,8 @@ void init_master_tests_lineno(void) {
     LINENO_ck_assert_str_gt,
     LINENO_ck_assert_str_ge,
     LINENO_ck_assert_str_expr,
+    LINENO_ck_assert_ptr_eq,
+    LINENO_ck_assert_ptr_ne,
 
 /* Signal Tests */
     "-1",
@@ -758,6 +784,8 @@ Suite *make_sub_suite(void)
   tcase_add_test (tc_simple, test_ck_assert_str_gt);
   tcase_add_test (tc_simple, test_ck_assert_str_ge);
   tcase_add_test (tc_simple, test_ck_assert_str_expr);
+  tcase_add_test (tc_simple, test_ck_assert_ptr_eq);
+  tcase_add_test (tc_simple, test_ck_assert_ptr_ne);
 
   tcase_add_test (tc_signal, test_segv);
   tcase_add_test_raise_signal (tc_signal, test_segv, 11); /* pass  */