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