mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-08 01:36:42 +08:00
105 lines
2.0 KiB
C
105 lines
2.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#if defined(TEST_TARGET_heap)
|
|
#include <varch/command.h>
|
|
#include <varch/heap.h>
|
|
#else
|
|
#include "init.h"
|
|
#include "command.h"
|
|
#include "heap.h"
|
|
#endif
|
|
|
|
static int heap_root_min(void *parent, void *child)
|
|
{
|
|
if (*(int *)parent < *(int *)child) return 1;
|
|
return 0;
|
|
}
|
|
static int heap_root_max(void *parent, void *child)
|
|
{
|
|
if (*(int *)parent > *(int *)child) return 1;
|
|
return 0;
|
|
}
|
|
static void test_base(void)
|
|
{
|
|
heap_t h = heap_create(sizeof(int), 11, heap_root_max);
|
|
int i = 0;
|
|
|
|
for (i = 0; i < 11; i++)
|
|
{
|
|
heap_push(h, &i);
|
|
}
|
|
printf("size %d\r\n", heap_size(h));
|
|
|
|
heap_pop(h, NULL);
|
|
i = -9;heap_modify(h, 6, &i);
|
|
i = -100;heap_modify(h, 3, &i);
|
|
i = 1000;heap_push(h, &i);
|
|
|
|
while (heap_size(h))
|
|
{
|
|
if (heap_pop(h, &i)) printf("pop %d\r\n", i);
|
|
else printf("pop fail!\r\n");
|
|
}
|
|
|
|
heap_delete(h);
|
|
}
|
|
|
|
static void usage(void)
|
|
{
|
|
printf(
|
|
"Usage: heap [opt] [arg]\n"
|
|
"\n"
|
|
"options:\n"
|
|
" -h Print help\n"
|
|
" -v Print version\n"
|
|
"\n"
|
|
"argument:\n"
|
|
" <read> Test default read function\n"
|
|
" <write> Test default write function\n"
|
|
);
|
|
}
|
|
|
|
static int test(int argc, char *argv[])
|
|
{
|
|
/* reset getopt */
|
|
command_opt_init();
|
|
|
|
while (1)
|
|
{
|
|
int opt = command_getopt(argc, argv, "hv");
|
|
if (opt == -1) break;
|
|
|
|
switch (opt)
|
|
{
|
|
case 'v' :
|
|
printf("heap version %d.%d.%d\r\n", HEAP_V_MAJOR, HEAP_V_MINOR, HEAP_V_PATCH);
|
|
return 0;
|
|
case '?':
|
|
printf("Unknown option `%c`\r\n", command_optopt);
|
|
return -1;
|
|
case 'h' :
|
|
default:
|
|
usage();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
test_base();
|
|
|
|
return 0;
|
|
}
|
|
|
|
#if defined(TEST_TARGET_heap)
|
|
int main(int argc, char *argv[])
|
|
{
|
|
return test(argc, argv);
|
|
}
|
|
#else
|
|
void test_heap(void)
|
|
{
|
|
command_export("heap", test);
|
|
}
|
|
init_export_app(test_heap);
|
|
#endif
|