[glib/ebassi/aligned-alloc] fixup! Add tests for g_aligned_alloc()



commit 37984e3ec3d6293a0e08aff40f79c2ce1858913f
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri Feb 11 13:27:24 2022 +0000

    fixup! Add tests for g_aligned_alloc()

 glib/tests/mem-overflow.c |  8 ++++++++
 glib/tests/utils.c        | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
---
diff --git a/glib/tests/mem-overflow.c b/glib/tests/mem-overflow.c
index e6a1081d0..fd926854f 100644
--- a/glib/tests/mem-overflow.c
+++ b/glib/tests/mem-overflow.c
@@ -72,6 +72,9 @@ MEM_OVERFLOW_TEST (renew_b, p = g_malloc (1); p = g_renew (X, p, b))
 MEM_OVERFLOW_TEST_FULL (aligned_alloc_a, p = g_aligned_alloc (sizeof(X), a, 16), g_aligned_free)
 MEM_OVERFLOW_TEST_FULL (aligned_alloc_b, p = g_aligned_alloc (sizeof(X), b, 16), g_aligned_free)
 
+MEM_OVERFLOW_TEST_FULL (aligned_alloc0_a, p = g_aligned_alloc0 (sizeof(X), a, 16), g_aligned_free)
+MEM_OVERFLOW_TEST_FULL (aligned_alloc0_b, p = g_aligned_alloc0 (sizeof(X), b, 16), g_aligned_free)
+
 static void
 mem_overflow_malloc_0 (void)
 {
@@ -178,6 +181,9 @@ mem_overflow (void)
 
   CHECK_SUBPROCESS_FAIL (aligned_alloc_a);
   CHECK_SUBPROCESS_PASS (aligned_alloc_b);
+
+  CHECK_SUBPROCESS_FAIL (aligned_alloc0_a);
+  CHECK_SUBPROCESS_PASS (aligned_alloc0_b);
 }
 
 #ifdef __GNUC__
@@ -240,6 +246,8 @@ main (int   argc,
   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);
+  g_test_add_func ("/mem/overflow/subprocess/aligned_alloc0_a", mem_overflow_aligned_alloc0_a);
+  g_test_add_func ("/mem/overflow/subprocess/aligned_alloc0_b", mem_overflow_aligned_alloc0_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 046d9b226..cfc08279f 100644
--- a/glib/tests/utils.c
+++ b/glib/tests/utils.c
@@ -953,6 +953,38 @@ test_aligned_mem (void)
   CHECK_SUBPROCESS_FAIL (aligned_alloc_npot, "Alignment must be a power of two");
 }
 
+static void
+test_aligned_mem_alignment (void)
+{
+  gchar *p;
+
+  g_test_summary ("Check that g_aligned_alloc() returns a correctly aligned pointer");
+
+  p = g_aligned_alloc (5, sizeof (*p), 256);
+  g_assert_nonnull (p);
+  g_assert_cmpuint (((guintptr) p) % 256, ==, 0);
+
+  g_aligned_free (p);
+}
+
+static void
+test_aligned_mem_zeroed (void)
+{
+  gsize n_blocks = 10;
+  guint *p;
+  gsize i;
+
+  g_test_summary ("Check that g_aligned_alloc0() zeroes out its allocation");
+
+  p = g_aligned_alloc0 (n_blocks, sizeof (*p), 16);
+  g_assert_nonnull (p);
+
+  for (i = 0; i < n_blocks; i++)
+    g_assert_cmpuint (p[i], ==, 0);
+
+  g_aligned_free (p);
+}
+
 static void
 test_nullify (void)
 {
@@ -1124,6 +1156,8 @@ main (int   argc,
   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/aligned-mem/alignment", test_aligned_mem_alignment);
+  g_test_add_func ("/utils/aligned-mem/zeroed", test_aligned_mem_zeroed);
   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]