]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
add some unit tests for distribution_new_linear
authorSvetlana <lana0771711@gmail.com>
Fri, 31 Jul 2020 09:27:09 +0000 (09:27 +0000)
committerSvetlana <lana0771711@gmail.com>
Fri, 31 Jul 2020 09:27:09 +0000 (09:27 +0000)
src/daemon/distribution_test.c

index cb9a2039923634e81e994758a4f2b33c08b21807..be6d2989cb0fbb712f3650b7315ca2c07094c2e3 100644 (file)
@@ -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;