summaryrefslogtreecommitdiff
path: root/tests/hglist.c
blob: a76b925c72b1defdad285fd1a08a36480c97fa48 (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
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
#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 tc3[] = {2, 3, 0};
	gint tc4[] = {1, 2, 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;
	}

	/* testcase 2 */
	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));

	list = hg_list_remove(list, GINT_TO_POINTER (1));
	iter = hg_list_iter_new(list);
	if (iter == NULL) {
		g_print("Failed to create an iter.\n");
		return 1;
	}
	for (i = 0; ; i++) {
		if (tc3[i] != GPOINTER_TO_INT (hg_list_iter_get_data(iter))) {
			g_print("Failed to compare: expected %d, but actually %d",
				tc3[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;
	}

	/* testcase 3 */
	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));

	list = hg_list_remove(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 (tc4[i] != GPOINTER_TO_INT (hg_list_iter_get_data(iter))) {
			g_print("Failed to compare: expected %d, but actually %d",
				tc4[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;
	}

	/* testcase 4 */
	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_remove(list, GINT_TO_POINTER (1));
	if (list != NULL) {
		g_print("Expected list size 0 but actually was %d\n", hg_list_length(list));
		return 1;
	}

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

	return 0;
}