2007-10-19 Marek Habersack <mhabersack@novell.com>
[mono.git] / eglib / test / ptrarray.c
index 420cd32531744e1121ce7efc6ef245805179d478..f5bc366c27754a85de074dc1431582b7e4377cf9 100644 (file)
@@ -17,7 +17,7 @@ static const char *items [] = {
        NULL
 };
 
-static GPtrArray *ptrarray_alloc_and_fill(gint *item_count)
+static GPtrArray *ptrarray_alloc_and_fill(guint *item_count)
 {
        GPtrArray *array = g_ptr_array_new();
        gint i;
@@ -33,9 +33,9 @@ static GPtrArray *ptrarray_alloc_and_fill(gint *item_count)
        return array;
 }
 
-static gint guess_size(gint length)
+static guint guess_size(guint length)
 {
-       gint size = 1;
+       guint size = 1;
 
        while(size < length) {
                size <<= 1;
@@ -44,36 +44,36 @@ static gint guess_size(gint length)
        return size;
 }
 
-char *ptrarray_alloc()
+RESULT ptrarray_alloc()
 {
        GPtrArrayPriv *array;
-       gint i;
+       guint i;
        
        array = (GPtrArrayPriv *)ptrarray_alloc_and_fill(&i);
        
        if(array->size != guess_size(array->len)) {
-               return g_strdup_printf("Size should be %d, but it is %d", 
+               return FAILED("Size should be %d, but it is %d", 
                        guess_size(array->len), array->size);
        }
        
        if(array->len != i) {
-               return g_strdup_printf("Expected %d node(s) in the array", i);
+               return FAILED("Expected %d node(s) in the array", i);
        }
        
        g_ptr_array_free((GPtrArray *)array, TRUE);
 
-       return NULL;
+       return OK;
 }
 
-char *ptrarray_for_iterate()
+RESULT ptrarray_for_iterate()
 {
        GPtrArray *array = ptrarray_alloc_and_fill(NULL);
-       gint i;
+       guint i;
 
        for(i = 0; i < array->len; i++) {
                char *item = (char *)g_ptr_array_index(array, i);
                if(item != items[i]) {
-                       return g_strdup_printf(
+                       return FAILED(
                                "Expected item at %d to be %s, but it was %s", 
                                i, items[i], item);
                }
@@ -81,7 +81,7 @@ char *ptrarray_for_iterate()
 
        g_ptr_array_free(array, TRUE);
 
-       return NULL;
+       return OK;
 }
 
 static gint foreach_iterate_index = 0;
@@ -97,13 +97,13 @@ void foreach_callback(gpointer data, gpointer user_data)
        }
 
        if(item != item_cmp) {
-               foreach_iterate_error = g_strdup_printf(
+               foreach_iterate_error = FAILED(
                        "Expected item at %d to be %s, but it was %s", 
                                foreach_iterate_index - 1, item_cmp, item);
        }
 }
 
-char *ptrarray_foreach_iterate()
+RESULT ptrarray_foreach_iterate()
 {
        GPtrArray *array = ptrarray_alloc_and_fill(NULL);
        
@@ -117,12 +117,129 @@ char *ptrarray_foreach_iterate()
        return foreach_iterate_error;
 }
 
+RESULT ptrarray_set_size()
+{
+       GPtrArray *array = g_ptr_array_new();
+       guint i, grow_length = 50;
+       
+       g_ptr_array_add(array, (gpointer)items[0]);
+       g_ptr_array_add(array, (gpointer)items[1]);
+       g_ptr_array_set_size(array, grow_length);
+
+       if(array->len != grow_length) {
+               return FAILED("Array length should be 50, it is %d", array->len);
+       } else if(array->pdata[0] != items[0]) {
+               return FAILED("Item 0 was overwritten, should be %s", items[0]);
+       } else if(array->pdata[1] != items[1]) {
+               return FAILED("Item 1 was overwritten, should be %s", items[1]);
+       }
+
+       for(i = 2; i < array->len; i++) {
+               if(array->pdata[i] != NULL) {
+                       return FAILED("Item %d is not NULL, it is %p", i, array->pdata[i]);
+               }
+       }
+
+       g_ptr_array_free(array, TRUE);
+
+       return OK;
+}
+
+RESULT ptrarray_remove_index()
+{
+       GPtrArray *array;
+       guint i;
+       
+       array = ptrarray_alloc_and_fill(&i);
+       
+       g_ptr_array_remove_index(array, 0);
+       if(array->pdata[0] != items[1]) {
+               return FAILED("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 FAILED("Last item is not %s, it is %s", 
+                       items[array->len - 2], array->pdata[array->len - 1]);
+       }
+
+       g_ptr_array_free(array, TRUE);
+
+       return OK;
+}
+
+RESULT ptrarray_remove()
+{
+       GPtrArray *array;
+       guint 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 FAILED("Item %s not removed", items[4]);
+       }
+
+       if(g_ptr_array_remove(array, (gpointer)items[4])) {
+               return FAILED("Item %s still in array after removal", items[4]);
+       }
+
+       if(array->pdata[array->len - 1] != items[array->len + 1]) {
+               return FAILED("Last item in GPtrArray not correct");
+       }
+
+       g_ptr_array_free(array, TRUE);
+
+       return OK;
+}
+
+static gint ptrarray_sort_compare(gconstpointer a, gconstpointer b)
+{
+       gchar *stra = *(gchar **) a;
+       gchar *strb = *(gchar **) b;
+       return strcmp(stra, strb);
+}
+
+RESULT ptrarray_sort()
+{
+       GPtrArray *array = g_ptr_array_new();
+       guint i;
+       gchar *letters [] = { "A", "B", "C", "D", "E" };
+       
+       g_ptr_array_add(array, letters[0]);
+       g_ptr_array_add(array, letters[1]);
+       g_ptr_array_add(array, letters[2]);
+       g_ptr_array_add(array, letters[3]);
+       g_ptr_array_add(array, letters[4]);
+       
+       g_ptr_array_sort(array, ptrarray_sort_compare);
+
+       for(i = 0; i < array->len; i++) {
+               if(array->pdata[i] != letters[i]) {
+                       return FAILED("Array out of order, expected %s got %s", 
+                               (gchar *)array->pdata[i], letters[i]);
+               }
+       }
+
+       g_ptr_array_free(array, TRUE);
+       
+       return OK;
+}
+
 static Test ptrarray_tests [] = {
-       {"ptrarray_alloc", ptrarray_alloc},
-       {"ptrarray_for_iterate", ptrarray_for_iterate},
-       {"ptrarray_foreach_iterate", ptrarray_foreach_iterate},
+       {"alloc", ptrarray_alloc},
+       {"for_iterate", ptrarray_for_iterate},
+       {"foreach_iterate", ptrarray_foreach_iterate},
+       {"set_size", ptrarray_set_size},
+       {"remove_index", ptrarray_remove_index},
+       {"remove", ptrarray_remove},
+       {"sort", ptrarray_sort},
        {NULL, NULL}
 };
 
 DEFINE_TEST_GROUP_INIT(ptrarray_tests_init, ptrarray_tests)
 
+