varch/test/test_vector.c

264 lines
6.6 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <string.h>
#if defined(TEST_TARGET_vector)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/vector.h>
#else
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "vector.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)
{
vector_t vector = vector(int, 16); // 创建长度为16的int型vector
int array[16];
if (vector)
{
printf("vector create success!!!\r\n");
}
else
{
printf("[ERROR] vector create fail!!!\r\n");
}
_vector(vector);
}
static void test_at(void)
{
vector_t vector = vector(int, 8);; // 定义并创建长度为8的int型vector
int i = 0;
for (i = 0; i < 8; i++)
{
vector_at(vector, int, i) = i; // 使用at方法访问
}
for (i = 0; i < 8; i++)
{
printf("vector[%d] = %d\r\n", i, v2a(vector, int)[i]); // 使用下标访问
}
_vector(vector); // 用完即删除
}
static void test_size(void)
{
vector_t vector = vector(int, 10);
printf("size=%d, capacity=%d\r\n", vector_size(vector), vector_capacity(vector));
_vector(vector);
}
static void test_resize(void)
{
vector_t vector = vector(int, 10);
printf("size=%d\r\n", vector_size(vector));
vector_resize(vector, 32);
printf("size=%d\r\n", vector_size(vector));
vector_resize(vector, 14);
printf("size=%d\r\n", vector_size(vector));
_vector(vector);
}
static void test_insert(void)
{
vector_t vector = vector(int, 0); // 创建0长度的vector容器
int array[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
printf("insert:\r\n");
vector_insert(vector, 0, array, 10); // 把array插入到vector当中相当于用array给vector赋值
for (i = 0; i < vector_size(vector); i++)
{
printf("vector[%d]=%d\r\n", i, v2a(vector, int)[i]);
}
printf("erase:\r\n");
vector_erase(vector, 5, 2); // 移除索引5后面的两个数据也就是索引5和索引6
for (i = 0; i < vector_size(vector); i++)
{
printf("vector[%d]=%d\r\n", i, v2a(vector, int)[i]);
}
_vector(vector);
}
static void test_base(void)
{
// test_resize();
test_at();
}
/************************************************************************************/
/************************************* Command ************************************/
/************************************************************************************/
static void usage(void)
{
printf(
"Usage: vector [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"
" <at> Test get data function\n"
" <size> Test get size and capacity function\n"
" <resize> Test resize function\n"
" <insert> Test insert and erase 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("vector version %d.%d.%d\r\n", VECTOR_V_MAJOR, VECTOR_V_MINOR, VECTOR_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_vector)
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, "at"))
{
test_at();
}
else if (!strcmp(execute, "size"))
{
test_size();
}
else if (!strcmp(execute, "resize"))
{
test_resize();
}
else if (!strcmp(execute, "insert"))
{
test_insert();
}
}
else
{
test_base();
}
return 0;
}
/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/
#if defined(TEST_TARGET_vector)
int main(int argc, char *argv[])
{
return test(argc, argv);
}
#else
void test_vector(void)
{
command_export("vector", test);
}
init_export_app(test_vector);
#endif