* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
+/*
+ * Modified by the GLib Team and others 1997-2000. See the AUTHORS
+ * file for a list of people on the GLib Team. See the ChangeLog
+ * files for a list of changes. These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+#undef G_LOG_DOMAIN
+
#include <stdio.h>
#include <string.h>
#include "glib.h"
#define C2P(c) ((gpointer) ((long) (c)))
#define P2C(p) ((gchar) ((long) (p)))
+#define GLIB_TEST_STRING "el dorado "
+#define GLIB_TEST_STRING_5 "el do"
+
static gboolean
node_build_string (GNode *node,
gpointer data)
GNode *node_G;
GNode *node_J;
guint i;
- gchar *tstring;
+ gchar *tstring, *cstring;
g_print ("checking n-way trees: ");
failed = FALSE;
g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
g_free (tstring); tstring = NULL;
-
+
+ cstring = NULL;
+ node = g_node_copy (root);
+ TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
+ TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
+ g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
+ g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
+ TEST (cstring, strcmp (tstring, cstring) == 0);
+ g_free (tstring); tstring = NULL;
+ g_free (cstring); cstring = NULL;
+ g_node_destroy (node);
+
g_node_destroy (root);
/* allocation tests */
g_print ("ok\n");
}
-void
+static gboolean
+my_hash_callback_remove (gpointer key,
+ gpointer value,
+ gpointer user_data)
+{
+ int *d = value;
+
+ if ((*d) % 2)
+ return TRUE;
+
+ return FALSE;
+}
+
+static void
+my_hash_callback_remove_test (gpointer key,
+ gpointer value,
+ gpointer user_data)
+{
+ int *d = value;
+
+ if ((*d) % 2)
+ g_print ("bad!\n");
+}
+
+static void
my_hash_callback (gpointer key,
gpointer value,
gpointer user_data)
*d = 1;
}
-guint
+static guint
my_hash (gconstpointer key)
{
return (guint) *((const gint*) key);
}
-gint
+static gint
my_hash_compare (gconstpointer a,
gconstpointer b)
{
return *((const gint*) a) == *((const gint*) b);
}
-gint
+static gint
my_list_compare_one (gconstpointer a, gconstpointer b)
{
gint one = *((const gint*)a);
return one-two;
}
-gint
+static gint
my_list_compare_two (gconstpointer a, gconstpointer b)
{
gint one = *((const gint*)a);
return two-one;
}
-/* void
+/* static void
my_list_print (gpointer a, gpointer b)
{
gint three = *((gint*)a);
g_print("%d", three);
}; */
-gint
+static gint
my_compare (gconstpointer a,
gconstpointer b)
{
return *cha - *chb;
}
-gint
+static gint
my_traverse (gpointer key,
gpointer value,
gpointer data)
gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
gchar *string;
- gchar *mem[10000], *tmp_string, *tmp_string_2;
+ gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
gint i, j;
GArray *garray;
GPtrArray *gparray;
gchar *filename;
gchar *dirname;
} dirname_checks[] = {
+#ifndef G_OS_WIN32
{ "/", "/" },
{ "////", "/" },
{ ".////", "." },
{ "a/b", "a" },
{ "a/b/", "a/b" },
{ "c///", "c" },
+#else
+ { "\\", "\\" },
+ { ".\\\\\\\\", "." },
+ { ".", "." },
+ { "..", "." },
+ { "..\\", ".." },
+ { "..\\\\\\\\", ".." },
+ { "", "." },
+ { "a\\b", "a" },
+ { "a\\b\\", "a\\b" },
+ { "c\\\\\\", "c" },
+#endif
};
guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
+ guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
+ guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
+#ifdef G_HAVE_GINT64
+ guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
+ gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
+#endif
g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
glib_major_version,
string = g_get_current_dir ();
g_print ("cwd: %s\n", string);
g_free (string);
+ g_print ("user: %s\n", g_get_user_name ());
+ g_print ("real: %s\n", g_get_real_name ());
+ g_print ("home: %s\n", g_get_home_dir ());
+ g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
/* type sizes */
- g_print ("checking size of gint8: %d", sizeof (gint8));
+ g_print ("checking size of gint8: %d", (int)sizeof (gint8));
TEST (NULL, sizeof (gint8) == 1);
- g_print ("\nchecking size of gint16: %d", sizeof (gint16));
+ g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
TEST (NULL, sizeof (gint16) == 2);
- g_print ("\nchecking size of gint32: %d", sizeof (gint32));
+ g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
TEST (NULL, sizeof (gint32) == 4);
-#ifdef HAVE_GINT64
- g_print ("\nchecking size of gint64: %d", sizeof (gint64));
+ g_print ("\nchecking size of gsize: %d", (int)sizeof (gsize));
+#ifdef G_HAVE_GINT64
+ g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
TEST (NULL, sizeof (gint64) == 8);
-#endif /* HAVE_GINT64 */
+#endif /* G_HAVE_GINT64 */
g_print ("\n");
- g_print ("checking g_dirname()...");
+ g_print ("checking g_path_get_dirname()...");
for (i = 0; i < n_dirname_checks; i++)
{
gchar *dirname;
- dirname = g_dirname (dirname_checks[i].filename);
+ dirname = g_path_get_dirname (dirname_checks[i].filename);
if (strcmp (dirname, dirname_checks[i].dirname) != 0)
{
g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
g_list_free (list);
list = NULL;
-
+
for (i = 0; i < 10; i++)
list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
if (*((gint*) t->data) != i)
g_error ("Sorted insert failed");
}
-
+
g_list_free (list);
list = NULL;
-
+
for (i = 0; i < 10; i++)
list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
if (*((gint*) t->data) != (9 - i))
g_error ("Sorted insert failed");
}
-
+
+ g_list_free (list);
+ list = NULL;
+
+ for (i = 0; i < 10; i++)
+ list = g_list_prepend (list, &morenums[i]);
+
+ list = g_list_sort (list, my_list_compare_two);
+
+ /*
+ g_print("\n");
+ g_list_foreach (list, my_list_print, NULL);
+ */
+
+ for (i = 0; i < 10; i++)
+ {
+ t = g_list_nth (list, i);
+ if (*((gint*) t->data) != (9 - i))
+ g_error ("Merge sort failed");
+ }
+
g_list_free (list);
g_print ("ok\n");
if (*((gint*) st->data) != i)
g_error ("Sorted insert failed");
}
-
+
g_slist_free(slist);
slist = NULL;
-
+
for (i = 0; i < 10; i++)
slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
if (*((gint*) st->data) != (9 - i))
g_error("Sorted insert failed");
}
-
+
+ g_slist_free(slist);
+ slist = NULL;
+
+ for (i = 0; i < 10; i++)
+ slist = g_slist_prepend (slist, &morenums[i]);
+
+ slist = g_slist_sort (slist, my_list_compare_two);
+
+ /*
+ g_print("\n");
+ g_slist_foreach (slist, my_list_print, NULL);
+ */
+
+ for (i = 0; i < 10; i++)
+ {
+ st = g_slist_nth (slist, i);
+ if (*((gint*) st->data) != (9 - i))
+ g_error("Sorted insert failed");
+ }
+
g_slist_free(slist);
g_print ("ok\n");
for (i = 0; i < 10000; i++)
g_hash_table_remove (hash_table, &array[i]);
+ for (i = 0; i < 10000; i++)
+ {
+ array[i] = i;
+ g_hash_table_insert (hash_table, &array[i], &array[i]);
+ }
+
+ if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
+ g_hash_table_size (hash_table) != 5000)
+ g_print ("bad!\n");
+
+ g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
+
+
g_hash_table_destroy (hash_table);
g_print ("ok\n");
g_print ("checking arrays...");
- garray = g_array_new (FALSE);
+ garray = g_array_new (FALSE, FALSE, sizeof (gint));
for (i = 0; i < 10000; i++)
- g_array_append_val (garray, gint, i);
+ g_array_append_val (garray, i);
for (i = 0; i < 10000; i++)
if (g_array_index (garray, gint, i) != i)
g_array_free (garray, TRUE);
- garray = g_array_new (FALSE);
- for (i = 0; i < 10000; i++)
- g_array_prepend_val (garray, gint, i);
+ garray = g_array_new (FALSE, FALSE, sizeof (gint));
+ for (i = 0; i < 100; i++)
+ g_array_prepend_val (garray, i);
- for (i = 0; i < 10000; i++)
- if (g_array_index (garray, gint, i) != (10000 - i - 1))
- g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
+ for (i = 0; i < 100; i++)
+ if (g_array_index (garray, gint, i) != (100 - i - 1))
+ g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
g_array_free (garray, TRUE);
for (i = 0; i < 10000; i++)
g_string_append_c (string1, 'a'+(i%26));
+#ifndef G_OS_WIN32
+ /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
+ the %10000.10000f format... */
g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
"this pete guy sure is a wuss, like he's the number ",
1,
" wuss. everyone agrees.\n",
string1->str,
10, 666, 15, 15, 666.666666666, 666.666666666);
+#else
+ g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
+ "this pete guy sure is a wuss, like he's the number ",
+ 1,
+ " wuss. everyone agrees.\n",
+ string1->str,
+ 10, 666, 15, 15, 666.666666666, 666.666666666);
+#endif
+ g_print ("string2 length = %d...\n", string2->len);
+ string2->str[70] = '\0';
+ g_print ("first 70 chars:\n%s\n", string2->str);
+ string2->str[141] = '\0';
+ g_print ("next 70 chars:\n%s\n", string2->str+71);
+ string2->str[212] = '\0';
+ g_print ("and next 70:\n%s\n", string2->str+142);
+ g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
+
+ g_string_free (string1, TRUE);
+ g_string_free (string2, TRUE);
+
+ /* append */
+ string1 = g_string_new ("firsthalf");
+ g_string_append (string1, "lasthalf");
+ g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+ g_string_free (string1, TRUE);
+
+ /* append_len */
+
+ string1 = g_string_new ("firsthalf");
+ g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
+ g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+ g_string_free (string1, TRUE);
+
+ /* prepend */
+ string1 = g_string_new ("lasthalf");
+ g_string_prepend (string1, "firsthalf");
+ g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+ g_string_free (string1, TRUE);
+
+ /* prepend_len */
+ string1 = g_string_new ("lasthalf");
+ g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
+ g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+ g_string_free (string1, TRUE);
+
+ /* insert */
+ string1 = g_string_new ("firstlast");
+ g_string_insert (string1, 5, "middle");
+ g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+ g_string_free (string1, TRUE);
+
+ /* insert with pos == end of the string */
+ string1 = g_string_new ("firstmiddle");
+ g_string_insert (string1, strlen ("firstmiddle"), "last");
+ g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+ g_string_free (string1, TRUE);
+
+ /* insert_len */
+
+ string1 = g_string_new ("firstlast");
+ g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
+ g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+ g_string_free (string1, TRUE);
+
+ /* insert_len with magic -1 pos for append */
+ string1 = g_string_new ("first");
+ g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
+ g_assert (strcmp (string1->str, "firstlast") == 0);
+ g_string_free (string1, TRUE);
+
+ /* insert_len with magic -1 len for strlen-the-string */
+ string1 = g_string_new ("first");
+ g_string_insert_len (string1, 5, "last", -1);
+ g_assert (strcmp (string1->str, "firstlast") == 0);
+ g_string_free (string1, TRUE);
+
g_print ("ok\n");
- g_print ("checking timers...\n");
+ /* g_string_equal */
+ string1 = g_string_new ("test");
+ string2 = g_string_new ("te");
+ g_assert (! g_string_equal(string1, string2));
+ g_string_append (string2, "st");
+ g_assert (g_string_equal(string1, string2));
+ g_string_free (string1, TRUE);
+ g_string_free (string2, TRUE);
+
+ /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
+ string1 = g_string_new ("fiddle");
+ string2 = g_string_new ("fiddle");
+ g_assert (g_string_equal(string1, string2));
+ g_string_append_c(string1, '\0');
+ g_assert (! g_string_equal(string1, string2));
+ g_string_append_c(string2, '\0');
+ g_assert (g_string_equal(string1, string2));
+ g_string_append_c(string1, 'x');
+ g_string_append_c(string2, 'y');
+ g_assert (! g_string_equal(string1, string2));
+ g_assert (string1->len == 8);
+ g_string_append(string1, "yzzy");
+ g_assert (string1->len == 12);
+ g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
+ g_string_insert(string1, 1, "QED");
+ g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
+ g_string_free (string1, TRUE);
+ g_string_free (string2, TRUE);
+
+ g_print ("test positional printf formats (not supported): ");
+ string = g_strdup_printf ("%.*s%s", 5, "a", "b");
+ tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
+ g_print ("%s%s\n", string, tmp_string);
+ g_free (tmp_string);
+ g_free (string);
+ g_print ("checking timers...\n");
+
timer = g_timer_new ();
g_print (" spinning for 3 seconds...\n");
g_print ("ok\n");
- g_print ("checking g_strcasecmp...\n");
+ g_print ("checking g_strcasecmp...");
+ g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
+ g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
+ g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
+ g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
+ g_assert (g_strcasecmp ("", "") == 0);
+ g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
+ g_assert (g_strcasecmp ("a", "b") < 0);
+ g_assert (g_strcasecmp ("a", "B") < 0);
+ g_assert (g_strcasecmp ("A", "b") < 0);
+ g_assert (g_strcasecmp ("A", "B") < 0);
+ g_assert (g_strcasecmp ("b", "a") > 0);
+ g_assert (g_strcasecmp ("b", "A") > 0);
+ g_assert (g_strcasecmp ("B", "a") > 0);
+ g_assert (g_strcasecmp ("B", "A") > 0);
+
+ g_print ("ok\n");
+
+ g_print ("checking g_strdup...");
+ g_assert(g_strdup(NULL) == NULL);
+ string = g_strdup(GLIB_TEST_STRING);
+ g_assert(string != NULL);
+ g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
+ g_free(string);
+
+ g_print ("ok\n");
+
+ g_print ("checking g_strconcat...");
+ string = g_strconcat(GLIB_TEST_STRING, NULL);
+ g_assert(string != NULL);
+ g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
+ g_free(string);
+ string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
+ GLIB_TEST_STRING, NULL);
+ g_assert(string != NULL);
+ g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
+ GLIB_TEST_STRING) == 0);
+ g_free(string);
+ g_print ("ok\n");
+
+
+ g_print("checking g_strlcpy/g_strlcat...");
+ /* The following is a torture test for strlcpy/strlcat, with lots of
+ * checking; normal users wouldn't use them this way!
+ */
+ string = g_malloc (6);
+ *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
+ *string = 'q';
+ g_assert (g_strlcpy(string, "" , 5) == 0);
+ g_assert ( *string == '\0' );
+ *string = 'q';
+ g_assert (g_strlcpy(string, "abc" , 5) == 3);
+ g_assert ( *(string + 3) == '\0' );
+ g_assert (g_str_equal(string, "abc"));
+ g_assert (g_strlcpy(string, "abcd" , 5) == 4);
+ g_assert ( *(string + 4) == '\0' );
+ g_assert ( *(string + 5) == 'Z' );
+ g_assert (g_str_equal(string, "abcd"));
+ g_assert (g_strlcpy(string, "abcde" , 5) == 5);
+ g_assert ( *(string + 4) == '\0' );
+ g_assert ( *(string + 5) == 'Z' );
+ g_assert (g_str_equal(string, "abcd"));
+ g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
+ g_assert ( *(string + 4) == '\0' );
+ g_assert ( *(string + 5) == 'Z' );
+ g_assert (g_str_equal(string, "abcd"));
+ *string = 'Y';
+ *(string + 1)= '\0';
+ g_assert (g_strlcpy(string, "Hello" , 0) == 5);
+ g_assert (*string == 'Y');
+ *string = '\0';
+ g_assert (g_strlcat(string, "123" , 5) == 3);
+ g_assert ( *(string + 3) == '\0' );
+ g_assert (g_str_equal(string, "123"));
+ g_assert (g_strlcat(string, "" , 5) == 3);
+ g_assert ( *(string + 3) == '\0' );
+ g_assert (g_str_equal(string, "123"));
+ g_assert (g_strlcat(string, "4", 5) == 4);
+ g_assert (g_str_equal(string, "1234"));
+ g_assert (g_strlcat(string, "5", 5) == 5);
+ g_assert ( *(string + 4) == '\0' );
+ g_assert (g_str_equal(string, "1234"));
+ g_assert ( *(string + 5) == 'Z' );
+ *string = 'Y';
+ *(string + 1)= '\0';
+ g_assert (g_strlcat(string, "123" , 0) == 3);
+ g_assert (*string == 'Y');
+
+ /* A few more tests, demonstrating more "normal" use */
+ g_assert (g_strlcpy(string, "hi", 5) == 2);
+ g_assert (g_str_equal(string, "hi"));
+ g_assert (g_strlcat(string, "t", 5) == 3);
+ g_assert (g_str_equal(string, "hit"));
+ g_free(string);
+
+ g_print ("ok\n");
+
+
+ g_print ("checking g_strdup_printf...");
+ string = g_strdup_printf ("%05d %-5s", 21, "test");
+ g_assert (string != NULL);
+ g_assert (strcmp(string, "00021 test ") == 0);
+ g_free (string);
+
+ g_print ("ok\n");
/* g_debug (argv[0]); */
gbarray = g_byte_array_new ();
for (i = 0; i < 10000; i++)
- g_byte_array_append (gbarray, "abcd", 4);
+ g_byte_array_append (gbarray, (guint8*) "abcd", 4);
for (i = 0; i < 10000; i++)
{
}
g_byte_array_free (gbarray, TRUE);
+ g_print ("ok\n");
+
+ g_printerr ("g_log tests:");
+ g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
+ g_message ("the next warning is a test:");
+ string = NULL;
+ g_print (string);
+
+ g_print ("checking endian macros (host is ");
+#if G_BYTE_ORDER == G_BIG_ENDIAN
+ g_print ("big endian)...");
+#else
+ g_print ("little endian)...");
+#endif
+ g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
+ g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
+#ifdef G_HAVE_GINT64
+ g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
+#endif
g_print ("ok\n");
+#ifdef G_OS_WIN32
+ g_print ("current locale: %s\n", g_win32_getlocale ());
+#endif
+
return 0;
}
+