[glib/ebassi/aligned-alloc: 3/4] Add tests for g_aligned_alloc()
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/ebassi/aligned-alloc: 3/4] Add tests for g_aligned_alloc()
- Date: Sat, 8 Jan 2022 15:03:14 +0000 (UTC)
commit 8ccddd5118de3464d0c3c23eb07f7e5d74852a40
Author: Emmanuele Bassi <ebassi gnome org>
Date: Fri Jan 7 17:24:03 2022 +0000
Add tests for g_aligned_alloc()
We want to test the API contract, by checking the return value for
zero-sized allocations, invalid alignments, and overflows.
glib/tests/mem-overflow.c | 8 ++++++++
glib/tests/utils.c | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+)
---
diff --git a/glib/tests/mem-overflow.c b/glib/tests/mem-overflow.c
index 1654ab7fc..8445caada 100644
--- a/glib/tests/mem-overflow.c
+++ b/glib/tests/mem-overflow.c
@@ -68,6 +68,9 @@ MEM_OVERFLOW_TEST (new0_b, p = g_new0 (X, b))
MEM_OVERFLOW_TEST (renew_a, p = g_malloc (1); p = g_renew (X, p, a))
MEM_OVERFLOW_TEST (renew_b, p = g_malloc (1); p = g_renew (X, p, b))
+MEM_OVERFLOW_TEST (aligned_alloc_a, p = g_aligned_alloc (sizeof(X), a, 16))
+MEM_OVERFLOW_TEST (aligned_alloc_b, p = g_aligned_alloc (sizeof(X), b, 16))
+
static void
mem_overflow_malloc_0 (void)
{
@@ -171,6 +174,9 @@ mem_overflow (void)
CHECK_SUBPROCESS_PASS (malloc_0);
CHECK_SUBPROCESS_PASS (realloc_0);
+
+ CHECK_SUBPROCESS_FAIL (aligned_alloc_a);
+ CHECK_SUBPROCESS_PASS (aligned_alloc_b);
}
#ifdef __GNUC__
@@ -231,6 +237,8 @@ main (int argc,
g_test_add_func ("/mem/overflow/subprocess/renew_b", mem_overflow_renew_b);
g_test_add_func ("/mem/overflow/subprocess/malloc_0", mem_overflow_malloc_0);
g_test_add_func ("/mem/overflow/subprocess/realloc_0", mem_overflow_realloc_0);
+ g_test_add_func ("/mem/overflow/subprocess/aligned_alloc_a", mem_overflow_aligned_alloc_a);
+ g_test_add_func ("/mem/overflow/subprocess/aligned_alloc_b", mem_overflow_aligned_alloc_b);
#ifdef __GNUC__
g_test_add_func ("/mem/empty-alloc", empty_alloc);
diff --git a/glib/tests/utils.c b/glib/tests/utils.c
index 643978149..046d9b226 100644
--- a/glib/tests/utils.c
+++ b/glib/tests/utils.c
@@ -915,6 +915,44 @@ test_misc_mem (void)
g_assert (a == NULL);
}
+static void
+aligned_alloc_npot (void)
+{
+ gpointer a;
+
+ a = g_aligned_alloc (16, sizeof(char), 15);
+ g_assert_null (a);
+ exit (0);
+}
+
+static void
+test_aligned_mem (void)
+{
+ gpointer a;
+
+ g_test_summary ("Aligned memory allocator");
+
+ a = g_aligned_alloc (0, sizeof(int), 8);
+ g_assert_null (a);
+
+ a = g_aligned_alloc0 (0, sizeof(int), 8);
+ g_assert_null (a);
+
+ a = g_aligned_alloc (16, 0, 8);
+ g_assert_null (a);
+
+#define CHECK_SUBPROCESS_FAIL(name,msg) do { \
+ if (g_test_undefined ()) \
+ { \
+ g_test_message (msg); \
+ g_test_trap_subprocess ("/utils/aligned-mem/subprocess/" #name, 0, 0); \
+ g_test_trap_assert_failed (); \
+ } \
+ } while (0)
+
+ CHECK_SUBPROCESS_FAIL (aligned_alloc_npot, "Alignment must be a power of two");
+}
+
static void
test_nullify (void)
{
@@ -1084,6 +1122,8 @@ main (int argc,
g_test_add_func ("/utils/take-pointer", test_take_pointer);
g_test_add_func ("/utils/clear-source", test_clear_source);
g_test_add_func ("/utils/misc-mem", test_misc_mem);
+ g_test_add_func ("/utils/aligned-mem", test_aligned_mem);
+ g_test_add_func ("/utils/aligned-mem/subprocess/aligned_alloc_npot", aligned_alloc_npot);
g_test_add_func ("/utils/nullify", test_nullify);
g_test_add_func ("/utils/atexit", test_atexit);
g_test_add_func ("/utils/check-setuid", test_check_setuid);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]