From 73e15eb00c24cef654bd938d22a7c5c0dfbd7c23 Mon Sep 17 00:00:00 2001 From: hanen mizouni Date: Fri, 25 Oct 2019 12:52:22 +0000 Subject: [PATCH] test:improve coverage of brick --- src/brick-int.h | 2 +- src/brick.c | 7 +- tests/core/test-core.c | 183 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 184 insertions(+), 8 deletions(-) diff --git a/src/brick-int.h b/src/brick-int.h index 3afad9afd..3ce9b8bb8 100644 --- a/src/brick-int.h +++ b/src/brick-int.h @@ -207,7 +207,7 @@ struct pg_brick *pg_brick_decref(struct pg_brick *brick, int pg_brick_reset(struct pg_brick *brick, struct pg_error **errp); /* testing */ -uint32_t pg_brick_links_count_get(const struct pg_brick *brick, +int pg_brick_links_count_get(const struct pg_brick *brick, const struct pg_brick *target, struct pg_error **errp); int64_t pg_brick_refcount(const struct pg_brick *brick); diff --git a/src/brick.c b/src/brick.c index df1c28f07..e359b8b11 100644 --- a/src/brick.c +++ b/src/brick.c @@ -321,17 +321,17 @@ static uint16_t count_side(const struct pg_brick_side *side, * @param errp a return pointer for an error message * @return the total number of link from the brick to the target */ -uint32_t pg_brick_links_count_get(const struct pg_brick *brick, +int pg_brick_links_count_get(const struct pg_brick *brick, const struct pg_brick *target, struct pg_error **errp) { - uint32_t count = 0; + int count = 0; enum pg_side i; const struct pg_brick_side *side; if (!brick) { *errp = pg_error_new("brick is NULL"); - return 0; + return -1; } if (brick->type == PG_MULTIPOLE) { @@ -349,6 +349,7 @@ uint32_t pg_brick_links_count_get(const struct pg_brick *brick, if (side->edge.link && side->edge.link == target) ++count; } else { + *errp = pg_error_new("brick config type is unknown"); return -1; } diff --git a/tests/core/test-core.c b/tests/core/test-core.c index cd85e63a0..2dc130462 100644 --- a/tests/core/test-core.c +++ b/tests/core/test-core.c @@ -30,8 +30,52 @@ static void test_brick_core_simple_lifecycle(void) { struct pg_error *error = NULL; struct pg_brick *brick; - struct pg_brick_config *config = pg_brick_config_new("mybrick", 2, 2, - PG_MULTIPOLE); + struct pg_brick_config *config = pg_brick_config_new("mybrick", 2, 2, PG_MULTIPOLE); + struct pg_brick_config *config1 = pg_brick_config_new("mybrick", 2, 2, PG_MONOPOLE); + struct pg_brick_config *config2 = pg_brick_config_new("mybrick", UINT16_MAX, UINT16_MAX, PG_MULTIPOLE); + + brick = pg_brick_new("nop", config1, &error); + g_assert(!brick); + g_assert(error); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "A 'PG_MONOPOLE' cannot have more than one neibour per side"); + pg_error_free(error); + error = NULL; + + brick = pg_brick_new("nop", config2, &error); + g_assert(!brick); + g_assert(error); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "A 'PG_MULTIPOLE' cannot have more than 65535 edge on PG_WEST_SIDE"); + pg_error_free(error); + error = NULL; + + brick = pg_brick_new(NULL, NULL, &error); + g_assert(!brick); + g_assert(error); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "Brick config not set"); + pg_error_free(error); + error = NULL; + + brick = pg_brick_new(NULL, config, &error); + g_assert(!brick); + g_assert(error); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "Brick name not set"); + pg_error_free(error); + error = NULL; + + pg_brick_decref(brick, &error); + g_assert(error); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "NULL brick"); + pg_error_free(error);error = NULL; + + g_assert(pg_brick_reset(brick,&error) < 0); + g_assert(error); + pg_error_free(error); + error = NULL; brick = pg_brick_new("foo", config, &error); g_assert(!brick); @@ -44,9 +88,11 @@ static void test_brick_core_simple_lifecycle(void) brick = pg_brick_new("nop", config, &error); g_assert(brick); g_assert(!error); + printf("brick refcount is %ld\n",brick->refcount); pg_brick_decref(brick, &error); g_assert(!error); + printf("brick refcount is %ld\n",brick->refcount); brick = pg_brick_decref(NULL, &error); g_assert(!brick); @@ -107,6 +153,11 @@ static void test_brick_core_link(void) g_assert(middle_brick); g_assert(!error); + g_assert(pg_brick_reset(middle_brick,&error)!=0); + g_assert(error); + pg_error_free(error); + error = NULL; + east_brick = pg_brick_new("nop", config, &error); g_assert(east_brick); g_assert(!error); @@ -180,6 +231,22 @@ static void test_brick_core_unlink_edge(void) g_assert(ret == 0); g_assert(!error); + ret = pg_brick_link(west_brick, west_brick, &error); + g_assert(ret == -1); + g_assert(error); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "Can not link a brick to itself"); + pg_error_free(error); + error = NULL; + + ret = pg_brick_link(NULL, west_brick, &error); + g_assert(ret == -1); + g_assert(error); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "Node is not valid"); + pg_error_free(error); + error = NULL; + ret = pg_brick_link(middle_brick, east_brick, &error); g_assert(ret == 0); g_assert(!error); @@ -199,6 +266,14 @@ static void test_brick_core_unlink_edge(void) g_assert(error); pg_error_free(error); error = NULL; + g_assert(pg_brick_unlink_edge(east_brick, NULL, &error) == -1); + g_assert(error); + pg_error_free(error); + error = NULL; + g_assert(pg_brick_unlink_edge(NULL, west_brick, &error) == -1); + g_assert(error); + pg_error_free(error); + error = NULL; g_assert(!pg_brick_unlink_edge(west_brick, middle_brick, &error)); refcount = pg_brick_refcount(west_brick); @@ -628,9 +703,19 @@ static void test_brick_sanity_check_expected(struct pg_brick *brick, static void test_brick_core_verify_multiple_link(void) { - struct pg_brick *west_brick, *middle_brick, *east_brick; + struct pg_brick *west_brick, *middle_brick, *east_brick, + *west_brick1, *middle_brick1, *east_brick1, + *west_brick2, *middle_brick2, *east_brick2, + *west_brick3, *middle_brick3, *east_brick3; + struct pg_brick_config *config = pg_brick_config_new("mybrick", 4, 4, PG_MULTIPOLE); + struct pg_brick_config *config1 = pg_brick_config_new("mybrick", 1, 1, + PG_DIPOLE); + struct pg_brick_config *config2 = pg_brick_config_new("mybrick", 1, 1, + PG_MONOPOLE); + struct pg_brick_config *config3 = pg_brick_config_new("mybrick", 1, 1, + 4); uint32_t links_count; struct pg_error *error = NULL; @@ -641,6 +726,27 @@ static void test_brick_core_verify_multiple_link(void) east_brick = pg_brick_new("nop", config, &error); g_assert(!error); + west_brick1 = pg_brick_new("nop", config1, &error); + g_assert(!error); + middle_brick1 = pg_brick_new("nop", config1, &error); + g_assert(!error); + east_brick1 = pg_brick_new("nop", config1, &error); + g_assert(!error); + + west_brick2 = pg_brick_new("nop", config2, &error); + g_assert(!error); + middle_brick2 = pg_brick_new("nop", config2, &error); + g_assert(!error); + east_brick2 = pg_brick_new("nop", config2, &error); + g_assert(!error); + + west_brick3 = pg_brick_new("nop", config3, &error); + g_assert(!error); + middle_brick3 = pg_brick_new("nop", config3, &error); + g_assert(!error); + east_brick3 = pg_brick_new("nop", config3, &error); + g_assert(!error); + /* create a few links */ pg_brick_link(west_brick, middle_brick, &error); g_assert(!error); @@ -652,13 +758,61 @@ static void test_brick_core_verify_multiple_link(void) g_assert(!error); pg_brick_link(middle_brick, east_brick, &error); g_assert(!error); + pg_brick_link(middle_brick1, east_brick1, &error); + g_assert(!error); + pg_brick_link(middle_brick2, east_brick2, &error); + g_assert(!error); + + pg_brick_link(middle_brick1, west_brick1, &error); + g_assert(error); + pg_error_free(error); + error = NULL; + + pg_brick_link(middle_brick2, west_brick2, &error); + g_assert(error); + pg_error_free(error); + error = NULL; + pg_brick_link(middle_brick3, west_brick3, &error); + g_assert(error); + pg_error_free(error); + error = NULL; + + pg_brick_link(west_brick3, middle_brick3, &error); + g_assert(error); + pg_error_free(error); + error = NULL; + /* sanity checks */ test_brick_sanity_check(west_brick); test_brick_sanity_check(middle_brick); test_brick_sanity_check(east_brick); - /* check the link count */ + /* check the link count */ + links_count = pg_brick_links_count_get(NULL, west_brick, &error); + g_assert(error); + g_assert(links_count == (uint32_t) -1); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "brick is NULL"); + pg_error_free(error); + error = NULL; + + links_count = pg_brick_links_count_get(middle_brick3, west_brick3, &error); + g_assert(error); + g_assert(links_count == (uint32_t)-1); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "brick config type is unknown"); + pg_error_free(error); + error = NULL; + + links_count = pg_brick_links_count_get(east_brick3, west_brick3, &error); + g_assert(error); + g_assert(links_count == (uint32_t)-1); + g_assert(error->message); + g_assert_cmpstr(error->message, ==, "brick config type is unknown"); + pg_error_free(error); + error = NULL; + links_count = pg_brick_links_count_get(west_brick, west_brick, &error); g_assert(!error); g_assert(links_count == 0); @@ -694,6 +848,22 @@ static void test_brick_core_verify_multiple_link(void) g_assert(!error); g_assert(links_count == 3); + links_count = pg_brick_links_count_get(middle_brick1,east_brick1, &error); + g_assert(!error); + g_assert(links_count == 1); + + links_count = pg_brick_links_count_get(middle_brick2, east_brick2, &error); + g_assert(!error); + g_assert(links_count == 1); + + links_count = pg_brick_links_count_get(west_brick2, middle_brick2, &error); + g_assert(!error); + g_assert(links_count == 0); + + links_count = pg_brick_links_count_get(east_brick1, east_brick1, &error); + g_assert(!error); + g_assert(links_count == 0); + /* unlink the west brick */ pg_brick_unlink(west_brick, &error); @@ -821,6 +991,11 @@ static void test_brick_core_verify_re_link(void) test_brick_sanity_check_expected(f, 1, 1); test_brick_sanity_check_expected(a, 1, 0); + pg_brick_chained_links(&e,v,NULL); + g_assert(e); + pg_error_free(e); + e = NULL; + /* Unlink f */ pg_brick_unlink(f, &e); g_assert(!e);