2008-11-07 Bill Holmes <billholmes54@gmail.com>
[mono.git] / eglib / test / array.c
index f44cec7c9d86150d22d56c54596675847f0321ee..37d5486ace50574f9326296890339d80a748d434 100644 (file)
@@ -3,6 +3,28 @@
 #include <glib.h>
 #include "test.h"
 
+/* example from glib documentation */
+RESULT
+test_array_big ()
+{
+       GArray *garray;
+       gint i;
+
+       /* We create a new array to store gint values.
+          We don't want it zero-terminated or cleared to 0's. */
+       garray = g_array_new (FALSE, FALSE, sizeof (gint));
+       for (i = 0; i < 10000; i++)
+               g_array_append_val (garray, i);
+
+       for (i = 0; i < 10000; i++)
+               if (g_array_index (garray, gint, i) != i)
+                       return FAILED ("array value didn't match");
+       
+       g_array_free (garray, TRUE);
+
+       return NULL;
+}
+
 RESULT
 test_array_index ()
 {
@@ -61,6 +83,49 @@ test_array_append ()
        return NULL;
 }
 
+RESULT
+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 ("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;
+}
+
 RESULT
 test_array_remove ()
 {
@@ -86,10 +151,12 @@ test_array_remove ()
 }
 
 static Test array_tests [] = {
-       {"array_append", test_array_append},
-       {"array_index", test_array_index},
-       {"array_remove", test_array_remove},
-       {"array_append_zero_terminated", test_array_append_zero_terminated},
+       {"big", test_array_big},
+       {"append", test_array_append},
+       {"insert_val", test_array_insert_val},
+       {"index", test_array_index},
+       {"remove", test_array_remove},
+       {"append_zero_term", test_array_append_zero_terminated},
        {NULL, NULL}
 };