--- tests/test_heap.c 2009-04-10 17:01:26.000000000 +0100 +++ ../asterisk-1.6.2.7-rc2-chris/tests/test_heap.c 2010-05-03 18:15:44.000000000 +0100 @@ -172,6 +172,84 @@ return res; } +static int test3(int fd) +{ + struct ast_heap *h = NULL; + struct node *nodes = NULL; + struct node *node; + unsigned int i = 1000; + long last = LONG_MAX, cur; + int res = 0; + unsigned int random_index; + + ast_cli(fd, "Test #3 - Push a thousand random elements on to a heap, " + "verify that the heap has been properly constructed, " + "randomly remove 500 elements and re-add, and then ensure that the elements are come back off in the proper order\n"); + + if (!(nodes = ast_malloc(1000 * 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 = 500; + while (i--) { + random_index = ast_random() % 999; + ast_heap_remove(h, &nodes[random_index]); + ast_heap_push(h, &nodes[random_index]); + } + + if (ast_heap_verify(h)) { + res = -4; + 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 = -5; + goto return_cleanup; + } + last = cur; + i++; + } + + if (i != 1000) { + ast_cli(fd, "Stopped popping off after only getting %u nodes\n", i); + res = -6; + 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; @@ -201,6 +279,11 @@ return CLI_FAILURE; } + if ((res = test3(a->fd))) { + ast_cli(a->fd, "Test 3 failed! (%d)\n", res); + return CLI_FAILURE; + } + return CLI_SUCCESS; }