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

91 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_valloc)
#include <varch/command.h>
#include <varch/valloc.h>
#else
#include "init.h"
#include "command.h"
#include "valloc.h"
#endif
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());
}
static void usage(void)
{
printf(
"Usage: valloc [opt] [arg]\n"
"\n"
"options:\n"
" -h Print help\n"
" -v Print version\n"
"\n"
"argument:\n"
" <base> Test default read 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("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;
}
#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