#include #include #if defined(TEST_TARGET_intl) #include #include #include #else #include "init.h" #include "command.h" #include "unitt.h" #include "kern.h" #include "intl.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 ************************************/ /************************************************************************************/ #define INTL_P_DEC 0x01 #define INTL_P_BIN 0x02 #define INTL_P_HEX 0x04 static void print_intl(intl a, uint8_t type) { char buffer[INTL_MAX_BIN]; if (type & INTL_P_DEC) printf("intl (decimal): %s\n", intl_sdec(a, buffer)); if (type & INTL_P_HEX) printf("intl (hex): %s\n", intl_shex(a, buffer)); if (type & INTL_P_BIN) printf("intl (bin): %s\n", intl_sbin(a, buffer)); } static void test_define(void) { intl a = intl(0); intl b = intl(10); intl c = intl(-10); intl d = intl(0xFF); intl e = intl_from("0"); intl f = intl_from("100"); intl g = intl_from("-100"); intl h = intl_from("123456789123456789123456789"); intl i = intl_from("0b1110000001111100101010100"); intl j = intl_from("0o1236541236763210233642166"); intl k = intl_from("0xF125E3F6D743648EEFFF12356"); intl max = INTL_MAX; intl min = INTL_MIN; intl n0 = INTL_ZERO; } static void test_print(void) { intl a = intl_from("123456789123456789123456789"); print_intl(a, INTL_P_DEC); print_intl(a, INTL_P_BIN); print_intl(a, INTL_P_HEX); } static void test_calculate(void) { intl a = intl_from("123456789123456789123456789"); intl b = intl_from("987654321987654321987654321"); printf("a: \r\n"); print_intl(a, INTL_P_DEC); printf("b: \r\n"); print_intl(b, INTL_P_DEC); printf("a + b: \r\n"); print_intl(intl_add(a, b), INTL_P_DEC); printf("a - b: \r\n"); print_intl(intl_sub(a, b), INTL_P_DEC); printf("a * b: \r\n"); print_intl(intl_mul(a, b), INTL_P_DEC); printf("b / a: \r\n"); print_intl(intl_div(b, a), INTL_P_DEC); printf("b %% a: \r\n"); print_intl(intl_mod(b, a), INTL_P_DEC); printf("a & b: \r\n"); print_intl(intl_and(a, b), INTL_P_DEC); printf("a | b: \r\n"); print_intl(intl_or(a, b), INTL_P_DEC); printf("a ^ b: \r\n"); print_intl(intl_xor(a, b), INTL_P_DEC); printf("~a: \r\n"); print_intl(intl_not(a), INTL_P_DEC); printf("a << 1: \r\n"); print_intl(intl_shl(a, 1), INTL_P_DEC); printf("b >> 1: \r\n"); print_intl(intl_shr(b, 1), INTL_P_DEC); printf("compare: %d\r\n", intl_cmp(a, b)); printf("a incremented: \r\n"); print_intl(intl_inc(a), INTL_P_DEC); printf("b decremented: \r\n"); print_intl(intl_dec(b), INTL_P_DEC); } static void test_base(void) { // test_define(); // test_print(); test_calculate(); } /************************************************************************************/ /************************************* Command ************************************/ /************************************************************************************/ static void usage(void) { printf( "Usage: intl [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" " -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("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; } } if (execute) { if (!strcmp(execute, "base")) { test_base(); } else if (!strcmp(execute, "ut")) { srand((unsigned int)time(NULL)); #if defined(TEST_TARGET_intl) while (1) { unitt_task(); usleep(1000 * ut_period); } #else printf("create task %d\r\n", task_create(ut_period, unitt_task)); #endif } } else { test_base(); } return 0; } /************************************************************************************/ /************************************ Test entry ************************************/ /************************************************************************************/ #if defined(TEST_TARGET_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