From: Svetlana Date: Fri, 31 Jul 2020 09:27:09 +0000 (+0000) Subject: add some unit tests for distribution_new_linear X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2a86e2061f284ffe28a69c8edb6c911c5dc9cc96;p=thirdparty%2Fcollectd.git add some unit tests for distribution_new_linear --- diff --git a/src/daemon/distribution_test.c b/src/daemon/distribution_test.c index cb9a20399..be6d2989c 100644 --- a/src/daemon/distribution_test.c +++ b/src/daemon/distribution_test.c @@ -34,21 +34,25 @@ DEF_TEST(distribution_new_linear) { size_t num_buckets; double size; double *want_get; + int want_err; } cases[] = { { .num_buckets = 0, .size = 5, .want_get = NULL, + .want_err = EINVAL, }, { .num_buckets = 3, .size = -5, .want_get = NULL, + .want_err = EINVAL, }, { .num_buckets = 5, .size = 0, .want_get = NULL, + .want_err = EINVAL, }, { .num_buckets = 3, @@ -58,12 +62,17 @@ DEF_TEST(distribution_new_linear) { }; for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { printf("## Case %zu:\n", i); - distribution_t *d; - if (cases[i].want_get == NULL) { - EXPECT_EQ_PTR(NULL, d = distribution_new_linear(cases[i].num_buckets, cases[i].size)); + if (cases[i].want_err != 0) { + EXPECT_EQ_PTR(cases[i].want_get, distribution_new_linear(cases[i].num_buckets, cases[i].size)); + EXPECT_EQ_INT(cases[i].want_err, errno); continue; } + distribution_t *d; CHECK_NOT_NULL(d = distribution_new_linear(cases[i].num_buckets, cases[i].size)); + buckets_array_t buckets_array = get_buckets(d); + for (size_t j = 0; j < cases[i].num_buckets; j++) { + EXPECT_EQ_DOUBLE(cases[i].want_get[j], buckets_array.buckets[j].maximum); + } distribution_destroy(d); } return 0;