From a30b783692380116480b61319571b430ba9cb1cd Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Tue, 28 Jun 2022 23:28:45 +0200 Subject: [PATCH] First basic test of tree container --- tests/test-mtree.c | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 tests/test-mtree.c diff --git a/tests/test-mtree.c b/tests/test-mtree.c new file mode 100644 index 00000000..e8461354 --- /dev/null +++ b/tests/test-mtree.c @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2017-2022, Patrick Pelissier + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#include + +#include "m-tree.h" + +#include "test-obj.h" +#include "m-string.h" +#include "coverage.h" + +#define M_GET_STRING_INT(str, x, append) \ + (append ? m_string_cat_printf : m_string_printf) (str, "%d", x) + +START_COVERAGE +TREE_DEF(tree, int, M_OPEXTEND(M_DEFAULT_OPLIST, GET_STR(M_GET_STRING_INT))) +END_COVERAGE +TREE_DEF(tree_mpz, testobj_t, TESTOBJ_OPLIST) + +static void test_basic(void) +{ + tree_t t; + string_t s; + tree_init(t); + string_init(s); + + assert( tree_size(t) == 0); + assert( tree_empty_p(t) == true); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[]")); + + tree_it_t root = tree_set_root(t, 0); + assert( tree_size(t) == 1); + assert( tree_empty_p(t) == false); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{0}]")); + + tree_it_t it = tree_insert_child(root, 2); + assert( tree_size(t) == 2); + assert( tree_empty_p(t) == false); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{0,[{2}]}]")); + + tree_insert_down(root, 1); + assert( tree_size(t) == 3); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{0,[{1,[{2}]}]}]")); + + tree_insert_left(it, 3); + assert( tree_size(t) == 4); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{0,[{1,[{3},{2}]}]}]")); + + tree_it_t it4 = tree_insert_right(it, 4); + assert( tree_size(t) == 5); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{0,[{1,[{3},{2},{4}]}]}]")); + + root = tree_insert_up(root, -1); + assert( tree_size(t) == 6); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{-1,[{0,[{1,[{3},{2},{4}]}]}]}]")); + + tree_insert_up(it, 5); + assert( tree_size(t) == 7); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{-1,[{0,[{1,[{3},{5,[{2}]},{4}]}]}]}]")); + + tree_insert_child(it4, 6); + assert( tree_size(t) == 8); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{-1,[{0,[{1,[{3},{5,[{2}]},{4,[{6}]}]}]}]}]")); + + tree_insert_child(it4, 7); + assert( tree_size(t) == 9); + tree_get_str(s, t, false); + assert(string_equal_str_p(s, "[{-1,[{0,[{1,[{3},{5,[{2}]},{4,[{7},{6}]}]}]}]}]")); + + string_clear(s); + tree_clear(t); +} + +int main(void) +{ + test_basic(); + return 0; +}