From: Jeff Garzik Date: Tue, 9 Mar 1999 19:41:19 +0000 (+0000) Subject: Added stack, queue ADTs and related tests. X-Git-Url: http://git.openbox.org/?a=commitdiff_plain;h=fd7ba69e32a926b0bb5b769a11893bbbd2440e89;p=dana%2Fcg-glib.git Added stack, queue ADTs and related tests. Tue Mar 9 14:37:32 1999 Jeff Garzik * Makefile.am, glib.h, gstack.c, gqueue.c, tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: Added stack, queue ADTs and related tests. * glib.h, glist.c: New g_list_delete() function. --- diff --git a/ChangeLog b/ChangeLog index f9b8b066..c48c7b2c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,13 @@ -Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen) +Tue Mar 9 14:37:32 1999 Jeff Garzik + + * Makefile.am, glib.h, gstack.c, gqueue.c, + tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: + Added stack, queue ADTs and related tests. + + * glib.h, glist.c: + New g_list_delete() function. + +Sat Mar 6 11:03:08 1999 Asbjorn Pettersen * gutils.c (g_get_any_init): add OS/2 changes. change '\\' in HOME to '/'. diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index f9b8b066..c48c7b2c 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,4 +1,13 @@ -Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen) +Tue Mar 9 14:37:32 1999 Jeff Garzik + + * Makefile.am, glib.h, gstack.c, gqueue.c, + tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: + Added stack, queue ADTs and related tests. + + * glib.h, glist.c: + New g_list_delete() function. + +Sat Mar 6 11:03:08 1999 Asbjorn Pettersen * gutils.c (g_get_any_init): add OS/2 changes. change '\\' in HOME to '/'. diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index f9b8b066..c48c7b2c 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,4 +1,13 @@ -Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen) +Tue Mar 9 14:37:32 1999 Jeff Garzik + + * Makefile.am, glib.h, gstack.c, gqueue.c, + tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: + Added stack, queue ADTs and related tests. + + * glib.h, glist.c: + New g_list_delete() function. + +Sat Mar 6 11:03:08 1999 Asbjorn Pettersen * gutils.c (g_get_any_init): add OS/2 changes. change '\\' in HOME to '/'. diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index f9b8b066..c48c7b2c 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,4 +1,13 @@ -Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen) +Tue Mar 9 14:37:32 1999 Jeff Garzik + + * Makefile.am, glib.h, gstack.c, gqueue.c, + tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: + Added stack, queue ADTs and related tests. + + * glib.h, glist.c: + New g_list_delete() function. + +Sat Mar 6 11:03:08 1999 Asbjorn Pettersen * gutils.c (g_get_any_init): add OS/2 changes. change '\\' in HOME to '/'. diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index f9b8b066..c48c7b2c 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,4 +1,13 @@ -Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen) +Tue Mar 9 14:37:32 1999 Jeff Garzik + + * Makefile.am, glib.h, gstack.c, gqueue.c, + tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: + Added stack, queue ADTs and related tests. + + * glib.h, glist.c: + New g_list_delete() function. + +Sat Mar 6 11:03:08 1999 Asbjorn Pettersen * gutils.c (g_get_any_init): add OS/2 changes. change '\\' in HOME to '/'. diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index f9b8b066..c48c7b2c 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,4 +1,13 @@ -Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen) +Tue Mar 9 14:37:32 1999 Jeff Garzik + + * Makefile.am, glib.h, gstack.c, gqueue.c, + tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: + Added stack, queue ADTs and related tests. + + * glib.h, glist.c: + New g_list_delete() function. + +Sat Mar 6 11:03:08 1999 Asbjorn Pettersen * gutils.c (g_get_any_init): add OS/2 changes. change '\\' in HOME to '/'. diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index f9b8b066..c48c7b2c 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,4 +1,13 @@ -Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen) +Tue Mar 9 14:37:32 1999 Jeff Garzik + + * Makefile.am, glib.h, gstack.c, gqueue.c, + tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: + Added stack, queue ADTs and related tests. + + * glib.h, glist.c: + New g_list_delete() function. + +Sat Mar 6 11:03:08 1999 Asbjorn Pettersen * gutils.c (g_get_any_init): add OS/2 changes. change '\\' in HOME to '/'. diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index f9b8b066..c48c7b2c 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,4 +1,13 @@ -Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen) +Tue Mar 9 14:37:32 1999 Jeff Garzik + + * Makefile.am, glib.h, gstack.c, gqueue.c, + tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: + Added stack, queue ADTs and related tests. + + * glib.h, glist.c: + New g_list_delete() function. + +Sat Mar 6 11:03:08 1999 Asbjorn Pettersen * gutils.c (g_get_any_init): add OS/2 changes. change '\\' in HOME to '/'. diff --git a/Makefile.am b/Makefile.am index faba5637..80d68170 100644 --- a/Makefile.am +++ b/Makefile.am @@ -44,9 +44,11 @@ libglib_la_SOURCES = \ gmutex.c \ gnode.c \ gprimes.c \ + gqueue.c \ grel.c \ gscanner.c \ gslist.c \ + gstack.c \ gstrfuncs.c \ gstring.c \ gtimer.c \ @@ -99,4 +101,4 @@ snapshot: $(MAKE) dist distdir=$(PACKAGE)`date +"%y%m%d"` dist-hook: - cp glib.spec $(distdir) \ No newline at end of file + cp glib.spec $(distdir) diff --git a/glib.h b/glib.h index eeb443e0..82893e2c 100644 --- a/glib.h +++ b/glib.h @@ -685,10 +685,12 @@ typedef struct _GList GList; typedef struct _GMemChunk GMemChunk; typedef struct _GNode GNode; typedef struct _GPtrArray GPtrArray; +typedef struct _GQueue GQueue; typedef struct _GRelation GRelation; typedef struct _GScanner GScanner; typedef struct _GScannerConfig GScannerConfig; typedef struct _GSList GSList; +typedef struct _GStack GStack; typedef struct _GString GString; typedef struct _GStringChunk GStringChunk; typedef struct _GTimer GTimer; @@ -805,6 +807,18 @@ struct _GSList GSList *next; }; +struct _GStack +{ + GList *list; +}; + +struct _GQueue +{ + GList *list; + GList *list_end; + guint list_size; +}; + struct _GString { gchar *str; @@ -860,6 +874,8 @@ GList* g_list_insert_sorted (GList *list, GCompareFunc func); GList* g_list_concat (GList *list1, GList *list2); +GList* g_list_delete (GList *list, + GList *link); GList* g_list_remove (GList *list, gpointer data); GList* g_list_remove_link (GList *list, @@ -939,6 +955,66 @@ gpointer g_slist_nth_data (GSList *list, #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL) +/* Stacks + */ + +GStack * g_stack_new (void); +void g_stack_free (GStack *stack); +gpointer g_stack_pop (GStack *stack); + +#define g_stack_empty(stack) \ + ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? FALSE : TRUE) + +#define g_stack_peek(stack) \ + ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \ + ((GStack *)(stack))->list->data : NULL) + +#define g_stack_index(stack,ptr) \ + ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \ + g_list_index (((GStack *)(stack))->list, (ptr)) : -1) + +#define g_stack_push(stack,data) G_STMT_START { \ + if ((GStack *)(stack)) \ + ((GStack *)(stack))->list = \ + g_list_prepend (((GStack *)(stack))->list, (data)); \ + } G_STMT_END + + + +/* Queues + */ + +GQueue * g_queue_new (void); +void g_queue_free (GQueue *q); +guint g_queue_get_size (GQueue *q); +void g_queue_push_front (GQueue *q, gpointer data); +void g_queue_push_back (GQueue *q, gpointer data); +gpointer g_queue_pop_front (GQueue *q); +gpointer g_queue_pop_back (GQueue *q); + +#define g_queue_empty(queue) \ + ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? FALSE : TRUE) + +#define g_queue_peek_front(queue) \ + ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \ + ((GQueue *)(queue))->list->data : NULL) + +#define g_queue_peek_back(queue) \ + ((((GQueue *)(queue)) && ((GQueue *)(queue))->list_end) ? \ + ((GQueue *)(queue))->list_end->data : NULL) + +#define g_queue_index(queue,ptr) \ + ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \ + g_list_index (((GQueue *)(queue))->list, (ptr)) : -1) + +#define g_queue_push g_queue_push_back +#define g_queue_pop g_queue_pop_front +#define g_queue_peek g_queue_peek_front + + + + + /* Hash tables */ GHashTable* g_hash_table_new (GHashFunc hash_func, diff --git a/glib/Makefile.am b/glib/Makefile.am index faba5637..80d68170 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -44,9 +44,11 @@ libglib_la_SOURCES = \ gmutex.c \ gnode.c \ gprimes.c \ + gqueue.c \ grel.c \ gscanner.c \ gslist.c \ + gstack.c \ gstrfuncs.c \ gstring.c \ gtimer.c \ @@ -99,4 +101,4 @@ snapshot: $(MAKE) dist distdir=$(PACKAGE)`date +"%y%m%d"` dist-hook: - cp glib.spec $(distdir) \ No newline at end of file + cp glib.spec $(distdir) diff --git a/glib/glib.h b/glib/glib.h index eeb443e0..82893e2c 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -685,10 +685,12 @@ typedef struct _GList GList; typedef struct _GMemChunk GMemChunk; typedef struct _GNode GNode; typedef struct _GPtrArray GPtrArray; +typedef struct _GQueue GQueue; typedef struct _GRelation GRelation; typedef struct _GScanner GScanner; typedef struct _GScannerConfig GScannerConfig; typedef struct _GSList GSList; +typedef struct _GStack GStack; typedef struct _GString GString; typedef struct _GStringChunk GStringChunk; typedef struct _GTimer GTimer; @@ -805,6 +807,18 @@ struct _GSList GSList *next; }; +struct _GStack +{ + GList *list; +}; + +struct _GQueue +{ + GList *list; + GList *list_end; + guint list_size; +}; + struct _GString { gchar *str; @@ -860,6 +874,8 @@ GList* g_list_insert_sorted (GList *list, GCompareFunc func); GList* g_list_concat (GList *list1, GList *list2); +GList* g_list_delete (GList *list, + GList *link); GList* g_list_remove (GList *list, gpointer data); GList* g_list_remove_link (GList *list, @@ -939,6 +955,66 @@ gpointer g_slist_nth_data (GSList *list, #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL) +/* Stacks + */ + +GStack * g_stack_new (void); +void g_stack_free (GStack *stack); +gpointer g_stack_pop (GStack *stack); + +#define g_stack_empty(stack) \ + ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? FALSE : TRUE) + +#define g_stack_peek(stack) \ + ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \ + ((GStack *)(stack))->list->data : NULL) + +#define g_stack_index(stack,ptr) \ + ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \ + g_list_index (((GStack *)(stack))->list, (ptr)) : -1) + +#define g_stack_push(stack,data) G_STMT_START { \ + if ((GStack *)(stack)) \ + ((GStack *)(stack))->list = \ + g_list_prepend (((GStack *)(stack))->list, (data)); \ + } G_STMT_END + + + +/* Queues + */ + +GQueue * g_queue_new (void); +void g_queue_free (GQueue *q); +guint g_queue_get_size (GQueue *q); +void g_queue_push_front (GQueue *q, gpointer data); +void g_queue_push_back (GQueue *q, gpointer data); +gpointer g_queue_pop_front (GQueue *q); +gpointer g_queue_pop_back (GQueue *q); + +#define g_queue_empty(queue) \ + ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? FALSE : TRUE) + +#define g_queue_peek_front(queue) \ + ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \ + ((GQueue *)(queue))->list->data : NULL) + +#define g_queue_peek_back(queue) \ + ((((GQueue *)(queue)) && ((GQueue *)(queue))->list_end) ? \ + ((GQueue *)(queue))->list_end->data : NULL) + +#define g_queue_index(queue,ptr) \ + ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \ + g_list_index (((GQueue *)(queue))->list, (ptr)) : -1) + +#define g_queue_push g_queue_push_back +#define g_queue_pop g_queue_pop_front +#define g_queue_peek g_queue_peek_front + + + + + /* Hash tables */ GHashTable* g_hash_table_new (GHashFunc hash_func, diff --git a/glib/glist.c b/glib/glist.c index ca37b8da..3c87a8f1 100644 --- a/glib/glist.c +++ b/glib/glist.c @@ -314,6 +314,17 @@ g_list_remove_link (GList *list, return list; } + +GList* +g_list_delete (GList *list, GList *link) +{ + list = g_list_remove_link (list, link); + g_list_free_1 (link); + + return list; +} + + GList* g_list_copy (GList *list) { diff --git a/glib/gqueue.c b/glib/gqueue.c new file mode 100644 index 00000000..85e9b54b --- /dev/null +++ b/glib/gqueue.c @@ -0,0 +1,144 @@ +/* +* GQueue, opaque ADT with interface: + + q = g_queue_new (); + count = g_queue_size (q); + + q_queue_push_front (q, data); + q_queue_push_back (q, data); + data = q_queue_pop_front (q); + data = q_queue_pop_back (q); + #define q_queue_push q_queue_push_back + #define q_queue_pop q_queue_pop_front + + list = g_queue_get_list (q); + #define g_queue_get_front g_queue_get_list + list_end = g_queue_get_back (q); +*/ + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + + + + +GQueue * +g_queue_new (void) +{ + GQueue *q = g_new (GQueue, 1); + + q->list = q->list_end = NULL; + q->list_size = 0; + + return q; +} + + +void +g_queue_free (GQueue *q) +{ + if (q) { + if (q->list) + g_list_free (q->list); + g_free (q); + } +} + + +guint +g_queue_get_size (GQueue *q) +{ + return (q == NULL) ? 0 : q->list_size; +} + + +void +g_queue_push_front (GQueue *q, gpointer data) +{ + if (q) { + q->list = g_list_prepend (q->list, data); + + if (q->list_end == NULL) + q->list_end = q->list; + + q->list_size++; + } +} + + +void +g_queue_push_back (GQueue *q, gpointer data) +{ + if (q) { + q->list_end = g_list_append (q->list_end, data); + + if (! q->list) + q->list = q->list_end; + else + q->list_end = q->list_end->next; + + q->list_size++; + } +} + + +gpointer +g_queue_pop_front (GQueue *q) +{ + gpointer data = NULL; + + if ((q) && (q->list)) { + GList *node; + + node = q->list; + data = node->data; + + if (! node->next) { + q->list = q->list_end = NULL; + q->list_size = 0; + } + else { + q->list = node->next; + q->list->prev = NULL; + q->list_size--; + } + + g_list_free_1 (node); + } + + return data; +} + + +gpointer +g_queue_pop_back (GQueue *q) +{ + gpointer data = NULL; + + if ((q) && (q->list)) { + GList *node; + + node = q->list_end; + data = node->data; + + if (! node->prev) { + q->list = q->list_end = NULL; + q->list_size = 0; + } + else { + q->list_end = node->prev; + q->list_end->next = NULL; + q->list_size--; + } + + g_list_free_1 (node); + } + + return data; +} + + diff --git a/glist.c b/glist.c index ca37b8da..3c87a8f1 100644 --- a/glist.c +++ b/glist.c @@ -314,6 +314,17 @@ g_list_remove_link (GList *list, return list; } + +GList* +g_list_delete (GList *list, GList *link) +{ + list = g_list_remove_link (list, link); + g_list_free_1 (link); + + return list; +} + + GList* g_list_copy (GList *list) { diff --git a/gqueue.c b/gqueue.c new file mode 100644 index 00000000..85e9b54b --- /dev/null +++ b/gqueue.c @@ -0,0 +1,144 @@ +/* +* GQueue, opaque ADT with interface: + + q = g_queue_new (); + count = g_queue_size (q); + + q_queue_push_front (q, data); + q_queue_push_back (q, data); + data = q_queue_pop_front (q); + data = q_queue_pop_back (q); + #define q_queue_push q_queue_push_back + #define q_queue_pop q_queue_pop_front + + list = g_queue_get_list (q); + #define g_queue_get_front g_queue_get_list + list_end = g_queue_get_back (q); +*/ + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + + + + +GQueue * +g_queue_new (void) +{ + GQueue *q = g_new (GQueue, 1); + + q->list = q->list_end = NULL; + q->list_size = 0; + + return q; +} + + +void +g_queue_free (GQueue *q) +{ + if (q) { + if (q->list) + g_list_free (q->list); + g_free (q); + } +} + + +guint +g_queue_get_size (GQueue *q) +{ + return (q == NULL) ? 0 : q->list_size; +} + + +void +g_queue_push_front (GQueue *q, gpointer data) +{ + if (q) { + q->list = g_list_prepend (q->list, data); + + if (q->list_end == NULL) + q->list_end = q->list; + + q->list_size++; + } +} + + +void +g_queue_push_back (GQueue *q, gpointer data) +{ + if (q) { + q->list_end = g_list_append (q->list_end, data); + + if (! q->list) + q->list = q->list_end; + else + q->list_end = q->list_end->next; + + q->list_size++; + } +} + + +gpointer +g_queue_pop_front (GQueue *q) +{ + gpointer data = NULL; + + if ((q) && (q->list)) { + GList *node; + + node = q->list; + data = node->data; + + if (! node->next) { + q->list = q->list_end = NULL; + q->list_size = 0; + } + else { + q->list = node->next; + q->list->prev = NULL; + q->list_size--; + } + + g_list_free_1 (node); + } + + return data; +} + + +gpointer +g_queue_pop_back (GQueue *q) +{ + gpointer data = NULL; + + if ((q) && (q->list)) { + GList *node; + + node = q->list_end; + data = node->data; + + if (! node->prev) { + q->list = q->list_end = NULL; + q->list_size = 0; + } + else { + q->list_end = node->prev; + q->list_end->next = NULL; + q->list_size--; + } + + g_list_free_1 (node); + } + + return data; +} + + diff --git a/gstack.c b/gstack.c new file mode 100644 index 00000000..ff748ec6 --- /dev/null +++ b/gstack.c @@ -0,0 +1,63 @@ +#ifdef HAVE_CONFIG_H +# include +#endif +#include + +/* +* GStack, opaque ADT with interface: + + stack = g_stack_new (); + count = g_stack_size (stack); + + g_stack_push (stack, data); + data = g_stack_pop (stack); + + slist = g_stack_get_list (stack); +*/ + +GStack * +g_stack_new (void) +{ + GStack *s; + + s = g_new (GStack, 1); + if (!s) + return NULL; + + s->list = NULL; + + return s; +} + + +void +g_stack_free (GStack *stack) +{ + if (stack) { + if (stack->list) + g_list_free (stack->list); + + g_free (stack); + } +} + + +gpointer +g_stack_pop (GStack *stack) +{ + gpointer data = NULL; + + if ((stack) && (stack->list)) { + GList *node = stack->list; + + stack->list = stack->list->next; + + data = node->data; + + g_list_free_1 (node); + } + + return data; +} + + diff --git a/tests/.cvsignore b/tests/.cvsignore index f6d398f4..8dacf558 100644 --- a/tests/.cvsignore +++ b/tests/.cvsignore @@ -33,3 +33,5 @@ tree-test dirname-test type-test strfunc-test +queue-test +stack-test diff --git a/tests/Makefile.am b/tests/Makefile.am index 1734038a..0e0bddfe 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -7,8 +7,10 @@ TESTS = \ hash-test \ list-test \ node-test \ + queue-test \ relation-test \ slist-test \ + stack-test \ string-test \ strfunc-test \ tree-test \ @@ -21,8 +23,10 @@ dirname_test_LDADD = $(top_builddir)/libglib.la hash_test_LDADD = $(top_builddir)/libglib.la list_test_LDADD = $(top_builddir)/libglib.la node_test_LDADD = $(top_builddir)/libglib.la +queue_test_LDADD = $(top_builddir)/libglib.la relation_test_LDADD = $(top_builddir)/libglib.la slist_test_LDADD = $(top_builddir)/libglib.la +stack_test_LDADD = $(top_builddir)/libglib.la string_test_LDADD = $(top_builddir)/libglib.la strfunc_test_LDADD = $(top_builddir)/libglib.la tree_test_LDADD = $(top_builddir)/libglib.la diff --git a/tests/queue-test.c b/tests/queue-test.c new file mode 100644 index 00000000..6985bcc2 --- /dev/null +++ b/tests/queue-test.c @@ -0,0 +1,83 @@ +#ifdef HAVE_CONFIG_H +# include +#endif +#include + +int main() +{ + GQueue *q; + + q = g_queue_new (); + + g_assert (g_queue_empty (q) == TRUE); + + g_queue_push (q, GINT_TO_POINTER (1)); + g_assert (g_list_length (q->list) == 1); + g_queue_push (q, GINT_TO_POINTER (2)); + g_assert (g_list_length (q->list) == 2); + g_queue_push (q, GINT_TO_POINTER (3)); + g_assert (g_list_length (q->list) == 3); + g_queue_push (q, GINT_TO_POINTER (4)); + g_assert (g_list_length (q->list) == 4); + g_queue_push (q, GINT_TO_POINTER (5)); + g_assert (g_list_length (q->list) == 5); + + g_assert (g_queue_empty (q) == FALSE); + + g_assert (g_queue_index (q, GINT_TO_POINTER (2)) == 1); + g_assert (g_queue_index (q, GINT_TO_POINTER (142)) == -1); + + g_assert (g_queue_peek (q) == GINT_TO_POINTER (1)); + g_assert (g_queue_peek_front (q) == GINT_TO_POINTER (1)); + g_assert (g_queue_peek_back (q) == GINT_TO_POINTER (5)); + + g_assert (g_queue_pop (q) == GINT_TO_POINTER (1)); + g_assert (g_list_length (q->list) == 4); + g_assert (g_queue_pop (q) == GINT_TO_POINTER (2)); + g_assert (g_list_length (q->list) == 3); + g_assert (g_queue_pop (q) == GINT_TO_POINTER (3)); + g_assert (g_list_length (q->list) == 2); + g_assert (g_queue_pop (q) == GINT_TO_POINTER (4)); + g_assert (g_list_length (q->list) == 1); + g_assert (g_queue_pop (q) == GINT_TO_POINTER (5)); + g_assert (g_list_length (q->list) == 0); + g_assert (g_queue_pop (q) == NULL); + g_assert (g_list_length (q->list) == 0); + g_assert (g_queue_pop (q) == NULL); + g_assert (g_list_length (q->list) == 0); + + g_assert (g_queue_empty (q) == TRUE); + + /************************/ + + g_queue_push_front (q, GINT_TO_POINTER (1)); + g_assert (g_list_length (q->list) == 1); + g_queue_push_front (q, GINT_TO_POINTER (2)); + g_assert (g_list_length (q->list) == 2); + g_queue_push_front (q, GINT_TO_POINTER (3)); + g_assert (g_list_length (q->list) == 3); + g_queue_push_front (q, GINT_TO_POINTER (4)); + g_assert (g_list_length (q->list) == 4); + g_queue_push_front (q, GINT_TO_POINTER (5)); + g_assert (g_list_length (q->list) == 5); + + g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (5)); + g_assert (g_list_length (q->list) == 4); + g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (4)); + g_assert (g_list_length (q->list) == 3); + g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (3)); + g_assert (g_list_length (q->list) == 2); + g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (2)); + g_assert (g_list_length (q->list) == 1); + g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (1)); + g_assert (g_list_length (q->list) == 0); + g_assert (g_queue_pop_front (q) == NULL); + g_assert (g_list_length (q->list) == 0); + g_assert (g_queue_pop_front (q) == NULL); + g_assert (g_list_length (q->list) == 0); + + g_queue_free (q); + + return 0; +} + diff --git a/tests/stack-test.c b/tests/stack-test.c new file mode 100644 index 00000000..f29378ba --- /dev/null +++ b/tests/stack-test.c @@ -0,0 +1,51 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +int main() +{ + GStack *s; + + s = g_stack_new (); + + g_assert (g_stack_empty (s) == TRUE); + + g_stack_push (s, GINT_TO_POINTER (1)); + g_assert (g_list_length (s->list) == 1); + g_stack_push (s, GINT_TO_POINTER (2)); + g_assert (g_list_length (s->list) == 2); + g_stack_push (s, GINT_TO_POINTER (3)); + g_assert (g_list_length (s->list) == 3); + g_stack_push (s, GINT_TO_POINTER (4)); + g_assert (g_list_length (s->list) == 4); + g_stack_push (s, GINT_TO_POINTER (5)); + g_assert (g_list_length (s->list) == 5); + + g_assert (g_stack_index (s, GINT_TO_POINTER (2)) == 3); + + g_assert (g_stack_empty (s) == FALSE); + + g_assert (g_stack_peek (s) == GINT_TO_POINTER (5)); + + g_assert (g_stack_pop (s) == GINT_TO_POINTER (5)); + g_assert (g_list_length (s->list) == 4); + g_assert (g_stack_pop (s) == GINT_TO_POINTER (4)); + g_assert (g_list_length (s->list) == 3); + g_assert (g_stack_pop (s) == GINT_TO_POINTER (3)); + g_assert (g_list_length (s->list) == 2); + g_assert (g_stack_pop (s) == GINT_TO_POINTER (2)); + g_assert (g_list_length (s->list) == 1); + g_assert (g_stack_pop (s) == GINT_TO_POINTER (1)); + g_assert (g_list_length (s->list) == 0); + g_assert (g_stack_pop (s) == NULL); + g_assert (g_list_length (s->list) == 0); + g_assert (g_stack_pop (s) == NULL); + g_assert (g_list_length (s->list) == 0); + + g_stack_free (s); + + return 0; +} +