mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-06 08:46:42 +08:00
266 lines
6.3 KiB
C
266 lines
6.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#if defined(TEST_TARGET_queue)
|
|
#include <varch/command.h>
|
|
#include <varch/unitt.h>
|
|
#include <varch/queue.h>
|
|
#else
|
|
#include "init.h"
|
|
#include "command.h"
|
|
#include "unitt.h"
|
|
#include "kern.h"
|
|
#include "queue.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 void test_create(void)
|
|
{
|
|
queue_t queue = queue(int, 10);
|
|
|
|
if (queue)
|
|
{
|
|
printf("queue create success!!!\r\n");
|
|
}
|
|
else
|
|
{
|
|
printf("[ERROR] queue create fail!!!\r\n");
|
|
}
|
|
|
|
_queue(queue);
|
|
}
|
|
|
|
static void test_push(void)
|
|
{
|
|
queue_t queue = queue(int, 10);
|
|
int i = 0;
|
|
|
|
for (i = 0; i < queue_capacity(queue); i++)
|
|
{
|
|
queue_push(queue, &i);
|
|
}
|
|
queue_pop(queue, NULL);
|
|
queue_pop(queue, NULL);
|
|
|
|
_queue(queue); // 成对使用,用完即删除
|
|
}
|
|
|
|
static void test_info(void)
|
|
{
|
|
queue_t queue = queue(int, 10);
|
|
int i = 0;
|
|
|
|
for (i = 0; i < queue_capacity(queue); i++)
|
|
{
|
|
queue_push(queue, &i);
|
|
}
|
|
queue_pop(queue, NULL);
|
|
queue_pop(queue, NULL);
|
|
printf("queue capacity=%d, size=%d, dsize=%d\r\n", queue_capacity(queue), queue_size(queue), queue_dsize(queue));
|
|
|
|
_queue(queue);
|
|
}
|
|
|
|
static void test_at(void)
|
|
{
|
|
queue_t queue = queue(int, 10);
|
|
int i = 0;
|
|
|
|
for (i = 0; i < queue_capacity(queue); i++)
|
|
{
|
|
queue_push(queue, &i);
|
|
}
|
|
queue_pop(queue, NULL);
|
|
queue_pop(queue, NULL);
|
|
for (i = 0; i < queue_size(queue); i++)
|
|
{
|
|
printf("queue[%d] = %d\r\n", i, queue_at(queue, int, i));
|
|
}
|
|
|
|
_queue(queue);
|
|
}
|
|
|
|
static void test_base(void)
|
|
{
|
|
queue_t queue = queue(int, 10);
|
|
int i = 0;
|
|
|
|
for (i = 0; i < queue_capacity(queue); i++)
|
|
{
|
|
queue_push(queue, &i);
|
|
}
|
|
queue_pop(queue, NULL);
|
|
queue_pop(queue, NULL);
|
|
printf("queue capacity=%d, size=%d, dsize=%d\r\n", queue_capacity(queue), queue_size(queue), queue_dsize(queue));
|
|
for (i = 0; i < queue_size(queue); i++)
|
|
{
|
|
printf("queue[%d] = %d\r\n", i, queue_at(queue, int, i));
|
|
}
|
|
|
|
_queue(queue);
|
|
}
|
|
|
|
/************************************************************************************/
|
|
/************************************* Command ************************************/
|
|
/************************************************************************************/
|
|
|
|
static void usage(void)
|
|
{
|
|
printf(
|
|
"Usage: queue [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 and pop functions\n"
|
|
" <info> Test info(size, capacity, data size) functions\n"
|
|
" <at> Test get data function\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("queue version %d.%d.%d\r\n", QUEUE_V_MAJOR, QUEUE_V_MINOR, QUEUE_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_queue)
|
|
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, "info"))
|
|
{
|
|
test_info();
|
|
}
|
|
else if (!strcmp(execute, "at"))
|
|
{
|
|
test_at();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
test_base();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/************************************************************************************/
|
|
/************************************ Test entry ************************************/
|
|
/************************************************************************************/
|
|
|
|
#if defined(TEST_TARGET_queue)
|
|
int main(int argc, char *argv[])
|
|
{
|
|
return test(argc, argv);
|
|
}
|
|
#else
|
|
void test_queue(void)
|
|
{
|
|
command_export("queue", test);
|
|
}
|
|
init_export_app(test_queue);
|
|
#endif
|
|
|
|
|