]> granicus.if.org Git - check/commitdiff
* add new ck_assert_(str|int)_(lt|le|gt|ge) comparison functions
authorcpickett <cpickett@64e312b2-a51f-0410-8e61-82d0ca0eb02a>
Sun, 12 Dec 2010 22:30:58 +0000 (22:30 +0000)
committercpickett <cpickett@64e312b2-a51f-0410-8e61-82d0ca0eb02a>
Sun, 12 Dec 2010 22:30:58 +0000 (22:30 +0000)
  - patch from zdenek crha

git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@599 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 a22ec89abb8f447dd2add5018f0ebbb7d0ec82ba..bd3c897ae380d7c1028dd84781fe0ed062a6b2b4 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,7 +1,8 @@
 In development.
 
-* Documented new Check API and fixed macros to allow multiple
-  evaluation.
+* Improvements to the new Check API: documentation, macros that allow
+  multiple evaluation, unit tests, and new
+  ck_assert_(str|int)_(lt|le|gt|ge) comparison functions.
 
 * Added checkmk, a tool for reducing "boilerplate coding" when writing
   unit tests with check.
index d8dd72655069ebd35c5a8807fcd83a597b478a2e..709b296f14dc33ab57853262120767bcdd3a8608 100644 (file)
@@ -818,49 +818,56 @@ easier for the developer to write, run, and analyse tests.
 @node Convenience Test Functions, Running Multiple Cases, Advanced Features, Advanced Features
 @section Convenience Test Functions
 
-Using the @code{fail_unless} function for all tests can lead to lot of repetitive
-code that is hard to read.  For your convenience Check provides a set of functions
-(actually macros) for testing often used conditions.
+Using the @code{fail_unless} function for all tests can lead to lot of
+repetitive code that is hard to read.  For your convenience Check
+provides a set of functions (actually macros) for testing often used
+conditions.
 
-@table @code
-@vindex ck_abort
+@ftable @code
 @item ck_abort
 Unconditionally fails test with default message.
 
-@vindex ck_abort_msg
 @item ck_abort_msg
 Unconditionally fails test with user supplied message.
 
-@vindex ck_assert
 @item ck_assert
-Fails test if supplied condition evaluates to true. This function is equivalent to
-@code{fail_unless} with @code{NULL} as a message.
+Fails test if supplied condition evaluates to true. This function is
+equivalent to @code{fail_unless} with @code{NULL} as a message.
 
-@vindex ck_assert_msg
 @item ck_assert_msg
-Fails test if supplied condition evaluates to true and displays user provided
-message. This function is equivalent to @code{fail_unless}.
+Fails test if supplied condition evaluates to true and displays user
+provided message. This function is equivalent to @code{fail_unless}.
 
-@vindex ck_assert_int_eq
 @item ck_assert_int_eq
-Takes two integer values, compares them for equality and fails test if values
-are not equal. Uses predefined message displaying failed condition and values of
-both input parameters.
-
-@vindex ck_assert_int_ne
-@item ck_assert_int_ne
-Same as @code{ck_assert_int_eq} but fails if the values are equal.
+@itemx ck_assert_int_ne
+@itemx ck_assert_int_lt
+@itemx ck_assert_int_le
+@itemx ck_assert_int_gt
+@itemx ck_assert_int_ge
+
+Compares two @code{int} values 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},
+@code{ne}, @code{lt}, @code{le}, @code{gt}, and @code{ge} correspond to
+@code{==}, @code{!=}, @code{<}, @code{<=}, @code{>}, and @code{>=}
+respectively.
 
-@vindex ck_assert_str_eq
 @item ck_assert_str_eq
-Same as @code{ck_assert_int_eq} but takes @code{char*} values and compares
-them using the @code{strcmp()} function.
-
-@vindex ck_assert_str_eq
-@item ck_assert_str_eq
-Same as @code{ck_assert_int_ne} but takes @code{char*} values and compares
-them using the @code{strcmp()} function.
-@end table
+@itemx ck_assert_str_ne
+@itemx ck_assert_str_lt
+@itemx ck_assert_str_le
+@itemx ck_assert_str_gt
+@itemx ck_assert_str_ge
+
+Compares two null-terminated @code{char *} string values, using the
+@code{strcmp()} function internally, and displays predefined message
+with condition and input parameter values on failure.  The comparison
+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}.
+
+@end ftable
 
 @node Running Multiple Cases, No Fork Mode, Convenience Test Functions, Advanced Features
 @section Running Multiple Cases
