2015-03-17 10:27:40 +00:00
|
|
|
/*
|
|
|
|
* BIRD Library -- Universal Heap Macros Tests
|
|
|
|
*
|
|
|
|
* (c) 2015 CZ.NIC z.s.p.o.
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2015-03-23 16:40:13 +00:00
|
|
|
#include "test/birdtest.h"
|
2015-04-15 13:42:43 +00:00
|
|
|
#include "sysdep/config.h"
|
2015-03-17 10:27:40 +00:00
|
|
|
#include "lib/heap.h"
|
2015-03-13 17:36:10 +00:00
|
|
|
|
|
|
|
#define MAX_NUM 1000
|
|
|
|
#define SPECIAL_KEY -3213
|
|
|
|
|
|
|
|
#define MY_CMP(x, y) ((x) < (y))
|
|
|
|
|
2015-03-23 16:40:13 +00:00
|
|
|
#define MY_HEAP_SWAP(heap,a,b,t) \
|
|
|
|
do { \
|
|
|
|
bt_debug("swap(%d %d) ", a, b); \
|
|
|
|
HEAP_SWAP(heap,a,b,t); \
|
|
|
|
} while(0)
|
|
|
|
|
2015-03-13 17:36:10 +00:00
|
|
|
static int num;
|
|
|
|
static int heap[MAX_NUM+1];
|
|
|
|
|
2015-03-27 13:08:39 +00:00
|
|
|
/*
|
|
|
|
* A valid heap must follow these rules:
|
|
|
|
* - `num >= 0`
|
|
|
|
* - `heap[i] >= heap[i / 2]` for each `i` in `[2, num]`
|
|
|
|
*/
|
2015-03-13 17:36:10 +00:00
|
|
|
static int
|
2015-03-27 13:08:39 +00:00
|
|
|
is_heap_valid(int heap[], int num)
|
2015-03-13 17:36:10 +00:00
|
|
|
{
|
2015-03-27 13:08:39 +00:00
|
|
|
int i;
|
2015-03-13 17:36:10 +00:00
|
|
|
|
2015-03-27 13:08:39 +00:00
|
|
|
if (num < 0)
|
2015-03-13 17:36:10 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-03-27 13:08:39 +00:00
|
|
|
for (i = 2; i <= num; i++)
|
|
|
|
if (heap[i] < heap[i / 2])
|
2015-03-13 17:36:10 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:08:39 +00:00
|
|
|
static void
|
|
|
|
show_heap(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
bt_debug("\n");
|
|
|
|
bt_debug("numbers %d; ", num);
|
|
|
|
for (i = 0; i <= num; i++)
|
|
|
|
bt_debug("%d ", heap[i]);
|
|
|
|
bt_debug(is_heap_valid(heap, num) ? "OK" : "NON-VALID HEAP!");
|
|
|
|
bt_debug("\n");
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:36:10 +00:00
|
|
|
static void
|
|
|
|
init_heap(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
num = 0;
|
|
|
|
heap[0] = SPECIAL_KEY; /* heap[0] should be unused */
|
2015-03-27 13:08:39 +00:00
|
|
|
for (i = 1; i <= MAX_NUM; i++)
|
2015-03-13 17:36:10 +00:00
|
|
|
heap[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
t_heap_insert(void)
|
|
|
|
{
|
2015-03-27 13:08:39 +00:00
|
|
|
int i;
|
|
|
|
|
2015-03-13 17:36:10 +00:00
|
|
|
init_heap();
|
|
|
|
|
2015-03-27 13:08:39 +00:00
|
|
|
for (i = MAX_NUM; i >= 1; i--)
|
2015-03-13 17:36:10 +00:00
|
|
|
{
|
2015-03-27 13:08:39 +00:00
|
|
|
bt_debug("ins %d at pos %d ", i, MAX_NUM - i);
|
|
|
|
heap[MAX_NUM - i + 1] = i;
|
2015-03-23 16:40:13 +00:00
|
|
|
HEAP_INSERT(heap, ++num, int, MY_CMP, MY_HEAP_SWAP);
|
2015-03-27 13:08:39 +00:00
|
|
|
show_heap();
|
2015-03-13 17:36:10 +00:00
|
|
|
bt_assert(is_heap_valid(heap, num));
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
t_heap_increase_decrease(void)
|
|
|
|
{
|
2015-03-27 13:08:39 +00:00
|
|
|
int i;
|
|
|
|
|
2015-03-13 17:36:10 +00:00
|
|
|
t_heap_insert();
|
|
|
|
|
2015-03-27 13:08:39 +00:00
|
|
|
for (i = 1; i <= MAX_NUM; i++)
|
2015-03-13 17:36:10 +00:00
|
|
|
{
|
2015-03-27 13:08:39 +00:00
|
|
|
if (i > heap[i])
|
2015-03-13 17:36:10 +00:00
|
|
|
{
|
|
|
|
bt_debug("inc %d ", i);
|
|
|
|
heap[i] = i;
|
2015-03-23 16:40:13 +00:00
|
|
|
HEAP_INCREASE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
2015-03-13 17:36:10 +00:00
|
|
|
}
|
|
|
|
else if (i < heap[i])
|
|
|
|
{
|
|
|
|
bt_debug("dec %d ", i);
|
|
|
|
heap[i] = i;
|
2015-03-23 16:40:13 +00:00
|
|
|
HEAP_INCREASE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
2015-03-13 17:36:10 +00:00
|
|
|
}
|
2015-03-27 13:08:39 +00:00
|
|
|
show_heap();
|
2015-03-13 17:36:10 +00:00
|
|
|
bt_assert(is_heap_valid(heap, num));
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
t_heap_delete(void)
|
|
|
|
{
|
2015-03-27 13:08:39 +00:00
|
|
|
int i;
|
|
|
|
|
2015-03-13 17:36:10 +00:00
|
|
|
t_heap_insert();
|
|
|
|
|
2015-03-27 13:08:39 +00:00
|
|
|
for (i = 1; i <= num; i++)
|
2015-03-13 17:36:10 +00:00
|
|
|
{
|
|
|
|
bt_debug("del at pos %d ", i);
|
2015-03-23 16:40:13 +00:00
|
|
|
HEAP_DELETE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
2015-03-27 13:08:39 +00:00
|
|
|
show_heap();
|
2015-03-13 17:36:10 +00:00
|
|
|
bt_assert(is_heap_valid(heap, num));
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
t_heap_0(void)
|
|
|
|
{
|
|
|
|
init_heap();
|
|
|
|
t_heap_insert();
|
|
|
|
t_heap_increase_decrease();
|
|
|
|
t_heap_delete();
|
|
|
|
|
|
|
|
return (heap[0] == SPECIAL_KEY) ? BT_SUCCESS : BT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:08:39 +00:00
|
|
|
static int
|
|
|
|
t_heap_insert_random(void)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int expected[MAX_NUM+1];
|
|
|
|
|
|
|
|
init_heap();
|
|
|
|
|
|
|
|
for (i = 1; i <= MAX_NUM; i++)
|
|
|
|
{
|
2015-08-27 07:25:28 +00:00
|
|
|
heap[i] = expected[i] = bt_random();
|
2015-03-27 13:08:39 +00:00
|
|
|
HEAP_INSERT(heap, ++num, int, MY_CMP, MY_HEAP_SWAP);
|
|
|
|
show_heap();
|
|
|
|
bt_assert(is_heap_valid(heap, num));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i <= MAX_NUM; i++)
|
|
|
|
for (j = 1; j <= MAX_NUM; j++)
|
|
|
|
if(expected[i] == heap[j])
|
|
|
|
break;
|
|
|
|
else if (j == MAX_NUM)
|
|
|
|
{
|
|
|
|
show_heap();
|
|
|
|
bt_abort_msg("Did not find a number %d in heap.", expected[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:36:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
bt_init(argc, argv);
|
|
|
|
|
2015-04-13 08:42:10 +00:00
|
|
|
bt_test_suite(t_heap_insert, "Inserting a descending sequence of numbers (the worst case)");
|
|
|
|
bt_test_suite(t_heap_insert_random, "Inserting pseudo-random numbers");
|
|
|
|
bt_test_suite(t_heap_increase_decrease, "Increasing/Decreasing");
|
|
|
|
bt_test_suite(t_heap_delete, "Deleting");
|
|
|
|
bt_test_suite(t_heap_0, "Is a heap[0] really unused?");
|
2015-03-13 17:36:10 +00:00
|
|
|
|
2015-04-13 08:52:21 +00:00
|
|
|
return bt_end();
|
2015-03-13 17:36:10 +00:00
|
|
|
}
|