#include #include #include #if defined(TEST_TARGET_dList) #include #include #include #else #include "init.h" #include "command.h" #include "unitt.h" #include "kern.h" #include "dList.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) { dList *list = NULL; list = dList_create(); if (!list) { printf("dList_create Fail!\r\n"); return; } printf("dList_create Success!\r\n"); dList_delete(list); } static void test_set(void) { dList *list = NULL; int dataInt = 3; char *dataString = "Hello dList"; list = dList_create(); if (!list) { printf("dList_create Fail!\r\n"); return; } printf("dList_create Success!\r\n"); dList_set(list, &dataInt, sizeof(dataInt)); printf("list->data %d\r\n", dList_ref(list, int)); dList_set(list, dataString, strlen(dataString) + 1); printf("list->data %s\r\n", ((char *)(list->data))); dList_delete(list); } static void test_insert(void) { dList *list = NULL; for (int i = 0; i < 2; i++) { if (!dList_insert(&list, -1, &i, sizeof(i))) goto FAIL; } int i = 100; if (!dList_insert(&list, -1, &i, sizeof(i))) goto FAIL; dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } printf("------------\r\n"); dList_forEachReverse(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); } static void test_erase(void) { dList *list = NULL; for (int i = 0; i < 5; i++) { if (!dList_insert(&list, -1, &i, sizeof(i))) goto FAIL; } dList_erase(&list, 0, NULL); dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); } static void test_attach(void) { dList *list = NULL, *a = NULL; for (int i = 0; i < 5; i++) { if (!dList_pushBack(&list, &i, sizeof(i))) goto FAIL; } for (int i = 0; i < 3; i++) { if (!dList_pushBack(&a, &i, sizeof(i))) goto FAIL; } dList_attach(&list, -1, a); dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); } static void test_detach(void) { dList *list = NULL, *node = NULL; for (int i = 0; i < 10; i++) { if (!dList_insert(&list, -1, &i, sizeof(i))) goto FAIL; } #if 1 node = dList_detach(&list, 0, 3, NULL); if (!node) { printf("dList_detach fail\r\n"); } #endif dList_forEachForward(node, n) { printf("node data %d\r\n", dList_ref(n, int)); } dList_delete(node); dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); } static void test_push(void) { dList *list = NULL; for (int i = 0; i < 5; i++) { if (!dList_pushFront(&list, &i, sizeof(i))) goto FAIL; } for (int i = 0; i < 5; i++) { if (!dList_pushBack(&list, &i, sizeof(i))) goto FAIL; } dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); } static void test_pop(void) { dList *list = NULL; for (int i = 0; i < 5; i++) { if (!dList_pushFront(&list, &i, sizeof(i))) goto FAIL; } for (int i = 0; i < 5; i++) { if (!dList_pushBack(&list, &i, sizeof(i))) goto FAIL; } dList_popBack(&list); dList_popBack(&list); dList_popFront(&list); dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); } static void test_to(void) { dList *list = NULL, *node; for (int i = 0; i < 10; i++) { if (!dList_pushBack(&list, &i, sizeof(i))) goto FAIL; } node = dList_to(list, -6); if (!node) { printf("dList_to fail\r\n"); goto FAIL; } printf("dList_to data %d\r\n", dList_ref(node, int)); dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); } static void test_size(void) { dList *list = NULL; for (int i = 0; i < 10; i++) { if (!dList_pushBack(&list, &i, sizeof(i))) goto FAIL; } printf("size %d\r\n", dList_size(list)); printf("size %d\r\n", dList_size(dList_to(list, 3))); FAIL: dList_delete(list); } static void test_append(void) { dList *list = NULL, *ap = NULL; for (int i = 0; i < 10; i++) { if (!dList_pushBack(&list, &i, sizeof(i))) goto FAIL; if (!dList_pushBack(&ap, &i, sizeof(i))) goto FAIL; } if (!dList_append(list, &ap)) goto FAIL; printf("ap: %p\r\n", ap); dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); dList_delete(ap); } static void test_copy(void) { dList *list = NULL, *copy = NULL; for (int i = 0; i < 10; i++) { if (!dList_pushBack(&list, &i, sizeof(i))) goto FAIL; } copy = dList_copy(list, -5, -1); if (!copy) { printf("dList_copy fail\r\n"); } dList_forEachForward(copy, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); dList_delete(copy); } static void test_reverse(void) { dList *list = NULL; for (int i = 0; i < 10; i++) { if (!dList_pushBack(&list, &i, sizeof(i))) goto FAIL; } if (!dList_reverse(list, 1, 5)) { printf("dList_reverse fail\r\n"); } dList_forEachForward(list, n) { printf("data %d\r\n", dList_ref(n, int)); } FAIL: dList_delete(list); } static void test_base(void) { // test_create(); // test_set(); // test_insert(); // test_erase(); // test_attach(); // test_detach(); // test_push(); // test_pop(); test_to(); // test_size(); // test_append(); // test_copy(); // test_reverse(); } /************************************************************************************/ /************************************* Command ************************************/ /************************************************************************************/ static void usage(void) { printf( "Usage: dList [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 set function\n" " Test insert function\n" " Test erase function\n" " Test attach function\n" " Test detach function\n" " Test push function\n" " Test pop function\n" " Test locate to specify index function\n" " Test size function\n" " Test append function\n" " Test copy function\n" " Test reverse 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("dList version %d.%d.%d\r\n", DLIST_V_MAJOR, DLIST_V_MINOR, DLIST_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")) { #if defined(TEST_TARGET_dList) 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, "set")) { test_set(); } else if (!strcmp(execute, "insert")) { test_insert(); } else if (!strcmp(execute, "erase")) { test_erase(); } else if (!strcmp(execute, "attach")) { test_attach(); } else if (!strcmp(execute, "detach")) { test_detach(); } else if (!strcmp(execute, "push")) { test_push(); } else if (!strcmp(execute, "pop")) { test_pop(); } else if (!strcmp(execute, "to")) { test_to(); } else if (!strcmp(execute, "size")) { test_size(); } else if (!strcmp(execute, "append")) { test_append(); } else if (!strcmp(execute, "copy")) { test_copy(); } else if (!strcmp(execute, "reverse")) { test_reverse(); } } else { test_base(); } return 0; } /************************************************************************************/ /************************************ Test entry ************************************/ /************************************************************************************/ #if defined(TEST_TARGET_dList) int main(int argc, char *argv[]) { return test(argc, argv); } #else void test_dList(void) { command_export("dList", test); } init_export_app(test_dList); #endif