Add I/O tests for variants.

Fix bugs in I/O operators for variants.
This commit is contained in:
Patrick Pelissier committed 2017-03-04 19:45:28 +01:00
1 parent 3b99c5b4b4
commit 709cd07995
2 files changed
+46 -3

No files matched your search

+6 -3
View File
@@ -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; \
}
+40
View File
@@ -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;
}