varch/test/test_list.c
2024-11-16 13:36:54 +08:00

116 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_list)
#include <varch/command.h>
#include <varch/list.h>
#include <varch/vector.h>
#include <varch/tool.h>
#else
#include "init.h"
#include "command.h"
#include "list.h"
#include "vector.h"
#include "tool.h"
#endif
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);
}
static void usage(void)
{
printf(
"Usage: list [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("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;
}
}
test_base();
return 0;
}
#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