Commit 674bfbb7 authored by Olivier Crête's avatar Olivier Crête
Browse files

gtestutils: Disable all g_assert_*() macros when G_DISABLE_ASSERT is set

The difference between plain g_assert() and those is buried in the
documentation that no one reads. There are numerous projets out there
using it outside of unit tests.
parent e045675d
Pipeline #329692 failed with stages
in 17 minutes and 28 seconds
......@@ -96,10 +96,6 @@
* messages can be more elaborate, and include the values of the compared
* entities.
*
* Note that g_assert() should not be used in unit tests, since it is a no-op
* when compiling with `G_DISABLE_ASSERT`. Use g_assert() in production code,
* and g_assert_true() in unit tests.
*
* A full example of creating a test suite with two tests using fixtures:
* |[<!-- language="C" -->
* #include <glib.h>
......@@ -462,10 +458,7 @@
*
* The macro can be turned off in final releases of code by defining
* `G_DISABLE_ASSERT` when compiling the application, so code must
* not depend on any side effects from @expr. Similarly, it must not be used
* in unit tests, otherwise the unit tests will be ineffective if compiled with
* `G_DISABLE_ASSERT`. Use g_assert_true() and related macros in unit tests
* instead.
* not depend on any side effects from @expr.
*/
/**
......@@ -476,8 +469,7 @@
* application is terminated.
*
* The macro can be turned off in final releases of code by defining
* `G_DISABLE_ASSERT` when compiling the application. Hence, it should not be
* used in unit tests, where assertions should always be effective.
* `G_DISABLE_ASSERT` when compiling the application.
*/
/**
......@@ -490,10 +482,6 @@
* an error message is logged and the application is either
* terminated or the testcase marked as failed.
*
* Note that unlike g_assert(), this macro is unaffected by whether
* `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and,
* conversely, g_assert() should not be used in tests.
*
* See g_test_set_nonfatal_assertions().
*
* Since: 2.38
......@@ -509,10 +497,6 @@
* an error message is logged and the application is either
* terminated or the testcase marked as failed.
*
* Note that unlike g_assert(), this macro is unaffected by whether
* `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and,
* conversely, g_assert() should not be used in tests.
*
* See g_test_set_nonfatal_assertions().
*
* Since: 2.38
......@@ -528,10 +512,6 @@
* an error message is logged and the application is either
* terminated or the testcase marked as failed.
*
* Note that unlike g_assert(), this macro is unaffected by whether
* `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and,
* conversely, g_assert() should not be used in tests.
*
* See g_test_set_nonfatal_assertions().
*
* Since: 2.38
......@@ -547,10 +527,6 @@
* an error message is logged and the application is either
* terminated or the testcase marked as failed.
*
* Note that unlike g_assert(), this macro is unaffected by whether
* `G_DISABLE_ASSERT` is defined. Hence it should only be used in tests and,
* conversely, g_assert() should not be used in tests.
*
* See g_test_set_nonfatal_assertions().
*
* Since: 2.40
......
......@@ -40,6 +40,39 @@ typedef void (*GTestFixtureFunc) (gpointer fixture,
gconstpointer user_data);
/* assertion API */
#ifdef G_DISABLE_ASSERT
/* https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable
* GCC 5 is not a strict lower bound for versions of GCC which provide __builtin_unreachable(). */
#if __GNUC__ >= 5 || g_macro__has_builtin(__builtin_unreachable)
#define g_assert_not_reached() G_STMT_START { (void) 0; __builtin_unreachable (); } G_STMT_END
#elif defined (_MSC_VER)
#define g_assert_not_reached() G_STMT_START { (void) 0; __assume (0); } G_STMT_END
#else /* if __builtin_unreachable() is not supported: */
#define g_assert_not_reached() G_STMT_START { (void) 0; } G_STMT_END
#endif
#define g_assert(expr) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmpstr(s1, cmp, s2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmpint(n1, cmp, n2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmpuint(n1, cmp, n2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmphex(n1, cmp, n2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmpfloat(n1, cmp, n2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmpfloat_with_epsilon(n1, cmp, n2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmpvariant(v1, v2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_cmpstrv(strv1, strv2) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_no_errno(expr) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_no_error(err) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_error(err, dom, c) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_true(expr) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_false(expr) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_null(expr) G_STMT_START { (void) 0; } G_STMT_END
#define g_assert_nonnull(expr) G_STMT_START { (void) 0; } G_STMT_END
#else /* !G_DISABLE_ASSERT */
#define g_assert_cmpstr(s1, cmp, s2) G_STMT_START { \
const char *__s1 = (s1), *__s2 = (s2); \
if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
......@@ -214,19 +247,6 @@ typedef void (*GTestFixtureFunc) (gpointer fixture,
} G_STMT_END
#endif
#ifdef G_DISABLE_ASSERT
/* https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable
* GCC 5 is not a strict lower bound for versions of GCC which provide __builtin_unreachable(). */
#if __GNUC__ >= 5 || g_macro__has_builtin(__builtin_unreachable)
#define g_assert_not_reached() G_STMT_START { (void) 0; __builtin_unreachable (); } G_STMT_END
#elif defined (_MSC_VER)
#define g_assert_not_reached() G_STMT_START { (void) 0; __assume (0); } G_STMT_END
#else /* if __builtin_unreachable() is not supported: */
#define g_assert_not_reached() G_STMT_START { (void) 0; } G_STMT_END
#endif
#define g_assert(expr) G_STMT_START { (void) 0; } G_STMT_END
#else /* !G_DISABLE_ASSERT */
#define g_assert_not_reached() G_STMT_START { g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } G_STMT_END
#define g_assert(expr) G_STMT_START { \
if G_LIKELY (expr) ; else \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment