doc: update copyright line for 2021
[adg.git] / src / adg / tests / test-point.c
blob6379a5fb5ba79f1d0267850e152971496395132d
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 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 <adg.h>
25 static void
26 _adg_behavior_misc(void)
28 AdgPoint *point, *dup_point;
29 CpmlPair *pair, *dup_pair;
30 CpmlPair dummy_pair = { 3.4, 5.6 };
32 point = adg_point_new();
33 g_assert_nonnull(point);
35 adg_point_set_pair_explicit(point, 1, 2);
36 pair = (CpmlPair *) point;
37 adg_assert_isapprox(pair->x, 1);
38 adg_assert_isapprox(pair->y, 2);
40 pair = adg_point_get_pair(point);
41 g_assert_nonnull(pair);
42 adg_assert_isapprox(pair->x, 1);
43 adg_assert_isapprox(pair->y, 2);
45 dup_point = adg_point_dup(point);
46 g_assert_nonnull(dup_point);
47 g_assert_true(dup_point != point);
49 /* Should be a noop with explicit pairs */
50 adg_point_invalidate(point);
52 dup_pair = adg_point_get_pair(dup_point);
53 g_assert_nonnull(dup_pair);
54 g_assert_true(dup_pair != pair);
56 adg_assert_isapprox(pair->x, dup_pair->x);
57 adg_assert_isapprox(pair->y, dup_pair->y);
58 g_assert_true(adg_point_equal(point, dup_point));
60 g_free(dup_pair);
61 adg_point_destroy(dup_point);
63 dup_point = adg_point_new();
64 adg_point_set_pair(dup_point, &dummy_pair);
65 dup_pair = (CpmlPair *) dup_point;
67 /* Should be a noop with explicit pairs */
68 adg_point_invalidate(dup_point);
70 adg_assert_isapprox(dup_pair->x, 3.4);
71 adg_assert_isapprox(dup_pair->y, 5.6);
72 g_assert_false(adg_point_equal(point, dup_point));
74 adg_point_copy(dup_point, point);
75 dup_pair = adg_point_get_pair(dup_point);
76 g_assert_nonnull(dup_pair);
77 g_assert_nonnull(dup_pair);
79 adg_assert_isapprox(pair->x, dup_pair->x);
80 adg_assert_isapprox(pair->y, dup_pair->y);
81 g_assert_true(adg_point_equal(point, dup_point));
83 g_free(pair);
84 g_free(dup_pair);
85 adg_point_destroy(point);
86 adg_point_destroy(dup_point);
89 static void
90 _adg_behavior_named_pair(void)
92 CpmlPair p1 = { 123, 456 };
93 AdgPoint *explicit_point, *explicit_point2, *model_point;
94 AdgModel *model;
95 CpmlPair *pair;
97 explicit_point = adg_point_new();
98 g_assert_nonnull(explicit_point);
99 adg_point_set_pair(explicit_point, &p1);
101 explicit_point2 = adg_point_new();
102 g_assert_nonnull(explicit_point2);
103 adg_point_set_pair_explicit(explicit_point2, p1.x, p1.y);
105 /* Checking comparison APIs */
106 g_assert_true(adg_point_equal(explicit_point, explicit_point2));
107 adg_point_set_pair_explicit(explicit_point2, 78, 90);
108 g_assert_false(adg_point_equal(explicit_point, explicit_point2));
109 pair = (CpmlPair *) explicit_point2;
110 adg_assert_isapprox(pair->x, 78);
111 adg_assert_isapprox(pair->y, 90);
113 pair = adg_point_get_pair(explicit_point);
114 g_assert_true(cpml_pair_equal(pair, &p1));
115 g_free(pair);
117 model = ADG_MODEL(adg_path_new());
118 g_assert_nonnull(model);
119 adg_model_set_named_pair(model, "named-pair", &p1);
121 model_point = adg_point_new();
122 g_assert_nonnull(model_point);
123 adg_point_set_pair_from_model(model_point, model, "named-pair");
125 pair = adg_point_get_pair(model_point);
126 g_assert_true(cpml_pair_equal(pair, &p1));
127 g_free(pair);
129 /* Ensure ADG does not consider an explicit point equals to
130 * a point bound to a named pair with the same coordinates */
131 g_assert_false(adg_point_equal(explicit_point, model_point));
133 /* Check for lazy evaluation of named pairs */
134 adg_point_set_pair_from_model(model_point, model, "unexistent-pair");
135 pair = (CpmlPair *) model_point;
136 adg_assert_isapprox(pair->x, p1.x);
137 adg_assert_isapprox(pair->y, p1.y);
139 /* Check behavior on undefined named pair */
140 g_assert_false(adg_point_update(model_point));
141 g_assert_null(adg_point_get_pair(model_point));
143 adg_point_set_pair_from_model(model_point, model, "named-pair");
144 g_assert_true(adg_point_update(model_point));
146 /* Check for case sensitiveness */
147 adg_point_set_pair_from_model(model_point, model, "Named-Pair");
148 g_assert_null(adg_point_get_pair(model_point));
149 g_assert_false(adg_point_update(model_point));
151 /* Check if adg_point_get_pair() triggers an adg_point_update() */
152 adg_point_set_pair_from_model(model_point, model, "named-pair");
153 pair = adg_point_get_pair(model_point);
154 g_assert_true(cpml_pair_equal(pair, &p1));
155 g_free(pair);
157 adg_point_destroy(explicit_point);
158 adg_point_destroy(model_point);
159 g_object_unref(model);
164 main(int argc, char *argv[])
166 adg_test_init(&argc, &argv);
168 adg_test_add_boxed_checks("/adg/point/type/boxed", ADG_TYPE_POINT, adg_point_new());
170 g_test_add_func("/adg/point/behavior/misc", _adg_behavior_misc);
171 g_test_add_func("/adg/point/behavior/named-pair", _adg_behavior_named_pair);
173 return g_test_run();