diff options
| author | Ethan Werner <mail@ethanwerner.com> | 2026-09-19 06:09:54 +0000 |
|---|---|---|
| committer | Ethan Werner <mail@ethanwerner.com> | 2026-09-19 06:09:54 +0000 |
| commit | bf19b9938508f70011395f4dc283ca758ec6a8d3 (patch) | |
| tree | 46dc4c442ed8d8d43040433c6a4794e76f8e618a /tests/test_interpolate.c | |
| parent | 96ecf29e8be977f51f351308ef2c7c492bb829aa (diff) | |
Diffstat (limited to 'tests/test_interpolate.c')
| -rw-r--r-- | tests/test_interpolate.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/test_interpolate.c b/tests/test_interpolate.c new file mode 100644 index 0000000..5e7166b --- /dev/null +++ b/tests/test_interpolate.c @@ -0,0 +1,112 @@ +// Tests for interpolate.h - interpolation and easing macros +#include <stdio.h> +#include <math.h> + +#include "../include/interpolate.h" + +#define EPS 1e-6 + +#define ASSERT(cond, msg) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, "FAIL: %s (test_interpolate.c:%d)\n", msg, __LINE__); \ + failures++; \ + } \ + } while (0) + +#define ASSERT_FLOAT_EQ(a, b, msg) \ + do { \ + if (fabs((a) - (b)) > EPS) { \ + fprintf(stderr, "FAIL: %s (got %f, expected %f) (test_interpolate.c:%d)\n", \ + msg, (double)(a), (double)(b), __LINE__); \ + failures++; \ + } \ + } while (0) + +static int failures = 0; + +void test_flip() { + ASSERT_FLOAT_EQ(flip(0.0), 1.0, "flip(0)"); + ASSERT_FLOAT_EQ(flip(1.0), 0.0, "flip(1)"); + ASSERT_FLOAT_EQ(flip(0.5), 0.5, "flip(0.5)"); +} + +void test_smooth_start() { + ASSERT_FLOAT_EQ(smooth_start_2(0.0), 0.0, "smooth_start_2(0)"); + ASSERT_FLOAT_EQ(smooth_start_2(1.0), 1.0, "smooth_start_2(1)"); + ASSERT_FLOAT_EQ(smooth_start_2(0.5), 0.25, "smooth_start_2(0.5)"); + + ASSERT_FLOAT_EQ(smooth_start_3(0.0), 0.0, "smooth_start_3(0)"); + ASSERT_FLOAT_EQ(smooth_start_3(1.0), 1.0, "smooth_start_3(1)"); +} + +void test_smooth_stop() { + ASSERT_FLOAT_EQ(smooth_stop_2(0.0), 0.0, "smooth_stop_2(0)"); + ASSERT_FLOAT_EQ(smooth_stop_2(1.0), 1.0, "smooth_stop_2(1)"); +} + +void test_mix() { + ASSERT_FLOAT_EQ(mix(0.0, 10.0, 0.0), 0.0, "mix t=0"); + ASSERT_FLOAT_EQ(mix(0.0, 10.0, 1.0), 10.0, "mix t=1"); + ASSERT_FLOAT_EQ(mix(0.0, 10.0, 0.5), 5.0, "mix t=0.5"); +} + +void test_crossfade() { + ASSERT_FLOAT_EQ(crossfade(0.0, 10.0, 0.0), 0.0, "crossfade t=0"); + ASSERT_FLOAT_EQ(crossfade(0.0, 10.0, 1.0), 10.0, "crossfade t=1"); + ASSERT_FLOAT_EQ(crossfade(5.0, 10.0, 0.5), 7.5, "crossfade mid"); +} + +void test_scale() { + ASSERT_FLOAT_EQ(scale(10.0, 0.5), 5.0, "scale 10*0.5"); + ASSERT_FLOAT_EQ(scale(10.0, 1.0), 10.0, "scale 10*1"); + ASSERT_FLOAT_EQ(scale(10.0, 0.0), 0.0, "scale 10*0"); +} + +void test_reverse_scale() { + ASSERT_FLOAT_EQ(reverse_scale(10.0, 0.0), 10.0, "reverse_scale t=0"); + ASSERT_FLOAT_EQ(reverse_scale(10.0, 1.0), 0.0, "reverse_scale t=1"); + ASSERT_FLOAT_EQ(reverse_scale(10.0, 0.5), 5.0, "reverse_scale t=0.5"); +} + +void test_arch() { + ASSERT_FLOAT_EQ(arch_2(0.0), 0.0, "arch_2(0)"); + ASSERT_FLOAT_EQ(arch_2(1.0), 0.0, "arch_2(1)"); + ASSERT_FLOAT_EQ(arch_2(0.5), 0.25, "arch_2(0.5)"); +} + +void test_smooth_step() { + ASSERT_FLOAT_EQ(smooth_step_3(0.0), 0.0, "smooth_step_3(0)"); + ASSERT_FLOAT_EQ(smooth_step_3(1.0), 1.0, "smooth_step_3(1)"); +} + +void test_namespaced_macros() { + ASSERT_FLOAT_EQ(INTERP_FLIP(0.3), 0.7, "INTERP_FLIP"); + ASSERT_FLOAT_EQ(INTERP_SCALE(10.0, 0.4), 4.0, "INTERP_SCALE"); + ASSERT_FLOAT_EQ(INTERP_REVERSE_SCALE(10.0, 0.3), 7.0, "INTERP_REVERSE_SCALE"); + ASSERT_FLOAT_EQ(INTERP_MIX(0.0, 20.0, 0.5), 10.0, "INTERP_MIX"); + ASSERT_FLOAT_EQ(INTERP_CROSSFADE(0.0, 20.0, 0.25), 5.0, "INTERP_CROSSFADE"); + ASSERT_FLOAT_EQ(INTERP_SMOOTH_START_2(0.5), 0.25, "INTERP_SMOOTH_START_2"); + ASSERT_FLOAT_EQ(INTERP_SMOOTH_STOP_2(0.5), 0.75, "INTERP_SMOOTH_STOP_2"); + ASSERT_FLOAT_EQ(INTERP_SMOOTH_STEP_3(0.0), 0.0, "INTERP_SMOOTH_STEP_3(0)"); + ASSERT_FLOAT_EQ(INTERP_SMOOTH_STEP_3(1.0), 1.0, "INTERP_SMOOTH_STEP_3(1)"); + ASSERT_FLOAT_EQ(INTERP_ARCH_2(0.5), 0.25, "INTERP_ARCH_2"); + ASSERT_FLOAT_EQ(INTERP_SMOOTH_START_ARCH_3(0.5), 0.125, "INTERP_SMOOTH_START_ARCH_3"); + ASSERT_FLOAT_EQ(INTERP_SMOOTH_STOP_ARCH_3(0.5), 0.125, "INTERP_SMOOTH_STOP_ARCH_3"); +} + +int main() { + test_flip(); + test_smooth_start(); + test_smooth_stop(); + test_mix(); + test_crossfade(); + test_scale(); + test_reverse_scale(); + test_arch(); + test_smooth_step(); + test_namespaced_macros(); + + printf("test_interpolate: %d failures\n", failures); + return failures; +} |