From 91615c80e131b6858d24e1231361429f024b2ba1 Mon Sep 17 00:00:00 2001 From: a bellenir Date: Tue, 2 Sep 2014 19:13:10 +0000 Subject: [PATCH] rename rbt_count_nodes to rbt_size; remove testing-only functions from .h --- Rakefile | 2 +- source/rbt/rbt.c | 9 +++++++-- source/rbt/rbt.h | 22 +--------------------- tests/test_rbt.c | 22 ++++++++++++---------- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/Rakefile b/Rakefile index 3241ce8..c62d830 100644 --- a/Rakefile +++ b/Rakefile @@ -24,7 +24,7 @@ end TestEnv = Env.clone do |env| env.build_dir('source','build/obj/test_source') env.build_dir('tests','build/obj/tests/source') - env['CFLAGS'] += ['-g', '--coverage', '-DLEAK_DETECT_LEVEL=1'] + env['CFLAGS'] += ['-g', '--coverage', '-DLEAK_DETECT_LEVEL=1', '-DTESTING'] #env['CFLAGS'] += ['-DNDEBUG'] #disables asserts so they won't effect coverage analysis env["LDFLAGS"] += ['--coverage'] env['CPPPATH'] += Dir['tests/'] diff --git a/source/rbt/rbt.c b/source/rbt/rbt.c index 80cfde1..4faeb80 100644 --- a/source/rbt/rbt.c +++ b/source/rbt/rbt.c @@ -34,6 +34,9 @@ static void rbt_node_free(void* v_node){ if(node->left) mem_release(node->left); if(node->right) mem_release(node->right); } +#ifndef TESTING +static +#endif rbt_node_t* rbt_node_new(void* contents){ rbt_node_t* node = mem_allocate(sizeof(rbt_node_t), &rbt_node_free); node->left = NULL; @@ -48,7 +51,9 @@ rbt_node_t* rbt_node_new(void* contents){ /* ---------------------------------------- */ /* informational / querying functions */ /* ---------------------------------------- */ - +#ifndef TESTING +static +#endif rbt_color_t rbt_node_color(rbt_node_t* node){ //leaves are NULL and black implicitly return (node ? node->color : BLACK); @@ -76,7 +81,7 @@ static rbt_node_t* rightmost_descendant(rbt_node_t* node){ static int rbt_count(rbt_node_t* node){ return (!node ? 0 : (1 + rbt_count(node->left) + rbt_count(node->right))); } -int rbt_count_nodes(rbt_t* tree){ +int rbt_size(rbt_t* tree){ return rbt_count(tree->root); } diff --git a/source/rbt/rbt.h b/source/rbt/rbt.h index dff00c2..adb05bf 100644 --- a/source/rbt/rbt.h +++ b/source/rbt/rbt.h @@ -46,26 +46,6 @@ typedef struct { rbt_t* rbt_new(comparator_t comparator); -/** - * @brief creates a new node for a red-black tree - * - * @param contents pointer to the contents of the node - * - * @return pointer to newly created node - */ -rbt_node_t* rbt_node_new(void* contents); - - -/** - * @brief get the color of a given node - * - * @param node the node on which to operate - * - * @return RED or BLACK. BLACK if node is NULL (a leaf) - */ -rbt_color_t rbt_node_color(rbt_node_t* node); - - /** * @brief find a value in a red-black tree * @@ -85,7 +65,7 @@ rbt_node_t* rbt_lookup(rbt_t* tree, void* value); * * @return the number of nodes present in the tree */ -int rbt_count_nodes(rbt_t* tree); +int rbt_size(rbt_t* tree); /** * @brief insert a value into a red-black tree diff --git a/tests/test_rbt.c b/tests/test_rbt.c index 172f105..0cd9c41 100644 --- a/tests/test_rbt.c +++ b/tests/test_rbt.c @@ -7,6 +7,9 @@ #include "mem.h" #include "list.h" +extern rbt_color_t rbt_node_color(rbt_node_t* node); +extern rbt_node_t* rbt_node_new(void* contents); + static int test_compare(void* a, void* b){ int ia = (int)(mem_unbox(a)); int ib = (int)(mem_unbox(b)); @@ -82,7 +85,6 @@ TEST_SUITE(RBT) { CHECK(NULL == node->right); CHECK(NULL == node->parent); CHECK(box42 == node->contents); - //CHECK(OK == rbt_check_node(node, -1, -1)); //TODO: fix this? mem_release(node); } @@ -417,13 +419,13 @@ TEST_SUITE(RBT) { //------------------------------------------------------------------------- // Test node count function //------------------------------------------------------------------------- - TEST(Verify_count_nodes_works){ + TEST(Verify_size_works){ int i=0; rbt_t* tree = rbt_new(NULL); - CHECK(0 == rbt_count_nodes(tree)); + CHECK(0 == rbt_size(tree)); for(i = 1; i < 10; i++){ rbt_insert(tree, mem_box(i)); - CHECK(i == rbt_count_nodes(tree)); + CHECK(i == rbt_size(tree)); } mem_release(tree); } @@ -2534,19 +2536,19 @@ TEST_SUITE(RBT) { //rbt_t* tree = rbt_new(NULL); rbt_delete(tree, target); CHECK(OK == rbt_check_status(tree)); - CHECK(0 == rbt_count_nodes(tree)); + CHECK(0 == rbt_size(tree)); rbt_insert(tree, box88); rbt_delete(tree, target); CHECK(OK == rbt_check_status(tree)); - CHECK(1 == rbt_count_nodes(tree)); + CHECK(1 == rbt_size(tree)); rbt_insert(tree, box36); rbt_delete(tree, target); CHECK(OK == rbt_check_status(tree)); - CHECK(2 == rbt_count_nodes(tree)); + CHECK(2 == rbt_size(tree)); rbt_insert(tree, box99); rbt_delete(tree, target); CHECK(OK == rbt_check_status(tree)); - CHECK(3 == rbt_count_nodes(tree)); + CHECK(3 == rbt_size(tree)); mem_release(target); mem_release(tree); } @@ -2566,7 +2568,7 @@ TEST_SUITE(RBT) { mem_retain(foo); rbt_insert(tree, foo); listsize++; - CHECK(listsize == rbt_count_nodes(tree)); + CHECK(listsize == rbt_size(tree)); } rbt_status_t status = rbt_check_status(tree); //printf("status after inserts is %d\n", status); @@ -2578,7 +2580,7 @@ TEST_SUITE(RBT) { rbt_delete(tree, foo); list_delete(vals, idx); listsize--; - CHECK(listsize == rbt_count_nodes(tree)); + CHECK(listsize == rbt_size(tree)); } status = rbt_check_status(tree); //printf("status after deletes is %d\n", status); -- 2.52.0