[glib: 2/3] Check for prefix/suffix smaller than string and check for non-inlined function




commit c7f24df744f23f0f75b00023c4ac272477efeb03
Author: Emmanuel Fleury <emmanuel fleury gmail com>
Date:   Fri Aug 19 16:47:30 2022 +0200

    Check for prefix/suffix smaller than string and check for non-inlined function
    
    * Add a test to check that smaller string than prefix/suffix are
      handled in g_str_has_*() functions.
    
    * Add a tests on macro prefixed function and ensure that function
      itselves are tested as well.

 glib/tests/strfuncs.c | 127 ++++++++++++++++++++++++++++++++++----------------
 1 file changed, 86 insertions(+), 41 deletions(-)
---
diff --git a/glib/tests/strfuncs.c b/glib/tests/strfuncs.c
index 0ae3f89c11..6c0d430901 100644
--- a/glib/tests/strfuncs.c
+++ b/glib/tests/strfuncs.c
@@ -1200,89 +1200,132 @@ test_strdelimit (void)
   g_free (string);
 }
 
-/* Testing g_str_has_prefix() */
+/* Testing g_str_has_prefix() function avoiding the optimizing macro */
 static void
 test_has_prefix (void)
 {
-  gboolean res;
-
   if (g_test_undefined ())
     {
       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                              "*assertion*!= NULL*");
-      res = g_str_has_prefix ("foo", NULL);
+      g_assert_false ((g_str_has_prefix) ("foo", NULL));
       g_test_assert_expected_messages ();
-      g_assert_false (res);
 
       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                              "*assertion*!= NULL*");
-      res = g_str_has_prefix (NULL, "foo");
+      g_assert_false ((g_str_has_prefix) (NULL, "foo"));
       g_test_assert_expected_messages ();
-      g_assert_false (res);
     }
 
-  res = g_str_has_prefix ("foo", "bar");
-  g_assert_cmpint (res, ==, FALSE);
+  /* Having a string smaller than the prefix */
+  g_assert_false ((g_str_has_prefix) ("aa", "aaa"));
 
-  res = g_str_has_prefix ("foo", "foobar");
-  g_assert_cmpint (res, ==, FALSE);
+  /* Negative tests */
+  g_assert_false ((g_str_has_prefix) ("foo", "bar"));
+  g_assert_false ((g_str_has_prefix) ("foo", "foobar"));
+  g_assert_false ((g_str_has_prefix) ("foobar", "bar"));
 
-  res = g_str_has_prefix ("foobar", "bar");
-  g_assert_cmpint (res, ==, FALSE);
+  /* Positive tests */
+  g_assert_true ((g_str_has_prefix) ("foobar", "foo"));
+  g_assert_true ((g_str_has_prefix) ("foo", ""));
+  g_assert_true ((g_str_has_prefix) ("foo", "foo"));
+  g_assert_true ((g_str_has_prefix) ("", ""));
+}
 
-  res = g_str_has_prefix ("foobar", "foo");
-  g_assert_cmpint (res, ==, TRUE);
+/* Testing g_str_has_prefix() optimized macro */
+static void
+test_has_prefix_macro (void)
+{
+  if (g_test_undefined ())
+    {
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_assert_false (g_str_has_prefix ("foo", NULL));
+      g_test_assert_expected_messages ();
 
-  res = g_str_has_prefix ("foo", "");
-  g_assert_cmpint (res, ==, TRUE);
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_assert_false (g_str_has_prefix (NULL, "foo"));
+      g_test_assert_expected_messages ();
+    }
 
-  res = g_str_has_prefix ("foo", "foo");
-  g_assert_cmpint (res, ==, TRUE);
+  /* Having a string smaller than the prefix */
+  g_assert_false (g_str_has_prefix ("aa", "aaa"));
 
-  res = g_str_has_prefix ("", "");
-  g_assert_cmpint (res, ==, TRUE);
+  /* Negative tests */
+  g_assert_false (g_str_has_prefix ("foo", "bar"));
+  g_assert_false (g_str_has_prefix ("foo", "foobar"));
+  g_assert_false (g_str_has_prefix ("foobar", "bar"));
+
+  /* Positive tests */
+  g_assert_true (g_str_has_prefix ("foobar", "foo"));
+  g_assert_true (g_str_has_prefix ("foo", ""));
+  g_assert_true (g_str_has_prefix ("foo", "foo"));
+  g_assert_true (g_str_has_prefix ("", ""));
 }
 
