mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2025-01-09 10:31:53 +00:00
Birdtest: Refactorize heap_test.c and lists_test.c
This commit is contained in:
parent
8edf5b99e8
commit
3c2ac3887e
116
lib/heap_test.c
116
lib/heap_test.c
@ -23,60 +23,61 @@
|
|||||||
static int num;
|
static int num;
|
||||||
static int heap[MAX_NUM+1];
|
static int heap[MAX_NUM+1];
|
||||||
|
|
||||||
#define SHOW_HEAP(heap) \
|
/*
|
||||||
do { \
|
* A valid heap must follow these rules:
|
||||||
uint _i; \
|
* - `num >= 0`
|
||||||
bt_debug("\nnum = %d; ", num); \
|
* - `heap[i] >= heap[i / 2]` for each `i` in `[2, num]`
|
||||||
for(_i = 1; _i <= num; _i++) \
|
*/
|
||||||
bt_debug("%d ", heap[_i]); \
|
|
||||||
if(is_heap_valid(heap, num)) \
|
|
||||||
bt_debug("OK \n"); \
|
|
||||||
else \
|
|
||||||
bt_debug("NON-VALID HEAP! \n"); \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
is_heap_valid(int heap[], uint num)
|
is_heap_valid(int heap[], int num)
|
||||||
{
|
{
|
||||||
/*
|
int i;
|
||||||
* A valid heap must follow these rules:
|
|
||||||
* - `num >= 0`
|
|
||||||
* - `heap[i] >= heap[i / 2]` for each `i` in `[2, num]`
|
|
||||||
*/
|
|
||||||
|
|
||||||
if(num < 0)
|
if (num < 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int i;
|
for (i = 2; i <= num; i++)
|
||||||
for(i = 2; i <= num; i++)
|
if (heap[i] < heap[i / 2])
|
||||||
if(heap[i] < heap[i / 2])
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
init_heap(void)
|
init_heap(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
num = 0;
|
num = 0;
|
||||||
heap[0] = SPECIAL_KEY; /* heap[0] should be unused */
|
heap[0] = SPECIAL_KEY; /* heap[0] should be unused */
|
||||||
for(i = 1; i <= MAX_NUM; i++)
|
for (i = 1; i <= MAX_NUM; i++)
|
||||||
heap[i] = 0;
|
heap[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
t_heap_insert(void)
|
t_heap_insert(void)
|
||||||
{
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
init_heap();
|
init_heap();
|
||||||
|
|
||||||
int i;
|
for (i = MAX_NUM; i >= 1; i--)
|
||||||
for(i = 1; i <= MAX_NUM; i++)
|
|
||||||
{
|
{
|
||||||
bt_debug("ins %d at pos %d ", MAX_NUM - i, i);
|
bt_debug("ins %d at pos %d ", i, MAX_NUM - i);
|
||||||
heap[i] = MAX_NUM - i;
|
heap[MAX_NUM - i + 1] = i;
|
||||||
HEAP_INSERT(heap, ++num, int, MY_CMP, MY_HEAP_SWAP);
|
HEAP_INSERT(heap, ++num, int, MY_CMP, MY_HEAP_SWAP);
|
||||||
SHOW_HEAP(heap);
|
show_heap();
|
||||||
bt_assert(is_heap_valid(heap, num));
|
bt_assert(is_heap_valid(heap, num));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,13 +87,13 @@ t_heap_insert(void)
|
|||||||
static int
|
static int
|
||||||
t_heap_increase_decrease(void)
|
t_heap_increase_decrease(void)
|
||||||
{
|
{
|
||||||
init_heap();
|
int i;
|
||||||
|
|
||||||
t_heap_insert();
|
t_heap_insert();
|
||||||
|
|
||||||
int i;
|
for (i = 1; i <= MAX_NUM; i++)
|
||||||
for(i = 1; i <= MAX_NUM; i++)
|
|
||||||
{
|
{
|
||||||
if(i > heap[i])
|
if (i > heap[i])
|
||||||
{
|
{
|
||||||
bt_debug("inc %d ", i);
|
bt_debug("inc %d ", i);
|
||||||
heap[i] = i;
|
heap[i] = i;
|
||||||
@ -104,7 +105,7 @@ t_heap_increase_decrease(void)
|
|||||||
heap[i] = i;
|
heap[i] = i;
|
||||||
HEAP_INCREASE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
HEAP_INCREASE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
||||||
}
|
}
|
||||||
SHOW_HEAP(heap);
|
show_heap();
|
||||||
bt_assert(is_heap_valid(heap, num));
|
bt_assert(is_heap_valid(heap, num));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,16 +115,15 @@ t_heap_increase_decrease(void)
|
|||||||
static int
|
static int
|
||||||
t_heap_delete(void)
|
t_heap_delete(void)
|
||||||
{
|
{
|
||||||
init_heap();
|
|
||||||
t_heap_insert();
|
|
||||||
t_heap_increase_decrease();
|
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for(i = 1; i <= num; i++)
|
|
||||||
|
t_heap_insert();
|
||||||
|
|
||||||
|
for (i = 1; i <= num; i++)
|
||||||
{
|
{
|
||||||
bt_debug("del at pos %d ", i);
|
bt_debug("del at pos %d ", i);
|
||||||
HEAP_DELETE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
HEAP_DELETE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
||||||
SHOW_HEAP(heap);
|
show_heap();
|
||||||
bt_assert(is_heap_valid(heap, num));
|
bt_assert(is_heap_valid(heap, num));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,15 +141,45 @@ t_heap_0(void)
|
|||||||
return (heap[0] == SPECIAL_KEY) ? BT_SUCCESS : BT_FAILURE;
|
return (heap[0] == SPECIAL_KEY) ? BT_SUCCESS : BT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
t_heap_insert_random(void)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
int expected[MAX_NUM+1];
|
||||||
|
|
||||||
|
init_heap();
|
||||||
|
|
||||||
|
for (i = 1; i <= MAX_NUM; i++)
|
||||||
|
{
|
||||||
|
heap[i] = expected[i] = bt_rand_num();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
bt_init(argc, argv);
|
bt_init(argc, argv);
|
||||||
|
|
||||||
bt_test_case(t_heap_insert, "Test Inserting");
|
bt_test_case(t_heap_insert, "Inserting a descending sequence of numbers (the worst case)");
|
||||||
bt_test_case(t_heap_increase_decrease, "Test Increasing/Decreasing");
|
bt_test_case(t_heap_insert_random, "Inserting pseudo-random numbers");
|
||||||
bt_test_case(t_heap_delete, "Test Deleting");
|
bt_test_case(t_heap_increase_decrease, "Increasing/Decreasing");
|
||||||
bt_test_case(t_heap_0, "Is heap[0] unused?");
|
bt_test_case(t_heap_delete, "Deleting");
|
||||||
|
bt_test_case(t_heap_0, "Is a heap[0] really unused?");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ is_filled_list_well_linked(void)
|
|||||||
|
|
||||||
for (i = 0; i < MAX_NUM; i++)
|
for (i = 0; i < MAX_NUM; i++)
|
||||||
{
|
{
|
||||||
if(i < (MAX_NUM-1))
|
if (i < (MAX_NUM-1))
|
||||||
bt_assert(nodes[i].next == &nodes[i+1]);
|
bt_assert(nodes[i].next == &nodes[i+1]);
|
||||||
|
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
@ -191,7 +191,7 @@ t_remove_node(void)
|
|||||||
|
|
||||||
/* Fill & Remove & Check */
|
/* Fill & Remove & Check */
|
||||||
fill_list();
|
fill_list();
|
||||||
for(i = 0; i < MAX_NUM; i++)
|
for (i = 0; i < MAX_NUM; i++)
|
||||||
rem2_node(&nodes[i]);
|
rem2_node(&nodes[i]);
|
||||||
bt_assert(is_empty_list_well_unlinked());
|
bt_assert(is_empty_list_well_unlinked());
|
||||||
|
|
||||||
@ -223,12 +223,11 @@ t_remove_node(void)
|
|||||||
static int
|
static int
|
||||||
t_replace_node(void)
|
t_replace_node(void)
|
||||||
{
|
{
|
||||||
init_list_();
|
|
||||||
show_list();
|
|
||||||
fill_list();
|
|
||||||
|
|
||||||
node head, inside, tail;
|
node head, inside, tail;
|
||||||
|
|
||||||
|
init_list_();
|
||||||
|
fill_list();
|
||||||
|
|
||||||
replace_node(&nodes[0], &head);
|
replace_node(&nodes[0], &head);
|
||||||
bt_assert(l.head == &head);
|
bt_assert(l.head == &head);
|
||||||
bt_assert(head.prev == NODE &l.head);
|
bt_assert(head.prev == NODE &l.head);
|
||||||
|
Loading…
Reference in New Issue
Block a user