History

tests: Added numerous tests.

The tests are taken almost verbatim from the open source project PicoC. It can be found at https://code.google.com/p/picoc/. The tests range from very simple/trivial ones to more complicated. My view is that the more tests the better. Without tests like this I was very reluctant to make any changes to tcc for the fear of breaking things. The tests pass on Win32, OSX, Linux x86 and x86_64. One or two tests fail on each platform due to differences in the runtime library.

Milutin Jovanović committed 14 years ago

Commit 42c1b6b1 parent b0ebcfa111 files · +2988 −0Browse files

Unified · Split

111 files
  1. Makefile +2 −0
  2. tests2/00_assignment.c +18 −0
  3. tests2/00_assignment.expect +3 −0
  4. tests2/01_comment.c +14 −0
  5. tests2/01_comment.expect +5 −0
  6. tests2/02_printf.c +18 −0
  7. tests2/02_printf.expect +15 −0
  8. tests2/03_struct.c +31 −0
  9. tests2/03_struct.expect +6 −0
  10. tests2/04_for.c +15 −0
  11. tests2/04_for.expect +10 −0
  12. tests2/05_array.c +21 −0
  13. tests2/05_array.expect +10 −0
  14. tests2/06_case.c +29 −0
  15. tests2/06_case.expect +8 −0
  16. tests2/07_function.c +30 −0
  17. tests2/07_function.expect +4 −0
  18. tests2/08_while.c +24 −0
  19. tests2/08_while.expect +11 −0
  20. tests2/09_do_while.c +24 −0
  21. tests2/09_do_while.expect +11 −0
  22. tests2/10_pointer.c +40 −0
  23. tests2/10_pointer.expect +8 −0
  24. tests2/11_precedence.c +40 −0
  25. tests2/11_precedence.expect +15 −0
  26. tests2/12_hashdefine.c +14 −0
  27. tests2/12_hashdefine.expect +2 −0
  28. tests2/13_integer_literals.c +20 −0
  29. tests2/13_integer_literals.expect +5 −0
  30. tests2/14_if.c +21 −0
  31. tests2/14_if.expect +2 −0
  32. tests2/15_recursion.c +21 −0
  33. tests2/15_recursion.expect +10 −0
  34. tests2/16_nesting.c +21 −0
  35. tests2/16_nesting.expect +18 −0
  36. tests2/17_enum.c +29 −0
  37. tests2/17_enum.expect +3 −0
  38. tests2/18_include.c +12 −0
  39. tests2/18_include.expect +3 −0
  40. tests2/18_include.h +1 −0
  41. tests2/19_pointer_arithmetic.c +28 −0
  42. tests2/19_pointer_arithmetic.expect +3 −0
  43. tests2/20_pointer_comparison.c +24 −0
  44. tests2/20_pointer_comparison.expect +6 −0
  45. tests2/21_char_array.c +33 −0
  46. tests2/21_char_array.expect +7 −0
  47. tests2/22_floating_point.c +50 −0
  48. tests2/22_floating_point.expect +16 −0
  49. tests2/23_type_coercion.c +54 −0
  50. tests2/23_type_coercion.expect +12 −0
  51. tests2/24_math_library.c +28 −0
  52. tests2/24_math_library.expect +18 −0
  53. tests2/25_quicksort.c +83 −0
  54. tests2/25_quicksort.expect +2 −0
  55. tests2/26_character_constants.c +17 −0
  56. tests2/26_character_constants.expect +8 −0
  57. tests2/27_sizeof.c +16 −0
  58. tests2/27_sizeof.expect +3 −0
  59. tests2/28_strings.c +46 −0
  60. tests2/28_strings.expect +19 −0
  61. tests2/29_array_address.c +13 −0
  62. tests2/29_array_address.expect +1 −0
  63. tests2/30_hanoi.c +122 −0
  64. tests2/30_hanoi.expect +71 −0
  65. tests2/31_args.c +14 −0
  66. tests2/31_args.expect +7 −0
  67. tests2/32_led.c +266 −0
  68. tests2/32_led.expect +4 −0
  69. tests2/33_ternary_op.c +15 −0
  70. tests2/33_ternary_op.expect +10 −0
  71. tests2/34_array_assignment.c +23 −0
  72. tests2/34_array_assignment.expect +2 −0
  73. tests2/35_sizeof.c +14 −0
  74. tests2/35_sizeof.expect +2 −0
  75. tests2/36_array_initialisers.c +21 −0
  76. tests2/36_array_initialisers.expect +20 −0
  77. tests2/37_sprintf.c +17 −0
  78. tests2/37_sprintf.expect +20 −0
  79. tests2/38_multiple_array_index.c +32 −0
  80. tests2/38_multiple_array_index.expect +4 −0
  81. tests2/39_typedef.c +31 −0
  82. tests2/39_typedef.expect +3 −0
  83. tests2/40_stdio.c +52 −0
  84. tests2/40_stdio.expect +27 −0
  85. tests2/41_hashif.c +85 −0
  86. tests2/41_hashif.expect +6 −0
  87. tests2/42_function_pointer.c +18 −0
  88. tests2/42_function_pointer.expect +2 −0
  89. tests2/43_void_param.c +15 −0
  90. tests2/43_void_param.expect +1 −0
  91. tests2/44_scoped_declarations.c +17 −0
  92. tests2/44_scoped_declarations.expect +1 −0
  93. tests2/45_empty_for.c +18 −0
  94. tests2/45_empty_for.expect +10 −0
  95. tests2/46_grep.c +564 −0
  96. tests2/47_switch_return.c +24 −0
  97. tests2/47_switch_return.expect +4 −0
  98. tests2/48_nested_break.c +26 −0
  99. tests2/48_nested_break.expect +1 −0
  100. tests2/49_bracket_evaluation.c +23 −0
  101. tests2/49_bracket_evaluation.expect +1 −0
  102. tests2/50_logical_second_arg.c +29 −0
  103. tests2/50_logical_second_arg.expect +20 −0
  104. tests2/51_static.c +30 −0
  105. tests2/51_static.expect +8 −0
  106. tests2/52_unnamed_enum.c +27 −0
  107. tests2/52_unnamed_enum.expect +9 −0
  108. tests2/54_goto.c +56 −0
  109. tests2/54_goto.expect +8 −0
  110. tests2/LICENSE +37 −0
  111. tests2/Makefile +90 −0
Makefile+2−0
@@ -358,10 +358,12 @@ tar: · 357 unmodified lines
358export LIBTCC1358export LIBTCC1
359 359
360%est:360%est:
361 $(MAKE) -C tests2 $@
361 $(MAKE) -C tests $@362 $(MAKE) -C tests $@
362 363
363clean:364clean:
364 rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.exe libtcc_test$(EXESUF)365 rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.exe libtcc_test$(EXESUF)
366 $(MAKE) -C tests2 $@
365 $(MAKE) -C tests $@367 $(MAKE) -C tests $@
366ifneq ($(LIBTCC1),)368ifneq ($(LIBTCC1),)
367 $(MAKE) -C lib $@369 $(MAKE) -C lib $@
tests2/00_assignment.c+18−0
@@ -0,0 +1,18 @@
1#include <stdio.h>
2
3int main()
4{
5 int a;
6 a = 42;
7 printf("%d\n", a);
8
9 int b = 64;
10 printf("%d\n", b);
11
12 int c = 12, d = 34;
13 printf("%d, %d\n", c, d);
14
15 return 0;
16}
17
18// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/00_assignment.expect+3−0
@@ -0,0 +1,3 @@
142
264
312, 34
tests2/01_comment.c+14−0
@@ -0,0 +1,14 @@
1#include <stdio.h>
2
3int main()
4{
5 printf("Hello\n");
6 printf("Hello\n"); /* this is a comment */ printf("Hello\n");
7 printf("Hello\n");
8 // this is also a comment sayhello();
9 printf("Hello\n");
10
11 return 0;
12}
13
14// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/01_comment.expect+5−0
@@ -0,0 +1,5 @@
1Hello
2Hello
3Hello
4Hello
5Hello
tests2/02_printf.c+18−0
@@ -0,0 +1,18 @@
1#include <stdio.h>
2
3int main()
4{
5 printf("Hello world\n");
6
7 int Count;
8 for (Count = -5; Count <= 5; Count++)
9 printf("Count = %d\n", Count);
10
11 printf("String 'hello', 'there' is '%s', '%s'\n", "hello", "there");
12 printf("Character 'A' is '%c'\n", 65);
13 printf("Character 'a' is '%c'\n", 'a');
14
15 return 0;
16}
17
18// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/02_printf.expect+15−0
@@ -0,0 +1,15 @@
1Hello world
2Count = -5
3Count = -4
4Count = -3
5Count = -2
6Count = -1
7Count = 0
8Count = 1
9Count = 2
10Count = 3
11Count = 4
12Count = 5
13String 'hello', 'there' is 'hello', 'there'
14Character 'A' is 'A'
15Character 'a' is 'a'
tests2/03_struct.c+31−0
@@ -0,0 +1,31 @@
1#include <stdio.h>
2
3struct fred
4{
5 int boris;
6 int natasha;
7};
8
9void main()
10{
11 struct fred bloggs;
12
13 bloggs.boris = 12;
14 bloggs.natasha = 34;
15
16 printf("%d\n", bloggs.boris);
17 printf("%d\n", bloggs.natasha);
18
19 struct fred jones[2];
20 jones[0].boris = 12;
21 jones[0].natasha = 34;
22 jones[1].boris = 56;
23 jones[1].natasha = 78;
24
25 printf("%d\n", jones[0].boris);
26 printf("%d\n", jones[0].natasha);
27 printf("%d\n", jones[1].boris);
28 printf("%d\n", jones[1].natasha);
29
30 return 0;
31}
tests2/03_struct.expect+6−0
@@ -0,0 +1,6 @@
112
234
312
434
556
678
tests2/04_for.c+15−0
@@ -0,0 +1,15 @@
1#include <stdio.h>
2
3int main()
4{
5 int Count;
6
7 for (Count = 1; Count <= 10; Count++)
8 {
9 printf("%d\n", Count);
10 }
11
12 return 0;
13}
14
15// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/04_for.expect+10−0
@@ -0,0 +1,10 @@
11
22
33
44
55
66
77
88
99
1010
tests2/05_array.c+21−0
@@ -0,0 +1,21 @@
1#include <stdio.h>
2
3int main()
4{
5 int Count;
6 int Array[10];
7
8 for (Count = 1; Count <= 10; Count++)
9 {
10 Array[Count-1] = Count * Count;
11 }
12
13 for (Count = 0; Count < 10; Count++)
14 {
15 printf("%d\n", Array[Count]);
16 }
17
18 return 0;
19}
20
21// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/05_array.expect+10−0
@@ -0,0 +1,10 @@
11
24
39
416
525
636
749
864
981
10100
tests2/06_case.c+29−0
@@ -0,0 +1,29 @@
1#include <stdio.h>
2
3int main()
4{
5 int Count;
6
7 for (Count = 0; Count < 4; Count++)
8 {
9 printf("%d\n", Count);
10 switch (Count)
11 {
12 case 1:
13 printf("%d\n", 1);
14 break;
15
16 case 2:
17 printf("%d\n", 2);
18 break;
19
20 default:
21 printf("%d\n", 0);
22 break;
23 }
24 }
25
26 return 0;
27}
28
29// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/06_case.expect+8−0
@@ -0,0 +1,8 @@
10
20
31
41
52
62
73
80
tests2/07_function.c+30−0
@@ -0,0 +1,30 @@
1#include <stdio.h>
2
3int myfunc(int x)
4{
5 return x * x;
6}
7
8void vfunc(int a)
9{
10 printf("a=%d\n", a);
11}
12
13void qfunc()
14{
15 printf("qfunc()\n");
16}
17
18int main()
19{
20 printf("%d\n", myfunc(3));
21 printf("%d\n", myfunc(4));
22
23 vfunc(1234);
24
25 qfunc();
26
27 return 0;
28}
29
30// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/07_function.expect+4−0
@@ -0,0 +1,4 @@
19
216
3a=1234
4qfunc()
tests2/08_while.c+24−0
@@ -0,0 +1,24 @@
1#include <stdio.h>
2
3int main()
4{
5 int a;
6 int p;
7 int t;
8
9 a = 1;
10 p = 0;
11 t = 0;
12
13 while (a < 100)
14 {
15 printf("%d\n", a);
16 t = a;
17 a = t + p;
18 p = t;
19 }
20
21 return 0;
22}
23
24// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/08_while.expect+11−0
@@ -0,0 +1,11 @@
11
21
32
43
55
68
713
821
934
1055
1189
tests2/09_do_while.c+24−0
@@ -0,0 +1,24 @@
1#include <stdio.h>
2
3int main()
4{
5 int a;
6 int p;
7 int t;
8
9 a = 1;
10 p = 0;
11 t = 0;
12
13 do
14 {
15 printf("%d\n", a);
16 t = a;
17 a = t + p;
18 p = t;
19 } while (a < 100);
20
21 return 0;
22}
23
24// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/09_do_while.expect+11−0
@@ -0,0 +1,11 @@
11
21
32
43
55
68
713
821
934
1055
1189
tests2/10_pointer.c+40−0
@@ -0,0 +1,40 @@
1#include <stdio.h>
2
3struct ziggy
4{
5 int a;
6 int b;
7 int c;
8} bolshevic;
9
10int main()
11{
12 int a;
13 int *b;
14 int c;
15
16 a = 42;
17 b = &a;
18 printf("a = %d\n", *b);
19
20 bolshevic.a = 12;
21 bolshevic.b = 34;
22 bolshevic.c = 56;
23
24 printf("bolshevic.a = %d\n", bolshevic.a);
25 printf("bolshevic.b = %d\n", bolshevic.b);
26 printf("bolshevic.c = %d\n", bolshevic.c);
27
28 struct ziggy *tsar = &bolshevic;
29
30 printf("tsar->a = %d\n", tsar->a);
31 printf("tsar->b = %d\n", tsar->b);
32 printf("tsar->c = %d\n", tsar->c);
33
34 b = &(bolshevic.b);
35 printf("bolshevic.b = %d\n", *b);
36
37 return 0;
38}
39
40// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/10_pointer.expect+8−0
@@ -0,0 +1,8 @@
1a = 42
2bolshevic.a = 12
3bolshevic.b = 34
4bolshevic.c = 56
5tsar->a = 12
6tsar->b = 34
7tsar->c = 56
8bolshevic.b = 34
tests2/11_precedence.c+40−0
@@ -0,0 +1,40 @@
1#include <stdio.h>
2
3int main()
4{
5 int a;
6 int b;
7 int c;
8 int d;
9 int e;
10 int f;
11 int x;
12 int y;
13
14 a = 12;
15 b = 34;
16 c = 56;
17 d = 78;
18 e = 0;
19 f = 1;
20
21 printf("%d\n", c + d);
22 printf("%d\n", (y = c + d));
23 printf("%d\n", e || e && f);
24 printf("%d\n", e || f && f);
25 printf("%d\n", e && e || f);
26 printf("%d\n", e && f || f);
27 printf("%d\n", a && f | f);
28 printf("%d\n", a | b ^ c & d);
29 printf("%d, %d\n", a == a, a == b);
30 printf("%d, %d\n", a != a, a != b);
31 printf("%d\n", a != b && c != d);
32 printf("%d\n", a + b * c / f);
33 printf("%d\n", a + b * c / f);
34 printf("%d\n", (4 << 4));
35 printf("%d\n", (64 >> 4));
36
37 return 0;
38}
39
40// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/11_precedence.expect+15−0
@@ -0,0 +1,15 @@
1134
2134
30
41
51
61
71
846
91, 0
100, 1
111
121916
131916
1464
154
tests2/12_hashdefine.c+14−0
@@ -0,0 +1,14 @@
1#include <stdio.h>
2
3#define FRED 12
4#define BLOGGS(x) (12*(x))
5
6int main()
7{
8 printf("%d\n", FRED);
9 printf("%d, %d, %d\n", BLOGGS(1), BLOGGS(2), BLOGGS(3));
10
11 return 0;
12}
13
14// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/12_hashdefine.expect+2−0
@@ -0,0 +1,2 @@
112
212, 24, 36
tests2/13_integer_literals.c+20−0
@@ -0,0 +1,20 @@
1#include <stdio.h>
2
3int main()
4{
5 int a = 24680;
6 int b = 01234567;
7 int c = 0x2468ac;
8 int d = 0x2468AC;
9 int e = 0b010101010101;
10
11 printf("%d\n", a);
12 printf("%d\n", b);
13 printf("%d\n", c);
14 printf("%d\n", d);
15 printf("%d\n", e);
16
17 return 0;
18}
19
20// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/13_integer_literals.expect+5−0
@@ -0,0 +1,5 @@
124680
2342391
32386092
42386092
51365
tests2/14_if.c+21−0
@@ -0,0 +1,21 @@
1#include <stdio.h>
2
3int main()
4{
5 int a = 1;
6
7 if (a)
8 printf("a is true\n");
9 else
10 printf("a is false\n");
11
12 int b = 0;
13 if (b)
14 printf("b is true\n");
15 else
16 printf("b is false\n");
17
18 return 0;
19}
20
21// vim: set expandtab ts=4 sw=3 sts=3 tw=80 :
tests2/14_if.expect+2−0
@@ -0,0 +1,2 @@
1a is true
2b is false
tests2/15_recursion.c+21−0
@@ -0,0 +1,21 @@
1#include <stdio.h>
2
3int factorial(int i)
4{
5 if (i < 2)
6 return i;
7 else
8 return i * factorial(i - 1);
9}
10
11int main()
12{
13 int Count;
14
15 for (Count = 1; Count <= 10; Count++)
16 printf("%d\n", factorial(Count));
17
18 return 0;
19}
20
21/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/15_recursion.expect+10−0
@@ -0,0 +1,10 @@
11
22
36
424
5120
6720
75040
840320
9362880
103628800
tests2/16_nesting.c+21−0
@@ -0,0 +1,21 @@
1#include <stdio.h>
2
3int main()
4{
5 int x, y, z;
6
7 for (x = 0; x < 2; x++)
8 {
9 for (y = 0; y < 3; y++)
10 {
11 for (z = 0; z < 3; z++)
12 {
13 printf("%d %d %d\n", x, y, z);
14 }
15 }
16 }
17
18 return 0;
19}
20
21/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/16_nesting.expect+18−0
@@ -0,0 +1,18 @@
10 0 0
20 0 1
30 0 2
40 1 0
50 1 1
60 1 2
70 2 0
80 2 1
90 2 2
101 0 0
111 0 1
121 0 2
131 1 0
141 1 1
151 1 2
161 2 0
171 2 1
181 2 2
tests2/17_enum.c+29−0
@@ -0,0 +1,29 @@
1#include <stdio.h>
2
3enum fred
4{
5 a,
6 b,
7 c,
8 d,
9 e = 54,
10 f = 73,
11 g,
12 h
13};
14
15int main()
16{
17 enum fred frod;
18
19 printf("%d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h);
20 /* printf("%d\n", frod); */
21 frod = 12;
22 printf("%d\n", frod);
23 frod = e;
24 printf("%d\n", frod);
25
26 return 0;
27}
28
29/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/17_enum.expect+3−0
@@ -0,0 +1,3 @@
10 1 2 3 54 73 74 75
212
354
tests2/18_include.c+12−0
@@ -0,0 +1,12 @@
1#include <stdio.h>
2
3int main()
4{
5 printf("including\n");
6#include "18_include.h"
7 printf("done\n");
8
9 return 0;
10}
11
12/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/18_include.expect+3−0
@@ -0,0 +1,3 @@
1including
2included
3done
tests2/18_include.h+1−0
@@ -0,0 +1 @@
1printf("included\n");
tests2/19_pointer_arithmetic.c+28−0
@@ -0,0 +1,28 @@
1#include <stdio.h>
2
3int main()
4{
5 int a;
6 int *b;
7 int *c;
8
9 a = 42;
10 b = &a;
11 c = NULL;
12
13 printf("%d\n", *b);
14
15 if (b == NULL)
16 printf("b is NULL\n");
17 else
18 printf("b is not NULL\n");
19
20 if (c == NULL)
21 printf("c is NULL\n");
22 else
23 printf("c is not NULL\n");
24
25 return 0;
26}
27
28/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/19_pointer_arithmetic.expect+3−0
@@ -0,0 +1,3 @@
142
2b is not NULL
3c is NULL
tests2/20_pointer_comparison.c+24−0
@@ -0,0 +1,24 @@
1#include <stdio.h>
2
3int main()
4{
5 int a;
6 int b;
7 int *d;
8 int *e;
9 d = &a;
10 e = &b;
11 a = 12;
12 b = 34;
13 printf("%d\n", *d);
14 printf("%d\n", *e);
15 printf("%d\n", d == e);
16 printf("%d\n", d != e);
17 d = e;
18 printf("%d\n", d == e);
19 printf("%d\n", d != e);
20
21 return 0;
22}
23
24/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/20_pointer_comparison.expect+6−0
@@ -0,0 +1,6 @@
112
234
30
41
51
60
tests2/21_char_array.c+33−0
@@ -0,0 +1,33 @@
1#include <stdio.h>
2
3int main()
4{
5 int x = 'a';
6 char y = x;
7
8 char *a = "hello";
9
10 printf("%s\n", a);
11
12 int c;
13 c = *a;
14
15 char *b;
16 for (b = a; *b != 0; b++)
17 printf("%c: %d\n", *b, *b);
18
19 char destarray[10];
20 char *dest = &destarray[0];
21 char *src = a;
22
23 while (*src != 0)
24 *dest++ = *src++;
25
26 *dest = 0;
27
28 printf("copied string is %s\n", destarray);
29
30 return 0;
31}
32
33/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/21_char_array.expect+7−0
@@ -0,0 +1,7 @@
1hello
2h: 104
3e: 101
4l: 108
5l: 108
6o: 111
7copied string is hello
tests2/22_floating_point.c+50−0
@@ -0,0 +1,50 @@
1#include <stdio.h>
2#include <math.h>
3
4int main()
5{
6 // variables
7 float a = 12.34 + 56.78;
8 printf("%f\n", a);
9
10 // infix operators
11 printf("%f\n", 12.34 + 56.78);
12 printf("%f\n", 12.34 - 56.78);
13 printf("%f\n", 12.34 * 56.78);
14 printf("%f\n", 12.34 / 56.78);
15
16 // comparison operators
17 printf("%d %d %d %d %d %d\n", 12.34 < 56.78, 12.34 <= 56.78, 12.34 == 56.78, 12.34 >= 56.78, 12.34 > 56.78, 12.34 != 56.78);
18 printf("%d %d %d %d %d %d\n", 12.34 < 12.34, 12.34 <= 12.34, 12.34 == 12.34, 12.34 >= 12.34, 12.34 > 12.34, 12.34 != 12.34);
19 printf("%d %d %d %d %d %d\n", 56.78 < 12.34, 56.78 <= 12.34, 56.78 == 12.34, 56.78 >= 12.34, 56.78 > 12.34, 56.78 != 12.34);
20
21 // assignment operators
22 a = 12.34;
23 a += 56.78;
24 printf("%f\n", a);
25
26 a = 12.34;
27 a -= 56.78;
28 printf("%f\n", a);
29
30 a = 12.34;
31 a *= 56.78;
32 printf("%f\n", a);
33
34 a = 12.34;
35 a /= 56.78;
36 printf("%f\n", a);
37
38 // prefix operators
39 printf("%f\n", +12.34);
40 printf("%f\n", -12.34);
41
42 // type coercion
43 a = 2;
44 printf("%f\n", a);
45 printf("%f\n", sin(2));
46
47 return 0;
48}
49
50/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/22_floating_point.expect+16−0
@@ -0,0 +1,16 @@
169.120003
269.120000
3-44.440000
4700.665200
50.217330
61 1 0 0 0 1
70 1 1 1 0 0
80 0 0 1 1 1
969.120003
10-44.439999
11700.665222
120.217330
1312.340000
14-12.340000
152.000000
160.909297
tests2/23_type_coercion.c+54−0
@@ -0,0 +1,54 @@
1#include <stdio.h>
2
3void charfunc(char a)
4{
5 printf("char: %c\n", a);
6}
7
8void intfunc(int a)
9{
10 printf("int: %d\n", a);
11}
12
13void floatfunc(float a)
14{
15 printf("float: %f\n", a);
16}
17
18int main()
19{
20 charfunc('a');
21 charfunc(98);
22 charfunc(99.0);
23
24 intfunc('a');
25 intfunc(98);
26 intfunc(99.0);
27
28 floatfunc('a');
29 floatfunc(98);
30 floatfunc(99.0);
31
32 /* printf("%c %d %f\n", 'a', 'b', 'c'); */
33 /* printf("%c %d %f\n", 97, 98, 99); */
34 /* printf("%c %d %f\n", 97.0, 98.0, 99.0); */
35
36 char b = 97;
37 char c = 97.0;
38
39 printf("%d %d\n", b, c);
40
41 int d = 'a';
42 int e = 97.0;
43
44 printf("%d %d\n", d, e);
45
46 float f = 'a';
47 float g = 97;
48
49 printf("%f %f\n", f, g);
50
51 return 0;
52}
53
54/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/23_type_coercion.expect+12−0
@@ -0,0 +1,12 @@
1char: a
2char: b
3char: c
4int: 97
5int: 98
6int: 99
7float: 97.000000
8float: 98.000000
9float: 99.000000
1097 97
1197 97
1297.000000 97.000000
tests2/24_math_library.c+28−0
@@ -0,0 +1,28 @@
1#include <stdio.h>
2#include <math.h>
3
4int main()
5{
6 printf("%f\n", sin(0.12));
7 printf("%f\n", cos(0.12));
8 printf("%f\n", tan(0.12));
9 printf("%f\n", asin(0.12));
10 printf("%f\n", acos(0.12));
11 printf("%f\n", atan(0.12));
12 printf("%f\n", sinh(0.12));
13 printf("%f\n", cosh(0.12));
14 printf("%f\n", tanh(0.12));
15 printf("%f\n", exp(0.12));
16 printf("%f\n", fabs(-0.12));
17 printf("%f\n", log(0.12));
18 printf("%f\n", log10(0.12));
19 printf("%f\n", pow(0.12, 0.12));
20 printf("%f\n", sqrt(0.12));
21 printf("%f\n", round(12.34));
22 printf("%f\n", ceil(12.34));
23 printf("%f\n", floor(12.34));
24
25 return 0;
26}
27
28/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/24_math_library.expect+18−0
@@ -0,0 +1,18 @@
10.119712
20.992809
30.120579
40.120290
51.450506
60.119429
70.120288
81.007209
90.119427
101.127497
110.120000
12-2.120264
13-0.920819
140.775357
150.346410
1612.000000
1713.000000
1812.000000
tests2/25_quicksort.c+83−0
@@ -0,0 +1,83 @@
1#include <stdio.h>
2
3int array[16];
4
5//Swap integer values by array indexes
6void swap(int a, int b)
7{
8 int tmp = array[a];
9 array[a] = array[b];
10 array[b] = tmp;
11}
12
13//Partition the array into two halves and return the
14//index about which the array is partitioned
15int partition(int left, int right)
16{
17 int pivotIndex = left;
18 int pivotValue = array[pivotIndex];
19 int index = left;
20 int i;
21
22 swap(pivotIndex, right);
23 for(i = left; i < right; i++)
24 {
25 if(array[i] < pivotValue)
26 {
27 swap(i, index);
28 index += 1;
29 }
30 }
31 swap(right, index);
32
33 return index;
34}
35
36//Quicksort the array
37void quicksort(int left, int right)
38{
39 if(left >= right)
40 return;
41
42 int index = partition(left, right);
43 quicksort(left, index - 1);
44 quicksort(index + 1, right);
45}
46
47int main()
48{
49 int i;
50
51 array[0] = 62;
52 array[1] = 83;
53 array[2] = 4;
54 array[3] = 89;
55 array[4] = 36;
56 array[5] = 21;
57 array[6] = 74;
58 array[7] = 37;
59 array[8] = 65;
60 array[9] = 33;
61 array[10] = 96;
62 array[11] = 38;
63 array[12] = 53;
64 array[13] = 16;
65 array[14] = 74;
66 array[15] = 55;
67
68 for (i = 0; i < 16; i++)
69 printf("%d ", array[i]);
70
71 printf("\n");
72
73 quicksort(0, 15);
74
75 for (i = 0; i < 16; i++)
76 printf("%d ", array[i]);
77
78 printf("\n");
79
80 return 0;
81}
82
83/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/25_quicksort.expect+2−0
@@ -0,0 +1,2 @@
162 83 4 89 36 21 74 37 65 33 96 38 53 16 74 55
24 16 21 33 36 37 38 53 55 62 65 74 74 83 89 96
tests2/26_character_constants.c+17−0
@@ -0,0 +1,17 @@
1#include <stdio.h>
2
3int main()
4{
5 printf("%d\n", '\1');
6 printf("%d\n", '\10');
7 printf("%d\n", '\100');
8 printf("%d\n", '\x01');
9 printf("%d\n", '\x0e');
10 printf("%d\n", '\x10');
11 printf("%d\n", '\x40');
12 printf("test \x40\n");
13
14 return 0;
15}
16
17/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/26_character_constants.expect+8−0
@@ -0,0 +1,8 @@
11
28
364
41
514
616
764
8test @
tests2/27_sizeof.c+16−0
@@ -0,0 +1,16 @@
1#include <stdio.h>
2
3int main()
4{
5 char a;
6 int b;
7 double c;
8
9 printf("%d\n", sizeof(a));
10 printf("%d\n", sizeof(b));
11 printf("%d\n", sizeof(c));
12
13 return 0;
14}
15
16/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/27_sizeof.expect+3−0
@@ -0,0 +1,3 @@
11
24
38
tests2/28_strings.c+46−0
@@ -0,0 +1,46 @@
1#include <stdio.h>
2#include <string.h>
3#include <strings.h>
4
5int main()
6{
7 char a[10];
8
9 strcpy(a, "hello");
10 printf("%s\n", a);
11
12 strncpy(a, "gosh", 2);
13 printf("%s\n", a);
14
15 printf("%d\n", strcmp(a, "apple") > 0);
16 printf("%d\n", strcmp(a, "goere") > 0);
17 printf("%d\n", strcmp(a, "zebra") < 0);
18
19 printf("%d\n", strlen(a));
20
21 strcat(a, "!");
22 printf("%s\n", a);
23
24 printf("%d\n", strncmp(a, "apple", 2) > 0);
25 printf("%d\n", strncmp(a, "goere", 2) == 0);
26 printf("%d\n", strncmp(a, "goerg", 2) == 0);
27 printf("%d\n", strncmp(a, "zebra", 2) < 0);
28
29 printf("%s\n", index(a, 'o'));
30 printf("%s\n", rindex(a, 'l'));
31 printf("%d\n", rindex(a, 'x') == NULL);
32
33 memset(&a[1], 'r', 4);
34 printf("%s\n", a);
35
36 memcpy(&a[2], a, 2);
37 printf("%s\n", a);
38
39 printf("%d\n", memcmp(a, "apple", 4) > 0);
40 printf("%d\n", memcmp(a, "grgr", 4) == 0);
41 printf("%d\n", memcmp(a, "zebra", 4) < 0);
42
43 return 0;
44}
45
46/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/28_strings.expect+19−0
@@ -0,0 +1,19 @@
1hello
2gollo
31
41
51
65
7gollo!
81
91
101
111
12ollo!
13lo!
141
15grrrr!
16grgrr!
171
181
191
tests2/29_array_address.c+13−0
@@ -0,0 +1,13 @@
1#include <stdio.h>
2#include <string.h>
3
4int main()
5{
6 char a[10];
7 strcpy(a, "abcdef");
8 printf("%s\n", &a[1]);
9
10 return 0;
11}
12
13/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/29_array_address.expect+1−0
@@ -0,0 +1 @@
1bcdef
tests2/30_hanoi.c+122−0
@@ -0,0 +1,122 @@
1/* example from http://barnyard.syr.edu/quickies/hanoi.c */
2
3/* hanoi.c: solves the tower of hanoi problem. (Programming exercise.) */
4/* By Terry R. McConnell (12/2/97) */
5/* Compile: cc -o hanoi hanoi.c */
6
7/* This program does no error checking. But then, if it's right,
8 it's right ... right ? */
9
10
11/* The original towers of hanoi problem seems to have been originally posed
12 by one M. Claus in 1883. There is a popular legend that goes along with
13 it that has been often repeated and paraphrased. It goes something like this:
14 In the great temple at Benares there are 3 golden spikes. On one of them,
15 God placed 64 disks increasing in size from bottom to top, at the beginning
16 of time. Since then, and to this day, the priest on duty constantly transfers
17 disks, one at a time, in such a way that no larger disk is ever put on top
18 of a smaller one. When the disks have been transferred entirely to another
19 spike the Universe will come to an end in a large thunderclap.
20
21 This paraphrases the original legend due to DeParville, La Nature, Paris 1884,
22 Part I, 285-286. For this and further information see: Mathematical
23 Recreations & Essays, W.W. Rouse Ball, MacMillan, NewYork, 11th Ed. 1967,
24 303-305.
25 *
26 *
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31
32#define TRUE 1
33#define FALSE 0
34
35/* This is the number of "disks" on tower A initially. Taken to be 64 in the
36 * legend. The number of moves required, in general, is 2^N - 1. For N = 64,
37 * this is 18,446,744,073,709,551,615 */
38#define N 4
39
40/* These are the three towers. For example if the state of A is 0,1,3,4, that
41 * means that there are three discs on A of sizes 1, 3, and 4. (Think of right
42 * as being the "down" direction.) */
43int A[N], B[N], C[N];
44
45void Hanoi(int,int*,int*,int*);
46
47/* Print the current configuration of A, B, and C to the screen */
48void PrintAll()
49{
50 int i;
51
52 printf("A: ");
53 for(i=0;i<N;i++)printf(" %d ",A[i]);
54 printf("\n");
55
56 printf("B: ");
57 for(i=0;i<N;i++)printf(" %d ",B[i]);
58 printf("\n");
59
60 printf("C: ");
61 for(i=0;i<N;i++)printf(" %d ",C[i]);
62 printf("\n");
63 printf("------------------------------------------\n");
64 return;
65}
66
67/* Move the leftmost nonzero element of source to dest, leave behind 0. */
68/* Returns the value moved (not used.) */
69int Move(int *source, int *dest)
70{
71 int i,j;
72
73 while (i<N && (source[i])==0) i++;
74 while (j<N && (dest[j])==0) j++;
75
76 dest[j-1] = source[i];
77 source[i] = 0;
78 PrintAll(); /* Print configuration after each move. */
79 return dest[j-1];
80}
81
82
83/* Moves first n nonzero numbers from source to dest using the rules of Hanoi.
84 Calls itself recursively.
85 */
86void Hanoi(int n,int *source, int *dest, int *spare)
87{
88 int i;
89 if(n==1){
90 Move(source,dest);
91 return;
92 }
93
94 Hanoi(n-1,source,spare,dest);
95 Move(source,dest);
96 Hanoi(n-1,spare,dest,source);
97 return;
98}
99
100int main()
101{
102 int i;
103
104 /* initialize the towers */
105 for(i=0;i<N;i++)A[i]=i+1;
106 for(i=0;i<N;i++)B[i]=0;
107 for(i=0;i<N;i++)C[i]=0;
108
109 printf("Solution of Tower of Hanoi Problem with %d Disks\n\n",N);
110
111 /* Print the starting state */
112 printf("Starting state:\n");
113 PrintAll();
114 printf("\n\nSubsequent states:\n\n");
115
116 /* Do it! Use A = Source, B = Destination, C = Spare */
117 Hanoi(N,A,B,C);
118
119 return 0;
120}
121
122/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/30_hanoi.expect+71−0
@@ -0,0 +1,71 @@
1Solution of Tower of Hanoi Problem with 4 Disks
2
3Starting state:
4A: 1 2 3 4
5B: 0 0 0 0
6C: 0 0 0 0
7------------------------------------------
8
9
10Subsequent states:
11
12A: 0 2 3 4
13B: 0 0 0 0
14C: 0 0 0 1
15------------------------------------------
16A: 0 0 3 4
17B: 0 0 0 2
18C: 0 0 0 1
19------------------------------------------
20A: 0 0 3 4
21B: 0 0 1 2
22C: 0 0 0 0
23------------------------------------------
24A: 0 0 0 4
25B: 0 0 1 2
26C: 0 0 0 3
27------------------------------------------
28A: 0 0 1 4
29B: 0 0 0 2
30C: 0 0 0 3
31------------------------------------------
32A: 0 0 1 4
33B: 0 0 0 0
34C: 0 0 2 3
35------------------------------------------
36A: 0 0 0 4
37B: 0 0 0 0
38C: 0 1 2 3
39------------------------------------------
40A: 0 0 0 0
41B: 0 0 0 4
42C: 0 1 2 3
43------------------------------------------
44A: 0 0 0 0
45B: 0 0 1 4
46C: 0 0 2 3
47------------------------------------------
48A: 0 0 0 2
49B: 0 0 1 4
50C: 0 0 0 3
51------------------------------------------
52A: 0 0 1 2
53B: 0 0 0 4
54C: 0 0 0 3
55------------------------------------------
56A: 0 0 1 2
57B: 0 0 3 4
58C: 0 0 0 0
59------------------------------------------
60A: 0 0 0 2
61B: 0 0 3 4
62C: 0 0 0 1
63------------------------------------------
64A: 0 0 0 0
65B: 0 2 3 4
66C: 0 0 0 1
67------------------------------------------
68A: 0 0 0 0
69B: 1 2 3 4
70C: 0 0 0 0
71------------------------------------------
tests2/31_args.c+14−0
@@ -0,0 +1,14 @@
1#include <stdio.h>
2
3int main(int argc, char **argv)
4{
5 int Count;
6
7 printf("hello world %d\n", argc);
8 for (Count = 0; Count < argc; Count++)
9 printf("arg %d: %s\n", Count, argv[Count]);
10
11 return 0;
12}
13
14/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/31_args.expect+7−0
@@ -0,0 +1,7 @@
1hello world 6
2arg 0: 31_args.c
3arg 1: -
4arg 2: arg1
5arg 3: arg2
6arg 4: arg3
7arg 5: arg4
tests2/32_led.c+266−0
@@ -0,0 +1,266 @@
1/* example from http://barnyard.syr.edu/quickies/led.c */
2
3/* led.c: print out number as if on 7 line led display. I.e., write integer
4 given on command line like this:
5 _ _ _
6 | _| _| |_| |_
7 | |_ _| | _| etc.
8
9 We assume the terminal behaves like a classical teletype. So the top
10 lines of all digits have to be printed first, then the middle lines of
11 all digits, etc.
12
13 By Terry R. McConnell
14
15compile: cc -o led led.c
16
17If you just want to link in the subroutine print_led that does all the
18work, compile with -DNO_MAIN, and declare the following in any source file
19that uses the call:
20
21extern void print_led(unsigned long x, char *buf);
22
23Bug: you cannot call repeatedly to print more than one number to a line.
24That would require curses or some other terminal API that allows moving the
25cursor to a previous line.
26
27*/
28
29
30
31#include <stdlib.h>
32#include <stdio.h>
33
34#define MAX_DIGITS 32
35#define NO_MAIN
36
37
38/* Print the top line of the digit d into buffer.
39 Does not null terminate buffer. */
40
41void topline(int d, char *p){
42
43 *p++ = ' ';
44 switch(d){
45
46 /* all these have _ on top line */
47
48 case 0:
49 case 2:
50 case 3:
51 case 5:
52 case 7:
53 case 8:
54 case 9:
55 *p++ = '_';
56 break;
57 default:
58 *p++=' ';
59
60 }
61 *p++=' ';
62}
63
64/* Print the middle line of the digit d into the buffer.
65 Does not null terminate. */
66
67void midline(int d, char *p){
68
69 switch(d){
70
71 /* those that have leading | on middle line */
72
73 case 0:
74 case 4:
75 case 5:
76 case 6:
77 case 8:
78 case 9:
79 *p++='|';
80 break;
81 default:
82 *p++=' ';
83 }
84 switch(d){
85
86 /* those that have _ on middle line */
87
88 case 2:
89 case 3:
90 case 4:
91 case 5:
92 case 6:
93 case 8:
94 case 9:
95 *p++='_';
96 break;
97 default:
98 *p++=' ';
99
100 }
101 switch(d){
102
103 /* those that have closing | on middle line */
104
105 case 0:
106 case 1:
107 case 2:
108 case 3:
109 case 4:
110 case 7:
111 case 8:
112 case 9:
113 *p++='|';
114 break;
115 default:
116 *p++=' ';
117
118 }
119}
120
121/* Print the bottom line of the digit d. Does not null terminate. */
122
123void botline(int d, char *p){
124
125
126 switch(d){
127
128 /* those that have leading | on bottom line */
129
130 case 0:
131 case 2:
132 case 6:
133 case 8:
134 *p++='|';
135 break;
136 default:
137 *p++=' ';
138 }
139 switch(d){
140
141 /* those that have _ on bottom line */
142
143 case 0:
144 case 2:
145 case 3:
146 case 5:
147 case 6:
148 case 8:
149 *p++='_';
150 break;
151 default:
152 *p++=' ';
153
154 }
155 switch(d){
156
157 /* those that have closing | on bottom line */
158
159 case 0:
160 case 1:
161 case 3:
162 case 4:
163 case 5:
164 case 6:
165 case 7:
166 case 8:
167 case 9:
168 *p++='|';
169 break;
170 default:
171 *p++=' ';
172
173 }
174}
175
176/* Write the led representation of integer to string buffer. */
177
178void print_led(unsigned long x, char *buf)
179{
180
181 int i=0,n;
182 static int d[MAX_DIGITS];
183
184
185 /* extract digits from x */
186
187 n = ( x == 0L ? 1 : 0 ); /* 0 is a digit, hence a special case */
188
189 while(x){
190 d[n++] = (int)(x%10L);
191 if(n >= MAX_DIGITS)break;
192 x = x/10L;
193 }
194
195 /* print top lines of all digits */
196
197 for(i=n-1;i>=0;i--){
198 topline(d[i],buf);
199 buf += 3;
200 *buf++=' ';
201 }
202 *buf++='\n'; /* move teletype to next line */
203
204 /* print middle lines of all digits */
205
206 for(i=n-1;i>=0;i--){
207 midline(d[i],buf);
208 buf += 3;
209 *buf++=' ';
210 }
211 *buf++='\n';
212
213 /* print bottom lines of all digits */
214
215 for(i=n-1;i>=0;i--){
216 botline(d[i],buf);
217 buf += 3;
218 *buf++=' ';
219 }
220 *buf++='\n';
221 *buf='\0';
222}
223
224int main()
225{
226 char buf[5*MAX_DIGITS];
227 print_led(1234567, buf);
228 printf("%s\n",buf);
229
230 return 0;
231}
232
233#ifndef NO_MAIN
234int main(int argc, char **argv)
235{
236
237 int i=0,n;
238 long x;
239 static int d[MAX_DIGITS];
240 char buf[5*MAX_DIGITS];
241
242 if(argc != 2){
243 fprintf(stderr,"led: usage: led integer\n");
244 return 1;
245 }
246
247 /* fetch argument from command line */
248
249 x = atol(argv[1]);
250
251 /* sanity check */
252
253 if(x<0){
254 fprintf(stderr,"led: %d must be non-negative\n",x);
255 return 1;
256 }
257
258 print_led(x,buf);
259 printf("%s\n",buf);
260
261 return 0;
262
263}
264#endif
265
266/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/32_led.expect+4−0
@@ -0,0 +1,4 @@
1 _ _ _ _
2 | _| _| |_| |_ |_ |
3 | |_ _| | _| |_| |
4
tests2/33_ternary_op.c+15−0
@@ -0,0 +1,15 @@
1#include <stdio.h>
2
3int main()
4{
5 int Count;
6
7 for (Count = 0; Count < 10; Count++)
8 {
9 printf("%d\n", (Count < 5) ? (Count*Count) : (Count * 3));
10 }
11
12 return 0;
13}
14
15/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/33_ternary_op.expect+10−0
@@ -0,0 +1,10 @@
10
21
34
49
516
615
718
821
924
1027
tests2/34_array_assignment.c+23−0
@@ -0,0 +1,23 @@
1#include <stdio.h>
2
3int main()
4{
5 int a[4];
6
7 a[0] = 12;
8 a[1] = 23;
9 a[2] = 34;
10 a[3] = 45;
11
12 printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
13
14 int b[4];
15
16 b = a;
17
18 printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]);
19
20 return 0;
21}
22
23/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/34_array_assignment.expect+2−0
@@ -0,0 +1,2 @@
112 23 34 45
212 23 34 45
tests2/35_sizeof.c+14−0
@@ -0,0 +1,14 @@
1#include <stdio.h>
2
3int main()
4{
5 char a;
6 short b;
7
8 printf("%d %d\n", sizeof(char), sizeof(a));
9 printf("%d %d\n", sizeof(short), sizeof(b));
10
11 return 0;
12}
13
14/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/35_sizeof.expect+2−0
@@ -0,0 +1,2 @@
11 1
22 2
tests2/36_array_initialisers.c+21−0
@@ -0,0 +1,21 @@
1#include <stdio.h>
2
3int main()
4{
5 int Count;
6
7 int Array[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753 };
8
9 for (Count = 0; Count < 10; Count++)
10 printf("%d: %d\n", Count, Array[Count]);
11
12 int Array2[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753, };
13
14 for (Count = 0; Count < 10; Count++)
15 printf("%d: %d\n", Count, Array2[Count]);
16
17
18 return 0;
19}
20
21/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/36_array_initialisers.expect+20−0
@@ -0,0 +1,20 @@
10: 12
21: 34
32: 56
43: 78
54: 90
65: 123
76: 456
87: 789
98: 8642
109: 9753
110: 12
121: 34
132: 56
143: 78
154: 90
165: 123
176: 456
187: 789
198: 8642
209: 9753
tests2/37_sprintf.c+17−0
@@ -0,0 +1,17 @@
1#include <stdio.h>
2
3int main()
4{
5 char Buf[100];
6 int Count;
7
8 for (Count = 1; Count <= 20; Count++)
9 {
10 sprintf(Buf, "->%02d<-\n", Count);
11 printf("%s", Buf);
12 }
13
14 return 0;
15}
16
17/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/37_sprintf.expect+20−0
@@ -0,0 +1,20 @@
1->01<-
2->02<-
3->03<-
4->04<-
5->05<-
6->06<-
7->07<-
8->08<-
9->09<-
10->10<-
11->11<-
12->12<-
13->13<-
14->14<-
15->15<-
16->16<-
17->17<-
18->18<-
19->19<-
20->20<-
tests2/38_multiple_array_index.c+32−0
@@ -0,0 +1,32 @@
1#include <stdio.h>
2
3int main()
4{
5 int a[4][4];
6 int b = 0;
7 int x;
8 int y;
9
10 for (x = 0; x < 4; x++)
11 {
12 for (y = 0; y < 4; y++)
13 {
14 b++;
15 a[x][y] = b;
16 }
17 }
18
19 for (x = 0; x < 4; x++)
20 {
21 printf("x=%d: ", x);
22 for (y = 0; y < 4; y++)
23 {
24 printf("%d ", a[x][y]);
25 }
26 printf("\n");
27 }
28
29 return 0;
30}
31
32/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/38_multiple_array_index.expect+4−0
@@ -0,0 +1,4 @@
1x=0: 1 2 3 4
2x=1: 5 6 7 8
3x=2: 9 10 11 12
4x=3: 13 14 15 16
tests2/39_typedef.c+31−0
@@ -0,0 +1,31 @@
1#include <stdio.h>
2
3typedef int MyInt;
4
5struct FunStruct
6{
7 int i;
8 int j;
9};
10
11typedef struct FunStruct MyFunStruct;
12
13typedef MyFunStruct *MoreFunThanEver;
14
15int main()
16{
17 MyInt a = 1;
18 printf("%d\n", a);
19
20 MyFunStruct b;
21 b.i = 12;
22 b.j = 34;
23 printf("%d,%d\n", b.i, b.j);
24
25 MoreFunThanEver c = &b;
26 printf("%d,%d\n", c->i, c->j);
27
28 return 0;
29}
30
31/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/39_typedef.expect+3−0
@@ -0,0 +1,3 @@
11
212,34
312,34
tests2/40_stdio.c+52−0
@@ -0,0 +1,52 @@
1#include <stdio.h>
2
3int main()
4{
5 FILE *f = fopen("fred.txt", "w");
6 fwrite("hello\nhello\n", 1, 12, f);
7 fclose(f);
8
9 char freddy[7];
10 f = fopen("fred.txt", "r");
11 if (fread(freddy, 1, 6, f) != 6)
12 printf("couldn't read fred.txt\n");
13
14 freddy[6] = '\0';
15 fclose(f);
16
17 printf("%s", freddy);
18
19 char InChar;
20 char ShowChar;
21 f = fopen("fred.txt", "r");
22 while ( (InChar = fgetc(f)) != EOF)
23 {
24 ShowChar = InChar;
25 if (ShowChar < ' ')
26 ShowChar = '.';
27
28 printf("ch: %d '%c'\n", InChar, ShowChar);
29 }
30 fclose(f);
31
32 f = fopen("fred.txt", "r");
33 while ( (InChar = getc(f)) != EOF)
34 {
35 ShowChar = InChar;
36 if (ShowChar < ' ')
37 ShowChar = '.';
38
39 printf("ch: %d '%c'\n", InChar, ShowChar);
40 }
41 fclose(f);
42
43 f = fopen("fred.txt", "r");
44 while (fgets(freddy, sizeof(freddy), f) != NULL)
45 printf("x: %s", freddy);
46
47 fclose(f);
48
49 return 0;
50}
51
52/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/40_stdio.expect+27−0
@@ -0,0 +1,27 @@
1hello
2ch: 104 'h'
3ch: 101 'e'
4ch: 108 'l'
5ch: 108 'l'
6ch: 111 'o'
7ch: 10 '.'
8ch: 104 'h'
9ch: 101 'e'
10ch: 108 'l'
11ch: 108 'l'
12ch: 111 'o'
13ch: 10 '.'
14ch: 104 'h'
15ch: 101 'e'
16ch: 108 'l'
17ch: 108 'l'
18ch: 111 'o'
19ch: 10 '.'
20ch: 104 'h'
21ch: 101 'e'
22ch: 108 'l'
23ch: 108 'l'
24ch: 111 'o'
25ch: 10 '.'
26x: hello
27x: hello
tests2/41_hashif.c+85−0
@@ -0,0 +1,85 @@
1#include <stdio.h>
2
3int main()
4{
5 printf("#include test\n");
6
7#if 1
8#if 0
9 printf("a\n");
10#else
11 printf("b\n");
12#endif
13#else
14#if 0
15 printf("c\n");
16#else
17 printf("d\n");
18#endif
19#endif
20
21#if 0
22#if 1
23 printf("e\n");
24#else
25 printf("f\n");
26#endif
27#else
28#if 1
29 printf("g\n");
30#else
31 printf("h\n");
32#endif
33#endif
34
35#define DEF
36
37#ifdef DEF
38#ifdef DEF
39 printf("i\n");
40#else
41 printf("j\n");
42#endif
43#else
44#ifdef DEF
45 printf("k\n");
46#else
47 printf("l\n");
48#endif
49#endif
50
51#ifndef DEF
52#ifndef DEF
53 printf("m\n");
54#else
55 printf("n\n");
56#endif
57#else
58#ifndef DEF
59 printf("o\n");
60#else
61 printf("p\n");
62#endif
63#endif
64
65#define ONE 1
66#define ZERO 0
67
68#if ONE
69#if ZERO
70 printf("q\n");
71#else
72 printf("r\n");
73#endif
74#else
75#if ZERO
76 printf("s\n");
77#else
78 printf("t\n");
79#endif
80#endif
81
82 return 0;
83}
84
85/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/41_hashif.expect+6−0
@@ -0,0 +1,6 @@
1#include test
2b
3g
4i
5p
6r
tests2/42_function_pointer.c+18−0
@@ -0,0 +1,18 @@
1#include <stdio.h>
2
3int fred(int p)
4{
5 printf("yo %d\n", p);
6 return 42;
7}
8
9int (*f)(int) = &fred;
10
11int main()
12{
13 printf("%d\n", (*f)(24));
14
15 return 0;
16}
17
18/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/42_function_pointer.expect+2−0
@@ -0,0 +1,2 @@
1yo 24
242
tests2/43_void_param.c+15−0
@@ -0,0 +1,15 @@
1#include <stdio.h>
2
3void fred(void)
4{
5 printf("yo\n");
6}
7
8int main()
9{
10 fred();
11
12 return 0;
13}
14
15/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/43_void_param.expect+1−0
@@ -0,0 +1 @@
1yo
tests2/44_scoped_declarations.c+17−0
@@ -0,0 +1,17 @@
1#include <stdio.h>
2
3int main()
4{
5 int a;
6
7 for (a = 0; a < 2; a++)
8 {
9 int b = a;
10 }
11
12 printf("it's all good\n");
13
14 return 0;
15}
16
17/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/44_scoped_declarations.expect+1−0
@@ -0,0 +1 @@
1it's all good
tests2/45_empty_for.c+18−0
@@ -0,0 +1,18 @@
1#include <stdio.h>
2
3int main()
4{
5 int Count = 0;
6
7 for (;;)
8 {
9 Count++;
10 printf("%d\n", Count);
11 if (Count >= 10)
12 break;
13 }
14
15 return 0;
16}
17
18/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/45_empty_for.expect+10−0
@@ -0,0 +1,10 @@
11
22
33
44
55
66
77
88
99
1010
tests2/46_grep.c+564−0
@@ -0,0 +1,564 @@
1/*
2 * The information in this document is subject to change
3 * without notice and should not be construed as a commitment
4 * by Digital Equipment Corporation or by DECUS.
5 *
6 * Neither Digital Equipment Corporation, DECUS, nor the authors
7 * assume any responsibility for the use or reliability of this
8 * document or the described software.
9 *
10 * Copyright (C) 1980, DECUS
11 *
12 * General permission to copy or modify, but not for profit, is
13 * hereby granted, provided that the above copyright notice is
14 * included and reference made to the fact that reproduction
15 * privileges were granted by DECUS.
16 */
17#include <stdio.h>
18
19/*
20 * grep
21 *
22 * Runs on the Decus compiler or on vms, On vms, define as:
23 * grep :== "$disk:[account]grep" (native)
24 * grep :== "$disk:[account]grep grep" (Decus)
25 * See below for more information.
26 */
27
28#if 0
29char *documentation[] = {
30 "grep searches a file for a given pattern. Execute by",
31 " grep [flags] regular_expression file_list\n",
32 "Flags are single characters preceeded by '-':",
33 " -c Only a count of matching lines is printed",
34 " -f Print file name for matching lines switch, see below",
35 " -n Each line is preceeded by its line number",
36 " -v Only print non-matching lines\n",
37 "The file_list is a list of files (wildcards are acceptable on RSX modes).",
38 "\nThe file name is normally printed if there is a file given.",
39 "The -f flag reverses this action (print name no file, not if more).\n",
40 0 };
41
42char *patdoc[] = {
43 "The regular_expression defines the pattern to search for. Upper- and",
44 "lower-case are always ignored. Blank lines never match. The expression",
45 "should be quoted to prevent file-name translation.",
46 "x An ordinary character (not mentioned below) matches that character.",
47 "'\\' The backslash quotes any character. \"\\$\" matches a dollar-sign.",
48 "'^' A circumflex at the beginning of an expression matches the",
49 " beginning of a line.",
50 "'$' A dollar-sign at the end of an expression matches the end of a line.",
51 "'.' A period matches any character except \"new-line\".",
52 "':a' A colon matches a class of characters described by the following",
53 "':d' character. \":a\" matches any alphabetic, \":d\" matches digits,",
54 "':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and",
55 "': ' other control characters, such as new-line.",
56 "'*' An expression followed by an asterisk matches zero or more",
57 " occurrances of that expression: \"fo*\" matches \"f\", \"fo\"",
58 " \"foo\", etc.",
59 "'+' An expression followed by a plus sign matches one or more",
60 " occurrances of that expression: \"fo+\" matches \"fo\", etc.",
61 "'-' An expression followed by a minus sign optionally matches",
62 " the expression.",
63 "'[]' A string enclosed in square brackets matches any character in",
64 " that string, but no others. If the first character in the",
65 " string is a circumflex, the expression matches any character",
66 " except \"new-line\" and the characters in the string. For",
67 " example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"",
68 " matches \"abc\" but not \"axb\". A range of characters may be",
69 " specified by two characters separated by \"-\". Note that,",
70 " [a-z] matches alphabetics, while [z-a] never matches.",
71 "The concatenation of regular expressions is a regular expression.",
72 0};
73#endif
74
75#define LMAX 512
76#define PMAX 256
77
78#define CHAR 1
79#define BOL 2
80#define EOL 3
81#define ANY 4
82#define CLASS 5
83#define NCLASS 6
84#define STAR 7
85#define PLUS 8
86#define MINUS 9
87#define ALPHA 10
88#define DIGIT 11
89#define NALPHA 12
90#define PUNCT 13
91#define RANGE 14
92#define ENDPAT 15
93
94int cflag=0, fflag=0, nflag=0, vflag=0, nfile=0, debug=0;
95
96char *pp, lbuf[LMAX], pbuf[PMAX];
97
98char *cclass();
99char *pmatch();
100
101
102/*** Display a file name *******************************/
103void file(char *s)
104{
105 printf("File %s:\n", s);
106}
107
108/*** Report unopenable file ****************************/
109void cant(char *s)
110{
111 fprintf(stderr, "%s: cannot open\n", s);
112}
113
114/*** Give good help ************************************/
115void help(char **hp)
116{
117 char **dp;
118
119 for (dp = hp; *dp; ++dp)
120 printf("%s\n", *dp);
121}
122
123/*** Display usage summary *****************************/
124void usage(char *s)
125{
126 fprintf(stderr, "?GREP-E-%s\n", s);
127 fprintf(stderr,
128 "Usage: grep [-cfnv] pattern [file ...]. grep ? for help\n");
129 exit(1);
130}
131
132/*** Compile the pattern into global pbuf[] ************/
133void compile(char *source)
134{
135 char *s; /* Source string pointer */
136 char *lp; /* Last pattern pointer */
137 int c; /* Current character */
138 int o; /* Temp */
139 char *spp; /* Save beginning of pattern */
140
141 s = source;
142 if (debug)
143 printf("Pattern = \"%s\"\n", s);
144 pp = pbuf;
145 while (c = *s++) {
146 /*
147 * STAR, PLUS and MINUS are special.
148 */
149 if (c == '*' || c == '+' || c == '-') {
150 if (pp == pbuf ||
151 (o=pp[-1]) == BOL ||
152 o == EOL ||
153 o == STAR ||
154 o == PLUS ||
155 o == MINUS)
156 badpat("Illegal occurrance op.", source, s);
157 store(ENDPAT);
158 store(ENDPAT);
159 spp = pp; /* Save pattern end */
160 while (--pp > lp) /* Move pattern down */
161 *pp = pp[-1]; /* one byte */
162 *pp = (c == '*') ? STAR :
163 (c == '-') ? MINUS : PLUS;
164 pp = spp; /* Restore pattern end */
165 continue;
166 }
167 /*
168 * All the rest.
169 */
170 lp = pp; /* Remember start */
171 switch(c) {
172
173 case '^':
174 store(BOL);
175 break;
176
177 case '$':
178 store(EOL);
179 break;
180
181 case '.':
182 store(ANY);
183 break;
184
185 case '[':
186 s = cclass(source, s);
187 break;
188
189 case ':':
190 if (*s) {
191 switch(tolower(c = *s++)) {
192
193 case 'a':
194 case 'A':
195 store(ALPHA);
196 break;
197
198 case 'd':
199 case 'D':
200 store(DIGIT);
201 break;
202
203 case 'n':
204 case 'N':
205 store(NALPHA);
206 break;
207
208 case ' ':
209 store(PUNCT);
210 break;
211
212 default:
213 badpat("Unknown : type", source, s);
214
215 }
216 break;
217 }
218 else badpat("No : type", source, s);
219
220 case '\\':
221 if (*s)
222 c = *s++;
223
224 default:
225 store(CHAR);
226 store(tolower(c));
227 }
228 }
229 store(ENDPAT);
230 store(0); /* Terminate string */
231 if (debug) {
232 for (lp = pbuf; lp < pp;) {
233 if ((c = (*lp++ & 0377)) < ' ')
234 printf("\\%o ", c);
235 else printf("%c ", c);
236 }
237 printf("\n");
238 }
239}
240
241/*** Compile a class (within []) ***********************/
242char *cclass(char *source, char *src)
243 /* char *source; // Pattern start -- for error msg. */
244 /* char *src; // Class start */
245{
246 char *s; /* Source pointer */
247 char *cp; /* Pattern start */
248 int c; /* Current character */
249 int o; /* Temp */
250
251 s = src;
252 o = CLASS;
253 if (*s == '^') {
254 ++s;
255 o = NCLASS;
256 }
257 store(o);
258 cp = pp;
259 store(0); /* Byte count */
260 while ((c = *s++) && c!=']') {
261 if (c == '\\') { /* Store quoted char */
262 if ((c = *s++) == '\0') /* Gotta get something */
263 badpat("Class terminates badly", source, s);
264 else store(tolower(c));
265 }
266 else if (c == '-' &&
267 (pp - cp) > 1 && *s != ']' && *s != '\0') {
268 c = pp[-1]; /* Range start */
269 pp[-1] = RANGE; /* Range signal */
270 store(c); /* Re-store start */
271 c = *s++; /* Get end char and*/
272 store(tolower(c)); /* Store it */
273 }
274 else {
275 store(tolower(c)); /* Store normal char */
276 }
277 }
278 if (c != ']')
279 badpat("Unterminated class", source, s);
280 if ((c = (pp - cp)) >= 256)
281 badpat("Class too large", source, s);
282 if (c == 0)
283 badpat("Empty class", source, s);
284 *cp = c;
285 return(s);
286}
287
288/*** Store an entry in the pattern buffer **************/
289void store(int op)
290{
291 if (pp >= &pbuf[PMAX])
292 error("Pattern too complex\n");
293 *pp++ = op;
294}
295
296/*** Report a bad pattern specification ****************/
297void badpat(char *message, char *source, char *stop)
298 /* char *message; // Error message */
299 /* char *source; // Pattern start */
300 /* char *stop; // Pattern end */
301{
302 fprintf(stderr, "-GREP-E-%s, pattern is\"%s\"\n", message, source);
303 fprintf(stderr, "-GREP-E-Stopped at byte %d, '%c'\n",
304 stop-source, stop[-1]);
305 error("?GREP-E-Bad pattern\n");
306}
307
308/*** Scan the file for the pattern in pbuf[] ***********/
309void grep(FILE *fp, char *fn)
310 /* FILE *fp; // File to process */
311 /* char *fn; // File name (for -f option) */
312{
313 int lno, count, m;
314
315 lno = 0;
316 count = 0;
317 while (fgets(lbuf, LMAX, fp)) {
318 ++lno;
319 m = match();
320 if ((m && !vflag) || (!m && vflag)) {
321 ++count;
322 if (!cflag) {
323 if (fflag && fn) {
324 file(fn);
325 fn = 0;
326 }
327 if (nflag)
328 printf("%d\t", lno);
329 printf("%s\n", lbuf);
330 }
331 }
332 }
333 if (cflag) {
334 if (fflag && fn)
335 file(fn);
336 printf("%d\n", count);
337 }
338}
339
340/*** Match line (lbuf) with pattern (pbuf) return 1 if match ***/
341void match()
342{
343 char *l; /* Line pointer */
344
345 for (l = lbuf; *l; ++l) {
346 if (pmatch(l, pbuf))
347 return(1);
348 }
349 return(0);
350}
351
352/*** Match partial line with pattern *******************/
353char *pmatch(char *line, char *pattern)
354 /* char *line; // (partial) line to match */
355 /* char *pattern; // (partial) pattern to match */
356{
357 char *l; /* Current line pointer */
358 char *p; /* Current pattern pointer */
359 char c; /* Current character */
360 char *e; /* End for STAR and PLUS match */
361 int op; /* Pattern operation */
362 int n; /* Class counter */
363 char *are; /* Start of STAR match */
364
365 l = line;
366 if (debug > 1)
367 printf("pmatch(\"%s\")\n", line);
368 p = pattern;
369 while ((op = *p++) != ENDPAT) {
370 if (debug > 1)
371 printf("byte[%d] = 0%o, '%c', op = 0%o\n",
372 l-line, *l, *l, op);
373 switch(op) {
374
375 case CHAR:
376 if (tolower(*l++) != *p++)
377 return(0);
378 break;
379
380 case BOL:
381 if (l != lbuf)
382 return(0);
383 break;
384
385 case EOL:
386 if (*l != '\0')
387 return(0);
388 break;
389
390 case ANY:
391 if (*l++ == '\0')
392 return(0);
393 break;
394
395 case DIGIT:
396 if ((c = *l++) < '0' || (c > '9'))
397 return(0);
398 break;
399
400 case ALPHA:
401 c = tolower(*l++);
402 if (c < 'a' || c > 'z')
403 return(0);
404 break;
405
406 case NALPHA:
407 c = tolower(*l++);
408 if (c >= 'a' && c <= 'z')
409 break;
410 else if (c < '0' || c > '9')
411 return(0);
412 break;
413
414 case PUNCT:
415 c = *l++;
416 if (c == 0 || c > ' ')
417 return(0);
418 break;
419
420 case CLASS:
421 case NCLASS:
422 c = tolower(*l++);
423 n = *p++ & 0377;
424 do {
425 if (*p == RANGE) {
426 p += 3;
427 n -= 2;
428 if (c >= p[-2] && c <= p[-1])
429 break;
430 }
431 else if (c == *p++)
432 break;
433 } while (--n > 1);
434 if ((op == CLASS) == (n <= 1))
435 return(0);
436 if (op == CLASS)
437 p += n - 2;
438 break;
439
440 case MINUS:
441 e = pmatch(l, p); /* Look for a match */
442 while (*p++ != ENDPAT); /* Skip over pattern */
443 if (e) /* Got a match? */
444 l = e; /* Yes, update string */
445 break; /* Always succeeds */
446
447 case PLUS: /* One or more ... */
448 if ((l = pmatch(l, p)) == 0)
449 return(0); /* Gotta have a match */
450 case STAR: /* Zero or more ... */
451 are = l; /* Remember line start */
452 while (*l && (e = pmatch(l, p)))
453 l = e; /* Get longest match */
454 while (*p++ != ENDPAT); /* Skip over pattern */
455 while (l >= are) { /* Try to match rest */
456 if (e = pmatch(l, p))
457 return(e);
458 --l; /* Nope, try earlier */
459 }
460 return(0); /* Nothing else worked */
461
462 default:
463 printf("Bad op code %d\n", op);
464 error("Cannot happen -- match\n");
465 }
466 }
467 return(l);
468}
469
470/*** Report an error ***********************************/
471void error(char *s)
472{
473 fprintf(stderr, "%s", s);
474 exit(1);
475}
476
477/*** Main program - parse arguments & grep *************/
478int main(int argc, char **argv)
479{
480 char *p;
481 int c, i;
482 int gotpattern;
483
484 FILE *f;
485
486 if (argc <= 1)
487 usage("No arguments");
488 if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
489 help(documentation);
490 help(patdoc);
491 return 0;
492 }
493 nfile = argc-1;
494 gotpattern = 0;
495 for (i=1; i < argc; ++i) {
496 p = argv[i];
497 if (*p == '-') {
498 ++p;
499 while (c = *p++) {
500 switch(tolower(c)) {
501
502 case '?':
503 help(documentation);
504 break;
505
506 case 'C':
507 case 'c':
508 ++cflag;
509 break;
510
511 case 'D':
512 case 'd':
513 ++debug;
514 break;
515
516 case 'F':
517 case 'f':
518 ++fflag;
519 break;
520
521 case 'n':
522 case 'N':
523 ++nflag;
524 break;
525
526 case 'v':
527 case 'V':
528 ++vflag;
529 break;
530
531 default:
532 usage("Unknown flag");
533 }
534 }
535 argv[i] = 0;
536 --nfile;
537 } else if (!gotpattern) {
538 compile(p);
539 argv[i] = 0;
540 ++gotpattern;
541 --nfile;
542 }
543 }
544 if (!gotpattern)
545 usage("No pattern");
546 if (nfile == 0)
547 grep(stdin, 0);
548 else {
549 fflag = fflag ^ (nfile > 0);
550 for (i=1; i < argc; ++i) {
551 if (p = argv[i]) {
552 if ((f=fopen(p, "r")) == NULL)
553 cant(p);
554 else {
555 grep(f, p);
556 fclose(f);
557 }
558 }
559 }
560 }
561 return 0;
562}
563
564/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/47_switch_return.c+24−0
@@ -0,0 +1,24 @@
1#include <stdio.h>
2
3void fred(int x)
4{
5 switch (x)
6 {
7 case 1: printf("1\n"); return;
8 case 2: printf("2\n"); break;
9 case 3: printf("3\n"); return;
10 }
11
12 printf("out\n");
13}
14
15int main()
16{
17 fred(1);
18 fred(2);
19 fred(3);
20
21 return 0;
22}
23
24/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/47_switch_return.expect+4−0
@@ -0,0 +1,4 @@
11
22
3out
43
tests2/48_nested_break.c+26−0
@@ -0,0 +1,26 @@
1#include <stdio.h>
2
3int main()
4{
5 int a;
6 char b;
7
8 a = 0;
9 while (a < 2)
10 {
11 printf("%d", a++);
12 break;
13
14 b = 'A';
15 while (b < 'C')
16 {
17 printf("%c", b++);
18 }
19 printf("e");
20 }
21 printf("\n");
22
23 return 0;
24}
25
26/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/48_nested_break.expect+1−0
@@ -0,0 +1 @@
10
tests2/49_bracket_evaluation.c+23−0
@@ -0,0 +1,23 @@
1#include <stdio.h>
2
3struct point
4{
5 double x;
6 double y;
7};
8
9struct point point_array[100];
10
11int main()
12{
13 int my_point = 10;
14
15 point_array[my_point].x = 12.34;
16 point_array[my_point].y = 56.78;
17
18 printf("%f, %f\n", point_array[my_point].x, point_array[my_point].y);
19
20 return 0;
21}
22
23/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/49_bracket_evaluation.expect+1−0
@@ -0,0 +1 @@
112.340000, 56.780000
tests2/50_logical_second_arg.c+29−0
@@ -0,0 +1,29 @@
1#include <stdio.h>
2
3int fred()
4{
5 printf("fred\n");
6 return 0;
7}
8
9int joe()
10{
11 printf("joe\n");
12 return 1;
13}
14
15int main()
16{
17 printf("%d\n", fred() && joe());
18 printf("%d\n", fred() || joe());
19 printf("%d\n", joe() && fred());
20 printf("%d\n", joe() || fred());
21 printf("%d\n", fred() && (1 + joe()));
22 printf("%d\n", fred() || (0 + joe()));
23 printf("%d\n", joe() && (0 + fred()));
24 printf("%d\n", joe() || (1 + fred()));
25
26 return 0;
27}
28
29/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/50_logical_second_arg.expect+20−0
@@ -0,0 +1,20 @@
1fred
20
3fred
4joe
51
6joe
7fred
80
9joe
101
11fred
120
13fred
14joe
151
16joe
17fred
180
19joe
201
tests2/51_static.c+30−0
@@ -0,0 +1,30 @@
1#include <stdio.h>
2
3static int fred = 1234;
4static int joe;
5
6void henry()
7{
8 static int fred = 4567;
9
10 printf("%d\n", fred);
11 fred++;
12}
13
14int main()
15{
16 printf("%d\n", fred);
17 henry();
18 henry();
19 henry();
20 henry();
21 printf("%d\n", fred);
22 fred = 8901;
23 joe = 2345;
24 printf("%d\n", fred);
25 printf("%d\n", joe);
26
27 return 0;
28}
29
30/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/51_static.expect+8−0
@@ -0,0 +1,8 @@
11234
24567
34568
44569
54570
61234
78901
82345
tests2/52_unnamed_enum.c+27−0
@@ -0,0 +1,27 @@
1#include <stdio.h>
2
3enum fred { a, b, c };
4
5int main()
6{
7 printf("a=%d\n", a);
8 printf("b=%d\n", b);
9 printf("c=%d\n", c);
10
11 enum fred d;
12
13 typedef enum { e, f, g } h;
14 typedef enum { i, j, k } m;
15
16 printf("e=%d\n", e);
17 printf("f=%d\n", f);
18 printf("g=%d\n", g);
19
20 printf("i=%d\n", i);
21 printf("j=%d\n", j);
22 printf("k=%d\n", k);
23
24 return 0;
25}
26
27/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/52_unnamed_enum.expect+9−0
@@ -0,0 +1,9 @@
1a=0
2b=1
3c=2
4e=0
5f=1
6g=2
7i=0
8j=1
9k=2
tests2/54_goto.c+56−0
@@ -0,0 +1,56 @@
1#include <stdio.h>
2
3void fred()
4{
5 printf("In fred()\n");
6 goto done;
7 printf("In middle\n");
8done:
9 printf("At end\n");
10}
11
12void joe()
13{
14 int b = 5678;
15
16 printf("In joe()\n");
17
18 {
19 int c = 1234;
20 printf("c = %d\n", c);
21 goto outer;
22 printf("uh-oh\n");
23 }
24
25outer:
26
27 printf("done\n");
28}
29
30void henry()
31{
32 int a;
33
34 printf("In henry()\n");
35 goto inner;
36
37 {
38 int b;
39inner:
40 b = 1234;
41 printf("b = %d\n", b);
42 }
43
44 printf("done\n");
45}
46
47int main()
48{
49 fred();
50 joe();
51 henry();
52
53 return 0;
54}
55
56/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
tests2/54_goto.expect+8−0
@@ -0,0 +1,8 @@
1In fred()
2At end
3In joe()
4c = 1234
5done
6In henry()
7b = 1234
8done
tests2/LICENSE+37−0
@@ -0,0 +1,37 @@
1The tests in this directory are either directly copied from the picoc project or
2are subsequently modified and added to for the purpose of TinyCC project. All
3these modifications are licensed under the same terms as TinyCC as specified in
4the file COPYING.
5
6=== picoc license ===
7
8Copyright (c) 2009-2011, Zik Saleeba
9All rights reserved.
10
11Redistribution and use in source and binary forms, with or without
12modification, are permitted provided that the following conditions are
13met:
14
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in
20 the documentation and/or other materials provided with the
21 distribution.
22
23 * Neither the name of the Zik Saleeba nor the names of its
24 contributors may be used to endorse or promote products derived
25 from this software without specific prior written permission.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
tests2/Makefile+90−0
@@ -0,0 +1,90 @@
1TOP = ..
2include $(TOP)/Makefile
3
4ifeq ($(TARGETOS),Darwin)
5 CFLAGS+=-Wl,-flat_namespace,-undefined,warning
6 TCCFLAGS=-D_ANSI_SOURCE
7 export MACOSX_DEPLOYMENT_TARGET:=10.2
8endif
9
10ifdef CONFIG_WIN32
11 TCCFLAGS=-I $(TOP)/win32/include -L$(TOP)
12endif
13
14TESTS= 00_assignment.test \
15 01_comment.test \
16 02_printf.test \
17 03_struct.test \
18 04_for.test \
19 05_array.test \
20 06_case.test \
21 07_function.test \
22 08_while.test \
23 09_do_while.test \
24 10_pointer.test \
25 11_precedence.test \
26 12_hashdefine.test \
27 13_integer_literals.test \
28 14_if.test \
29 15_recursion.test \
30 16_nesting.test \
31 17_enum.test \
32 18_include.test \
33 19_pointer_arithmetic.test \
34 20_pointer_comparison.test \
35 21_char_array.test \
36 22_floating_point.test \
37 23_type_coercion.test \
38 24_math_library.test \
39 25_quicksort.test \
40 26_character_constants.test \
41 27_sizeof.test \
42 28_strings.test \
43 29_array_address.test \
44 31_args.test \
45 32_led.test \
46 33_ternary_op.test \
47 35_sizeof.test \
48 36_array_initialisers.test \
49 37_sprintf.test \
50 38_multiple_array_index.test \
51 39_typedef.test \
52 40_stdio.test \
53 41_hashif.test \
54 42_function_pointer.test \
55 43_void_param.test \
56 44_scoped_declarations.test \
57 45_empty_for.test \
58 47_switch_return.test \
59 48_nested_break.test \
60 49_bracket_evaluation.test \
61 50_logical_second_arg.test \
62 51_static.test \
63 52_unnamed_enum.test \
64 54_goto.test
65
66 # 30_hanoi.test \ # seg fault in the code, gcc as well
67 # 34_array_assignment.test \ # array assignment is not in C standard
68 # 46_grep.test \ # does not compile even with gcc
69
70%.test: %.expect %.c
71 @echo Test: $*...
72 -@if [ "x`echo $* | grep args`" != "x" ]; \
73 then \
74 ../tcc -B.. $(TCCFLAGS) -run $*.c - arg1 arg2 arg3 arg4 2>&1 >$*.output; \
75 else \
76 ../tcc -B.. $(TCCFLAGS) -run $*.c 2>&1 >$*.output; \
77 fi
78 @if diff -bu $*.expect $*.output ; \
79 then \
80 rm -f $*.output \
81 : \
82 else \
83 echo "ERROR: test $*"; \
84 fi
85
86all: test
87
88test: $(TESTS)
89
90# vim: set expandtab ts=4 sw=4 sts=4 tw=80 :

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t