Commit 00797520e9ea072d6c7fcecf9098bdd07cf2027f

Thomas de Grivel 2024-08-29T02:11:30

wip tests

diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index b2c61a1..a486966 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -4658,8 +4658,11 @@ sw buf_parse_var (s_buf *buf, s_var *dest)
   if ((r = buf_read_1(buf, "0x")) < 0)
     goto ok;
   if (r > 0) {
+    result += r;
     if ((r = buf_parse_uw_hexadecimal(buf, &i)) < 0)
       goto restore;
+    result += r;
+    tmp.ptr = (s_tag *) i;
   }
   if (buf_peek_character_utf8(buf, &c) > 0 &&
       ! ident_character_is_reserved(c)) {
diff --git a/libkc3/inspect.c b/libkc3/inspect.c
index e3eddc8..99e6630 100644
--- a/libkc3/inspect.c
+++ b/libkc3/inspect.c
@@ -331,3 +331,29 @@ s_str * inspect_tuple (const s_tuple *tuple, s_str *dest)
   }
   return buf_to_str(&buf, dest);
 }
+
+s_str * inspect_var (const s_var *var, s_str *dest)
+{
+  s_pretty pretty = {0};
+  sw size;
+  s_buf tmp;
+  size = buf_inspect_var_size(&pretty, var);
+  if (size < 0) {
+    err_puts("inspect_var: buf_inspect_var_size error");
+    assert(! "inspect_var: buf_inspect_var_size error");
+    return NULL;
+  }
+  if (! buf_init_alloc(&tmp, size)) {
+    err_puts("inspect_var: buf_init alloc");
+    assert(! "inspect_var: buf_init_alloc");
+    return NULL;
+  }
+  buf_inspect_var(&tmp, var);
+  if (tmp.wpos != tmp.size) {
+    err_puts("inspect_var: tmp.wpos != tmp.size");
+    assert(! "inspect_var: tmp.wpos != tmp.size");
+    buf_clean(&tmp);
+    return NULL;
+  }
+  return buf_to_str(&tmp, dest);
+}
diff --git a/libkc3/inspect.h b/libkc3/inspect.h
index d2786dd..1ee693a 100644
--- a/libkc3/inspect.h
+++ b/libkc3/inspect.h
@@ -34,5 +34,6 @@ s_str * inspect_struct (const s_struct *s, s_str *dest);
 s_str * inspect_sym (const s_sym *sym, s_str *dest);
 s_str * inspect_tag (const s_tag *tag, s_str *dest);
 s_str * inspect_tuple (const s_tuple *tuple, s_str *dest);
+s_str * inspect_var (const s_var *var, s_str *dest);
 
 #endif /* LIBKC3_INSPECT_H */
diff --git a/libkc3/var.c b/libkc3/var.c
index c27ed60..2e617c9 100644
--- a/libkc3/var.c
+++ b/libkc3/var.c
@@ -10,7 +10,10 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
+#include <string.h>
 #include "assert.h"
+#include "buf.h"
+#include "buf_parse.h"
 #include "data.h"
 #include "sym.h"
 #include "tag.h"
@@ -45,6 +48,26 @@ s_var * var_init (s_var *var, s_tag *ptr, const s_sym *type)
   return var;
 }
 
+s_var * var_init_1 (s_var *var, const char *p)
+{
+  s_buf buf;
+  uw len;
+  sw r;
+  s_var tmp;
+  assert(var);
+  assert(p);
+  len = strlen(p);
+  buf_init_const(&buf, len, p);
+  buf.wpos = len;
+  r = buf_parse_var(&buf, &tmp);
+  if (r < 0 || (uw) r != len) {
+    err_puts("var_init_1: invalid var");
+    assert(! "var_init_1: invalid var");
+    return NULL;
+  }
+  return var;
+}
+
 s_var * var_init_cast (s_var *var, const s_sym * const *type,
                        const s_tag *src)
 {
diff --git a/libkc3/var.h b/libkc3/var.h
index d25f395..95ff67e 100644
--- a/libkc3/var.h
+++ b/libkc3/var.h
@@ -17,6 +17,7 @@
 
 /* Stack-allocation compatible functions. */
 s_var * var_init (s_var *var, s_tag *ptr, const s_sym *type);
+s_var * var_init_1 (s_var *var, const char *p);
 s_var * var_init_cast (s_var *tag, const s_sym * const *type,
                        const s_tag *src);
 s_var * var_init_copy (s_var *tag, const s_var *src);
diff --git a/test/buf_parse_test.c b/test/buf_parse_test.c
index b642301..493f27a 100644
--- a/test/buf_parse_test.c
+++ b/test/buf_parse_test.c
@@ -611,6 +611,18 @@
     test_context(NULL);                                                \
   } while (0)
 
