summaryrefslogtreecommitdiffstats
path: root/tests/test_interpolate.c
blob: 5e7166bb05439195cf247a137d75d39cf5450338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;
}