]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
add unit tests for update function
authorSvetlana <lana0771711@gmail.com>
Mon, 3 Aug 2020 13:16:17 +0000 (13:16 +0000)
committerSvetlana <lana0771711@gmail.com>
Mon, 3 Aug 2020 13:16:17 +0000 (13:16 +0000)
src/daemon/distribution_test.c

index f175fbcc5ab42bb77882ae9e0aac68164f21fcfc..aedb81ef8c3f34de6131cd36da6feefebc6c9e8f 100644 (file)
@@ -286,9 +286,67 @@ DEF_TEST(distribution_new_custom) {
   return 0;
 }
 
+DEF_TEST(update) {
+  struct {
+    distribution_t *dist;
+    size_t num_gauges;
+    double *gauges;
+    uint64_t *want_counters;
+    int want_err;
+  } cases[] = {
+    {
+      .dist = distribution_new_linear(6, 5),
+      .num_gauges = 10,
+      .gauges = (double[]){25, 30, 5, 7, 11, 10.5, 8.03, 1112.4, 35, 12.7},
+      .want_counters = (uint64_t[]){0, 3, 3, 0, 0, 4},
+    },
+    {
+      .dist = distribution_new_exponential(4, 1.41, 1),
+      .num_gauges = 0,
+      .gauges = NULL,
+      .want_counters = (uint64_t[]){0, 0, 0, 0},
+    },
+    {
+      .dist = distribution_new_exponential(5, 2, 3),
+      .num_gauges = 5,
+      .gauges = (double[]){1, 7, 3, 10, 77},
+      .want_counters = (uint64_t[]){1, 1, 2, 0, 1},
+    },
+    {
+      .dist = distribution_new_linear(100, 22),
+      .num_gauges = 3,
+      .gauges = (double[]){1000, 2, -8},
+      .want_err = EINVAL,
+    },
+    {
+      .dist = distribution_new_custom(3, (double[]) {5, 20, 35}),
+      .num_gauges = 7,
+      .gauges = (double[]){7.05, 22.37, 40.83, 90.55, 12.34, 14.2, 6.0},
+      .want_counters = (uint64_t[]){0, 4, 1, 2},
+    },
+  };
+  for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
+    for (size_t j = 0; j < cases[i].num_gauges; j++) {
+      distribution_update(cases[i].dist, cases[i].gauges[j]);
+    }
+    if (cases[i].want_err != 0) {
+      EXPECT_EQ_INT(cases[i].want_err, errno);
+      continue;
+    }
+    buckets_array_t buckets_array = get_buckets(cases[i].dist);
+    for (size_t j = 0; j < buckets_array.num_buckets; j++) {
+      EXPECT_EQ_INT(cases[i].want_counters[j], buckets_array.buckets[j].bucket_counter);
+    }
+    destroy_buckets_array(buckets_array);
+    distribution_destroy(cases[i].dist);
+  }
+  return 0;
+}
+
 int main() {
   RUN_TEST(distribution_new_linear);
   RUN_TEST(distribution_new_exponential);
   RUN_TEST(distribution_new_custom); 
+  RUN_TEST(update);
   END_TEST;
 }