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;
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;
RESULT ptrarray_alloc()
{
GPtrArrayPriv *array;
- gint i;
+ guint i;
array = (GPtrArrayPriv *)ptrarray_alloc_and_fill(&i);
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);
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]);
RESULT ptrarray_remove_index()
{
GPtrArray *array;
- gint i;
+ guint i;
array = ptrarray_alloc_and_fill(&i);
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);
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]);
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)
+