scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

0200-cpp.c (1291B)


      1 /* taken from ISO/IEC 9899:1999 Section 6.10.3.5 p6 */
      2 
      3 #define str(s)      # s
      4 #define xstr(s)     str(s)
      5 #define debug(s, t) test1("x" # s "= %d, x" # t "= %d", \
      6 		  x ## s, x ## t)
      7 #define INCFILE(n) vers ## n
      8 #define vers2      0200-cpp
      9 #define glue(a, b) a ## b
     10 #define xglue(a, b) glue(a, b)
     11 #define HIGHLOW     "hello"
     12 #define LOW         LOW ", world"
     13 
     14 int
     15 test1(char *s, int x, int y)
     16 {
     17 	int i;
     18 
     19 	for (i = 0; s[i]; i++) {
     20 		if (s[i] != "x1= %d, x2= %d"[i])
     21 			return 0;
     22 	}
     23 	if (x + y != 3)
     24 		return 0;
     25 	return 1;
     26 }
     27 
     28 int
     29 test2(char *s1, char *s2)
     30 {
     31 	int i;
     32 
     33 	for (i = 0; s1[i]; i++) {
     34 		if (s1[i] != s2[i])
     35 			return 0;
     36 	}
     37 	return s1[i] == '\0' && s2[i] == '\0';
     38 }
     39 
     40 int
     41 test4(char *s)
     42 {
     43 	int i;
     44 
     45 	for (i = 0; s[i]; i++) {
     46 		if (s[i] != "hello"[i])
     47 			return 0;
     48 	}
     49 
     50 	return 1;
     51 }
     52 
     53 int
     54 test5(char *s)
     55 {
     56 	int i;
     57 
     58 	for (i = 0; s[i]; i++) {
     59 		if (s[i] != "hello, world"[i])
     60 			return 0;
     61 	}
     62 
     63 	return 1;
     64 }
     65 
     66 int
     67 main()
     68 {
     69 	int x1 = 1, x2 = 2;
     70 	char *s = "strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: @\n";
     71 
     72 	if (!debug(1, 2))
     73 		return 1;
     74 
     75 	if (!test2(str(strncmp("abc\0d", "abc", '\4') // this goes away
     76 == 0) str(: @\n), s))
     77 		return 2;
     78 
     79 #include xstr(INCFILE(2).h)
     80 
     81 	if (x != 2)
     82 		return 3;
     83 
     84 	if (!test4(glue(HIGH, LOW)))
     85 		return 4;
     86 
     87 	if (!test5(xglue(HIGH, LOW)))
     88 		return 5;
     89 
     90 	return 0;
     91 }