Add test for OUT_STR / IN_STR & GET_STR for BRTREE

Fix bugs
This commit is contained in:
Patrick Pelissier committed 2017-03-09 22:24:34 +01:00
1 parent 018e09a4b3
commit e164b3fb44
2 files changed
+75 -6

No files matched your search

+14 -5
View File
@@ -158,7 +158,7 @@ typedef enum {
} \
\
static inline void \
M_C3(rbtree_, name, _clear)(tree_t tree) \
M_C3(rbtree_, name, _clean)(tree_t tree) \
{ \
RBTREEI_CONTRACT(tree); \
node_t *stack[RBTREEI_MAX_STACK]; \
@@ -190,6 +190,12 @@ typedef enum {
} \
tree->node = NULL; \
tree->size = 0; \
} \
\
static inline void \
M_C3(rbtree_, name, _clear)(tree_t tree) \
{ \
M_C3(rbtree_, name, _clean)(tree); \
} \
\
static inline void \
@@ -794,13 +800,13 @@ typedef enum {
, /* NO HASH METHOD */ ) \
\
M_IF_METHOD(GET_STR, oplist)( \
static inline size_t M_C3(rbtree_,name,_get_str)(string_t str, \
static inline void M_C3(rbtree_,name,_get_str)(string_t str, \
const tree_t t1, bool append) { \
RBTREEI_CONTRACT(t1); \
assert(str != NULL); \
(append ? string_cat_str : string_set_str) (str, "["); \
/* NOTE: The print is really naive, and not really efficient */ \
bool commaToPrint = false \
bool commaToPrint = false; \
M_C3(rbtree_it_, name, _t) it1; \
M_C3(rbtree_, name, _it)(it1, t1); \
while (!M_C3(rbtree_, name, _end_p)(it1)) { \
@@ -808,7 +814,8 @@ typedef enum {
string_push_back (str, M_GET_SEPARATOR oplist); \
commaToPrint = true; \
const type *ref1 = M_C3(rbtree_, name, _cref)(it1); \
M_GET_STR oplist(str, *ref1, true); \
M_GET_GET_STR oplist(str, *ref1, true); \
M_C3(rbtree_, name, _next)(it1); \
} \
string_push_back (str, ']'); \
} \
@@ -849,7 +856,9 @@ typedef enum {
M_GET_INIT oplist (item); \
do { \
bool b = M_GET_IN_STR oplist (item, file); \
if (!b) { M_GET_CLEAR oplist (item); return false; } \
if (!b) { \
c = fgetc(file); break; \
} \
M_C3(rbtree_, name, _push)(rbtree, item); \
c = fgetc(file); \
} while (c == M_GET_SEPARATOR oplist && !feof(file) && !ferror(file)); \
+61 -1
View File
@@ -22,10 +22,27 @@
*/
#include <stdio.h>
#include <gmp.h> // For testing purpose only.
#include "m-string.h"
#include "m-rbtree.h"
static bool uint_in_str(unsigned int *u, FILE *f)
{
int n = fscanf(f, "%u", u);
return n == 1;
}
static void uint_out_str(FILE *f, unsigned int u)
{
fprintf(f, "%u", u);
}
static void uint_get_str(string_t str, unsigned int u, bool append)
{
(append ? string_cat_printf : string_printf) (str, "%u", u);
}
START_COVERAGE
RBTREE_DEF(uint, unsigned int)
RBTREE_DEF(uint, unsigned int, M_OPLIST_CAT((IN_STR(uint_in_str M_IPTR), OUT_STR(uint_out_str), GET_STR(uint_get_str)) , M_DEFAULT_OPLIST) )
RBTREE_DEF(float, float)
END_COVERAGE
@@ -97,10 +114,53 @@ static void test_float(void)
}
}
}
static void test_io(void)
{
M_LET(str, STRING_OPLIST)
M_LET(tree1, tree2, UINT_OPLIST) {
// Empty one
FILE *f = fopen ("a.dat", "wt");
if (!f) abort();
rbtree_uint_out_str(f, tree1);
fclose (f);
f = fopen ("a.dat", "rt");
if (!f) abort();
bool b = rbtree_uint_in_str (tree2, f);
assert (b == true);
assert (rbtree_uint_equal_p (tree1, tree2));
fclose(f);
rbtree_uint_get_str(str, tree1, false);
assert(string_equal_str_p(str, "[]"));
// Fill in data
for(unsigned int i = 0 ; i < 10; i++)
rbtree_uint_push(tree1, i);
f = fopen ("a.dat", "wt");
if (!f) abort();
rbtree_uint_out_str(f, tree1);
fclose (f);
f = fopen ("a.dat", "rt");
if (!f) abort();
b = rbtree_uint_in_str (tree2, f);
assert (b == true);
assert (rbtree_uint_equal_p (tree1, tree2));
fclose(f);
rbtree_uint_get_str(str, tree1, false);
assert(string_equal_str_p(str, "[0,1,2,3,4,5,6,7,8,9]"));
}
}
int main(void)
{
test_uint();
test_float();
test_io();
return 0;
}