comparison tests/mpp/misc.c @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 ////////////////////////////////////////////////////////////
2 ... line splicing
3
4 he\
5 llo
6
7 /\
8 / comment
9
10 //\
11 comment
12
13 #define SPLIT splat
14 SPL\
15 IT
16
17 int c = '\0\
18 12';
19 const char *x = "blah \0\
20 12";
21
22 ////////////////////////////////////////////////////////////
23 ... digraphs
24
25 #define DI_STR(x) %:x
26 #define DI_CAT(x,y) x %:%: y
27
28 DI_STR(hi)
29 DI_CAT(fu,bar)
30
31 DI_STR(<:)
32 DI_STR(:>)
33 DI_STR(<%)
34 DI_STR(%>)
35 DI_STR(%:)
36
37 #define HASH %:
38 #define HASHHASH %:%:
39 HASH
40 HASHHASH
41
42 %:define FOO 123
43 #ifdef FOO
44 right
45 #else
46 wrong
47 #endif
48 %:undef FOO
49 #ifdef FOO
50 wrong
51 #else
52 right
53 #endif
54
55 ////////////////////////////////////////////////////////////
56 ... comments
57
58 a /**// b;
59
60 a = b//*q*/ blah
61 - c;
62
63 #define CAT(x,y) x ## y
64 #define CAT2(x,y) CAT(x,y)
65
66 CAT(/,*) not comment CAT(*,/)
67 CAT(/,/) not comment
68
69 <a/*comment*/b>
70
71 /* comment /* comment */
72 not comment
73
74 #define CM stuff // comment
75 CM staff
76 (should be stuff staff on one line)
77
78 #define CMM stuff /* comment */
79 CMM staff
80 (should be stuff staff on one line)
81
82 ////////////////////////////////////////////////////////////
83 ... conditionals
84
85 #if 0
86 wrong
87 #elif 1
88 right
89 #else
90 wrong
91 #endif
92
93 #if NOTDEF
94 wrong
95 #else
96 right
97 #endif
98
99 #if NOTDEF
100 wrong
101 #elif NOTDEF
102 wrong
103 #elif NOTDEF2
104 wrong
105 #elif 1
106 right
107 #endif
108
109 #if 0
110 #if 1
111 wrong
112 #else
113 wrong
114 #endif
115 #if 0
116 wrong
117 #else
118 wrong
119 #endif
120 #elif 1
121 right
122 #if 1
123 right
124 #else
125 wrong
126 #endif
127 #if 0
128 wrong
129 #else
130 right
131 #endif
132 #elif 1
133 wrong
134 #else
135 wrong
136 #endif
137
138 #if /*blah*/ 1 /*boo*/
139 right
140 #else /* bar */
141 wrong
142 #endif /* baz */
143
144 #ifdef NOTDEF
145 wrong
146 #else
147 right
148 #endif
149
150 #ifndef NOTDEF
151 right
152 #else
153 wrong
154 #endif
155
156 #define TWO 2
157 #if TWO > 1
158 right
159 #else
160 wrong
161 #endif
162
163 #if defined(TWO)
164 right
165 #else
166 wrong
167 #endif
168
169 #if defined TWO
170 right
171 #else
172 wrong
173 #endif
174
175 #if defined(THREE)
176 wrong
177 #else
178 right
179 #endif
180
181 #if defined THREE
182 wrong
183 #else
184 right
185 #endif
186
187 #if defined(defined)
188 wrong
189 #else
190 right
191 #endif
192
193 defined(TWO) should have a 2 in it
194 defined(THREE) should not be changed
195
196 #define SUCC(x) x+1
197
198 #if SUCC(1) == 2
199 right
200 #else
201 wrong
202 #endif
203
204 #if TWO == 2
205 right
206 #else
207 wrong
208 #endif
209
210 #if TWO + NOTDEF == 2
211 right
212 #else
213 wrong
214 #endif
215
216 #if SUCC(TWO + NOTDEF * 12574) == 3
217 right
218 #else
219 wrong
220 #endif
221
222 #if SUCC(TWO + '0') == '3'
223 right
224 #else
225 wrong
226 #endif
227
228 #if 'A'
229 right
230 #else
231 wrong
232 #endif
233
234 #if '\0'
235 wrong
236 #else
237 right
238 #endif
239
240 #if '\007' == 7
241 right
242 #else
243 wrong
244 #endif
245
246 #if defined TWO + 3 == 4
247 right
248 #else
249 wrong
250 #endif
251
252 #if defined NOTDEF * 5
253 wrong
254 #else
255 right
256 #endif
257
258 #undef TWO
259 #if defined(TWO)
260 wrong
261 #else
262 right
263 #endif
264
265 #if 1
266 #elif NOTDEF(1)
267 #endif
268
269 #if 0
270 #if NOTDEF(1)
271 #endif
272 #endif
273
274 ////////////////////////////////////////////////////////////
275 ... definitions
276
277 #define AA aa
278 #define BB() bb
279 #define CC(x) x cc
280 /* put these back in when/if varargs macros are supported */
281 /*#define DD(...) dd*/
282 /*#define EE(x, ...) x ee*/
283
284 AA
285 /*BB*/
286 /*CC*/
287 DD
288 EE
289
290 AA()
291 BB()
292 /*CC()*/
293 DD()
294 DD(,)
295 EE(,)
296 DD(,,)
297 EE(,,)
298
299 #undef AA
300 #undef BB
301 #undef CC
302 #undef DD
303 #undef EE
304
305 /*
306 #define FF(x, ...) __VA_ARGS__ x
307 FF(ff, )
308 FF(ff, a)
309 FF(ff, a, b)
310 FF(ff, a, b, c)
311 FF(ff, a, b, c, d)
312 */
313
314 #undef FF
315
316 #define define fedine
317 #define undef dunef
318 #define ifndef difnef
319 #define GG gg
320 GG should be gg
321 #ifdef GG
322 right
323 #else
324 wrong
325 #endif
326 define should be fedine
327 undef should be dunef
328 ifndef should be difnef
329
330 #ifdef define
331 right
332 #else
333 wrong
334 #endif
335
336 #ifdef undef
337 right
338 #else
339 wrong
340 #endif
341
342 #ifdef ifndef
343 right
344 #else
345 wrong
346 #endif
347
348 #ifndef ifndef
349 wrong
350 #else
351 right
352 #endif
353
354 #undef define
355 #undef undef
356 #undef ifndef
357
358 #ifdef define
359 wrong
360 #else
361 right
362 #endif
363
364 #ifdef undef
365 wrong
366 #else
367 right
368 #endif
369
370 #ifdef ifndef
371 wrong
372 #else
373 right
374 #endif
375
376 #define HH(a,b,c) a b c
377 HH((1,2),(3,4),(5,6))
378 /*
379 HH(1,
380 2,
381 3
382 )
383 HH(4,
384 5,
385 6)
386 */
387 #undef HH
388
389 #define SUM(a,b) a + b
390 #define CALL(x) x(1,2)
391 CALL(SUM) should be 1+2
392
393 #if CALL(SUM) == 3
394 right
395 #else
396 wrong
397 #endif
398
399 #define ADD SUM
400 ADD(1,ADD(2,3)) should be 1+2+3
401
402 /*
403 #define RECURSE RECURSE
404 RECURSE
405 */
406
407 /*
408 #define CROSSRECURSE1(x) x CROSSRECURSE2(1)
409 #define CROSSRECURSE2(x) x CROSSRECURSE1(2)
410 CROSSRECURSE1(0)
411 CROSSRECURSE2(0)
412 */
413
414 #define TWO two
415 #define ADDTWO(x) TWO + x
416 #undef TWO
417 ADDTWO(1) should be two + 1
418
419 ... legal redefinitions
420 #define ZERO (1-1)
421 #define ZERO /* foo */ (1-1) /* bar */
422 #define IDENTITY(x) ( x )
423 #define IDENTITY(x) ( /* foo */ x )
424
425
426 ////////////////////////////////////////////////////////////
427 ... stringize
428
429 #define STR(x) #x
430 #define STR2(x) STR(x)
431
432 #define STRY(x, y) #y
433 /* these should be using STRY not STR, but that doesn't work */
434 #if 0
435 STRY(, a b c) should be "a b c"
436 STRY(, a b c) should be "a b c"
437 STRY(, "stry\n") should be "\"stry\\n\""
438 STRY(, ) should be ""
439 STRY(,) should be ""
440 STRY(, /*woodle*/ b) should be "b"
441 STRY(, a/*noodle*/b) should be "a b"
442 STRY(, a /*boodle*/ b) should be "a b"
443 #endif
444 STR( a b c) should be "a b c"
445 STR( a b c) should be "a b c"
446 STR( "stry\n") should be "\"stry\\n\""
447 /*
448 STR( ) should be ""
449 STR() should be ""
450 */
451 STR( /*woodle*/ b) should be "b"
452 STR( a/*noodle*/b) should be "a b"
453 STR( a /*boodle*/ b) should be "a b"
454
455 #define GRACKLE(x) STR(x(1,2))
456 GRACKLE(SUM) should give "SUM(1,2)", not "1+2"
457 STR2(SUM)(3,4) should give "SUM"(3,4), not "3+4"
458
459 #if 0 /* broken */
460 STR(/*comment*/) should be ""
461 #endif
462
463 ////////////////////////////////////////////////////////////
464 ... concat
465
466 #define HASHES # ## #
467 #define YY(x,y) STR(x H y)
468 #define ZZ(x,y) STR2(x H y)
469
470 YY(1,2) should be "1 H 2"
471 ZZ(1,2) should be "1 ## 2"
472
473 ... pasting of punctuation is not allowed
474 CAT(+,+)
475 CAT(-,-)
476 CAT(<<,=)
477 CAT(>>,=)
478 CAT(<,<=)
479 CAT(>,>=)
480 CAT(<,>=)
481 CAT(<,CAT(<,=))
482 CAT(>,CAT(>,=))
483
484 CAT2(+,+)
485 CAT2(-,-)
486 CAT2(<<,=)
487 CAT2(>>,=)
488 CAT2(<,<=)
489 CAT2(>,>=)
490 CAT2(<,>=)
491 CAT2(<,CAT2(<,=))
492 CAT2(>,CAT2(>,=))
493
494 #define ONEHUNDRED 100
495 CAT(ONE, HUNDRED) should be 100
496
497 #define BASTET "bast"
498 #define ET ET "et"
499
500 CAT(BAST,ET) ---> "bast"
501 /*
502 CAT2(BAST,ET) ---> "bast" "et"
503 */
504
505 #undef BASTET
506 #undef ET
507
508 #define KLUDGE KLU
509 CAT2(KLUDGE, DGE) should be "KLU"
510 #undef KLUDGE
511 #define KLUDGE HACK KLUDGE
512 /*
513 CAT2(KLUDGE, ) should be "HACK KLUDGE" not "HACK HACK KLUDGE"
514 */
515
516 #define CATE(x) x ## // comment
517 CATE(10)
518 #if 0
519 #define ECAT(x) /* comment */ ## x
520 ECAT(20)
521 #endif
522
523 /*
524 #define CATVA(x, ...) x ## __VA_ARGS__
525 CATVA(z,y,z) should be zy,x
526 */
527
528 CAT(IDENTITY,IDENTITY(z)) should be IDENTITYIDENTITY(z)
529 CAT2(IDENTITY,IDENTITY(z)) should be IDENTITYz
530
531 CAT(1, /*comment*/ 2) should be 12
532
533 ////////////////////////////////////////////////////////////
534 ... directive recognition
535
536 #define EMPTY
537
538 /*
539 EMPTY #error wrong
540 EMPTY #define SHOULDNT bad
541 #ifdef SHOULDNT
542 wrong
543 #else
544 wrong
545 #endif
546 */
547
548 # define SHOULD1 good
549 #ifdef SHOULD1
550 right
551 #else
552 wrong
553 #endif
554
555 #define SHOULD2 good
556 #ifdef SHOULD2
557 right
558 #else
559 wrong
560 #endif
561
562 # define SHOULD3 good
563 #ifdef SHOULD3
564 right
565 #else
566 wrong
567 #endif
568
569 /*comment*/ #define SHOULD4 good
570 #ifdef SHOULD4
571 right
572 #else
573 wrong
574 #endif
575
576 /*
577 #define DEFINE #define FOO foo
578 DEFINE
579 #ifdef FOO
580 wrong
581 #else
582 right
583 #endif
584 */
585
586 /*
587 #define INCLUDE #include "nosuchfile.h"
588 INCLUDE
589 */
590
591 /*
592 #define ERROR #error wrong
593 ERROR
594 */
595
596 ////////////////////////////////////////////////////////////
597 ... keywords
598
599 #define register 1
600 #define int +
601 #define e 3
602 #if register int e == 4
603 right
604 #else
605 wrong
606 #endif
607
608 #undef register
609 #undef int
610 #undef e
611
612 ////////////////////////////////////////////////////////////
613 ... builtin macros
614
615 __STDC__ should be 1
616 __STDC_VERSION__ should be a date
617