From 42c1b6ba380d9d1ab85f0cc21db07e71667cc682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milutin=20Jovanovi=C4=87?= Date: Mon, 18 Jun 2012 13:27:32 -0400 Subject: [PATCH] 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. --- Makefile | 2 + tests2/00_assignment.c | 18 + tests2/00_assignment.expect | 3 + tests2/01_comment.c | 14 + tests2/01_comment.expect | 5 + tests2/02_printf.c | 18 + tests2/02_printf.expect | 15 + tests2/03_struct.c | 31 ++ tests2/03_struct.expect | 6 + tests2/04_for.c | 15 + tests2/04_for.expect | 10 + tests2/05_array.c | 21 + tests2/05_array.expect | 10 + tests2/06_case.c | 29 ++ tests2/06_case.expect | 8 + tests2/07_function.c | 30 ++ tests2/07_function.expect | 4 + tests2/08_while.c | 24 ++ tests2/08_while.expect | 11 + tests2/09_do_while.c | 24 ++ tests2/09_do_while.expect | 11 + tests2/10_pointer.c | 40 ++ tests2/10_pointer.expect | 8 + tests2/11_precedence.c | 40 ++ tests2/11_precedence.expect | 15 + tests2/12_hashdefine.c | 14 + tests2/12_hashdefine.expect | 2 + tests2/13_integer_literals.c | 20 + tests2/13_integer_literals.expect | 5 + tests2/14_if.c | 21 + tests2/14_if.expect | 2 + tests2/15_recursion.c | 21 + tests2/15_recursion.expect | 10 + tests2/16_nesting.c | 21 + tests2/16_nesting.expect | 18 + tests2/17_enum.c | 29 ++ tests2/17_enum.expect | 3 + tests2/18_include.c | 12 + tests2/18_include.expect | 3 + tests2/18_include.h | 1 + tests2/19_pointer_arithmetic.c | 28 ++ tests2/19_pointer_arithmetic.expect | 3 + tests2/20_pointer_comparison.c | 24 ++ tests2/20_pointer_comparison.expect | 6 + tests2/21_char_array.c | 33 ++ tests2/21_char_array.expect | 7 + tests2/22_floating_point.c | 50 +++ tests2/22_floating_point.expect | 16 + tests2/23_type_coercion.c | 54 +++ tests2/23_type_coercion.expect | 12 + tests2/24_math_library.c | 28 ++ tests2/24_math_library.expect | 18 + tests2/25_quicksort.c | 83 ++++ tests2/25_quicksort.expect | 2 + tests2/26_character_constants.c | 17 + tests2/26_character_constants.expect | 8 + tests2/27_sizeof.c | 16 + tests2/27_sizeof.expect | 3 + tests2/28_strings.c | 46 +++ tests2/28_strings.expect | 19 + tests2/29_array_address.c | 13 + tests2/29_array_address.expect | 1 + tests2/30_hanoi.c | 122 ++++++ tests2/30_hanoi.expect | 71 ++++ tests2/31_args.c | 14 + tests2/31_args.expect | 7 + tests2/32_led.c | 266 ++++++++++++ tests2/32_led.expect | 4 + tests2/33_ternary_op.c | 15 + tests2/33_ternary_op.expect | 10 + tests2/34_array_assignment.c | 23 ++ tests2/34_array_assignment.expect | 2 + tests2/35_sizeof.c | 14 + tests2/35_sizeof.expect | 2 + tests2/36_array_initialisers.c | 21 + tests2/36_array_initialisers.expect | 20 + tests2/37_sprintf.c | 17 + tests2/37_sprintf.expect | 20 + tests2/38_multiple_array_index.c | 32 ++ tests2/38_multiple_array_index.expect | 4 + tests2/39_typedef.c | 31 ++ tests2/39_typedef.expect | 3 + tests2/40_stdio.c | 52 +++ tests2/40_stdio.expect | 27 ++ tests2/41_hashif.c | 85 ++++ tests2/41_hashif.expect | 6 + tests2/42_function_pointer.c | 18 + tests2/42_function_pointer.expect | 2 + tests2/43_void_param.c | 15 + tests2/43_void_param.expect | 1 + tests2/44_scoped_declarations.c | 17 + tests2/44_scoped_declarations.expect | 1 + tests2/45_empty_for.c | 18 + tests2/45_empty_for.expect | 10 + tests2/46_grep.c | 564 ++++++++++++++++++++++++++ tests2/47_switch_return.c | 24 ++ tests2/47_switch_return.expect | 4 + tests2/48_nested_break.c | 26 ++ tests2/48_nested_break.expect | 1 + tests2/49_bracket_evaluation.c | 23 ++ tests2/49_bracket_evaluation.expect | 1 + tests2/50_logical_second_arg.c | 29 ++ tests2/50_logical_second_arg.expect | 20 + tests2/51_static.c | 30 ++ tests2/51_static.expect | 8 + tests2/52_unnamed_enum.c | 27 ++ tests2/52_unnamed_enum.expect | 9 + tests2/54_goto.c | 56 +++ tests2/54_goto.expect | 8 + tests2/LICENSE | 37 ++ tests2/Makefile | 90 ++++ 111 files changed, 2988 insertions(+) create mode 100644 tests2/00_assignment.c create mode 100644 tests2/00_assignment.expect create mode 100644 tests2/01_comment.c create mode 100644 tests2/01_comment.expect create mode 100644 tests2/02_printf.c create mode 100644 tests2/02_printf.expect create mode 100644 tests2/03_struct.c create mode 100644 tests2/03_struct.expect create mode 100644 tests2/04_for.c create mode 100644 tests2/04_for.expect create mode 100644 tests2/05_array.c create mode 100644 tests2/05_array.expect create mode 100644 tests2/06_case.c create mode 100644 tests2/06_case.expect create mode 100644 tests2/07_function.c create mode 100644 tests2/07_function.expect create mode 100644 tests2/08_while.c create mode 100644 tests2/08_while.expect create mode 100644 tests2/09_do_while.c create mode 100644 tests2/09_do_while.expect create mode 100644 tests2/10_pointer.c create mode 100644 tests2/10_pointer.expect create mode 100644 tests2/11_precedence.c create mode 100644 tests2/11_precedence.expect create mode 100644 tests2/12_hashdefine.c create mode 100644 tests2/12_hashdefine.expect create mode 100644 tests2/13_integer_literals.c create mode 100644 tests2/13_integer_literals.expect create mode 100644 tests2/14_if.c create mode 100644 tests2/14_if.expect create mode 100644 tests2/15_recursion.c create mode 100644 tests2/15_recursion.expect create mode 100644 tests2/16_nesting.c create mode 100644 tests2/16_nesting.expect create mode 100644 tests2/17_enum.c create mode 100644 tests2/17_enum.expect create mode 100644 tests2/18_include.c create mode 100644 tests2/18_include.expect create mode 100644 tests2/18_include.h create mode 100644 tests2/19_pointer_arithmetic.c create mode 100644 tests2/19_pointer_arithmetic.expect create mode 100644 tests2/20_pointer_comparison.c create mode 100644 tests2/20_pointer_comparison.expect create mode 100644 tests2/21_char_array.c create mode 100644 tests2/21_char_array.expect create mode 100644 tests2/22_floating_point.c create mode 100644 tests2/22_floating_point.expect create mode 100644 tests2/23_type_coercion.c create mode 100644 tests2/23_type_coercion.expect create mode 100644 tests2/24_math_library.c create mode 100644 tests2/24_math_library.expect create mode 100644 tests2/25_quicksort.c create mode 100644 tests2/25_quicksort.expect create mode 100644 tests2/26_character_constants.c create mode 100644 tests2/26_character_constants.expect create mode 100644 tests2/27_sizeof.c create mode 100644 tests2/27_sizeof.expect create mode 100644 tests2/28_strings.c create mode 100644 tests2/28_strings.expect create mode 100644 tests2/29_array_address.c create mode 100644 tests2/29_array_address.expect create mode 100644 tests2/30_hanoi.c create mode 100644 tests2/30_hanoi.expect create mode 100644 tests2/31_args.c create mode 100644 tests2/31_args.expect create mode 100644 tests2/32_led.c create mode 100644 tests2/32_led.expect create mode 100644 tests2/33_ternary_op.c create mode 100644 tests2/33_ternary_op.expect create mode 100644 tests2/34_array_assignment.c create mode 100644 tests2/34_array_assignment.expect create mode 100644 tests2/35_sizeof.c create mode 100644 tests2/35_sizeof.expect create mode 100644 tests2/36_array_initialisers.c create mode 100644 tests2/36_array_initialisers.expect create mode 100644 tests2/37_sprintf.c create mode 100644 tests2/37_sprintf.expect create mode 100644 tests2/38_multiple_array_index.c create mode 100644 tests2/38_multiple_array_index.expect create mode 100644 tests2/39_typedef.c create mode 100644 tests2/39_typedef.expect create mode 100644 tests2/40_stdio.c create mode 100644 tests2/40_stdio.expect create mode 100644 tests2/41_hashif.c create mode 100644 tests2/41_hashif.expect create mode 100644 tests2/42_function_pointer.c create mode 100644 tests2/42_function_pointer.expect create mode 100644 tests2/43_void_param.c create mode 100644 tests2/43_void_param.expect create mode 100644 tests2/44_scoped_declarations.c create mode 100644 tests2/44_scoped_declarations.expect create mode 100644 tests2/45_empty_for.c create mode 100644 tests2/45_empty_for.expect create mode 100644 tests2/46_grep.c create mode 100644 tests2/47_switch_return.c create mode 100644 tests2/47_switch_return.expect create mode 100644 tests2/48_nested_break.c create mode 100644 tests2/48_nested_break.expect create mode 100644 tests2/49_bracket_evaluation.c create mode 100644 tests2/49_bracket_evaluation.expect create mode 100644 tests2/50_logical_second_arg.c create mode 100644 tests2/50_logical_second_arg.expect create mode 100644 tests2/51_static.c create mode 100644 tests2/51_static.expect create mode 100644 tests2/52_unnamed_enum.c create mode 100644 tests2/52_unnamed_enum.expect create mode 100644 tests2/54_goto.c create mode 100644 tests2/54_goto.expect create mode 100644 tests2/LICENSE create mode 100644 tests2/Makefile diff --git a/Makefile b/Makefile index ba94577a..cdaad28b 100644 --- a/Makefile +++ b/Makefile @@ -358,10 +358,12 @@ tar: export LIBTCC1 %est: + $(MAKE) -C tests2 $@ $(MAKE) -C tests $@ clean: rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.exe libtcc_test$(EXESUF) + $(MAKE) -C tests2 $@ $(MAKE) -C tests $@ ifneq ($(LIBTCC1),) $(MAKE) -C lib $@ diff --git a/tests2/00_assignment.c b/tests2/00_assignment.c new file mode 100644 index 00000000..c96109fd --- /dev/null +++ b/tests2/00_assignment.c @@ -0,0 +1,18 @@ +#include + +int main() +{ + int a; + a = 42; + printf("%d\n", a); + + int b = 64; + printf("%d\n", b); + + int c = 12, d = 34; + printf("%d, %d\n", c, d); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/00_assignment.expect b/tests2/00_assignment.expect new file mode 100644 index 00000000..d4407f3f --- /dev/null +++ b/tests2/00_assignment.expect @@ -0,0 +1,3 @@ +42 +64 +12, 34 diff --git a/tests2/01_comment.c b/tests2/01_comment.c new file mode 100644 index 00000000..a2e6bc63 --- /dev/null +++ b/tests2/01_comment.c @@ -0,0 +1,14 @@ +#include + +int main() +{ + printf("Hello\n"); + printf("Hello\n"); /* this is a comment */ printf("Hello\n"); + printf("Hello\n"); + // this is also a comment sayhello(); + printf("Hello\n"); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/01_comment.expect b/tests2/01_comment.expect new file mode 100644 index 00000000..b1387ad0 --- /dev/null +++ b/tests2/01_comment.expect @@ -0,0 +1,5 @@ +Hello +Hello +Hello +Hello +Hello diff --git a/tests2/02_printf.c b/tests2/02_printf.c new file mode 100644 index 00000000..4c34dd87 --- /dev/null +++ b/tests2/02_printf.c @@ -0,0 +1,18 @@ +#include + +int main() +{ + printf("Hello world\n"); + + int Count; + for (Count = -5; Count <= 5; Count++) + printf("Count = %d\n", Count); + + printf("String 'hello', 'there' is '%s', '%s'\n", "hello", "there"); + printf("Character 'A' is '%c'\n", 65); + printf("Character 'a' is '%c'\n", 'a'); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/02_printf.expect b/tests2/02_printf.expect new file mode 100644 index 00000000..f67a0f6e --- /dev/null +++ b/tests2/02_printf.expect @@ -0,0 +1,15 @@ +Hello world +Count = -5 +Count = -4 +Count = -3 +Count = -2 +Count = -1 +Count = 0 +Count = 1 +Count = 2 +Count = 3 +Count = 4 +Count = 5 +String 'hello', 'there' is 'hello', 'there' +Character 'A' is 'A' +Character 'a' is 'a' diff --git a/tests2/03_struct.c b/tests2/03_struct.c new file mode 100644 index 00000000..df0d3e76 --- /dev/null +++ b/tests2/03_struct.c @@ -0,0 +1,31 @@ +#include + +struct fred +{ + int boris; + int natasha; +}; + +void main() +{ + struct fred bloggs; + + bloggs.boris = 12; + bloggs.natasha = 34; + + printf("%d\n", bloggs.boris); + printf("%d\n", bloggs.natasha); + + struct fred jones[2]; + jones[0].boris = 12; + jones[0].natasha = 34; + jones[1].boris = 56; + jones[1].natasha = 78; + + printf("%d\n", jones[0].boris); + printf("%d\n", jones[0].natasha); + printf("%d\n", jones[1].boris); + printf("%d\n", jones[1].natasha); + + return 0; +} diff --git a/tests2/03_struct.expect b/tests2/03_struct.expect new file mode 100644 index 00000000..ecbf5893 --- /dev/null +++ b/tests2/03_struct.expect @@ -0,0 +1,6 @@ +12 +34 +12 +34 +56 +78 diff --git a/tests2/04_for.c b/tests2/04_for.c new file mode 100644 index 00000000..312fed85 --- /dev/null +++ b/tests2/04_for.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + int Count; + + for (Count = 1; Count <= 10; Count++) + { + printf("%d\n", Count); + } + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/04_for.expect b/tests2/04_for.expect new file mode 100644 index 00000000..f00c965d --- /dev/null +++ b/tests2/04_for.expect @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/tests2/05_array.c b/tests2/05_array.c new file mode 100644 index 00000000..c218f316 --- /dev/null +++ b/tests2/05_array.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + int Count; + int Array[10]; + + for (Count = 1; Count <= 10; Count++) + { + Array[Count-1] = Count * Count; + } + + for (Count = 0; Count < 10; Count++) + { + printf("%d\n", Array[Count]); + } + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/05_array.expect b/tests2/05_array.expect new file mode 100644 index 00000000..bc7257c0 --- /dev/null +++ b/tests2/05_array.expect @@ -0,0 +1,10 @@ +1 +4 +9 +16 +25 +36 +49 +64 +81 +100 diff --git a/tests2/06_case.c b/tests2/06_case.c new file mode 100644 index 00000000..c0191e2b --- /dev/null +++ b/tests2/06_case.c @@ -0,0 +1,29 @@ +#include + +int main() +{ + int Count; + + for (Count = 0; Count < 4; Count++) + { + printf("%d\n", Count); + switch (Count) + { + case 1: + printf("%d\n", 1); + break; + + case 2: + printf("%d\n", 2); + break; + + default: + printf("%d\n", 0); + break; + } + } + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/06_case.expect b/tests2/06_case.expect new file mode 100644 index 00000000..fab2c201 --- /dev/null +++ b/tests2/06_case.expect @@ -0,0 +1,8 @@ +0 +0 +1 +1 +2 +2 +3 +0 diff --git a/tests2/07_function.c b/tests2/07_function.c new file mode 100644 index 00000000..0477ce14 --- /dev/null +++ b/tests2/07_function.c @@ -0,0 +1,30 @@ +#include + +int myfunc(int x) +{ + return x * x; +} + +void vfunc(int a) +{ + printf("a=%d\n", a); +} + +void qfunc() +{ + printf("qfunc()\n"); +} + +int main() +{ + printf("%d\n", myfunc(3)); + printf("%d\n", myfunc(4)); + + vfunc(1234); + + qfunc(); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/07_function.expect b/tests2/07_function.expect new file mode 100644 index 00000000..8ffb0a70 --- /dev/null +++ b/tests2/07_function.expect @@ -0,0 +1,4 @@ +9 +16 +a=1234 +qfunc() diff --git a/tests2/08_while.c b/tests2/08_while.c new file mode 100644 index 00000000..602ffc73 --- /dev/null +++ b/tests2/08_while.c @@ -0,0 +1,24 @@ +#include + +int main() +{ + int a; + int p; + int t; + + a = 1; + p = 0; + t = 0; + + while (a < 100) + { + printf("%d\n", a); + t = a; + a = t + p; + p = t; + } + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/08_while.expect b/tests2/08_while.expect new file mode 100644 index 00000000..702d4c0f --- /dev/null +++ b/tests2/08_while.expect @@ -0,0 +1,11 @@ +1 +1 +2 +3 +5 +8 +13 +21 +34 +55 +89 diff --git a/tests2/09_do_while.c b/tests2/09_do_while.c new file mode 100644 index 00000000..1d3315d3 --- /dev/null +++ b/tests2/09_do_while.c @@ -0,0 +1,24 @@ +#include + +int main() +{ + int a; + int p; + int t; + + a = 1; + p = 0; + t = 0; + + do + { + printf("%d\n", a); + t = a; + a = t + p; + p = t; + } while (a < 100); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/09_do_while.expect b/tests2/09_do_while.expect new file mode 100644 index 00000000..702d4c0f --- /dev/null +++ b/tests2/09_do_while.expect @@ -0,0 +1,11 @@ +1 +1 +2 +3 +5 +8 +13 +21 +34 +55 +89 diff --git a/tests2/10_pointer.c b/tests2/10_pointer.c new file mode 100644 index 00000000..0177f4d8 --- /dev/null +++ b/tests2/10_pointer.c @@ -0,0 +1,40 @@ +#include + +struct ziggy +{ + int a; + int b; + int c; +} bolshevic; + +int main() +{ + int a; + int *b; + int c; + + a = 42; + b = &a; + printf("a = %d\n", *b); + + bolshevic.a = 12; + bolshevic.b = 34; + bolshevic.c = 56; + + printf("bolshevic.a = %d\n", bolshevic.a); + printf("bolshevic.b = %d\n", bolshevic.b); + printf("bolshevic.c = %d\n", bolshevic.c); + + struct ziggy *tsar = &bolshevic; + + printf("tsar->a = %d\n", tsar->a); + printf("tsar->b = %d\n", tsar->b); + printf("tsar->c = %d\n", tsar->c); + + b = &(bolshevic.b); + printf("bolshevic.b = %d\n", *b); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/10_pointer.expect b/tests2/10_pointer.expect new file mode 100644 index 00000000..1e3c473f --- /dev/null +++ b/tests2/10_pointer.expect @@ -0,0 +1,8 @@ +a = 42 +bolshevic.a = 12 +bolshevic.b = 34 +bolshevic.c = 56 +tsar->a = 12 +tsar->b = 34 +tsar->c = 56 +bolshevic.b = 34 diff --git a/tests2/11_precedence.c b/tests2/11_precedence.c new file mode 100644 index 00000000..db2049df --- /dev/null +++ b/tests2/11_precedence.c @@ -0,0 +1,40 @@ +#include + +int main() +{ + int a; + int b; + int c; + int d; + int e; + int f; + int x; + int y; + + a = 12; + b = 34; + c = 56; + d = 78; + e = 0; + f = 1; + + printf("%d\n", c + d); + printf("%d\n", (y = c + d)); + printf("%d\n", e || e && f); + printf("%d\n", e || f && f); + printf("%d\n", e && e || f); + printf("%d\n", e && f || f); + printf("%d\n", a && f | f); + printf("%d\n", a | b ^ c & d); + printf("%d, %d\n", a == a, a == b); + printf("%d, %d\n", a != a, a != b); + printf("%d\n", a != b && c != d); + printf("%d\n", a + b * c / f); + printf("%d\n", a + b * c / f); + printf("%d\n", (4 << 4)); + printf("%d\n", (64 >> 4)); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/11_precedence.expect b/tests2/11_precedence.expect new file mode 100644 index 00000000..b6923966 --- /dev/null +++ b/tests2/11_precedence.expect @@ -0,0 +1,15 @@ +134 +134 +0 +1 +1 +1 +1 +46 +1, 0 +0, 1 +1 +1916 +1916 +64 +4 diff --git a/tests2/12_hashdefine.c b/tests2/12_hashdefine.c new file mode 100644 index 00000000..5c521e02 --- /dev/null +++ b/tests2/12_hashdefine.c @@ -0,0 +1,14 @@ +#include + +#define FRED 12 +#define BLOGGS(x) (12*(x)) + +int main() +{ + printf("%d\n", FRED); + printf("%d, %d, %d\n", BLOGGS(1), BLOGGS(2), BLOGGS(3)); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/12_hashdefine.expect b/tests2/12_hashdefine.expect new file mode 100644 index 00000000..99f2ed5d --- /dev/null +++ b/tests2/12_hashdefine.expect @@ -0,0 +1,2 @@ +12 +12, 24, 36 diff --git a/tests2/13_integer_literals.c b/tests2/13_integer_literals.c new file mode 100644 index 00000000..7cee98b1 --- /dev/null +++ b/tests2/13_integer_literals.c @@ -0,0 +1,20 @@ +#include + +int main() +{ + int a = 24680; + int b = 01234567; + int c = 0x2468ac; + int d = 0x2468AC; + int e = 0b010101010101; + + printf("%d\n", a); + printf("%d\n", b); + printf("%d\n", c); + printf("%d\n", d); + printf("%d\n", e); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/13_integer_literals.expect b/tests2/13_integer_literals.expect new file mode 100644 index 00000000..f5aca06f --- /dev/null +++ b/tests2/13_integer_literals.expect @@ -0,0 +1,5 @@ +24680 +342391 +2386092 +2386092 +1365 diff --git a/tests2/14_if.c b/tests2/14_if.c new file mode 100644 index 00000000..2bd25505 --- /dev/null +++ b/tests2/14_if.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + int a = 1; + + if (a) + printf("a is true\n"); + else + printf("a is false\n"); + + int b = 0; + if (b) + printf("b is true\n"); + else + printf("b is false\n"); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/tests2/14_if.expect b/tests2/14_if.expect new file mode 100644 index 00000000..c32c4159 --- /dev/null +++ b/tests2/14_if.expect @@ -0,0 +1,2 @@ +a is true +b is false diff --git a/tests2/15_recursion.c b/tests2/15_recursion.c new file mode 100644 index 00000000..f79a00df --- /dev/null +++ b/tests2/15_recursion.c @@ -0,0 +1,21 @@ +#include + +int factorial(int i) +{ + if (i < 2) + return i; + else + return i * factorial(i - 1); +} + +int main() +{ + int Count; + + for (Count = 1; Count <= 10; Count++) + printf("%d\n", factorial(Count)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/15_recursion.expect b/tests2/15_recursion.expect new file mode 100644 index 00000000..db47b283 --- /dev/null +++ b/tests2/15_recursion.expect @@ -0,0 +1,10 @@ +1 +2 +6 +24 +120 +720 +5040 +40320 +362880 +3628800 diff --git a/tests2/16_nesting.c b/tests2/16_nesting.c new file mode 100644 index 00000000..2b72cc02 --- /dev/null +++ b/tests2/16_nesting.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + int x, y, z; + + for (x = 0; x < 2; x++) + { + for (y = 0; y < 3; y++) + { + for (z = 0; z < 3; z++) + { + printf("%d %d %d\n", x, y, z); + } + } + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/16_nesting.expect b/tests2/16_nesting.expect new file mode 100644 index 00000000..625ee130 --- /dev/null +++ b/tests2/16_nesting.expect @@ -0,0 +1,18 @@ +0 0 0 +0 0 1 +0 0 2 +0 1 0 +0 1 1 +0 1 2 +0 2 0 +0 2 1 +0 2 2 +1 0 0 +1 0 1 +1 0 2 +1 1 0 +1 1 1 +1 1 2 +1 2 0 +1 2 1 +1 2 2 diff --git a/tests2/17_enum.c b/tests2/17_enum.c new file mode 100644 index 00000000..0853c42c --- /dev/null +++ b/tests2/17_enum.c @@ -0,0 +1,29 @@ +#include + +enum fred +{ + a, + b, + c, + d, + e = 54, + f = 73, + g, + h +}; + +int main() +{ + enum fred frod; + + printf("%d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h); + /* printf("%d\n", frod); */ + frod = 12; + printf("%d\n", frod); + frod = e; + printf("%d\n", frod); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/17_enum.expect b/tests2/17_enum.expect new file mode 100644 index 00000000..bef4be6d --- /dev/null +++ b/tests2/17_enum.expect @@ -0,0 +1,3 @@ +0 1 2 3 54 73 74 75 +12 +54 diff --git a/tests2/18_include.c b/tests2/18_include.c new file mode 100644 index 00000000..dbae3aa2 --- /dev/null +++ b/tests2/18_include.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + printf("including\n"); +#include "18_include.h" + printf("done\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/18_include.expect b/tests2/18_include.expect new file mode 100644 index 00000000..87729dfb --- /dev/null +++ b/tests2/18_include.expect @@ -0,0 +1,3 @@ +including +included +done diff --git a/tests2/18_include.h b/tests2/18_include.h new file mode 100644 index 00000000..01f894de --- /dev/null +++ b/tests2/18_include.h @@ -0,0 +1 @@ +printf("included\n"); diff --git a/tests2/19_pointer_arithmetic.c b/tests2/19_pointer_arithmetic.c new file mode 100644 index 00000000..aff65e5b --- /dev/null +++ b/tests2/19_pointer_arithmetic.c @@ -0,0 +1,28 @@ +#include + +int main() +{ + int a; + int *b; + int *c; + + a = 42; + b = &a; + c = NULL; + + printf("%d\n", *b); + + if (b == NULL) + printf("b is NULL\n"); + else + printf("b is not NULL\n"); + + if (c == NULL) + printf("c is NULL\n"); + else + printf("c is not NULL\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/19_pointer_arithmetic.expect b/tests2/19_pointer_arithmetic.expect new file mode 100644 index 00000000..0cf781b9 --- /dev/null +++ b/tests2/19_pointer_arithmetic.expect @@ -0,0 +1,3 @@ +42 +b is not NULL +c is NULL diff --git a/tests2/20_pointer_comparison.c b/tests2/20_pointer_comparison.c new file mode 100644 index 00000000..825f7787 --- /dev/null +++ b/tests2/20_pointer_comparison.c @@ -0,0 +1,24 @@ +#include + +int main() +{ + int a; + int b; + int *d; + int *e; + d = &a; + e = &b; + a = 12; + b = 34; + printf("%d\n", *d); + printf("%d\n", *e); + printf("%d\n", d == e); + printf("%d\n", d != e); + d = e; + printf("%d\n", d == e); + printf("%d\n", d != e); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/20_pointer_comparison.expect b/tests2/20_pointer_comparison.expect new file mode 100644 index 00000000..5d1e5f5c --- /dev/null +++ b/tests2/20_pointer_comparison.expect @@ -0,0 +1,6 @@ +12 +34 +0 +1 +1 +0 diff --git a/tests2/21_char_array.c b/tests2/21_char_array.c new file mode 100644 index 00000000..f22f5275 --- /dev/null +++ b/tests2/21_char_array.c @@ -0,0 +1,33 @@ +#include + +int main() +{ + int x = 'a'; + char y = x; + + char *a = "hello"; + + printf("%s\n", a); + + int c; + c = *a; + + char *b; + for (b = a; *b != 0; b++) + printf("%c: %d\n", *b, *b); + + char destarray[10]; + char *dest = &destarray[0]; + char *src = a; + + while (*src != 0) + *dest++ = *src++; + + *dest = 0; + + printf("copied string is %s\n", destarray); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/21_char_array.expect b/tests2/21_char_array.expect new file mode 100644 index 00000000..dbc60683 --- /dev/null +++ b/tests2/21_char_array.expect @@ -0,0 +1,7 @@ +hello +h: 104 +e: 101 +l: 108 +l: 108 +o: 111 +copied string is hello diff --git a/tests2/22_floating_point.c b/tests2/22_floating_point.c new file mode 100644 index 00000000..e3491f5c --- /dev/null +++ b/tests2/22_floating_point.c @@ -0,0 +1,50 @@ +#include +#include + +int main() +{ + // variables + float a = 12.34 + 56.78; + printf("%f\n", a); + + // infix operators + printf("%f\n", 12.34 + 56.78); + printf("%f\n", 12.34 - 56.78); + printf("%f\n", 12.34 * 56.78); + printf("%f\n", 12.34 / 56.78); + + // comparison operators + 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); + 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); + 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); + + // assignment operators + a = 12.34; + a += 56.78; + printf("%f\n", a); + + a = 12.34; + a -= 56.78; + printf("%f\n", a); + + a = 12.34; + a *= 56.78; + printf("%f\n", a); + + a = 12.34; + a /= 56.78; + printf("%f\n", a); + + // prefix operators + printf("%f\n", +12.34); + printf("%f\n", -12.34); + + // type coercion + a = 2; + printf("%f\n", a); + printf("%f\n", sin(2)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/22_floating_point.expect b/tests2/22_floating_point.expect new file mode 100644 index 00000000..75ea3a7e --- /dev/null +++ b/tests2/22_floating_point.expect @@ -0,0 +1,16 @@ +69.120003 +69.120000 +-44.440000 +700.665200 +0.217330 +1 1 0 0 0 1 +0 1 1 1 0 0 +0 0 0 1 1 1 +69.120003 +-44.439999 +700.665222 +0.217330 +12.340000 +-12.340000 +2.000000 +0.909297 diff --git a/tests2/23_type_coercion.c b/tests2/23_type_coercion.c new file mode 100644 index 00000000..1fcc3351 --- /dev/null +++ b/tests2/23_type_coercion.c @@ -0,0 +1,54 @@ +#include + +void charfunc(char a) +{ + printf("char: %c\n", a); +} + +void intfunc(int a) +{ + printf("int: %d\n", a); +} + +void floatfunc(float a) +{ + printf("float: %f\n", a); +} + +int main() +{ + charfunc('a'); + charfunc(98); + charfunc(99.0); + + intfunc('a'); + intfunc(98); + intfunc(99.0); + + floatfunc('a'); + floatfunc(98); + floatfunc(99.0); + + /* printf("%c %d %f\n", 'a', 'b', 'c'); */ + /* printf("%c %d %f\n", 97, 98, 99); */ + /* printf("%c %d %f\n", 97.0, 98.0, 99.0); */ + + char b = 97; + char c = 97.0; + + printf("%d %d\n", b, c); + + int d = 'a'; + int e = 97.0; + + printf("%d %d\n", d, e); + + float f = 'a'; + float g = 97; + + printf("%f %f\n", f, g); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/23_type_coercion.expect b/tests2/23_type_coercion.expect new file mode 100644 index 00000000..d9076f01 --- /dev/null +++ b/tests2/23_type_coercion.expect @@ -0,0 +1,12 @@ +char: a +char: b +char: c +int: 97 +int: 98 +int: 99 +float: 97.000000 +float: 98.000000 +float: 99.000000 +97 97 +97 97 +97.000000 97.000000 diff --git a/tests2/24_math_library.c b/tests2/24_math_library.c new file mode 100644 index 00000000..4cc3d9e1 --- /dev/null +++ b/tests2/24_math_library.c @@ -0,0 +1,28 @@ +#include +#include + +int main() +{ + printf("%f\n", sin(0.12)); + printf("%f\n", cos(0.12)); + printf("%f\n", tan(0.12)); + printf("%f\n", asin(0.12)); + printf("%f\n", acos(0.12)); + printf("%f\n", atan(0.12)); + printf("%f\n", sinh(0.12)); + printf("%f\n", cosh(0.12)); + printf("%f\n", tanh(0.12)); + printf("%f\n", exp(0.12)); + printf("%f\n", fabs(-0.12)); + printf("%f\n", log(0.12)); + printf("%f\n", log10(0.12)); + printf("%f\n", pow(0.12, 0.12)); + printf("%f\n", sqrt(0.12)); + printf("%f\n", round(12.34)); + printf("%f\n", ceil(12.34)); + printf("%f\n", floor(12.34)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/24_math_library.expect b/tests2/24_math_library.expect new file mode 100644 index 00000000..99f7299c --- /dev/null +++ b/tests2/24_math_library.expect @@ -0,0 +1,18 @@ +0.119712 +0.992809 +0.120579 +0.120290 +1.450506 +0.119429 +0.120288 +1.007209 +0.119427 +1.127497 +0.120000 +-2.120264 +-0.920819 +0.775357 +0.346410 +12.000000 +13.000000 +12.000000 diff --git a/tests2/25_quicksort.c b/tests2/25_quicksort.c new file mode 100644 index 00000000..5cc08bd9 --- /dev/null +++ b/tests2/25_quicksort.c @@ -0,0 +1,83 @@ +#include + +int array[16]; + +//Swap integer values by array indexes +void swap(int a, int b) +{ + int tmp = array[a]; + array[a] = array[b]; + array[b] = tmp; +} + +//Partition the array into two halves and return the +//index about which the array is partitioned +int partition(int left, int right) +{ + int pivotIndex = left; + int pivotValue = array[pivotIndex]; + int index = left; + int i; + + swap(pivotIndex, right); + for(i = left; i < right; i++) + { + if(array[i] < pivotValue) + { + swap(i, index); + index += 1; + } + } + swap(right, index); + + return index; +} + +//Quicksort the array +void quicksort(int left, int right) +{ + if(left >= right) + return; + + int index = partition(left, right); + quicksort(left, index - 1); + quicksort(index + 1, right); +} + +int main() +{ + int i; + + array[0] = 62; + array[1] = 83; + array[2] = 4; + array[3] = 89; + array[4] = 36; + array[5] = 21; + array[6] = 74; + array[7] = 37; + array[8] = 65; + array[9] = 33; + array[10] = 96; + array[11] = 38; + array[12] = 53; + array[13] = 16; + array[14] = 74; + array[15] = 55; + + for (i = 0; i < 16; i++) + printf("%d ", array[i]); + + printf("\n"); + + quicksort(0, 15); + + for (i = 0; i < 16; i++) + printf("%d ", array[i]); + + printf("\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/25_quicksort.expect b/tests2/25_quicksort.expect new file mode 100644 index 00000000..2d39cd32 --- /dev/null +++ b/tests2/25_quicksort.expect @@ -0,0 +1,2 @@ +62 83 4 89 36 21 74 37 65 33 96 38 53 16 74 55 +4 16 21 33 36 37 38 53 55 62 65 74 74 83 89 96 diff --git a/tests2/26_character_constants.c b/tests2/26_character_constants.c new file mode 100644 index 00000000..95c44237 --- /dev/null +++ b/tests2/26_character_constants.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + printf("%d\n", '\1'); + printf("%d\n", '\10'); + printf("%d\n", '\100'); + printf("%d\n", '\x01'); + printf("%d\n", '\x0e'); + printf("%d\n", '\x10'); + printf("%d\n", '\x40'); + printf("test \x40\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/26_character_constants.expect b/tests2/26_character_constants.expect new file mode 100644 index 00000000..8f8bfa48 --- /dev/null +++ b/tests2/26_character_constants.expect @@ -0,0 +1,8 @@ +1 +8 +64 +1 +14 +16 +64 +test @ diff --git a/tests2/27_sizeof.c b/tests2/27_sizeof.c new file mode 100644 index 00000000..d1694b64 --- /dev/null +++ b/tests2/27_sizeof.c @@ -0,0 +1,16 @@ +#include + +int main() +{ + char a; + int b; + double c; + + printf("%d\n", sizeof(a)); + printf("%d\n", sizeof(b)); + printf("%d\n", sizeof(c)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/27_sizeof.expect b/tests2/27_sizeof.expect new file mode 100644 index 00000000..7329e00f --- /dev/null +++ b/tests2/27_sizeof.expect @@ -0,0 +1,3 @@ +1 +4 +8 diff --git a/tests2/28_strings.c b/tests2/28_strings.c new file mode 100644 index 00000000..115fe096 --- /dev/null +++ b/tests2/28_strings.c @@ -0,0 +1,46 @@ +#include +#include +#include + +int main() +{ + char a[10]; + + strcpy(a, "hello"); + printf("%s\n", a); + + strncpy(a, "gosh", 2); + printf("%s\n", a); + + printf("%d\n", strcmp(a, "apple") > 0); + printf("%d\n", strcmp(a, "goere") > 0); + printf("%d\n", strcmp(a, "zebra") < 0); + + printf("%d\n", strlen(a)); + + strcat(a, "!"); + printf("%s\n", a); + + printf("%d\n", strncmp(a, "apple", 2) > 0); + printf("%d\n", strncmp(a, "goere", 2) == 0); + printf("%d\n", strncmp(a, "goerg", 2) == 0); + printf("%d\n", strncmp(a, "zebra", 2) < 0); + + printf("%s\n", index(a, 'o')); + printf("%s\n", rindex(a, 'l')); + printf("%d\n", rindex(a, 'x') == NULL); + + memset(&a[1], 'r', 4); + printf("%s\n", a); + + memcpy(&a[2], a, 2); + printf("%s\n", a); + + printf("%d\n", memcmp(a, "apple", 4) > 0); + printf("%d\n", memcmp(a, "grgr", 4) == 0); + printf("%d\n", memcmp(a, "zebra", 4) < 0); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/28_strings.expect b/tests2/28_strings.expect new file mode 100644 index 00000000..fd9217a2 --- /dev/null +++ b/tests2/28_strings.expect @@ -0,0 +1,19 @@ +hello +gollo +1 +1 +1 +5 +gollo! +1 +1 +1 +1 +ollo! +lo! +1 +grrrr! +grgrr! +1 +1 +1 diff --git a/tests2/29_array_address.c b/tests2/29_array_address.c new file mode 100644 index 00000000..bda5ddde --- /dev/null +++ b/tests2/29_array_address.c @@ -0,0 +1,13 @@ +#include +#include + +int main() +{ + char a[10]; + strcpy(a, "abcdef"); + printf("%s\n", &a[1]); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/29_array_address.expect b/tests2/29_array_address.expect new file mode 100644 index 00000000..9bc86832 --- /dev/null +++ b/tests2/29_array_address.expect @@ -0,0 +1 @@ +bcdef diff --git a/tests2/30_hanoi.c b/tests2/30_hanoi.c new file mode 100644 index 00000000..b1a11814 --- /dev/null +++ b/tests2/30_hanoi.c @@ -0,0 +1,122 @@ +/* example from http://barnyard.syr.edu/quickies/hanoi.c */ + +/* hanoi.c: solves the tower of hanoi problem. (Programming exercise.) */ +/* By Terry R. McConnell (12/2/97) */ +/* Compile: cc -o hanoi hanoi.c */ + +/* This program does no error checking. But then, if it's right, + it's right ... right ? */ + + +/* The original towers of hanoi problem seems to have been originally posed + by one M. Claus in 1883. There is a popular legend that goes along with + it that has been often repeated and paraphrased. It goes something like this: + In the great temple at Benares there are 3 golden spikes. On one of them, + God placed 64 disks increasing in size from bottom to top, at the beginning + of time. Since then, and to this day, the priest on duty constantly transfers + disks, one at a time, in such a way that no larger disk is ever put on top + of a smaller one. When the disks have been transferred entirely to another + spike the Universe will come to an end in a large thunderclap. + + This paraphrases the original legend due to DeParville, La Nature, Paris 1884, + Part I, 285-286. For this and further information see: Mathematical + Recreations & Essays, W.W. Rouse Ball, MacMillan, NewYork, 11th Ed. 1967, + 303-305. + * + * + */ + +#include +#include + +#define TRUE 1 +#define FALSE 0 + +/* This is the number of "disks" on tower A initially. Taken to be 64 in the + * legend. The number of moves required, in general, is 2^N - 1. For N = 64, + * this is 18,446,744,073,709,551,615 */ +#define N 4 + +/* These are the three towers. For example if the state of A is 0,1,3,4, that + * means that there are three discs on A of sizes 1, 3, and 4. (Think of right + * as being the "down" direction.) */ +int A[N], B[N], C[N]; + +void Hanoi(int,int*,int*,int*); + +/* Print the current configuration of A, B, and C to the screen */ +void PrintAll() +{ + int i; + + printf("A: "); + for(i=0;i + +int main(int argc, char **argv) +{ + int Count; + + printf("hello world %d\n", argc); + for (Count = 0; Count < argc; Count++) + printf("arg %d: %s\n", Count, argv[Count]); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/31_args.expect b/tests2/31_args.expect new file mode 100644 index 00000000..c392b675 --- /dev/null +++ b/tests2/31_args.expect @@ -0,0 +1,7 @@ +hello world 6 +arg 0: 31_args.c +arg 1: - +arg 2: arg1 +arg 3: arg2 +arg 4: arg3 +arg 5: arg4 diff --git a/tests2/32_led.c b/tests2/32_led.c new file mode 100644 index 00000000..5596cbfd --- /dev/null +++ b/tests2/32_led.c @@ -0,0 +1,266 @@ +/* example from http://barnyard.syr.edu/quickies/led.c */ + +/* led.c: print out number as if on 7 line led display. I.e., write integer + given on command line like this: + _ _ _ + | _| _| |_| |_ + | |_ _| | _| etc. + + We assume the terminal behaves like a classical teletype. So the top + lines of all digits have to be printed first, then the middle lines of + all digits, etc. + + By Terry R. McConnell + +compile: cc -o led led.c + +If you just want to link in the subroutine print_led that does all the +work, compile with -DNO_MAIN, and declare the following in any source file +that uses the call: + +extern void print_led(unsigned long x, char *buf); + +Bug: you cannot call repeatedly to print more than one number to a line. +That would require curses or some other terminal API that allows moving the +cursor to a previous line. + +*/ + + + +#include +#include + +#define MAX_DIGITS 32 +#define NO_MAIN + + +/* Print the top line of the digit d into buffer. + Does not null terminate buffer. */ + +void topline(int d, char *p){ + + *p++ = ' '; + switch(d){ + + /* all these have _ on top line */ + + case 0: + case 2: + case 3: + case 5: + case 7: + case 8: + case 9: + *p++ = '_'; + break; + default: + *p++=' '; + + } + *p++=' '; +} + +/* Print the middle line of the digit d into the buffer. + Does not null terminate. */ + +void midline(int d, char *p){ + + switch(d){ + + /* those that have leading | on middle line */ + + case 0: + case 4: + case 5: + case 6: + case 8: + case 9: + *p++='|'; + break; + default: + *p++=' '; + } + switch(d){ + + /* those that have _ on middle line */ + + case 2: + case 3: + case 4: + case 5: + case 6: + case 8: + case 9: + *p++='_'; + break; + default: + *p++=' '; + + } + switch(d){ + + /* those that have closing | on middle line */ + + case 0: + case 1: + case 2: + case 3: + case 4: + case 7: + case 8: + case 9: + *p++='|'; + break; + default: + *p++=' '; + + } +} + +/* Print the bottom line of the digit d. Does not null terminate. */ + +void botline(int d, char *p){ + + + switch(d){ + + /* those that have leading | on bottom line */ + + case 0: + case 2: + case 6: + case 8: + *p++='|'; + break; + default: + *p++=' '; + } + switch(d){ + + /* those that have _ on bottom line */ + + case 0: + case 2: + case 3: + case 5: + case 6: + case 8: + *p++='_'; + break; + default: + *p++=' '; + + } + switch(d){ + + /* those that have closing | on bottom line */ + + case 0: + case 1: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + *p++='|'; + break; + default: + *p++=' '; + + } +} + +/* Write the led representation of integer to string buffer. */ + +void print_led(unsigned long x, char *buf) +{ + + int i=0,n; + static int d[MAX_DIGITS]; + + + /* extract digits from x */ + + n = ( x == 0L ? 1 : 0 ); /* 0 is a digit, hence a special case */ + + while(x){ + d[n++] = (int)(x%10L); + if(n >= MAX_DIGITS)break; + x = x/10L; + } + + /* print top lines of all digits */ + + for(i=n-1;i>=0;i--){ + topline(d[i],buf); + buf += 3; + *buf++=' '; + } + *buf++='\n'; /* move teletype to next line */ + + /* print middle lines of all digits */ + + for(i=n-1;i>=0;i--){ + midline(d[i],buf); + buf += 3; + *buf++=' '; + } + *buf++='\n'; + + /* print bottom lines of all digits */ + + for(i=n-1;i>=0;i--){ + botline(d[i],buf); + buf += 3; + *buf++=' '; + } + *buf++='\n'; + *buf='\0'; +} + +int main() +{ + char buf[5*MAX_DIGITS]; + print_led(1234567, buf); + printf("%s\n",buf); + + return 0; +} + +#ifndef NO_MAIN +int main(int argc, char **argv) +{ + + int i=0,n; + long x; + static int d[MAX_DIGITS]; + char buf[5*MAX_DIGITS]; + + if(argc != 2){ + fprintf(stderr,"led: usage: led integer\n"); + return 1; + } + + /* fetch argument from command line */ + + x = atol(argv[1]); + + /* sanity check */ + + if(x<0){ + fprintf(stderr,"led: %d must be non-negative\n",x); + return 1; + } + + print_led(x,buf); + printf("%s\n",buf); + + return 0; + +} +#endif + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/32_led.expect b/tests2/32_led.expect new file mode 100644 index 00000000..c53b58ae --- /dev/null +++ b/tests2/32_led.expect @@ -0,0 +1,4 @@ + _ _ _ _ + | _| _| |_| |_ |_ | + | |_ _| | _| |_| | + diff --git a/tests2/33_ternary_op.c b/tests2/33_ternary_op.c new file mode 100644 index 00000000..8579b502 --- /dev/null +++ b/tests2/33_ternary_op.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + int Count; + + for (Count = 0; Count < 10; Count++) + { + printf("%d\n", (Count < 5) ? (Count*Count) : (Count * 3)); + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/33_ternary_op.expect b/tests2/33_ternary_op.expect new file mode 100644 index 00000000..45ea507a --- /dev/null +++ b/tests2/33_ternary_op.expect @@ -0,0 +1,10 @@ +0 +1 +4 +9 +16 +15 +18 +21 +24 +27 diff --git a/tests2/34_array_assignment.c b/tests2/34_array_assignment.c new file mode 100644 index 00000000..5885c973 --- /dev/null +++ b/tests2/34_array_assignment.c @@ -0,0 +1,23 @@ +#include + +int main() +{ + int a[4]; + + a[0] = 12; + a[1] = 23; + a[2] = 34; + a[3] = 45; + + printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]); + + int b[4]; + + b = a; + + printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/34_array_assignment.expect b/tests2/34_array_assignment.expect new file mode 100644 index 00000000..9736bf50 --- /dev/null +++ b/tests2/34_array_assignment.expect @@ -0,0 +1,2 @@ +12 23 34 45 +12 23 34 45 diff --git a/tests2/35_sizeof.c b/tests2/35_sizeof.c new file mode 100644 index 00000000..672e87e7 --- /dev/null +++ b/tests2/35_sizeof.c @@ -0,0 +1,14 @@ +#include + +int main() +{ + char a; + short b; + + printf("%d %d\n", sizeof(char), sizeof(a)); + printf("%d %d\n", sizeof(short), sizeof(b)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/35_sizeof.expect b/tests2/35_sizeof.expect new file mode 100644 index 00000000..534fb831 --- /dev/null +++ b/tests2/35_sizeof.expect @@ -0,0 +1,2 @@ +1 1 +2 2 diff --git a/tests2/36_array_initialisers.c b/tests2/36_array_initialisers.c new file mode 100644 index 00000000..1bc8ee08 --- /dev/null +++ b/tests2/36_array_initialisers.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + int Count; + + int Array[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753 }; + + for (Count = 0; Count < 10; Count++) + printf("%d: %d\n", Count, Array[Count]); + + int Array2[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753, }; + + for (Count = 0; Count < 10; Count++) + printf("%d: %d\n", Count, Array2[Count]); + + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/36_array_initialisers.expect b/tests2/36_array_initialisers.expect new file mode 100644 index 00000000..3ac6c772 --- /dev/null +++ b/tests2/36_array_initialisers.expect @@ -0,0 +1,20 @@ +0: 12 +1: 34 +2: 56 +3: 78 +4: 90 +5: 123 +6: 456 +7: 789 +8: 8642 +9: 9753 +0: 12 +1: 34 +2: 56 +3: 78 +4: 90 +5: 123 +6: 456 +7: 789 +8: 8642 +9: 9753 diff --git a/tests2/37_sprintf.c b/tests2/37_sprintf.c new file mode 100644 index 00000000..1dd1dce2 --- /dev/null +++ b/tests2/37_sprintf.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + char Buf[100]; + int Count; + + for (Count = 1; Count <= 20; Count++) + { + sprintf(Buf, "->%02d<-\n", Count); + printf("%s", Buf); + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/37_sprintf.expect b/tests2/37_sprintf.expect new file mode 100644 index 00000000..a643da84 --- /dev/null +++ b/tests2/37_sprintf.expect @@ -0,0 +1,20 @@ +->01<- +->02<- +->03<- +->04<- +->05<- +->06<- +->07<- +->08<- +->09<- +->10<- +->11<- +->12<- +->13<- +->14<- +->15<- +->16<- +->17<- +->18<- +->19<- +->20<- diff --git a/tests2/38_multiple_array_index.c b/tests2/38_multiple_array_index.c new file mode 100644 index 00000000..4e1868ef --- /dev/null +++ b/tests2/38_multiple_array_index.c @@ -0,0 +1,32 @@ +#include + +int main() +{ + int a[4][4]; + int b = 0; + int x; + int y; + + for (x = 0; x < 4; x++) + { + for (y = 0; y < 4; y++) + { + b++; + a[x][y] = b; + } + } + + for (x = 0; x < 4; x++) + { + printf("x=%d: ", x); + for (y = 0; y < 4; y++) + { + printf("%d ", a[x][y]); + } + printf("\n"); + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/38_multiple_array_index.expect b/tests2/38_multiple_array_index.expect new file mode 100644 index 00000000..747ad751 --- /dev/null +++ b/tests2/38_multiple_array_index.expect @@ -0,0 +1,4 @@ +x=0: 1 2 3 4 +x=1: 5 6 7 8 +x=2: 9 10 11 12 +x=3: 13 14 15 16 diff --git a/tests2/39_typedef.c b/tests2/39_typedef.c new file mode 100644 index 00000000..79ab58b7 --- /dev/null +++ b/tests2/39_typedef.c @@ -0,0 +1,31 @@ +#include + +typedef int MyInt; + +struct FunStruct +{ + int i; + int j; +}; + +typedef struct FunStruct MyFunStruct; + +typedef MyFunStruct *MoreFunThanEver; + +int main() +{ + MyInt a = 1; + printf("%d\n", a); + + MyFunStruct b; + b.i = 12; + b.j = 34; + printf("%d,%d\n", b.i, b.j); + + MoreFunThanEver c = &b; + printf("%d,%d\n", c->i, c->j); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/39_typedef.expect b/tests2/39_typedef.expect new file mode 100644 index 00000000..b9050a9b --- /dev/null +++ b/tests2/39_typedef.expect @@ -0,0 +1,3 @@ +1 +12,34 +12,34 diff --git a/tests2/40_stdio.c b/tests2/40_stdio.c new file mode 100644 index 00000000..575127af --- /dev/null +++ b/tests2/40_stdio.c @@ -0,0 +1,52 @@ +#include + +int main() +{ + FILE *f = fopen("fred.txt", "w"); + fwrite("hello\nhello\n", 1, 12, f); + fclose(f); + + char freddy[7]; + f = fopen("fred.txt", "r"); + if (fread(freddy, 1, 6, f) != 6) + printf("couldn't read fred.txt\n"); + + freddy[6] = '\0'; + fclose(f); + + printf("%s", freddy); + + char InChar; + char ShowChar; + f = fopen("fred.txt", "r"); + while ( (InChar = fgetc(f)) != EOF) + { + ShowChar = InChar; + if (ShowChar < ' ') + ShowChar = '.'; + + printf("ch: %d '%c'\n", InChar, ShowChar); + } + fclose(f); + + f = fopen("fred.txt", "r"); + while ( (InChar = getc(f)) != EOF) + { + ShowChar = InChar; + if (ShowChar < ' ') + ShowChar = '.'; + + printf("ch: %d '%c'\n", InChar, ShowChar); + } + fclose(f); + + f = fopen("fred.txt", "r"); + while (fgets(freddy, sizeof(freddy), f) != NULL) + printf("x: %s", freddy); + + fclose(f); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/40_stdio.expect b/tests2/40_stdio.expect new file mode 100644 index 00000000..e08167a6 --- /dev/null +++ b/tests2/40_stdio.expect @@ -0,0 +1,27 @@ +hello +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +x: hello +x: hello diff --git a/tests2/41_hashif.c b/tests2/41_hashif.c new file mode 100644 index 00000000..cb37b9eb --- /dev/null +++ b/tests2/41_hashif.c @@ -0,0 +1,85 @@ +#include + +int main() +{ + printf("#include test\n"); + +#if 1 +#if 0 + printf("a\n"); +#else + printf("b\n"); +#endif +#else +#if 0 + printf("c\n"); +#else + printf("d\n"); +#endif +#endif + +#if 0 +#if 1 + printf("e\n"); +#else + printf("f\n"); +#endif +#else +#if 1 + printf("g\n"); +#else + printf("h\n"); +#endif +#endif + +#define DEF + +#ifdef DEF +#ifdef DEF + printf("i\n"); +#else + printf("j\n"); +#endif +#else +#ifdef DEF + printf("k\n"); +#else + printf("l\n"); +#endif +#endif + +#ifndef DEF +#ifndef DEF + printf("m\n"); +#else + printf("n\n"); +#endif +#else +#ifndef DEF + printf("o\n"); +#else + printf("p\n"); +#endif +#endif + +#define ONE 1 +#define ZERO 0 + +#if ONE +#if ZERO + printf("q\n"); +#else + printf("r\n"); +#endif +#else +#if ZERO + printf("s\n"); +#else + printf("t\n"); +#endif +#endif + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/41_hashif.expect b/tests2/41_hashif.expect new file mode 100644 index 00000000..5fd414bd --- /dev/null +++ b/tests2/41_hashif.expect @@ -0,0 +1,6 @@ +#include test +b +g +i +p +r diff --git a/tests2/42_function_pointer.c b/tests2/42_function_pointer.c new file mode 100644 index 00000000..49c331bf --- /dev/null +++ b/tests2/42_function_pointer.c @@ -0,0 +1,18 @@ +#include + +int fred(int p) +{ + printf("yo %d\n", p); + return 42; +} + +int (*f)(int) = &fred; + +int main() +{ + printf("%d\n", (*f)(24)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/42_function_pointer.expect b/tests2/42_function_pointer.expect new file mode 100644 index 00000000..6c8b6ce4 --- /dev/null +++ b/tests2/42_function_pointer.expect @@ -0,0 +1,2 @@ +yo 24 +42 diff --git a/tests2/43_void_param.c b/tests2/43_void_param.c new file mode 100644 index 00000000..de17098b --- /dev/null +++ b/tests2/43_void_param.c @@ -0,0 +1,15 @@ +#include + +void fred(void) +{ + printf("yo\n"); +} + +int main() +{ + fred(); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/43_void_param.expect b/tests2/43_void_param.expect new file mode 100644 index 00000000..092bfb9b --- /dev/null +++ b/tests2/43_void_param.expect @@ -0,0 +1 @@ +yo diff --git a/tests2/44_scoped_declarations.c b/tests2/44_scoped_declarations.c new file mode 100644 index 00000000..2febf987 --- /dev/null +++ b/tests2/44_scoped_declarations.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + int a; + + for (a = 0; a < 2; a++) + { + int b = a; + } + + printf("it's all good\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/44_scoped_declarations.expect b/tests2/44_scoped_declarations.expect new file mode 100644 index 00000000..231ccc0c --- /dev/null +++ b/tests2/44_scoped_declarations.expect @@ -0,0 +1 @@ +it's all good diff --git a/tests2/45_empty_for.c b/tests2/45_empty_for.c new file mode 100644 index 00000000..b14e9d4e --- /dev/null +++ b/tests2/45_empty_for.c @@ -0,0 +1,18 @@ +#include + +int main() +{ + int Count = 0; + + for (;;) + { + Count++; + printf("%d\n", Count); + if (Count >= 10) + break; + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/45_empty_for.expect b/tests2/45_empty_for.expect new file mode 100644 index 00000000..f00c965d --- /dev/null +++ b/tests2/45_empty_for.expect @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/tests2/46_grep.c b/tests2/46_grep.c new file mode 100644 index 00000000..5f52220a --- /dev/null +++ b/tests2/46_grep.c @@ -0,0 +1,564 @@ +/* + * The information in this document is subject to change + * without notice and should not be construed as a commitment + * by Digital Equipment Corporation or by DECUS. + * + * Neither Digital Equipment Corporation, DECUS, nor the authors + * assume any responsibility for the use or reliability of this + * document or the described software. + * + * Copyright (C) 1980, DECUS + * + * General permission to copy or modify, but not for profit, is + * hereby granted, provided that the above copyright notice is + * included and reference made to the fact that reproduction + * privileges were granted by DECUS. + */ +#include + +/* + * grep + * + * Runs on the Decus compiler or on vms, On vms, define as: + * grep :== "$disk:[account]grep" (native) + * grep :== "$disk:[account]grep grep" (Decus) + * See below for more information. + */ + +#if 0 +char *documentation[] = { + "grep searches a file for a given pattern. Execute by", + " grep [flags] regular_expression file_list\n", + "Flags are single characters preceeded by '-':", + " -c Only a count of matching lines is printed", + " -f Print file name for matching lines switch, see below", + " -n Each line is preceeded by its line number", + " -v Only print non-matching lines\n", + "The file_list is a list of files (wildcards are acceptable on RSX modes).", + "\nThe file name is normally printed if there is a file given.", + "The -f flag reverses this action (print name no file, not if more).\n", + 0 }; + +char *patdoc[] = { + "The regular_expression defines the pattern to search for. Upper- and", + "lower-case are always ignored. Blank lines never match. The expression", + "should be quoted to prevent file-name translation.", + "x An ordinary character (not mentioned below) matches that character.", + "'\\' The backslash quotes any character. \"\\$\" matches a dollar-sign.", + "'^' A circumflex at the beginning of an expression matches the", + " beginning of a line.", + "'$' A dollar-sign at the end of an expression matches the end of a line.", + "'.' A period matches any character except \"new-line\".", + "':a' A colon matches a class of characters described by the following", + "':d' character. \":a\" matches any alphabetic, \":d\" matches digits,", + "':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and", + "': ' other control characters, such as new-line.", + "'*' An expression followed by an asterisk matches zero or more", + " occurrances of that expression: \"fo*\" matches \"f\", \"fo\"", + " \"foo\", etc.", + "'+' An expression followed by a plus sign matches one or more", + " occurrances of that expression: \"fo+\" matches \"fo\", etc.", + "'-' An expression followed by a minus sign optionally matches", + " the expression.", + "'[]' A string enclosed in square brackets matches any character in", + " that string, but no others. If the first character in the", + " string is a circumflex, the expression matches any character", + " except \"new-line\" and the characters in the string. For", + " example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"", + " matches \"abc\" but not \"axb\". A range of characters may be", + " specified by two characters separated by \"-\". Note that,", + " [a-z] matches alphabetics, while [z-a] never matches.", + "The concatenation of regular expressions is a regular expression.", + 0}; +#endif + +#define LMAX 512 +#define PMAX 256 + +#define CHAR 1 +#define BOL 2 +#define EOL 3 +#define ANY 4 +#define CLASS 5 +#define NCLASS 6 +#define STAR 7 +#define PLUS 8 +#define MINUS 9 +#define ALPHA 10 +#define DIGIT 11 +#define NALPHA 12 +#define PUNCT 13 +#define RANGE 14 +#define ENDPAT 15 + +int cflag=0, fflag=0, nflag=0, vflag=0, nfile=0, debug=0; + +char *pp, lbuf[LMAX], pbuf[PMAX]; + +char *cclass(); +char *pmatch(); + + +/*** Display a file name *******************************/ +void file(char *s) +{ + printf("File %s:\n", s); +} + +/*** Report unopenable file ****************************/ +void cant(char *s) +{ + fprintf(stderr, "%s: cannot open\n", s); +} + +/*** Give good help ************************************/ +void help(char **hp) +{ + char **dp; + + for (dp = hp; *dp; ++dp) + printf("%s\n", *dp); +} + +/*** Display usage summary *****************************/ +void usage(char *s) +{ + fprintf(stderr, "?GREP-E-%s\n", s); + fprintf(stderr, + "Usage: grep [-cfnv] pattern [file ...]. grep ? for help\n"); + exit(1); +} + +/*** Compile the pattern into global pbuf[] ************/ +void compile(char *source) +{ + char *s; /* Source string pointer */ + char *lp; /* Last pattern pointer */ + int c; /* Current character */ + int o; /* Temp */ + char *spp; /* Save beginning of pattern */ + + s = source; + if (debug) + printf("Pattern = \"%s\"\n", s); + pp = pbuf; + while (c = *s++) { + /* + * STAR, PLUS and MINUS are special. + */ + if (c == '*' || c == '+' || c == '-') { + if (pp == pbuf || + (o=pp[-1]) == BOL || + o == EOL || + o == STAR || + o == PLUS || + o == MINUS) + badpat("Illegal occurrance op.", source, s); + store(ENDPAT); + store(ENDPAT); + spp = pp; /* Save pattern end */ + while (--pp > lp) /* Move pattern down */ + *pp = pp[-1]; /* one byte */ + *pp = (c == '*') ? STAR : + (c == '-') ? MINUS : PLUS; + pp = spp; /* Restore pattern end */ + continue; + } + /* + * All the rest. + */ + lp = pp; /* Remember start */ + switch(c) { + + case '^': + store(BOL); + break; + + case '$': + store(EOL); + break; + + case '.': + store(ANY); + break; + + case '[': + s = cclass(source, s); + break; + + case ':': + if (*s) { + switch(tolower(c = *s++)) { + + case 'a': + case 'A': + store(ALPHA); + break; + + case 'd': + case 'D': + store(DIGIT); + break; + + case 'n': + case 'N': + store(NALPHA); + break; + + case ' ': + store(PUNCT); + break; + + default: + badpat("Unknown : type", source, s); + + } + break; + } + else badpat("No : type", source, s); + + case '\\': + if (*s) + c = *s++; + + default: + store(CHAR); + store(tolower(c)); + } + } + store(ENDPAT); + store(0); /* Terminate string */ + if (debug) { + for (lp = pbuf; lp < pp;) { + if ((c = (*lp++ & 0377)) < ' ') + printf("\\%o ", c); + else printf("%c ", c); + } + printf("\n"); + } +} + +/*** Compile a class (within []) ***********************/ +char *cclass(char *source, char *src) + /* char *source; // Pattern start -- for error msg. */ + /* char *src; // Class start */ +{ + char *s; /* Source pointer */ + char *cp; /* Pattern start */ + int c; /* Current character */ + int o; /* Temp */ + + s = src; + o = CLASS; + if (*s == '^') { + ++s; + o = NCLASS; + } + store(o); + cp = pp; + store(0); /* Byte count */ + while ((c = *s++) && c!=']') { + if (c == '\\') { /* Store quoted char */ + if ((c = *s++) == '\0') /* Gotta get something */ + badpat("Class terminates badly", source, s); + else store(tolower(c)); + } + else if (c == '-' && + (pp - cp) > 1 && *s != ']' && *s != '\0') { + c = pp[-1]; /* Range start */ + pp[-1] = RANGE; /* Range signal */ + store(c); /* Re-store start */ + c = *s++; /* Get end char and*/ + store(tolower(c)); /* Store it */ + } + else { + store(tolower(c)); /* Store normal char */ + } + } + if (c != ']') + badpat("Unterminated class", source, s); + if ((c = (pp - cp)) >= 256) + badpat("Class too large", source, s); + if (c == 0) + badpat("Empty class", source, s); + *cp = c; + return(s); +} + +/*** Store an entry in the pattern buffer **************/ +void store(int op) +{ + if (pp >= &pbuf[PMAX]) + error("Pattern too complex\n"); + *pp++ = op; +} + +/*** Report a bad pattern specification ****************/ +void badpat(char *message, char *source, char *stop) + /* char *message; // Error message */ + /* char *source; // Pattern start */ + /* char *stop; // Pattern end */ +{ + fprintf(stderr, "-GREP-E-%s, pattern is\"%s\"\n", message, source); + fprintf(stderr, "-GREP-E-Stopped at byte %d, '%c'\n", + stop-source, stop[-1]); + error("?GREP-E-Bad pattern\n"); +} + +/*** Scan the file for the pattern in pbuf[] ***********/ +void grep(FILE *fp, char *fn) + /* FILE *fp; // File to process */ + /* char *fn; // File name (for -f option) */ +{ + int lno, count, m; + + lno = 0; + count = 0; + while (fgets(lbuf, LMAX, fp)) { + ++lno; + m = match(); + if ((m && !vflag) || (!m && vflag)) { + ++count; + if (!cflag) { + if (fflag && fn) { + file(fn); + fn = 0; + } + if (nflag) + printf("%d\t", lno); + printf("%s\n", lbuf); + } + } + } + if (cflag) { + if (fflag && fn) + file(fn); + printf("%d\n", count); + } +} + +/*** Match line (lbuf) with pattern (pbuf) return 1 if match ***/ +void match() +{ + char *l; /* Line pointer */ + + for (l = lbuf; *l; ++l) { + if (pmatch(l, pbuf)) + return(1); + } + return(0); +} + +/*** Match partial line with pattern *******************/ +char *pmatch(char *line, char *pattern) + /* char *line; // (partial) line to match */ + /* char *pattern; // (partial) pattern to match */ +{ + char *l; /* Current line pointer */ + char *p; /* Current pattern pointer */ + char c; /* Current character */ + char *e; /* End for STAR and PLUS match */ + int op; /* Pattern operation */ + int n; /* Class counter */ + char *are; /* Start of STAR match */ + + l = line; + if (debug > 1) + printf("pmatch(\"%s\")\n", line); + p = pattern; + while ((op = *p++) != ENDPAT) { + if (debug > 1) + printf("byte[%d] = 0%o, '%c', op = 0%o\n", + l-line, *l, *l, op); + switch(op) { + + case CHAR: + if (tolower(*l++) != *p++) + return(0); + break; + + case BOL: + if (l != lbuf) + return(0); + break; + + case EOL: + if (*l != '\0') + return(0); + break; + + case ANY: + if (*l++ == '\0') + return(0); + break; + + case DIGIT: + if ((c = *l++) < '0' || (c > '9')) + return(0); + break; + + case ALPHA: + c = tolower(*l++); + if (c < 'a' || c > 'z') + return(0); + break; + + case NALPHA: + c = tolower(*l++); + if (c >= 'a' && c <= 'z') + break; + else if (c < '0' || c > '9') + return(0); + break; + + case PUNCT: + c = *l++; + if (c == 0 || c > ' ') + return(0); + break; + + case CLASS: + case NCLASS: + c = tolower(*l++); + n = *p++ & 0377; + do { + if (*p == RANGE) { + p += 3; + n -= 2; + if (c >= p[-2] && c <= p[-1]) + break; + } + else if (c == *p++) + break; + } while (--n > 1); + if ((op == CLASS) == (n <= 1)) + return(0); + if (op == CLASS) + p += n - 2; + break; + + case MINUS: + e = pmatch(l, p); /* Look for a match */ + while (*p++ != ENDPAT); /* Skip over pattern */ + if (e) /* Got a match? */ + l = e; /* Yes, update string */ + break; /* Always succeeds */ + + case PLUS: /* One or more ... */ + if ((l = pmatch(l, p)) == 0) + return(0); /* Gotta have a match */ + case STAR: /* Zero or more ... */ + are = l; /* Remember line start */ + while (*l && (e = pmatch(l, p))) + l = e; /* Get longest match */ + while (*p++ != ENDPAT); /* Skip over pattern */ + while (l >= are) { /* Try to match rest */ + if (e = pmatch(l, p)) + return(e); + --l; /* Nope, try earlier */ + } + return(0); /* Nothing else worked */ + + default: + printf("Bad op code %d\n", op); + error("Cannot happen -- match\n"); + } + } + return(l); +} + +/*** Report an error ***********************************/ +void error(char *s) +{ + fprintf(stderr, "%s", s); + exit(1); +} + +/*** Main program - parse arguments & grep *************/ +int main(int argc, char **argv) +{ + char *p; + int c, i; + int gotpattern; + + FILE *f; + + if (argc <= 1) + usage("No arguments"); + if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) { + help(documentation); + help(patdoc); + return 0; + } + nfile = argc-1; + gotpattern = 0; + for (i=1; i < argc; ++i) { + p = argv[i]; + if (*p == '-') { + ++p; + while (c = *p++) { + switch(tolower(c)) { + + case '?': + help(documentation); + break; + + case 'C': + case 'c': + ++cflag; + break; + + case 'D': + case 'd': + ++debug; + break; + + case 'F': + case 'f': + ++fflag; + break; + + case 'n': + case 'N': + ++nflag; + break; + + case 'v': + case 'V': + ++vflag; + break; + + default: + usage("Unknown flag"); + } + } + argv[i] = 0; + --nfile; + } else if (!gotpattern) { + compile(p); + argv[i] = 0; + ++gotpattern; + --nfile; + } + } + if (!gotpattern) + usage("No pattern"); + if (nfile == 0) + grep(stdin, 0); + else { + fflag = fflag ^ (nfile > 0); + for (i=1; i < argc; ++i) { + if (p = argv[i]) { + if ((f=fopen(p, "r")) == NULL) + cant(p); + else { + grep(f, p); + fclose(f); + } + } + } + } + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/47_switch_return.c b/tests2/47_switch_return.c new file mode 100644 index 00000000..1ec7924c --- /dev/null +++ b/tests2/47_switch_return.c @@ -0,0 +1,24 @@ +#include + +void fred(int x) +{ + switch (x) + { + case 1: printf("1\n"); return; + case 2: printf("2\n"); break; + case 3: printf("3\n"); return; + } + + printf("out\n"); +} + +int main() +{ + fred(1); + fred(2); + fred(3); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/47_switch_return.expect b/tests2/47_switch_return.expect new file mode 100644 index 00000000..b6deb7e9 --- /dev/null +++ b/tests2/47_switch_return.expect @@ -0,0 +1,4 @@ +1 +2 +out +3 diff --git a/tests2/48_nested_break.c b/tests2/48_nested_break.c new file mode 100644 index 00000000..5bc5ba47 --- /dev/null +++ b/tests2/48_nested_break.c @@ -0,0 +1,26 @@ +#include + +int main() +{ + int a; + char b; + + a = 0; + while (a < 2) + { + printf("%d", a++); + break; + + b = 'A'; + while (b < 'C') + { + printf("%c", b++); + } + printf("e"); + } + printf("\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/48_nested_break.expect b/tests2/48_nested_break.expect new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/tests2/48_nested_break.expect @@ -0,0 +1 @@ +0 diff --git a/tests2/49_bracket_evaluation.c b/tests2/49_bracket_evaluation.c new file mode 100644 index 00000000..0cbe57d6 --- /dev/null +++ b/tests2/49_bracket_evaluation.c @@ -0,0 +1,23 @@ +#include + +struct point +{ + double x; + double y; +}; + +struct point point_array[100]; + +int main() +{ + int my_point = 10; + + point_array[my_point].x = 12.34; + point_array[my_point].y = 56.78; + + printf("%f, %f\n", point_array[my_point].x, point_array[my_point].y); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/49_bracket_evaluation.expect b/tests2/49_bracket_evaluation.expect new file mode 100644 index 00000000..1da66dbe --- /dev/null +++ b/tests2/49_bracket_evaluation.expect @@ -0,0 +1 @@ +12.340000, 56.780000 diff --git a/tests2/50_logical_second_arg.c b/tests2/50_logical_second_arg.c new file mode 100644 index 00000000..5fa0bb91 --- /dev/null +++ b/tests2/50_logical_second_arg.c @@ -0,0 +1,29 @@ +#include + +int fred() +{ + printf("fred\n"); + return 0; +} + +int joe() +{ + printf("joe\n"); + return 1; +} + +int main() +{ + printf("%d\n", fred() && joe()); + printf("%d\n", fred() || joe()); + printf("%d\n", joe() && fred()); + printf("%d\n", joe() || fred()); + printf("%d\n", fred() && (1 + joe())); + printf("%d\n", fred() || (0 + joe())); + printf("%d\n", joe() && (0 + fred())); + printf("%d\n", joe() || (1 + fred())); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/50_logical_second_arg.expect b/tests2/50_logical_second_arg.expect new file mode 100644 index 00000000..d6174ae7 --- /dev/null +++ b/tests2/50_logical_second_arg.expect @@ -0,0 +1,20 @@ +fred +0 +fred +joe +1 +joe +fred +0 +joe +1 +fred +0 +fred +joe +1 +joe +fred +0 +joe +1 diff --git a/tests2/51_static.c b/tests2/51_static.c new file mode 100644 index 00000000..d6c0917c --- /dev/null +++ b/tests2/51_static.c @@ -0,0 +1,30 @@ +#include + +static int fred = 1234; +static int joe; + +void henry() +{ + static int fred = 4567; + + printf("%d\n", fred); + fred++; +} + +int main() +{ + printf("%d\n", fred); + henry(); + henry(); + henry(); + henry(); + printf("%d\n", fred); + fred = 8901; + joe = 2345; + printf("%d\n", fred); + printf("%d\n", joe); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/51_static.expect b/tests2/51_static.expect new file mode 100644 index 00000000..18224fa3 --- /dev/null +++ b/tests2/51_static.expect @@ -0,0 +1,8 @@ +1234 +4567 +4568 +4569 +4570 +1234 +8901 +2345 diff --git a/tests2/52_unnamed_enum.c b/tests2/52_unnamed_enum.c new file mode 100644 index 00000000..d0395b20 --- /dev/null +++ b/tests2/52_unnamed_enum.c @@ -0,0 +1,27 @@ +#include + +enum fred { a, b, c }; + +int main() +{ + printf("a=%d\n", a); + printf("b=%d\n", b); + printf("c=%d\n", c); + + enum fred d; + + typedef enum { e, f, g } h; + typedef enum { i, j, k } m; + + printf("e=%d\n", e); + printf("f=%d\n", f); + printf("g=%d\n", g); + + printf("i=%d\n", i); + printf("j=%d\n", j); + printf("k=%d\n", k); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/52_unnamed_enum.expect b/tests2/52_unnamed_enum.expect new file mode 100644 index 00000000..84f2ac84 --- /dev/null +++ b/tests2/52_unnamed_enum.expect @@ -0,0 +1,9 @@ +a=0 +b=1 +c=2 +e=0 +f=1 +g=2 +i=0 +j=1 +k=2 diff --git a/tests2/54_goto.c b/tests2/54_goto.c new file mode 100644 index 00000000..9509e83d --- /dev/null +++ b/tests2/54_goto.c @@ -0,0 +1,56 @@ +#include + +void fred() +{ + printf("In fred()\n"); + goto done; + printf("In middle\n"); +done: + printf("At end\n"); +} + +void joe() +{ + int b = 5678; + + printf("In joe()\n"); + + { + int c = 1234; + printf("c = %d\n", c); + goto outer; + printf("uh-oh\n"); + } + +outer: + + printf("done\n"); +} + +void henry() +{ + int a; + + printf("In henry()\n"); + goto inner; + + { + int b; +inner: + b = 1234; + printf("b = %d\n", b); + } + + printf("done\n"); +} + +int main() +{ + fred(); + joe(); + henry(); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests2/54_goto.expect b/tests2/54_goto.expect new file mode 100644 index 00000000..8e553fa6 --- /dev/null +++ b/tests2/54_goto.expect @@ -0,0 +1,8 @@ +In fred() +At end +In joe() +c = 1234 +done +In henry() +b = 1234 +done diff --git a/tests2/LICENSE b/tests2/LICENSE new file mode 100644 index 00000000..b08a652f --- /dev/null +++ b/tests2/LICENSE @@ -0,0 +1,37 @@ +The tests in this directory are either directly copied from the picoc project or +are subsequently modified and added to for the purpose of TinyCC project. All +these modifications are licensed under the same terms as TinyCC as specified in +the file COPYING. + +=== picoc license === + +Copyright (c) 2009-2011, Zik Saleeba +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + * Neither the name of the Zik Saleeba nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/tests2/Makefile b/tests2/Makefile new file mode 100644 index 00000000..cc13cded --- /dev/null +++ b/tests2/Makefile @@ -0,0 +1,90 @@ +TOP = .. +include $(TOP)/Makefile + +ifeq ($(TARGETOS),Darwin) + CFLAGS+=-Wl,-flat_namespace,-undefined,warning + TCCFLAGS=-D_ANSI_SOURCE + export MACOSX_DEPLOYMENT_TARGET:=10.2 +endif + +ifdef CONFIG_WIN32 + TCCFLAGS=-I $(TOP)/win32/include -L$(TOP) +endif + +TESTS= 00_assignment.test \ + 01_comment.test \ + 02_printf.test \ + 03_struct.test \ + 04_for.test \ + 05_array.test \ + 06_case.test \ + 07_function.test \ + 08_while.test \ + 09_do_while.test \ + 10_pointer.test \ + 11_precedence.test \ + 12_hashdefine.test \ + 13_integer_literals.test \ + 14_if.test \ + 15_recursion.test \ + 16_nesting.test \ + 17_enum.test \ + 18_include.test \ + 19_pointer_arithmetic.test \ + 20_pointer_comparison.test \ + 21_char_array.test \ + 22_floating_point.test \ + 23_type_coercion.test \ + 24_math_library.test \ + 25_quicksort.test \ + 26_character_constants.test \ + 27_sizeof.test \ + 28_strings.test \ + 29_array_address.test \ + 31_args.test \ + 32_led.test \ + 33_ternary_op.test \ + 35_sizeof.test \ + 36_array_initialisers.test \ + 37_sprintf.test \ + 38_multiple_array_index.test \ + 39_typedef.test \ + 40_stdio.test \ + 41_hashif.test \ + 42_function_pointer.test \ + 43_void_param.test \ + 44_scoped_declarations.test \ + 45_empty_for.test \ + 47_switch_return.test \ + 48_nested_break.test \ + 49_bracket_evaluation.test \ + 50_logical_second_arg.test \ + 51_static.test \ + 52_unnamed_enum.test \ + 54_goto.test + + # 30_hanoi.test \ # seg fault in the code, gcc as well + # 34_array_assignment.test \ # array assignment is not in C standard + # 46_grep.test \ # does not compile even with gcc + +%.test: %.expect %.c + @echo Test: $*... + -@if [ "x`echo $* | grep args`" != "x" ]; \ + then \ + ../tcc -B.. $(TCCFLAGS) -run $*.c - arg1 arg2 arg3 arg4 2>&1 >$*.output; \ + else \ + ../tcc -B.. $(TCCFLAGS) -run $*.c 2>&1 >$*.output; \ + fi + @if diff -bu $*.expect $*.output ; \ + then \ + rm -f $*.output \ + : \ + else \ + echo "ERROR: test $*"; \ + fi + +all: test + +test: $(TESTS) + +# vim: set expandtab ts=4 sw=4 sts=4 tw=80 :