mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-06 16:56:42 +08:00
164 lines
3.7 KiB
C
164 lines
3.7 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#if defined(TEST_TARGET_intl)
|
|
#include <varch/command.h>
|
|
#include <varch/intl.h>
|
|
#else
|
|
#include "init.h"
|
|
#include "command.h"
|
|
#include "intl.h"
|
|
#endif
|
|
|
|
#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 usage(void)
|
|
{
|
|
printf(
|
|
"Usage: intl [opt] [arg]\n"
|
|
"\n"
|
|
"options:\n"
|
|
" -h Print help\n"
|
|
" -v Print version\n"
|
|
"\n"
|
|
"argument:\n"
|
|
" <read> Test default read function\n"
|
|
" <write> 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;
|
|
}
|
|
|
|
#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
|