asterisk/tests/test_heap.c
Matt Jordan 4a58261694 git migration: Refactor the ASTERISK_FILE_VERSION macro
Git does not support the ability to replace a token with a version
string during check-in. While it does have support for replacing a
token on clone, this is somewhat sub-optimal: the token is replaced
with the object hash, which is not particularly easy for human
consumption. What's more, in practice, the source file version was often
not terribly useful. Generally, when triaging bugs, the overall version
of Asterisk is far more useful than an individual SVN version of a file. As a
result, this patch removes Asterisk's support for showing source file
versions.

Specifically, it does the following:

* Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and
  remove passing the version in with the macro. Other facilities
  than 'core show file version' make use of the file names, such as
  setting a debug level only on a specific file. As such, the act of
  registering source files with the Asterisk core still has use. The
  macro rename now reflects the new macro purpose.

* main/asterisk:
  - Refactor the file_version structure to reflect that it no longer
    tracks a version field.
  - Remove the "core show file version" CLI command. Without the file
    version, it is no longer useful.
  - Remove the ast_file_version_find function. The file version is no
    longer tracked.
  - Rename ast_register_file_version/ast_unregister_file_version to
    ast_register_file/ast_unregister_file, respectively.

* main/manager: Remove value from the Version key of the ModuleCheck
  Action. The actual key itself has not been removed, as doing so would
  absolutely constitute a backwards incompatible change. However, since
  the file version is no longer tracked, there is no need to attempt to
  include it in the Version key.

* UPGRADE: Add notes for:
  - Modification to the ModuleCheck AMI Action
  - Removal of the "core show file version" CLI command

Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-13 03:48:57 -04:00

309 lines
6.6 KiB
C

/*
* 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
<depend>TEST_FRAMEWORK</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_REGISTER_FILE()
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "asterisk/heap.h"
#include "asterisk/test.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;
}
}
AST_TEST_DEFINE(heap_test_1)
{
struct ast_heap *h;
struct node *obj;
struct node nodes[3] = {
{ 1, } ,
{ 2, } ,
{ 3, } ,
};
switch (cmd) {
case TEST_INIT:
info->name = "heap_test_1";
info->category = "/main/heap/";
info->summary = "push and pop elements";
info->description = "Push a few elements onto a heap and make sure that they come back off in the right order.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (!(h = ast_heap_create(8, node_cmp, offsetof(struct node, index)))) {
return AST_TEST_FAIL;
}
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) {
ast_test_status_update(test, "expected 3, got %ld\n", obj->val);
return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj->val != 2) {
ast_test_status_update(test, "expected 2, got %ld\n", obj->val);
return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj->val != 1) {
ast_test_status_update(test, "expected 1, got %ld\n", obj->val);
return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj) {
ast_test_status_update(test, "got unexpected object\n");
return AST_TEST_FAIL;
}
h = ast_heap_destroy(h);
return AST_TEST_PASS;
}
AST_TEST_DEFINE(heap_test_2)
{
struct ast_heap *h = NULL;
static const unsigned int total = 100000;
struct node *nodes = NULL;
struct node *node;
unsigned int i = total;
long last = LONG_MAX;
long cur;
enum ast_test_result_state res = AST_TEST_PASS;
switch (cmd) {
case TEST_INIT:
info->name = "heap_test_2";
info->category = "/main/heap/";
info->summary = "load test";
info->description =
"Push one hundred thousand random elements on to a heap, "
"verify that the heap has been properly constructed, "
"and then ensure that the elements are come back off "
"in the proper order.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (!(nodes = ast_malloc(total * sizeof(*node)))) {
res = AST_TEST_FAIL;
goto return_cleanup;
}
if (!(h = ast_heap_create(20, node_cmp, offsetof(struct node, index)))) {
res = AST_TEST_FAIL;
goto return_cleanup;
}
while (i--) {
nodes[i].val = ast_random();
ast_heap_push(h, &nodes[i]);
}
if (ast_heap_verify(h)) {
res = AST_TEST_FAIL;
goto return_cleanup;
}
i = 0;
while ((node = ast_heap_pop(h))) {
cur = node->val;
if (cur > last) {
ast_test_status_update(test, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
res = AST_TEST_FAIL;
goto return_cleanup;
}
last = cur;
i++;
}
if (i != total) {
ast_test_status_update(test, "Stopped popping off after only getting %u nodes\n", i);
res = AST_TEST_FAIL;
goto return_cleanup;
}
return_cleanup:
if (h) {
h = ast_heap_destroy(h);
}
if (nodes) {
ast_free(nodes);
}
return res;
}
AST_TEST_DEFINE(heap_test_3)
{
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 random_index;
enum ast_test_result_state res = AST_TEST_PASS;
switch (cmd) {
case TEST_INIT:
info->name = "heap_test_3";
info->category = "/main/heap/";
info->summary = "random element removal test";
info->description =
"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.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (!(nodes = ast_malloc(test_size * sizeof(*node)))) {
ast_test_status_update(test, "memory allocation failure\n");
res = AST_TEST_FAIL;
goto return_cleanup;
}
if (!(h = ast_heap_create(20, node_cmp, offsetof(struct node, index)))) {
ast_test_status_update(test, "Failed to allocate heap\n");
res = AST_TEST_FAIL;
goto return_cleanup;
}
while (i--) {
nodes[i].val = ast_random();
ast_heap_push(h, &nodes[i]);
}
if (ast_heap_verify(h)) {
ast_test_status_update(test, "Failed to verify heap after populating it\n");
res = AST_TEST_FAIL;
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){
ast_test_status_update(test, "Failed to remove what we expected to\n");
res = AST_TEST_FAIL;
goto return_cleanup;
}
ast_heap_push(h, &nodes[random_index]);
}
if (ast_heap_verify(h)) {
ast_test_status_update(test, "Failed to verify after removals\n");
res = AST_TEST_FAIL;
goto return_cleanup;
}
i = 0;
while ((node = ast_heap_pop(h))) {
cur = node->val;
if (cur > last) {
ast_test_status_update(test, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
res = AST_TEST_FAIL;
goto return_cleanup;
}
last = cur;
i++;
}
if (i != test_size) {
ast_test_status_update(test, "Stopped popping off after only getting %u nodes\n", i);
res = AST_TEST_FAIL;
goto return_cleanup;
}
return_cleanup:
if (h) {
h = ast_heap_destroy(h);
}
if (nodes) {
ast_free(nodes);
}
return res;
}
static int unload_module(void)
{
AST_TEST_UNREGISTER(heap_test_1);
AST_TEST_UNREGISTER(heap_test_2);
AST_TEST_UNREGISTER(heap_test_3);
return 0;
}
static int load_module(void)
{
AST_TEST_REGISTER(heap_test_1);
AST_TEST_REGISTER(heap_test_2);
AST_TEST_REGISTER(heap_test_3);
return AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Heap test module");