#include "minheap-internal.h"
#include <time.h>
static void set_random_timeout(struct event *ev);
static void check_heap(struct min_heap *heap);
static void set_random_timeout(struct event *ev)
{
//srand( (unsigned)time( NULL ));
ev->ev_timeout.tv_sec = rand();
ev->ev_timeout.tv_usec = rand() & 0xfffff;
ev->ev_timeout_pos.min_heap_idx = -1;
}
static void check_heap(struct min_heap *heap)
{
unsigned i;
for (i = 1; i < heap->n; ++i) {
unsigned parent_idx = (i-1)/2;
if( !evutil_timercmp(&heap->p[i]->ev_timeout,&heap->p[parent_idx]->ev_timeout, >=) )
{
printf("evutil_timercmp(&heap->p[i]->ev_timeout,&heap->p[parent_idx]->ev_timeout, >=)\n");
}
}
}
int main(void)
{
struct min_heap heap;
struct event *inserted[1024];
struct event *e, *last_e,*tmp_e;
int i;
min_heap_ctor(&heap);
for (i = 0; i < 16; ++i) {
inserted[i] = malloc(sizeof(struct event));
set_random_timeout(inserted[i]);
min_heap_push(&heap, inserted[i]);
}
check_heap(&heap);
//tt_assert(min_heap_size(&heap) == 1024);
if( min_heap_size(&heap) == 16 )
{
printf("The min_heap_size(&heap) == 16 \n");
}
for(i=0,tmp_e = (heap.p)[0];tmp_e;i++,tmp_e = (heap.p)[i])
{
printf("The tmp_e->ev_timeout.tv_sec is %ld, tmp_e->ev_timeout.tv_usec is %ld\n",tmp_e->ev_timeout.tv_sec,tmp_e->ev_timeout.tv_usec);
printf("The tmp_e->ev_timeout_pos.min_heap_idx is %d\n",tmp_e->ev_timeout_pos.min_heap_idx);
}
#if 0
for (i = 0; i < 5; ++i) {
min_heap_erase(&heap, inserted[i]);
if (0 == (i % 3))
check_heap(&heap);
}
//tt_assert(min_heap_size(&heap) == 512);
if( min_heap_size(&heap) == 5 )
{
printf("The min_heap_size(&heap) == 5 \n");
}
#endif
last_e = min_heap_pop(&heap);
//printf("The last_e->ev_timeout.tv_sec is %ld, last_e->ev_timeout.tv_usec is %ld\n",last_e->ev_timeout.tv_sec,last_e->ev_timeout.tv_usec);
//printf("The last_e->ev_timeout_pos.min_heap_idx is %d\n",last_e->ev_timeout_pos.min_heap_idx);
//printf("The heap.n is %d heap.a is %d\n",heap.n,heap.a);
while (1) {
e = min_heap_pop(&heap);
if (!e)
break;
//tt_want(evutil_timercmp(&last_e->ev_timeout,
//&e->ev_timeout, <=));
printf("The e->ev_timeout.tv_sec is %ld, e->ev_timeout.tv_usec is %ld\n",e->ev_timeout.tv_sec,e->ev_timeout.tv_usec);
printf("The e->ev_timeout_pos.min_heap_idx is %d\n",e->ev_timeout_pos.min_heap_idx);
//printf("The heap.n is %d heap.a is %d\n",heap.n,heap.a);
if( !evutil_timercmp(&last_e->ev_timeout,&e->ev_timeout, <=) )
{
printf("evutil_timercmp(&last_e->ev_timeout,&e->ev_timeout, <=\n");
}
}
//tt_assert(min_heap_size(&heap) == 0);
if( min_heap_size(&heap) == 0 )
{
printf("The min_heap_size(&heap) == 0 \n");
}
for (i = 0; i < 16; ++i)
free(inserted[i]);
min_heap_dtor(&heap);
return 0 ;
}