Merge branch 'strcanon-docs' into 'master'

Merge of initial commits from !784

Closes #29

See merge request GNOME/glib!807
This commit is contained in:
Philip Withnall 2019-04-30 10:52:42 +00:00
commit 94f63b8de1
2 changed files with 190 additions and 139 deletions

View File

@ -1988,10 +1988,17 @@ g_strncasecmp (const gchar *s1,
* changed to the @new_delimiter character. Modifies @string in place,
* and returns @string itself, not a copy. The return value is to
* allow nesting such as
* |[<!-- language="C" -->
* |[<!-- language="C" -->
* g_ascii_strup (g_strdelimit (str, "abc", '?'))
* ]|
*
* In order to modify a copy, you may use `g_strdup()`:
* |[<!-- language="C" -->
* reformatted = g_strdelimit (g_strdup (const_str), "abc", '?');
* ...
* g_free (reformatted);
* ]|
*
* Returns: @string
*/
gchar *
@ -2025,10 +2032,17 @@ g_strdelimit (gchar *string,
* replaces the character with @substitutor. Modifies @string in place,
* and return @string itself, not a copy. The return value is to allow
* nesting such as
* |[<!-- language="C" -->
* |[<!-- language="C" -->
* g_ascii_strup (g_strcanon (str, "abc", '?'))
* ]|
*
* In order to modify a copy, you may use `g_strdup()`:
* |[<!-- language="C" -->
* reformatted = g_strcanon (g_strdup (const_str), "abc", '?');
* ...
* g_free (reformatted);
* ]|
*
* Returns: @string
*/
gchar *

View File

@ -199,50 +199,53 @@ test_is_to_digit (void)
#undef TEST_DIGIT
}
/* Testing g_strdup() function with various positive and negative cases */
static void
test_strdup (void)
{
gchar *str;
str = g_strdup (NULL);
g_assert (str == NULL);
g_assert_null (g_strdup (NULL));
str = g_strdup (GLIB_TEST_STRING);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
g_free (str);
}
/* Testing g_strndup() function with various positive and negative cases */
static void
test_strndup (void)
{
gchar *str;
str = g_strndup (NULL, 3);
g_assert (str == NULL);
g_assert_null (str);
str = g_strndup ("aaaa", 5);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "aaaa");
g_free (str);
str = g_strndup ("aaaa", 2);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "aa");
g_free (str);
}
/* Testing g_strdup_printf() function with various positive and negative cases */
static void
test_strdup_printf (void)
{
gchar *str;
str = g_strdup_printf ("%05d %-5s", 21, "test");
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "00021 test ");
g_free (str);
}
/* Testing g_strdupv() function with various positive and negative cases */
static void
test_strdupv (void)
{
@ -250,92 +253,96 @@ test_strdupv (void)
gchar **copy;
copy = g_strdupv (NULL);
g_assert (copy == NULL);
g_assert_null (copy);
copy = g_strdupv (vec);
g_assert (copy != NULL);
g_assert_nonnull (copy);
g_assert_cmpstr (copy[0], ==, "Foo");
g_assert_cmpstr (copy[1], ==, "Bar");
g_assert (copy[2] == NULL);
g_assert_null (copy[2]);
g_strfreev (copy);
}
/* Testing g_strfill() function with various positive and negative cases */
static void
test_strnfill (void)
{
gchar *str;
str = g_strnfill (0, 'a');
g_assert (str != NULL);
g_assert (*str == '\0');
g_assert_nonnull (str);
g_assert_true (*str == '\0');
g_free (str);
str = g_strnfill (5, 'a');
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "aaaaa");
g_free (str);
}
/* Testing g_strconcat() function with various positive and negative cases */
static void
test_strconcat (void)
{
gchar *str;
str = g_strconcat (GLIB_TEST_STRING, NULL);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
g_free (str);
str = g_strconcat (GLIB_TEST_STRING,
GLIB_TEST_STRING,
GLIB_TEST_STRING,
GLIB_TEST_STRING,
NULL);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
g_free (str);
g_assert (g_strconcat (NULL, "bla", NULL) == NULL);
g_assert_null (g_strconcat (NULL, "bla", NULL));
}
/* Testing g_strjoin() function with various positive and negative cases */
static void
test_strjoin (void)
{
gchar *str;
str = g_strjoin (NULL, NULL);
g_assert (str != NULL);
g_assert (*str == '\0');
g_assert_nonnull (str);
g_assert_true (*str == '\0');
g_free (str);
str = g_strjoin (":", NULL);
g_assert (str != NULL);
g_assert (*str == '\0');
g_assert_nonnull (str);
g_assert_true (*str == '\0');
g_free (str);
str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
g_free (str);
str = g_strjoin (NULL,
GLIB_TEST_STRING,
GLIB_TEST_STRING,
GLIB_TEST_STRING,
GLIB_TEST_STRING,
NULL);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
g_free (str);
str = g_strjoin (":",
GLIB_TEST_STRING,
GLIB_TEST_STRING,
GLIB_TEST_STRING,
GLIB_TEST_STRING,
NULL);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
g_free (str);
}
/* Testing g_strcanon() function with various positive and negative cases */
static void
test_strcanon (void)
{
@ -349,24 +356,25 @@ test_strcanon (void)
"*assertion*!= NULL*");
str = g_strcanon (NULL, "ab", 'y');
g_test_assert_expected_messages ();
g_assert (str == NULL);
g_assert_null (str);
str = g_strdup ("abxabxab");
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion*!= NULL*");
ret = g_strcanon (str, NULL, 'y');
g_test_assert_expected_messages ();
g_assert (ret == NULL);
g_assert_null (ret);
g_free (str);
}
str = g_strdup ("abxabxab");
str = g_strcanon (str, "ab", 'y');
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "abyabyab");
g_free (str);
}
/* Testing g_strcompress() and g_strescape() functions with various cases */
static void
test_strcompress_strescape (void)
{
@ -380,7 +388,7 @@ test_strcompress_strescape (void)
"*assertion*!= NULL*");
str = g_strcompress (NULL);
g_test_assert_expected_messages ();
g_assert (str == NULL);
g_assert_null (str);
/* trailing slashes are not allowed */
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
@ -392,7 +400,7 @@ test_strcompress_strescape (void)
}
str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313\\12345z");
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313\12345z");
g_free (str);
@ -403,29 +411,30 @@ test_strcompress_strescape (void)
"*assertion*!= NULL*");
str = g_strescape (NULL, NULL);
g_test_assert_expected_messages ();
g_assert (str == NULL);
g_assert_null (str);
}
str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313");
g_free (str);
str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313",
"\b\f\001\002\003\004");
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\\v\003\\177\\234\\313");
g_free (str);
/* round trip */
tmp = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
str = g_strcompress (tmp);
g_assert (str != NULL);
g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313");
g_free (str);
g_free (tmp);
}
/* Testing g_ascii_strcasecmp() and g_ascii_strncasecmp() */
static void
test_ascii_strcasecmp (void)
{
@ -437,13 +446,13 @@ test_ascii_strcasecmp (void)
"*assertion*!= NULL*");
res = g_ascii_strcasecmp ("foo", NULL);
g_test_assert_expected_messages ();
g_assert (res == FALSE);
g_assert_false (res);
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion*!= NULL*");
res = g_ascii_strcasecmp (NULL, "foo");
g_test_assert_expected_messages ();
g_assert (res == FALSE);
g_assert_false (res);
}
res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
@ -504,6 +513,7 @@ do_test_strchug (const gchar *str, const gchar *expected)
g_assert_cmpint (res, ==, TRUE);
}
/* Testing g_strchug() function with various positive and negative cases */
static void
test_strchug (void)
{
@ -539,6 +549,7 @@ do_test_strchomp (const gchar *str, const gchar *expected)
g_assert_cmpint (res, ==, TRUE);
}
/* Testing g_strchomp() function with various positive and negative cases */
static void
test_strchomp (void)
{
@ -559,6 +570,7 @@ test_strchomp (void)
do_test_strchomp ("a a ", "a a");
}
/* Testing g_strreverse() function with various positive and negative cases */
static void
test_strreverse (void)
{
@ -571,22 +583,23 @@ test_strreverse (void)
"*assertion*!= NULL*");
str = g_strreverse (NULL);
g_test_assert_expected_messages ();
g_assert (str == NULL);
g_assert_null (str);
}
str = p = g_strdup ("abcde");
str = g_strreverse (str);
g_assert (str != NULL);
g_assert (p == str);
g_assert_nonnull (str);
g_assert_true (p == str);
g_assert_cmpstr (str, ==, "edcba");
g_free (str);
}
/* Testing g_strncasecmp() functions */
static void
test_strncasecmp (void)
{
g_assert (g_strncasecmp ("abc1", "ABC2", 3) == 0);
g_assert (g_strncasecmp ("abc1", "ABC2", 4) != 0);
g_assert_cmpint (g_strncasecmp ("abc1", "ABC2", 3), ==, 0);
g_assert_cmpint (g_strncasecmp ("abc1", "ABC2", 4), !=, 0);
}
static void
@ -599,71 +612,72 @@ test_strstr (void)
/* strstr_len */
res = g_strstr_len (haystack, 6, "xxx");
g_assert (res == NULL);
g_assert_null (res);
res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
g_assert (res == NULL);
g_assert_null (res);
res = g_strstr_len (haystack, 3, "Bar");
g_assert (res == NULL);
g_assert_null (res);
res = g_strstr_len (haystack, 6, "");
g_assert (res == haystack);
g_assert_true (res == haystack);
g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
res = g_strstr_len (haystack, 6, "Bar");
g_assert (res == haystack + 3);
g_assert_true (res == haystack + 3);
g_assert_cmpstr (res, ==, "BarFooBarFoo");
res = g_strstr_len (haystack, -1, "Bar");
g_assert (res == haystack + 3);
g_assert_true (res == haystack + 3);
g_assert_cmpstr (res, ==, "BarFooBarFoo");
/* strrstr */
res = g_strrstr (haystack, "xxx");
g_assert (res == NULL);
g_assert_null (res);
res = g_strrstr (haystack, "FooBarFooBarFooBar");
g_assert (res == NULL);
g_assert_null (res);
res = g_strrstr (haystack, "");
g_assert (res == haystack);
g_assert_true (res == haystack);
g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
res = g_strrstr (haystack, "Bar");
g_assert (res == haystack + 9);
g_assert_true (res == haystack + 9);
g_assert_cmpstr (res, ==, "BarFoo");
/* strrstr_len */
res = g_strrstr_len (haystack, 14, "xxx");
g_assert (res == NULL);
g_assert_null (res);
res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
g_assert (res == NULL);
g_assert_null (res);
res = g_strrstr_len (haystack, 3, "Bar");
g_assert (res == NULL);
g_assert_null (res);
res = g_strrstr_len (haystack, 14, "BarFoo");
g_assert (res == haystack + 3);
g_assert_true (res == haystack + 3);
g_assert_cmpstr (res, ==, "BarFooBarFoo");
res = g_strrstr_len (haystack, 15, "BarFoo");
g_assert (res == haystack + 9);
g_assert_true (res == haystack + 9);
g_assert_cmpstr (res, ==, "BarFoo");
res = g_strrstr_len (haystack, -1, "BarFoo");
g_assert (res == haystack + 9);
g_assert_true (res == haystack + 9);
g_assert_cmpstr (res, ==, "BarFoo");
/* test case for strings with \0 in the middle */
*(haystack + 7) = '\0';
res = g_strstr_len (haystack, 15, "BarFoo");
g_assert (res == NULL);
g_assert_null (res);
g_free (haystack);
}
/* Testing g_str_has_prefix() */
static void
test_has_prefix (void)
{
@ -675,13 +689,13 @@ test_has_prefix (void)
"*assertion*!= NULL*");
res = g_str_has_prefix ("foo", NULL);
g_test_assert_expected_messages ();
g_assert (res == FALSE);
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_test_assert_expected_messages ();
g_assert (res == FALSE);
g_assert_false (res);
}
res = g_str_has_prefix ("foo", "bar");
@ -717,35 +731,35 @@ test_has_suffix (void)
"*assertion*!= NULL*");
res = g_str_has_suffix ("foo", NULL);
g_test_assert_expected_messages ();
g_assert (res == FALSE);
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_test_assert_expected_messages ();
g_assert (res == FALSE);
g_assert_false (res);
}
res = g_str_has_suffix ("foo", "bar");
g_assert_cmpint (res, ==, FALSE);
g_assert_false (res);
res = g_str_has_suffix ("bar", "foobar");
g_assert_cmpint (res, ==, FALSE);
g_assert_false (res);
res = g_str_has_suffix ("foobar", "foo");
g_assert_cmpint (res, ==, FALSE);
g_assert_false (res);
res = g_str_has_suffix ("foobar", "bar");
g_assert_cmpint (res, ==, TRUE);
g_assert_true (res);
res = g_str_has_suffix ("foo", "");
g_assert_cmpint (res, ==, TRUE);
g_assert_true (res);
res = g_str_has_suffix ("foo", "foo");
g_assert_cmpint (res, ==, TRUE);
g_assert_true (res);
res = g_str_has_suffix ("", "");
g_assert_cmpint (res, ==, TRUE);
g_assert_true (res);
}
static void
@ -761,7 +775,7 @@ strv_check (gchar **strv, ...)
const gchar *str = va_arg (list, const char *);
if (strv[i] == NULL)
{
g_assert (str == NULL);
g_assert_null (str);
break;
}
if (str == NULL)
@ -779,6 +793,7 @@ strv_check (gchar **strv, ...)
g_strfreev (strv);
}
/* Testing g_strsplit() function with various positive and negative cases */
static void
test_strsplit (void)
{
@ -822,6 +837,7 @@ test_strsplit (void)
strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
}
/* Testing function g_strsplit_set() */
static void
test_strsplit_set (void)
{
@ -846,7 +862,7 @@ test_strsplit_set (void)
strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
strv_check (g_strsplit_set ("", ",", 0), NULL);
strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
@ -883,10 +899,11 @@ test_strsplit_set (void)
strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
}
/* Testing g_strv_length() function with various positive and negative cases */
static void
test_strv_length (void)
{
@ -914,13 +931,13 @@ static void
check_strtod_string (gchar *number,
double res,
gboolean check_end,
gint correct_len)
gsize correct_len)
{
double d;
gint l;
gsize l;
gchar *dummy;
/* we try a copy of number, with some free space for malloc before that.
/* we try a copy of number, with some free space for malloc before that.
* This is supposed to smash the some wrong pointer calculations. */
dummy = g_malloc (100000);
@ -933,8 +950,9 @@ check_strtod_string (gchar *number,
setlocale (LC_NUMERIC, locales[l]);
d = g_ascii_strtod (number, &end);
g_assert (isnan (res) ? isnan (d) : (d == res));
g_assert ((end - number) == (check_end ? correct_len : strlen (number)));
g_assert_true (isnan (res) ? isnan (d) : (d == res));
g_assert_true ((gsize) (end - number) ==
(check_end ? correct_len : strlen (number)));
}
g_free (number);
@ -943,7 +961,7 @@ check_strtod_string (gchar *number,
static void
check_strtod_number (gdouble num, gchar *fmt, gchar *str)
{
int l;
gsize l;
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
for (l = 0; l < G_N_ELEMENTS (locales); l++)
@ -954,8 +972,9 @@ check_strtod_number (gdouble num, gchar *fmt, gchar *str)
}
}
/* Testing g_ascii_strtod() function with various positive and negative cases */
static void
test_strtod (void)
test_ascii_strtod (void)
{
gdouble d, our_nan, our_inf;
char buffer[G_ASCII_DTOSTR_BUF_SIZE];
@ -966,15 +985,25 @@ test_strtod (void)
/* Do this before any call to setlocale. */
our_nan = atof ("NaN");
#endif
g_assert (isnan (our_nan));
g_assert_true (isnan (our_nan));
#ifdef INFINITY
our_inf = INFINITY;
#else
our_inf = atof ("Infinity");
#endif
g_assert (our_inf > 1 && our_inf == our_inf / 2);
g_assert_true (our_inf > 1 && our_inf == our_inf / 2);
/* Testing degenerated cases */
if (g_test_undefined ())
{
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion*!= NULL*");
d = g_ascii_strtod (NULL, NULL);
g_test_assert_expected_messages ();
}
/* Testing normal cases */
check_strtod_string ("123.123", 123.123, FALSE, 0);
check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
@ -1006,17 +1035,17 @@ test_strtod (void)
#ifndef _MSC_VER
/* the values of d in the following 2 tests generate a C1064 compiler limit error */
d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
#endif
d = pow (2.0, -1024.1);
g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
d = -pow (2.0, -1024.1);
g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
/* for #343899 */
check_strtod_string (" 0.75", 0.75, FALSE, 0);
@ -1055,9 +1084,9 @@ check_uint64 (const gchar *str,
actual = g_ascii_strtoull (str, &endptr, base);
err = errno;
g_assert (actual == result);
g_assert_true (actual == result);
g_assert_cmpstr (end, ==, endptr);
g_assert (err == error);
g_assert_true (err == error);
}
static void
@ -1075,9 +1104,9 @@ check_int64 (const gchar *str,
actual = g_ascii_strtoll (str, &endptr, base);
err = errno;
g_assert (actual == result);
g_assert_true (actual == result);
g_assert_cmpstr (end, ==, endptr);
g_assert (err == error);
g_assert_true (err == error);
}
static void
@ -1103,6 +1132,7 @@ test_strtoll (void)
check_int64 ("-001", "", 10, -1, 0);
}
/* Testing functions bounds */
static void
test_bounds (void)
{
@ -1129,12 +1159,12 @@ test_bounds (void)
g_mapped_file_unref (before);
g_mapped_file_unref (after);
g_assert (file != NULL);
g_assert_nonnull (file);
g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
string = g_mapped_file_get_contents (file);
/* ensure they're all non-nul */
g_assert (memchr (string, '\0', 4096) == NULL);
g_assert_null (memchr (string, '\0', 4096));
/* test set 1: ensure that nothing goes past its maximum length, even in
* light of a missing nul terminator.
@ -1146,7 +1176,7 @@ test_bounds (void)
g_free (tmp);
/* found no bugs in gnome, i hope :) */
g_assert (g_strstr_len (string, 4096, "BUGS") == NULL);
g_assert_null (g_strstr_len (string, 4096, "BUGS"));
g_strstr_len (string, 4096, "B");
g_strstr_len (string, 4096, ".");
g_strstr_len (string, 4096, "");
@ -1225,8 +1255,8 @@ test_bounds (void)
tmp = g_ascii_strdown (string, -1);
tmp2 = g_ascii_strdown (tmp, -1);
g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
g_assert_cmpint (strlen(string), ==, strlen(tmp));
g_assert_cmpint (strlen (tmp), ==, strlen (tmp2));
g_assert_cmpint (strlen (string), ==, strlen (tmp));
g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
@ -1235,8 +1265,8 @@ test_bounds (void)
tmp = g_ascii_strup (string, -1);
tmp2 = g_ascii_strup (string, -1);
g_assert_cmpint (strlen(tmp), ==, strlen(tmp2));
g_assert_cmpint (strlen(string), ==, strlen(tmp));
g_assert_cmpint (strlen (tmp), ==, strlen (tmp2));
g_assert_cmpint (strlen (string), ==, strlen (tmp));
g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
@ -1295,6 +1325,7 @@ test_bounds (void)
g_mapped_file_unref (file);
}
/* Testing g_strip_context() function with various cases */
static void
test_strip_context (void)
{
@ -1302,23 +1333,22 @@ test_strip_context (void)
const gchar *msgval;
const gchar *s;
msgid = "blabla";
msgval = "bla";
s = g_strip_context (msgid, msgval);
g_assert (s == msgval);
g_assert_true (s == msgval);
msgid = msgval = "blabla";
s = g_strip_context (msgid, msgval);
g_assert (s == msgval);
g_assert_true (s == msgval);
msgid = msgval = "blabla|foo";
s = g_strip_context (msgid, msgval);
g_assert (s == msgval + 7);
g_assert_true (s == msgval + 7);
msgid = msgval = "blabla||bar";
s = g_strip_context (msgid, msgval);
g_assert (s == msgval + 7);
g_assert_true (s == msgval + 7);
}
/* Test the strings returned by g_strerror() are valid and unique. On Windows,
@ -1340,8 +1370,8 @@ test_strerror (void)
gboolean is_unknown;
str = g_strerror (i);
is_unknown = (strcmp (str, unknown_str) == 0);
g_assert (str != NULL);
g_assert (g_utf8_validate (str, -1, NULL));
g_assert_nonnull (str);
g_assert_true (g_utf8_validate (str, -1, NULL));
g_assert_true (!g_hash_table_contains (strs, str) || is_unknown);
g_hash_table_add (strs, (char *)str);
}
@ -1349,6 +1379,7 @@ test_strerror (void)
g_hash_table_unref (strs);
}
/* Testing g_strsignal() function with various cases */
static void
test_strsignal (void)
{
@ -1358,11 +1389,12 @@ test_strsignal (void)
for (i = 1; i < 20; i++)
{
str = g_strsignal (i);
g_assert (str != NULL);
g_assert (g_utf8_validate (str, -1, NULL));
g_assert_nonnull (str);
g_assert_true (g_utf8_validate (str, -1, NULL));
}
}
/* Testing g_strup(), g_strdown() and g_strcasecmp() */
static void
test_strup (void)
{
@ -1371,10 +1403,11 @@ test_strup (void)
s = g_strdup ("lower");
g_assert_cmpstr (g_strup (s), ==, "LOWER");
g_assert_cmpstr (g_strdown (s), ==, "lower");
g_assert (g_strcasecmp ("lower", "LOWER") == 0);
g_assert_cmpint (g_strcasecmp ("lower", "LOWER"), ==, 0);
g_free (s);
}
/* Testing g_str_to_ascii() function with various cases */
static void
test_transliteration (void)
{
@ -1481,12 +1514,13 @@ test_transliteration (void)
g_free (out);
}
/* Testing g_strv_contains() function with various cases */
static void
test_strv_contains (void)
{
static const gchar *strv_simple[] = { "hello", "there", NULL };
static const gchar *strv_dupe[] = { "dupe", "dupe", NULL };
static const gchar *strv_empty[] = { NULL };
const gchar *strv_simple[] = { "hello", "there", NULL };
const gchar *strv_dupe[] = { "dupe", "dupe", NULL };
const gchar *strv_empty[] = { NULL };
g_assert_true (g_strv_contains (strv_simple, "hello"));
g_assert_true (g_strv_contains (strv_simple, "there"));
@ -1609,11 +1643,13 @@ const TestData test_data[] = {
{ "+ 0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
};
/* Testing g_ascii_string_to_signed() and g_ascii_string_to_unsigned() functions */
static void
test_ascii_string_to_number_usual (void)
{
gsize idx;
/* Testing usual cases */
for (idx = 0; idx < G_N_ELEMENTS (test_data); ++idx)
{
GError *error = NULL;
@ -1658,7 +1694,7 @@ test_ascii_string_to_number_usual (void)
if (data->should_fail)
{
g_assert_false (result);
g_assert_error (error, G_NUMBER_PARSER_ERROR, data->error_code);
g_assert_error (error, G_NUMBER_PARSER_ERROR, (gint) data->error_code);
g_clear_error (&error);
}
else
@ -1670,6 +1706,7 @@ test_ascii_string_to_number_usual (void)
}
}
/* Testing pathological cases for g_ascii_string_to_(un)signed() */
static void
test_ascii_string_to_number_pathological (void)
{
@ -1751,39 +1788,39 @@ main (int argc,
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
g_test_add_func ("/strfuncs/strdup", test_strdup);
g_test_add_func ("/strfuncs/strndup", test_strndup);
g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
g_test_add_func ("/strfuncs/strdupv", test_strdupv);
g_test_add_func ("/strfuncs/strnfill", test_strnfill);
g_test_add_func ("/strfuncs/strconcat", test_strconcat);
g_test_add_func ("/strfuncs/strjoin", test_strjoin);
g_test_add_func ("/strfuncs/strcanon", test_strcanon);
g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
g_test_add_func ("/strfuncs/strchug", test_strchug);
g_test_add_func ("/strfuncs/strchomp", test_strchomp);
g_test_add_func ("/strfuncs/strreverse", test_strreverse);
g_test_add_func ("/strfuncs/strncasecmp", test_strncasecmp);
g_test_add_func ("/strfuncs/strstr", test_strstr);
g_test_add_func ("/strfuncs/ascii-string-to-num/pathological", test_ascii_string_to_number_pathological);
g_test_add_func ("/strfuncs/ascii-string-to-num/usual", test_ascii_string_to_number_usual);
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-suffix", test_has_suffix);
g_test_add_func ("/strfuncs/strcanon", test_strcanon);
g_test_add_func ("/strfuncs/strchomp", test_strchomp);
g_test_add_func ("/strfuncs/strchug", test_strchug);
g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
g_test_add_func ("/strfuncs/strconcat", test_strconcat);
g_test_add_func ("/strfuncs/strdup", test_strdup);
g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
g_test_add_func ("/strfuncs/strdupv", test_strdupv);
g_test_add_func ("/strfuncs/strerror", test_strerror);
g_test_add_func ("/strfuncs/strip-context", test_strip_context);
g_test_add_func ("/strfuncs/strjoin", test_strjoin);
g_test_add_func ("/strfuncs/strncasecmp", test_strncasecmp);
g_test_add_func ("/strfuncs/strndup", test_strndup);
g_test_add_func ("/strfuncs/strnfill", test_strnfill);
g_test_add_func ("/strfuncs/strreverse", test_strreverse);
g_test_add_func ("/strfuncs/strsignal", test_strsignal);
g_test_add_func ("/strfuncs/strsplit", test_strsplit);
g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
g_test_add_func ("/strfuncs/strv-length", test_strv_length);
g_test_add_func ("/strfuncs/strtod", test_strtod);
g_test_add_func ("/strfuncs/strstr", test_strstr);
g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
g_test_add_func ("/strfuncs/bounds-check", test_bounds);
g_test_add_func ("/strfuncs/strip-context", test_strip_context);
g_test_add_func ("/strfuncs/strerror", test_strerror);
g_test_add_func ("/strfuncs/strsignal", test_strsignal);
g_test_add_func ("/strfuncs/strup", test_strup);
g_test_add_func ("/strfuncs/transliteration", test_transliteration);
g_test_add_func ("/strfuncs/strv-contains", test_strv_contains);
g_test_add_func ("/strfuncs/strv-equal", test_strv_equal);
g_test_add_func ("/strfuncs/ascii-string-to-num/usual", test_ascii_string_to_number_usual);
g_test_add_func ("/strfuncs/ascii-string-to-num/pathological", test_ascii_string_to_number_pathological);
g_test_add_func ("/strfuncs/strv-length", test_strv_length);
g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
g_test_add_func ("/strfuncs/transliteration", test_transliteration);
return g_test_run();
}