tests: add basic CpmlCurve checking
[adg.git] / src / cpml / tests / test-curve.c
blob72642692d93a12036d42567053d2333a2c9b3e0d
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2015 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_test_sanity_pair_at_time(gint i)
45 CpmlPair pair;
47 /* Check that invalid arguments crashes the process */
48 switch (i) {
49 case 1:
50 cpml_curve_put_pair_at_time(NULL, 0, &pair);
51 break;
52 case 2:
53 cpml_curve_put_pair_at_time(&curve, 0, NULL);
54 break;
55 default:
56 g_test_trap_assert_failed();
57 break;
61 static void
62 _cpml_test_sanity_vector_at_time(gint i)
64 CpmlVector vector;
66 /* Check that invalid arguments crashes the process */
67 switch (i) {
68 case 1:
69 cpml_curve_put_vector_at_time(NULL, 0, &vector);
70 break;
71 case 2:
72 cpml_curve_put_vector_at_time(&curve, 0, NULL);
73 break;
74 default:
75 g_test_trap_assert_failed();
76 break;
80 static void
81 _cpml_test_sanity_offset_at_time(gint i)
83 CpmlPair pair;
85 /* Check that invalid arguments crashes the process */
86 switch (i) {
87 case 1:
88 cpml_curve_put_offset_at_time(NULL, 0, 0, &pair);
89 break;
90 case 2:
91 cpml_curve_put_offset_at_time(&curve, 0, 0, NULL);
92 break;
93 default:
94 g_test_trap_assert_failed();
95 break;
99 static void
100 _cpml_test_offset_algorithm(void)
102 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL), ==, CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT);
103 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_NONE), ==, CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL);
104 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_BAIOCA), ==, CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL);
105 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_DEFAULT), ==, CPML_CURVE_OFFSET_ALGORITHM_BAIOCA);
106 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL), ==, CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT);
107 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT), ==, CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL);
108 g_assert_cmpint(cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_NONE), ==, CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT);
111 static void
112 _cpml_test_pair_at_time(void)
114 CpmlPair pair;
116 cpml_curve_put_pair_at_time(&curve, 0, &pair);
117 g_assert_cmpfloat(pair.x, ==, 1);
118 g_assert_cmpfloat(pair.y, ==, 1);
120 cpml_curve_put_pair_at_time(&curve, 0.5, &pair);
121 g_assert_cmpfloat(pair.x, ==, 2);
122 g_assert_cmpfloat(pair.y, ==, 3);
124 cpml_curve_put_pair_at_time(&curve, 1, &pair);
125 g_assert_cmpfloat(pair.x, ==, 3);
126 g_assert_cmpfloat(pair.y, ==, 5);
128 /* t is not bound to domain 0..1 */
129 cpml_curve_put_pair_at_time(&curve, -1, &pair);
130 g_assert_cmpfloat(pair.x, ==, 11);
131 g_assert_cmpfloat(pair.y, ==, -15);
133 cpml_curve_put_pair_at_time(&curve, 2, &pair);
134 g_assert_cmpfloat(pair.x, ==, -7);
135 g_assert_cmpfloat(pair.y, ==, 21);
138 static void
139 _cpml_test_vector_at_time(void)
141 CpmlVector vector;
143 cpml_curve_put_vector_at_time(&curve, 0, &vector);
144 g_assert_cmpfloat(vector.x, ==, 0);
145 g_assert_cmpfloat(vector.y, ==, 6);
147 cpml_curve_put_vector_at_time(&curve, 0.5, &vector);
148 g_assert_cmpfloat(vector.x, ==, 3);
149 g_assert_cmpfloat(vector.y, ==, 3);
151 cpml_curve_put_vector_at_time(&curve, 1, &vector);
152 g_assert_cmpfloat(vector.x, ==, 0);
153 g_assert_cmpfloat(vector.y, ==, 6);
155 /* t is not bound to domain 0..1 */
156 cpml_curve_put_vector_at_time(&curve, -1, &vector);
157 g_assert_cmpfloat(vector.x, ==, -24);
158 g_assert_cmpfloat(vector.y, ==, 30);
160 cpml_curve_put_vector_at_time(&curve, 2, &vector);
161 g_assert_cmpfloat(vector.x, ==, -24);
162 g_assert_cmpfloat(vector.y, ==, 30);
165 static void
166 _cpml_test_offset_at_time(void)
168 CpmlPair pair;
170 /* TODO: test some other points other than 0 and 1, also
171 * outside the 0..1 domainother offset algorithms */
173 cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_HANDCRAFT);
175 cpml_curve_put_offset_at_time(&curve, 0, 1, &pair);
176 g_assert_cmpfloat(pair.x, ==, 0);
177 g_assert_cmpfloat(pair.y, ==, 1);
179 cpml_curve_put_offset_at_time(&curve, 1, 1, &pair);
180 g_assert_cmpfloat(pair.x, ==, 2);
181 g_assert_cmpfloat(pair.y, ==, 5);
183 cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_GEOMETRICAL);
185 cpml_curve_put_offset_at_time(&curve, 0, 1, &pair);
186 g_assert_cmpfloat(pair.x, ==, 0);
187 g_assert_cmpfloat(pair.y, ==, 1);
189 cpml_curve_put_offset_at_time(&curve, 1, 1, &pair);
190 g_assert_cmpfloat(pair.x, ==, 2);
191 g_assert_cmpfloat(pair.y, ==, 5);
193 cpml_curve_offset_algorithm(CPML_CURVE_OFFSET_ALGORITHM_BAIOCA);
195 cpml_curve_put_offset_at_time(&curve, 0, 1, &pair);
196 g_assert_cmpfloat(pair.x, ==, 0);
197 g_assert_cmpfloat(pair.y, ==, 1);
199 cpml_curve_put_offset_at_time(&curve, 1, 1, &pair);
200 g_assert_cmpfloat(pair.x, ==, 2);
201 g_assert_cmpfloat(pair.y, ==, 5);
206 main(int argc, char *argv[])
208 adg_test_init(&argc, &argv);
210 adg_test_add_traps("/cpml/curve/sanity/pair-at-time", _cpml_test_sanity_pair_at_time, 2);
211 adg_test_add_traps("/cpml/curve/sanity/vector-at-time", _cpml_test_sanity_vector_at_time, 2);
212 adg_test_add_traps("/cpml/curve/sanity/offset-at-time", _cpml_test_sanity_offset_at_time, 2);
214 g_test_add_func("/cpml/curve/method/offset-algorithm", _cpml_test_offset_algorithm);
215 g_test_add_func("/cpml/curve/method/pair-at-time", _cpml_test_pair_at_time);
216 g_test_add_func("/cpml/curve/method/vector-at-time", _cpml_test_vector_at_time);
217 g_test_add_func("/cpml/curve/method/offset-at-time", _cpml_test_offset_at_time);
219 return g_test_run();