From e440a96e997ab177b0732149877d43631516842b Mon Sep 17 00:00:00 2001 From: Lamdonn Date: Sat, 16 Nov 2024 13:36:54 +0800 Subject: [PATCH] Update test framework --- makefile | 10 ++- source/01_general/calculate.h | 8 +-- source/01_general/command.c | 34 +++++---- source/01_general/command.h | 8 +-- source/01_general/fsm.h | 2 +- source/01_general/kern.h | 2 +- source/01_general/oscp.h | 2 +- source/03_container/dict.h | 2 +- source/04_algorithm/encrypt.h | 10 ++- source/04_algorithm/hash.h | 4 ++ source/06_performance/cpul.h | 2 +- source/06_performance/ramt.h | 2 +- source/06_performance/romt.h | 2 +- source/06_performance/unitt.h | 2 +- test/test.mk | 98 ++++++++++++-------------- test/test_arg.c | 62 +++++++++++++++- test/test_cQueue.c | 64 +++++++++++++++-- test/test_calculate.c | 69 ++++++++++++++++-- test/test_check.c | 64 +++++++++++++++-- test/test_command.c | 62 ++++++++++++++-- test/test_cpul.c | 62 +++++++++++++++- test/test_crc.c | 64 +++++++++++++++-- test/test_csv.c | 64 +++++++++++++++-- test/test_dList.c | 66 +++++++++++++++-- test/test_date.c | 62 +++++++++++++++- test/test_deque.c | 69 ++++++++++++++++-- test/test_dict.c | 70 +++++++++++++++--- test/test_encrypt.c | 69 ++++++++++++++++-- test/test_filter.c | 65 +++++++++++++++-- test/test_graph.c | 67 +++++++++++++++--- test/test_hash.c | 64 +++++++++++++++-- test/test_heap.c | 69 ++++++++++++++++-- test/test_ini.c | 64 +++++++++++++++-- test/test_init.c | 63 +++++++++++++++++ test/test_install.c | 31 -------- test/test_intl.c | 62 +++++++++++++++- test/test_json.c | 70 +++++++++++++++--- test/test_kern.c | 85 ++++++++++++++++++---- test/test_list.c | 88 +++++++++++++++++------ test/test_map.c | 74 ++++++++++++++++--- test/test_oscp.c | 75 +++++++++++++++++--- test/test_pid.c | 67 ++++++++++++++++-- test/test_pthread.c | 32 --------- test/test_queue.c | 71 +++++++++++++++++-- test/test_ramt.c | 85 ++++++++++++++++++++-- test/test_rbtree.c | 25 ------- test/test_sList.c | 66 +++++++++++++++-- test/test_search.c | 65 +++++++++++++++-- test/test_set.c | 71 ++++++++++++++++--- test/test_sort.c | 66 +++++++++++++++-- test/test_stack.c | 70 ++++++++++++++++-- test/test_str.c | 64 +++++++++++++++-- test/test_tool.c | 63 ++++++++++++++++- test/test_tree.c | 69 +++++++++++++++--- test/test_txls.c | 102 ++++++++++++++++++++++----- test/test_unitt.c | 129 ++++++++++++++++++++++++---------- test/test_valloc.c | 72 ++++++++++++++++++- test/test_vector.c | 75 ++++++++++++++------ test/test_vlog.c | 122 +++++++++++++++++++++++++++++--- test/test_vstd.c | 28 ++++++-- test/test_xml.c | 79 +++++++++++++++++++-- 61 files changed, 2839 insertions(+), 494 deletions(-) delete mode 100644 test/test_install.c delete mode 100644 test/test_pthread.c delete mode 100644 test/test_rbtree.c diff --git a/makefile b/makefile index 97a8b9b..ef6a937 100644 --- a/makefile +++ b/makefile @@ -56,7 +56,7 @@ CFLAG += -std=$(CSTD) include $(TESTSPACE)/test.mk TESTSRC += $(APPLICATION_PATH)/main.c -TESTSRC += $(TEST_SRC) +TESTSRC += $(patsubst %,$(TESTSPACE)/test_%.c,$(TEST_LIST)) ifeq ($(OS),Windows_NT) UNAME := Windows @@ -96,6 +96,10 @@ $(OBJP)/%.o:%.c app:${TARP} +$(TEST_LIST): %: $(TESTSPACE)/test_%.c + @ mkdir -p $(BINP) + @ $(CC) $(CFLAG) -o $(BINP)/$@ $< -DTEST_TARGET_$@ $(LIBS) -l$(LIB) -lm + # lib lib:$(LIBO) @ $(AR) -crv $(LIBA) $(LIBO) @@ -114,6 +118,9 @@ help: @echo "" @echo " lib - Build the library ($(LIB))" @echo "" + @echo " [test] - Build test app, and link to $(LIB) lib" + @echo " Test target: $(TEST_LIST)" + @echo "" @echo " run - Execute the default application" @echo "" @echo " install - Install the library" @@ -127,6 +134,7 @@ help: @echo " clean - Remove build artifacts" @echo "" @echo " help - Show this help message" + @echo "" .PHONY:clean diff --git a/source/01_general/calculate.h b/source/01_general/calculate.h index aa8603c..e7db5ad 100644 --- a/source/01_general/calculate.h +++ b/source/01_general/calculate.h @@ -14,12 +14,12 @@ #define __calculate_H /* Version infomation */ -#define CALCULATE_V_MAJOR 1 -#define CALCULATE_V_MINOR 0 -#define CALCULATE_V_REVISE 0 +#define CALCULATE_V_MAJOR 1 +#define CALCULATE_V_MINOR 0 +#define CALCULATE_V_PATCH 0 /* Configuration information */ -#define CALCULATE_EXFUNC_MAX 32 /**< The maximum extern function count supported */ +#define CALCULATE_EXFUNC_MAX 32 /**< The maximum extern function count supported */ /** * \brief enter a calculation expression to calculate the result, support 'in_function_table[]' table operation function diff --git a/source/01_general/command.c b/source/01_general/command.c index b3ba177..7b6cf25 100644 --- a/source/01_general/command.c +++ b/source/01_general/command.c @@ -44,18 +44,18 @@ static struct COMMAND base[COMMAND_COUNT_MAX] = static void usage(void) { printf( -"Usage:\n" +"Usage: cmd [opt] [arg]\n" "Enter the command line to execute the corresponding command\n" "\n" -"OPTIONS\n" -"[-l] : Print currently supported commands\n" -"[-n] : Print the number of currently supported commands\n" -"[-h] : Print help\n" -"[-v] : Print version\n" -"[-c] : Print the configuration information of the current command module\n" -" `argc` : The maximum number of parameters supported for parsing in the input command\n" -" `line` : The maximum length supported for parsing in the input command\n" -" `count` : The maximum command count supported\n" +"options:\n" +" -l Print currently supported commands\n" +" -n Print the number of currently supported commands\n" +" -c Print the configuration information of the current command module\n" +" `argc` : The maximum number of parameters supported for parsing in the input command\n" +" `line` : The maximum length supported for parsing in the input command\n" +" `count` : The maximum command count supported\n" +" -h Print help\n" +" -v Print version\n" ); } @@ -75,7 +75,7 @@ static int cmd(int argc, char *argv[]) command_opt_init(); while (1) { - opt = command_getopt(argc, argv, "lnhvc:"); + opt = command_getopt(argc, argv, "lnc:hv"); if (opt == -1) break; switch (opt) @@ -90,12 +90,6 @@ static int cmd(int argc, char *argv[]) case 'n': printf("%d\r\n", command_num); break; - case 'h' : - usage(); - break; - case 'v' : - printf("command version %d.%d.%d\r\n", COMAMND_V_MAJOR, COMAMND_V_MINOR, COMAMND_V_PATCH); - break; case 'c' : if (!strcmp(command_optarg, "argc")) { @@ -114,12 +108,16 @@ static int cmd(int argc, char *argv[]) printf("no such optarg: %s\r\n", 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(); - break; + return 0; } } diff --git a/source/01_general/command.h b/source/01_general/command.h index 8777dbf..376c09f 100644 --- a/source/01_general/command.h +++ b/source/01_general/command.h @@ -15,14 +15,14 @@ #define __command_H /* Version infomation */ -#define COMAMND_V_MAJOR 1 -#define COMAMND_V_MINOR 0 -#define COMAMND_V_PATCH 0 +#define COMMAND_V_MAJOR 1 +#define COMMAND_V_MINOR 0 +#define COMMAND_V_PATCH 1 /* Configuration information */ #define COMMAND_ARGC_MAX 16 /**< The maximum number of parameters supported for parsing in the input command */ #define COMMAND_LINE_MAX 256 /**< The maximum length supported for parsing in the input command */ -#define COMMAND_COUNT_MAX 32 /**< The maximum command count supported */ +#define COMMAND_COUNT_MAX 128 /**< The maximum command count supported */ /* Return value of command module * Negative return values are used internally by the module diff --git a/source/01_general/fsm.h b/source/01_general/fsm.h index fef3705..4e39be1 100644 --- a/source/01_general/fsm.h +++ b/source/01_general/fsm.h @@ -15,7 +15,7 @@ /* Version infomation */ #define FSM_V_MAJOR 1 #define FSM_V_MINOR 0 -#define FSM_V_REVISE 0 +#define FSM_V_PATCH 0 /* State transition type definition */ typedef struct diff --git a/source/01_general/kern.h b/source/01_general/kern.h index cc00dd7..21cc5cc 100644 --- a/source/01_general/kern.h +++ b/source/01_general/kern.h @@ -16,7 +16,7 @@ /* Version infomation */ #define KERN_V_MAJOR 1 #define KERN_V_MINOR 0 -#define KERN_V_REVISE 0 +#define KERN_V_PATCH 0 /* task type */ typedef unsigned int task_t; diff --git a/source/01_general/oscp.h b/source/01_general/oscp.h index 37743af..0e54b43 100644 --- a/source/01_general/oscp.h +++ b/source/01_general/oscp.h @@ -16,7 +16,7 @@ /* Version infomation */ #define OSCP_V_MAJOR 1 #define OSCP_V_MINOR 0 -#define OSCP_V_REVISE 0 +#define OSCP_V_PATCH 0 /* Display resolution, which is the maximum horizontal value that a numerical value can display */ #define RESOLUTION 100 diff --git a/source/03_container/dict.h b/source/03_container/dict.h index 4e63845..9de2af6 100644 --- a/source/03_container/dict.h +++ b/source/03_container/dict.h @@ -18,7 +18,7 @@ /* Version infomation */ #define DICT_V_MAJOR 1 #define DICT_V_MINOR 0 -#define DICT_V_REVISE 0 +#define DICT_V_PATCH 0 /** Using dict error area * when there is an error in the read or write operation, diff --git a/source/04_algorithm/encrypt.h b/source/04_algorithm/encrypt.h index 6310033..fbbdcc3 100644 --- a/source/04_algorithm/encrypt.h +++ b/source/04_algorithm/encrypt.h @@ -32,8 +32,14 @@ #include #include -#define DES_ENCRYPT 0 -#define DES_DECRYPT 1 +/* Version infomation */ + +#define ENCRYPT_V_MAJOR 1 +#define ENCRYPT_V_MINOR 0 +#define ENCRYPT_V_PATCH 0 + +#define DES_ENCRYPT 0 +#define DES_DECRYPT 1 int des_set_key(const uint8_t key[8]); int des_crypt_ecb(const uint8_t input[8], uint8_t output[8], uint8_t mode); diff --git a/source/04_algorithm/hash.h b/source/04_algorithm/hash.h index f3bff01..88326d5 100644 --- a/source/04_algorithm/hash.h +++ b/source/04_algorithm/hash.h @@ -15,6 +15,10 @@ #include +#define HASH_V_MAJOR 1 +#define HASH_V_MINOR 0 +#define HASH_V_PATCH 0 + /* Hash algorithm declare * * All method interface types are consistent, and the external use methods are also consistent. diff --git a/source/06_performance/cpul.h b/source/06_performance/cpul.h index f9fdd9e..a018640 100644 --- a/source/06_performance/cpul.h +++ b/source/06_performance/cpul.h @@ -18,7 +18,7 @@ /* Version infomation */ #define CPUL_V_MAJOR 0 #define CPUL_V_MINOR 1 -#define CPUL_V_PATcH 0 +#define CPUL_V_PATCH 0 #define CPUL_GEN_BSIZE 4 diff --git a/source/06_performance/ramt.h b/source/06_performance/ramt.h index bb9a42d..161fc51 100644 --- a/source/06_performance/ramt.h +++ b/source/06_performance/ramt.h @@ -22,7 +22,7 @@ /* Version infomation */ #define RAMT_V_MAJOR 0 #define RAMT_V_MINOR 1 -#define RAMT_V_REVISE 0 +#define RAMT_V_PATCH 0 // Define different RAM test modes #define RAMT_MODE_NORMAL ((uint32_t)0x00000001) // Normal read/write test diff --git a/source/06_performance/romt.h b/source/06_performance/romt.h index fb5577f..b8e900b 100644 --- a/source/06_performance/romt.h +++ b/source/06_performance/romt.h @@ -20,7 +20,7 @@ /* Version infomation */ #define ROMT_V_MAJOR 0 #define ROMT_V_MINOR 1 -#define ROMT_V_REVISE 0 +#define ROMT_V_PATCH 0 #define ROMT_MODE_READ ((uint32_t)0x00000001) ///< Mode for reading from ROM #define ROMT_MODE_WRITE ((uint32_t)0x00000002) ///< Mode for writing to ROM diff --git a/source/06_performance/unitt.h b/source/06_performance/unitt.h index 86183f1..e14c9ef 100644 --- a/source/06_performance/unitt.h +++ b/source/06_performance/unitt.h @@ -23,7 +23,7 @@ /* Version infomation */ #define UNITT_V_MAJOR 0 #define UNITT_V_MINOR 1 -#define UNITT_V_PATcH 0 +#define UNITT_V_PATCH 0 // Type definition for a clock function typedef uint64_t (*unitt_clock_t)(); diff --git a/test/test.mk b/test/test.mk index 6e1ea54..5608c16 100644 --- a/test/test.mk +++ b/test/test.mk @@ -1,55 +1,43 @@ - -TEST_SRC = -TEST_INC = - -#---------------------------------------- -# test sources -#---------------------------------------- -# TEST_SRC += $(TESTSPACE)/test_init.c -# TEST_SRC += $(TESTSPACE)/test_kern.c -# TEST_SRC += $(TESTSPACE)/test_valloc.c -# TEST_SRC += $(TESTSPACE)/test_arg.c -# TEST_SRC += $(TESTSPACE)/test_vstd.c -# TEST_SRC += $(TESTSPACE)/test_vlog.c -# TEST_SRC += $(TESTSPACE)/test_ini.c -# TEST_SRC += $(TESTSPACE)/test_txls.c -# TEST_SRC += $(TESTSPACE)/test_xml.c -# TEST_SRC += $(TESTSPACE)/test_csv.c -# TEST_SRC += $(TESTSPACE)/test_json.c -# TEST_SRC += $(TESTSPACE)/test_vector.c -# TEST_SRC += $(TESTSPACE)/test_list.c -# TEST_SRC += $(TESTSPACE)/test_str.c -# TEST_SRC += $(TESTSPACE)/test_queue.c -# TEST_SRC += $(TESTSPACE)/test_stack.c -# TEST_SRC += $(TESTSPACE)/test_deque.c -# TEST_SRC += $(TESTSPACE)/test_set.c -# TEST_SRC += $(TESTSPACE)/test_dict.c -# TEST_SRC += $(TESTSPACE)/test_map.c -# TEST_SRC += $(TESTSPACE)/test_heap.c -# TEST_SRC += $(TESTSPACE)/test_tree.c -# TEST_SRC += $(TESTSPACE)/test_graph.c -# TEST_SRC += $(TESTSPACE)/test_rbtree.c -# TEST_SRC += $(TESTSPACE)/test_pthread.c -# TEST_SRC += $(TESTSPACE)/test_encrypt.c -# TEST_SRC += $(TESTSPACE)/test_calculate.c -# TEST_SRC += $(TESTSPACE)/test_command.c -# TEST_SRC += $(TESTSPACE)/test_check.c -# TEST_SRC += $(TESTSPACE)/test_crc.c -# TEST_SRC += $(TESTSPACE)/test_sort.c -# TEST_SRC += $(TESTSPACE)/test_hash.c -# TEST_SRC += $(TESTSPACE)/test_pid.c -# TEST_SRC += $(TESTSPACE)/test_search.c -# TEST_SRC += $(TESTSPACE)/test_filter.c -# TEST_SRC += $(TESTSPACE)/test_oscp.c -# TEST_SRC += $(TESTSPACE)/test_tool.c -# TEST_SRC += $(TESTSPACE)/test_sList.c -# TEST_SRC += $(TESTSPACE)/test_dList.c -# TEST_SRC += $(TESTSPACE)/test_cQueue.c -# TEST_SRC += $(TESTSPACE)/test_intl.c -# TEST_SRC += $(TESTSPACE)/test_ramt.c -# TEST_SRC += $(TESTSPACE)/test_cpul.c -# TEST_SRC += $(TESTSPACE)/test_date.c -TEST_SRC += $(TESTSPACE)/test_unitt.c - - -export TEST_SRC TEST_INC +TEST_LIST += init +TEST_LIST += kern +TEST_LIST += valloc +TEST_LIST += arg +TEST_LIST += vstd +TEST_LIST += vlog +TEST_LIST += ini +TEST_LIST += txls +TEST_LIST += xml +TEST_LIST += csv +TEST_LIST += json +TEST_LIST += vector +TEST_LIST += list +TEST_LIST += str +TEST_LIST += queue +TEST_LIST += stack +TEST_LIST += deque +TEST_LIST += set +TEST_LIST += dict +TEST_LIST += map +TEST_LIST += heap +TEST_LIST += tree +TEST_LIST += graph +TEST_LIST += encrypt +TEST_LIST += calculate +TEST_LIST += command +TEST_LIST += check +TEST_LIST += crc +TEST_LIST += sort +TEST_LIST += hash +TEST_LIST += pid +TEST_LIST += search +TEST_LIST += filter +TEST_LIST += oscp +TEST_LIST += tool +TEST_LIST += sList +TEST_LIST += dList +TEST_LIST += cQueue +TEST_LIST += intl +TEST_LIST += ramt +TEST_LIST += cpul +TEST_LIST += date +TEST_LIST += unitt diff --git a/test/test_arg.c b/test/test_arg.c index 5813787..0280e80 100644 --- a/test/test_arg.c +++ b/test/test_arg.c @@ -1,11 +1,55 @@ #include #include #include +#if defined(TEST_TARGET_arg) +#include +#include +#else #include "init.h" +#include "command.h" #include "arg.h" +#endif -static void test(void) +static void usage(void) { + printf( +"Usage: arg [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("arg version %d.%d.%d\r\n", ARG_V_MAJOR, ARG_V_MINOR, ARG_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + printf("ARG_MAX %d\r\n", ARG_MAX); printf("ARGC %d\r\n", ARGC()); @@ -21,5 +65,19 @@ static void test(void) printf("ARGS %s\r\n", ARGS(0, "arg0", "arg1", "arg2")); printf("ARGS %s\r\n", ARGS(1, "arg0", "arg1", "arg2")); printf("ARGS %s\r\n", ARGS(2, "arg0", "arg1", "arg2")); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_arg) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_arg(void) +{ + command_export("arg", test); +} +init_export_app(test_arg); +#endif diff --git a/test/test_cQueue.c b/test/test_cQueue.c index a3eacf5..3172a21 100644 --- a/test/test_cQueue.c +++ b/test/test_cQueue.c @@ -1,10 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_cQueue) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "cQueue.h" +#endif void test_int(void) { @@ -63,13 +67,61 @@ void test_struct(void) } } -static void test(void) +static void usage(void) { - printf("cQueue test!\r\n"); + printf( +"Usage: cQueue [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("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; + } + } // test_int(); test_struct(); - v_check_unfree(); + return 0; } -init_export_app(test); + +#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 diff --git a/test/test_calculate.c b/test/test_calculate.c index 0871012..1d719f0 100644 --- a/test/test_calculate.c +++ b/test/test_calculate.c @@ -1,15 +1,18 @@ #include #include #include -#include "init.h" -#include "tool.h" -#include "valloc.h" -#include "calculate.h" #include #include -#include "command.h" #include #include +#if defined(TEST_TARGET_calculate) +#include +#include +#else +#include "init.h" +#include "command.h" +#include "calculate.h" +#endif static int command_calculate(int argc, char *argv[]) { @@ -40,8 +43,46 @@ static double K() return 1024.0; } -static void test(void) +static void usage(void) { + printf( +"Usage: calculate [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("calculate version %d.%d.%d\r\n", CALCULATE_V_MAJOR, CALCULATE_V_MINOR, CALCULATE_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + printf("mul %lf\r\n", calculate(" ( 99 * 3 ) ")); printf("min %lf\r\n", calculate(" min (12, 3)")); printf("sin %lf\r\n", calculate("sin ( 11 / 2 * pi ) + 100 ")); @@ -50,5 +91,19 @@ static void test(void) calculate_export("K", K, 0); command_export("cal", command_calculate); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_calculate) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_calculate(void) +{ + command_export("calculate", test); +} +init_export_app(test_calculate); +#endif diff --git a/test/test_check.c b/test/test_check.c index f479524..a30faee 100644 --- a/test/test_check.c +++ b/test/test_check.c @@ -3,10 +3,14 @@ #include #include #include +#if defined(TEST_TARGET_check) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "check.h" +#endif typedef uint8_t (*checkType)(uint8_t* data, uint32_t len); @@ -17,8 +21,46 @@ static checkType checkFunTable[] = { check_xor, }; -static void test(void) +static void usage(void) { + printf( +"Usage: check [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("check version %d.%d.%d\r\n", CHECK_V_MAJOR, CHECK_V_MINOR, CHECK_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + char *testSample[] = { "Hello", "check algorithms", @@ -34,5 +76,19 @@ static void test(void) printf("Func %d, check %02X\r\n", j, check); } } + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_check) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_check(void) +{ + command_export("check", test); +} +init_export_app(test_check); +#endif diff --git a/test/test_command.c b/test/test_command.c index a594b92..8640e11 100644 --- a/test/test_command.c +++ b/test/test_command.c @@ -1,10 +1,12 @@ #include #include #include +#if defined(TEST_TARGET_command) +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" #include "command.h" +#endif int func1(int argc, char *argv[]) { @@ -29,8 +31,46 @@ int func3(int argc, char *argv[]) return 1; } -static void test(void) +static void usage(void) { + printf( +"Usage: command [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("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; + } + } + command_export("func1", func1); command_export("func2", func2); command_export("func3", func3); @@ -48,5 +88,19 @@ static void test(void) // command("func1 1\\ 2 3"); // printf("--------------------------\r\n"); // command("func1 \"1 2 3\""); + + return 0; } -init_export_app(test); + +#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 diff --git a/test/test_cpul.c b/test/test_cpul.c index 2be8e4f..099c70f 100644 --- a/test/test_cpul.c +++ b/test/test_cpul.c @@ -10,8 +10,14 @@ #include #include #include +#if defined(TEST_TARGET_cpul) +#include +#include +#else #include "init.h" +#include "command.h" #include "cpul.h" +#endif #define CORE_NUM_MAX 32 #define LINE_BUFFER_SIZE 1024 @@ -164,8 +170,46 @@ void *loadgen_entry(void *ptr) } } -static void test(void) +static void usage(void) { + printf( +"Usage: cpul [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("cpul version %d.%d.%d\r\n", CPUL_V_MAJOR, CPUL_V_MINOR, CPUL_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + int num_cpus = sysconf(_SC_NPROCESSORS_CONF); pthread_t thread_update, thread_gen[num_cpus]; int coreid[num_cpus]; @@ -196,5 +240,19 @@ static void test(void) { pthread_join(thread_gen[i], NULL); } + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_cpul) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_cpul(void) +{ + command_export("cpul", test); +} +init_export_app(test_cpul); +#endif diff --git a/test/test_crc.c b/test/test_crc.c index e3774ad..5fe04e6 100644 --- a/test/test_crc.c +++ b/test/test_crc.c @@ -3,10 +3,14 @@ #include #include #include +#if defined(TEST_TARGET_crc) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "crc.h" +#endif uint32_t std_crc(uint8_t* data, uint32_t len, uint32_t index) { @@ -42,8 +46,46 @@ uint32_t std_crc(uint8_t* data, uint32_t len, uint32_t index) return crc; } -static void test(void) +static void usage(void) { + printf( +"Usage: crc [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("crc version %d.%d.%d\r\n", CRC_V_MAJOR, CRC_V_MINOR, CRC_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + char *testSample[] = { "Hello", "crc algorithms", @@ -61,5 +103,19 @@ static void test(void) printf("crc %02d, crc0 %08X, crc1 %08X, same %d\r\n", j, crc0, crc1, (crc0 == crc1) ? 1 : 0); } } + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_crc) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_crc(void) +{ + command_export("crc", test); +} +init_export_app(test_crc); +#endif diff --git a/test/test_csv.c b/test/test_csv.c index 4199213..89039a0 100644 --- a/test/test_csv.c +++ b/test/test_csv.c @@ -1,8 +1,13 @@ #include #include +#if defined(TEST_TARGET_csv) +#include +#include +#else #include "init.h" +#include "command.h" #include "csv.h" -#include "valloc.h" +#endif #define READ_FILE "source/application/test/file/read.csv" #define WRITE_FILE "source/application/test/file/write.csv" @@ -176,13 +181,64 @@ static void load_demo(void) csv_delete(csv); } -static void test(void) +static void usage(void) { + printf( +"Usage: csv [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("csv version %d.%d.%d\r\n", CSV_V_MAJOR, CSV_V_MINOR, CSV_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + // test_dump(); // test_load(); // dump_demo(); load_demo(); - printf("use %d\r\n", v_check_used()); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_csv) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_csv(void) +{ + command_export("csv", test); +} +init_export_app(test_csv); +#endif diff --git a/test/test_dList.c b/test/test_dList.c index f907a72..952c0eb 100644 --- a/test/test_dList.c +++ b/test/test_dList.c @@ -1,10 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_dList) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "dList.h" +#endif static void test_create(void) { @@ -254,7 +258,7 @@ static void test_append(void) if (!dList_append(list, &ap)) goto FAIL; - printPoint(ap); + printf("ap: %p\r\n", ap); dList_forEachForward(list, n) { @@ -314,9 +318,45 @@ FAIL: dList_delete(list); } -static void test(void) +static void usage(void) { - printf("dList test!\r\n"); + printf( +"Usage: dList [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("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; + } + } // test_create(); // test_set(); @@ -332,6 +372,18 @@ static void test(void) // test_copy(); // test_reverse(); - v_check_unfree(); + return 0; } -init_export_app(test); + +#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 diff --git a/test/test_date.c b/test/test_date.c index 29c4463..662e677 100644 --- a/test/test_date.c +++ b/test/test_date.c @@ -1,8 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_date) +#include +#include +#else #include "init.h" +#include "command.h" #include "date.h" +#endif #if 0 static int century_days(int century) @@ -45,8 +51,46 @@ static int thousand3_days(int base) } #endif -static void test(void) +static void usage(void) { + printf( +"Usage: date [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("date version %d.%d.%d\r\n", DATE_V_MAJOR, DATE_V_MINOR, DATE_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + printf("month days %d\r\n", date_month_days(2024, 11)); printf("date_isleap %d\r\n", date_isleap(1582)); // printf("date_current_days %d\r\n", date_current_days(DATE(1,1,1))); @@ -65,5 +109,19 @@ static void test(void) date_show(1998,7); date_show(2024,11); date_show(2224,11); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_date) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_date(void) +{ + command_export("date", test); +} +init_export_app(test_date); +#endif diff --git a/test/test_deque.c b/test/test_deque.c index 2e135c2..7baa218 100644 --- a/test/test_deque.c +++ b/test/test_deque.c @@ -1,12 +1,16 @@ #include #include #include +#if defined(TEST_TARGET_deque) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "deque.h" +#endif -static void test_deque(void) +static void test_base(void) { deque_t deque = deque(int, 10); int i = 0; @@ -25,9 +29,60 @@ static void test_deque(void) _deque(deque); } -static void test(void) +static void usage(void) { - test_deque(); - v_check_unfree(); + printf( +"Usage: deque [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("deque version %d.%d.%d\r\n", DEQUE_V_MAJOR, DEQUE_V_MINOR, DEQUE_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + test_base(); + + return 0; +} + +#if defined(TEST_TARGET_deque) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_deque(void) +{ + command_export("deque", test); +} +init_export_app(test_deque); +#endif diff --git a/test/test_dict.c b/test/test_dict.c index 6a3f873..446b523 100644 --- a/test/test_dict.c +++ b/test/test_dict.c @@ -1,10 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_dict) +#include +#include +#else #include "init.h" -#include "tool.h" -// #include "valloc.h" +#include "command.h" #include "dict.h" +#endif #if 0 static void dict_print(dict_t dict) @@ -70,7 +74,7 @@ static void test_performance(void) } #endif -static void test_dict(void) +static void test_base(void) { dict_t dict = dict(int); int value; @@ -120,12 +124,62 @@ static void test_dict(void) _dict(dict); } -static void test(void) +static void usage(void) { - test_dict(); + printf( +"Usage: dict [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("dict version %d.%d.%d\r\n", DICT_V_MAJOR, DICT_V_MINOR, DICT_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + test_base(); // test_it(); // test_performance(); - // v_check_unfree(); - // printf("mem use %d\r\n", v_check_used()); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_dict) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_dict(void) +{ + command_export("dict", test); +} +init_export_app(test_dict); +#endif diff --git a/test/test_encrypt.c b/test/test_encrypt.c index 9b1bba2..9b5d1af 100644 --- a/test/test_encrypt.c +++ b/test/test_encrypt.c @@ -1,10 +1,17 @@ #include #include #include -#include "init.h" -#include "tool.h" -#include "encrypt.h" #include +#if defined(TEST_TARGET_encrypt) +#include +#include +#else +#include "init.h" +#include "command.h" +#include "encrypt.h" +#endif + + static void test_des(void) { @@ -92,9 +99,61 @@ static void test_des3(void) printf("\r\n"); } -static void test(void) +static void usage(void) { + printf( +"Usage: encrypt [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("encrypt version %d.%d.%d\r\n", ENCRYPT_V_MAJOR, ENCRYPT_V_MINOR, ENCRYPT_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + // test_des(); test_des3(); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_encrypt) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_encrypt(void) +{ + command_export("encrypt", test); +} +init_export_app(test_encrypt); +#endif diff --git a/test/test_filter.c b/test/test_filter.c index 26ed472..128b214 100644 --- a/test/test_filter.c +++ b/test/test_filter.c @@ -1,9 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_filter) +#include +#include +#else #include "init.h" -#include "tool.h" +#include "command.h" #include "filter.h" +#endif static void test_median(void) { @@ -50,12 +55,62 @@ static void test_kalman(void) printf("Estimated value: %f\n", estimates[0]); } -static void test(void) +static void usage(void) { - printf("pid test!\r\n"); - + printf( +"Usage: filter [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("filter version %d.%d.%d\r\n", FILTER_V_MAJOR, FILTER_V_MINOR, FILTER_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + test_median(); // test_average(); // test_kalman(); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_filter) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_filter(void) +{ + command_export("filter", test); +} +init_export_app(test_filter); +#endif diff --git a/test/test_graph.c b/test/test_graph.c index ce68b24..63d38d8 100644 --- a/test/test_graph.c +++ b/test/test_graph.c @@ -1,11 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_graph) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "graph.h" -#include "arg.h" +#endif #define la(type, value) ((type[1]){value}) @@ -364,10 +367,46 @@ static void test_others(void) graph_delete(graph); } -static void test(void) +static void usage(void) { - printf("graph test!\r\n"); - + printf( +"Usage: graph [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("graph version %d.%d.%d\r\n", GRAPH_V_MAJOR, GRAPH_V_MINOR, GRAPH_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + // test_create(); // test_add_vertex(); // test_add_edge(); @@ -379,6 +418,18 @@ static void test(void) // test_shortest_path(); test_min_cover(); - v_check_unfree(); + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_graph) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_graph(void) +{ + command_export("graph", test); +} +init_export_app(test_graph); +#endif diff --git a/test/test_hash.c b/test/test_hash.c index f05509d..9f1a92a 100644 --- a/test/test_hash.c +++ b/test/test_hash.c @@ -3,13 +3,55 @@ #include #include #include +#if defined(TEST_TARGET_hash) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "hash.h" +#endif -static void test(void) +static void usage(void) { + printf( +"Usage: hash [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("hash version %d.%d.%d\r\n", HASH_V_MAJOR, HASH_V_MINOR, HASH_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + printf("hash_bkdr 0x%X\r\n", hash_bkdr("Hello", 5)); printf("hash_ap 0x%X\r\n", hash_ap("Hello", 5)); printf("hash_djb 0x%X\r\n", hash_djb("Hello", 5)); @@ -22,5 +64,19 @@ static void test(void) printf("hash_bp 0x%X\r\n", hash_bp("Hello", 5)); printf("hash_fnv 0x%X\r\n", hash_fnv("Hello", 5)); printf("hash_jdk6 0x%X\r\n", hash_jdk6("Hello", 5)); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_hash) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_hash(void) +{ + command_export("hash", test); +} +init_export_app(test_hash); +#endif diff --git a/test/test_heap.c b/test/test_heap.c index 34fac30..394a133 100644 --- a/test/test_heap.c +++ b/test/test_heap.c @@ -1,10 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_heap) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "heap.h" +#endif static int heap_root_min(void *parent, void *child) { @@ -16,7 +20,7 @@ static int heap_root_max(void *parent, void *child) if (*(int *)parent > *(int *)child) return 1; return 0; } -static void test_heap(void) +static void test_base(void) { heap_t h = heap_create(sizeof(int), 11, heap_root_max); int i = 0; @@ -41,9 +45,60 @@ static void test_heap(void) heap_delete(h); } -static void test(void) +static void usage(void) { - test_heap(); - v_check_unfree(); + printf( +"Usage: heap [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("heap version %d.%d.%d\r\n", HEAP_V_MAJOR, HEAP_V_MINOR, HEAP_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + test_base(); + + return 0; +} + +#if defined(TEST_TARGET_heap) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_heap(void) +{ + command_export("heap", test); +} +init_export_app(test_heap); +#endif diff --git a/test/test_ini.c b/test/test_ini.c index eaecac1..4030ba1 100644 --- a/test/test_ini.c +++ b/test/test_ini.c @@ -1,7 +1,12 @@ #include +#if defined(TEST_TARGET_ini) +#include +#include +#else #include "init.h" +#include "command.h" #include "ini.h" -#include "valloc.h" +#endif #define READ_FILE "test/file/read.ini" #define WRITE_FILE "test/file/write.ini" @@ -75,12 +80,63 @@ static void test_load(void) #endif ini_file_dump(ini, WRITE_FILE); ini_delete(ini); - v_check_unfree(); } -static void test(void) +static void usage(void) { + printf( +"Usage: ini [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("ini version %d.%d.%d\r\n", INI_V_MAJOR, INI_V_MINOR, INI_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + // test_dump(); test_load(); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_ini) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_ini(void) +{ + command_export("ini", test); +} +init_export_app(test_ini); +#endif diff --git a/test/test_init.c b/test/test_init.c index a996e15..7abed08 100644 --- a/test/test_init.c +++ b/test/test_init.c @@ -1,5 +1,12 @@ #include +#if defined(TEST_TARGET_init) +#include +#include +#else #include "init.h" +#include "command.h" +#include "init.h" +#endif void test_init_hardware(void) { @@ -36,3 +43,59 @@ void test_init_system1(void) printf("system1 init!\r\n"); } init_export_system(test_init_system1); + +static void usage(void) +{ + printf( +"Usage: init [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("init version %d.%d.%d\r\n", INIT_V_MAJOR, INIT_V_MINOR, INIT_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + return 0; +} + +#if defined(TEST_TARGET_init) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_init(void) +{ + command_export("init", test); +} +init_export_app(test_init); +#endif diff --git a/test/test_install.c b/test/test_install.c deleted file mode 100644 index 307dc70..0000000 --- a/test/test_install.c +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include -#include - -int main(int argc, char *argv[]) -{ - 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); -} - -// gcc ./test/test_install.c -o ./built/bin/app -lvarch -// ./built/bin/app diff --git a/test/test_intl.c b/test/test_intl.c index e190420..1711fb0 100644 --- a/test/test_intl.c +++ b/test/test_intl.c @@ -1,7 +1,13 @@ #include #include +#if defined(TEST_TARGET_intl) +#include +#include +#else #include "init.h" +#include "command.h" #include "intl.h" +#endif #define INTL_P_DEC 0x01 #define INTL_P_BIN 0x02 @@ -96,10 +102,62 @@ static void test_calculate(void) print_intl(intl_dec(b), INTL_P_DEC); } -static void test(void) +static void usage(void) { + printf( +"Usage: intl [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("intl version %d.%d.%d\r\n", INTL_V_MAJOR, INTL_V_MINOR, INTL_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + // test_define(); // test_print(); test_calculate(); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_intl) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_intl(void) +{ + command_export("intl", test); +} +init_export_app(test_intl); +#endif diff --git a/test/test_json.c b/test/test_json.c index 7ffe667..d8019b0 100644 --- a/test/test_json.c +++ b/test/test_json.c @@ -2,9 +2,14 @@ #include #include #include +#if defined(TEST_TARGET_json) +#include +#include +#else #include "init.h" +#include "command.h" #include "json.h" -#include "valloc.h" +#endif #define READ_FILE "test/file/read.json" #define WRITE_FILE "test/file/write.json" @@ -110,12 +115,61 @@ static void test_write(void) json_delete(json); } -static void test(void) +static void usage(void) { - printf("json test!\r\n"); - test_write(); - // test_read(); - - v_check_unfree(); + printf( +"Usage: json [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("json version %d.%d.%d\r\n", JSON_V_MAJOR, JSON_V_MINOR, JSON_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + // test_write(); + test_read(); + + return 0; +} + +#if defined(TEST_TARGET_json) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_json(void) +{ + command_export("json", test); +} +init_export_app(test_json); +#endif diff --git a/test/test_kern.c b/test/test_kern.c index daedadd..1fbb29a 100644 --- a/test/test_kern.c +++ b/test/test_kern.c @@ -1,11 +1,15 @@ #include #include #include -#include "init.h" -#include "tool.h" -#include "valloc.h" -#include "kern.h" #include +#if defined(TEST_TARGET_kern) +#include +#include +#else +#include "init.h" +#include "command.h" +#include "kern.h" +#endif static unsigned int get_msec(void) { @@ -16,20 +20,59 @@ static unsigned int get_msec(void) return ms; } -void task1(void) +static void task1(void) { static int count = 0; printf("task1 running! %d\r\n", ++count); } -void task2(void) +static void task2(void) { static int count = 0; printf("task2 running! %d\r\n", ++count); } -static void test(void) +static void usage(void) { + printf( +"Usage: kern [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("kern version %d.%d.%d\r\n", KERN_V_MAJOR, KERN_V_MINOR, KERN_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + +#if defined(TEST_TARGET_kern) if (kern_init(get_msec, 1) == KE_OK) { printf("kern init success!\r\n"); @@ -37,12 +80,30 @@ static void test(void) else { printf("*** kern init fail!\r\n"); - return; + return 0; } - // printf("create task %d\r\n", task_create(1000, task1)); - // printf("create task %d\r\n", task_create(500, task2)); + printf("create task %d\r\n", task_create(1000, task1)); + printf("create task %d\r\n", task_create(500, task2)); - // kern_schedule(); + kern_schedule(); +#else + printf("create task %d\r\n", task_create(1000, task1)); + printf("create task %d\r\n", task_create(500, task2)); +#endif + + return 0; } -init_export_system(test); + +#if defined(TEST_TARGET_kern) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_kern(void) +{ + command_export("kern", test); +} +init_export_app(test_kern); +#endif diff --git a/test/test_list.c b/test/test_list.c index 43adad3..49515f2 100644 --- a/test/test_list.c +++ b/test/test_list.c @@ -1,47 +1,43 @@ #include #include #include +#if defined(TEST_TARGET_list) +#include +#include +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "list.h" #include "vector.h" +#include "tool.h" +#endif -static void test(void) +static void test_base(void) { list_t list = list(int); // 定义并创建int型list int i = 0; - unsigned long long start = 0; - unsigned long long stop = 0; for (i = 0; i < 10000; i++) list_push_back(list, NULL); // 依次插入 0 1 2 // 正向遍历用时 - start = reckon_usec(); for (i = 0; i < list_size(list); i++) { list_at(list, int, i); } - stop = reckon_usec(); - printf("use %llu us!\r\n", (stop - start)); // 反向遍历用时 - start = reckon_usec(); for (i = list_size(list) - 1; i >= 0; i--) { list_at(list, int, i); } - stop = reckon_usec(); - printf("use %llu us!\r\n", (stop - start)); // 随机访问用时 - start = reckon_usec(); for (i = 0; i < list_size(list); i++) { list_at(list, int, rand()%list_size(list)); } - stop = reckon_usec(); - printf("use %llu us!\r\n", (stop - start)); printf("size %d\r\n", list_size(list)); @@ -52,16 +48,68 @@ static void test(void) vector_t vt = vector(int, 10000); // 正向遍历用时 - start = reckon_usec(); for (i = 0; i < vector_size(vt); i++) { vector_at(vt, int, i); } - stop = reckon_usec(); - printf("use %llu us!\r\n", (stop - start)); _vector(vt); - - v_check_unfree(); } -init_export_app(test); + +static void usage(void) +{ + printf( +"Usage: list [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("list version %d.%d.%d\r\n", LIST_V_MAJOR, LIST_V_MINOR, LIST_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + test_base(); + + return 0; +} + +#if defined(TEST_TARGET_list) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_list(void) +{ + command_export("list", test); +} +init_export_app(test_list); +#endif diff --git a/test/test_map.c b/test/test_map.c index ed8ec4a..77552ba 100644 --- a/test/test_map.c +++ b/test/test_map.c @@ -1,10 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_map) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "map.h" +#endif static void test_it(void) { @@ -32,7 +36,7 @@ static void test_it(void) _map(map); } -static void test_map(void) +static void test_base(void) { int value; map_t map = map(string, int); @@ -63,11 +67,63 @@ static void test_float_map(void) _map(map); } -static void test(void) +static void usage(void) { - test_it(); - // test_map(); - // test_float_map(); - v_check_unfree(); + printf( +"Usage: map [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); \ No newline at end of file + +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("map version %d.%d.%d\r\n", MAP_V_MAJOR, MAP_V_MINOR, MAP_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + test_it(); + // test_base(); + // test_float_map(); + + return 0; +} + +#if defined(TEST_TARGET_map) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_map(void) +{ + command_export("map", test); +} +init_export_app(test_map); +#endif + diff --git a/test/test_oscp.c b/test/test_oscp.c index f8823d6..a930a71 100644 --- a/test/test_oscp.c +++ b/test/test_oscp.c @@ -2,10 +2,17 @@ #include #include #include -#include "init.h" -#include "kern.h" -#include "oscp.h" #include +#if defined(TEST_TARGET_oscp) +#include +#include +#include +#else +#include "init.h" +#include "command.h" +#include "oscp.h" +#include "kern.h" +#endif int level = 0; @@ -18,12 +25,12 @@ static unsigned int get_msec(void) return ms; } -void task1(void) +static void task1(void) { oscp_handle(); } -void task2(void) +static void task2(void) { static int count = 0; @@ -33,8 +40,46 @@ void task2(void) level = RESOLUTION / 2 * sin((double)count / 10) + RESOLUTION / 2; } -static void test(void) +static void usage(void) { + printf( +"Usage: oscp [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("oscp version %d.%d.%d\r\n", OSCP_V_MAJOR, OSCP_V_MINOR, OSCP_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + if (kern_init(get_msec, 1) == KE_OK) { printf("kern init success!\r\n"); @@ -42,7 +87,7 @@ static void test(void) else { printf("*** kern init fail!\r\n"); - return; + return 0; } printf("create task %d\r\n", task_create(5, task1)); @@ -52,5 +97,19 @@ static void test(void) oscp_set_monitor(&level); kern_schedule(); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_oscp) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_oscp(void) +{ + command_export("oscp", test); +} +init_export_app(test_oscp); +#endif diff --git a/test/test_pid.c b/test/test_pid.c index 7c80e43..7f2a7b2 100644 --- a/test/test_pid.c +++ b/test/test_pid.c @@ -1,9 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_pid) +#include +#include +#else #include "init.h" -#include "tool.h" +#include "command.h" #include "pid.h" +#endif static void test_sim(void) { @@ -27,10 +32,60 @@ static void test_sim(void) } } -static void test(void) +static void usage(void) { - printf("pid test!\r\n"); - - test_sim(); + printf( +"Usage: pid [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("pid version %d.%d.%d\r\n", PID_V_MAJOR, PID_V_MINOR, PID_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + test_sim(); + + return 0; +} + +#if defined(TEST_TARGET_pid) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_pid(void) +{ + command_export("pid", test); +} +init_export_app(test_pid); +#endif diff --git a/test/test_pthread.c b/test/test_pthread.c deleted file mode 100644 index 0a42913..0000000 --- a/test/test_pthread.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include -#include "init.h" -#include "tool.h" -#include "valloc.h" -#include -#include - -sem_t s; - -static void *thread_entry(void *param) -{ - long thread_id = (long)param; - - for (;;) - { - sem_wait(&s); - printf("in\r\n"); - } - return NULL; -}; - - -static void test(void) -{ - pthread_t t; - pthread_create(&t, NULL, thread_entry, NULL); - printf("pthread\r\n"); - sem_post(&s); -} -init_export_app(test); diff --git a/test/test_queue.c b/test/test_queue.c index 5ba7d4c..f1eb7c3 100644 --- a/test/test_queue.c +++ b/test/test_queue.c @@ -1,12 +1,16 @@ #include #include #include +#if defined(TEST_TARGET_queue) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "queue.h" +#endif -static void test_queue(void) +static void test_base(void) { queue_t queue = queue(int, 10); int i = 0; @@ -26,9 +30,62 @@ static void test_queue(void) _queue(queue); } -static void test(void) +static void usage(void) { - test_queue(); - v_check_unfree(); + printf( +"Usage: queue [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("queue version %d.%d.%d\r\n", QUEUE_V_MAJOR, QUEUE_V_MINOR, QUEUE_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + test_base(); + + return 0; +} + +#if defined(TEST_TARGET_queue) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_queue(void) +{ + command_export("queue", test); +} +init_export_app(test_queue); +#endif + + diff --git a/test/test_ramt.c b/test/test_ramt.c index 78b0261..9019e11 100644 --- a/test/test_ramt.c +++ b/test/test_ramt.c @@ -1,10 +1,16 @@ #include #include #include + +#if defined(TEST_TARGET_ramt) +#include +#include +#else #include "init.h" -#include "tool.h" +#include "command.h" #include "ramt.h" #include "kern.h" +#endif #define TASK_PERIOD 5 // ms @@ -25,11 +31,55 @@ static void test_task(void) } } -static void test_ramt(void) +static void usage(void) { - task_t task; - - printf("ramt test!\r\n"); + printf( +"Usage: ramt [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); +} + +static int test(int argc, char *argv[]) +{ + int period = 1000; // ms + + /* reset getopt */ + command_opt_init(); + + while (1) + { + int opt = command_getopt(argc, argv, "p:hv"); + if (opt == -1) break; + + switch (opt) + { + case 'p' : + period = atoi(command_optarg); + break; + case 'v' : + printf("ramt version %d.%d.%d\r\n", RAMT_V_MAJOR, RAMT_V_MINOR, RAMT_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + if (1 == command_optind) // no opt + { + test_task(); + return 0; + } ramt_object.base = (void *)buffer; ramt_object.size = sizeof(buffer); @@ -37,7 +87,28 @@ static void test_ramt(void) ramt_start(&ramt_object, RAMT_MODE_NORMAL, 0xFFFFFFFF); - task = task_create(TASK_PERIOD, test_task); - printf("task <%d>\r\n", task); +#if defined(TEST_TARGET_ramt) + while (1) + { + test_task(); + usleep(1000 * period); + } +#else + printf("create task %d\r\n", task_create(TASK_PERIOD, test_task)); +#endif + + return 0; +} + +#if defined(TEST_TARGET_ramt) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_ramt(void) +{ + command_export("ramt", test); } init_export_app(test_ramt); +#endif diff --git a/test/test_rbtree.c b/test/test_rbtree.c deleted file mode 100644 index 20f2790..0000000 --- a/test/test_rbtree.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -#include "init.h" -#include "tool.h" -#include "valloc.h" -#include "rbtree.h" - -static void print_int(tree_t tree) -{ - if (tree_data(tree)) printf("%d", *(int *)tree_data(tree)); -} - - - -static void test(void) -{ - printf("rbtree test\r\n"); - - - - v_check_unfree(); - // printf("used %d\r\n", v_check_used()); -} -init_export_app(test); diff --git a/test/test_sList.c b/test/test_sList.c index 4bd9ef4..da9e2bf 100644 --- a/test/test_sList.c +++ b/test/test_sList.c @@ -1,10 +1,16 @@ #include #include #include +#if defined(TEST_TARGET_sList) +#include +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "sList.h" +#include "tool.h" +#endif static void test_create(void); static void test_set(void); @@ -319,9 +325,45 @@ FAIL: sList_delete(list); } -static void test(void) +static void usage(void) { - printf("sList test!\r\n"); + printf( +"Usage: sList [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("sList version %d.%d.%d\r\n", SLIST_V_MAJOR, SLIST_V_MINOR, SLIST_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } // test_create(); test_set(); @@ -337,6 +379,18 @@ static void test(void) // test_copy(); // test_reverse(); - v_check_unfree(); + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_sList) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_sList(void) +{ + command_export("sList", test); +} +init_export_app(test_sList); +#endif diff --git a/test/test_search.c b/test/test_search.c index dcd7a30..73190a9 100644 --- a/test/test_search.c +++ b/test/test_search.c @@ -1,9 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_search) +#include +#include +#else #include "init.h" -#include "tool.h" +#include "command.h" #include "search.h" +#endif static void* int_array_addr(void *array, int index) { @@ -57,11 +62,61 @@ static void test_binary(void) target = -9, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); } -static void test(void) +static void usage(void) { - printf("search test!\r\n"); - + printf( +"Usage: search [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("search version %d.%d.%d\r\n", SEARCH_V_MAJOR, SEARCH_V_MINOR, SEARCH_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + // test_linear(); test_binary(); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_search) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_search(void) +{ + command_export("search", test); +} +init_export_app(test_search); +#endif diff --git a/test/test_set.c b/test/test_set.c index 7cbcac4..0e17169 100644 --- a/test/test_set.c +++ b/test/test_set.c @@ -1,10 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_set) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "set.h" +#endif static void test_it(void) { @@ -32,7 +36,7 @@ static void test_it(void) _set(set); } -static void test_set(void) +static void test_base(void) { set_t set = set(int); int i; @@ -56,10 +60,61 @@ static void test_set(void) _set(set); } -static void test(void) +static void usage(void) { - // test_set(); - test_it(); - v_check_unfree(); + printf( +"Usage: set [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("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; + } + } + + // test_base(); + test_it(); + + return 0; +} + +#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 diff --git a/test/test_sort.c b/test/test_sort.c index c207d8f..50dbee5 100644 --- a/test/test_sort.c +++ b/test/test_sort.c @@ -1,11 +1,16 @@ #include #include #include +#if defined(TEST_TARGET_sort) +#include +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "sort.h" #include "list.h" +#endif static void test_basic_usage(void) { @@ -208,12 +213,65 @@ static void test_struct(void) printf("---------------------------------\r\n"); } -static void test(void) +static void usage(void) { + printf( +"Usage: sort [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("sort version %d.%d.%d\r\n", SORT_V_MAJOR, SORT_V_MINOR, SORT_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + // test_basic_usage(); // test_interval(); // test_list_sort(); test_struct(); + + return 0; } -init_export_app(test); +#if defined(TEST_TARGET_sort) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_sort(void) +{ + command_export("sort", test); +} +init_export_app(test_sort); +#endif + + diff --git a/test/test_stack.c b/test/test_stack.c index 355b6a2..1fbdf21 100644 --- a/test/test_stack.c +++ b/test/test_stack.c @@ -1,12 +1,16 @@ #include #include #include +#if defined(TEST_TARGET_stack) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "stack.h" +#endif -static void test_stack(void) +static void test_base(void) { stack_t stack = stack(int, 10); int i = 0; @@ -25,9 +29,61 @@ static void test_stack(void) _stack(stack); } -static void test(void) +static void usage(void) { - test_stack(); - v_check_unfree(); + printf( +"Usage: stack [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("stack version %d.%d.%d\r\n", STACK_V_MAJOR, STACK_V_MINOR, STACK_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + test_base(); + + return 0; +} + +#if defined(TEST_TARGET_stack) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_stack(void) +{ + command_export("stack", test); +} +init_export_app(test_stack); +#endif + diff --git a/test/test_str.c b/test/test_str.c index c84a314..60e2da1 100644 --- a/test/test_str.c +++ b/test/test_str.c @@ -1,17 +1,73 @@ #include #include #include +#if defined(TEST_TARGET_str) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "str.h" +#endif -static void test(void) +static void usage(void) { + printf( +"Usage: str [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("str version %d.%d.%d\r\n", STR_V_MAJOR, STR_V_MINOR, STR_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + str_t s = str("0123456789"); str_replace(s, 9, 10, "AAAAAAAAAA"); printf("len %d, %d\r\n", str_length(s), str_capacity(s)); printf("%s\r\n", _S(s)); _str(s); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_str) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_str(void) +{ + command_export("str", test); +} +init_export_app(test_str); +#endif diff --git a/test/test_tool.c b/test/test_tool.c index 8581bc6..f267ea2 100644 --- a/test/test_tool.c +++ b/test/test_tool.c @@ -1,7 +1,13 @@ #include #include +#if defined(TEST_TARGET_tool) +#include +#include +#else #include "init.h" +#include "command.h" #include "tool.h" +#endif static void test_GetStringHex(void) { @@ -48,10 +54,63 @@ static void test_print(void) printString(s); } -static void test(void) +static void usage(void) { + printf( +"Usage: tool [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("tool version %d.%d.%d\r\n", TOOL_V_MAJOR, TOOL_V_MINOR, TOOL_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + // test_GetStringHex(); // test_ASSERT(); test_print(); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_tool) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_tool(void) +{ + command_export("tool", test); +} +init_export_app(test_tool); +#endif + diff --git a/test/test_tree.c b/test/test_tree.c index cd2e0b5..563531b 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -1,10 +1,14 @@ #include #include #include +#if defined(TEST_TARGET_tree) +#include +#include +#else #include "init.h" -#include "tool.h" -#include "valloc.h" +#include "command.h" #include "tree.h" +#endif static void print_int(tree_t tree) { @@ -189,8 +193,6 @@ static tree_t bitree_gen(int depth) return tree; } - - static void test_bitree(void) { tree_t tree = bitree_gen(5); @@ -199,16 +201,63 @@ static void test_bitree(void) tree_delete(tree, NULL); } -static void test(void) +static void usage(void) { - printf("tree test\r\n"); + printf( +"Usage: tree [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write 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("tree version %d.%d.%d\r\n", TREE_V_MAJOR, TREE_V_MINOR, TREE_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } // test_create(); test_dir(); // test_ini(); // test_bitree(); - - v_check_unfree(); - // printf("used %d\r\n", v_check_used()); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_tree) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_tree(void) +{ + command_export("tree", test); +} +init_export_app(test_tree); +#endif diff --git a/test/test_txls.c b/test/test_txls.c index cb6ceba..9e7dccc 100644 --- a/test/test_txls.c +++ b/test/test_txls.c @@ -1,8 +1,13 @@ #include #include +#if defined(TEST_TARGET_txls) +#include +#include +#else #include "init.h" +#include "command.h" #include "txls.h" -#include "valloc.h" +#endif #define READ_FILE "test/file/read.md" #define WRITE_FILE "test/file/write.md" @@ -21,11 +26,11 @@ void txls_preview(txls_t txls) static void test_read(void) { - txls_t x = NULL; // 定义txls对象,习惯初始化为NULL + txls_t x = NULL; // Define the txls object, initialized to NULL as usual - /* 加载txls文件 */ + /* Load the txls file */ x = txls_file_load(READ_FILE); - if (!x) // 加载失败,定位错误 + if (!x) // Loading failed, locating error { int line, type; type = txls_error_info(&line); @@ -33,7 +38,7 @@ static void test_read(void) return; } - /* 遍历表头,定位所在列 */ + /* Iterate over the header to locate the column*/ int col = 0; for (col = 1; col <= txls_col(x); col++) { @@ -42,13 +47,13 @@ static void test_read(void) break; } } - if (col > txls_col(x)) // 没有查找到 + if (col > txls_col(x)) // Not found { printf("Lookup failed\r\n"); return; } - /* 打印信息 */ + /* print info */ printf("name: %s, age=%s, gender: %s, height=%s, weight=%s, email:%s\r\n", txls_get_text(x, col, 0), txls_get_text(x, col, 1), @@ -62,32 +67,32 @@ static void test_read(void) static void test_write(void) { - txls_t x = NULL; // 定义txls对象,习惯初始化为NULL + txls_t x = NULL; // Define the txls object, initialized to NULL as usual - x = txls_create(4, 5); // 创建4x5的表格 + x = txls_create(4, 5); // Create a 4x5 txls if (!x) { return; } - /* 设置表头,第一列留空 */ + /* Sets the header of the table, leaving the first column blank */ txls_set_head(x, 2, "Zhang San"); txls_set_head(x, 3, "Li Si"); txls_set_head(x, 4, "Wang Wu"); - /* 设置对齐方式 */ + /* Set the alignment */ txls_set_align(x, 2, TXLS_ALIGN_LEFT); txls_set_align(x, 3, TXLS_ALIGN_CENTER); txls_set_align(x, 4, TXLS_ALIGN_RIGHT); - /* 第一列作为信息类别 */ + /* The first column serves as the information category */ txls_set_text(x, 1, 1, "age"); txls_set_text(x, 1, 2, "gender"); txls_set_text(x, 1, 3, "height"); txls_set_text(x, 1, 4, "weight"); txls_set_text(x, 1, 5, "email"); - /* 写入每个人信息 */ + /* Write per-person information */ // Zhang San txls_set_text(x, 2, 1, "18"); txls_set_text(x, 2, 2, "man"); @@ -112,11 +117,70 @@ static void test_write(void) txls_delete(x); } -static void test(void) +static void usage(void) { - // test_write(); - test_read(); - - // v_check_unfree(); + printf( +"Usage: txls [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("txls version %d.%d.%d\r\n", TXLS_V_MAJOR, TXLS_V_MINOR, TXLS_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], "read")) + { + test_read(); + } + else if (!strcmp(argv[index], "write")) + { + test_write(); + } + } + + return 0; +} + +#if defined(TEST_TARGET_txls) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_txls(void) +{ + command_export("txls", test); +} +init_export_app(test_txls); +#endif diff --git a/test/test_unitt.c b/test/test_unitt.c index dcb070e..a2b9ed7 100644 --- a/test/test_unitt.c +++ b/test/test_unitt.c @@ -1,9 +1,16 @@ #include #include #include +#include +#if defined(TEST_TARGET_unitt) +#include +#include +#else #include "init.h" +#include "command.h" #include "unitt.h" #include "kern.h" +#endif #if 0 #ifdef _WIN32 @@ -19,8 +26,8 @@ void get_memory_usage(int* resident_set_size, int* virtual_memory_size) #ifdef _WIN32 PROCESS_MEMORY_COUNTERS pmc; if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) { - *resident_set_size = pmc.WorkingSetSize / 1024; // 转换为KB - *virtual_memory_size = pmc.PagefileUsage / 1024; // 转换为KB + *resident_set_size = pmc.WorkingSetSize / 1024; // Converting to KB + *virtual_memory_size = pmc.PagefileUsage / 1024; // Converting to KB } else { *resident_set_size = 0; *virtual_memory_size = 0; @@ -49,114 +56,97 @@ uint64_t unitt_clock(void) return us; } -// 计算两个整数的和 -int unit_add(int a, int b) +int unitt_add(int a, int b) { return a + b; } -// 计算两个整数的差 -int unit_subtract(int a, int b) +int unitt_subtract(int a, int b) { return a - b; } -// 计算两个整数的乘积 -int unit_multiply(int a, int b) +int unitt_multiply(int a, int b) { return a * b; } -// 计算两个整数的商 -float unit_divide(int a, int b) +float unitt_divide(int a, int b) { if (b == 0) { - return 0; // 简单处理,真实应用中应更严格处理 + return 0; } return (float)a / (float)b; } -// 测试前的初始化函数 int setup() { - // 初始化代码(如果需要) return 0; } -// 测试后的清理函数 int teardown() { - // 清理代码(如果需要) return 0; } -// 随机输入生成函数 int random_input() { - return rand() % 100; // 生成0到99之间的随机整数 + return rand() % 100; } -// 测试加法函数,使用随机输入 int test_add() { - // 进行10次随机测试 for (int i = 0; i < SINGLE_TCOUNT; i++) { int a = random_input(); int b = random_input(); - if (UNITT_E_FAIL == unitt_det_equal(unit_add(a, b), a + b, "Random addition failed")) return UNITT_E_FAIL; + if (UNITT_E_FAIL == unitt_det_equal(unitt_add(a, b), a + b, "Random addition failed")) return UNITT_E_FAIL; } return UNITT_E_OK; } -// 测试减法函数,使用随机输入 int test_subtract() { - // 进行10次随机测试 for (int i = 0; i < SINGLE_TCOUNT; i++) { int a = random_input(); int b = random_input(); - if (UNITT_E_FAIL == unitt_det_equal(unit_subtract(a, b), a - b, "Random subtraction failed")) return UNITT_E_FAIL; + if (UNITT_E_FAIL == unitt_det_equal(unitt_subtract(a, b), a - b, "Random subtraction failed")) return UNITT_E_FAIL; } return UNITT_E_OK; } -// 测试乘法函数,使用随机输入 int test_multiply() { - // 进行10次随机测试 for (int i = 0; i < SINGLE_TCOUNT; i++) { int a = random_input(); int b = random_input(); - if (UNITT_E_FAIL == unitt_det_equal(unit_multiply(a, b), a * b, "Random multiplication failed")) return UNITT_E_FAIL; + if (UNITT_E_FAIL == unitt_det_equal(unitt_multiply(a, b), a * b, "Random multiplication failed")) return UNITT_E_FAIL; } return UNITT_E_OK; } -// 测试除法函数,使用随机输入 int test_divide() { - // 进行10次随机测试 for (int i = 0; i < SINGLE_TCOUNT; i++) { int a = random_input(); int b = random_input(); if (b != 0) { - if (UNITT_E_FAIL == unitt_det_float(unit_divide(a, b), (float)a / (float)b, 0.00001, "Random division failed")) return UNITT_E_FAIL; + if (UNITT_E_FAIL == unitt_det_float(unitt_divide(a, b), (float)a / (float)b, 0.00001, "Random division failed")) return UNITT_E_FAIL; } } return UNITT_E_OK; } -void unit_main() +void unitt_main() { static UNITT_TCASE math_tests[] = { UNITT_TCASE(test_add), @@ -172,11 +162,78 @@ void unit_main() UNITT_EXE(suites); } -static void test(void) +static void usage(void) { - srand((unsigned int)time(NULL)); // 初始化随机数种子 - - // unit_main(); - printf("create task %d\r\n", task_create(1000, unit_main)); + printf( +"Usage: unitt [opt] [arg]\n" +"\n" +"options:\n" +" -p Test period, unit ms\n" +" -h Print help\n" +" -v Print version\n" +"\n" + ); } -init_export_app(test); + +static int test(int argc, char *argv[]) +{ + int period = 1000; // ms + + /* reset getopt */ + command_opt_init(); + + while (1) + { + int opt = command_getopt(argc, argv, "p:hv"); + if (opt == -1) break; + + switch (opt) + { + case 'p' : + period = atoi(command_optarg); + break; + case 'v' : + printf("unitt version %d.%d.%d\r\n", UNITT_V_MAJOR, UNITT_V_MINOR, UNITT_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + if (1 == command_optind) // no opt + { + unitt_main(); + return 0; + } + + srand((unsigned int)time(NULL)); + +#if defined(TEST_TARGET_unitt) + while (1) + { + unitt_main(); + usleep(1000 * period); + } +#else + printf("create task %d\r\n", task_create(period, unitt_main)); +#endif + + return 0; +} + +#if defined(TEST_TARGET_unitt) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_unitt(void) +{ + command_export("unitt", test); +} +init_export_app(test_unitt); +#endif diff --git a/test/test_valloc.c b/test/test_valloc.c index 0560eb4..672f56a 100644 --- a/test/test_valloc.c +++ b/test/test_valloc.c @@ -1,10 +1,16 @@ #include #include #include +#if defined(TEST_TARGET_valloc) +#include +#include +#else #include "init.h" +#include "command.h" #include "valloc.h" +#endif -static void test(void) +static void test_base(void) { void* p = NULL; printf("malloc %p\r\n", malloc(0)); @@ -19,4 +25,66 @@ static void test(void) printf("count = %d\r\n", v_check_count()); printf("used = %d\r\n", v_check_used()); } -init_export_app(test); \ No newline at end of file + +static void usage(void) +{ + printf( +"Usage: valloc [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" 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 diff --git a/test/test_vector.c b/test/test_vector.c index 5551e8e..d66967d 100644 --- a/test/test_vector.c +++ b/test/test_vector.c @@ -1,33 +1,66 @@ #include #include +#if defined(TEST_TARGET_vector) +#include +#include +#else #include "init.h" -#include "valloc.h" +#include "command.h" #include "vector.h" +#endif -#define up_multiple(x, mul) ((x)+((mul)-((x)-1)%(mul))-1) /* get the smallest 'mul' multiple larger than 'x' */ -static int gradient_capacity(int size) +static void usage(void) { - int capacity = 1; - if (size <= 1) return 1; - while (capacity < size) capacity <<= 1; - capacity >>= 1; - if (capacity < 4) capacity = capacity << 1; - else if (capacity < 16) capacity = up_multiple(size, capacity >> 1); - else if (capacity < 256) capacity = up_multiple(size, capacity >> 2); - else capacity = up_multiple(size, 64); - return capacity; + printf( +"Usage: vector [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -static void test(void) +static int test(int argc, char *argv[]) { - int size = 0, capacity = 0; - for (size = 1; size < 1024; ) + /* reset getopt */ + command_opt_init(); + + while (1) { - capacity = gradient_capacity(size); - printf("+%d\t%d\r\n", capacity - size + 1, capacity); - size = capacity + 1; + int opt = command_getopt(argc, argv, "hv"); + if (opt == -1) break; + + switch (opt) + { + case 'v' : + printf("vector version %d.%d.%d\r\n", VECTOR_V_MAJOR, VECTOR_V_MINOR, VECTOR_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } } - - v_check_unfree(); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_vector) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_vector(void) +{ + command_export("vector", test); +} +init_export_app(test_vector); +#endif diff --git a/test/test_vlog.c b/test/test_vlog.c index 419618e..752ccbb 100644 --- a/test/test_vlog.c +++ b/test/test_vlog.c @@ -1,13 +1,23 @@ -#include "vlog.h" -#include "init.h" #include +#include +#include +#if defined(TEST_TARGET_vlog) +#include +#include +#else +#include "init.h" +#include "command.h" +#include "vlog.h" +#endif + +#define DEFAULT_OFFLINEFILE "./test/file/log.txt" static void vlog_callback(char *buf, int len) { printf("vlog_callback[%d]: %s", len, buf); } -static void test_vlog(void) +static void test_base(void) { vlog(VLOG_CHANNEL_0, "[VLOG_CHANNEL_0] vlog!\r\n"); vlog(VLOG_CHANNEL_1, "[VLOG_CHANNEL_1] vlog!\r\n"); @@ -26,7 +36,7 @@ static void test_channel(void) static void test_offline(void) { - vlog_set_offline(VLOG_CHANNEL_0, "./test/file/log.txt"); + vlog_set_offline(VLOG_CHANNEL_0, DEFAULT_OFFLINEFILE); for (int i = 0; i < 10; i++) { @@ -36,11 +46,103 @@ static void test_offline(void) vlog_set_offline(VLOG_CHANNEL_0, NULL); } -static void test(void) +static void usage(void) { - // vlog_set_func(vlog_callback); - // test_vlog(); - // test_channel(); - test_offline(); + printf( +"Usage: vlog [opt] [arg]\n" +"\n" +"options:\n" +" -c [] Select the output channel and select by bit, channel 0 by default\n" +" 1: channel 0\n" +" 2: channel 1\n" +" 4: channel 2\n" +" 8: channel 3\n" +" ...\n" +" -f [] Select the offline out file name, \"./test/file/log.txt\" by default\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test base function\n" +" Test channel function\n" +" Test offline function\n" + ); } -init_export_app(test); \ No newline at end of file + +static int test(int argc, char *argv[]) +{ + char *logfile = DEFAULT_OFFLINEFILE; + vlogChnType chn = VLOG_CHANNEL_0; + + /* reset getopt */ + command_opt_init(); + + while (1) + { + int opt = command_getopt(argc, argv, "c::f::hv"); + if (opt == -1) break; + + switch (opt) + { + case 'c' : + chn = atoi(command_optarg); + break; + case 'f' : + if (command_optarg) + { + logfile = command_optarg; + } + else + { + logfile = DEFAULT_OFFLINEFILE; + } + break; + case 'v' : + printf("vlog version %d.%d.%d\r\n", VLOG_V_MAJOR, VLOG_V_MINOR, VLOG_V_PATCH); + return 0; + case '?': + printf("Unknown option `%c`\r\n", command_optopt); + return -1; + case 'h' : + default: + usage(); + return 0; + } + } + + if (logfile) vlog_set_offline(chn, logfile); + vlog(chn, "Hello vlog!\r\n"); + if (logfile) vlog_set_offline(chn, NULL); + + // vlog_set_func(vlog_callback); + for (int index = command_optind; index < argc; index++) + { + if (!strcmp(argv[index], "base")) + { + test_base(); + } + else if (!strcmp(argv[index], "channel")) + { + test_channel(); + } + else if (!strcmp(argv[index], "offline")) + { + test_offline(); + } + } + + return 0; +} + +#if defined(TEST_TARGET_vlog) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_vlog(void) +{ + command_export("vlog", test); +} +init_export_app(test_vlog); +#endif \ No newline at end of file diff --git a/test/test_vstd.c b/test/test_vstd.c index f2ea145..e790b01 100644 --- a/test/test_vstd.c +++ b/test/test_vstd.c @@ -1,10 +1,16 @@ + +#if defined(TEST_TARGET_vstd) +#include +#include +#else #include "init.h" #include "vstdlib.h" #include "vmem.h" +#endif -int printf (const char *__format, ...); +extern int printf (const char *__format, ...); -static void test(void) +static int test(int argc, char *argv[]) { uint8_t *p; @@ -14,7 +20,7 @@ static void test(void) if (!p) { printf("malloc fail!\r\n"); - return; + return 0; } else { @@ -26,5 +32,19 @@ static void test(void) v_free(p); printf("used %d\r\n", vmem_used()); + + return 0; } -init_export_app(test); + +#if defined(TEST_TARGET_vstd) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_vstd(void) +{ + command_export("vstd", test); +} +init_export_app(test_vstd); +#endif \ No newline at end of file diff --git a/test/test_xml.c b/test/test_xml.c index 05513a6..4e0511f 100644 --- a/test/test_xml.c +++ b/test/test_xml.c @@ -1,7 +1,14 @@ #include #include +#if defined(TEST_TARGET_xml) +#include +#include +#else #include "init.h" +#include "command.h" #include "xml.h" +#endif + #define READ_FILE "test/file/read.xml" #define WRITE_FILE "test/file/write.xml" @@ -55,12 +62,70 @@ static void test_write(void) xml_delete(root); } -static void test(void) +static void usage(void) { - printf("xml test!\r\n"); - test_write(); - // test_read(); - - v_check_unfree(); + printf( +"Usage: xml [opt] [arg]\n" +"\n" +"options:\n" +" -h Print help\n" +" -v Print version\n" +"\n" +"argument:\n" +" Test default read function\n" +" Test default write function\n" + ); } -init_export_app(test); + +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("xml version %d.%d.%d\r\n", XML_V_MAJOR, XML_V_MINOR, XML_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], "read")) + { + test_read(); + } + else if (!strcmp(argv[index], "write")) + { + test_write(); + } + } + + return 0; +} + +#if defined(TEST_TARGET_xml) +int main(int argc, char *argv[]) +{ + return test(argc, argv); +} +#else +void test_xml(void) +{ + command_export("xml", test); +} +init_export_app(test_xml); +#endif