]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
add unit tests for custom constructor + add inf checker to custom constructor
authorSvetlana <lana0771711@gmail.com>
Mon, 3 Aug 2020 09:25:22 +0000 (09:25 +0000)
committerSvetlana <lana0771711@gmail.com>
Mon, 3 Aug 2020 09:25:22 +0000 (09:25 +0000)
src/daemon/distribution.c
src/daemon/distribution_test.c

index df1579b32c6cbf6e59b370d2e6e20006768a9378..0269eb8461dd864404ddffc8b61dbdafbcc68f81 100644 (file)
@@ -141,6 +141,10 @@ distribution_t* distribution_new_custom(size_t array_size, double *custom_bucket
       return NULL;
     }
   }
+  if (array_size > 0 && custom_buckets_boundaries[array_size - 1] == INFINITY) {
+    errno = EINVAL;
+    return NULL;
+  }
 
   size_t num_buckets = array_size + 1;
   bucket_t bucket_array[num_buckets];
index 6b3660445fc0310d95efba55f9d8903d67aeed88..f175fbcc5ab42bb77882ae9e0aac68164f21fcfc 100644 (file)
@@ -215,8 +215,80 @@ DEF_TEST(distribution_new_exponential) {
   return 0;
 }
 
+DEF_TEST(distribution_new_custom) {
+  struct {
+    size_t array_size;
+    double *custom_boundaries;
+    double *want_get;
+    int want_err;
+  } cases[] = {
+    {
+      .array_size = 0,
+      .want_get = (double[]){INFINITY},
+    },
+    {
+      .array_size = 5,
+      .custom_boundaries = (double[]){0, 1, 2, 3, 4},
+      .want_get = NULL,
+      .want_err = EINVAL,
+    },
+    {
+      .array_size = 3,
+      .custom_boundaries = (double[]){-5, 7, 3},
+      .want_get = NULL,
+      .want_err = EINVAL,
+    },
+    {
+      .array_size = 4,
+      .custom_boundaries = (double[]){5.7, 6.0, 6.0, 7.0},
+      .want_get = NULL,
+      .want_err = EINVAL,
+    },
+    {
+      .array_size = 1,
+      .custom_boundaries = (double[]){105.055},
+      .want_get = (double[]){105.055, INFINITY},
+    },
+    {
+      .array_size = 5,
+      .custom_boundaries = (double[]){8, 100, 1000, 1008, INFINITY},
+      .want_get = NULL,
+      .want_err = EINVAL,
+    },
+    {
+      .array_size = 7,
+      .custom_boundaries = (double[]){2, 4, 8, 6, 2, 16, 77.5},
+      .want_get = NULL,
+      .want_err = EINVAL,
+    },
+    {
+      .array_size = 10,
+      .custom_boundaries = (double[]){77.5, 100.203, 122.01, 137.23, 200, 205, 210, 220, 230, 256},
+      .want_get = (double[]){77.5, 100.203, 122.01, 137.23, 200, 205, 210, 220, 230, 256, INFINITY},
+    },
+  };
+
+  for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
+    if (cases[i].want_err != 0) {
+      EXPECT_EQ_PTR(cases[i].want_get, distribution_new_custom(cases[i].array_size, cases[i].custom_boundaries));
+      EXPECT_EQ_INT(cases[i].want_err, errno);
+      continue;
+    }
+    distribution_t *d;
+    CHECK_NOT_NULL(d = distribution_new_custom(cases[i].array_size, cases[i].custom_boundaries));
+    buckets_array_t buckets_array = get_buckets(d);
+    for (size_t j = 0; j < cases[i].array_size + 1; j++) {
+      EXPECT_EQ_DOUBLE(cases[i].want_get[j], buckets_array.buckets[j].maximum);
+    }
+    destroy_buckets_array(buckets_array);
+    distribution_destroy(d);
+  }
+  return 0;
+}
+
 int main() {
   RUN_TEST(distribution_new_linear);
-  RUN_TEST(distribution_new_exponential); 
+  RUN_TEST(distribution_new_exponential);
+  RUN_TEST(distribution_new_custom); 
   END_TEST;
 }