#include #include #include #if defined(TEST_TARGET_set) #include #include #include #else #include "init.h" #include "command.h" #include "unitt.h" #include "kern.h" #include "set.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) { set_t set = set(int); if (set) { printf("set create success!!!\r\n"); } else { printf("[ERROR] set create fail!!!\r\n"); } _set(set); } static void test_insert(void) { set_t set = set(int); int i; for (i = 0; i < 100; i++) { set_insert(set, i, &i); } printf("size = %d, data size = %d\r\n", set_size(set), set_dsize(set)); i = -100; set_insert(set, i, &i); i = 1024; set_insert(set, i, &i); printf("set[6] = %d\r\n", set_at(set, int, 6)); printf("set[-100] = %d\r\n", set_at(set, int, -100)); printf("set[1024] = %d\r\n", set_at(set, int, 1024)); set_at(set, int, 6) = 11111; printf("set[6] = %d\r\n", set_at(set, int, 6)); _set(set); } static void test_at(void) { set_t set = set(int); int i; for (i = 0; i < 100; i++) { set_insert(set, i, &i); } i = -100; set_insert(set, i, &i); i = 1024; set_insert(set, i, &i); printf("set[6] = %d\r\n", set_at(set, int, 6)); printf("set[-100] = %d\r\n", set_at(set, int, -100)); printf("set[1024] = %d\r\n", set_at(set, int, 1024)); set_at(set, int, 6) = 11111; printf("set[6] = %d\r\n", set_at(set, int, 6)); _set(set); } static void test_find(void) { set_t set = set(int); int value; value = -100; set_insert(set, 20, &value); value = 1024; set_insert(set, -1, &value); printf("find 20 %d\r\n", set_find(set, 20)); printf("find 21 %d\r\n", set_find(set, 21)); _set(set); } static void test_size(void) { set_t set = set(int); int i; for (i = 0; i < 100; i++) { set_insert(set, i, &i); } printf("size = %d, data size = %d\r\n", set_size(set), set_dsize(set)); _set(set); } static void test_it(void) { set_t set = set(int); int i, index; void *data; i = -100; set_insert(set, i, &i); i = 1024; set_insert(set, i, &i); i = 0; set_insert(set, i, &i); i = 7; set_insert(set, i, &i); i = -2; set_insert(set, i, &i); i = -2; set_insert(set, i, &i); set_at(set, int, 3) = 100; set_it_init(set, SET_HEAD); i = set_size(set); while (i--) { data = set_it_get(set, &index); printf("set[%d] = %d\r\n", index, *(int *)data); } _set(set); } static void test_base(void) { // test_insert(); test_it(); } /************************************************************************************/ /************************************* Command ************************************/ /************************************************************************************/ static void usage(void) { printf( "Usage: set [opt] [arg] ...\n" "\n" "options:\n" " -e Specifies the function to execute, the default is the test\n" " Test base function\n" " Unit test\n" " Test create and delete functions\n" " Test insert and erase function\n" " Test get data function\n" " Test find function\n" " Test sizes function\n" " Test iterator function\n" " -h Print help\n" " -v Print version\n" " -u [] 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("set version %d.%d.%d\r\n", SET_V_MAJOR, SET_V_MINOR, SET_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_set) 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, "insert")) { test_insert(); } else if (!strcmp(execute, "at")) { test_at(); } else if (!strcmp(execute, "find")) { test_find(); } else if (!strcmp(execute, "size")) { test_size(); } else if (!strcmp(execute, "it")) { test_it(); } } else { test_base(); } return 0; } /************************************************************************************/ /************************************ Test entry ************************************/ /************************************************************************************/ #if defined(TEST_TARGET_set) int main(int argc, char *argv[]) { return test(argc, argv); } #else void test_set(void) { command_export("set", test); } init_export_app(test_set); #endif