From 97c2fb7cdcc7b568bbaecdc3a2e629a369cf3f9b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=9Fingen?= Date: Mon, 10 Jul 2017 14:02:57 +0200 Subject: [PATCH] Fix tests for NH multiple recursion levels Tests were no passing. Besides, added cases to test more than 1 level depth in nexthop recursion. --- tests/lib/test_nexthop_iter.c | 98 +++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 26 deletions(-) diff --git a/tests/lib/test_nexthop_iter.c b/tests/lib/test_nexthop_iter.c index 6c8ffb8da1..5c5550a644 100644 --- a/tests/lib/test_nexthop_iter.c +++ b/tests/lib/test_nexthop_iter.c @@ -107,6 +107,29 @@ nexthop_chain_add_top(struct nexthop_chain *nc) str_appendf(&nc->repr, "%p\n", nh); } +static void +add_string_representation (char **repr, struct nexthop *nh) +{ + struct nexthop *parent; + + /* add indentations first */ + parent = nh->rparent; + while (parent) + { + str_appendf(repr, " "); + parent = parent->rparent; + } + str_appendf(repr, "%p\n", nh); +} + +static void +start_recursive_chain (struct nexthop_chain *nc, struct nexthop *nh) +{ + SET_FLAG(nc->current_top->flags, NEXTHOP_FLAG_RECURSIVE); + nc->current_top->resolved = nh; + nh->rparent = nc->current_top; + nc->current_recursive = nh; +} static void nexthop_chain_add_recursive(struct nexthop_chain *nc) { @@ -120,36 +143,52 @@ nexthop_chain_add_recursive(struct nexthop_chain *nc) { nc->current_recursive->next = nh; nh->prev = nc->current_recursive; + nh->rparent = nc->current_recursive->rparent; nc->current_recursive = nh; } else + start_recursive_chain (nc, nh); + + add_string_representation (&nc->repr, nh); +} + +static void +nexthop_chain_add_recursive_level(struct nexthop_chain *nc) +{ + struct nexthop *nh; + + nh = calloc(sizeof(*nh), 1); + assert(nh); + + assert(nc->current_top); + if (nc->current_recursive) { - SET_FLAG(nc->current_top->flags, NEXTHOP_FLAG_RECURSIVE); - nc->current_top->resolved = nh; + SET_FLAG(nc->current_recursive->flags, NEXTHOP_FLAG_RECURSIVE); + nc->current_recursive->resolved = nh; + nh->rparent = nc->current_recursive; nc->current_recursive = nh; } - str_appendf(&nc->repr, " %p\n", nh); + else + start_recursive_chain (nc, nh); + + add_string_representation (&nc->repr, nh); } +static void +nexthop_clear_recursive (struct nexthop *tcur) +{ + if (!tcur) + return; + if (CHECK_FLAG(tcur->flags, NEXTHOP_FLAG_RECURSIVE)) + nexthop_clear_recursive (tcur->resolved); + if (tcur->next) + nexthop_clear_recursive (tcur->next); + free (tcur); +} static void nexthop_chain_clear(struct nexthop_chain *nc) { - struct nexthop *tcur, *tnext; - - for (tcur = nc->head; tcur; tcur = tnext) - { - tnext = tcur->next; - if (CHECK_FLAG(tcur->flags, NEXTHOP_FLAG_RECURSIVE)) - { - struct nexthop *rcur, *rnext; - for (rcur = tcur->resolved; rcur; rcur = rnext) - { - rnext = rcur->next; - free(rcur); - } - } - free(tcur); - } + nexthop_clear_recursive (nc->head); nc->head = nc->current_top = nc->current_recursive = NULL; free(nc->repr); nc->repr = NULL; @@ -177,12 +216,7 @@ nexthop_chain_verify_iter(struct nexthop_chain *nc) char *repr = NULL; for (ALL_NEXTHOPS_RO(nc->head, nh)) - { - if (nh->rparent) - str_appendf(&repr, " %p\n", nh); - else - str_appendf(&repr, "%p\n", nh); - } + add_string_representation (&repr, nh); if (repr && verbose) printf("===\n%s", repr); @@ -233,6 +267,15 @@ test_run_first(void) nexthop_chain_add_recursive(nc); nexthop_chain_verify_iter(nc); + nexthop_chain_add_recursive_level(nc); + nexthop_chain_verify_iter(nc); + + nexthop_chain_add_recursive(nc); + nexthop_chain_verify_iter(nc); + + nexthop_chain_add_recursive(nc); + nexthop_chain_verify_iter(nc); + nexthop_chain_free(nc); } @@ -267,10 +310,13 @@ test_run_prng(void) case 6: case 7: case 8: - case 9: if (nc->current_top) nexthop_chain_add_recursive(nc); break; + case 9: + if (nc->current_top) + nexthop_chain_add_recursive_level(nc); + break; } nexthop_chain_verify_iter(nc); } -- 2.39.5