doc: update copyright line for 2019
[adg.git] / src / cpml / tests / test-curve.c
blobcef60fe5c569750eecae4f89184dfb93cd9ec3b6
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2019 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include <adg-test.h>
22 #include <cpml.h>
25 static cairo_path_data_t curve_data[] = {
26 { .header = { CPML_MOVE, 2 }},
27 { .point = { 1, 1 }},
29 { .header = { CPML_CURVE, 4 }},
30 { .point = { 1, 3 }},
31 { .point = { 3, 3 }},
32 { .point = { 3, 5 }}
35 CpmlPrimitive curve = {
36 NULL,
37 &curve_data[1],
38 &curve_data[2]
42 static void
43 _cpml_sanity_pair_at_time(gint i)
45 CpmlPair pair;
47 switch (i) {
48 case 1:
49 cpml_curve_put_pair_at_time(NULL, 0, &pair);
50 break;
51 case 2:
52 cpml_curve_put_pair_at_time(&curve, 0, NULL);
53 break;
54 default:
55 g_test_trap_assert_failed();
56 break;
60 static void
61 _cpml_sanity_vector_at_time(gint i)
63 CpmlVector vector;
65 switch (i) {
66 case 1:
67 cpml_curve_put_vector_at_time(NULL, 0, &vector);
68 break;
69 case 2:
70 cpml_curve_put_vector_at_time(&curve, 0, NULL);
71 break;
72 default:
73 g_test_trap_assert_failed();
74 break;
78 static void
79 _cpml_sanity_offset_at_time(gint i)
81 CpmlPair pair;
83 switch (i) {
84 case 1:
85 cpml_curve_put_offset_at_time(NULL, 0, 0, &pair);
86 break;
87 case 2:
88 cpml_curve_put_offset_at_time(&curve, 0, 0, NULL);
89 break;
90 default:
91 g_test_trap_assert_failed();
92 break;
96 static void
97 _cpml_method_offset_algorithm(void)
99 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL), ==, CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT);
100 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_NONE), ==, CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL);
101 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_BAIOCA), ==, CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL);
102 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_DEFAULT), ==, CPML_CURVE_OFFSET_ALGORITHM_BAIOCA);
103 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL), ==, CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT);
104 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT), ==, CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL);
105 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_NONE), ==, CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT);
108 static void
109 _cpml_method_pair_at_time(void)
111 CpmlPair pair;
113 cpml_curve_put_pair_at_time(&curve, 0, &pair);
114 adg_assert_isapprox(pair.x, 1);
115 adg_assert_isapprox(pair.y, 1);
117 cpml_curve_put_pair_at_time(&curve, 0.5, &pair);
118 adg_assert_isapprox(pair.x, 2);
119 adg_assert_isapprox(pair.y, 3);
121 cpml_curve_put_pair_at_time(&curve, 1, &pair);
122 adg_assert_isapprox(pair.x, 3);
123 adg_assert_isapprox(pair.y, 5);
125 /* t is not bound to domain 0..1 */
126 cpml_curve_put_pair_at_time(&curve, -1, &pair);
127 adg_assert_isapprox(pair.x, 11);
128 adg_assert_isapprox(pair.y, -15);
130 cpml_curve_put_pair_at_time(&curve, 2, &pair);
131 adg_assert_isapprox(pair.x, -7);
132 adg_assert_isapprox(pair.y, 21);
135 static void
136 _cpml_method_vector_at_time(void)
138 CpmlVector vector;
140 cpml_curve_put_vector_at_time(&curve, 0, &vector);
141 adg_assert_isapprox(vector.x, 0);
142 adg_assert_isapprox(vector.y, 6);
144 cpml_curve_put_vector_at_time(&curve, 0.5, &vector);
145 adg_assert_isapprox(vector.x, 3);
146 adg_assert_isapprox(vector.y, 3);
148 cpml_curve_put_vector_at_time(&curve, 1, &vector);
149 adg_assert_isapprox(vector.x, 0);
150 adg_assert_isapprox(vector.y, 6);
152 /* t is not bound to domain 0..1 */
153 cpml_curve_put_vector_at_time(&curve, -1, &vector);
154 adg_assert_isapprox(vector.x, -24);
155 adg_assert_isapprox(vector.y, 30);
157 cpml_curve_put_vector_at_time(&curve, 2, &vector);
158 adg_assert_isapprox(vector.x, -24);
159 adg_assert_isapprox(vector.y, 30);
162 static void
163 _cpml_method_offset_at_time(void)
165 CpmlPair pair;
167 cpml_curve_put_offset_at_time(&curve, 0, 1, &pair);
168 adg_assert_isapprox(pair.x, 0);
169 adg_assert_isapprox(pair.y, 1);
171 cpml_curve_put_offset_at_time(&curve, 1, 1, &pair);
172 adg_assert_isapprox(pair.x, 2);
173 adg_assert_isapprox(pair.y, 5);
175 cpml_curve_put_offset_at_time(&curve, 0, 2, &pair);
176 adg_assert_isapprox(pair.x, -1);
177 adg_assert_isapprox(pair.y, 1);
179 cpml_curve_put_offset_at_time(&curve, 0.5, 0, &pair);
180 adg_assert_isapprox(pair.x, 2);
181 adg_assert_isapprox(pair.y, 3);
183 cpml_curve_put_offset_at_time(&curve, 0.5, G_SQRT2, &pair);
184 /* Check only the first 5 digits to avoid rounding errors */
185 g_assert_cmpint((pair.x + 0.00005) * 10000, ==, 10000);
186 g_assert_cmpint((pair.y + 0.00005) * 10000, ==, 40000);
191 main(int argc, char *argv[])
193 adg_test_init(&argc, &argv);
195 adg_test_add_traps("/cpml/curve/sanity/pair-at-time", _cpml_sanity_pair_at_time, 2);
196 adg_test_add_traps("/cpml/curve/sanity/vector-at-time", _cpml_sanity_vector_at_time, 2);
197 adg_test_add_traps("/cpml/curve/sanity/offset-at-time", _cpml_sanity_offset_at_time, 2);
199 g_test_add_func("/cpml/curve/method/offset-algorithm", _cpml_method_offset_algorithm);
200 g_test_add_func("/cpml/curve/method/pair-at-time", _cpml_method_pair_at_time);
201 g_test_add_func("/cpml/curve/method/vector-at-time", _cpml_method_vector_at_time);
202 g_test_add_func("/cpml/curve/method/offset-at-time", _cpml_method_offset_at_time);
204 return g_test_run();