doc: update copyright line for 2019
[adg.git] / src / cpml / tests / test-arc.c
blob78b26ff7af33612309b17574454b03793b56b03f
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>
23 #include <math.h>
26 static cairo_path_data_t arc_data[] = {
27 { .header = { CPML_MOVE, 2 }},
28 { .point = { 0, 3 }},
30 { .header = { CPML_ARC, 3 }},
31 { .point = { 3, 0 }},
32 { .point = { 0, -3 }}
35 CpmlPrimitive arc = {
36 NULL,
37 &arc_data[1],
38 &arc_data[2]
42 static void
43 _cpml_sanity_info(gint i)
45 switch (i) {
46 case 1:
47 cpml_arc_info(NULL, NULL, NULL, NULL, NULL);
48 break;
49 default:
50 g_test_trap_assert_failed();
51 break;
55 static void
56 _cpml_sanity_to_cairo(gint i)
58 cairo_t *cr;
60 cr = adg_test_cairo_context();
62 switch (i) {
63 case 1:
64 cpml_arc_to_cairo(NULL, cr);
65 break;
66 case 2:
67 cpml_arc_to_cairo(&arc, NULL);
68 break;
69 default:
70 g_test_trap_assert_failed();
71 break;
74 cairo_destroy(cr);
77 static void
78 _cpml_sanity_to_curves(int i)
80 CpmlSegment segment;
82 switch (i) {
83 case 1:
84 cpml_arc_to_curves(NULL, &segment, 1);
85 break;
86 case 2:
87 cpml_arc_to_curves(&arc, NULL, 1);
88 break;
89 case 3:
90 /* XXX: this generates a division by 0 but on Travis, for some reason,
91 * that is not enough to crash the process:
92 * https://travis-ci.org/ntd/adg/builds/189592982
94 * Disabling for now.
96 cpml_arc_to_curves(&arc, &segment, 0);
97 break;
98 default:
99 g_test_trap_assert_failed();
100 break;
104 static void
105 _cpml_method_info(void)
107 CpmlPair center;
108 double r, start, end;
110 /* Passing NULL to the output arguments is valid */
111 g_assert_true(cpml_arc_info(&arc, NULL, NULL, NULL, NULL));
113 g_assert_true(cpml_arc_info(&arc, &center, &r, &start, &end));
115 adg_assert_isapprox(center.x, 0);
116 adg_assert_isapprox(center.y, 0);
117 adg_assert_isapprox(r, 3);
118 adg_assert_isapprox(start, M_PI_2);
119 adg_assert_isapprox(end, -M_PI_2);
122 static void
123 _cpml_method_to_curves(void)
125 cairo_path_data_t data[4*2];
126 CpmlSegment segment = { NULL, data, 0 };
128 /* Approximate with a single curve */
129 cpml_arc_to_curves(&arc, &segment, 1);
131 g_assert_cmpint(data[0].header.type, ==, CPML_CURVE);
132 adg_assert_isapprox(data[1].point.x, 4);
133 adg_assert_isapprox(data[1].point.y, 3);
134 adg_assert_isapprox(data[2].point.x, 4);
135 adg_assert_isapprox(data[2].point.y, -3);
136 adg_assert_isapprox(data[3].point.x, 0);
137 adg_assert_isapprox(data[3].point.y, -3);
139 /* Approximate with two curves */
140 cpml_arc_to_curves(&arc, &segment, 2);
142 g_assert_cmpint(data[0].header.type, ==, CPML_CURVE);
143 adg_assert_isapprox(data[1].point.x, 1.65685425);
144 adg_assert_isapprox(data[1].point.y, 3);
145 adg_assert_isapprox(data[2].point.x, 3);
146 adg_assert_isapprox(data[2].point.y, 1.65685425);
147 adg_assert_isapprox(data[3].point.x, 3);
148 adg_assert_isapprox(data[3].point.y, 0);
150 g_assert_cmpint(data[4].header.type, ==, CPML_CURVE);
151 adg_assert_isapprox(data[5].point.x, 3);
152 adg_assert_isapprox(data[5].point.y, -1.65685425);
153 adg_assert_isapprox(data[6].point.x, 1.65685425);
154 adg_assert_isapprox(data[6].point.y, -3);
155 adg_assert_isapprox(data[7].point.x, 0);
156 adg_assert_isapprox(data[7].point.y, -3);
162 main(int argc, char *argv[])
164 adg_test_init(&argc, &argv);
166 adg_test_add_traps("/cpml/arc/sanity/info", _cpml_sanity_info, 1);
167 adg_test_add_traps("/cpml/arc/sanity/to-cairo", _cpml_sanity_to_cairo, 2);
168 adg_test_add_traps("/cpml/arc/sanity/to-curves", _cpml_sanity_to_curves, 2);
170 g_test_add_func("/cpml/arc/method/info", _cpml_method_info);
171 g_test_add_func("/cpml/arc/method/to-curves", _cpml_method_to_curves);
173 return g_test_run();