diff options
author | Gonzalo Paniagua Javier <gonzalo.mono@gmail.com> | 2006-10-18 22:39:12 +0400 |
---|---|---|
committer | Gonzalo Paniagua Javier <gonzalo.mono@gmail.com> | 2006-10-18 22:39:12 +0400 |
commit | 6809d60afc394cdc7c9c85d8b596a18c9399098b (patch) | |
tree | 8a0610d1ed02c79d4d7b5c06933f1676ff3085f7 /eglib | |
parent | 58fbe13675e7478abfbd435f15e400c766777049 (diff) |
2006-10-18 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* test/array.c: new test for insertion in the middle of other values.
* src/garray.c: fixed destination index.
svn path=/trunk/mono/; revision=66798
Diffstat (limited to 'eglib')
-rw-r--r-- | eglib/ChangeLog | 5 | ||||
-rw-r--r-- | eglib/src/garray.c | 15 | ||||
-rw-r--r-- | eglib/test/array.c | 32 |
3 files changed, 47 insertions, 5 deletions
diff --git a/eglib/ChangeLog b/eglib/ChangeLog index c0c30042de2..98888e85571 100644 --- a/eglib/ChangeLog +++ b/eglib/ChangeLog @@ -1,3 +1,8 @@ +2006-10-18 Gonzalo Paniagua Javier <gonzalo@ximian.com> + + * test/array.c: new test for insertion in the middle of other values. + * src/garray.c: fixed destination index. + 2006-10-18 Miguel de Icaza <miguel@novell.com> * src/gpath.c (g_build_path): Do not append terminator if the next diff --git a/eglib/src/garray.c b/eglib/src/garray.c index 23d0a5c16ea..e9605d0f786 100644 --- a/eglib/src/garray.c +++ b/eglib/src/garray.c @@ -85,6 +85,9 @@ g_array_free (GArray *array, gboolean free_segment) { gchar* rv = NULL; + + g_return_val_if_fail (array != NULL, NULL); + if (free_segment) g_free (array->data); else @@ -102,6 +105,8 @@ g_array_append_vals (GArray *array, { GArrayPriv *priv = (GArrayPriv*)array; + g_return_val_if_fail (array != NULL, NULL); + ensure_capacity (priv, priv->array.len + len + (priv->zero_terminated ? 1 : 0)); memmove (element_offset (priv, priv->array.len), @@ -126,8 +131,11 @@ g_array_insert_vals (GArray *array, guint len) { GArrayPriv *priv = (GArrayPriv*)array; + guint extra = (priv->zero_terminated ? 1 : 0); + + g_return_val_if_fail (array != NULL, NULL); - ensure_capacity (priv, array->len + len + (priv->zero_terminated ? 1 : 0)); + ensure_capacity (priv, array->len + len + extra); /* first move the existing elements out of the way */ memmove (element_offset (priv, index_ + len), @@ -135,7 +143,7 @@ g_array_insert_vals (GArray *array, element_length (priv, array->len - index_)); /* then copy the new elements into the array */ - memmove (element_offset (priv, array->len), + memmove (element_offset (priv, index_), data, element_length (priv, len)); @@ -156,6 +164,8 @@ g_array_remove_index (GArray *array, { GArrayPriv *priv = (GArrayPriv*)array; + g_return_val_if_fail (array != NULL, NULL); + memmove (element_offset (priv, index_), element_offset (priv, index_ + 1), element_length (priv, array->len - index_)); @@ -170,3 +180,4 @@ g_array_remove_index (GArray *array, return array; } + diff --git a/eglib/test/array.c b/eglib/test/array.c index 19914cc64e5..37d5486ace5 100644 --- a/eglib/test/array.c +++ b/eglib/test/array.c @@ -84,19 +84,45 @@ test_array_append () } RESULT -test_array_append2 () +test_array_insert_val () { GArray *array = g_array_new (FALSE, FALSE, sizeof (gpointer)); + gpointer ptr0, ptr1, ptr2, ptr3; g_array_insert_val (array, 0, array); if (array != g_array_index (array, gpointer, 0)) - return FAILED ("The value in the array is incorrect"); + return FAILED ("1 The value in the array is incorrect"); g_array_insert_val (array, 1, array); + if (array != g_array_index (array, gpointer, 1)) + return FAILED ("2 The value in the array is incorrect"); + g_array_insert_val (array, 2, array); + if (array != g_array_index (array, gpointer, 2)) + return FAILED ("3 The value in the array is incorrect"); g_array_free (array, TRUE); + array = g_array_new (FALSE, FALSE, sizeof (gpointer)); + ptr0 = array; + ptr1 = array + 1; + ptr2 = array + 2; + ptr3 = array + 3; + + g_array_insert_val (array, 0, ptr0); + g_array_insert_val (array, 1, ptr1); + g_array_insert_val (array, 2, ptr2); + g_array_insert_val (array, 1, ptr3); + if (ptr0 != g_array_index (array, gpointer, 0)) + return FAILED ("4 The value in the array is incorrect"); + if (ptr3 != g_array_index (array, gpointer, 1)) + return FAILED ("5 The value in the array is incorrect"); + if (ptr1 != g_array_index (array, gpointer, 2)) + return FAILED ("6 The value in the array is incorrect"); + if (ptr2 != g_array_index (array, gpointer, 3)) + return FAILED ("7 The value in the array is incorrect"); + + g_array_free (array, TRUE); return NULL; } @@ -127,7 +153,7 @@ test_array_remove () static Test array_tests [] = { {"big", test_array_big}, {"append", test_array_append}, - {"append2", test_array_append2}, + {"insert_val", test_array_insert_val}, {"index", test_array_index}, {"remove", test_array_remove}, {"append_zero_term", test_array_append_zero_terminated}, |