index 185c558b662368df4bf2b33188e49de714137055..57fd67287e34fd741a211bb04dd8b0fab8ad5c16 100644 (file)
@@ -257,6 +257,10 @@ void CK_EXPORT _fail_unless (int result, const char *file,
 } while (0)
 #define ck_assert_int_eq(X, Y) _ck_assert_int(X, ==, Y) 
 #define ck_assert_int_ne(X, Y) _ck_assert_int(X, !=, Y) 
+#define ck_assert_int_lt(X, Y) _ck_assert_int(X, <, Y)
+#define ck_assert_int_le(X, Y) _ck_assert_int(X, <=, Y)
+#define ck_assert_int_gt(X, Y) _ck_assert_int(X, >, Y)
+#define ck_assert_int_ge(X, Y) _ck_assert_int(X, >=, Y)
 
 /* String comparsion macros with improved output compared to fail_unless() */
 /* OP might be any operator that can be used in '0 OP strcmp(X,Y)' comparison */
@@ -269,6 +273,10 @@ void CK_EXPORT _fail_unless (int result, const char *file,
 } while (0)
 #define ck_assert_str_eq(X, Y) _ck_assert_str(X, ==, Y)
 #define ck_assert_str_ne(X, Y) _ck_assert_str(X, !=, Y)
+#define ck_assert_str_lt(X, Y) _ck_assert_str(X, <, Y)
+#define ck_assert_str_le(X, Y) _ck_assert_str(X, <=, Y)
+#define ck_assert_str_gt(X, Y) _ck_assert_str(X, >, Y)
+#define ck_assert_str_ge(X, Y) _ck_assert_str(X, >=, Y)
 
 
 /* Mark the last point reached in a unit test
index 2179004b032a4638590539ad6e003d884d469af4..dbd2f825a30812dbf119b5ad3ad6d3910596c322 100644 (file)
@@ -47,10 +47,17 @@ static master_test_t master_tests[] = {
   { "Simple Tests", CK_FAILURE, "Assertion '0' failed" },
   { "Simple Tests", CK_FAILURE, "Assertion 'x==y' failed: x==3, y==4" },
   { "Simple Tests", CK_FAILURE, "Assertion 'x!=y' failed: x==3, y==3" },
-  { "Simple Tests", CK_PASS,    "Passed" },
+  { "Simple Tests", CK_FAILURE, "Assertion 'x<x' failed: x==2, x==2" },
+  { "Simple Tests", CK_FAILURE, "Assertion 'y<=x' failed: y==3, x==2" },
+  { "Simple Tests", CK_FAILURE, "Assertion 'y>y' failed: y==3, y==3" },
+  { "Simple Tests", CK_FAILURE, "Assertion 'x>=y' failed: x==2, y==3" },
   { "Simple Tests", CK_PASS,    "Passed" },
   { "Simple Tests", CK_FAILURE, "Assertion '\"test1\"==s' failed: \"test1\"==\"test1\", s==\"test2\"" },
   { "Simple Tests", CK_FAILURE, "Assertion 't!=s' failed: t==\"test2\", s==\"test2\"" },
+  { "Simple Tests", CK_FAILURE, "Assertion 's<s' failed: s==\"test1\", s==\"test1\"" },
+  { "Simple Tests", CK_FAILURE, "Assertion 't<=s' failed: t==\"test2\", s==\"test1\"" },
+  { "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" },
 
   { "Signal Tests", CK_ERROR,   signal_11_str },
index 5f86ce52be20bfe1c71ef5af7d1bc609e9f789f5..9b4d37934fad32c6c86635b91e6dfad5c8546086 100644 (file)
@@ -134,6 +134,9 @@ START_TEST(test_ck_abort_msg_null)
 }
 END_TEST
 
+/* These ck_assert tests are all designed to fail on the last
+   assertion. */
+
 START_TEST(test_ck_assert)
 {
   int x = 3;
@@ -176,6 +179,48 @@ START_TEST(test_ck_assert_int_ne)
 }
 END_TEST
 
+START_TEST(test_ck_assert_int_lt)
+{
+  int x = 2;
+  int y = 3;
+  ck_assert_int_lt(x, y);
+  ck_assert_int_lt(x, x);
+  #define LINENO_ck_assert_int_lt _STR(__LINE__)
+}
+END_TEST
+
+START_TEST(test_ck_assert_int_le)
+{
+  int x = 2;
+  int y = 3;
+  ck_assert_int_le(x, y);
+  ck_assert_int_le(x, x);
+  ck_assert_int_le(y, x);
+  #define LINENO_ck_assert_int_le _STR(__LINE__)
+}
+END_TEST
+
+START_TEST(test_ck_assert_int_gt)
+{
+  int x = 2;
+  int y = 3;
+  ck_assert_int_gt(y, x);
+  ck_assert_int_gt(y, y);
+  #define LINENO_ck_assert_int_gt _STR(__LINE__)
+}
+END_TEST
+
+START_TEST(test_ck_assert_int_ge)
+{
+  int x = 2;
+  int y = 3;
+  ck_assert_int_ge(y, x);
+  ck_assert_int_ge(y, x);
+  ck_assert_int_ge(x, y);
+  #define LINENO_ck_assert_int_ge _STR(__LINE__)
+}
+END_TEST
+
 START_TEST(test_ck_assert_int_expr)
 {
   int x = 1;
@@ -185,20 +230,6 @@ START_TEST(test_ck_assert_int_expr)
   #define LINENO_ck_assert_int_expr _STR(__LINE__)
 } END_TEST
 
