diff --git a/m-variant.h b/m-variant.h index d2254f6b..e5ab9ec2 100644 --- a/m-variant.h +++ b/m-variant.h @@ -248,7 +248,7 @@ } #define VARIANTI_DEFINE_GET_STR_FUNC(name, a) \ case M_C3(name, _, VARIANTI_GET_FIELD a): \ - func(str, "@" M_AS_STR(VARIANTI_GET_FIELD a) "@"); \ + func(str, "@" M_APPLY (M_AS_STR, VARIANTI_GET_FIELD a) "@"); \ VARIANTI_GET_STR a (str, el -> value . VARIANTI_GET_FIELD a, true); \ break; @@ -266,7 +266,7 @@ } #define VARIANTI_DEFINE_OUT_STR_FUNC(name, a) \ case M_C3(name, _, VARIANTI_GET_FIELD a): \ - fprintf(f, "@" M_AS_STR(VARIANTI_GET_FIELD a) "@"); \ + fprintf(f, "@" M_APPLY(M_AS_STR, VARIANTI_GET_FIELD a) "@"); \ VARIANTI_GET_OUT_STR a (f, el -> value . VARIANTI_GET_FIELD a); \ break; @@ -286,6 +286,7 @@ } \ variantTypeBuf[i++] = 0; \ assert(i < sizeof(variantTypeBuf)); \ + M_C(name, _clear)(el); \ /* In function of the type */ \ if (strcmp(variantTypeBuf, "EMPTY") == 0) { \ el->type = M_C(name, _EMPTY); \ @@ -295,7 +296,9 @@ return fgetc(f) == '@'; \ } #define VARIANTI_DEFINE_IN_STR_FUNC(name, a) \ - else if (strcmp (variantTypeBuf, M_AS_STR(VARIANTI_GET_FIELD a)) == 0) { \ + else if (strcmp (variantTypeBuf, M_APPLY (M_AS_STR, VARIANTI_GET_FIELD a)) == 0) { \ + el->type = M_C3(name, _, VARIANTI_GET_FIELD a); \ + VARIANTI_GET_INIT a (el ->value . VARIANTI_GET_FIELD a ); \ bool b = VARIANTI_GET_IN_STR a (el -> value . VARIANTI_GET_FIELD a, f); \ if (!b) return false; \ } diff --git a/tests/test-mvariant.c b/tests/test-mvariant.c index 03b72d07..80c98c54 100644 --- a/tests/test-mvariant.c +++ b/tests/test-mvariant.c @@ -128,10 +128,50 @@ test_triple(void) VARIANT_DEF2(single, (s, string_t, STRING_OPLIST)) +static void +test_io(void) +{ + M_LET(s, STRING_OPLIST) + M_LET(x, y, VARIANT_OPLIST(single, STRING_OPLIST)) { + // Check empty + FILE *f = fopen ("a.dat", "wt"); + if (!f) abort(); + single_out_str(f, x); + fclose (f); + + f = fopen ("a.dat", "rt"); + bool b = single_in_str (y, f); + assert (b == true); + fclose(f); + assert (single_equal_p (x, y)); + + single_get_str(s, x, false); + assert(string_equal_str_p(s, "@EMPTY@@")); + + // Fill in the variant + single_set_s (x, s); + + f = fopen ("a.dat", "wt"); + if (!f) abort(); + single_out_str(f, x); + fclose (f); + + f = fopen ("a.dat", "rt"); + b = single_in_str (y, f); + assert (b == true); + fclose(f); + assert (single_equal_p (x, y)); + + single_get_str(s, x, false); + assert(string_equal_str_p(s, "@s@\"@EMPTY@@\"@")); + } +} + int main(void) { test_pair(); test_triple(); + test_io(); return 0; }