+#define BUF_PARSE_TEST_VAR(test, expected)                             \
+  do {                                                                 \
+    s_buf buf;                                                         \
+    s_var dest = {0};                                                  \
+    test_context("buf_parse_var(" # test ")");                         \
+    buf_init_1(&buf, false, (test));                                   \
+    TEST_EQ(buf_parse_var(&buf, &dest), strlen(test));                 \
+    TEST_EQ(dest.type, expected.type);                                 \
+    TEST_EQ(dest.ptr, expected.ptr);                                   \
+    test_context(NULL);                                                \
+  } while (0)
+
 TEST_CASE_PROTOTYPE(buf_parse_array);
 TEST_CASE_PROTOTYPE(buf_parse_bool);
 TEST_CASE_PROTOTYPE(buf_parse_call);
@@ -639,6 +651,7 @@ TEST_CASE_PROTOTYPE(buf_parse_sym);
 TEST_CASE_PROTOTYPE(buf_parse_tag);
 TEST_CASE_PROTOTYPE(buf_parse_tuple);
 TEST_CASE_PROTOTYPE(buf_parse_unquote);
+TEST_CASE_PROTOTYPE(buf_parse_var);
 
 void buf_parse_test (void)
 {
@@ -670,6 +683,7 @@ void buf_parse_test (void)
   TEST_CASE_RUN(buf_parse_tag);
   TEST_CASE_RUN(buf_parse_tuple);
   TEST_CASE_RUN(buf_parse_unquote);
+  TEST_CASE_RUN(buf_parse_var);
 #ifdef KC3_TEST_BUF_PARSE_SU
   TEST_CASE_RUN(buf_parse_u8_binary);
   TEST_CASE_RUN(buf_parse_u8_octal);
@@ -1368,6 +1382,19 @@ TEST_CASE(buf_parse_unquote)
 }
 TEST_CASE_END(buf_parse_unquote)
 
+TEST_CASE(buf_parse_var)
+{
+  s_var expected;
+  expected.type = &g_sym_Tag;
+  expected.ptr = NULL;
+  BUF_PARSE_TEST_VAR("?", expected);
+  expected.type = &g_sym_U8;
+  BUF_PARSE_TEST_VAR("(U8) ?", expected);
+  expected.ptr = (s_tag *)  0x123456789abcdef0;
+  BUF_PARSE_TEST_VAR("(U8) ?0x123456789abcdef0", expected);
+}
+TEST_CASE_END(buf_parse_var)
+
 TEST_CASE(buf_parse_uw)
 {
 }
diff --git a/test/inspect_test.c b/test/inspect_test.c
index 422cddf..411ea2c 100644
--- a/test/inspect_test.c
+++ b/test/inspect_test.c
@@ -26,6 +26,7 @@
 #include "../libkc3/tag.h"
 #include "../libkc3/struct.h"
 #include "../libkc3/tuple.h"
+#include "../libkc3/var.h"
 #include "test.h"
 
 #define INSPECT_TEST_ARRAY(test, expected)                             \
@@ -188,6 +189,19 @@
     test_context(NULL);                                                \
   } while (0)
 
+#define INSPECT_TEST_VAR(test, expected)                             \
+  do {                                                                 \
+    s_var var_test;                                                    \
+    s_str str_result;                                                  \
+    test_context("inspect_var(" # test ") -> " # expected);            \
+    var_init_1(&var_test, (test));                                     \
+    TEST_EQ(inspect_var(&var_test, &str_result), &str_result);         \
+    TEST_STRNCMP(str_result.ptr.p, (expected), str_result.size);       \
+    TEST_EQ(str_result.size, strlen(expected));                        \
+    str_clean(&str_result);                                            \
+    test_context(NULL);                                                \
+  } while (0)
+
 TEST_CASE_PROTOTYPE(inspect_array);
 TEST_CASE_PROTOTYPE(inspect_bool);
 TEST_CASE_PROTOTYPE(inspect_call);
@@ -199,6 +213,7 @@ TEST_CASE_PROTOTYPE(inspect_str);
 TEST_CASE_PROTOTYPE(inspect_struct);
 TEST_CASE_PROTOTYPE(inspect_sym);
 TEST_CASE_PROTOTYPE(inspect_tuple);
+TEST_CASE_PROTOTYPE(inspect_var);
 
 void inspect_test (void)
 {
@@ -213,6 +228,7 @@ void inspect_test (void)
   TEST_CASE_RUN(inspect_struct);
   TEST_CASE_RUN(inspect_sym);
   TEST_CASE_RUN(inspect_tuple);
+  TEST_CASE_RUN(inspect_var);
 }
 
 TEST_CASE(inspect_array)
@@ -492,3 +508,12 @@ TEST_CASE(inspect_tuple)
                      "{{:a, :b}, {:c, :d}, {:e, :f}}");
 }
 TEST_CASE_END(inspect_tuple)
+
+TEST_CASE(inspect_var)
+{
+  INSPECT_TEST_VAR("?", "?");
+  INSPECT_TEST_VAR("(U8) ?", "(U8) ?");
+  INSPECT_TEST_VAR("(U8) ?0x123456789abcdef0",
+                   "(U8) ?0x123456789abcdef0");
+}
+TEST_CASE_END(inspect_var)