aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_heap.c
blob: 3ba9abb4860f5a798553426c4ab891a7c73e9ad9 (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
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2009, Digium, Inc.
 *
 * Russell Bryant <russell@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Heap data structure test module
 *
 * \author Russell Bryant <russell@digium.com>
 */

/*** MODULEINFO
	<defaultenabled>no</defaultenabled>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/heap.h"

struct node {
	long val;
	size_t index;
};

static int node_cmp(void *_n1, void *_n2)
{
	struct node *n1 = _n1;
	struct node *n2 = _n2;

	if (n1->val < n2->val) {
		return -1;
	} else if (n1->val == n2->val) {
		return 0;
	} else {
		return 1;
	}
}

static int test1(int fd)
{
	struct ast_heap *h;
	struct node *obj;
	struct node nodes[3] = {
		{ 1, },
		{ 2, },
		{ 3, },
	};

	if (!(h = ast_heap_create(8, node_cmp, offsetof(struct node, index)))) {
		return -1;
	}

	/* Pushing 1 2 3, and then popping 3 elements */

	ast_cli(fd, "Test #1 - Push a few elements onto a heap and make sure that they "
			"come back off in the right order.\n");

	ast_heap_push(h, &nodes[0]);

	ast_heap_push(h, &nodes[1]);

	ast_heap_push(h, &nodes[2]);

	obj = ast_heap_pop(h);
	if (obj->val != 3) {
		return -2;
	}

	obj = ast_heap_pop(h);
	if (obj->val != 2) {
		return -3;
	}

	obj = ast_heap_pop(h);
	if (obj->val != 1) {
		return -4;
	}

	obj = ast_heap_pop(h);
	if (obj) {
		return -5;
	}

	h = ast_heap_destroy(h);

	ast_cli(fd, "Test #1 successful.\n");

	return 0;
}

static int test2(int fd)
{
	struct ast_heap *h = NULL;
	static const unsigned int one_million = 1000000;
	struct node *nodes = NULL;
	struct node *node;
	unsigned int i = one_million;
	long last = LONG_MAX, cur;
	int res = 0;

	ast_cli(fd, "Test #2 - Push a million random elements on to a heap, "
			"verify that the heap has been properly constructed, "
			"and then ensure that the elements come back off in the proper order\n");

	if (!(nodes = ast_malloc(one_million * sizeof(*node)))) {
		res = -1;
		goto return_cleanup;
	}

	if (!(h = ast_heap_create(20, node_cmp, offsetof(struct node, index)))) {
		res = -2;
		goto return_cleanup;
	}

	while (i--) {
		nodes[i].val = ast_random();
		ast_heap_push(h, &nodes[i]);
	}

	if (ast_heap_verify(h)) {
		res = -3;
		goto return_cleanup;
	}

	i = 0;
	while ((node = ast_heap_pop(h))) {
		cur = node->val;
		if (cur > last) {
			ast_cli(fd, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
			res = -4;
			goto return_cleanup;
		}
		last = cur;
		i++;
	}

	if (i != one_million) {
		ast_cli(fd, "Stopped popping off after only getting %u nodes\n", i);
		res = -5;
		goto return_cleanup;
	}

	ast_cli(fd, "Test #2 successful.\n");

return_cleanup:
	if (h) {
		h = ast_heap_destroy(h);
	}
	if (nodes) {
		ast_free(nodes);
	}

	return res;
}

static int test3(int fd)
{
	struct ast_heap *h = NULL;
	struct node *nodes = NULL;
	struct node *node;
	static const unsigned int test_size = 100000;
	unsigned int i = test_size;
	long last = LONG_MAX, cur;
	int res = 0;
	int random_index;

	ast_cli(fd, "Test #3 - Push a hundred thousand random elements on to a heap, "
			"verify that the heap has been properly constructed, "
			"randomly remove and re-add 10000 elements and then ensure that the elements come back off in the proper order\n");

	if (!(nodes = ast_malloc(test_size * sizeof(*node)))) {
		res = -1;
		goto return_cleanup;
	}

	if (!(h = ast_heap_create(20, node_cmp, offsetof(struct node, index)))) {
		res = -2;
		goto return_cleanup;
	}

	while (i--) {
		nodes[i].val = ast_random();
		ast_heap_push(h, &nodes[i]);
	}

	if (ast_heap_verify(h)) {
		res = -3;
		goto return_cleanup;
	}

	i = test_size / 10;
	while (i--) {
		random_index = ast_random() % test_size;
		node = ast_heap_remove(h, &nodes[random_index]);
		if (nodes[random_index].val != node->val){
			res = -4;
			goto return_cleanup;
		}
		ast_heap_push(h, &nodes[random_index]); 

	}

	if (ast_heap_verify(h)) {
		res = -5;
		goto return_cleanup;
	}

	i = 0;
	while ((node = ast_heap_pop(h))) {
		cur = node->val;
		if (cur > last) {
			ast_cli(fd, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
			res = -6;
			goto return_cleanup;
		}
		last = cur;
		i++;
	}

	if (i != test_size) {
		ast_cli(fd, "Stopped popping off after only getting %u nodes\n", i);
		res = -7;
		goto return_cleanup;
	}

	ast_cli(fd, "Test #3 successful.\n");

return_cleanup:
	if (h) {
		h = ast_heap_destroy(h);
	}
	if (nodes) {
		ast_free(nodes);
	}

	return res;
}


static char *handle_cli_heap_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	int res;

	switch (cmd) {
	case CLI_INIT:
		e->command = "heap test";
		e->usage = ""
			"Usage: heap test\n"
			"";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	if (a->argc != e->args) {
		return CLI_SHOWUSAGE;
	}

	if ((res = test1(a->fd))) {
		ast_cli(a->fd, "Test 1 failed! (%d)\n", res);
		return CLI_FAILURE;
	}

	if ((res = test2(a->fd))) {
		ast_cli(a->fd, "Test 2 failed! (%d)\n", res);
		return CLI_FAILURE;
	}

	if ((res = test3(a->fd))) {
		ast_cli(a->fd, "Test 3 failed! (%d)\n", res);
		return CLI_FAILURE;
	}

	return CLI_SUCCESS;
}

static struct ast_cli_entry cli_heap[] = {
	AST_CLI_DEFINE(handle_cli_heap_test, "Test the heap implementation"),
};

static int unload_module(void)
{
	ast_cli_unregister_multiple(cli_heap, ARRAY_LEN(cli_heap));
	return 0;
}

static int load_module(void)
{
	ast_cli_register_multiple(cli_heap, ARRAY_LEN(cli_heap));
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Heap test module");