END_TEST START_TEST(test_counter_init_add) { counter c; int res = init_counter(&c); fail_unless(res == 0); fail_unless(counter_add_sample(&c, 100) == 0); fail_unless(counter_count(&c) == 1); fail_unless(counter_sum(&c) == 100); fail_unless(counter_mean(&c) == 100); fail_unless(counter_stddev(&c) == 0); }
/** * Increments the counter with the given name * by a value. * @arg name The name of the counter * @arg val The value to add * @return 0 on success */ static int metrics_increment_counter(metrics *m, char *name, double val) { counter *c; int res = hashmap_get(m->counters, name, (void**)&c); // New counter if (res == -1) { c = malloc(sizeof(counter)); init_counter(c); hashmap_put(m->counters, name, c); } // Add the sample value return counter_add_sample(c, val); }
END_TEST START_TEST(test_counter_add_loop) { counter c; int res = init_counter(&c); fail_unless(res == 0); for (int i=1; i<=100; i++) fail_unless(counter_add_sample(&c, i) == 0); fail_unless(counter_count(&c) == 100); fail_unless(counter_sum(&c) == 5050); fail_unless(counter_mean(&c) == 50.5); fail_unless(round(counter_stddev(&c)*1000)/1000 == 29.011); }
END_TEST START_TEST(test_counter_sample_rate) { counter c; int res = init_counter(&c); fail_unless(res == 0); for (int i=1; i<=100; i++) fail_unless(counter_add_sample(&c, i, 0.5) == 0); fail_unless(counter_count(&c) == 200); fail_unless(counter_sum(&c) == 5050); fail_unless(counter_mean(&c) == 50.5); fail_unless(round(counter_stddev(&c)*1000)/1000 == 29.011); fail_unless(counter_squared_sum(&c) == 338350); fail_unless(counter_min(&c) == 1); fail_unless(counter_max(&c) == 100); }