varch/test/test_command.c

215 lines
5.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_command)
#include <varch/command.h>
#include <varch/unitt.h>
#else
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#endif
/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/
// #define EXIT_TEST
extern uint64_t unitt_clock(void);
static int cb2(int argc, char *argv[])
{
return 2;
}
static int cb3(int argc, char *argv[])
{
return 3;
}
static int cb4(int argc, char *argv[])
{
return 4;
}
static int test_0(void)
{
static char cbuffer[] = "cb0";
for (int i = 2; i <= 4; i++)
{
cbuffer[2] = '0' + i;
if (i != command(cbuffer))
{
#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),
};
static UNITT suites[] = {
{ "command suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
};
UNITT_EXE(suites);
}
/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/
static int func1(int argc, char *argv[])
{
printf("I am func1!\r\n");
printf("argc = %d\r\n", argc);
for (int i = 0; i < argc; i++)
{
printf("argv[%d] = %s\r\n", i, argv[i]);
}
return 1;
}
static int func2(int argc, char *argv[])
{
printf("I am func2!\r\n");
return 1;
}
static int func3(int argc, char *argv[])
{
printf("I am func3!\r\n");
return 1;
}
static void test_base(void)
{
command_export("func1", func1);
command_export("func2", func2);
command_export("func3", func3);
command("cmd -nl");
printf("--------------------------\r\n");
command("cmd -ln");
printf("--------------------------\r\n");
// command("func2");
// printf("--------------------------\r\n");
// command("func3");
// printf("--------------------------\r\n");
// command("func1 1 2 3");
// printf("--------------------------\r\n");
// command("func1 1\\ 2 3");
// printf("--------------------------\r\n");
// command("func1 \"1 2 3\"");
}
/************************************************************************************/
/************************************* Command ************************************/
/************************************************************************************/
static void usage(void)
{
printf(
"Usage: command [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"
);
}
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("command version %d.%d.%d\r\n", COMMAND_V_MAJOR, COMMAND_V_MINOR, COMMAND_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"))
{
command_export("cb2", cb2);
command_export("cb3", cb3);
command_export("cb4", cb4);
#if defined(TEST_TARGET_command)
while (1)
{
unitt_task();
usleep(1000 * ut_period);
}
#else
printf("create task %d\r\n", task_create(ut_period, unitt_task));
#endif
}
}
else
{
test_base();
}
return 0;
}
/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/
#if defined(TEST_TARGET_command)
int main(int argc, char *argv[])
{
return test(argc, argv);
}
#else
void test_command(void)
{
command_export("command", test);
}
init_export_app(test_command);
#endif