summaryrefslogtreecommitdiff
path: root/tests/hglist.c
blob: b6bea30ae9117baa7ef2a6e5d46b95af626af47d (plain)
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
#include <hieroglyph/hgallocator-bfit.h>
#include <hieroglyph/hgmem.h>
#include <hieroglyph/hglist.h>

int
main(void)
{
	HG_MEM_INIT;

	HgAllocator *allocator;
	HgMemPool *pool;
	HgList *list;
	HgListIter iter;
	gint tc1[] = {1, 2, 3, 0};
	gint tc2[] = {1, 3, 0};
	gint i;

	allocator = hg_allocator_new(hg_allocator_bfit_get_vtable());
	pool = hg_mem_pool_new(allocator, "test", 128, HG_MEM_RESIZABLE);
	if (pool == NULL) {
		g_print("Failed to create a memory pool.\n");
		return 1;
	}
	list = hg_list_new(pool);
	if (list == NULL) {
		g_print("Failed to create an list.\n");
		return 1;
	}

	list = hg_list_append(list, GINT_TO_POINTER (1));
	list = hg_list_append(list, GINT_TO_POINTER (2));
	list = hg_list_append(list, GINT_TO_POINTER (3));

	iter = hg_list_iter_new(list);
	if (iter == NULL) {
		g_print("Failed to create an iter.\n");
		return 1;
	}

	for (i = 0; ; i++) {
		if (tc1[i] != GPOINTER_TO_INT (hg_list_iter_get_data(iter))) {
			g_print("Failed to compare: expected %d, but actually %d",
				tc1[i], GPOINTER_TO_INT (hg_list_iter_get_data(iter)));
			return 1;
		}
		if (!hg_list_get_iter_next(list, iter))
			break;
	}
	if (i != 2) {
		g_print("Expected list size 3 but actually was %d\n", i + 1);
		return 1;
	}
	list = hg_list_remove(list, GINT_TO_POINTER (2));
	hg_list_get_iter_first(list, iter);
	for (i = 0; ; i++) {
		if (tc2[i] != GPOINTER_TO_INT (hg_list_iter_get_data(iter))) {
			g_print("Failed to compare: expected %d, but actually %d",
				tc2[i], GPOINTER_TO_INT (hg_list_iter_get_data(iter)));
			return 1;
		}
		if (!hg_list_get_iter_next(list, iter))
			break;
	}
	if (i != 1) {
		g_print("Expected list size 2 but actually was %d\n", i + 1);
		return 1;
	}

	hg_mem_pool_destroy(pool);
	hg_allocator_destroy(allocator);
	hg_mem_finalize();

	return 0;
}