-START_TEST(test_ck_assert_str)
-{
-  _ck_assert_str("test1", < ,"test2");
-  _ck_assert_str("test2", > ,"test1");
-
-  _ck_assert_str("test1", <= ,"test2");
-  _ck_assert_str("test2", >= ,"test1");
-
-  _ck_assert_str("test1", <= ,"test1");
-  _ck_assert_str("test1", >= ,"test1");
-  #define LINENO_ck_assert_str _STR(__LINE__)
-}
-END_TEST
-
 START_TEST(test_ck_assert_str_eq)
 {
   const char *s = "test2";
@@ -219,6 +250,48 @@ START_TEST(test_ck_assert_str_ne)
 }
 END_TEST
 
+START_TEST(test_ck_assert_str_lt)
+{
+  const char *s = "test1";
+  const char *t = "test2";
+  ck_assert_str_lt(s, t);
+  ck_assert_str_lt(s, s);
+  #define LINENO_ck_assert_str_lt _STR(__LINE__)
+}
+END_TEST
+
+START_TEST(test_ck_assert_str_le)
+{
+  const char *s = "test1";
+  const char *t = "test2";
+  ck_assert_str_le(s, t);
+  ck_assert_str_le(s, s);
+  ck_assert_str_le(t, s);
+  #define LINENO_ck_assert_str_le _STR(__LINE__)
+}
+END_TEST
+
+START_TEST(test_ck_assert_str_gt)
+{
+  const char *s = "test1";
+  const char *t = "test2";
+  ck_assert_str_gt(t, s);
+  ck_assert_str_gt(t, t);
+  #define LINENO_ck_assert_str_gt _STR(__LINE__)
+}
+END_TEST
+
+START_TEST(test_ck_assert_str_ge)
+{
+  const char *s = "test1";
+  const char *t = "test2";
+  ck_assert_str_ge(t, s);
+  ck_assert_str_ge(t, t);
+  ck_assert_str_ge(s, t);
+  #define LINENO_ck_assert_str_ge _STR(__LINE__)
+}
+END_TEST
+
 START_TEST(test_ck_assert_str_expr)
 {
   const char *s = "test1";
@@ -499,10 +572,17 @@ void init_master_tests_lineno(void) {
     LINENO_ck_assert_null,
     LINENO_ck_assert_int_eq,
     LINENO_ck_assert_int_ne,
+    LINENO_ck_assert_int_lt,
+    LINENO_ck_assert_int_le,
+    LINENO_ck_assert_int_gt,
+    LINENO_ck_assert_int_ge,
     LINENO_ck_assert_int_expr,
-    LINENO_ck_assert_str,
     LINENO_ck_assert_str_eq,
     LINENO_ck_assert_str_ne,
+    LINENO_ck_assert_str_lt,
+    LINENO_ck_assert_str_le,
+    LINENO_ck_assert_str_gt,
+    LINENO_ck_assert_str_ge,
     LINENO_ck_assert_str_expr,
 
 /* Signal Tests */
@@ -668,10 +748,17 @@ Suite *make_sub_suite(void)
   tcase_add_test (tc_simple, test_ck_assert_null);
   tcase_add_test (tc_simple, test_ck_assert_int_eq);
   tcase_add_test (tc_simple, test_ck_assert_int_ne);
+  tcase_add_test (tc_simple, test_ck_assert_int_lt);
+  tcase_add_test (tc_simple, test_ck_assert_int_le);
+  tcase_add_test (tc_simple, test_ck_assert_int_gt);
+  tcase_add_test (tc_simple, test_ck_assert_int_ge);
   tcase_add_test (tc_simple, test_ck_assert_int_expr);
-  tcase_add_test (tc_simple, test_ck_assert_str);
   tcase_add_test (tc_simple, test_ck_assert_str_eq);
   tcase_add_test (tc_simple, test_ck_assert_str_ne);
+  tcase_add_test (tc_simple, test_ck_assert_str_lt);
+  tcase_add_test (tc_simple, test_ck_assert_str_le);
+  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_signal, test_segv);