varch/test/test_valloc.c

158 lines
4.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_valloc)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/valloc.h>
#else
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "valloc.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_base(void)
{
void* p = NULL;
printf("malloc %p\r\n", malloc(0));
p = realloc(NULL, 100);
p = malloc(64);
p = malloc(50);
printf("realloc %p\r\n", realloc(NULL, 0));
printf("%d\r\n", *(int *)p);
free(p);
v_check_unfree();
printf("count = %d\r\n", v_check_count());
printf("used = %d\r\n", v_check_used());
}
/************************************************************************************/
/************************************* Command ************************************/
/************************************************************************************/
static void usage(void)
{
printf(
"Usage: valloc [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"
" -h Print help\n"
" -v Print version\n"
" -u [<period>] Unit test period, unit ms, the default is 1000ms\n"
"\n"
"argument:\n"
" <base> Test default read function\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("valloc version %d.%d.%d\r\n", VALLOC_V_MAJOR, VALLOC_V_MINOR, VALLOC_V_PATCH);
return 0;
case '?':
printf("Unknown option `%c`\r\n", command_optopt);
return -1;
case 'h' :
default:
usage();
return 0;
}
}
for (int index = command_optind; index < argc; index++)
{
if (!strcmp(argv[index], "base"))
{
test_base();
}
}
return 0;
}
/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/
#if defined(TEST_TARGET_valloc)
int main(int argc, char *argv[])
{
return test(argc, argv);
}
#else
void test_valloc(void)
{
command_export("valloc", test);
}
init_export_app(test_valloc);
#endif