From e164b3fb447e5b6d5acca23fb69f1c81e10df4ff Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 9 Mar 2017 22:24:34 +0100 Subject: [PATCH] Add test for OUT_STR / IN_STR & GET_STR for BRTREE Fix bugs --- m-rbtree.h | 19 ++++++++++---- tests/test-mrbtree.c | 62 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/m-rbtree.h b/m-rbtree.h index 1000fb29..cbc87d2f 100644 --- a/m-rbtree.h +++ b/m-rbtree.h @@ -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)); \ diff --git a/tests/test-mrbtree.c b/tests/test-mrbtree.c index f8f96787..e50e5ac3 100644 --- a/tests/test-mrbtree.c +++ b/tests/test-mrbtree.c @@ -22,10 +22,27 @@ */ #include #include // 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; }