Add test for M_ENUM_OPLIST (in particular I/O tests)
This commit is contained in:
1 parent
68788a6fb8
commit
c80fb7bebe
2 files changed
+70
-18
No files matched your search
+49
-4
@@ -36,6 +36,12 @@ DEQUE_DEF(deque_mpz, testobj_t, TESTOBJ_OPLIST)
|
||||
// Deque with the minimum number of methods.
|
||||
DEQUE_DEF(deque_min_z, testobj_t, (INIT_SET(testobj_init_set), SET(testobj_set), CLEAR(testobj_clear)))
|
||||
|
||||
typedef enum {
|
||||
SUCCESS = 0, NULL_PARAM, INVALID_PARAM
|
||||
} ReturnCode_t;
|
||||
|
||||
DEQUE_DEF(deque_retcode, ReturnCode_t, M_ENUM_OPLIST(ReturnCode_t, SUCCESS))
|
||||
|
||||
#define OPL DEQUE_OPLIST(deque)
|
||||
|
||||
static void test_ti1(int n)
|
||||
@@ -276,12 +282,12 @@ static void test_io(void)
|
||||
M_LET(z, TESTOBJ_OPLIST)
|
||||
M_LET(str, STRING_OPLIST) {
|
||||
// Test empty
|
||||
FILE *f = m_core_fopen ("a-marray.dat", "wt");
|
||||
FILE *f = m_core_fopen ("a-mdeque.dat", "wt");
|
||||
if (!f) abort();
|
||||
deque_mpz_out_str(f, d1);
|
||||
fclose (f);
|
||||
|
||||
f = m_core_fopen ("a-marray.dat", "rt");
|
||||
f = m_core_fopen ("a-mdeque.dat", "rt");
|
||||
if (!f) abort();
|
||||
bool b = deque_mpz_in_str (d2, f);
|
||||
assert (b == true);
|
||||
@@ -294,12 +300,12 @@ static void test_io(void)
|
||||
deque_mpz_push_back (d1, z);
|
||||
}
|
||||
|
||||
f = m_core_fopen ("a-marray.dat", "wt");
|
||||
f = m_core_fopen ("a-mdeque.dat", "wt");
|
||||
if (!f) abort();
|
||||
deque_mpz_out_str(f, d1);
|
||||
fclose (f);
|
||||
|
||||
f = m_core_fopen ("a-marray.dat", "rt");
|
||||
f = m_core_fopen ("a-mdeque.dat", "rt");
|
||||
if (!f) abort();
|
||||
b = deque_mpz_in_str (d2, f);
|
||||
assert (b == true);
|
||||
@@ -340,6 +346,44 @@ static void test_io(void)
|
||||
}
|
||||
}
|
||||
|
||||
static void test_io_enum(void)
|
||||
{
|
||||
deque_retcode_t d1, d2;
|
||||
string_t s;
|
||||
|
||||
string_init(s);
|
||||
deque_retcode_init(d1);
|
||||
deque_retcode_init(d2);
|
||||
|
||||
ReturnCode_t *ret = deque_retcode_push_back_new(d1);
|
||||
assert( *ret == SUCCESS);
|
||||
deque_retcode_push_front(d1, INVALID_PARAM);
|
||||
|
||||
FILE *f = m_core_fopen ("a-mdeque.dat", "wt");
|
||||
if (!f) abort();
|
||||
deque_retcode_out_str(f, d1);
|
||||
fclose (f);
|
||||
|
||||
f = m_core_fopen ("a-mdeque.dat", "rt");
|
||||
if (!f) abort();
|
||||
bool b = deque_retcode_in_str (d2, f);
|
||||
assert (b == true);
|
||||
assert (deque_retcode_equal_p (d1, d2));
|
||||
fclose(f);
|
||||
|
||||
deque_retcode_get_str(s, d1, false);
|
||||
assert (string_equal_str_p(s, "[2,0]"));
|
||||
|
||||
b = deque_retcode_parse_str(d2, string_get_cstr(s), NULL);
|
||||
assert(b == true);
|
||||
assert (deque_retcode_equal_p (d1, d2));
|
||||
|
||||
string_clear(s);
|
||||
deque_retcode_clear(d1);
|
||||
deque_retcode_clear(d2);
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test1();
|
||||
@@ -350,6 +394,7 @@ int main(void)
|
||||
test_it();
|
||||
test_set();
|
||||
test_io();
|
||||
test_io_enum();
|
||||
test_z();
|
||||
test_advance();
|
||||
exit(0);
|
||||
|
||||
+21
-14
@@ -43,7 +43,12 @@ VARIANT_DEF2(v2,
|
||||
(is_int, int),
|
||||
(is_bool, bool) )
|
||||
#define M_OPL_v2_t() VARIANT_OPLIST(v2, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST)
|
||||
|
||||
|
||||
typedef enum {
|
||||
SUCCESS = 0, NULL_PARAM, INVALID_PARAM
|
||||
} my_return_code_t;
|
||||
#define M_OPL_my_return_code_t() M_ENUM_OPLIST(my_return_code_t, SUCCESS)
|
||||
|
||||
TUPLE_DEF2(my,
|
||||
(vala, int),
|
||||
(valb, float),
|
||||
@@ -52,9 +57,10 @@ TUPLE_DEF2(my,
|
||||
(vale, a2_t),
|
||||
(valf, v2_t),
|
||||
(valg, l2_t),
|
||||
(valh, d2_t)
|
||||
(valh, d2_t),
|
||||
(vali, my_return_code_t)
|
||||
)
|
||||
#define M_OPL_my_t() TUPLE_OPLIST(my, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST, STRING_OPLIST, M_OPL_a2_t(), M_OPL_v2_t(), M_OPL_l2_t(), M_OPL_d2_t() )
|
||||
#define M_OPL_my_t() TUPLE_OPLIST(my, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST, STRING_OPLIST, M_OPL_a2_t(), M_OPL_v2_t(), M_OPL_l2_t(), M_OPL_d2_t(), M_OPL_my_return_code_t() )
|
||||
|
||||
TUPLE_DEF2(my2,
|
||||
(activated, bool),
|
||||
@@ -69,7 +75,7 @@ static void test_out_empty(void)
|
||||
my2_init(el1);
|
||||
my2_init(el2);
|
||||
|
||||
FILE *f = m_core_m_core_fopen ("a-mjson.dat", "wt");
|
||||
FILE *f = m_core_fopen ("a-mjson.dat", "wt");
|
||||
if (!f) abort();
|
||||
m_serial_json_write_init(out, f);
|
||||
ret = my2_out_serial(out, el1);
|
||||
@@ -77,15 +83,15 @@ static void test_out_empty(void)
|
||||
m_serial_json_write_clear(out);
|
||||
fclose(f);
|
||||
|
||||
f = m_core_m_core_fopen ("a-mjson.dat", "rt");
|
||||
f = m_core_fopen ("a-mjson.dat", "rt");
|
||||
if (!f) abort();
|
||||
static const char expected[] = "{ \"activated\":false,\"data\":{ \"vala\":0,\"valb\":0.000000,\"valc\":false,\"vald\":\"\",\"vale\":[],\"valf\":{},\"valg\":[],\"valh\":{}}";
|
||||
static const char expected[] = "{ \"activated\":false,\"data\":{ \"vala\":0,\"valb\":0.000000,\"valc\":false,\"vald\":\"\",\"vale\":[],\"valf\":{},\"valg\":[],\"valh\":{},\"vali\":0}";
|
||||
char get[sizeof expected];
|
||||
fgets (get, sizeof expected , f);
|
||||
assert (strcmp(get, expected) == 0);
|
||||
fclose(f);
|
||||
|
||||
f = m_core_m_core_fopen ("a-mjson.dat", "rt");
|
||||
f = m_core_fopen ("a-mjson.dat", "rt");
|
||||
if (!f) abort();
|
||||
m_serial_json_read_init(in, f);
|
||||
ret = my2_in_serial(el2, in);
|
||||
@@ -108,7 +114,7 @@ static void test_out_fill(void)
|
||||
my2_init(el1);
|
||||
my2_init(el2);
|
||||
|
||||
FILE *f = m_core_m_core_fopen("a-mjson.dat", "wt");
|
||||
FILE *f = m_core_fopen("a-mjson.dat", "wt");
|
||||
if (!f) abort();
|
||||
fprintf(f,
|
||||
"{\n"
|
||||
@@ -121,10 +127,11 @@ static void test_out_fill(void)
|
||||
"\"valf\": { \"is_bool\": true },\n"
|
||||
" \"vala\":1742,\n"
|
||||
" \"vald\": \"This is a test\",\n"
|
||||
"\"vali\": 3,"
|
||||
" \"valc\": true } }\n");
|
||||
fclose(f);
|
||||
|
||||
f = m_core_m_core_fopen ("a-mjson.dat", "rt");
|
||||
f = m_core_fopen ("a-mjson.dat", "rt");
|
||||
if (!f) abort();
|
||||
m_serial_json_read_init(in, f);
|
||||
ret = my2_in_serial(el2, in);
|
||||
@@ -132,7 +139,7 @@ static void test_out_fill(void)
|
||||
m_serial_json_read_clear(in);
|
||||
fclose(f);
|
||||
|
||||
f = m_core_m_core_fopen ("a-mjson.dat", "wt");
|
||||
f = m_core_fopen ("a-mjson.dat", "wt");
|
||||
if (!f) abort();
|
||||
m_serial_json_write_init(out, f);
|
||||
ret = my2_out_serial(out, el2);
|
||||
@@ -140,16 +147,16 @@ static void test_out_fill(void)
|
||||
m_serial_json_write_clear(out);
|
||||
fclose(f);
|
||||
|
||||
f = m_core_m_core_fopen ("a-mjson.dat", "rt");
|
||||
f = m_core_fopen ("a-mjson.dat", "rt");
|
||||
if (!f) abort();
|
||||
static const char expected[] = "{ \"activated\":false,\"data\":{ \"vala\":1742,\"valb\":-2.300000,\"valc\":true,\"vald\":\"This is a test\",\"vale\":[1,2,3],\"valf\":{\"is_bool\":true},\"valg\":[1,2,3,4,5,6],\"valh\":{\"steeve\":-4,\"jane\":3}}}";
|
||||
static const char expected2[] = "{ \"activated\":false,\"data\":{ \"vala\":1742,\"valb\":-2.300000,\"valc\":true,\"vald\":\"This is a test\",\"vale\":[1,2,3],\"valf\":{\"is_bool\":true},\"valg\":[1,2,3,4,5,6],\"valh\":{\"jane\":3,\"steeve\":-4}}}";
|
||||
static const char expected[] = "{ \"activated\":false,\"data\":{ \"vala\":1742,\"valb\":-2.300000,\"valc\":true,\"vald\":\"This is a test\",\"vale\":[1,2,3],\"valf\":{\"is_bool\":true},\"valg\":[1,2,3,4,5,6],\"valh\":{\"steeve\":-4,\"jane\":3},\"vali\":3}}";
|
||||
static const char expected2[] = "{ \"activated\":false,\"data\":{ \"vala\":1742,\"valb\":-2.300000,\"valc\":true,\"vald\":\"This is a test\",\"vale\":[1,2,3],\"valf\":{\"is_bool\":true},\"valg\":[1,2,3,4,5,6],\"valh\":{\"jane\":3,\"steeve\":-4},\"vali\":3}}";
|
||||
char get[sizeof expected];
|
||||
fgets (get, sizeof expected , f);
|
||||
assert (strcmp(get, expected) == 0 || strcmp(get, expected2) == 0);
|
||||
fclose(f);
|
||||
|
||||
f = m_core_m_core_fopen ("a-mjson.dat", "rt");
|
||||
f = m_core_fopen ("a-mjson.dat", "rt");
|
||||
if (!f) abort();
|
||||
m_serial_json_read_init(in, f);
|
||||
ret = my2_in_serial(el1, in);
|
||||
|
||||
Reference in new issue
Block a user