]> git.sur5r.net Git - cc65/blob - test/ref/struct.c
ccd74953f1fc9b6105cda0cfcfd26fcd75ba7d4f
[cc65] / test / ref / struct.c
1 /*
2   !!DESCRIPTION!! structs
3   !!ORIGIN!!      LCC 4.1 Testsuite
4   !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
5 */
6
7 typedef struct point { int x,y; } point;
8 typedef struct rect { point pt1, pt2; } rect;
9
10 #define min(a, b) ((a) < (b) ? (a) : (b))
11 #define max(a, b) ((a) > (b) ? (a) : (b))
12
13 #ifdef NO_FUNCS_RETURN_STRUCTS
14 # ifdef NO_FUNCS_TAKE_STRUCTS
15 /* canonicalize rectangle coordinates */
16 void canonrect(rect *d,rect *r) {
17         d->pt1.x = min(r->pt1.x, r->pt2.x);
18         d->pt1.y = min(r->pt1.y, r->pt2.y);
19         d->pt2.x = max(r->pt1.x, r->pt2.x);
20         d->pt2.y = max(r->pt1.y, r->pt2.y);
21 }
22 /* add two points */
23 void addpoint(point *p, point *p1, point *p2) {
24         p->x= p1->x + p2->x;
25         p->y= p1->y + p2->y;
26 }
27 /* make a point from x and y components */
28 void makepoint(point *p,int x, int y) {
29         p->x = x;
30         p->y = y;
31 }
32 /* make a rectangle from two points */
33 void makerect(rect *d,point *p1, point *p2) {
34 rect r;
35         r.pt1.x = p1->x;
36         r.pt1.y = p1->y;
37         r.pt2.x = p2->x;
38         r.pt2.y = p2->y;
39
40         canonrect(d,&r);
41 }
42
43 #ifdef NO_SLOPPY_STRUCT_INIT
44 struct odd {char a[3]; } y = {{'a', 'b', 0 }};
45 #else
46 struct odd {char a[3]; } y = {'a', 'b', 0};
47 #endif
48
49 odd(struct odd *y) {
50         struct odd *x = y;
51         printf("%s\n\r", x->a);
52 }
53
54 # else  /* FUNCS_TAKE_STRUCTS */
55 /* canonicalize rectangle coordinates */
56 void canonrect(rect *d,rect r) {
57         d->pt1.x = min(r.pt1.x, r.pt2.x);
58         d->pt1.y = min(r.pt1.y, r.pt2.y);
59         d->pt2.x = max(r.pt1.x, r.pt2.x);
60         d->pt2.y = max(r.pt1.y, r.pt2.y);
61 }
62 /* add two points */
63 void addpoint(point *p, point p1, point p2) {
64         p->x= p1.x + p2.x;
65         p->y= p1.y + p2.y;
66 }
67 /* make a point from x and y components */
68 void makepoint(point *p,int x, int y) {
69         p->x = x;
70         p->y = y;
71 }
72 /* make a rectangle from two points */
73 void makerect(rect *d,point p1, point p2) {
74 rect r;
75         r.pt1 = p1;
76         r.pt2 = p2;
77
78         canonrect(d,r);
79 }
80
81 #ifdef NO_SLOPPY_STRUCT_INIT
82 struct odd {char a[3]; } y = {{'a', 'b', 0}};
83 #else
84 struct odd {char a[3]; } y = {'a', 'b', 0};
85 #endif
86
87 odd(struct odd y) {
88         struct odd x = y;
89         printf("%s\n\r", x.a);
90 }
91
92 # endif /* FUNCS_TAKE_STRUCTS */
93
94 #else /* FUNCS_RETURN_STRUCTS */
95
96 /* add two points */
97 point addpoint(point p1, point p2) {
98         p1.x += p2.x;
99         p1.y += p2.y;
100         return p1;
101 }
102 /* canonicalize rectangle coordinates */
103 rect canonrect(rect r) {
104         rect temp;
105
106         temp.pt1.x = min(r.pt1.x, r.pt2.x);
107         temp.pt1.y = min(r.pt1.y, r.pt2.y);
108         temp.pt2.x = max(r.pt1.x, r.pt2.x);
109         temp.pt2.y = max(r.pt1.y, r.pt2.y);
110         return temp;
111 }
112 /* make a point from x and y components */
113 point makepoint(int x, int y) {
114         point p;
115
116         p.x = x;
117         p.y = y;
118         return p;
119 }
120
121 /* make a rectangle from two points */
122 rect makerect(point p1, point p2) {
123         rect r;
124
125         r.pt1 = p1;
126         r.pt2 = p2;
127         return canonrect(r);
128 }
129
130 struct odd {char a[3]; } y =
131 {
132 #ifdef NO_SLOPPY_STRUCT_INIT
133         {
134 #endif
135         'a', 'b', 0
136 #ifdef NO_SLOPPY_STRUCT_INIT
137         }
138 #endif
139 };
140
141 odd(struct odd y)
142 {
143         struct odd x
144                 = y;
145         printf("%s\n\r", x.a);
146 }
147
148 #endif
149
150 /* is p in r? */
151 # ifdef NO_FUNCS_TAKE_STRUCTS
152 int ptinrect(point *p, rect *r) {
153         return p->x >= r->pt1.x && p->x < r->pt2.x
154                 && p->y >= r->pt1.y && p->y < r->pt2.y;
155 }
156 #else
157 int ptinrect(point p, rect r) {
158         return p.x >= r.pt1.x && p.x < r.pt2.x
159                 && p.y >= r.pt1.y && p.y < r.pt2.y;
160 }
161 #endif
162
163 #ifdef NO_FUNCS_RETURN_STRUCTS
164
165 #ifdef NO_LOCAL_STRUCT_INIT
166 #ifdef NO_SLOPPY_STRUCT_INIT
167 point pts[] = { {-1, -1},{ 1, 1},{ 20, 300},{ 500, 400 } };
168 #else
169 point pts[] = { -1, -1, 1, 1, 20, 300, 500, 400 };
170 #endif
171 point origin = { 0, 0 };
172 point maxpt = { 320, 320 };
173 #endif
174
175 main() {
176 int i;
177 point x;
178 rect screen;
179 #ifndef NO_LOCAL_STRUCT_INIT
180 point origin = { 0, 0 };
181 point maxpt = { 320, 320 };
182 #ifdef NO_SLOPPY_STRUCT_INIT
183 point pts[] = { {-1, -1},{ 1, 1},{ 20, 300},{ 500, 400 } };
184 #else
185 point pts[] = { -1, -1, 1, 1, 20, 300, 500, 400 };
186 #endif
187 #endif
188
189           makepoint (     &x,    -10,    -10);
190 #ifdef NO_FUNCS_TAKE_STRUCTS
191           addpoint  ( &maxpt, &maxpt,     &x);
192 #else
193           addpoint  ( &maxpt, maxpt,     x);
194 #endif
195           makepoint (     &x,     10,     10);
196
197 #ifdef NO_FUNCS_TAKE_STRUCTS
198           addpoint  (&origin,&origin,     &x);
199           makerect  (&screen, &maxpt,&origin);
200 #else
201           addpoint  (&origin,origin,     x);
202           makerect  (&screen, maxpt,origin);
203 #endif
204
205         for (i = 0; i < sizeof pts/sizeof pts[0]; i++) {
206                 makepoint(&x,pts[i].x, pts[i].y);
207                 printf("(%d,%d) is ", pts[i].x, x.y);
208 #ifdef NO_FUNCS_TAKE_STRUCTS
209                 if (ptinrect(&x, &screen) == 0)
210 #else
211                 if (ptinrect(x, screen) == 0)
212 #endif
213                 {
214                         printf("not ");
215                 }
216                 printf("within (%d,%d; %d,%d)\n\r", screen.pt1.x, screen.pt1.y,
217                         screen.pt2.x, screen.pt2.y);
218         }
219 #ifdef NO_FUNCS_TAKE_STRUCTS
220         odd(&y);
221 #else
222         odd(y);
223 #endif
224
225         return 0;
226 }
227
228 #else /* FUNCS_RETURN_STRUCTS */
229
230 main() {
231 int i;
232 point x, origin = { 0, 0 }, maxpt = { 320, 320 };
233
234 #ifdef NO_SLOPPY_STRUCT_INIT
235 point pts[] = { {-1, -1}, {1, 1}, {20, 300}, {500, 400} };
236 #else
237 point pts[] = { -1, -1, 1, 1, 20, 300, 500, 400 };
238 #endif
239
240 rect screen =
241         makerect(
242                 addpoint(maxpt, makepoint(-10, -10)),
243                 addpoint(origin, makepoint(10, 10))
244                 );
245
246         test1();
247         
248         for (i = 0; i < sizeof pts/sizeof pts[0]; i++) {
249                 printf("(%d,%d) is ", pts[i].x,
250                         (x = makepoint(pts[i].x, pts[i].y)).y);
251                 if (ptinrect(x, screen) == 0)
252                         printf("not ");
253                 printf("within (%d,%d; %d,%d)\n\r", screen.pt1.x, screen.pt1.y,
254                         screen.pt2.x, screen.pt2.y);
255         }
256         odd(y);
257
258         return 0;
259 }
260
261 #endif /* FUNCS_RETURN_STRUCTS */