diff mbox

[v5,40/48] glib-compat: add 2.38/2.40/2.46 asserts

Message ID 1443812991-17356-41-git-send-email-marcandre.lureau@redhat.com
State New
Headers show

Commit Message

Marc-André Lureau Oct. 2, 2015, 7:09 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Those are mostly useful for writing tests.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/glib-compat.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

Comments

Claudio Fontana Oct. 5, 2015, 10:56 a.m. UTC | #1
On 02.10.2015 21:09, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> Those are mostly useful for writing tests.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  include/glib-compat.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 61 insertions(+)
> 
> diff --git a/include/glib-compat.h b/include/glib-compat.h
> index 318e000..fb25f43 100644
> --- a/include/glib-compat.h
> +++ b/include/glib-compat.h
> @@ -165,4 +165,65 @@ static inline GThread *g_thread_new(const char *name,
>  #define CompatGCond GCond
>  #endif /* glib 2.31 */
>  
> +#ifndef g_assert_true
> +#define g_assert_true(expr)                                                    \
> +    do {                                                                       \
> +        if (G_LIKELY(expr)) {                                                  \
> +        } else {                                                               \
> +            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
> +                                "'" #expr "' should be TRUE");                 \
> +        }                                                                      \
> +    } while (0)
> +#endif
> +
> +#ifndef g_assert_false
> +#define g_assert_false(expr)                                                   \
> +    do {                                                                       \
> +        if (G_LIKELY(!(expr))) {                                               \
> +        } else {                                                               \
> +            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
> +                                "'" #expr "' should be FALSE");                \
> +        }                                                                      \
> +    } while (0)
> +#endif
> +
> +#ifndef g_assert_null
> +#define g_assert_null(expr)                                                    \
> +    do {                                                                       \
> +        if (G_LIKELY((expr) == NULL)) {                                        \
> +        } else {                                                               \
> +            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
> +                                "'" #expr "' should be NULL");                 \
> +        }                                                                      \
> +    } while (0)
> +#endif
> +
> +#ifndef g_assert_nonnull
> +#define g_assert_nonnull(expr)                                                 \
> +    do {                                                                       \
> +        if (G_LIKELY((expr) != NULL)) {                                        \
> +        } else {                                                               \
> +            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
> +                                "'" #expr "' should not be NULL");             \
> +        }                                                                      \
> +    } while (0)
> +#endif
> +
> +#ifndef g_assert_cmpmem
> +#define g_assert_cmpmem(m1, l1, m2, l2)                                        \
> +    do {                                                                       \
> +        gconstpointer __m1 = m1, __m2 = m2;                                    \
> +        int __l1 = l1, __l2 = l2;                                              \
> +        if (__l1 != __l2) {                                                    \
> +            g_assertion_message_cmpnum(                                        \
> +                G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,                   \
> +                #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==",   \
> +                __l2, 'i');                                                    \
> +        } else if (memcmp(__m1, __m2, __l1) != 0) {                            \
> +            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
> +                                "assertion failed (" #m1 " == " #m2 ")");      \
> +        }                                                                      \
> +    } while (0)
> +#endif
> +
>  #endif
> 

Hi, I did not find g_assertion_message in any of the exported GLIB APIs.
In fact, I found it in glib/gtestutils.h under the section "internal ABI".
This is a hint that we should not be using it right?

Ciao,

CLaudio
Marc-André Lureau Oct. 5, 2015, 11:14 a.m. UTC | #2
Hi

On Mon, Oct 5, 2015 at 12:56 PM, Claudio Fontana
<claudio.fontana@huawei.com> wrote:
> Hi, I did not find g_assertion_message in any of the exported GLIB APIs.
> In fact, I found it in glib/gtestutils.h under the section "internal ABI".
> This is a hint that we should not be using it right?

Good point, I didn't see that. Notice that this file has other weird
comments, such as "semi-internal API". I think it's fine to use
though, because it's an external symbol and glib has ABI stability
guarantees. I opened https://bugzilla.gnome.org/show_bug.cgi?id=756077
to change the glib comments.
Paolo Bonzini Oct. 5, 2015, 11:44 a.m. UTC | #3
On 05/10/2015 12:56, Claudio Fontana wrote:
> Hi, I did not find g_assertion_message in any of the exported GLIB APIs.
> In fact, I found it in glib/gtestutils.h under the section "internal ABI".
> This is a hint that we should not be using it right?

Since this only matters for these pre-2.46 versions, where we know that
g_assertion_message exists, we can use it.

Paolo
Marc-André Lureau Oct. 5, 2015, 1:35 p.m. UTC | #4
glib comment is now fixed:

https://git.gnome.org/browse/glib/commit/?id=dabf6627886c1d21b9fe03c897809eb64ef2ac54

On Mon, Oct 5, 2015 at 1:44 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
>
> On 05/10/2015 12:56, Claudio Fontana wrote:
>> Hi, I did not find g_assertion_message in any of the exported GLIB APIs.
>> In fact, I found it in glib/gtestutils.h under the section "internal ABI".
>> This is a hint that we should not be using it right?
>
> Since this only matters for these pre-2.46 versions, where we know that
> g_assertion_message exists, we can use it.
>
> Paolo
>
Claudio Fontana Oct. 5, 2015, 2:41 p.m. UTC | #5
On 05.10.2015 15:35, Marc-André Lureau wrote:
> glib comment is now fixed:
> 
> https://git.gnome.org/browse/glib/commit/?id=dabf6627886c1d21b9fe03c897809eb64ef2ac54
> 

Good!

> On Mon, Oct 5, 2015 at 1:44 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>>
>>
>> On 05/10/2015 12:56, Claudio Fontana wrote:
>>> Hi, I did not find g_assertion_message in any of the exported GLIB APIs.
>>> In fact, I found it in glib/gtestutils.h under the section "internal ABI".
>>> This is a hint that we should not be using it right?
>>
>> Since this only matters for these pre-2.46 versions, where we know that
>> g_assertion_message exists, we can use it.
>>
>> Paolo


This is also a valid point.

Reviewed-by: Claudio Fontana <claudio.fontana@huawei.com>
diff mbox

Patch

diff --git a/include/glib-compat.h b/include/glib-compat.h
index 318e000..fb25f43 100644
--- a/include/glib-compat.h
+++ b/include/glib-compat.h
@@ -165,4 +165,65 @@  static inline GThread *g_thread_new(const char *name,
 #define CompatGCond GCond
 #endif /* glib 2.31 */
 
+#ifndef g_assert_true
+#define g_assert_true(expr)                                                    \
+    do {                                                                       \
+        if (G_LIKELY(expr)) {                                                  \
+        } else {                                                               \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "'" #expr "' should be TRUE");                 \
+        }                                                                      \
+    } while (0)
+#endif
+
+#ifndef g_assert_false
+#define g_assert_false(expr)                                                   \
+    do {                                                                       \
+        if (G_LIKELY(!(expr))) {                                               \
+        } else {                                                               \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "'" #expr "' should be FALSE");                \
+        }                                                                      \
+    } while (0)
+#endif
+
+#ifndef g_assert_null
+#define g_assert_null(expr)                                                    \
+    do {                                                                       \
+        if (G_LIKELY((expr) == NULL)) {                                        \
+        } else {                                                               \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "'" #expr "' should be NULL");                 \
+        }                                                                      \
+    } while (0)
+#endif
+
+#ifndef g_assert_nonnull
+#define g_assert_nonnull(expr)                                                 \
+    do {                                                                       \
+        if (G_LIKELY((expr) != NULL)) {                                        \
+        } else {                                                               \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "'" #expr "' should not be NULL");             \
+        }                                                                      \
+    } while (0)
+#endif
+
+#ifndef g_assert_cmpmem
+#define g_assert_cmpmem(m1, l1, m2, l2)                                        \
+    do {                                                                       \
+        gconstpointer __m1 = m1, __m2 = m2;                                    \
+        int __l1 = l1, __l2 = l2;                                              \
+        if (__l1 != __l2) {                                                    \
+            g_assertion_message_cmpnum(                                        \
+                G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,                   \
+                #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==",   \
+                __l2, 'i');                                                    \
+        } else if (memcmp(__m1, __m2, __l1) != 0) {                            \
+            g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,   \
+                                "assertion failed (" #m1 " == " #m2 ")");      \
+        }                                                                      \
+    } while (0)
+#endif
+
 #endif