From 2f03141e9fed348ae3053e1d185df3e1f8028e4a Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 6 Jul 2021 23:57:01 +0200 Subject: [PATCH] Add clock tests The clock rolling sum is not trivial. Test it. --- app/meson.build | 4 +++ app/tests/test_clock.c | 79 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 app/tests/test_clock.c diff --git a/app/meson.build b/app/meson.build index d7ab0416..f5345803 100644 --- a/app/meson.build +++ b/app/meson.build @@ -168,6 +168,10 @@ if get_option('buildtype') == 'debug' 'src/cli.c', 'src/util/str_util.c', ]], + ['test_clock', [ + 'tests/test_clock.c', + 'src/clock.c', + ]], ['test_control_msg_serialize', [ 'tests/test_control_msg_serialize.c', 'src/control_msg.c', diff --git a/app/tests/test_clock.c b/app/tests/test_clock.c new file mode 100644 index 00000000..a88d5800 --- /dev/null +++ b/app/tests/test_clock.c @@ -0,0 +1,79 @@ +#include "common.h" + +#include + +#include "clock.h" + +void test_small_rolling_sum(void) { + struct sc_clock clock; + sc_clock_init(&clock); + + assert(clock.count == 0); + assert(clock.left_sum.system == 0); + assert(clock.left_sum.stream == 0); + assert(clock.right_sum.system == 0); + assert(clock.right_sum.stream == 0); + + sc_clock_update(&clock, 2, 3); + assert(clock.count == 1); + assert(clock.left_sum.system == 0); + assert(clock.left_sum.stream == 0); + assert(clock.right_sum.system == 2); + assert(clock.right_sum.stream == 3); + + sc_clock_update(&clock, 10, 20); + assert(clock.count == 2); + assert(clock.left_sum.system == 2); + assert(clock.left_sum.stream == 3); + assert(clock.right_sum.system == 10); + assert(clock.right_sum.stream == 20); + + sc_clock_update(&clock, 40, 80); + assert(clock.count == 3); + assert(clock.left_sum.system == 2); + assert(clock.left_sum.stream == 3); + assert(clock.right_sum.system == 50); + assert(clock.right_sum.stream == 100); + + sc_clock_update(&clock, 400, 800); + assert(clock.count == 4); + assert(clock.left_sum.system == 12); + assert(clock.left_sum.stream == 23); + assert(clock.right_sum.system == 440); + assert(clock.right_sum.stream == 880); +} + +void test_large_rolling_sum(void) { + const unsigned half_range = SC_CLOCK_RANGE / 2; + + struct sc_clock clock1; + sc_clock_init(&clock1); + for (unsigned i = 0; i < 5 * half_range; ++i) { + sc_clock_update(&clock1, i, 2 * i + 1); + } + + struct sc_clock clock2; + sc_clock_init(&clock2); + for (unsigned i = 3 * half_range; i < 5 * half_range; ++i) { + sc_clock_update(&clock2, i, 2 * i + 1); + } + + assert(clock1.count == SC_CLOCK_RANGE); + assert(clock2.count == SC_CLOCK_RANGE); + + // The values before the last SC_CLOCK_RANGE points in clock1 should have + // no impact + assert(clock1.left_sum.system == clock2.left_sum.system); + assert(clock1.left_sum.stream == clock2.left_sum.stream); + assert(clock1.right_sum.system == clock2.right_sum.system); + assert(clock1.right_sum.stream == clock2.right_sum.stream); +} + +int main(int argc, char *argv[]) { + (void) argc; + (void) argv; + + test_small_rolling_sum(); + test_large_rolling_sum(); + return 0; +};