New tests.
[mono.git] / eglib / test / ptrarray.c
index 926dbee0f593c7fc6fc3599418356c12255aad41..a823dd7655517d64f1ff115631f1bf62acbb674c 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;
@@ -47,7 +47,7 @@ static gint guess_size(gint length)
 RESULT ptrarray_alloc()
 {
        GPtrArrayPriv *array;
-       gint i;
+       guint i;
        
        array = (GPtrArrayPriv *)ptrarray_alloc_and_fill(&i);
        
@@ -68,7 +68,7 @@ RESULT ptrarray_alloc()
 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);
@@ -120,7 +120,7 @@ RESULT ptrarray_foreach_iterate()
 RESULT ptrarray_set_size()
 {
        GPtrArray *array = g_ptr_array_new();
-       gint i, grow_length = 50;
+       guint i, grow_length = 50;
        
        g_ptr_array_add(array, (gpointer)items[0]);
        g_ptr_array_add(array, (gpointer)items[1]);
@@ -148,7 +148,7 @@ RESULT ptrarray_set_size()
 RESULT ptrarray_remove_index()
 {
        GPtrArray *array;
-       gint i;
+       guint i;
        
        array = ptrarray_alloc_and_fill(&i);
        
@@ -170,10 +170,34 @@ RESULT ptrarray_remove_index()
        return OK;
 }
 
+RESULT ptrarray_remove_index_fast()
+{
+       GPtrArray *array;
+       guint i;
+
+       array = ptrarray_alloc_and_fill(&i);
+
+       g_ptr_array_remove_index_fast(array, 0);
+       if(array->pdata[0] != items[array->len]) {
+               return FAILED("First item is not %s, it is %s", items[array->len],
+                       array->pdata[0]);
+       }
+
+       g_ptr_array_remove_index_fast(array, array->len - 1);
+       if(array->pdata[array->len - 1] != items[array->len - 1]) {
+               return FAILED("Last item is not %s, it is %s",
+                       items[array->len - 1], array->pdata[array->len - 1]);
+       }
+
+       g_ptr_array_free(array, TRUE);
+
+       return OK;
+}
+
 RESULT ptrarray_remove()
 {
        GPtrArray *array;
-       gint i;
+       guint i;
        
        array = ptrarray_alloc_and_fill(&i);
 
@@ -206,7 +230,7 @@ static gint ptrarray_sort_compare(gconstpointer a, gconstpointer b)
 RESULT ptrarray_sort()
 {
        GPtrArray *array = g_ptr_array_new();
-       gint i;
+       guint i;
        gchar *letters [] = { "A", "B", "C", "D", "E" };
        
        g_ptr_array_add(array, letters[0]);
@@ -229,16 +253,59 @@ RESULT ptrarray_sort()
        return OK;
 }
 
+RESULT ptrarray_remove_fast()
+{
+       GPtrArray *array = g_ptr_array_new();
+       gchar *letters [] = { "A", "B", "C", "D", "E" };
+       
+       if (g_ptr_array_remove_fast (array, NULL))
+               return FAILED ("Removing NULL succeeded");
+
+       g_ptr_array_add(array, letters[0]);
+       if (!g_ptr_array_remove_fast (array, letters[0]) || array->len != 0)
+               return FAILED ("Removing last element failed");
+
+       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]);
+
+       if (!g_ptr_array_remove_fast (array, letters[0]) || array->len != 4)
+               return FAILED ("Removing first element failed");
+
+       if (array->pdata [0] != letters [4])
+               return FAILED ("First element wasn't replaced with last upon removal");
+
+       if (g_ptr_array_remove_fast (array, letters[0]))
+               return FAILED ("Succedeed removing a non-existing element");
+
+       if (!g_ptr_array_remove_fast (array, letters[3]) || array->len != 3)
+               return FAILED ("Failed removing \"D\"");
+
+       if (!g_ptr_array_remove_fast (array, letters[1]) || array->len != 2)
+               return FAILED ("Failed removing \"B\"");
+
+       if (array->pdata [0] != letters [4] || array->pdata [1] != letters [2])
+               return FAILED ("Last two elements are wrong");
+       g_ptr_array_free(array, TRUE);
+       
+       return OK;
+}
+
 static Test ptrarray_tests [] = {
        {"alloc", ptrarray_alloc},
        {"for_iterate", ptrarray_for_iterate},
        {"foreach_iterate", ptrarray_foreach_iterate},
        {"set_size", ptrarray_set_size},
        {"remove_index", ptrarray_remove_index},
+       {"remove_index_fast", ptrarray_remove_index_fast},
        {"remove", ptrarray_remove},
        {"sort", ptrarray_sort},
+       {"remove_fast", ptrarray_remove_fast},
        {NULL, NULL}
 };
 
 DEFINE_TEST_GROUP_INIT(ptrarray_tests_init, ptrarray_tests)
 
+