mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-06 08:46:42 +08:00
304 lines
7.0 KiB
C
304 lines
7.0 KiB
C
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#if defined(TEST_TARGET_list)
|
||
#include <varch/command.h>
|
||
#include <varch/unitt.h>
|
||
#include <varch/list.h>
|
||
#include <varch/vector.h>
|
||
#include <varch/tool.h>
|
||
#else
|
||
#include "init.h"
|
||
#include "command.h"
|
||
#include "unitt.h"
|
||
#include "kern.h"
|
||
#include "list.h"
|
||
#include "vector.h"
|
||
#include "tool.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)
|
||
{
|
||
list_t list = list(int);
|
||
|
||
if (list)
|
||
{
|
||
printf("list create success!!!\r\n");
|
||
}
|
||
else
|
||
{
|
||
printf("[ERROR] list create fail!!!\r\n");
|
||
}
|
||
|
||
_list(list);
|
||
}
|
||
|
||
static void test_insert(void)
|
||
{
|
||
list_t list = list(int); // 定义并创建一个int型的list
|
||
int i = 0;
|
||
void *data = NULL;
|
||
|
||
/* 插入数据 */
|
||
for (i = 0; i < 6; i++)
|
||
{
|
||
data = list_push_back(list, &i); // 尾插 0~5
|
||
if (data) printf("insert %d\r\n", *(int*)data); // 插入后打印出来
|
||
}
|
||
|
||
_list(list); // 成对使用,用完即删除
|
||
}
|
||
|
||
static void test_erase(void)
|
||
{
|
||
list_t list = list(int); // 定义并创建一个int型的list
|
||
int i = 0;
|
||
|
||
/* 插入数据 */
|
||
for (i = 0; i < 6; i++)
|
||
{
|
||
list_push_back(list, &i); // 尾插 0~5
|
||
}
|
||
|
||
list_erase(list, 0, 2);
|
||
|
||
_list(list); // 成对使用,用完即删除
|
||
}
|
||
|
||
static void test_at(void)
|
||
{
|
||
list_t list = list(int);
|
||
int i = 0;
|
||
|
||
for (i = 0; i < 6; i++)
|
||
{
|
||
list_push_back(list, &i);
|
||
}
|
||
|
||
for (i = 0; i < 6; i++) // 正向遍历
|
||
{
|
||
printf("list[%d] = %d\r\n", i, list_at(list, int, i));
|
||
}
|
||
|
||
_list(list);
|
||
}
|
||
|
||
static void test_size(void)
|
||
{
|
||
list_t list = list(int);
|
||
int i = 5;
|
||
while (i--) list_push_back(list, NULL); // 尾插5个空数据,也就是只开辟空间,但不赋值
|
||
printf("size = %d, data size = %d\r\n", list_size(list), list_dsize(list));
|
||
_list(list);
|
||
}
|
||
|
||
static void test_base(void)
|
||
{
|
||
list_t list = list(int); // 定义并创建int型list
|
||
int i = 0;
|
||
|
||
for (i = 0; i < 10000; i++) list_push_back(list, NULL); // 依次插入 0 1 2
|
||
|
||
// 正向遍历用时
|
||
for (i = 0; i < list_size(list); i++)
|
||
{
|
||
list_at(list, int, i);
|
||
}
|
||
|
||
// 反向遍历用时
|
||
for (i = list_size(list) - 1; i >= 0; i--)
|
||
{
|
||
list_at(list, int, i);
|
||
}
|
||
|
||
// 随机访问用时
|
||
for (i = 0; i < list_size(list); i++)
|
||
{
|
||
list_at(list, int, rand()%list_size(list));
|
||
}
|
||
|
||
|
||
printf("size %d\r\n", list_size(list));
|
||
|
||
// 结束使用该list后就删除
|
||
_list(list);
|
||
|
||
vector_t vt = vector(int, 10000);
|
||
|
||
// 正向遍历用时
|
||
for (i = 0; i < vector_size(vt); i++)
|
||
{
|
||
vector_at(vt, int, i);
|
||
}
|
||
|
||
_vector(vt);
|
||
}
|
||
|
||
/************************************************************************************/
|
||
/************************************* Command ************************************/
|
||
/************************************************************************************/
|
||
|
||
static void usage(void)
|
||
{
|
||
printf(
|
||
"Usage: list [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"
|
||
" <insert> Test insert function\n"
|
||
" <erase> Test erase function\n"
|
||
" <at> Test get data function\n"
|
||
" <size> Test size 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("list version %d.%d.%d\r\n", LIST_V_MAJOR, LIST_V_MINOR, LIST_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_list)
|
||
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, "insert"))
|
||
{
|
||
test_insert();
|
||
}
|
||
else if (!strcmp(execute, "erase"))
|
||
{
|
||
test_erase();
|
||
}
|
||
else if (!strcmp(execute, "at"))
|
||
{
|
||
test_at();
|
||
}
|
||
else if (!strcmp(execute, "size"))
|
||
{
|
||
test_size();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
test_base();
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
/************************************************************************************/
|
||
/************************************ Test entry ************************************/
|
||
/************************************************************************************/
|
||
|
||
#if defined(TEST_TARGET_list)
|
||
int main(int argc, char *argv[])
|
||
{
|
||
return test(argc, argv);
|
||
}
|
||
#else
|
||
void test_list(void)
|
||
{
|
||
command_export("list", test);
|
||
}
|
||
init_export_app(test_list);
|
||
#endif
|