mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-06 08:46:42 +08:00
258 lines
6.3 KiB
C
258 lines
6.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#if defined(TEST_TARGET_heap)
|
|
#include <varch/command.h>
|
|
#include <varch/unitt.h>
|
|
#include <varch/heap.h>
|
|
#else
|
|
#include "init.h"
|
|
#include "command.h"
|
|
#include "unitt.h"
|
|
#include "kern.h"
|
|
#include "heap.h"
|
|
#endif
|
|
|
|
/************************************************************************************/
|
|
/************************************* Unit Test ************************************/
|
|
/************************************************************************************/
|
|
|
|
// #define EXIT_TEST
|
|
extern uint64_t unitt_clock(void);
|
|
|
|
static int test_0(void)
|
|
{
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
if (0)
|
|
{
|
|
|
|
#if defined (EXIT_TEST)
|
|
exit(0);
|
|
#endif
|
|
return UNITT_E_FAIL;
|
|
}
|
|
}
|
|
|
|
return UNITT_E_OK;
|
|
}
|
|
|
|
static void unitt_task(void)
|
|
{
|
|
static UNITT_TCASE rand_tests[] = {
|
|
UNITT_TCASE(test_0),
|
|
// UNITT_TCASE(test_1),
|
|
// UNITT_TCASE(test_2),
|
|
};
|
|
|
|
static UNITT suites[] = {
|
|
{ "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
|
|
};
|
|
|
|
UNITT_EXE(suites);
|
|
}
|
|
|
|
/************************************************************************************/
|
|
/************************************* Base Test ************************************/
|
|
/************************************************************************************/
|
|
|
|
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_create(void)
|
|
{
|
|
heap_t heap = heap_create(sizeof(int), 11, heap_root_max);
|
|
|
|
if (heap)
|
|
{
|
|
printf("heap create success!!!\r\n");
|
|
}
|
|
else
|
|
{
|
|
printf("[ERROR] heap create fail!!!\r\n");
|
|
}
|
|
|
|
heap_delete(heap);
|
|
}
|
|
|
|
static void test_push(void)
|
|
{
|
|
heap_t heap = heap_create(sizeof(int), 11, heap_root_max);
|
|
int push = 0, top = 0;
|
|
|
|
push = 100; heap_push(heap, &push); heap_top(heap, &top); printf("top = %d\r\n", top);
|
|
push = 1; heap_push(heap, &push); heap_top(heap, &top); printf("top = %d\r\n", top);
|
|
push = 2; heap_push(heap, &push); heap_top(heap, &top); printf("top = %d\r\n", top);
|
|
push = 200; heap_push(heap, &push); heap_top(heap, &top); printf("top = %d\r\n", top);
|
|
push = -10; heap_push(heap, &push); heap_top(heap, &top); printf("top = %d\r\n", top);
|
|
|
|
heap_delete(heap);
|
|
}
|
|
|
|
static void test_pop(void)
|
|
{
|
|
heap_t heap = heap_create(sizeof(int), 11, heap_root_max);
|
|
int push = 0, pop = 0;
|
|
|
|
push = 100; heap_push(heap, &push);
|
|
push = 1; heap_push(heap, &push);
|
|
push = 2; heap_push(heap, &push);
|
|
push = 200; heap_push(heap, &push);
|
|
push = -10; heap_push(heap, &push);
|
|
|
|
while (heap_pop(heap, &pop))
|
|
{
|
|
printf("pop %d\r\n", pop);
|
|
}
|
|
|
|
heap_delete(heap);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/************************************************************************************/
|
|
/************************************* Command ************************************/
|
|
/************************************************************************************/
|
|
|
|
static void usage(void)
|
|
{
|
|
printf(
|
|
"Usage: heap [opt] [arg] ...\n"
|
|
"\n"
|
|
"options:\n"
|
|
" -e <execute> Specifies the function to execute, the default is the <base> test\n"
|
|
" <base> Test base function\n"
|
|
" <ut> Unit test\n"
|
|
" <create> Test create and delete functions\n"
|
|
" <push> Test push functions\n"
|
|
" <pop> Test pop functions\n"
|
|
" -h Print help\n"
|
|
" -v Print version\n"
|
|
" -u [<period>] Unit test period, unit ms, the default is 1000ms\n"
|
|
"\n"
|
|
|
|
);
|
|
}
|
|
|
|
static int test(int argc, char *argv[])
|
|
{
|
|
char *execute = NULL;
|
|
int ut_period = 1000;
|
|
|
|
/* reset getopt */
|
|
command_opt_init();
|
|
|
|
while (1)
|
|
{
|
|
int opt = command_getopt(argc, argv, "e:hvu::");
|
|
if (opt == -1) break;
|
|
|
|
switch (opt)
|
|
{
|
|
case 'u' :
|
|
if (command_optarg) ut_period = atoi(command_optarg);
|
|
break;
|
|
case 'e' :
|
|
execute = command_optarg;
|
|
break;
|
|
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;
|
|
}
|
|
}
|
|
|
|
if (execute)
|
|
{
|
|
if (!strcmp(execute, "base"))
|
|
{
|
|
test_base();
|
|
}
|
|
else if (!strcmp(execute, "ut"))
|
|
{
|
|
srand((unsigned int)time(NULL));
|
|
#if defined(TEST_TARGET_heap)
|
|
while (1)
|
|
{
|
|
unitt_task();
|
|
usleep(1000 * ut_period);
|
|
}
|
|
#else
|
|
printf("create task %d\r\n", task_create(ut_period, unitt_task));
|
|
#endif
|
|
}
|
|
else if (!strcmp(execute, "create"))
|
|
{
|
|
test_create();
|
|
}
|
|
else if (!strcmp(execute, "push"))
|
|
{
|
|
test_push();
|
|
}
|
|
else if (!strcmp(execute, "pop"))
|
|
{
|
|
test_pop();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
test_base();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/************************************************************************************/
|
|
/************************************ Test entry ************************************/
|
|
/************************************************************************************/
|
|
|
|
#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
|