From e842123ee01cfdf3fe81f86af47f3f355bad3ae7 Mon Sep 17 00:00:00 2001 From: Aaron Bockover Date: Fri, 18 Aug 2006 03:57:07 +0000 Subject: [PATCH 1/1] 2006-08-17 Aaron Bockover * src/gptrarray.c: Implemented g_ptr_array_remove and g_ptr_array_remove_index * test/other: Removed, rewritten in Makefile.am * test/Makefile.am: Build test-eglib against local eglib and test-glib against GLib 2.0 (replaces 'other') * test/ptrarray.c: Added tests for g_ptr_array_remove and g_ptr_array_remove_index svn path=/trunk/mono/; revision=63964 --- eglib/ChangeLog | 13 +++++++++++ eglib/src/gptrarray.c | 38 ++++++++++++++++++++++++++++++++ eglib/test/Makefile.am | 30 ++++++++++++++----------- eglib/test/other | 10 --------- eglib/test/ptrarray.c | 50 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 23 deletions(-) delete mode 100755 eglib/test/other diff --git a/eglib/ChangeLog b/eglib/ChangeLog index a35d9e3450a..76f2c78839f 100644 --- a/eglib/ChangeLog +++ b/eglib/ChangeLog @@ -1,3 +1,16 @@ +2006-08-17 Aaron Bockover + + * src/gptrarray.c: Implemented g_ptr_array_remove and + g_ptr_array_remove_index + + * test/other: Removed, rewritten in Makefile.am + + * test/Makefile.am: Build test-eglib against local eglib and + test-glib against GLib 2.0 (replaces 'other') + + * test/ptrarray.c: Added tests for g_ptr_array_remove and + g_ptr_array_remove_index + 2006-08-17 Duncan Mak * src/gslist.c: Added MIT license. diff --git a/eglib/src/gptrarray.c b/eglib/src/gptrarray.c index 7f2aea6818d..6f09e7e72b3 100644 --- a/eglib/src/gptrarray.c +++ b/eglib/src/gptrarray.c @@ -121,6 +121,44 @@ g_ptr_array_add(GPtrArray *array, gpointer data) array->pdata[array->len++] = data; } +gpointer +g_ptr_array_remove_index(GPtrArray *array, guint index) +{ + gpointer removed_node; + + g_return_val_if_fail(array != NULL, NULL); + g_return_val_if_fail(index >= 0 || index < array->len, NULL); + + removed_node = array->pdata[index]; + + if(index != array->len - 1) { + g_memmove(array->pdata + index, array->pdata + index + 1, + (array->len - index - 1) * sizeof(gpointer)); + } + + array->len -= 1; +// array->pdata[array->len - 1] = NULL; + + return removed_node; +} + +gboolean +g_ptr_array_remove(GPtrArray *array, gpointer data) +{ + guint i; + + g_return_val_if_fail(array != NULL, FALSE); + + for(i = 0; i < array->len; i++) { + if(array->pdata[i] == data) { + g_ptr_array_remove_index(array, i); + return TRUE; + } + } + + return FALSE; +} + void g_ptr_array_foreach(GPtrArray *array, GFunc func, gpointer user_data) { diff --git a/eglib/test/Makefile.am b/eglib/test/Makefile.am index 36df80a4007..f8b98668d4c 100644 --- a/eglib/test/Makefile.am +++ b/eglib/test/Makefile.am @@ -1,20 +1,24 @@ -noinst_PROGRAMS = test +noinst_PROGRAMS = test-eglib test-glib -test_SOURCES = \ - test.c \ - test.h \ - tests.h \ - driver.c \ - hashtable.c \ - string-util.c \ - string.c \ - slist.c \ +SOURCES = \ + test.c \ + test.h \ + tests.h \ + driver.c \ + hashtable.c \ + string-util.c \ + string.c \ + slist.c \ ptrarray.c -test_CFLAGS = -Wall -Werror -D_FORTIFY_SOURCE=2 -INCLUDES = -I../src +test_eglib_SOURCES = $(SOURCES) +test_glib_SOURCES = $(SOURCES) -test_LDADD = -L../src -leglib +test_eglib_CFLAGS = -Wall -Werror -D_FORTIFY_SOURCE=2 -I../src +test_eglib_LDADD = -L../src -leglib + +test_glib_CFLAGS = `pkg-config --cflags glib-2.0` +test_glib_LDFLAGS = `pkg-config --libs glib-2.0` MAINTAINERCLEANFILES = Makefile.in diff --git a/eglib/test/other b/eglib/test/other deleted file mode 100755 index 05e38dda64d..00000000000 --- a/eglib/test/other +++ /dev/null @@ -1,10 +0,0 @@ -SOURCES="test.c driver.c hashtable.c string-util.c slist.c string.c ptrarray.c" - -OBJS=`echo $SOURCES | sed -e 's/\.c/\.o/g'` -mkdir o2 >& /dev/null - -(cd o2; for i in $SOURCES; do - gcc -Wall -Werror -c ../$i `pkg-config --cflags glib-2.0` -done; -gcc -o basetest $OBJS `pkg-config --libs glib-2.0`; -./basetest) diff --git a/eglib/test/ptrarray.c b/eglib/test/ptrarray.c index 370b93b7917..615eb7d45a1 100644 --- a/eglib/test/ptrarray.c +++ b/eglib/test/ptrarray.c @@ -149,11 +149,61 @@ char *ptrarray_set_size() return NULL; } +char *ptrarray_remove_index() +{ + GPtrArray *array; + gint i; + + array = ptrarray_alloc_and_fill(&i); + + g_ptr_array_remove_index(array, 0); + if(array->pdata[0] != items[1]) { + return g_strdup_printf("First item is not %s, it is %s", items[1], + array->pdata[0]); + } + + g_ptr_array_remove_index(array, array->len - 1); + + if(array->pdata[array->len - 1] != items[array->len]) { + return g_strdup_printf("Last item is not %s, it is %s", + items[array->len - 2], array->pdata[array->len - 1]); + } + + return NULL; +} + +char *ptrarray_remove() +{ + GPtrArray *array; + gint i; + + array = ptrarray_alloc_and_fill(&i); + + g_ptr_array_remove(array, (gpointer)items[7]); + + if(!g_ptr_array_remove(array, (gpointer)items[4])) { + return g_strdup_printf("Item %s not removed", items[4]); + } + + if(g_ptr_array_remove(array, (gpointer)items[4])) { + return g_strdup_printf("Item %s still in array after removal", + items[4]); + } + + if(array->pdata[array->len - 1] != items[array->len + 1]) { + return g_strdup_printf("Last item in GPtrArray not correct"); + } + + return NULL; +} + static Test ptrarray_tests [] = { {"ptrarray_alloc", ptrarray_alloc}, {"ptrarray_for_iterate", ptrarray_for_iterate}, {"ptrarray_foreach_iterate", ptrarray_foreach_iterate}, {"ptrarray_set_size", ptrarray_set_size}, + {"ptrarray_remove_index", ptrarray_remove_index}, + {"ptrarray_remove", ptrarray_remove}, {NULL, NULL} }; -- 2.25.1