1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
#include <stdio.h>
#include <glib.h>
#include "test.h"
/* Redefine the private structure only to verify proper allocations */
typedef struct _GPtrArrayPriv {
gpointer *pdata;
guint len;
guint size;
} GPtrArrayPriv;
/* Don't add more than 32 items to this please */
static const char *items [] = {
"Apples", "Oranges", "Plumbs", "Goats", "Snorps", "Grapes",
"Tickle", "Place", "Coffee", "Cookies", "Cake", "Cheese",
"Tseng", "Holiday", "Avenue", "Smashing", "Water", "Toilet",
NULL
};
static GPtrArray *ptrarray_alloc_and_fill(guint *item_count)
{
GPtrArray *array = g_ptr_array_new();
gint i;
for(i = 0; items[i] != NULL; i++) {
g_ptr_array_add(array, (gpointer)items[i]);
}
if(item_count != NULL) {
*item_count = i;
}
return array;
}
static guint guess_size(guint length)
{
guint size = 1;
while(size < length) {
size <<= 1;
}
return size;
}
RESULT ptrarray_alloc()
{
GPtrArrayPriv *array;
guint i;
array = (GPtrArrayPriv *)ptrarray_alloc_and_fill(&i);
if(array->size != guess_size(array->len)) {
return FAILED("Size should be %d, but it is %d",
guess_size(array->len), array->size);
}
if(array->len != i) {
return FAILED("Expected %d node(s) in the array", i);
}
g_ptr_array_free((GPtrArray *)array, TRUE);
return OK;
}
RESULT ptrarray_for_iterate()
{
GPtrArray *array = ptrarray_alloc_and_fill(NULL);
guint i;
for(i = 0; i < array->len; i++) {
char *item = (char *)g_ptr_array_index(array, i);
if(item != items[i]) {
return FAILED(
"Expected item at %d to be %s, but it was %s",
i, items[i], item);
}
}
g_ptr_array_free(array, TRUE);
return OK;
}
static gint foreach_iterate_index = 0;
static char *foreach_iterate_error = NULL;
void foreach_callback(gpointer data, gpointer user_data)
{
char *item = (char *)data;
const char *item_cmp = items[foreach_iterate_index++];
if(foreach_iterate_error != NULL) {
return;
}
if(item != item_cmp) {
foreach_iterate_error = FAILED(
"Expected item at %d to be %s, but it was %s",
foreach_iterate_index - 1, item_cmp, item);
}
}
RESULT ptrarray_foreach_iterate()
{
GPtrArray *array = ptrarray_alloc_and_fill(NULL);
foreach_iterate_index = 0;
foreach_iterate_error = NULL;
g_ptr_array_foreach(array, foreach_callback, array);
g_ptr_array_free(array, TRUE);
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_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;
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 at position %d",
letters [i], (gchar *) array->pdata [i], i);
}
}
g_ptr_array_free(array, TRUE);
return OK;
}
static gint ptrarray_sort_compare_with_data (gconstpointer a, gconstpointer b, gpointer user_data)
{
gchar *stra = *(gchar **) a;
gchar *strb = *(gchar **) b;
if (strcmp (user_data, "this is the data for qsort") != 0)
fprintf (stderr, "oops at compare with_data\n");
return strcmp(stra, strb);
}
RESULT ptrarray_sort_with_data ()
{
GPtrArray *array = g_ptr_array_new();
guint i;
gchar *letters [] = { "A", "B", "C", "D", "E" };
g_ptr_array_add(array, letters[4]);
g_ptr_array_add(array, letters[1]);
g_ptr_array_add(array, letters[2]);
g_ptr_array_add(array, letters[0]);
g_ptr_array_add(array, letters[3]);
g_ptr_array_sort_with_data(array, ptrarray_sort_compare_with_data, "this is the data for qsort");
for(i = 0; i < array->len; i++) {
if(array->pdata[i] != letters[i]) {
return FAILED("Array out of order, expected %s got %s at position %d",
letters [i], (gchar *) array->pdata [i], i);
}
}
g_ptr_array_free(array, TRUE);
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},
{"sort_with_data", ptrarray_sort_with_data},
{NULL, NULL}
};
DEFINE_TEST_GROUP_INIT(ptrarray_tests_init, ptrarray_tests)
|