From 0b74f03e70ea7cab8c0f9d58108a446e3cb16d1e Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Sun, 3 Jan 1999 03:42:59 +0000 Subject: [PATCH] Added 'make check' tests, based on testglib code. --- ChangeLog | 5 + ChangeLog.pre-2-0 | 5 + ChangeLog.pre-2-10 | 5 + ChangeLog.pre-2-12 | 5 + ChangeLog.pre-2-2 | 5 + ChangeLog.pre-2-4 | 5 + ChangeLog.pre-2-6 | 5 + ChangeLog.pre-2-8 | 5 + Makefile.am | 4 +- configure.in | 1 + glib/Makefile.am | 4 +- tests/.cvsignore | 24 +++ tests/Makefile.am | 22 +++ tests/alloca-test.c | 90 ++++++++++ tests/hash-test.c | 373 ++++++++++++++++++++++++++++++++++++++++++ tests/list-test.c | 227 +++++++++++++++++++++++++ tests/node-test.c | 283 ++++++++++++++++++++++++++++++++ tests/relation-test.c | 130 +++++++++++++++ tests/slist-test.c | 225 +++++++++++++++++++++++++ tests/string-test.c | 207 +++++++++++++++++++++++ 20 files changed, 1626 insertions(+), 4 deletions(-) create mode 100644 tests/.cvsignore create mode 100644 tests/Makefile.am create mode 100644 tests/alloca-test.c create mode 100644 tests/hash-test.c create mode 100644 tests/list-test.c create mode 100644 tests/node-test.c create mode 100644 tests/relation-test.c create mode 100644 tests/slist-test.c create mode 100644 tests/string-test.c diff --git a/ChangeLog b/ChangeLog index 8d8c2112..ab1044b5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Jan 2 22:42:25 EST 1999 Jeff Garzik + + * Makefile.am, configure.in, tests/*: + Added 'make check' tests, based on testglib code. + Sat Jan 2 19:52:45 EST 1999 Jeff Garzik * glib.h, testglib.c: diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 8d8c2112..ab1044b5 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,8 @@ +Sat Jan 2 22:42:25 EST 1999 Jeff Garzik + + * Makefile.am, configure.in, tests/*: + Added 'make check' tests, based on testglib code. + Sat Jan 2 19:52:45 EST 1999 Jeff Garzik * glib.h, testglib.c: diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 8d8c2112..ab1044b5 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,8 @@ +Sat Jan 2 22:42:25 EST 1999 Jeff Garzik + + * Makefile.am, configure.in, tests/*: + Added 'make check' tests, based on testglib code. + Sat Jan 2 19:52:45 EST 1999 Jeff Garzik * glib.h, testglib.c: diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 8d8c2112..ab1044b5 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,8 @@ +Sat Jan 2 22:42:25 EST 1999 Jeff Garzik + + * Makefile.am, configure.in, tests/*: + Added 'make check' tests, based on testglib code. + Sat Jan 2 19:52:45 EST 1999 Jeff Garzik * glib.h, testglib.c: diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 8d8c2112..ab1044b5 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,8 @@ +Sat Jan 2 22:42:25 EST 1999 Jeff Garzik + + * Makefile.am, configure.in, tests/*: + Added 'make check' tests, based on testglib code. + Sat Jan 2 19:52:45 EST 1999 Jeff Garzik * glib.h, testglib.c: diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 8d8c2112..ab1044b5 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,8 @@ +Sat Jan 2 22:42:25 EST 1999 Jeff Garzik + + * Makefile.am, configure.in, tests/*: + Added 'make check' tests, based on testglib code. + Sat Jan 2 19:52:45 EST 1999 Jeff Garzik * glib.h, testglib.c: diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 8d8c2112..ab1044b5 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,8 @@ +Sat Jan 2 22:42:25 EST 1999 Jeff Garzik + + * Makefile.am, configure.in, tests/*: + Added 'make check' tests, based on testglib code. + Sat Jan 2 19:52:45 EST 1999 Jeff Garzik * glib.h, testglib.c: diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 8d8c2112..ab1044b5 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,8 @@ +Sat Jan 2 22:42:25 EST 1999 Jeff Garzik + + * Makefile.am, configure.in, tests/*: + Added 'make check' tests, based on testglib code. + Sat Jan 2 19:52:45 EST 1999 Jeff Garzik * glib.h, testglib.c: diff --git a/Makefile.am b/Makefile.am index 5f24bb70..b36864ef 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,10 +1,10 @@ ## Process this file with automake to produce Makefile.in # build . first, then SUBDIRS -SUBDIRS = gmodule gthread docs +SUBDIRS = gmodule gthread docs tests all-recursive-am: all-am # alpha `automake' supports this better -#SUBDIRS = . gmodule gthread docs +#SUBDIRS = . gmodule gthread docs tests configincludedir = $(pkglibdir)/include diff --git a/configure.in b/configure.in index ea05878c..c606eb93 100644 --- a/configure.in +++ b/configure.in @@ -1193,6 +1193,7 @@ gmodule/gmoduleconf.h gmodule/Makefile gthread/Makefile docs/Makefile +tests/Makefile ],[case "$CONFIG_FILES" in *glib-config*)chmod +x glib-config;; esac]) diff --git a/glib/Makefile.am b/glib/Makefile.am index 5f24bb70..b36864ef 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -1,10 +1,10 @@ ## Process this file with automake to produce Makefile.in # build . first, then SUBDIRS -SUBDIRS = gmodule gthread docs +SUBDIRS = gmodule gthread docs tests all-recursive-am: all-am # alpha `automake' supports this better -#SUBDIRS = . gmodule gthread docs +#SUBDIRS = . gmodule gthread docs tests configincludedir = $(pkglibdir)/include diff --git a/tests/.cvsignore b/tests/.cvsignore new file mode 100644 index 00000000..f736b44c --- /dev/null +++ b/tests/.cvsignore @@ -0,0 +1,24 @@ +*.lo +Makefile +Makefile.in +configure +aclocal.m4 +.deps +_libs +libglib-1.1.la +config.log +glibconfig.h +libtool +config.status +stamp-h +.libs +testglib +config.cache +glib-config +libglib.la +config.h.in +config.h +stamp-gc-h +testgdate +testgdateparser +glibconfig-sysdefs.h diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 00000000..0b0d1b40 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,22 @@ + +INCLUDES = -I.. + +TESTS = \ + alloca-test \ + hash-test \ + list-test \ + node-test \ + relation-test \ + slist-test \ + string-test + +noinst_PROGRAMS = $(TESTS) + +alloca_test_LDADD = ../libglib.la +hash_test_LDADD = ../libglib.la +list_test_LDADD = ../libglib.la +node_test_LDADD = ../libglib.la +relation_test_LDADD = ../libglib.la +slist_test_LDADD = ../libglib.la +string_test_LDADD = ../libglib.la + diff --git a/tests/alloca-test.c b/tests/alloca-test.c new file mode 100644 index 00000000..0f019619 --- /dev/null +++ b/tests/alloca-test.c @@ -0,0 +1,90 @@ +/* GLIB - Library of useful routines for C programming + * 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 + * 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. + * + * You should have received a copy of the GNU Library 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. + */ +#undef G_LOG_DOMAIN + +#include +#include +#include "../glib.h" + +#define GLIB_TEST_STRING "el dorado " +#define GLIB_TEST_STRING_5 "el do" + +typedef struct { + guint age; + gchar name[40]; +} GlibTestInfo; + +int +main (int argc, + char *argv[]) +{ +#ifdef G_HAVE_ALLOCA + gchar *string; + GlibTestInfo *gti; + gint i, j; + + string = g_alloca(80); + g_assert(string != NULL); + for (i = 0; i < 80; i++) + string[i] = 'x'; + string[79] = 0; + g_assert(strlen(string) == 79); + + gti = g_new_a(GlibTestInfo, 2); + string = g_alloca(2); + strcpy(string, "x"); + for (i = 0; i < 2; i++) { + for (j = 0; j < 40; j++) + gti[i].name[j] = 'x'; + gti[i].name[39] = 0; + g_assert(strlen(gti[i].name) == 39); + gti[i].age = 42; + } + g_assert(strcmp(string, "x") == 0); + + string = g_new0_a(char, 40); + for (i = 0; i < 39; i++) + string[i] = 'x'; + g_assert(strlen(string) == 39); + + g_strdup_a(string, GLIB_TEST_STRING); + g_assert(string != NULL); + g_assert(strcmp(string, GLIB_TEST_STRING) == 0); + g_strdup_a(string, NULL); + g_assert(string == NULL); + + g_strndup_a(string, GLIB_TEST_STRING, 5); + g_assert(string != NULL); + g_assert(strlen(string) == 5); + g_assert(strcmp(string, GLIB_TEST_STRING_5) == 0); + g_strndup_a(string, NULL, 20); + g_assert(string == NULL); + + g_strconcat3_a(string, GLIB_TEST_STRING, GLIB_TEST_STRING, GLIB_TEST_STRING); + g_assert(string != NULL); + g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING + GLIB_TEST_STRING) == 0); + +#else + exit(77); /* tell automake test was skipped */ +#endif + + return 0; +} + diff --git a/tests/hash-test.c b/tests/hash-test.c new file mode 100644 index 00000000..bc4ddddd --- /dev/null +++ b/tests/hash-test.c @@ -0,0 +1,373 @@ +/* GLIB - Library of useful routines for C programming + * 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 + * 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. + * + * You should have received a copy of the GNU Library 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. + */ +#undef G_LOG_DOMAIN + +#include +#include +#include "glib.h" + +int array[10000]; +gboolean failed = FALSE; + +#define TEST(m,cond) G_STMT_START { failed = !(cond); \ +if (failed) \ + { if (!m) \ + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ + else \ + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ + } \ +else \ + g_print ("."); fflush (stdout); \ +} G_STMT_END + +#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" + +typedef struct { + guint age; + gchar name[40]; +} GlibTestInfo; + +static gboolean +node_build_string (GNode *node, + gpointer data) +{ + gchar **p = data; + gchar *string; + gchar c[2] = "_"; + + c[0] = P2C (node->data); + + string = g_strconcat (*p ? *p : "", c, NULL); + g_free (*p); + *p = string; + + return FALSE; +} + +static void +g_node_test (void) +{ + GNode *root; + GNode *node; + GNode *node_B; + GNode *node_F; + GNode *node_G; + GNode *node_J; + guint i; + gchar *tstring; + + g_print ("checking n-way trees: "); + failed = FALSE; + + root = g_node_new (C2P ('A')); + TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1); + + node_B = g_node_new (C2P ('B')); + g_node_append (root, node_B); + TEST (NULL, root->children == node_B); + + g_node_append_data (node_B, C2P ('E')); + g_node_prepend_data (node_B, C2P ('C')); + g_node_insert (node_B, 1, g_node_new (C2P ('D'))); + + node_F = g_node_new (C2P ('F')); + g_node_append (root, node_F); + TEST (NULL, root->children->next == node_F); + + node_G = g_node_new (C2P ('G')); + g_node_append (node_F, node_G); + node_J = g_node_new (C2P ('J')); + g_node_prepend (node_G, node_J); + g_node_insert (node_G, 42, g_node_new (C2P ('K'))); + g_node_insert_data (node_G, 0, C2P ('H')); + g_node_insert (node_G, 1, g_node_new (C2P ('I'))); + + TEST (NULL, g_node_depth (root) == 1); + TEST (NULL, g_node_max_height (root) == 4); + TEST (NULL, g_node_depth (node_G->children->next) == 4); + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7); + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4); + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11); + TEST (NULL, g_node_max_height (node_F) == 3); + TEST (NULL, g_node_n_children (node_G) == 4); + TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F); + TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL); + TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J); + + for (i = 0; i < g_node_n_children (node_B); i++) + { + node = g_node_nth_child (node_B, i); + TEST (NULL, P2C (node->data) == ('C' + i)); + } + + for (i = 0; i < g_node_n_children (node_G); i++) + TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i); + + /* we have built: A + * / \ + * B F + * / | \ \ + * C D E G + * / /\ \ + * H I J K + * + * for in-order traversal, 'G' is considered to be the "left" + * child of 'F', which will cause 'F' to be the last node visited. + */ + + tstring = NULL; + g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0); + g_free (tstring); tstring = NULL; + g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0); + g_free (tstring); tstring = NULL; + g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0); + g_free (tstring); tstring = NULL; + g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0); + g_free (tstring); tstring = NULL; + + g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "CDEHIJK") == 0); + g_free (tstring); tstring = NULL; + g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "ABFG") == 0); + g_free (tstring); tstring = NULL; + + g_node_reverse_children (node_B); + g_node_reverse_children (node_G); + + 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; + + g_node_destroy (root); + + /* allocation tests */ + + root = g_node_new (NULL); + node = root; + + for (i = 0; i < 2048; i++) + { + g_node_append (node, g_node_new (NULL)); + if ((i%5) == 4) + node = node->children->next; + } + TEST (NULL, g_node_max_height (root) > 100); + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048); + + g_node_destroy (root); + + if (!failed) + g_print ("ok\n"); +} + +gboolean +my_hash_callback_remove (gpointer key, + gpointer value, + gpointer user_data) +{ + int *d = value; + + if ((*d) % 2) + return TRUE; + + return FALSE; +} + +void +my_hash_callback_remove_test (gpointer key, + gpointer value, + gpointer user_data) +{ + int *d = value; + + if ((*d) % 2) + g_print ("bad!\n"); +} + +void +my_hash_callback (gpointer key, + gpointer value, + gpointer user_data) +{ + int *d = value; + *d = 1; +} + +guint +my_hash (gconstpointer key) +{ + return (guint) *((const gint*) key); +} + +gint +my_hash_compare (gconstpointer a, + gconstpointer b) +{ + return *((const gint*) a) == *((const gint*) b); +} + +gint +my_list_compare_one (gconstpointer a, gconstpointer b) +{ + gint one = *((const gint*)a); + gint two = *((const gint*)b); + return one-two; +} + +gint +my_list_compare_two (gconstpointer a, gconstpointer b) +{ + gint one = *((const gint*)a); + gint two = *((const gint*)b); + return two-one; +} + +/* void +my_list_print (gpointer a, gpointer b) +{ + gint three = *((gint*)a); + g_print("%d", three); +}; */ + +gint +my_compare (gconstpointer a, + gconstpointer b) +{ + const char *cha = a; + const char *chb = b; + + return *cha - *chb; +} + +gint +my_traverse (gpointer key, + gpointer value, + gpointer data) +{ + char *ch = key; + g_print ("%c ", *ch); + return FALSE; +} + +int +main (int argc, + char *argv[]) +{ + GList *list, *t; + GSList *slist, *st; + GHashTable *hash_table; + GMemChunk *mem_chunk; + GStringChunk *string_chunk; + GTimer *timer; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6}; + gchar *string; + + gchar *mem[10000], *tmp_string, *tmp_string_2; + gint i, j; + GArray *garray; + GPtrArray *gparray; + GByteArray *gbarray; + GString *string1, *string2; + GTree *tree; + char chars[62]; + GRelation *relation; + GTuples *tuples; + gint data [1024]; + GlibTestInfo *gti; + struct { + gchar *filename; + gchar *dirname; + } dirname_checks[] = { +#ifndef NATIVE_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 + + + hash_table = g_hash_table_new (my_hash, my_hash_compare); + for (i = 0; i < 10000; i++) + { + array[i] = i; + g_hash_table_insert (hash_table, &array[i], &array[i]); + } + g_hash_table_foreach (hash_table, my_hash_callback, NULL); + + for (i = 0; i < 10000; i++) + if (array[i] == 0) + g_assert_not_reached(); + + 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_assert_not_reached(); + + g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL); + + + g_hash_table_destroy (hash_table); + + return 0; + +} diff --git a/tests/list-test.c b/tests/list-test.c new file mode 100644 index 00000000..bb143819 --- /dev/null +++ b/tests/list-test.c @@ -0,0 +1,227 @@ +/* GLIB - Library of useful routines for C programming + * 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 + * 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. + * + * You should have received a copy of the GNU Library 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. + */ +#undef G_LOG_DOMAIN + +#include +#include +#include "glib.h" + +int array[10000]; +gboolean failed = FALSE; + +#define TEST(m,cond) G_STMT_START { failed = !(cond); \ +if (failed) \ + { if (!m) \ + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ + else \ + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ + } \ +else \ + g_print ("."); fflush (stdout); \ +} G_STMT_END + +#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" + +typedef struct { + guint age; + gchar name[40]; +} GlibTestInfo; + +gint +my_list_compare_one (gconstpointer a, gconstpointer b) +{ + gint one = *((const gint*)a); + gint two = *((const gint*)b); + return one-two; +} + +gint +my_list_compare_two (gconstpointer a, gconstpointer b) +{ + gint one = *((const gint*)a); + gint two = *((const gint*)b); + return two-one; +} + +/* void +my_list_print (gpointer a, gpointer b) +{ + gint three = *((gint*)a); + g_print("%d", three); +}; */ + +gint +my_compare (gconstpointer a, + gconstpointer b) +{ + const char *cha = a; + const char *chb = b; + + return *cha - *chb; +} + +gint +my_traverse (gpointer key, + gpointer value, + gpointer data) +{ + char *ch = key; + g_print ("%c ", *ch); + return FALSE; +} + +int +main (int argc, + char *argv[]) +{ + GList *list, *t; + GSList *slist, *st; + GHashTable *hash_table; + GMemChunk *mem_chunk; + GStringChunk *string_chunk; + GTimer *timer; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6}; + gchar *string; + + gchar *mem[10000], *tmp_string, *tmp_string_2; + gint i, j; + GArray *garray; + GPtrArray *gparray; + GByteArray *gbarray; + GString *string1, *string2; + GTree *tree; + char chars[62]; + GRelation *relation; + GTuples *tuples; + gint data [1024]; + GlibTestInfo *gti; + struct { + gchar *filename; + gchar *dirname; + } dirname_checks[] = { +#ifndef NATIVE_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 + + list = NULL; + for (i = 0; i < 10; i++) + list = g_list_append (list, &nums[i]); + list = g_list_reverse (list); + + for (i = 0; i < 10; i++) + { + t = g_list_nth (list, i); + g_assert (*((gint*) t->data) == (9 - i)); + } + + for (i = 0; i < 10; i++) + g_assert (g_list_position(list, g_list_nth (list, i)) == i); + + g_list_free (list); + list = NULL; + + for (i = 0; i < 10; i++) + list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one); + + /* + g_print("\n"); + g_list_foreach (list, my_list_print, NULL); + */ + + for (i = 0; i < 10; i++) + { + t = g_list_nth (list, i); + g_assert (*((gint*) t->data) == i); + } + + g_list_free (list); + list = NULL; + + for (i = 0; i < 10; i++) + list = g_list_insert_sorted (list, &morenums[i], 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); + g_assert (*((gint*) t->data) == (9 - i)); + } + + 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); + g_assert (*((gint*) t->data) == (9 - i)); + } + + g_list_free (list); + + return 0; +} + diff --git a/tests/node-test.c b/tests/node-test.c new file mode 100644 index 00000000..efd7b766 --- /dev/null +++ b/tests/node-test.c @@ -0,0 +1,283 @@ +/* GLIB - Library of useful routines for C programming + * 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 + * 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. + * + * You should have received a copy of the GNU Library 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. + */ +#undef G_LOG_DOMAIN + +#include +#include +#include "glib.h" + +int array[10000]; +gboolean failed = FALSE; + +#define TEST(m,cond) G_STMT_START { failed = !(cond); \ +if (failed) \ + { if (!m) \ + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ + else \ + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ + exit(1); \ + } \ +} G_STMT_END + +#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" + +typedef struct { + guint age; + gchar name[40]; +} GlibTestInfo; + +static gboolean +node_build_string (GNode *node, + gpointer data) +{ + gchar **p = data; + gchar *string; + gchar c[2] = "_"; + + c[0] = P2C (node->data); + + string = g_strconcat (*p ? *p : "", c, NULL); + g_free (*p); + *p = string; + + return FALSE; +} + +static void +g_node_test (void) +{ + GNode *root; + GNode *node; + GNode *node_B; + GNode *node_F; + GNode *node_G; + GNode *node_J; + guint i; + gchar *tstring; + + failed = FALSE; + + root = g_node_new (C2P ('A')); + TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1); + + node_B = g_node_new (C2P ('B')); + g_node_append (root, node_B); + TEST (NULL, root->children == node_B); + + g_node_append_data (node_B, C2P ('E')); + g_node_prepend_data (node_B, C2P ('C')); + g_node_insert (node_B, 1, g_node_new (C2P ('D'))); + + node_F = g_node_new (C2P ('F')); + g_node_append (root, node_F); + TEST (NULL, root->children->next == node_F); + + node_G = g_node_new (C2P ('G')); + g_node_append (node_F, node_G); + node_J = g_node_new (C2P ('J')); + g_node_prepend (node_G, node_J); + g_node_insert (node_G, 42, g_node_new (C2P ('K'))); + g_node_insert_data (node_G, 0, C2P ('H')); + g_node_insert (node_G, 1, g_node_new (C2P ('I'))); + + TEST (NULL, g_node_depth (root) == 1); + TEST (NULL, g_node_max_height (root) == 4); + TEST (NULL, g_node_depth (node_G->children->next) == 4); + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7); + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4); + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11); + TEST (NULL, g_node_max_height (node_F) == 3); + TEST (NULL, g_node_n_children (node_G) == 4); + TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F); + TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL); + TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J); + + for (i = 0; i < g_node_n_children (node_B); i++) + { + node = g_node_nth_child (node_B, i); + TEST (NULL, P2C (node->data) == ('C' + i)); + } + + for (i = 0; i < g_node_n_children (node_G); i++) + TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i); + + /* we have built: A + * / \ + * B F + * / | \ \ + * C D E G + * / /\ \ + * H I J K + * + * for in-order traversal, 'G' is considered to be the "left" + * child of 'F', which will cause 'F' to be the last node visited. + */ + + tstring = NULL; + g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0); + g_free (tstring); tstring = NULL; + g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0); + g_free (tstring); tstring = NULL; + g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0); + g_free (tstring); tstring = NULL; + g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0); + g_free (tstring); tstring = NULL; + + g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "CDEHIJK") == 0); + g_free (tstring); tstring = NULL; + g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring); + TEST (tstring, strcmp (tstring, "ABFG") == 0); + g_free (tstring); tstring = NULL; + + g_node_reverse_children (node_B); + g_node_reverse_children (node_G); + + 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; + + g_node_destroy (root); + + /* allocation tests */ + + root = g_node_new (NULL); + node = root; + + for (i = 0; i < 2048; i++) + { + g_node_append (node, g_node_new (NULL)); + if ((i%5) == 4) + node = node->children->next; + } + TEST (NULL, g_node_max_height (root) > 100); + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048); + + g_node_destroy (root); + + if (failed) + exit(1); +} + +gboolean +my_hash_callback_remove (gpointer key, + gpointer value, + gpointer user_data) +{ + int *d = value; + + if ((*d) % 2) + return TRUE; + + return FALSE; +} + +void +my_hash_callback_remove_test (gpointer key, + gpointer value, + gpointer user_data) +{ + int *d = value; + + if ((*d) % 2) + g_print ("bad!\n"); +} + +void +my_hash_callback (gpointer key, + gpointer value, + gpointer user_data) +{ + int *d = value; + *d = 1; +} + +guint +my_hash (gconstpointer key) +{ + return (guint) *((const gint*) key); +} + +gint +my_hash_compare (gconstpointer a, + gconstpointer b) +{ + return *((const gint*) a) == *((const gint*) b); +} + +gint +my_list_compare_one (gconstpointer a, gconstpointer b) +{ + gint one = *((const gint*)a); + gint two = *((const gint*)b); + return one-two; +} + +gint +my_list_compare_two (gconstpointer a, gconstpointer b) +{ + gint one = *((const gint*)a); + gint two = *((const gint*)b); + return two-one; +} + +/* void +my_list_print (gpointer a, gpointer b) +{ + gint three = *((gint*)a); + g_print("%d", three); +}; */ + +gint +my_compare (gconstpointer a, + gconstpointer b) +{ + const char *cha = a; + const char *chb = b; + + return *cha - *chb; +} + +gint +my_traverse (gpointer key, + gpointer value, + gpointer data) +{ + char *ch = key; + g_print ("%c ", *ch); + return FALSE; +} + +int +main (int argc, + char *argv[]) +{ + g_node_test (); + + return 0; +} + diff --git a/tests/relation-test.c b/tests/relation-test.c new file mode 100644 index 00000000..a3d37a2c --- /dev/null +++ b/tests/relation-test.c @@ -0,0 +1,130 @@ +/* GLIB - Library of useful routines for C programming + * 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 + * 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. + * + * You should have received a copy of the GNU Library 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. + */ +#undef G_LOG_DOMAIN + +#include +#include +#include "glib.h" + +int array[10000]; +gboolean failed = FALSE; + +#define TEST(m,cond) G_STMT_START { failed = !(cond); \ +if (failed) \ + { if (!m) \ + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ + else \ + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ + } \ +else \ + g_print ("."); fflush (stdout); \ +} G_STMT_END + +#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" + +typedef struct { + guint age; + gchar name[40]; +} GlibTestInfo; + + + +int +main (int argc, + char *argv[]) +{ + gint i; + GRelation *relation; + GTuples *tuples; + gint data [1024]; + + + relation = g_relation_new (2); + + g_relation_index (relation, 0, g_int_hash, g_int_equal); + g_relation_index (relation, 1, g_int_hash, g_int_equal); + + for (i = 0; i < 1024; i += 1) + data[i] = i; + + for (i = 1; i < 1023; i += 1) + { + g_relation_insert (relation, data + i, data + i + 1); + g_relation_insert (relation, data + i, data + i - 1); + } + + for (i = 2; i < 1022; i += 1) + { + g_assert (! g_relation_exists (relation, data + i, data + i)); + g_assert (! g_relation_exists (relation, data + i, data + i + 2)); + g_assert (! g_relation_exists (relation, data + i, data + i - 2)); + } + + for (i = 1; i < 1023; i += 1) + { + g_assert (g_relation_exists (relation, data + i, data + i + 1)); + g_assert (g_relation_exists (relation, data + i, data + i - 1)); + } + + for (i = 2; i < 1022; i += 1) + { + g_assert (g_relation_count (relation, data + i, 0) == 2); + g_assert (g_relation_count (relation, data + i, 1) == 2); + } + + g_assert (g_relation_count (relation, data, 0) == 0); + + g_assert (g_relation_count (relation, data + 42, 0) == 2); + g_assert (g_relation_count (relation, data + 43, 1) == 2); + g_assert (g_relation_count (relation, data + 41, 1) == 2); + g_relation_delete (relation, data + 42, 0); + g_assert (g_relation_count (relation, data + 42, 0) == 0); + g_assert (g_relation_count (relation, data + 43, 1) == 1); + g_assert (g_relation_count (relation, data + 41, 1) == 1); + + tuples = g_relation_select (relation, data + 200, 0); + + g_assert (tuples->len == 2); + +#if 0 + for (i = 0; i < tuples->len; i += 1) + { + printf ("%d %d\n", + *(gint*) g_tuples_index (tuples, i, 0), + *(gint*) g_tuples_index (tuples, i, 1)); + } +#endif + + g_assert (g_relation_exists (relation, data + 300, data + 301)); + g_relation_delete (relation, data + 300, 0); + g_assert (!g_relation_exists (relation, data + 300, data + 301)); + + g_tuples_destroy (tuples); + + g_relation_destroy (relation); + + relation = NULL; + + return 0; +} + diff --git a/tests/slist-test.c b/tests/slist-test.c new file mode 100644 index 00000000..ab314033 --- /dev/null +++ b/tests/slist-test.c @@ -0,0 +1,225 @@ +/* GLIB - Library of useful routines for C programming + * 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 + * 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. + * + * You should have received a copy of the GNU Library 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. + */ +#undef G_LOG_DOMAIN + +#include +#include +#include "glib.h" + +int array[10000]; +gboolean failed = FALSE; + +#define TEST(m,cond) G_STMT_START { failed = !(cond); \ +if (failed) \ + { if (!m) \ + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ + else \ + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ + } \ +else \ + g_print ("."); fflush (stdout); \ +} G_STMT_END + +#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" + +typedef struct { + guint age; + gchar name[40]; +} GlibTestInfo; + +gint +my_list_compare_one (gconstpointer a, gconstpointer b) +{ + gint one = *((const gint*)a); + gint two = *((const gint*)b); + return one-two; +} + +gint +my_list_compare_two (gconstpointer a, gconstpointer b) +{ + gint one = *((const gint*)a); + gint two = *((const gint*)b); + return two-one; +} + +/* void +my_list_print (gpointer a, gpointer b) +{ + gint three = *((gint*)a); + g_print("%d", three); +}; */ + +gint +my_compare (gconstpointer a, + gconstpointer b) +{ + const char *cha = a; + const char *chb = b; + + return *cha - *chb; +} + +gint +my_traverse (gpointer key, + gpointer value, + gpointer data) +{ + char *ch = key; + g_print ("%c ", *ch); + return FALSE; +} + +int +main (int argc, + char *argv[]) +{ + GList *list, *t; + GSList *slist, *st; + GHashTable *hash_table; + GMemChunk *mem_chunk; + GStringChunk *string_chunk; + GTimer *timer; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6}; + gchar *string; + + gchar *mem[10000], *tmp_string, *tmp_string_2; + gint i, j; + GArray *garray; + GPtrArray *gparray; + GByteArray *gbarray; + GString *string1, *string2; + GTree *tree; + char chars[62]; + GRelation *relation; + GTuples *tuples; + gint data [1024]; + GlibTestInfo *gti; + struct { + gchar *filename; + gchar *dirname; + } dirname_checks[] = { +#ifndef NATIVE_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 + + + + slist = NULL; + for (i = 0; i < 10; i++) + slist = g_slist_append (slist, &nums[i]); + slist = g_slist_reverse (slist); + + for (i = 0; i < 10; i++) + { + st = g_slist_nth (slist, i); + g_assert (*((gint*) st->data) == (9 - i)); + } + + g_slist_free (slist); + slist = NULL; + + for (i = 0; i < 10; i++) + slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one); + + /* + g_print("\n"); + g_slist_foreach (slist, my_list_print, NULL); + */ + + for (i = 0; i < 10; i++) + { + st = g_slist_nth (slist, i); + g_assert (*((gint*) st->data) == i); + } + + g_slist_free(slist); + slist = NULL; + + for (i = 0; i < 10; i++) + slist = g_slist_insert_sorted (slist, &morenums[i], 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); + g_assert (*((gint*) st->data) == (9 - i)); + } + + 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); + g_assert (*((gint*) st->data) == (9 - i)); + } + + g_slist_free(slist); + + return 0; +} diff --git a/tests/string-test.c b/tests/string-test.c new file mode 100644 index 00000000..1d86a36f --- /dev/null +++ b/tests/string-test.c @@ -0,0 +1,207 @@ +/* GLIB - Library of useful routines for C programming + * 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 + * 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. + * + * You should have received a copy of the GNU Library 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. + */ +#undef G_LOG_DOMAIN + +#include +#include +#include "glib.h" + +int array[10000]; +gboolean failed = FALSE; + +#define TEST(m,cond) G_STMT_START { failed = !(cond); \ +if (failed) \ + { if (!m) \ + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ + else \ + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ + } \ +else \ + g_print ("."); fflush (stdout); \ +} G_STMT_END + +#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" + +typedef struct { + guint age; + gchar name[40]; +} GlibTestInfo; + +int +main (int argc, + char *argv[]) +{ + GList *list, *t; + GSList *slist, *st; + GHashTable *hash_table; + GMemChunk *mem_chunk; + GStringChunk *string_chunk; + GTimer *timer; + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6}; + gchar *string; + + gchar *mem[10000], *tmp_string, *tmp_string_2; + gint i, j; + GArray *garray; + GPtrArray *gparray; + GByteArray *gbarray; + GString *string1, *string2; + GTree *tree; + char chars[62]; + GRelation *relation; + GTuples *tuples; + gint data [1024]; + GlibTestInfo *gti; + struct { + gchar *filename; + gchar *dirname; + } dirname_checks[] = { +#ifndef NATIVE_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 + + + string_chunk = g_string_chunk_new (1024); + + for (i = 0; i < 100000; i ++) + { + tmp_string = g_string_chunk_insert (string_chunk, "hi pete"); + + if (strcmp ("hi pete", tmp_string) != 0) + g_error ("string chunks are broken.\n"); + } + + tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string); + + g_assert (tmp_string_2 != tmp_string && + strcmp(tmp_string_2, tmp_string) == 0); + + tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string); + + g_assert (tmp_string_2 == tmp_string); + + g_string_chunk_free (string_chunk); + + string1 = g_string_new ("hi pete!"); + string2 = g_string_new (""); + + g_assert (string1 != NULL); + g_assert (string2 != NULL); + g_assert (strcmp ("hi pete!", string1->str) == 0); + g_assert (strcmp ("", string2->str) == 0); + + for (i = 0; i < 10000; i++) + g_string_append_c (string1, 'a'+(i%26)); + + g_assert((strlen("hi pete!") + 10000) == string1->len); + g_assert((strlen("hi pete!") + 10000) == strlen(string1->str)); + +#if !(defined (_MSC_VER) || defined (__LCC__)) + /* MSVC 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_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_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); + + 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); + + string = g_strdup_printf ("%05d %-5s", 21, "test"); + g_assert (string != NULL); + g_assert (strcmp(string, "00021 test ") == 0); + g_free (string); + + return 0; +} + -- 2.34.1