diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 9eb8de4..b834e7e 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -1521,8 +1521,8 @@ sw buf_parse_s8 (s_buf *buf, s8 *dest)
negative = true;
if ((r = buf_read_1(buf, "0b")) < 0)
goto restore;
- result += r;
if (r > 0) {
+ result += r;
if ((r = buf_parse_s8_base(buf, &g_c3_base_bin, negative,
dest)) <= 0)
goto restore;
@@ -1531,8 +1531,8 @@ sw buf_parse_s8 (s_buf *buf, s8 *dest)
}
if ((r = buf_read_1(buf, "0o")) < 0)
goto restore;
- result += r;
if (r > 0) {
+ result += r;
if ((r = buf_parse_s8_base(buf, &g_c3_base_oct, negative,
dest)) <= 0)
goto restore;
@@ -1541,8 +1541,8 @@ sw buf_parse_s8 (s_buf *buf, s8 *dest)
}
if ((r = buf_read_1(buf, "0x")) < 0)
goto restore;
- result += r;
if (r > 0) {
+ result += r;
if ((r = buf_parse_s8_base(buf, g_c3_bases_hex, negative,
dest)) < 0)
goto restore;
@@ -2184,100 +2184,114 @@ sw buf_parse_tuple (s_buf *buf, s_tuple *tuple)
return r;
}
-sw buf_parse_u8 (s_buf *buf, u8 *dest)
-{
- sw r;
- sw result = 0;
- s_buf_save save;
- buf_save_init(buf, &save);
- if ((r = buf_read_1(buf, "0b")) < 0)
- goto restore;
- result += r;
- if (r > 0) {
- if ((r = buf_parse_u8_base(buf, &g_c3_base_bin, dest)) <= 0)
- goto restore;
- result += r;
- goto ok;
- }
- if ((r = buf_read_1(buf, "0o")) < 0)
- goto restore;
- result += r;
- if (r > 0) {
- if ((r = buf_parse_u8_base(buf, &g_c3_base_oct, dest)) <= 0)
- goto restore;
- result += r;
- goto ok;
- }
- if ((r = buf_read_1(buf, "0x")) < 0)
- goto restore;
- result += r;
- if (r > 0) {
- if ((r = buf_parse_u8_base(buf, g_c3_bases_hex, dest)) < 0)
- goto restore;
- if (! r && (r = buf_parse_u8_base(buf, g_c3_bases_hex + 1,
- dest)) <= 0)
- goto restore;
- result += r;
- goto ok;
- }
- if ((r = buf_parse_u8_base(buf, &g_c3_base_dec, dest)) <= 0)
- goto restore;
- result += r;
- ok:
- r = result;
- goto clean;
- restore:
- buf_save_restore_rpos(buf, &save);
- clean:
- buf_save_clean(buf, &save);
- return r;
-}
-
-sw buf_parse_u8_base (s_buf *buf, const s_str *base, u8 *dest)
-{
- u8 digit;
- sw r;
- sw radix;
- sw result = 0;
- s_buf_save save;
- u8 tmp = 0;
- assert(buf);
- assert(base);
- assert(dest);
- buf_save_init(buf, &save);
- radix = str_length_utf8(base);
- if (radix < 2 || radix > U8_MAX) {
- assert(! "buf_parse_u8_base: invalid radix");
- errx(1, "buf_parse_u8_base: invalid radix: %ld", radix);
- return -1;
- }
- while ((r = buf_parse_digit(buf, base, &digit)) > 0) {
- result += r;
- if (digit >= radix) {
- assert(! "buf_parse_u8_base: digit not in radix");
- errx(1, "buf_parse_u8_base: digit not in radix: %u", digit);
- return -1;
- }
- if (tmp > ceiling_u8(U8_MAX, radix)) {
- warnx("buf_parse_u8_base: integer overflow");
- goto restore;
- }
- tmp *= radix;
- if (tmp > (u8) (U8_MAX - digit)) {
- warnx("buf_parse_u8_base: integer overflow");
- goto restore;
- }
- tmp += digit;
- }
- *dest = tmp;
- r = result;
- goto clean;
- restore:
- buf_save_restore_rpos(buf, &save);
- clean:
- buf_save_clean(buf, &save);
- return r;
-}
+#define DEF_BUF_PARSE_U(TYPE, type) \
+ sw buf_parse_ ## type (s_buf *buf, type *dest) \
+ { \
+ sw r; \
+ sw result = 0; \
+ s_buf_save save; \
+ buf_save_init(buf, &save); \
+ if ((r = buf_read_1(buf, "0b")) < 0) \
+ goto restore; \
+ result += r; \
+ if (r > 0) { \
+ if ((r = buf_parse_ ## type ## _base(buf, &g_c3_base_bin, \
+ dest)) <= 0) \
+ goto restore; \
+ result += r; \
+ goto ok; \
+ } \
+ if ((r = buf_read_1(buf, "0o")) < 0) \
+ goto restore; \
+ result += r; \
+ if (r > 0) { \
+ if ((r = buf_parse_ ## type ## _base(buf, &g_c3_base_oct, \
+ dest)) <= 0) \
+ goto restore; \
+ result += r; \
+ goto ok; \
+ } \
+ if ((r = buf_read_1(buf, "0x")) < 0) \
+ goto restore; \
+ result += r; \
+ if (r > 0) { \
+ if ((r = buf_parse_## type ##_base(buf, g_c3_bases_hex, \
+ dest)) < 0) \
+ goto restore; \
+ if (! r && (r = buf_parse_ ## type ## _base(buf, \
+ g_c3_bases_hex + 1, \
+ dest)) <= 0) \
+ goto restore; \
+ result += r; \
+ goto ok; \
+ } \
+ if ((r = buf_parse_ ## type ## _base(buf, &g_c3_base_dec, \
+ dest)) <= 0) \
+ goto restore; \
+ result += r; \
+ ok: \
+ r = result; \
+ goto clean; \
+ restore: \
+ buf_save_restore_rpos(buf, &save); \
+ clean: \
+ buf_save_clean(buf, &save); \
+ return r; \
+ } \
+ \
+ sw buf_parse_ ## type ## _base (s_buf *buf, const s_str *base, \
+ type *dest) \
+ { \
+ u8 digit; \
+ sw r; \
+ sw radix; \
+ sw result = 0; \
+ s_buf_save save; \
+ type tmp = 0; \
+ assert(buf); \
+ assert(base); \
+ assert(dest); \
+ buf_save_init(buf, &save); \
+ radix = str_length_utf8(base); \
+ if (radix < 2 || (uw) radix > TYPE ## _MAX) { \
+ assert(! "buf_parse_" # type "_base: invalid radix"); \
+ errx(1, "buf_parse_" # type "_base: invalid radix: %ld", radix); \
+ return -1; \
+ } \
+ while ((r = buf_parse_digit(buf, base, &digit)) > 0) { \
+ result += r; \
+ if (digit >= radix) { \
+ assert(! "buf_parse_" # type "_base: digit not in radix"); \
+ errx(1, "buf_parse_" # type "_base: digit not in radix: %u", \
+ digit); \
+ return -1; \
+ } \
+ if (tmp > ceiling_ ## type (TYPE ## _MAX, radix)) { \
+ warnx("buf_parse_" # type "_base: integer overflow"); \
+ goto restore; \
+ } \
+ tmp *= radix; \
+ if (tmp > (type) (TYPE ## _MAX - digit)) { \
+ warnx("buf_parse_" # type "_base: integer overflow"); \
+ goto restore; \
+ } \
+ tmp += digit; \
+ } \
+ *dest = tmp; \
+ r = result; \
+ goto clean; \
+ restore: \
+ buf_save_restore_rpos(buf, &save); \
+ clean: \
+ buf_save_clean(buf, &save); \
+ return r; \
+ }
+
+DEF_BUF_PARSE_U(U8, u8)
+DEF_BUF_PARSE_U(U16, u16)
+DEF_BUF_PARSE_U(U32, u32)
+DEF_BUF_PARSE_U(U64, u64)
+DEF_BUF_PARSE_U(UW, uw)
sw buf_parse_u64_dec (s_buf *buf, u64 *dest)
{
diff --git a/libc3/c3.c b/libc3/c3.c
index 61fa657..00c1941 100644
--- a/libc3/c3.c
+++ b/libc3/c3.c
@@ -19,8 +19,8 @@
const s_str g_c3_base_bin = {{NULL}, 2, {"01"}};
const s_str g_c3_base_oct = {{NULL}, 8, {"01234567"}};
const s_str g_c3_base_dec = {{NULL}, 10, {"0123456789"}};
-const s_str g_c3_bases_hex[2] = {{{NULL}, 16, {"01234567890abcdef"}},
- {{NULL}, 16, {"01234567890ABCDEF"}}};
+const s_str g_c3_bases_hex[2] = {{{NULL}, 16, {"0123456789abcdef"}},
+ {{NULL}, 16, {"0123456789ABCDEF"}}};
void c3_break ()
{
diff --git a/libc3/tag.c b/libc3/tag.c
index e618bd8..7c26d8a 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -1811,44 +1811,6 @@ s_tag * tag_sym_1 (s_tag *tag, const s8 *p)
return tag_init_sym_1(tag, p);
}
-ffi_type tag_to_ffi_type(const s_tag *tag)
-{
- switch (tag->type.type) {
- case TAG_S8:
- return ffi_type_sint8;
- case TAG_S16:
- return ffi_type_sint16;
- case TAG_S32:
- return ffi_type_sint32;
- case TAG_S64:
- return ffi_type_sint64;
- case TAG_U8:
- return ffi_type_uint8;
- case TAG_U16:
- return ffi_type_uint16;
- case TAG_U32:
- return ffi_type_uint32;
- case TAG_U64:
- return ffi_type_uint64;
- case TAG_F32:
- return ffi_type_float;
- case TAG_F64:
- return ffi_type_double;
- case TAG_VOID:
- return ffi_type_void;
- case TAG_INTEGER:
- return ffi_type_sint;
- case TAG_STR:
- return ffi_type_pointer;
- case TAG_CHARACTER:
- return ffi_type_schar;
- default: ;
- }
- assert(! "tag_to_ffi_type: unknown type");
- errx(1, "tag_to_ffi_type: unknown type");
- return ffi_type_void;
-}
-
void * tag_to_pointer (s_tag *tag, const s_sym *type)
{
if (type == sym_1("tag"))
@@ -2070,6 +2032,69 @@ f_buf_parse tag_type_to_buf_parse (e_tag_type type)
return NULL;
}
+ffi_type * tag_type_to_ffi_type (e_tag_type type)
+{
+ switch (type) {
+ case TAG_ARRAY:
+ return &ffi_type_pointer;
+ case TAG_BOOL:
+ return &ffi_type_uint8;
+ case TAG_CALL:
+ case TAG_CALL_FN:
+ case TAG_CALL_MACRO:
+ return &ffi_type_pointer;
+ case TAG_CFN:
+ return &ffi_type_pointer;
+ case TAG_CHARACTER:
+ return &ffi_type_schar;
+ case TAG_F32:
+ return &ffi_type_float;
+ case TAG_F64:
+ return &ffi_type_double;
+ case TAG_FN:
+ return &ffi_type_pointer;
+ case TAG_IDENT:
+ return &ffi_type_pointer;
+ case TAG_INTEGER:
+ return &ffi_type_sint;
+ case TAG_LIST:
+ return &ffi_type_pointer;
+ case TAG_PTAG:
+ return &ffi_type_pointer;
+ case TAG_QUOTE:
+ return &ffi_type_pointer;
+ case TAG_S8:
+ return &ffi_type_sint8;
+ case TAG_S16:
+ return &ffi_type_sint16;
+ case TAG_S32:
+ return &ffi_type_sint32;
+ case TAG_S64:
+ return &ffi_type_sint64;
+ case TAG_STR:
+ return &ffi_type_pointer;
+ case TAG_SYM:
+ return &ffi_type_pointer;
+ case TAG_TUPLE:
+ return &ffi_type_pointer;
+ case TAG_U8:
+ return &ffi_type_uint8;
+ case TAG_U16:
+ return &ffi_type_uint16;
+ case TAG_U32:
+ return &ffi_type_uint32;
+ case TAG_U64:
+ return &ffi_type_uint64;
+ case TAG_VAR:
+ return &ffi_type_pointer;
+ case TAG_VOID:
+ return &ffi_type_void;
+ }
+ assert(! "tag_type_to_ffi_type: unknown tag type");
+ errx(1, "tag_type_to_ffi_type: unknown tag type");
+ return &ffi_type_void;
+}
+
s8 * tag_type_to_string (e_tag_type type)
{
switch (type) {
diff --git a/libc3/types.h b/libc3/types.h
index 055b6a3..041f2bc 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -47,10 +47,10 @@ typedef uint64_t u64;
#define S64_MIN ((s64) (1 << (8 * sizeof(s64))))
#define SW_MIN ((sw) (1 << (8 * sizeof(sw))))
-#define U8_MAX ((u8) ((1 << 8 * sizeof(u8)) - 1))
-#define U16_MAX ((u16) ((1 << 8 * sizeof(u16)) - 1))
-#define U32_MAX ((u32) ((1 << 8 * sizeof(u32)) - 1))
-#define U64_MAX ((u64) ((1 << 8 * sizeof(u64)) - 1))
+#define U8_MAX ((u8) -1)
+#define U16_MAX ((u16) -1)
+#define U32_MAX ((u32) -1)
+#define U64_MAX ((u64) -1)
#define UW_MAX ((uw) -1)
/* IEEE 754 floating point numbers. */
@@ -58,7 +58,7 @@ typedef float f32;
typedef double f64;
/* Boolean : true or false. */
-typedef s8 bool;
+typedef u8 bool;
/* enums */
typedef enum {
diff --git a/test/buf_parse_test.c b/test/buf_parse_test.c
index 3d44cc0..2b3b489 100644
--- a/test/buf_parse_test.c
+++ b/test/buf_parse_test.c
@@ -286,12 +286,12 @@
#define BUF_PARSE_TEST_NOT_BOOL(test) \
do { \
s_buf buf; \
- bool dest = -1; \
+ bool dest = 2; \
test_context("buf_parse_bool(" # test ") -> 0"); \
buf_init_1(&buf, (test)); \
TEST_EQ(buf_parse_bool(&buf, &dest), 0); \
TEST_EQ(buf.rpos, 0); \
- TEST_EQ(dest, -1); \
+ TEST_EQ(dest, 2); \
buf_clean(&buf); \
test_context(NULL); \
} while (0)
@@ -498,6 +498,17 @@
test_context(NULL); \
} while (0)
+#define BUF_PARSE_TEST_S(bits, test, expected) \
+ do { \
+ s_buf buf; \
+ s ## bits s; \
+ test_context("buf_parse_s" # bits "(" # test ")"); \
+ buf_init_1(&buf, (test)); \
+ TEST_EQ(buf_parse_s ## bits(&buf, &s), strlen(test)); \
+ TEST_EQ(s, (expected)); \
+ test_context(NULL); \
+ } while (0)
+
#define BUF_PARSE_TEST_STR(test, expected) \
do { \
s_buf buf; \
@@ -637,13 +648,13 @@
test_context(NULL); \
} while (0)
-#define BUF_PARSE_TEST_U8(test, expected) \
+#define BUF_PARSE_TEST_U(bits, test, expected) \
do { \
s_buf buf; \
- u8 u; \
- test_context("buf_parse_u8(" # test ")"); \
+ u ## bits u; \
+ test_context("buf_parse_u" # bits "(" # test ")"); \
buf_init_1(&buf, (test)); \
- TEST_EQ(buf_parse_u8(&buf, &u), strlen(test)); \
+ TEST_EQ(buf_parse_u ## bits(&buf, &u), strlen(test)); \
TEST_EQ(u, (expected)); \
test_context(NULL); \
} while (0)
@@ -667,6 +678,7 @@ void buf_parse_test_integer_oct ();
void buf_parse_test_integer_bin ();
void buf_parse_test_ident ();
void buf_parse_test_list ();
+void buf_parse_test_s8 ();
void buf_parse_test_str ();
void buf_parse_test_str_character ();
void buf_parse_test_str_u8 ();
@@ -695,6 +707,7 @@ void buf_parse_test ()
buf_parse_test_integer_hex();
buf_parse_test_integer_oct();
buf_parse_test_integer();
+ buf_parse_test_s8();
buf_parse_test_str();
buf_parse_test_sym();
buf_parse_test_list();
@@ -1096,6 +1109,170 @@ void buf_parse_test_list ()
BUF_PARSE_TEST_LIST("(a, b, c | d)");
}
+void buf_parse_test_s8 ()
+{
+ BUF_PARSE_TEST_S(8, "-0b0", 0);
+ BUF_PARSE_TEST_S(8, "-0b1", -1);
+ BUF_PARSE_TEST_S(8, "-0b00", 0);
+ BUF_PARSE_TEST_S(8, "-0b01", -1);
+ BUF_PARSE_TEST_S(8, "-0b10", -2);
+ BUF_PARSE_TEST_S(8, "-0b11", -3);
+ BUF_PARSE_TEST_S(8, "-0b000", 0);
+ BUF_PARSE_TEST_S(8, "-0b001", -1);
+ BUF_PARSE_TEST_S(8, "-0b010", -2);
+ BUF_PARSE_TEST_S(8, "-0b011", -3);
+ BUF_PARSE_TEST_S(8, "-0b100", -4);
+ BUF_PARSE_TEST_S(8, "-0b101", -5);
+ BUF_PARSE_TEST_S(8, "-0b110", -6);
+ BUF_PARSE_TEST_S(8, "-0b111", -7);
+ BUF_PARSE_TEST_S(8, "-0b10000000", -128);
+ BUF_PARSE_TEST_S(8, "-0b010000000", -128);
+ BUF_PARSE_TEST_S(8, "-0o0", 0);
+ BUF_PARSE_TEST_S(8, "-0o1", -1);
+ BUF_PARSE_TEST_S(8, "-0o2", -2);
+ BUF_PARSE_TEST_S(8, "-0o3", -3);
+ BUF_PARSE_TEST_S(8, "-0o4", -4);
+ BUF_PARSE_TEST_S(8, "-0o5", -5);
+ BUF_PARSE_TEST_S(8, "-0o6", -6);
+ BUF_PARSE_TEST_S(8, "-0o7", -7);
+ BUF_PARSE_TEST_S(8, "-0o00", 0);
+ BUF_PARSE_TEST_S(8, "-0o01", -1);
+ BUF_PARSE_TEST_S(8, "-0o10", -8);
+ BUF_PARSE_TEST_S(8, "-0o11", -9);
+ BUF_PARSE_TEST_S(8, "-0o000", 0);
+ BUF_PARSE_TEST_S(8, "-0o200", -128);
+ BUF_PARSE_TEST_S(8, "-0o0200", -128);
+ BUF_PARSE_TEST_S(8, "-0x0", 0);
+ BUF_PARSE_TEST_S(8, "-0x1", -1);
+ BUF_PARSE_TEST_S(8, "-0x2", -2);
+ BUF_PARSE_TEST_S(8, "-0x3", -3);
+ BUF_PARSE_TEST_S(8, "-0x4", -4);
+ BUF_PARSE_TEST_S(8, "-0x5", -5);
+ BUF_PARSE_TEST_S(8, "-0x6", -6);
+ BUF_PARSE_TEST_S(8, "-0x7", -7);
+ BUF_PARSE_TEST_S(8, "-0x8", -8);
+ BUF_PARSE_TEST_S(8, "-0x9", -9);
+ BUF_PARSE_TEST_S(8, "-0xA", -10);
+ BUF_PARSE_TEST_S(8, "-0xB", -11);
+ BUF_PARSE_TEST_S(8, "-0xC", -12);
+ BUF_PARSE_TEST_S(8, "-0xD", -13);
+ BUF_PARSE_TEST_S(8, "-0xE", -14);
+ BUF_PARSE_TEST_S(8, "-0xF", -15);
+ BUF_PARSE_TEST_S(8, "-0x00", 0);
+ BUF_PARSE_TEST_S(8, "-0x01", -1);
+ BUF_PARSE_TEST_S(8, "-0x10", -16);
+ BUF_PARSE_TEST_S(8, "-0x11", -17);
+ BUF_PARSE_TEST_S(8, "-0x000", 0);
+ BUF_PARSE_TEST_S(8, "-0x80", -128);
+ BUF_PARSE_TEST_S(8, "-0x080", -128);
+ BUF_PARSE_TEST_S(8, "-0", 0);
+ BUF_PARSE_TEST_S(8, "-1", -1);
+ BUF_PARSE_TEST_S(8, "-2", -2);
+ BUF_PARSE_TEST_S(8, "-3", -3);
+ BUF_PARSE_TEST_S(8, "-4", -4);
+ BUF_PARSE_TEST_S(8, "-5", -5);
+ BUF_PARSE_TEST_S(8, "-6", -6);
+ BUF_PARSE_TEST_S(8, "-7", -7);
+ BUF_PARSE_TEST_S(8, "-8", -8);
+ BUF_PARSE_TEST_S(8, "-9", -9);
+ BUF_PARSE_TEST_S(8, "-00", 0);
+ BUF_PARSE_TEST_S(8, "-10", -10);
+ BUF_PARSE_TEST_S(8, "-11", -11);
+ BUF_PARSE_TEST_S(8, "-12", -12);
+ BUF_PARSE_TEST_S(8, "-20", -20);
+ BUF_PARSE_TEST_S(8, "-21", -21);
+ BUF_PARSE_TEST_S(8, "-22", -22);
+ BUF_PARSE_TEST_S(8, "-000", 0);
+ BUF_PARSE_TEST_S(8, "-010", -10);
+ BUF_PARSE_TEST_S(8, "-011", -11);
+ BUF_PARSE_TEST_S(8, "-012", -12);
+ BUF_PARSE_TEST_S(8, "-020", -20);
+ BUF_PARSE_TEST_S(8, "-021", -21);
+ BUF_PARSE_TEST_S(8, "-022", -22);
+ BUF_PARSE_TEST_S(8, "-128", 128);
+ BUF_PARSE_TEST_S(8, "-0128", 128);
+ BUF_PARSE_TEST_S(8, "0b0", 0);
+ BUF_PARSE_TEST_S(8, "0b1", 1);
+ BUF_PARSE_TEST_S(8, "0b00", 0);
+ BUF_PARSE_TEST_S(8, "0b01", 1);
+ BUF_PARSE_TEST_S(8, "0b10", 2);
+ BUF_PARSE_TEST_S(8, "0b11", 3);
+ BUF_PARSE_TEST_S(8, "0b000", 0);
+ BUF_PARSE_TEST_S(8, "0b001", 1);
+ BUF_PARSE_TEST_S(8, "0b010", 2);
+ BUF_PARSE_TEST_S(8, "0b011", 3);
+ BUF_PARSE_TEST_S(8, "0b100", 4);
+ BUF_PARSE_TEST_S(8, "0b101", 5);
+ BUF_PARSE_TEST_S(8, "0b110", 6);
+ BUF_PARSE_TEST_S(8, "0b111", 7);
+ BUF_PARSE_TEST_S(8, "0b1111111", 127);
+ BUF_PARSE_TEST_S(8, "0b01111111", 127);
+ BUF_PARSE_TEST_S(8, "0o0", 0);
+ BUF_PARSE_TEST_S(8, "0o1", 1);
+ BUF_PARSE_TEST_S(8, "0o2", 2);
+ BUF_PARSE_TEST_S(8, "0o3", 3);
+ BUF_PARSE_TEST_S(8, "0o4", 4);
+ BUF_PARSE_TEST_S(8, "0o5", 5);
+ BUF_PARSE_TEST_S(8, "0o6", 6);
+ BUF_PARSE_TEST_S(8, "0o7", 7);
+ BUF_PARSE_TEST_S(8, "0o00", 0);
+ BUF_PARSE_TEST_S(8, "0o01", 1);
+ BUF_PARSE_TEST_S(8, "0o10", 8);
+ BUF_PARSE_TEST_S(8, "0o11", 9);
+ BUF_PARSE_TEST_S(8, "0o000", 0);
+ BUF_PARSE_TEST_S(8, "0o177", 127);
+ BUF_PARSE_TEST_S(8, "0o0177", 127);
+ BUF_PARSE_TEST_S(8, "0x0", 0);
+ BUF_PARSE_TEST_S(8, "0x1", 1);
+ BUF_PARSE_TEST_S(8, "0x2", 2);
+ BUF_PARSE_TEST_S(8, "0x3", 3);
+ BUF_PARSE_TEST_S(8, "0x4", 4);
+ BUF_PARSE_TEST_S(8, "0x5", 5);
+ BUF_PARSE_TEST_S(8, "0x6", 6);
+ BUF_PARSE_TEST_S(8, "0x7", 7);
+ BUF_PARSE_TEST_S(8, "0x8", 8);
+ BUF_PARSE_TEST_S(8, "0x9", 9);
+ BUF_PARSE_TEST_S(8, "0xA", 10);
+ BUF_PARSE_TEST_S(8, "0xB", 11);
+ BUF_PARSE_TEST_S(8, "0xC", 12);
+ BUF_PARSE_TEST_S(8, "0xD", 13);
+ BUF_PARSE_TEST_S(8, "0xE", 14);
+ BUF_PARSE_TEST_S(8, "0xF", 15);
+ BUF_PARSE_TEST_S(8, "0x00", 0);
+ BUF_PARSE_TEST_S(8, "0x01", 1);
+ BUF_PARSE_TEST_S(8, "0x10", 16);
+ BUF_PARSE_TEST_S(8, "0x11", 17);
+ BUF_PARSE_TEST_S(8, "0x000", 0);
+ BUF_PARSE_TEST_S(8, "0x7F", 127);
+ BUF_PARSE_TEST_S(8, "0x07F", 127);
+ BUF_PARSE_TEST_S(8, "0", 0);
+ BUF_PARSE_TEST_S(8, "1", 1);
+ BUF_PARSE_TEST_S(8, "2", 2);
+ BUF_PARSE_TEST_S(8, "3", 3);
+ BUF_PARSE_TEST_S(8, "4", 4);
+ BUF_PARSE_TEST_S(8, "5", 5);
+ BUF_PARSE_TEST_S(8, "6", 6);
+ BUF_PARSE_TEST_S(8, "7", 7);
+ BUF_PARSE_TEST_S(8, "8", 8);
+ BUF_PARSE_TEST_S(8, "9", 9);
+ BUF_PARSE_TEST_S(8, "00", 0);
+ BUF_PARSE_TEST_S(8, "10", 10);
+ BUF_PARSE_TEST_S(8, "11", 11);
+ BUF_PARSE_TEST_S(8, "12", 12);
+ BUF_PARSE_TEST_S(8, "20", 20);
+ BUF_PARSE_TEST_S(8, "21", 21);
+ BUF_PARSE_TEST_S(8, "22", 22);
+ BUF_PARSE_TEST_S(8, "000", 0);
+ BUF_PARSE_TEST_S(8, "010", 10);
+ BUF_PARSE_TEST_S(8, "011", 11);
+ BUF_PARSE_TEST_S(8, "012", 12);
+ BUF_PARSE_TEST_S(8, "020", 20);
+ BUF_PARSE_TEST_S(8, "021", 21);
+ BUF_PARSE_TEST_S(8, "022", 22);
+ BUF_PARSE_TEST_S(8, "127", 127);
+ BUF_PARSE_TEST_S(8, "0127", 127);
+}
+
void buf_parse_test_str ()
{
BUF_PARSE_TEST_NOT_STR("");
@@ -1257,66 +1434,84 @@ void buf_parse_test_tuple ()
void buf_parse_test_u8 ()
{
- BUF_PARSE_TEST_U8("0b0", 0);
- BUF_PARSE_TEST_U8("0b1", 1);
- BUF_PARSE_TEST_U8("0b00", 0);
- BUF_PARSE_TEST_U8("0b01", 1);
- BUF_PARSE_TEST_U8("0b10", 2);
- BUF_PARSE_TEST_U8("0b11", 3);
- BUF_PARSE_TEST_U8("0b000", 0);
- BUF_PARSE_TEST_U8("0b001", 1);
- BUF_PARSE_TEST_U8("0b010", 2);
- BUF_PARSE_TEST_U8("0b011", 3);
- BUF_PARSE_TEST_U8("0b100", 4);
- BUF_PARSE_TEST_U8("0b101", 5);
- BUF_PARSE_TEST_U8("0b110", 6);
- BUF_PARSE_TEST_U8("0b111", 7);
- BUF_PARSE_TEST_U8("0o0", 0);
- BUF_PARSE_TEST_U8("0o1", 1);
- BUF_PARSE_TEST_U8("0o2", 2);
- BUF_PARSE_TEST_U8("0o3", 3);
- BUF_PARSE_TEST_U8("0o4", 4);
- BUF_PARSE_TEST_U8("0o5", 5);
- BUF_PARSE_TEST_U8("0o6", 6);
- BUF_PARSE_TEST_U8("0o7", 7);
- BUF_PARSE_TEST_U8("0o00", 0);
- BUF_PARSE_TEST_U8("0o01", 1);
- BUF_PARSE_TEST_U8("0o10", 8);
- BUF_PARSE_TEST_U8("0o11", 9);
- BUF_PARSE_TEST_U8("0o000", 0);
- BUF_PARSE_TEST_U8("0x0", 0);
- BUF_PARSE_TEST_U8("0x1", 1);
- BUF_PARSE_TEST_U8("0x2", 2);
- BUF_PARSE_TEST_U8("0x3", 2);
- BUF_PARSE_TEST_U8("0x4", 2);
- BUF_PARSE_TEST_U8("0x5", 2);
- BUF_PARSE_TEST_U8("0x6", 2);
- BUF_PARSE_TEST_U8("0x7", 2);
- BUF_PARSE_TEST_U8("0x8", 2);
- BUF_PARSE_TEST_U8("0x9", 2);
- BUF_PARSE_TEST_U8("0xA", 2);
- BUF_PARSE_TEST_U8("0xB", 2);
- BUF_PARSE_TEST_U8("0xC", 2);
- BUF_PARSE_TEST_U8("0xD", 2);
- BUF_PARSE_TEST_U8("0xE", 2);
- BUF_PARSE_TEST_U8("0xF", 2);
- BUF_PARSE_TEST_U8("0x00", 0);
- BUF_PARSE_TEST_U8("0x01", 1);
- BUF_PARSE_TEST_U8("0x10", 16);
- BUF_PARSE_TEST_U8("0x11", 17);
- BUF_PARSE_TEST_U8("0x000", 0);
- BUF_PARSE_TEST_U8("0", 0);
- BUF_PARSE_TEST_U8("1", 1);
- BUF_PARSE_TEST_U8("2", 2);
- BUF_PARSE_TEST_U8("3", 3);
- BUF_PARSE_TEST_U8("4", 4);
- BUF_PARSE_TEST_U8("5", 5);
- BUF_PARSE_TEST_U8("6", 6);
- BUF_PARSE_TEST_U8("7", 7);
- BUF_PARSE_TEST_U8("8", 8);
- BUF_PARSE_TEST_U8("9", 9);
- BUF_PARSE_TEST_U8("00", 0);
- BUF_PARSE_TEST_U8("10", 10);
- BUF_PARSE_TEST_U8("11", 11);
- BUF_PARSE_TEST_U8("000", 0);
+ BUF_PARSE_TEST_U(8, "0b0", 0);
+ BUF_PARSE_TEST_U(8, "0b1", 1);
+ BUF_PARSE_TEST_U(8, "0b00", 0);
+ BUF_PARSE_TEST_U(8, "0b01", 1);
+ BUF_PARSE_TEST_U(8, "0b10", 2);
+ BUF_PARSE_TEST_U(8, "0b11", 3);
+ BUF_PARSE_TEST_U(8, "0b000", 0);
+ BUF_PARSE_TEST_U(8, "0b001", 1);
+ BUF_PARSE_TEST_U(8, "0b010", 2);
+ BUF_PARSE_TEST_U(8, "0b011", 3);
+ BUF_PARSE_TEST_U(8, "0b100", 4);
+ BUF_PARSE_TEST_U(8, "0b101", 5);
+ BUF_PARSE_TEST_U(8, "0b110", 6);
+ BUF_PARSE_TEST_U(8, "0b111", 7);
+ BUF_PARSE_TEST_U(8, "0b1111111", 127);
+ BUF_PARSE_TEST_U(8, "0b01111111", 127);
+ BUF_PARSE_TEST_U(8, "0o0", 0);
+ BUF_PARSE_TEST_U(8, "0o1", 1);
+ BUF_PARSE_TEST_U(8, "0o2", 2);
+ BUF_PARSE_TEST_U(8, "0o3", 3);
+ BUF_PARSE_TEST_U(8, "0o4", 4);
+ BUF_PARSE_TEST_U(8, "0o5", 5);
+ BUF_PARSE_TEST_U(8, "0o6", 6);
+ BUF_PARSE_TEST_U(8, "0o7", 7);
+ BUF_PARSE_TEST_U(8, "0o00", 0);
+ BUF_PARSE_TEST_U(8, "0o01", 1);
+ BUF_PARSE_TEST_U(8, "0o10", 8);
+ BUF_PARSE_TEST_U(8, "0o11", 9);
+ BUF_PARSE_TEST_U(8, "0o000", 0);
+ BUF_PARSE_TEST_U(8, "0o177", 127);
+ BUF_PARSE_TEST_U(8, "0o0177", 127);
+ BUF_PARSE_TEST_U(8, "0x0", 0);
+ BUF_PARSE_TEST_U(8, "0x1", 1);
+ BUF_PARSE_TEST_U(8, "0x2", 2);
+ BUF_PARSE_TEST_U(8, "0x3", 3);
+ BUF_PARSE_TEST_U(8, "0x4", 4);
+ BUF_PARSE_TEST_U(8, "0x5", 5);
+ BUF_PARSE_TEST_U(8, "0x6", 6);
+ BUF_PARSE_TEST_U(8, "0x7", 7);
+ BUF_PARSE_TEST_U(8, "0x8", 8);
+ BUF_PARSE_TEST_U(8, "0x9", 9);
+ BUF_PARSE_TEST_U(8, "0xA", 10);
+ BUF_PARSE_TEST_U(8, "0xB", 11);
+ BUF_PARSE_TEST_U(8, "0xC", 12);
+ BUF_PARSE_TEST_U(8, "0xD", 13);
+ BUF_PARSE_TEST_U(8, "0xE", 14);
+ BUF_PARSE_TEST_U(8, "0xF", 15);
+ BUF_PARSE_TEST_U(8, "0x00", 0);
+ BUF_PARSE_TEST_U(8, "0x01", 1);
+ BUF_PARSE_TEST_U(8, "0x10", 16);
+ BUF_PARSE_TEST_U(8, "0x11", 17);
+ BUF_PARSE_TEST_U(8, "0x000", 0);
+ BUF_PARSE_TEST_U(8, "0x7F", 127);
+ BUF_PARSE_TEST_U(8, "0x07F", 127);
+ BUF_PARSE_TEST_U(8, "0", 0);
+ BUF_PARSE_TEST_U(8, "1", 1);
+ BUF_PARSE_TEST_U(8, "2", 2);
+ BUF_PARSE_TEST_U(8, "3", 3);
+ BUF_PARSE_TEST_U(8, "4", 4);
+ BUF_PARSE_TEST_U(8, "5", 5);
+ BUF_PARSE_TEST_U(8, "6", 6);
+ BUF_PARSE_TEST_U(8, "7", 7);
+ BUF_PARSE_TEST_U(8, "8", 8);
+ BUF_PARSE_TEST_U(8, "9", 9);
+ BUF_PARSE_TEST_U(8, "00", 0);
+ BUF_PARSE_TEST_U(8, "10", 10);
+ BUF_PARSE_TEST_U(8, "11", 11);
+ BUF_PARSE_TEST_U(8, "12", 12);
+ BUF_PARSE_TEST_U(8, "20", 20);
+ BUF_PARSE_TEST_U(8, "21", 21);
+ BUF_PARSE_TEST_U(8, "22", 22);
+ BUF_PARSE_TEST_U(8, "000", 0);
+ BUF_PARSE_TEST_U(8, "010", 10);
+ BUF_PARSE_TEST_U(8, "011", 11);
+ BUF_PARSE_TEST_U(8, "012", 12);
+ BUF_PARSE_TEST_U(8, "020", 20);
+ BUF_PARSE_TEST_U(8, "021", 21);
+ BUF_PARSE_TEST_U(8, "022", 22);
+ BUF_PARSE_TEST_U(8, "127", 127);
+ BUF_PARSE_TEST_U(8, "0127", 127);
}
diff --git a/test/cfn_test.c b/test/cfn_test.c
index 863578c..07a70a9 100644
--- a/test/cfn_test.c
+++ b/test/cfn_test.c
@@ -17,52 +17,45 @@
#include "../libc3/tag.h"
#include "test.h"
-void cfn_test ();
+/* 1 */
+bool cfn_test_not (bool a);
+
+/* 2 */
void cfn_test_apply ();
void cfn_test_copy ();
void cfn_test_init_clean ();
void cfn_test_link ();
-bool cfn_test_not (bool a);
-void cfn_test_set_type ();
+void cfn_test_prep_cif ();
+
+/* 3 */
+void cfn_test ();
void cfn_test ()
{
cfn_test_init_clean();
- cfn_test_apply();
cfn_test_copy();
cfn_test_link();
- cfn_test_set_type();
-}
-
-bool cfn_test_not (bool a)
-{
- return a ? false : true;
-}
-
-void cfn_test_init_clean ()
-{
- s_cfn tmp;
- TEST_EQ(cfn_init(&tmp, sym_1("cfn_test_not"),
- list_1("(:bool)"),
- sym_1("bool")), &tmp);
- cfn_clean(&tmp);
- test_ok();
+ cfn_test_prep_cif();
+ cfn_test_apply();
}
void cfn_test_apply ()
{
s_list *args;
s_tag result;
- s_cfn tmp;
- cfn_init(&tmp, sym_1("cfn_test_not"),
+ s_cfn a;
+ cfn_init(&a, sym_1("cfn_test_not"),
list_1("(:bool)"),
sym_1("bool"));
+ cfn_link(&a);
+ cfn_prep_cif(&a);
args = list_new(NULL, NULL);
tag_init_bool(&args->tag, false);
- TEST_EQ(cfn_apply(&tmp, args, &result), &result);
+ TEST_EQ(cfn_apply(&a, args, &result), &result);
+ TEST_EQ(result.type.type, TAG_BOOL);
TEST_EQ(result.data.bool, true);
list_delete_all(args);
- cfn_clean(&tmp);
+ cfn_clean(&a);
}
void cfn_test_copy ()
@@ -89,19 +82,91 @@ void cfn_test_copy ()
a_arg_types = list_next(a_arg_types);
b_arg_types = list_next(b_arg_types);
}
- TEST_EQ(a.cif, b.cif);
+ TEST_EQ(a.cif.abi, b.cif.abi);
+ TEST_EQ(a.cif.nargs, b.cif.nargs);
+ TEST_EQ(memcmp(a.cif.arg_types, b.cif.arg_types,
+ a.cif.nargs * sizeof(ffi_type *)), 0);
+ TEST_EQ(a.cif.rtype, b.cif.rtype);
+ TEST_EQ(a.cif.bytes, b.cif.bytes);
+ TEST_EQ(a.cif.flags, b.cif.flags);
cfn_clean(&b);
cfn_clean(&a);
}
+void cfn_test_init_clean ()
+{
+ s_cfn a;
+ TEST_EQ(cfn_init(&a, sym_1("cfn_test_not"),
+ list_1("(:bool)"),
+ sym_1("bool")), &a);
+ TEST_EQ(a.name, sym_1("cfn_test_not"));
+ TEST_EQ(a.ptr.f, 0);
+ TEST_EQ(a.ptr.p, 0);
+ TEST_EQ(a.arity, 1);
+ TEST_EQ(a.result_type, sym_1("bool"));
+ TEST_EQ(a.arg_result, false);
+ TEST_EQ(a.arg_types->tag.type.type, TAG_SYM);
+ TEST_EQ(a.arg_types->tag.data.sym, sym_1("bool"));
+ TEST_EQ(a.arg_types->next.type.type, TAG_LIST);
+ TEST_EQ(a.arg_types->next.data.list, NULL);
+ TEST_EQ(a.cif.abi, 0);
+ TEST_EQ(a.cif.nargs, 0);
+ TEST_EQ(a.cif.arg_types, 0);
+ TEST_EQ(a.cif.rtype, 0);
+ TEST_EQ(a.cif.bytes, 0);
+ TEST_EQ(a.cif.flags, 0);
+ cfn_clean(&a);
+ test_ok();
+}
+
void cfn_test_link ()
{
- s_cfn tmp;
- cfn_init(&tmp);
- tmp.name
- cfn_clean(&tmp);
+ s_cfn a;
+ s_cfn b;
+ cfn_init(&a, sym_1("cfn_test_not"),
+ list_1("(:bool)"),
+ sym_1("bool"));
+ b = a;
+ TEST_EQ(cfn_link(&a), &a);
+ TEST_EQ(a.name, b.name);
+ TEST_EQ(a.ptr.f, &cfn_test_not);
+ TEST_EQ(a.arity, b.arity);
+ TEST_EQ(a.result_type, b.result_type);
+ TEST_EQ(a.arg_result, b.arg_result);
+ TEST_EQ(a.arg_types, b.arg_types);
+ TEST_EQ(a.cif.abi, b.cif.abi);
+ TEST_EQ(a.cif.nargs, b.cif.nargs);
+ TEST_EQ(a.cif.arg_types, b.cif.arg_types);
+ TEST_EQ(a.cif.rtype, b.cif.rtype);
+ TEST_EQ(a.cif.bytes, b.cif.bytes);
+ TEST_EQ(a.cif.flags, b.cif.flags);
+ cfn_clean(&a);
+}
+
+bool cfn_test_not (bool a)
+{
+ return a ? false : true;
}
-void cfn_test_set_type ()
+void cfn_test_prep_cif ()
{
+ s_cfn a;
+ s_cfn b;
+ cfn_init(&a, sym_1("cfn_test_not"),
+ list_1("(:bool)"),
+ sym_1("bool"));
+ b = a;
+ TEST_EQ(cfn_prep_cif(&a), &a);
+ TEST_EQ(a.name, b.name);
+ TEST_EQ(a.ptr.f, b.ptr.f);
+ TEST_EQ(a.ptr.p, b.ptr.p);
+ TEST_EQ(a.arity, b.arity);
+ TEST_EQ(a.result_type, b.result_type);
+ TEST_EQ(a.arg_result, b.arg_result);
+ TEST_EQ(a.arg_types, b.arg_types);
+ TEST_EQ(a.cif.nargs, 1);
+ TEST_ASSERT(a.cif.arg_types);
+ TEST_EQ(a.cif.arg_types[0], &ffi_type_uint8);
+ TEST_EQ(a.cif.rtype, &ffi_type_uint8);
+ cfn_clean(&a);
}