v2 / vlib / math / interpolation_test.v
122 lines · 104 sloc · 4.3 KB · fde6c9d903c5720afd226b60a71db8ac2325255c
Raw
1import math
2
3const decay = 100
4
5fn test_mix() {
6 assert math.mix(0.0, 100.0, 0.0) == 0.0
7 assert math.mix(0.0, 100.0, 0.1) == 10.0
8 assert math.mix(0.0, 100.0, 0.2) == 20.0
9 assert math.mix(0.0, 100.0, 0.5) == 50.0
10 assert math.mix(0.0, 100.0, 0.8) == 80.0
11 assert math.mix(0.0, 100.0, 0.9) == 90.0
12 assert math.mix(0.0, 100.0, 1.0) == 100.0
13
14 assert math.mix(100.0, 500.0, 0.0) == 100.0
15 assert math.mix(100.0, 500.0, 0.1) == 140.0
16 assert math.mix(100.0, 500.0, 0.2) == 180.0
17 assert math.mix(100.0, 500.0, 0.5) == 300.0
18 assert math.mix(100.0, 500.0, 0.8) == 420.0
19 assert math.mix(100.0, 500.0, 0.9) == 460.0
20 assert math.mix(100.0, 500.0, 1.0) == 500.0
21}
22
23fn test_exp_decay() {
24 assert math.exp_decay(0.0, 100.0, decay, 0.0) == 0.0
25 assert math.exp_decay(0.0, 100.0, decay, 1.0) == 100.0
26
27 assert math.exp_decay(100.0, 500.0, decay, 0.0) == 100.0
28 assert math.exp_decay(100.0, 500.0, decay, 1.0) == 500.0
29
30 assert math.exp_decay(0, 100, decay, 0.0) == 0
31 assert math.exp_decay(0, 100, decay, 1.0) == 100
32
33 assert math.exp_decay(100, 500, decay, 0.0) == 100
34 assert math.exp_decay(100, 500, decay, 1.0) == 500
35}
36
37fn test_clip() {
38 assert math.clip(0.0, 10.0, 50.0) == 10.0
39 assert math.clip(5.5, 10.0, 50.0) == 10.0
40 assert math.clip(10.0, 10.0, 50.0) == 10.0
41 assert math.clip(20.0, 10.0, 50.0) == 20.0
42 assert math.clip(50.0, 10.0, 50.0) == 50.0
43 assert math.clip(80.0, 10.0, 50.0) == 50.0
44 assert math.clip(90.5, 10.0, 50.0) == 50.0
45
46 assert math.clip(0, 10, 50) == 10
47 assert math.clip(5, 10, 50) == 10
48 assert math.clip(10, 10, 50) == 10
49 assert math.clip(20, 10, 50) == 20
50 assert math.clip(50, 10, 50) == 50
51 assert math.clip(80, 10, 50) == 50
52 assert math.clip(90, 10, 50) == 50
53}
54
55// The test curve control points are taken from: https://cubic-bezier.com/#.19,-0.09,.42,1.19
56const b = [
57 math.BezierPoint{0, 0},
58 math.BezierPoint{0.19, -0.09},
59 math.BezierPoint{0.42, 1.19},
60 math.BezierPoint{1.0, 1.0},
61]
62const bx = b.map(it.x)
63const by = b.map(it.y)
64const bx_fa = [4]f64{init: b[index].x}
65const by_fa = [4]f64{init: b[index].y}
66
67fn test_cubic_bezier() {
68 assert math.cubic_bezier(0.0, b) == math.BezierPoint{b[0].x, b[0].x}
69 assert math.cubic_bezier(0.5, b) == math.BezierPoint{0.35375, 0.5375}
70 assert math.cubic_bezier(1.0, b) == math.BezierPoint{b[3].x, b[3].x}
71}
72
73fn test_cubic_bezier_a() {
74 assert math.cubic_bezier_a(0.0, bx, by) == math.BezierPoint{bx[0], by[0]}
75 assert math.cubic_bezier_a(0.5, bx, by) == math.BezierPoint{0.35375, 0.5375}
76 assert math.cubic_bezier_a(1.0, bx, by) == math.BezierPoint{bx[3], by[3]}
77}
78
79fn test_cubic_bezier_fa() {
80 assert math.cubic_bezier_fa(0.0, bx_fa, by_fa) == math.BezierPoint{bx_fa[0], by_fa[0]}
81 assert math.cubic_bezier_fa(0.5, bx_fa, by_fa) == math.BezierPoint{0.35375, 0.5375}
82 assert math.cubic_bezier_fa(1.0, bx_fa, by_fa) == math.BezierPoint{bx_fa[3], by_fa[3]}
83}
84
85fn test_remap() {
86 assert math.remap(20, 1, 100, 50, 5000) == 1000
87 assert math.remap(20.0, 1, 100, 50, 5000) == 1000.0
88
89 assert math.remap(55, 1, 100, 52, 5000) == 2750
90 assert math.remap(55.0, 1, 100, 52, 5000) == 2750.909090909091
91
92 assert math.remap(25, 1, 100, 50, 5000) == 1250
93 assert math.remap(25, 1, 100, -50, -5000) == -1250
94 assert math.remap(25, 1, 100, 5000, 50) == 3800
95 assert math.remap(25, 1, 100, -5000, -50) == -3800
96 assert math.remap(25, 100, 1, 50, 5000) == 3800
97 assert math.remap(25, 100, 1, -50, -5000) == -3800
98 assert math.remap(25, 100, 1, 5000, 50) == 1250
99 assert math.remap(25, 100, 1, -5000, -50) == -1250
100}
101
102fn test_smoothstep() {
103 assert math.smoothstep(0.0, 1, 0) == 0
104 assert math.close(math.smoothstep(0.0, 1, 0.05), 0.00725)
105 assert math.close(math.smoothstep(0.0, 1, 0.1), 0.028)
106 assert math.smoothstep(0.0, 1, 0.5) == 0.5
107 assert math.close(math.smoothstep(0.0, 1, 0.9), 0.972)
108 assert math.close(math.smoothstep(0.0, 1, 0.95), 0.99275)
109 assert math.smoothstep(0.0, 1, 1) == 1
110}
111
112fn test_smootherstep() {
113 assert math.smootherstep(0.0, 1, 0) == 0
114 assert math.close(math.smootherstep(0.0, 1, 0.05), 0.001158125)
115 assert math.close(math.smootherstep(0.0, 1, 0.1), 0.00856)
116 assert math.smootherstep(0.0, 1, 0.5) == 0.5
117 assert math.close(math.smootherstep(0.0, 1, 0.9), 0.99144)
118 assert math.close(math.smootherstep(0.0, 1, 0.95), 0.998841875)
119 assert math.smootherstep(0.0, 1, 1) == 1
120 assert math.close(math.smootherstep(0.0, 1, 0.12345), 0.01550187121622219)
121 assert math.close(math.smootherstep(0.0, 1, 12345.12345), 1.0)
122}
123