summaryrefslogtreecommitdiff
path: root/tests/hgstack.c
blob: c0876523c73349b0532f1b8655e8c9e7d065a737 (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * hgstack.c
 * Copyright (C) 2010-2011 Akira TAGOH
 * 
 * Authors:
 *   Akira TAGOH  <akira@tagoh.org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "main.h"
#include "hgint.h"
#include "hgstack.h"
#include "hgstack-private.h"
#include "hgmem.h"


hg_mem_t *mem = NULL;
hg_object_vtable_t *vtable = NULL;

/** common **/
void
setup(void)
{
	hg_object_init();
	HG_STACK_INIT;
	mem = hg_mem_spool_new(HG_MEM_TYPE_LOCAL, 100000);
	vtable = hg_object_stack_get_vtable();
}

void
teardown(void)
{
	hg_char_t *e = hieroglyph_test_pop_error();

	if (e) {
		g_print("E: %s\n", e);
		g_free(e);
	}
	hg_object_tini();
	hg_mem_spool_destroy(mem);
}

/** test cases **/
TDEF (get_capsulated_size)
{
	hg_usize_t size;

	size = vtable->get_capsulated_size();
	fail_unless(size == sizeof (hg_stack_t), "Obtaining the different size: expect: %lx actual: %lx", sizeof (hg_stack_t), size);
} TEND

TDEF (hg_stack_new)
{
	hg_stack_t *s;

	s = hg_stack_new(mem, 10, NULL);
	fail_unless(s != NULL, "Unable to create a stack object");
} TEND

TDEF (hg_stack_set_validation)
{
	hg_stack_t *s;

	s = hg_stack_new(mem, 1, NULL);
	fail_unless(s != NULL, "Unable to create a stack object");
	fail_unless(hg_stack_push(s, Qnil), "Unexpected result to push a value into the stack");
	fail_unless(hg_stack_depth(s) == 1, "Unexpected result to obtain the current depth on the stack");
	fail_unless(!hg_stack_push(s, Qnil), "Unexpected result to push more than the max depth");
	hg_stack_set_validation(s, FALSE);
	fail_unless(hg_stack_push(s, Qnil), "Unexpected result to push more than the max depth without the validation");
	fail_unless(hg_stack_depth(s) == 2, "Unexpected result to obtain the current depth on the stack [take 2]");
} TEND

TDEF (hg_stack_depth)
{
	/* should be done the above */
} TEND

TDEF (hg_stack_push)
{
	/* should be done the above */
} TEND

TDEF (hg_stack_pop)
{
	hg_stack_t *s;

	s = hg_stack_new(mem, 5, NULL);
	fail_unless(s != NULL, "Unable to create a stack object");
	fail_unless(hg_stack_push(s, HG_QINT (5)), "Unexpected result to push a value into the stack");
	fail_unless(hg_stack_depth(s) == 1, "Unexpected result to obtain the current depth on the stack");
	fail_unless(hg_stack_push(s, HG_QINT (6)), "Unexpected result to push a value into the stack [take 2]");
	fail_unless(hg_stack_index(s, 1) == HG_QINT (5), "Unexpected result to obtain a value with the index.");
	fail_unless(hg_stack_pop(s) == HG_QINT (6), "Unexpected result to pop a value from the stack");
	hg_stack_clear(s);
	fail_unless(hg_stack_depth(s) == 0, "Unexpected result to obtain the current depth after cleaning up");
} TEND

TDEF (hg_stack_clear)
{
	/* should be done the above */
} TEND

TDEF (hg_stack_index)
{
	/* should be done the above */
} TEND

static hg_bool_t
_s2s(hg_mem_t     *mem,
     hg_quark_t    q,
     hg_pointer_t  data)
{
	GString *s = data;

	if (q == Qnil)
		g_string_append(s, "nil ");
	else
		g_string_append_printf(s, "%lx ", hg_quark_get_value(q));

	return TRUE;
}

TDEF (hg_stack_roll)
{
	hg_stack_t *s;
	hg_usize_t i;
	GString *str = g_string_new(NULL);

	s = hg_stack_new(mem, 10, NULL);
	fail_unless(s != NULL, "Unable to create a stack object");
	for (i = 1; i < 6; i++) {
		fail_unless(hg_stack_push(s, HG_QINT (i)), "Unexpected result to push a value into the stack: %lx", i);
	}
	hg_stack_roll(s, 5, 1);
	hg_stack_foreach(s, _s2s, str, FALSE);
	fail_unless(strcmp(str->str, "4 3 2 1 5 ") == 0, "Unexpected result to roll up: actual: %s", str->str);
	hg_stack_roll(s, 5, -1);
	g_string_erase(str, 0, -1);
	hg_stack_foreach(s, _s2s, str, FALSE);
	fail_unless(strcmp(str->str, "5 4 3 2 1 ") == 0, "Unexpected result to roll down: actual: %s", str->str);
	fail_unless(hg_stack_push(s, HG_QINT (6)), "Unexpected result to push a value into the stack: %lx", 6);

	hg_stack_roll(s, 5, 1);
	g_string_erase(str, 0, -1);
	hg_stack_foreach(s, _s2s, str, FALSE);
	fail_unless(strcmp(str->str, "5 4 3 2 6 1 ") == 0, "Unexpected result to roll up [take 2]: actual: %s", str->str);
	hg_stack_roll(s, 5, -1);
	g_string_erase(str, 0, -1);
	hg_stack_foreach(s, _s2s, str, FALSE);
	fail_unless(strcmp(str->str, "6 5 4 3 2 1 ") == 0, "Unexpected result to roll down [take 2]: actual: %s", str->str);

	hg_stack_roll(s, 5, 2);
	g_string_erase(str, 0, -1);
	hg_stack_foreach(s, _s2s, str, FALSE);
	fail_unless(strcmp(str->str, "4 3 2 6 5 1 ") == 0, "Unexpected result to roll up [take 3]: actual: %s", str->str);
	hg_stack_roll(s, 5, -2);
	g_string_erase(str, 0, -1);
	hg_stack_foreach(s, _s2s, str, FALSE);
	fail_unless(strcmp(str->str, "6 5 4 3 2 1 ") == 0, "Unexpected result to roll down [take 3]: actual: %s", str->str);
} TEND

TDEF (hg_stack_foreach)
{
	/* should be done the above */
} TEND

/****/
Suite *
hieroglyph_suite(void)
{
	Suite *s = suite_create("hgstack.h");
	TCase *tc = tcase_create("Generic Functionalities");

	tcase_add_checked_fixture(tc, setup, teardown);

	T (get_capsulated_size);
	T (hg_stack_new);
	T (hg_stack_set_validation);
	T (hg_stack_depth);
	T (hg_stack_push);
	T (hg_stack_pop);
	T (hg_stack_clear);
	T (hg_stack_index);
	T (hg_stack_roll);
	T (hg_stack_foreach);

	suite_add_tcase(s, tc);

	return s;
}