mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-06 16:56:42 +08:00
250 lines
5.6 KiB
C
250 lines
5.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#if defined(TEST_TARGET_cQueue)
|
|
#include <varch/command.h>
|
|
#include <varch/unitt.h>
|
|
#include <varch/cQueue.h>
|
|
#else
|
|
#include "init.h"
|
|
#include "command.h"
|
|
#include "unitt.h"
|
|
#include "kern.h"
|
|
#include "cQueue.h"
|
|
#endif
|
|
|
|
/************************************************************************************/
|
|
/************************************* Unit Test ************************************/
|
|
/************************************************************************************/
|
|
|
|
// #define EXIT_TEST
|
|
extern uint64_t unitt_clock(void);
|
|
|
|
static int test_0(void)
|
|
{
|
|
static cQueue(unsigned int, 10) queue;
|
|
static int initFlag = 0;
|
|
static unsigned int start = 0, end = 0;
|
|
|
|
unsigned int data = 0;
|
|
unsigned int size = end - start;
|
|
|
|
if (!initFlag)
|
|
{
|
|
cQueue_init(queue);
|
|
initFlag = 1;
|
|
}
|
|
|
|
if (size != cQueue_size(queue)) return UNITT_E_FAIL;
|
|
if (size > 0)
|
|
{
|
|
data = rand() % cQueue_size(queue);
|
|
if (cQueue_at(queue, data) != (start + data)) return UNITT_E_FAIL;
|
|
}
|
|
|
|
printf("size %u\r\n", size);
|
|
|
|
if (rand() % 2)
|
|
{
|
|
if (cQueue_push(queue, end))
|
|
{
|
|
end++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (cQueue_pop(queue, data))
|
|
{
|
|
start++;
|
|
}
|
|
}
|
|
|
|
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[] = {
|
|
{ "cQueue suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
|
|
};
|
|
|
|
UNITT_EXE(suites);
|
|
}
|
|
|
|
/************************************************************************************/
|
|
/************************************* Base Test ************************************/
|
|
/************************************************************************************/
|
|
|
|
static void test_int(void)
|
|
{
|
|
#if 0
|
|
typedef struct
|
|
{
|
|
cQueue queue;
|
|
int data[10];
|
|
} intQueueType;
|
|
intQueueType intQueue;
|
|
#else
|
|
cQueue(int, 10) intQueue;
|
|
#endif
|
|
|
|
cQueue_init(intQueue);
|
|
|
|
for (int i = 0; i < cQueue_cap(intQueue); i++)
|
|
{
|
|
cQueue_push(intQueue, i);
|
|
}
|
|
|
|
printf("cQueue[5] = %d\r\n", cQueue_at(intQueue, 5));
|
|
|
|
while (cQueue_size(intQueue) > 0)
|
|
{
|
|
int data;
|
|
cQueue_pop(intQueue, data);
|
|
printf("cQueue_pop %d\r\n", data);
|
|
}
|
|
}
|
|
|
|
static void test_struct(void)
|
|
{
|
|
typedef struct
|
|
{
|
|
char *name;
|
|
int age;
|
|
} Stu;
|
|
// typedef struct
|
|
// {
|
|
// cQueue queue;
|
|
// Stu data[10];
|
|
// } StuQueueType;
|
|
// StuQueueType StuQueue;
|
|
cQueue(Stu, 10) StuQueue;
|
|
|
|
Stu s = {"Zhang", 18};
|
|
|
|
cQueue_init(StuQueue);
|
|
|
|
for (int i = 0; i < cQueue_cap(StuQueue); i++)
|
|
{
|
|
s.age = 18 + i;
|
|
cQueue_push(StuQueue, s);
|
|
}
|
|
|
|
while (cQueue_size(StuQueue) > 0)
|
|
{
|
|
cQueue_pop(StuQueue, s);
|
|
printf("cQueue_pop name: %s age %d\r\n", s.name, s.age);
|
|
}
|
|
}
|
|
|
|
static void test_base(void)
|
|
{
|
|
// test_int();
|
|
test_struct();
|
|
}
|
|
|
|
/************************************************************************************/
|
|
/************************************* Command ************************************/
|
|
/************************************************************************************/
|
|
|
|
static void usage(void)
|
|
{
|
|
printf(
|
|
"Usage: cQueue [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("cQueue version %d.%d.%d\r\n", CQUEUE_V_MAJOR, CQUEUE_V_MINOR, CQUEUE_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_cQueue)
|
|
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_cQueue)
|
|
int main(int argc, char *argv[])
|
|
{
|
|
return test(argc, argv);
|
|
}
|
|
#else
|
|
void test_cQueue(void)
|
|
{
|
|
command_export("cQueue", test);
|
|
}
|
|
init_export_app(test_cQueue);
|
|
#endif
|