netlist: Fix component refdes mangling
[geda-gaf.git] / tests / storage / normalize.c
blob0b66fc6de7e15f7f19a66a7fdd021970fc819b3c
1 /* Copyright (C) 2013-2020 Roland Lutz
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 #include <xornstorage.h>
18 #include <alloca.h>
19 #include <assert.h>
20 #include <string.h>
23 static void assert_line(xorn_revision_t rev, xorn_object_t ob,
24 double width, int cap_style, int dash_style,
25 double dash_length, double dash_space)
27 xorn_selection_t sel;
28 xorn_attst_t state = xorn_attst_na;
29 struct xornsch_line_attr expected_line, real_line;
31 memset(&expected_line, 0, sizeof expected_line);
32 expected_line.width = width;
33 expected_line.cap_style = cap_style;
34 expected_line.dash_style = dash_style;
35 expected_line.dash_length = dash_length;
36 expected_line.dash_space = dash_space;
38 sel = xorn_select_object(ob);
39 assert(sel != NULL);
40 xornsch_get_line(rev, sel, &state, &real_line);
41 assert(state == xorn_attst_consistent);
42 assert(memcmp(&expected_line, &real_line, sizeof expected_line) == 0);
43 xorn_free_selection(sel);
46 static void assert_fill(xorn_revision_t rev, xorn_object_t ob,
47 int type, double width, int angle0, double pitch0,
48 int angle1, double pitch1)
50 xorn_selection_t sel;
51 xorn_attst_t state = xorn_attst_na;
52 struct xornsch_fill_attr expected_fill, real_fill;
54 memset(&expected_fill, 0, sizeof expected_fill);
55 expected_fill.type = type;
56 expected_fill.width = width;
57 expected_fill.angle0 = angle0;
58 expected_fill.pitch0 = pitch0;
59 expected_fill.angle1 = angle1;
60 expected_fill.pitch1 = pitch1;
62 sel = xorn_select_object(ob);
63 assert(sel != NULL);
64 xornsch_get_fill(rev, sel, &state, &real_fill);
65 assert(state == xorn_attst_consistent);
66 assert(memcmp(&expected_fill, &real_fill, sizeof expected_fill) == 0);
67 xorn_free_selection(sel);
70 static void check_line_attr(xorn_revision_t rev, xorn_object_t ob,
71 xorn_obtype_t type, ptrdiff_t offset, size_t size)
73 void *data = alloca(size);
74 struct xornsch_line_attr *line = data + offset;
76 memset(data, 0, size);
77 line->width = 70.;
78 line->cap_style = 1;
79 line->dash_length = 73.;
80 line->dash_space = 74.;
82 line->dash_style = 0;
83 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
84 assert_line(rev, ob, 70., 1, 0, 0., 0.);
86 line->dash_style = 1;
87 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
88 assert_line(rev, ob, 70., 1, 1, 0., 74.);
90 line->dash_style = 2;
91 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
92 assert_line(rev, ob, 70., 1, 2, 73., 74.);
94 line->dash_style = 3;
95 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
96 assert_line(rev, ob, 70., 1, 3, 73., 74.);
98 line->dash_style = 4;
99 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
100 assert_line(rev, ob, 70., 1, 4, 73., 74.);
103 static void check_fill_attr(xorn_revision_t rev, xorn_object_t ob,
104 xorn_obtype_t type, ptrdiff_t offset, size_t size)
106 void *data = alloca(size);
107 struct xornsch_fill_attr *fill = data + offset;
109 memset(data, 0, size);
110 fill->width = 81.;
111 fill->angle0 = 82;
112 fill->pitch0 = 83.;
113 fill->angle1 = 84;
114 fill->pitch1 = 85.;
116 fill->type = 0;
117 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
118 assert_fill(rev, ob, 0, 0., 0, 0., 0, 0.);
120 fill->type = 1;
121 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
122 assert_fill(rev, ob, 1, 0., 0, 0., 0, 0.);
124 fill->type = 2;
125 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
126 assert_fill(rev, ob, 2, 81., 82, 83., 84, 85.);
128 fill->type = 3;
129 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
130 assert_fill(rev, ob, 3, 81., 82, 83., 0, 0.);
132 fill->type = 4;
133 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
134 assert_fill(rev, ob, 4, 0., 0, 0., 0, 0.);
137 static void check_string(xorn_revision_t rev, xorn_object_t ob,
138 xorn_obtype_t type, ptrdiff_t offset, size_t size)
140 void *data = alloca(size);
141 struct xorn_string *in = data + offset;
142 const struct xorn_string *out;
144 memset(data, 0, size);
145 in->s = "ABCDE";
147 in->len = 3;
148 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
149 out = xorn_get_object_data(rev, ob, type) + offset;
150 assert(out->s != NULL);
151 assert(out->s != in->s);
152 assert(memcmp(in->s, out->s, 3) == 0);
153 assert(out->len == 3);
155 in->len = 0;
156 assert(xorn_set_object_data(rev, ob, type, data, NULL) == 0);
157 out = xorn_get_object_data(rev, ob, type) + offset;
158 assert(out->s == NULL);
159 assert(out->len == 0);
162 int main(void)
164 xorn_revision_t rev;
165 xorn_object_t ob;
166 struct xornsch_net net;
168 rev = xorn_new_revision(NULL);
169 assert(rev != NULL);
170 memset(&net, 0, sizeof net);
171 ob = xornsch_add_net(rev, &net, NULL);
172 assert(ob != NULL);
174 /* arc */
175 check_line_attr(rev, ob, xornsch_obtype_arc,
176 offsetof(struct xornsch_arc, line),
177 sizeof(struct xornsch_arc));
179 /* box */
180 check_line_attr(rev, ob, xornsch_obtype_box,
181 offsetof(struct xornsch_box, line),
182 sizeof(struct xornsch_box));
184 check_fill_attr(rev, ob, xornsch_obtype_box,
185 offsetof(struct xornsch_box, fill),
186 sizeof(struct xornsch_box));
188 /* circle */
189 check_line_attr(rev, ob, xornsch_obtype_circle,
190 offsetof(struct xornsch_circle, line),
191 sizeof(struct xornsch_circle));
193 check_fill_attr(rev, ob, xornsch_obtype_circle,
194 offsetof(struct xornsch_circle, fill),
195 sizeof(struct xornsch_circle));
197 /* component */
199 /* line */
200 check_line_attr(rev, ob, xornsch_obtype_line,
201 offsetof(struct xornsch_line, line),
202 sizeof(struct xornsch_line));
204 /* net */
206 /* path */
207 check_string(rev, ob, xornsch_obtype_path,
208 offsetof(struct xornsch_path, pathdata),
209 sizeof(struct xornsch_path));
211 check_line_attr(rev, ob, xornsch_obtype_path,
212 offsetof(struct xornsch_path, line),
213 sizeof(struct xornsch_path));
215 check_fill_attr(rev, ob, xornsch_obtype_path,
216 offsetof(struct xornsch_path, fill),
217 sizeof(struct xornsch_path));
219 /* picture */
221 /* text */
222 check_string(rev, ob, xornsch_obtype_text,
223 offsetof(struct xornsch_text, text),
224 sizeof(struct xornsch_text));
226 xorn_free_revision(rev);
227 return 0;