+/* Testing g_str_has_suffix() function avoiding the optimizing macro */
 static void
 test_has_suffix (void)
 {
-  gboolean res;
-
   if (g_test_undefined ())
     {
       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                              "*assertion*!= NULL*");
-      res = g_str_has_suffix ("foo", NULL);
+      g_assert_false ((g_str_has_suffix) ("foo", NULL));
       g_test_assert_expected_messages ();
-      g_assert_false (res);
 
       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                              "*assertion*!= NULL*");
-      res = g_str_has_suffix (NULL, "foo");
+      g_assert_false ((g_str_has_suffix) (NULL, "foo"));
       g_test_assert_expected_messages ();
-      g_assert_false (res);
     }
 
-  res = g_str_has_suffix ("foo", "bar");
-  g_assert_false (res);
+  /* Having a string smaller than the suffix */
+  g_assert_false ((g_str_has_suffix) ("aa", "aaa"));
 
-  res = g_str_has_suffix ("bar", "foobar");
-  g_assert_false (res);
+  /* Negative tests */
+  g_assert_false ((g_str_has_suffix) ("foo", "bar"));
+  g_assert_false ((g_str_has_suffix) ("bar", "foobar"));
+  g_assert_false ((g_str_has_suffix) ("foobar", "foo"));
 
-  res = g_str_has_suffix ("foobar", "foo");
-  g_assert_false (res);
+  /* Positive tests */
+  g_assert_true ((g_str_has_suffix) ("foobar", "bar"));
+  g_assert_true ((g_str_has_suffix) ("foo", ""));
+  g_assert_true ((g_str_has_suffix) ("foo", "foo"));
+  g_assert_true ((g_str_has_suffix) ("", ""));
+}
 
-  res = g_str_has_suffix ("foobar", "bar");
-  g_assert_true (res);
+/* Testing g_str_has_prefix() optimized macro */
+static void
+test_has_suffix_macro (void)
+{
+  if (g_test_undefined ())
+    {
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_assert_false (g_str_has_suffix ("foo", NULL));
+      g_test_assert_expected_messages ();
+
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_assert_false (g_str_has_suffix (NULL, "foo"));
+      g_test_assert_expected_messages ();
+    }
 
-  res = g_str_has_suffix ("foo", "");
-  g_assert_true (res);
+  /* Having a string smaller than the suffix */
+  g_assert_false (g_str_has_suffix ("aa", "aaa"));
 
-  res = g_str_has_suffix ("foo", "foo");
-  g_assert_true (res);
+  /* Negative tests */
+  g_assert_false (g_str_has_suffix ("foo", "bar"));
+  g_assert_false (g_str_has_suffix ("bar", "foobar"));
+  g_assert_false (g_str_has_suffix ("foobar", "foo"));
 
-  res = g_str_has_suffix ("", "");
-  g_assert_true (res);
+  /* Positive tests */
+  g_assert_true (g_str_has_suffix ("foobar", "bar"));
+  g_assert_true (g_str_has_suffix ("foo", ""));
+  g_assert_true (g_str_has_suffix ("foo", "foo"));
+  g_assert_true (g_str_has_suffix ("", ""));
 }
 
 static void
@@ -2587,7 +2630,9 @@ main (int   argc,
   g_test_add_func ("/strfuncs/ascii_strtod", test_ascii_strtod);
   g_test_add_func ("/strfuncs/bounds-check", test_bounds);
   g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
+  g_test_add_func ("/strfuncs/has-prefix-macro", test_has_prefix_macro);
   g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
+  g_test_add_func ("/strfuncs/has-suffix-macro", test_has_suffix_macro);
   g_test_add_func ("/strfuncs/memdup", test_memdup);
   g_test_add_func ("/strfuncs/memdup2", test_memdup2);
   g_test_add_func ("/strfuncs/set_str", test_set_str);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]