From 46e79e69ae866df88cb9442a7329c72662ed2db3 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 4 Oct 2025 15:33:07 +0900 Subject: refactor: rename test files --- tests/arithmetic_operators.sh | 11 ++ tests/array_basics.sh | 163 ++++++++++++++++++++++++++ tests/assignment_operators.sh | 64 +++++++++++ tests/bitwise_operators.sh | 40 +++++++ tests/bool_type.sh | 12 ++ tests/cast_expressions.sh | 154 +++++++++++++++++++++++++ tests/char_literals.sh | 25 ++++ tests/comma_operator.sh | 37 ++++++ tests/command_line_args.sh | 19 ++++ tests/comments.sh | 25 ++++ tests/comparison_operators.sh | 14 +++ tests/compile_errors.sh | 33 ++++++ tests/compiler_output.sh | 17 +++ tests/compiler_version.sh | 7 ++ tests/conditional_errors.sh | 74 ++++++++++++ tests/define_macros.sh | 92 +++++++++++++++ tests/do_while.sh | 92 +++++++++++++++ tests/empty_preprocessor.sh | 10 ++ tests/empty_statements.sh | 7 ++ tests/enums.sh | 48 ++++++++ tests/error_directive.sh | 27 +++++ tests/example_programs.sh | 133 ++++++++++++++++++++++ tests/file_io.sh | 31 +++++ tests/for_loops.sh | 141 +++++++++++++++++++++++ tests/function_basics.sh | 58 ++++++++++ tests/function_macros.sh | 110 ++++++++++++++++++ tests/function_pointers.sh | 24 ++++ tests/global_variables.sh | 39 +++++++ tests/goto.sh | 168 +++++++++++++++++++++++++++ tests/identifiers.sh | 16 +++ tests/if_elif_else.sh | 215 +++++++++++++++++++++++++++++++++++ tests/if_else.sh | 21 ++++ tests/ifdef_ifndef.sh | 95 ++++++++++++++++ tests/implicit_return.sh | 19 ++++ tests/include_errors.sh | 52 +++++++++ tests/include_guards.sh | 26 +++++ tests/include_local.sh | 100 ++++++++++++++++ tests/increment_operators.sh | 29 +++++ tests/input_operations.sh | 37 ++++++ tests/invalid_directives.sh | 28 +++++ tests/keywords.sh | 14 +++ tests/line_continuation.sh | 57 ++++++++++ tests/local_variables.sh | 56 +++++++++ tests/logical_operators.sh | 68 +++++++++++ tests/macro_operators.sh | 89 +++++++++++++++ tests/nested_conditionals.sh | 12 ++ tests/pointer_basics.sh | 80 +++++++++++++ tests/predefined_macros.sh | 55 +++++++++ tests/preprocess_flag.sh | 122 ++++++++++++++++++++ tests/preprocessor_comments.sh | 11 ++ tests/printf.sh | 220 ++++++++++++++++++++++++++++++++++++ tests/recursive_functions.sh | 15 +++ tests/short_type.sh | 17 +++ tests/sizeof_operator.sh | 177 +++++++++++++++++++++++++++++ tests/stdlib_declarations.sh | 22 ++++ tests/string_operations.sh | 57 ++++++++++ tests/struct_arrays.sh | 34 ++++++ tests/struct_basics.sh | 121 ++++++++++++++++++++ tests/struct_initialization.sh | 108 ++++++++++++++++++ tests/switch.sh | 212 ++++++++++++++++++++++++++++++++++ tests/ternary_operator.sh | 8 ++ tests/test_arithmetic_operators.sh | 11 -- tests/test_array_basics.sh | 163 -------------------------- tests/test_assignment_operators.sh | 64 ----------- tests/test_bitwise_operators.sh | 40 ------- tests/test_bool_type.sh | 12 -- tests/test_cast_expressions.sh | 154 ------------------------- tests/test_char_literals.sh | 25 ---- tests/test_comma_operator.sh | 37 ------ tests/test_command_line_args.sh | 19 ---- tests/test_comments.sh | 25 ---- tests/test_comparison_operators.sh | 14 --- tests/test_compile_errors.sh | 33 ------ tests/test_compiler_output.sh | 17 --- tests/test_compiler_version.sh | 7 -- tests/test_conditional_errors.sh | 74 ------------ tests/test_define_macros.sh | 92 --------------- tests/test_do_while.sh | 92 --------------- tests/test_empty_preprocessor.sh | 10 -- tests/test_empty_statements.sh | 7 -- tests/test_enums.sh | 48 -------- tests/test_error_directive.sh | 27 ----- tests/test_example_programs.sh | 133 ---------------------- tests/test_file_io.sh | 31 ----- tests/test_for_loops.sh | 141 ----------------------- tests/test_function_basics.sh | 58 ---------- tests/test_function_macros.sh | 110 ------------------ tests/test_function_pointers.sh | 24 ---- tests/test_global_variables.sh | 39 ------- tests/test_goto.sh | 168 --------------------------- tests/test_identifiers.sh | 16 --- tests/test_if_elif_else.sh | 215 ----------------------------------- tests/test_if_else.sh | 21 ---- tests/test_ifdef_ifndef.sh | 95 ---------------- tests/test_implicit_return.sh | 19 ---- tests/test_include_errors.sh | 52 --------- tests/test_include_guards.sh | 26 ----- tests/test_include_local.sh | 100 ---------------- tests/test_increment_operators.sh | 29 ----- tests/test_input_operations.sh | 37 ------ tests/test_invalid_directives.sh | 28 ----- tests/test_keywords.sh | 14 --- tests/test_line_continuation.sh | 57 ---------- tests/test_local_variables.sh | 56 --------- tests/test_logical_operators.sh | 68 ----------- tests/test_macro_operators.sh | 89 --------------- tests/test_nested_conditionals.sh | 12 -- tests/test_pointer_basics.sh | 80 ------------- tests/test_predefined_macros.sh | 55 --------- tests/test_preprocess_flag.sh | 122 -------------------- tests/test_preprocessor_comments.sh | 11 -- tests/test_printf.sh | 220 ------------------------------------ tests/test_recursive_functions.sh | 15 --- tests/test_short_type.sh | 17 --- tests/test_sizeof_operator.sh | 177 ----------------------------- tests/test_stdlib_declarations.sh | 22 ---- tests/test_string_operations.sh | 57 ---------- tests/test_struct_arrays.sh | 34 ------ tests/test_struct_basics.sh | 121 -------------------- tests/test_struct_initialization.sh | 108 ------------------ tests/test_switch.sh | 212 ---------------------------------- tests/test_ternary_operator.sh | 8 -- tests/test_typedef_struct.sh | 85 -------------- tests/test_undef.sh | 33 ------ tests/test_unions.sh | 23 ---- tests/test_variadic_functions.sh | 32 ------ tests/test_void_functions.sh | 49 -------- tests/typedef_struct.sh | 85 ++++++++++++++ tests/undef.sh | 33 ++++++ tests/unions.sh | 23 ++++ tests/variadic_functions.sh | 32 ++++++ tests/void_functions.sh | 49 ++++++++ 132 files changed, 4090 insertions(+), 4090 deletions(-) create mode 100644 tests/arithmetic_operators.sh create mode 100644 tests/array_basics.sh create mode 100644 tests/assignment_operators.sh create mode 100644 tests/bitwise_operators.sh create mode 100644 tests/bool_type.sh create mode 100644 tests/cast_expressions.sh create mode 100644 tests/char_literals.sh create mode 100644 tests/comma_operator.sh create mode 100644 tests/command_line_args.sh create mode 100644 tests/comments.sh create mode 100644 tests/comparison_operators.sh create mode 100644 tests/compile_errors.sh create mode 100644 tests/compiler_output.sh create mode 100644 tests/compiler_version.sh create mode 100644 tests/conditional_errors.sh create mode 100644 tests/define_macros.sh create mode 100644 tests/do_while.sh create mode 100644 tests/empty_preprocessor.sh create mode 100644 tests/empty_statements.sh create mode 100644 tests/enums.sh create mode 100644 tests/error_directive.sh create mode 100644 tests/example_programs.sh create mode 100644 tests/file_io.sh create mode 100644 tests/for_loops.sh create mode 100644 tests/function_basics.sh create mode 100644 tests/function_macros.sh create mode 100644 tests/function_pointers.sh create mode 100644 tests/global_variables.sh create mode 100644 tests/goto.sh create mode 100644 tests/identifiers.sh create mode 100644 tests/if_elif_else.sh create mode 100644 tests/if_else.sh create mode 100644 tests/ifdef_ifndef.sh create mode 100644 tests/implicit_return.sh create mode 100644 tests/include_errors.sh create mode 100644 tests/include_guards.sh create mode 100644 tests/include_local.sh create mode 100644 tests/increment_operators.sh create mode 100644 tests/input_operations.sh create mode 100644 tests/invalid_directives.sh create mode 100644 tests/keywords.sh create mode 100644 tests/line_continuation.sh create mode 100644 tests/local_variables.sh create mode 100644 tests/logical_operators.sh create mode 100644 tests/macro_operators.sh create mode 100644 tests/nested_conditionals.sh create mode 100644 tests/pointer_basics.sh create mode 100644 tests/predefined_macros.sh create mode 100644 tests/preprocess_flag.sh create mode 100644 tests/preprocessor_comments.sh create mode 100644 tests/printf.sh create mode 100644 tests/recursive_functions.sh create mode 100644 tests/short_type.sh create mode 100644 tests/sizeof_operator.sh create mode 100644 tests/stdlib_declarations.sh create mode 100644 tests/string_operations.sh create mode 100644 tests/struct_arrays.sh create mode 100644 tests/struct_basics.sh create mode 100644 tests/struct_initialization.sh create mode 100644 tests/switch.sh create mode 100644 tests/ternary_operator.sh delete mode 100644 tests/test_arithmetic_operators.sh delete mode 100644 tests/test_array_basics.sh delete mode 100644 tests/test_assignment_operators.sh delete mode 100644 tests/test_bitwise_operators.sh delete mode 100644 tests/test_bool_type.sh delete mode 100644 tests/test_cast_expressions.sh delete mode 100644 tests/test_char_literals.sh delete mode 100644 tests/test_comma_operator.sh delete mode 100644 tests/test_command_line_args.sh delete mode 100644 tests/test_comments.sh delete mode 100644 tests/test_comparison_operators.sh delete mode 100644 tests/test_compile_errors.sh delete mode 100644 tests/test_compiler_output.sh delete mode 100644 tests/test_compiler_version.sh delete mode 100644 tests/test_conditional_errors.sh delete mode 100644 tests/test_define_macros.sh delete mode 100644 tests/test_do_while.sh delete mode 100644 tests/test_empty_preprocessor.sh delete mode 100644 tests/test_empty_statements.sh delete mode 100644 tests/test_enums.sh delete mode 100644 tests/test_error_directive.sh delete mode 100644 tests/test_example_programs.sh delete mode 100644 tests/test_file_io.sh delete mode 100644 tests/test_for_loops.sh delete mode 100644 tests/test_function_basics.sh delete mode 100644 tests/test_function_macros.sh delete mode 100644 tests/test_function_pointers.sh delete mode 100644 tests/test_global_variables.sh delete mode 100644 tests/test_goto.sh delete mode 100644 tests/test_identifiers.sh delete mode 100644 tests/test_if_elif_else.sh delete mode 100644 tests/test_if_else.sh delete mode 100644 tests/test_ifdef_ifndef.sh delete mode 100644 tests/test_implicit_return.sh delete mode 100644 tests/test_include_errors.sh delete mode 100644 tests/test_include_guards.sh delete mode 100644 tests/test_include_local.sh delete mode 100644 tests/test_increment_operators.sh delete mode 100644 tests/test_input_operations.sh delete mode 100644 tests/test_invalid_directives.sh delete mode 100644 tests/test_keywords.sh delete mode 100644 tests/test_line_continuation.sh delete mode 100644 tests/test_local_variables.sh delete mode 100644 tests/test_logical_operators.sh delete mode 100644 tests/test_macro_operators.sh delete mode 100644 tests/test_nested_conditionals.sh delete mode 100644 tests/test_pointer_basics.sh delete mode 100644 tests/test_predefined_macros.sh delete mode 100644 tests/test_preprocess_flag.sh delete mode 100644 tests/test_preprocessor_comments.sh delete mode 100644 tests/test_printf.sh delete mode 100644 tests/test_recursive_functions.sh delete mode 100644 tests/test_short_type.sh delete mode 100644 tests/test_sizeof_operator.sh delete mode 100644 tests/test_stdlib_declarations.sh delete mode 100644 tests/test_string_operations.sh delete mode 100644 tests/test_struct_arrays.sh delete mode 100644 tests/test_struct_basics.sh delete mode 100644 tests/test_struct_initialization.sh delete mode 100644 tests/test_switch.sh delete mode 100644 tests/test_ternary_operator.sh delete mode 100644 tests/test_typedef_struct.sh delete mode 100644 tests/test_undef.sh delete mode 100644 tests/test_unions.sh delete mode 100644 tests/test_variadic_functions.sh delete mode 100644 tests/test_void_functions.sh create mode 100644 tests/typedef_struct.sh create mode 100644 tests/undef.sh create mode 100644 tests/unions.sh create mode 100644 tests/variadic_functions.sh create mode 100644 tests/void_functions.sh (limited to 'tests') diff --git a/tests/arithmetic_operators.sh b/tests/arithmetic_operators.sh new file mode 100644 index 0000000..a98ca15 --- /dev/null +++ b/tests/arithmetic_operators.sh @@ -0,0 +1,11 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + ASSERT_EQ(42, 42); + ASSERT_EQ(21, 5+20-4); + ASSERT_EQ(26, 2*3+4*5); + ASSERT_EQ(197, (((3+5)/2) + (5*(9-6)) * (5+6*7)) % 256); + ASSERT_EQ(30, (-10 + 20 * -3) + 100); +} +EOF diff --git a/tests/array_basics.sh b/tests/array_basics.sh new file mode 100644 index 0000000..75bb244 --- /dev/null +++ b/tests/array_basics.sh @@ -0,0 +1,163 @@ +cat <<'EOF' > expected +42 +EOF +test_diff <<'EOF' +int printf(); +void* calloc(); + +int main() { + int b; + int* a = &b; + a[0] = 42; + printf("%d\n", *a); + return 0; +} +EOF + +cat <<'EOF' > expected +0 0 +1 1 +2 2 +3 3 +4 4 +5 5 +6 6 +7 7 +8 8 +9 9 +EOF +test_diff <<'EOF' +int printf(); +void* calloc(); + +int main() { + long* a = calloc(10, sizeof(long)); + long i = 0; + for (i = 0; i < 10; i = i + 1) { + a[i] = i; + } + for (i = 0; i < 10; i = i + 1) { + printf("%d %d\n", *(a + i), a[i]); + } + return 0; +} +EOF + +cat <<'EOF' > expected +65 +65 +66 +67 +68 +EOF +test_diff <<'EOF' +void* calloc(); +int printf(); + +int main() { + char* source = calloc(4, sizeof(char)); + + source[0] = 'A'; + source[1] = 'B'; + source[2] = 'C'; + source[3] = 'D'; + + int a = source[0]; + + printf("%d\n", a); + printf("%d\n", source[0]); + printf("%d\n", source[1]); + printf("%d\n", source[2]); + printf("%d\n", source[3]); + + return 0; +} +EOF + +cat <<'EOF' > expected +0 +1 +4 +9 +16 +25 +36 +49 +64 +81 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + int i; + int a[10]; + for (i = 0; i < 10; ++i) { + a[i] = i * i; + } + for (i = 0; i < 10; ++i) { + printf("%d\n", a[i]); + } +} +EOF + +cat <<'EOF' > expected +0 +42 +48 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +123 +EOF + +test_diff <<'EOF' +int printf(); + +int a; +int b[12]; + +int main() { + printf("%d\n", a); + a = 42; + printf("%d\n", a); + printf("%zu\n", sizeof(b)); + int i; + for (i = 0; i < 12; ++i) { + printf("%d\n", b[i]); + } + b[11] = 123; + printf("%d\n", b[11]); +} +EOF + +cat <<'EOF' > expected +400 +80 +16 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + int a[10 * 10]; + int b[10 + 10]; + int c[1 << 2]; + + printf("%zu\n", sizeof(a)); + printf("%zu\n", sizeof(b)); + printf("%zu\n", sizeof(c)); +} +EOF + diff --git a/tests/assignment_operators.sh b/tests/assignment_operators.sh new file mode 100644 index 0000000..9b3a33e --- /dev/null +++ b/tests/assignment_operators.sh @@ -0,0 +1,64 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int i = 0; + for (; i < 5; i += 1) {} + ASSERT_EQ(5, i); + + for (i = 5; i >= 0; i -= 1); + ASSERT_EQ(-1, i); + + int x = 123; + x *= 456; + ASSERT_EQ(56088, x); + + int y = 120; + y /= 5; + ASSERT_EQ(24, y); + + int z = 17; + z %= 7; + ASSERT_EQ(3, z); + + int a = 0x05; + a |= 0x0A; + ASSERT_EQ(0x0F, a); + + int b = 0x0F; + b &= 0x0A; + ASSERT_EQ(0x0A, b); + + int c = 7; + c |= 8; + ASSERT_EQ(15, c); + + int d = 15; + d &= 6; + ASSERT_EQ(6, d); + + int e = 0x0F; + e ^= 0x05; + ASSERT_EQ(0x0A, e); + + int f = 3; + f <<= 2; + ASSERT_EQ(12, f); + + int g = 16; + g >>= 2; + ASSERT_EQ(4, g); + + int h = -16; + h >>= 2; + ASSERT_EQ(-4, h); + + int j = 1; + j <<= 4; + ASSERT_EQ(16, j); + + int k = 64; + k >>= 3; + ASSERT_EQ(8, k); +} +EOF diff --git a/tests/bitwise_operators.sh b/tests/bitwise_operators.sh new file mode 100644 index 0000000..f683099 --- /dev/null +++ b/tests/bitwise_operators.sh @@ -0,0 +1,40 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + ASSERT_EQ(123, 0 | 123); + ASSERT_EQ(460, 12 | 456); + + ASSERT_EQ(8, 1 << 3); + ASSERT_EQ(336, 21 << 4); + ASSERT_EQ(13, 111 >> 3); + ASSERT_EQ(0, 15 >> 14); + + int a = 5; + int b = 3; + ASSERT_EQ(1, a & b); + ASSERT_EQ(7, a | b); + ASSERT_EQ(6, a ^ b); + ASSERT_EQ(4, 2 + 3 & 4); + + int c = 1 + 2 & 3; + int d = 4 & 5 ^ 6; + int e = 1 ^ 2 | 3; + int f = 0 | 1 & 2; + ASSERT_EQ(3, c); + ASSERT_EQ(2, d); + ASSERT_EQ(3, e); + ASSERT_EQ(0, f); + + ASSERT_EQ(-1, ~0); + ASSERT_EQ(-2, ~1); + ASSERT_EQ(-6, ~5); + ASSERT_EQ(0, ~(-1)); + ASSERT_EQ(5, ~(-6)); + + int x = 10; + ASSERT_EQ(-11, ~x); + ASSERT_EQ(-1, ~(x & 0)); + ASSERT_EQ(-16, ~(x | 5)); +} +EOF diff --git a/tests/bool_type.sh b/tests/bool_type.sh new file mode 100644 index 0000000..d18fd30 --- /dev/null +++ b/tests/bool_type.sh @@ -0,0 +1,12 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + bool b1 = true, b0 = false; + ASSERT_EQ(1, b1); + ASSERT_EQ(0, b0); + ASSERT_EQ(1, sizeof(b1)); + ASSERT_EQ(1, sizeof(b0)); + ASSERT_EQ(1, sizeof(bool)); +} +EOF diff --git a/tests/cast_expressions.sh b/tests/cast_expressions.sh new file mode 100644 index 0000000..f6824ad --- /dev/null +++ b/tests/cast_expressions.sh @@ -0,0 +1,154 @@ +cat <<'EOF' > expected +65 +65 +127 +1 +42 +99 +10 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + char c = 65; + int i = (int)c; + printf("%d\n", i); + + int i2 = 321; + char c2 = (char)i2; + printf("%d\n", c2); + + short s = 127; + int i3 = (int)s; + printf("%d\n", i3); + + int i4 = 65537; + short s2 = (short)i4; + printf("%d\n", s2); + + long l = 42; + int i5 = (int)l; + printf("%d\n", i5); + + int i6 = 99; + long l2 = (long)i6; + printf("%d\n", (int)l2); + + char c3 = 10; + short s3 = (short)c3; + int i7 = (int)s3; + long l3 = (long)i7; + printf("%d\n", (int)l3); + + return 0; +} +EOF + +cat <<'EOF' > expected +Result: 130 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + char c = 65; + int result = (int)c + (int)c; + printf("Result: %d\n", result); + return 0; +} +EOF + +cat <<'EOF' > expected +10 +20 +30 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + char a = 5; + char b = 5; + int sum = (int)a + (int)b; + printf("%d\n", sum); + + short s1 = 10; + short s2 = 10; + int sum2 = (int)s1 + (int)s2; + printf("%d\n", sum2); + + long l1 = 15; + long l2 = 15; + int sum3 = (int)(l1 + l2); + printf("%d\n", sum3); + + return 0; +} +EOF + +cat <<'EOF' > expected +10 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + char c = -10; + int i = (int)c; + printf("%d\n", -i); + return 0; +} +EOF + +cat <<'EOF' > expected +Char: 65 +Int: 65 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +char get_char() { + return 65; +} + +int main() { + char c = get_char(); + int i = (int)get_char(); + printf("Char: %d\n", c); + printf("Int: %d\n", i); + return 0; +} +EOF + +cat <<'EOF' > expected +Equal +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + char c = 42; + int i = 42; + if ((int)c == i) { + printf("Equal\n"); + } else { + printf("Not equal\n"); + } + return 0; +} +EOF + +cat <<'EOF' > expected +55 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + long l = 55; + char c = (char)(short)(int)l; + printf("%d\n", c); + return 0; +} +EOF diff --git a/tests/char_literals.sh b/tests/char_literals.sh new file mode 100644 index 0000000..c883eba --- /dev/null +++ b/tests/char_literals.sh @@ -0,0 +1,25 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + ASSERT_EQ(97, 'a'); + ASSERT_EQ(48, '0'); + ASSERT_EQ(92, '\\'); + ASSERT_EQ(39, '\''); + ASSERT_EQ(10, '\n'); + + ASSERT_EQ(39, '\''); + ASSERT_EQ(34, '\"'); + ASSERT_EQ(63, '\?'); + ASSERT_EQ(92, '\\'); + ASSERT_EQ(7, '\a'); + ASSERT_EQ(8, '\b'); + ASSERT_EQ(12, '\f'); + ASSERT_EQ(10, '\n'); + ASSERT_EQ(13, '\r'); + ASSERT_EQ(9, '\t'); + ASSERT_EQ(11, '\v'); + + ASSERT_EQ(0, '\0'); +} +EOF diff --git a/tests/comma_operator.sh b/tests/comma_operator.sh new file mode 100644 index 0000000..7c53845 --- /dev/null +++ b/tests/comma_operator.sh @@ -0,0 +1,37 @@ +cat <<'EOF' > expected +0 0 +1 1 +2 2 +3 3 +4 4 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + int i = 1000; + int j = 1000; + for (i = 0, j = 0; i < 5; i++, j++) { + printf("%d %d\n", i, j); + } +} +EOF + +cat <<'EOF' > expected +1 2 3 4 +0 0 5 +EOF + +test_diff <<'EOF' +int printf(); +int x, y, z = 5; +int main() { + int a, b; + a = 1, b = 2; + int c = 3, d = 4; + printf("%d %d %d %d\n", a, b, c, d); + printf("%d %d %d\n", x, y, z); +} +EOF + diff --git a/tests/command_line_args.sh b/tests/command_line_args.sh new file mode 100644 index 0000000..094b36c --- /dev/null +++ b/tests/command_line_args.sh @@ -0,0 +1,19 @@ +cat <<'EOF' > expected +argc = 4 +argv[1] = hoge +argv[2] = piyo +argv[3] = fuga +EOF +test_diff hoge piyo fuga<<'EOF' +int printf(); + +int main(int argc, char** argv) { + printf("argc = %d\n", argc); + int i; + for (i = 1; i < argc; ++i) { + printf("argv[%d] = %s\n", i, argv[i]); + } + return 0; +} +EOF + diff --git a/tests/comments.sh b/tests/comments.sh new file mode 100644 index 0000000..7a43832 --- /dev/null +++ b/tests/comments.sh @@ -0,0 +1,25 @@ +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +// TODO: check if the token is at the beginning of line. +// TODO: check if skipped whitespaces do not contain line breaks. +int main() { + return 0; +} +EOF + +cat <<'EOF' > expected +EOF + +test_diff <<'EOF' +/* lorem ipsum */ +/* +lorem ipsum +*/ +// /* +/* /* + */ +int/**/main() { +} +EOF + diff --git a/tests/comparison_operators.sh b/tests/comparison_operators.sh new file mode 100644 index 0000000..7bcb689 --- /dev/null +++ b/tests/comparison_operators.sh @@ -0,0 +1,14 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + ASSERT_EQ(1, 0 == 0); + ASSERT_EQ(0, 123 != 123); + ASSERT_EQ(1, 123 != 456); + ASSERT_EQ(0, 123 == 124); + ASSERT_EQ(1, 123 < 567); + ASSERT_EQ(1, 123 <= 567); + ASSERT_EQ(1, 123 <= 123); + ASSERT_EQ(0, 123 < 123); +} +EOF diff --git a/tests/compile_errors.sh b/tests/compile_errors.sh new file mode 100644 index 0000000..23bf394 --- /dev/null +++ b/tests/compile_errors.sh @@ -0,0 +1,33 @@ +cat <<'EOF' > expected +undefined function: f +EOF +test_compile_error <<'EOF' +int main() { + f(); +} +EOF + +# TODO: improve error message +# cat <<'EOF' > expected +# main.c:1: expected ';' or '{', but got '}' +# EOF +cat <<'EOF' > expected +main.c:1: expected ';', but got '}' +EOF + +test_compile_error <<'EOF' +int main() } +EOF + +# TODO: improve error message +# cat <<'EOF' > expected +# main.c:1: expected ';' or '{', but got '}' +# EOF +cat <<'EOF' > expected +main.c:1: expected ';', but got '123' +EOF + +test_compile_error <<'EOF' +int main() 123 +EOF + diff --git a/tests/compiler_output.sh b/tests/compiler_output.sh new file mode 100644 index 0000000..6993c36 --- /dev/null +++ b/tests/compiler_output.sh @@ -0,0 +1,17 @@ +cat > foo.c <<'EOF' +int main() {} +EOF + +"$ducc" -o bar.s foo.c +if [[ $? -ne 0 ]]; then + exit 1 +fi +gcc -o a.out bar.s +./a.out "$@" +exit_code=$? + +if [[ $exit_code -ne 0 ]]; then + echo "invalid exit code: $exit_code" >&2 + exit 1 +fi + diff --git a/tests/compiler_version.sh b/tests/compiler_version.sh new file mode 100644 index 0000000..1e22718 --- /dev/null +++ b/tests/compiler_version.sh @@ -0,0 +1,7 @@ +expected="ducc v0.2.0" + +if [[ "$("$ducc" --version)" != "$expected" ]]; then + echo "invalid output" >&2 + exit 1 +fi + diff --git a/tests/conditional_errors.sh b/tests/conditional_errors.sh new file mode 100644 index 0000000..5a33e8c --- /dev/null +++ b/tests/conditional_errors.sh @@ -0,0 +1,74 @@ +cat <<'EOF' > expected +main.c:2: expected '#endif', but got '' +EOF +test_compile_error <<'EOF' +#if 0 +EOF + +cat <<'EOF' > expected +main.c:3: expected '#endif', but got '' +EOF +test_compile_error <<'EOF' +#if 1 +#else +EOF + +cat <<'EOF' > expected +main.c:3: expected '#endif', but got '' +EOF +test_compile_error <<'EOF' +#if 1 +#elif 1 +EOF + +cat <<'EOF' > expected +main.c:1: unexpected '#elif'; no corresponding '#if'* +EOF +test_compile_error <<'EOF' +#elif +#endif + +int main() {} +EOF + +cat <<'EOF' > expected +main.c:1: unexpected '#endif'; no corresponding '#if'* +EOF +test_compile_error <<'EOF' +#endif + +int main() {} +EOF + +cat <<'EOF' > expected +main.c:1: unexpected '#else'; no corresponding '#if'* +EOF +test_compile_error <<'EOF' +#else +#endif + +int main() {} +EOF + +cat <<'EOF' > expected +main.c:4: expected '#endif', but got '' +EOF +test_compile_error <<'EOF' +#if 1 + +int main() {} +EOF + +cat <<'EOF' > a.h +#if 1 +EOF +cat <<'EOF' > expected +./a.h:2: expected '#endif', but got '' +EOF +test_compile_error <<'EOF' +#include "a.h" +#endif + +int main() {} +EOF + diff --git a/tests/define_macros.sh b/tests/define_macros.sh new file mode 100644 index 0000000..1b674f1 --- /dev/null +++ b/tests/define_macros.sh @@ -0,0 +1,92 @@ +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +#define A 1 +int main() { + return 0; +} +EOF + +cat <<'EOF' > expected +1,2,3 +EOF +test_diff <<'EOF' +int printf(); + +#define A 1 +#define B 2 +#define C 3 + +int main() { + printf("%d,%d,%d\n", A, B, C); + return 0; +} +EOF + +cat <<'EOF' > expected +0,0,0,0 +EOF +test_diff <<'EOF' +int printf(); + +#define NULL 0 +#define TK_EOF 0 +#define TY_UNKNOWN 0 +#define AST_UNKNOWN 0 + +int main() { + printf("%d,%d,%d,%d\n", NULL, TK_EOF, TY_UNKNOWN, AST_UNKNOWN); + return 0; +} +EOF + +cat <<'EOF' > expected +42,123 +EOF +test_diff <<'EOF' +int printf(); + +#define A foo_a +#define B foo_b + +int main() { + int foo_a = 42; + int foo_b = 123; + + printf("%d,%d\n", A, B); + + return 0; +} +EOF + +cat <<'EOF' > expected +123 +EOF + +test_diff <<'EOF' +#define foo() 123 + +int printf(); + +int main() { + printf("%d\n", foo()); +} +EOF + +cat <<'EOF' > expected +579 +975 +EOF + +test_diff <<'EOF' +#define foo 123 + 456 +#define bar() 321 + 654 + +int printf(); + +int main() { + printf("%d\n", foo); + printf("%d\n", bar()); +} +EOF + diff --git a/tests/do_while.sh b/tests/do_while.sh new file mode 100644 index 0000000..dbb4815 --- /dev/null +++ b/tests/do_while.sh @@ -0,0 +1,92 @@ +cat <<'EOF' > expected +body 0 +foo 1 +body 1 +foo 2 +body 2 +foo 3 +body 3 +foo 4 +body 4 +foo 5 +EOF +test_diff <<'EOF' +int printf(); + +int foo(int i) { + printf("foo %d\n", i); + return i; +} + +int main() { + int i = 0; + do { + printf("body %d\n", i); + ++i; + } while (foo(i) < 5); + + return 0; +} +EOF + +cat <<'EOF' > expected +body 0 +foo 1 +body 1 +foo 2 +body 2 +EOF +test_diff <<'EOF' +int printf(); + +int foo(int i) { + printf("foo %d\n", i); + return i; +} + +int main() { + int i = 0; + do { + printf("body %d\n", i); + ++i; + if (i == 3) { + break; + } + } while (foo(i) < 5); + + return 0; +} +EOF + +cat <<'EOF' > expected +body 1 +foo 1 +foo 2 +body 3 +foo 3 +foo 4 +body 5 +foo 5 +EOF +test_diff <<'EOF' +int printf(); + +int foo(int i) { + printf("foo %d\n", i); + return i; +} + +int main() { + int i = 0; + do { + ++i; + if (i % 2 == 0) { + continue; + } + printf("body %d\n", i); + } while (foo(i) < 5); + + return 0; +} +EOF + diff --git a/tests/empty_preprocessor.sh b/tests/empty_preprocessor.sh new file mode 100644 index 0000000..57e33ad --- /dev/null +++ b/tests/empty_preprocessor.sh @@ -0,0 +1,10 @@ +touch expected +test_diff <<'EOF' +# +# +# +# +# +int main() {} +EOF + diff --git a/tests/empty_statements.sh b/tests/empty_statements.sh new file mode 100644 index 0000000..71caed5 --- /dev/null +++ b/tests/empty_statements.sh @@ -0,0 +1,7 @@ +cat <<'EOF' > expected +EOF + +test_diff <<'EOF' +int main() { ; } +EOF + diff --git a/tests/enums.sh b/tests/enums.sh new file mode 100644 index 0000000..3fc15a0 --- /dev/null +++ b/tests/enums.sh @@ -0,0 +1,48 @@ +cat <<'EOF' > expected +4 +0,1,2 +EOF +test_diff <<'EOF' +int printf(); + +enum E { + A, + B, + C, +}; + +int main() { + enum E x = A; + printf("%d\n", sizeof(enum E)); + printf("%d,%d,%d\n", A, B, C); + return 0; +} +EOF + +cat <<'EOF' > expected +10,11,20,21 +0,5,6,6 +EOF + +test_diff <<'EOF' +int printf(); + +enum E1 { + A = 10, + B, + C = 20, + D, +}; + +enum E2 { + E, + F = 5, + G, + H = G, +}; + +int main() { + printf("%d,%d,%d,%d\n", A, B, C, D); + printf("%d,%d,%d,%d\n", E, F, G, H); +} +EOF diff --git a/tests/error_directive.sh b/tests/error_directive.sh new file mode 100644 index 0000000..a8b3fdb --- /dev/null +++ b/tests/error_directive.sh @@ -0,0 +1,27 @@ +cat <<'EOF' > expected +main.c:1: foo +EOF +test_compile_error <<'EOF' +#error "foo" +EOF + +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +#if 0 +#error "foo" +#endif +int main() {} +EOF + +cat <<'EOF' > expected +main.c:5: foo +EOF +test_compile_error <<'EOF' +#define \ + A \ + B + +#error "foo" +EOF + diff --git a/tests/example_programs.sh b/tests/example_programs.sh new file mode 100644 index 0000000..21b7ca4 --- /dev/null +++ b/tests/example_programs.sh @@ -0,0 +1,133 @@ +cat <<'EOF' > expected +1 +2 +Fizz +4 +Buzz +Fizz +7 +8 +Fizz +Buzz +11 +Fizz +13 +14 +FizzBuzz +16 +17 +Fizz +19 +Buzz +Fizz +22 +23 +Fizz +Buzz +26 +Fizz +28 +29 +FizzBuzz +31 +32 +Fizz +34 +Buzz +Fizz +37 +38 +Fizz +Buzz +41 +Fizz +43 +44 +FizzBuzz +46 +47 +Fizz +49 +Buzz +Fizz +52 +53 +Fizz +Buzz +56 +Fizz +58 +59 +FizzBuzz +61 +62 +Fizz +64 +Buzz +Fizz +67 +68 +Fizz +Buzz +71 +Fizz +73 +74 +FizzBuzz +76 +77 +Fizz +79 +Buzz +Fizz +82 +83 +Fizz +Buzz +86 +Fizz +88 +89 +FizzBuzz +91 +92 +Fizz +94 +Buzz +Fizz +97 +98 +Fizz +Buzz +EOF +test_diff <<'EOF' +int printf(); + +int main() { + int i; + for (i = 1; i <= 100; i = i + 1) { + if (i % 15 == 0) { + printf("FizzBuzz\n"); + } else if (i % 3 == 0) { + printf("Fizz\n"); + } else if (i % 5 == 0) { + printf("Buzz\n"); + } else { + printf("%d\n", i); + } + } + return 0; +} +EOF + +cat <<'EOF' > expected +hello, world +EOF +test_diff <<'EOF' +int printf(); +int main() { + printf("hello, world\n"); + return 0; +} +EOF + diff --git a/tests/file_io.sh b/tests/file_io.sh new file mode 100644 index 0000000..07a335e --- /dev/null +++ b/tests/file_io.sh @@ -0,0 +1,31 @@ +cat <<'EOF' > input +foo +bar +baz +EOF +cat <<'EOF' > expected +foo +foo +bar +bar +baz +baz +EOF +test_diff hoge piyo fuga<<'EOF' +struct FILE; +typedef struct FILE FILE; +extern FILE* stdin; +extern FILE* stdout; +int fprintf(); +char* fgets(); +void* calloc(); + +int main() { + char* buf = calloc(256, sizeof(char)); + while (fgets(buf, 256, stdin)) { + fprintf(stdout, "%s%s", buf, buf); + } + return 0; +} +EOF + diff --git a/tests/for_loops.sh b/tests/for_loops.sh new file mode 100644 index 0000000..2e3205f --- /dev/null +++ b/tests/for_loops.sh @@ -0,0 +1,141 @@ +test_exit_code 45 <<'EOF' +int main() { + int i; + int ret; + i = 0; + ret = 0; + for (i = 0; i < 10; i = i + 1) { + ret = ret + i; + } + return ret; +} +EOF + +test_exit_code 25 <<'EOF' +int main() { + int i; + int ret; + i = 0; + ret = 0; + for (i = 0; i < 10; i = i + 1) { + if (i % 2 == 0) { + continue; + } + ret = ret + i; + } + return ret; +} +EOF + +test_exit_code 66 <<'EOF' +int main() { + int i; + int ret; + i = 0; + ret = 0; + for (i = 0; i < 100; i = i + 1) { + if (i == 12) { + break; + } + ret = ret + i; + } + return ret; +} +EOF + +cat <<'EOF' > expected +0 +1 +2 +3 +4 +10 +11 +12 +13 +14 +20 +21 +22 +23 +24 +25 +EOF +test_diff <<'EOF' +int printf(); + +int main() { + int i = 0; + for (; i < 5; i = i + 1) { + printf("%d\n", i); + } + for (i = 10; i < 15; ) { + printf("%d\n", i); + i = i + 1; + } + for (i = 20; ; i = i + 1) { + printf("%d\n", i); + if (i == 25) break; + } + + return 0; +} +EOF + +cat <<'EOF' > expected +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + for (int i = 0; i < 10; i++) { + printf("%d\n", i); + } +} +EOF + +cat <<'EOF' > expected +0 +1 +2 +3 +4 +0 +1 +2 +3 +4 +43 +42 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + for (int i = 0; i < 5; i++) { + printf("%d\n", i); + } + for (int i = 0; i < 5; i++) { + printf("%d\n", i); + } + int x = 42; + { + int x = 43; + printf("%d\n", x); + } + printf("%d\n", x); +} +EOF + diff --git a/tests/function_basics.sh b/tests/function_basics.sh new file mode 100644 index 0000000..f064622 --- /dev/null +++ b/tests/function_basics.sh @@ -0,0 +1,58 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int foo() { + int i; + int ret; + i = 0; + ret = 0; + for (i = 0; i < 100; i = i + 1) { + if (i == 12) { + break; + } + ret = ret + i; + } + return ret; +} + +int f(int a, int b, int c, int d, int e, int f) { + return a; +} + +int f2(int a, int b, int c, int d, int e, int f) { + return b; +} + +int f3(int a, int b, int c, int d, int e, int f) { + return c; +} + +int f4(int a, int b, int c, int d, int e, int f) { + return d; +} + +int f5(int a, int b, int c, int d, int e, int f) { + return e; +} + +int f6(int a, int b, int c, int d, int e, int f) { + return f; +} + +int main() { + ASSERT_EQ(66, foo()); + ASSERT_EQ(10, 10 * f(1, 2, 3, 4, 5, 6)); + ASSERT_EQ(20, 10 * f2(1, 2, 3, 4, 5, 6)); + ASSERT_EQ(30, 10 * f3(1, 2, 3, 4, 5, 6)); + ASSERT_EQ(40, 10 * f4(1, 2, 3, 4, 5, 6)); + ASSERT_EQ(50, 10 * f5(1, 2, 3, 4, 5, 6)); + ASSERT_EQ(60, 10 * f6(1, 2, 3, 4, 5, 6)); +} +EOF + +touch expected +test_diff <<'EOF' +int main() { + return 0; +} +EOF diff --git a/tests/function_macros.sh b/tests/function_macros.sh new file mode 100644 index 0000000..1108650 --- /dev/null +++ b/tests/function_macros.sh @@ -0,0 +1,110 @@ +cat <<'EOF' > expected +42 +246 +221 +EOF + +test_diff <<'EOF' +int printf(); + +#define A(x) x +#define B(x) x+x +#define C(x, y) x*y + +int main() { + printf("%d\n", A(42)); + printf("%d\n", B + (123)); + printf("%d\n", C (13, 17)); +} +EOF + +cat <<'EOF' > expected +123 +EOF +test_diff <<'EOF' +#define A(x) x + +int printf(); + +int main() { + printf("%d\n", A ( 123 )); +} +EOF + +cat <<'EOF' > expected +main.c:4: expected ')', but got '' +EOF +test_compile_error <<'EOF' +#define A(x) x + +int main() { +#if A ( + 123) < A(345) + printf("1\n"); +#endif +} +EOF + +cat <<'EOF' > expected +42 +100 200 +300 +0 +1 2 3 +15 +42 +123 +879 +EOF + +test_diff <<'EOF' +int printf(); + +#define ADD(a, b) ((a) + (b)) +#define PRINT_TWO(x, y) printf("%d %d\n", x, y) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define NESTED(x) (x) +#define CONCAT3(a, b, c) a ## b ## c + +int main() { + printf("%d\n", ADD(40, 2)); + PRINT_TWO(100, 200); + printf("%d\n", MAX(100 + 200, 250)); + printf("%d\n"); + NESTED((printf("1 "), printf("2 "), printf("3\n"), 0)); + + int x = 5, y = 10; + printf("%d\n", ADD(x + 2, y - 2)); + printf("%d\n", ADD(MAX(10, 20), MAX(15 + 5, 22))); + printf("%d\n", ADD( 100 , 23 )); + printf("%d\n", ADD(NESTED((100 + 200)), MAX((123 + 456), (111 + 222)))); +} +EOF + +cat <<'EOF' > expected +int printf ( const char *, ...); + +int main () { + int foo = 42; + printf ( %d\n, foo); + + int a = 123; + printf ( %d\n, a); +} +EOF +test_cpp <<'EOF' +int printf(const char*, ...); + +int main() { +#define foo foo + int foo = 42; + printf("%d\n", foo); + +#define a b +#define b c +#define c a + int a = 123; + printf("%d\n", a); +} +EOF diff --git a/tests/function_pointers.sh b/tests/function_pointers.sh new file mode 100644 index 0000000..cddc623 --- /dev/null +++ b/tests/function_pointers.sh @@ -0,0 +1,24 @@ +cat <<'EOF' > expected +a +h +g +EOF + +test_diff <<'EOF' +int* f1(int a); +int (*f2)(int a); + +extern int atexit (void (*) (void)); +extern int atexit (void (*fn) (void)); + +int printf(const char*, ...); + +void g() { printf("g\n"); } +void h() { printf("h\n"); } + +int main() { + atexit(g); + atexit(h); + printf("a\n"); +} +EOF diff --git a/tests/global_variables.sh b/tests/global_variables.sh new file mode 100644 index 0000000..8de1483 --- /dev/null +++ b/tests/global_variables.sh @@ -0,0 +1,39 @@ +cat <<'EOF' > expected +42 123 999 +EOF + +test_diff <<'EOF' +int printf(); + +char a = 42; +short b = 123; +int c = 999; + +int main() { + printf("%d %d %d\n", a, b, c); +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int a; +int* b = &a; +int c[10]; +int* d = c; +int e, *f = e, g[10], *h = g; + +int main() { + *b = 123; + ASSERT_EQ(123, a); + + d[2] = 42; + ASSERT_EQ(42, c[2]); + + *f = 456; + ASSERT_EQ(456, e); + + h[5] = 789; + ASSERT_EQ(789, g[5]); +} +EOF diff --git a/tests/goto.sh b/tests/goto.sh new file mode 100644 index 0000000..1e28a51 --- /dev/null +++ b/tests/goto.sh @@ -0,0 +1,168 @@ +touch expected +test_diff <<'EOF' +int main() { + goto end; + return 1; +end: + return 0; +} +EOF + +cat <<'EOF' > expected +1 +2 +3 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + int i = 0; +loop: + i++; + printf("%d\n", i); + if (i < 3) + goto loop; + return 0; +} +EOF + +cat < /dev/null > expected +test_diff <<'EOF' +int main() { + goto skip; + int x = 5; +skip: + return 0; +} +EOF + +cat <<'EOF' > expected +start +middle +end +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + printf("start\n"); + goto middle; +first: + printf("first\n"); + goto end; +middle: + printf("middle\n"); + goto end; +last: + printf("last\n"); +end: + printf("end\n"); + return 0; +} +EOF + +cat <<'EOF' > expected +before +after +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + printf("before\n"); + { + { + goto out; + printf("inside\n"); + } + printf("middle\n"); + } +out: + printf("after\n"); + return 0; +} +EOF + +cat <<'EOF' > expected +x is 5 +x is 10 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + int x = 5; + if (x == 5) { + printf("x is 5\n"); + goto next; + } + printf("x is not 5\n"); +next: + x = 10; + printf("x is %d\n", x); + return 0; +} +EOF + +cat <<'EOF' > expected +case 2 +done +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { + int x = 2; + switch (x) { + case 1: + printf("case 1\n"); + break; + case 2: + printf("case 2\n"); + goto done; + case 3: + printf("case 3\n"); + break; + } + printf("after switch\n"); +done: + printf("done\n"); + return 0; +} +EOF + +# cat <<'EOF' > expected +# error: use of undeclared label 'undefined' +# EOF +# test_compile_error <<'EOF' +# int main() { +# goto undefined; +# return 0; +# } +# EOF + +# cat <<'EOF' > expected +# error: redefinition of label 'duplicate' +# EOF +# test_compile_error <<'EOF' +# int main() { +# duplicate: +# ; +# duplicate: +# return 0; +# } +# EOF + +# cat <<'EOF' > expected +# error: label at end of compound statement +# EOF +# test_compile_error <<'EOF' +# int main() { +# { +# goto end; +# end: +# } +# return 0; +# } +# EOF diff --git a/tests/identifiers.sh b/tests/identifiers.sh new file mode 100644 index 0000000..5786ff7 --- /dev/null +++ b/tests/identifiers.sh @@ -0,0 +1,16 @@ +cat <<'EOF' > expected +42,123 +EOF +test_diff <<'EOF' +int printf(); + +int main() { + int _a = 42; + int _b = 123; + + printf("%d,%d\n", _a, _b); + + return 0; +} +EOF + diff --git a/tests/if_elif_else.sh b/tests/if_elif_else.sh new file mode 100644 index 0000000..e0adee0 --- /dev/null +++ b/tests/if_elif_else.sh @@ -0,0 +1,215 @@ +cat <<'EOF' > expected +int printf (); + + +int main () { + printf ( 1\n); +} +EOF + +test_cpp <<'EOF' +int printf(); + +#define A +#define B + +int main() { +#if defined A && defined(B) + printf("1\n"); +#else + printf("2\n"); +#endif +} +EOF + +cat <<'EOF' > expected +int printf (); + +int main () { + printf ( 1\n); + + printf ( 1\n); + + printf ( 2\n); + + printf ( 3\n); +} +EOF + +test_cpp <<'EOF' +int printf(); + +int main() { +#if 1 + printf("1\n"); +#elif 1 + printf("2\n"); +#else + printf("3\n"); +#endif + +#if 1 + printf("1\n"); +#elif 0 + printf("2\n"); +#else + printf("3\n"); +#endif + +#if 0 + printf("1\n"); +#elif 1 + printf("2\n"); +#else + printf("3\n"); +#endif + +#if 0 + printf("1\n"); +#elif 0 + printf("2\n"); +#else + printf("3\n"); +#endif +} +EOF + +cat <<'EOF' > expected +int printf (); + +int main () { + printf ( 1\n); + + printf ( 1\n); + + printf ( 2\n); + + printf ( 2\n); + + printf ( 3\n); + + printf ( 4\n); + + printf ( 3\n); + + printf ( 4\n); +} +EOF + +test_cpp <<'EOF' +int printf(); + +int main() { +#if 1 +# if 1 + printf("1\n"); +# else + printf("2\n"); +# endif +#else +# if 1 + printf("3\n"); +# else + printf("4\n"); +# endif +#endif + +#if 1 +# if 1 + printf("1\n"); +# else + printf("2\n"); +# endif +#else +# if 0 + printf("3\n"); +# else + printf("4\n"); +# endif +#endif + +#if 1 +# if 0 + printf("1\n"); +# else + printf("2\n"); +# endif +#else +# if 1 + printf("3\n"); +# else + printf("4\n"); +# endif +#endif + +#if 1 +# if 0 + printf("1\n"); +# else + printf("2\n"); +# endif +#else +# if 0 + printf("3\n"); +# else + printf("4\n"); +# endif +#endif + +#if 0 +# if 1 + printf("1\n"); +# else + printf("2\n"); +# endif +#else +# if 1 + printf("3\n"); +# else + printf("4\n"); +# endif +#endif + +#if 0 +# if 1 + printf("1\n"); +# else + printf("2\n"); +# endif +#else +# if 0 + printf("3\n"); +# else + printf("4\n"); +# endif +#endif + +#if 0 +# if 0 + printf("1\n"); +# else + printf("2\n"); +# endif +#else +# if 1 + printf("3\n"); +# else + printf("4\n"); +# endif +#endif + +#if 0 +# if 0 + printf("1\n"); +# else + printf("2\n"); +# endif +#else +# if 0 + printf("3\n"); +# else + printf("4\n"); +# endif +#endif +} +EOF diff --git a/tests/if_else.sh b/tests/if_else.sh new file mode 100644 index 0000000..f1da0d1 --- /dev/null +++ b/tests/if_else.sh @@ -0,0 +1,21 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int result1; + if (1) { + result1 = 12; + } else { + result1 = 34; + } + ASSERT_EQ(12, result1); + + int result2; + if (1 + 1 != 2) { + result2 = 12; + } else { + result2 = 34; + } + ASSERT_EQ(34, result2); +} +EOF diff --git a/tests/ifdef_ifndef.sh b/tests/ifdef_ifndef.sh new file mode 100644 index 0000000..683a1dc --- /dev/null +++ b/tests/ifdef_ifndef.sh @@ -0,0 +1,95 @@ +cat <<'EOF' > expected +int printf (); + + +int main () { + + printf ( __ducc__ is defined.\n); + + printf ( A is defined.\n); + + printf ( B is undefined.\n); + + + return 0; +} +EOF + +test_cpp <<'EOF' +int printf(); + +#define A 123 + +int main() { + +#ifdef __ducc__ + printf("__ducc__ is defined.\n"); +#else + printf("__ducc__ is undefined.\n"); +#endif + +#ifdef A + printf("A is defined.\n"); +#else + printf("A is undefined.\n"); +#endif + +#ifdef B + printf("B is defined.\n"); +#else + printf("B is undefined.\n"); +#endif + +#define B 456 + + return 0; +} +EOF + +cat <<'EOF' > expected +int printf (); + + +int main () { + + printf ( __ducc__ is defined.\n); + + printf ( A is defined.\n); + + printf ( B is undefined.\n); + + + return 0; +} +EOF + +test_cpp <<'EOF' +int printf(); + +#define A 123 + +int main() { + +#ifndef __ducc__ + printf("__ducc__ is undefined.\n"); +#else + printf("__ducc__ is defined.\n"); +#endif + +#ifndef A + printf("A is undefined.\n"); +#else + printf("A is defined.\n"); +#endif + +#ifndef B + printf("B is undefined.\n"); +#else + printf("B is defined.\n"); +#endif + +#define B 456 + + return 0; +} +EOF diff --git a/tests/implicit_return.sh b/tests/implicit_return.sh new file mode 100644 index 0000000..423844e --- /dev/null +++ b/tests/implicit_return.sh @@ -0,0 +1,19 @@ +# C99: 5.1.2.2.3 +test_exit_code 0 <<'EOF' +int main() { +} +EOF + +test_exit_code 0 <<'EOF' +int main() { + 1 + 2 + 3; +} +EOF + +test_exit_code 0 <<'EOF' +int main() { + if (1); + else return 1; +} +EOF + diff --git a/tests/include_errors.sh b/tests/include_errors.sh new file mode 100644 index 0000000..3ac546f --- /dev/null +++ b/tests/include_errors.sh @@ -0,0 +1,52 @@ +cat <<'EOF' > expected +main.c:1: cannot open include file: "nonexistent.h" +EOF + +test_compile_error <<'EOF' +#include "nonexistent.h" + +int main() { + return 0; +} +EOF + +cat <<'EOF' > expected +include depth limit exceeded +EOF + +# Create circular include files +cat <<'EOF' > a.h +#include "b.h" +int a() { return 1; } +EOF + +cat <<'EOF' > b.h +#include "a.h" +int b() { return 2; } +EOF + +test_compile_error <<'EOF' +#include "a.h" + +int main() { + a() + b(); + return 0; +} +EOF + +cat <<'EOF' > expected +main.c:1: cannot open include file: "hoge.h" +EOF + +test_compile_error <<'EOF' +#include "hoge.h" +EOF + +cat <<'EOF' > expected +main.c:1: cannot resolve include file name: +EOF + +test_compile_error <<'EOF' +#include +EOF + diff --git a/tests/include_guards.sh b/tests/include_guards.sh new file mode 100644 index 0000000..87c44a9 --- /dev/null +++ b/tests/include_guards.sh @@ -0,0 +1,26 @@ +cat <<'EOF' > expected +42 +EOF + +cat <<'EOF' > header.h +#ifndef HEADER_H +#define HEADER_H + +int f() { return 42; } + +#endif +EOF + +test_diff <<'EOF' +#include "header.h" +#include "header.h" +#include "header.h" + +int printf(); + +int main() { + printf("%d\n", HEADER_H HEADER_H HEADER_H f() HEADER_H HEADER_H HEADER_H); + return 0; +} +EOF + diff --git a/tests/include_local.sh b/tests/include_local.sh new file mode 100644 index 0000000..c9fe755 --- /dev/null +++ b/tests/include_local.sh @@ -0,0 +1,100 @@ +cat <<'EOF' > header.h +int add(int a, int b) { + return a + b; +} + +int printf(const char*, ...); +EOF + +cat <<'EOF' > expected +8 +EOF +test_diff <<'EOF' +#include "header.h" + +int main() { + printf("%d\n", add(5, 3)); + return 0; +} +EOF + +mkdir -p foo +cat <<'EOF' > bar.h +#include "baz.h" +EOF +cat <<'EOF' > baz.h +#define A 123 +EOF +cat <<'EOF' > foo/bar.h +#include "baz.h" +EOF +cat <<'EOF' > foo/baz.h +#define A 456 +EOF + +cat <<'EOF' > expected +123 +456 +EOF +test_diff <<'EOF' +int printf(const char*, ...); + +int main() { +#include "bar.h" + printf("%d\n", A); + +#undef A + +#include "foo/bar.h" + printf("%d\n", A); +} +EOF + +cat <<'EOF' > expected +12 +EOF + +cat <<'EOF' > math.h +int multiply(int a, int b) { + return a * b; +} +EOF + +cat <<'EOF' > calc.h +#include "math.h" + +int calculate(int x) { + return multiply(x, 2); +} + +int printf(const char*, ...); +EOF + +test_diff <<'EOF' +#include "calc.h" + +int main() { + printf("%d\n", calculate(6)); + return 0; +} +EOF + +cat <<'EOF' > expected +42 +EOF + +cat <<'EOF' > header.h +#define A 42 +EOF + +test_diff <<'EOF' +#include "header.h" + +int printf(); + +int main() { + printf("%d\n", A); + return 0; +} +EOF + diff --git a/tests/increment_operators.sh b/tests/increment_operators.sh new file mode 100644 index 0000000..f6aa2b5 --- /dev/null +++ b/tests/increment_operators.sh @@ -0,0 +1,29 @@ +cat <<'EOF' > expected +44 +44 +46 +46 +44 +42 +42 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + int a = 42; + ++a; + a++; + printf("%d\n", a); + printf("%d\n", a++); + printf("%d\n", ++a); + printf("%d\n", a); + --a; + a--; + printf("%d\n", a--); + printf("%d\n", --a); + printf("%d\n", a); +} +EOF + diff --git a/tests/input_operations.sh b/tests/input_operations.sh new file mode 100644 index 0000000..15dfb2f --- /dev/null +++ b/tests/input_operations.sh @@ -0,0 +1,37 @@ +cat <<'EOF' > input +abc +EOF +cat <<'EOF' > expected +4 +abc +EOF +test_diff <<'EOF' +void* calloc(); +int getchar(); +int printf(); + +int read_all(char* buf) { + int c; + int n = 0; + for (0; 1; 0) { + c = getchar(); + if (c == -1) { + break; + } + buf[n] = c; + n = n + 1; + } + return n; +} + +int main() { + char* source = calloc(1024, sizeof(char)); + int source_len = read_all(source); + + printf("%d\n", source_len); + printf("%s", source); + + return 0; +} +EOF + diff --git a/tests/invalid_directives.sh b/tests/invalid_directives.sh new file mode 100644 index 0000000..877f9e9 --- /dev/null +++ b/tests/invalid_directives.sh @@ -0,0 +1,28 @@ +cat <<'EOF' > expected +main.c:1: invalid preprocessing directive, '#foo' +EOF +test_compile_error <<'EOF' +#foo + +int main() {} +EOF + +cat <<'EOF' > expected +main.c:1: invalid preprocessing directive, '#bar' +EOF +test_compile_error <<'EOF' +# bar 1 2 3 + +int main() {} +EOF + +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +#if 0 +#baz +#endif + +int main() {} +EOF + diff --git a/tests/keywords.sh b/tests/keywords.sh new file mode 100644 index 0000000..0bdb6f7 --- /dev/null +++ b/tests/keywords.sh @@ -0,0 +1,14 @@ +cat <<'EOF' > expected + + +int printf (); +int main () {} +EOF + +test_cpp <<'EOF' +// A keyword is treated as a normal identifier in preprocessing phase. +#define auto int + +auto printf(); +auto main() {} +EOF diff --git a/tests/line_continuation.sh b/tests/line_continuation.sh new file mode 100644 index 0000000..c5b5c2e --- /dev/null +++ b/tests/line_continuation.sh @@ -0,0 +1,57 @@ +cat <<'EOF' > expected +10 20 +100 300 500 +EOF + +test_diff <<'EOF' +int printf(); +in\ +t ma\ +in() { + int x = 1\ +0; + + int y = 2\ +\ +\ +0; + +#def\ +ine X 100 +#define Y 100 + \ +200 +#define Z /* +*/ 500 + // comment \ + x *= 100, y *= 200; + /* comment *\ +/ + + printf("%d %d\n", x, y); + printf("%d %d %d\n", X, Y, Z); +} +EOF + +cat <<'EOF' > expected +main.c:1: expected, but got +EOF + +echo -n 'int main() {}\' | test_compile_error + +cat <<'EOF' > expected +Hello World +Line continues +EOF + +# CRLF +printf 'int printf(const char*, ...);\r\nint main() {\r\n printf("Hello World\\n");\r\n printf("Line con\\\r\ntinues\\n");\r\n return 0;\r\n}\r\n' > main_crlf.c +test_diff < main_crlf.c + +# CR +printf 'int printf(const char*, ...);\rint main() {\r printf("Hello World\\n");\r printf("Line con\\\rtinues\\n");\r return 0;\r}\r' > main_cr.c +test_diff < main_cr.c + +# Mixed +printf 'int printf(const char*, ...);\nint main() {\r\n printf("Hello World\\n");\r printf("Line con\\\r\ntinues\\n");\n return 0;\r\n}\r\n' > main_mixed.c +test_diff < main_mixed.c + diff --git a/tests/local_variables.sh b/tests/local_variables.sh new file mode 100644 index 0000000..674899d --- /dev/null +++ b/tests/local_variables.sh @@ -0,0 +1,56 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int foo; + foo = 42; + ASSERT_EQ(42, foo); + + int bar; + bar = 28; + ASSERT_EQ(70, foo + bar); + + int a1; + int a2; + int a3; + int a4; + int a5; + int a6; + int a7; + int a8; + int a9; + + a1 = 1; + a2 = 2; + a3 = 3; + a4 = 4; + a5 = 5; + a6 = 6; + a7 = 7; + a8 = 8; + a9 = 9; + + ASSERT_EQ(45, + a1 + + a2 + + a3 + + a4 + + a5 + + a6 + + a7 + + a8 + + a9 + + 0); +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int d = 2, e = d, f = d + e; + ASSERT_EQ(2, d); + ASSERT_EQ(2, e); + ASSERT_EQ(4, f); +} +EOF diff --git a/tests/logical_operators.sh b/tests/logical_operators.sh new file mode 100644 index 0000000..1cc6594 --- /dev/null +++ b/tests/logical_operators.sh @@ -0,0 +1,68 @@ +cat <<'EOF' > expected +foo +EOF +test_diff <<'EOF' +int printf(); + +int foo() { + printf("foo\n"); + return 0; +} + +int bar() { + printf("bar\n"); + return 1; +} + +int main() { + if (foo() && bar()) { + printf("baz\n"); + } + + return 0; +} +EOF + +cat <<'EOF' > expected +foo +bar +baz +EOF +test_diff <<'EOF' +int printf(); + +int foo() { + printf("foo\n"); + return 0; +} + +int bar() { + printf("bar\n"); + return 1; +} + +int main() { + if (foo() || bar()) { + printf("baz\n"); + } + + return 0; +} +EOF + +cat <<'EOF' > expected +0 +1 +0 +EOF +test_diff <<'EOF' +int printf(); + +int main() { + printf("%d\n", !1); + printf("%d\n", !0); + printf("%d\n", !23); + return 0; +} +EOF + diff --git a/tests/macro_operators.sh b/tests/macro_operators.sh new file mode 100644 index 0000000..e456d4f --- /dev/null +++ b/tests/macro_operators.sh @@ -0,0 +1,89 @@ +cat <<'EOF' > expected +int printf (); + + +int foobar = 100; +int prefix_test = 200; +int test_suffix = 300; + +int var_1 = 10; +int var_2 = 20; + +int var_A = 30; + +int number_12 = 12; + +int bazqux = 42; + +int main () { + printf ( foobar=%d\n, foobar); + printf ( prefix_test=%d\n, prefix_test); + printf ( test_suffix=%d\n, test_suffix); + printf ( var_1=%d\n, var_1); + printf ( var_2=%d\n, var_2); + printf ( var_A=%d\n, var_A); + printf ( number_12=%d\n, number_12); + return 0; +} +EOF + +test_cpp <<'EOF' +int printf(); + +#define CONCAT(a, b) a##b +#define PREFIX(name) prefix_##name +#define SUFFIX(name) name##_suffix + +int CONCAT(foo, bar) = 100; +int PREFIX(test) = 200; +int SUFFIX(test) = 300; + +#define MAKE_VAR(n) var_##n +int MAKE_VAR(1) = 10; +int MAKE_VAR(2) = 20; + +#define A 0 +int MAKE_VAR(A) = 30; + +#define NUMBER(x, y) number_##x##y +int NUMBER(1, 2) = 12; + +#define CONCAT2(a, b) a ## b +int CONCAT2(baz, qux) = 42; + +int main() { + printf("foobar=%d\n", foobar); + printf("prefix_test=%d\n", prefix_test); + printf("test_suffix=%d\n", test_suffix); + printf("var_1=%d\n", var_1); + printf("var_2=%d\n", var_2); + printf("var_A=%d\n", var_A); + printf("number_12=%d\n", number_12); + return 0; +} +EOF + +cat <<'EOF' > expected +int printf ( const char *, ...); + + +int H ( int n) { return n; } + +int main () { + printf ( %d\n, H ( 123)); +} +EOF + +test_cpp <<'EOF' +int printf(const char*, ...); + +#define F(x) CHECK(G(x)) +#define G(x) CHECK(H(x)) +#define CHECK(x) x + +int H(int n) { return n; } + +int main() { + printf("%d\n", F(123)); +} +EOF diff --git a/tests/nested_conditionals.sh b/tests/nested_conditionals.sh new file mode 100644 index 0000000..d149e07 --- /dev/null +++ b/tests/nested_conditionals.sh @@ -0,0 +1,12 @@ +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +#if 0 +# if 1 +# error "not an error" +# endif +#endif + +int main() {} +EOF + diff --git a/tests/pointer_basics.sh b/tests/pointer_basics.sh new file mode 100644 index 0000000..2b0beef --- /dev/null +++ b/tests/pointer_basics.sh @@ -0,0 +1,80 @@ +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +int main() { + int a1; + int* a2; + char a3; + char* a4; + long a5; + long* a6; + void* a8; + int** a10; + char** a12; + long** a14; + void** a16; + int*** a18; + char*** a20; + long*** a22; + void*** a24; + int* const* const* a25; + return 0; +} +EOF + +cat <<'EOF' > expected +42 42 +EOF +test_diff <<'EOF' +int printf(); + +int main() { + int x; + int* y; + y = &x; + *y = 42; + printf("%d %d\n", x, *y); + return 0; +} +EOF + +cat <<'EOF' > expected +3 +3 +3 +EOF +test_diff <<'EOF' +int printf(); + +int main() { + char c; + int i; + long l; + c = 42; + i = 42*2; + l = 42*3; + + char* cp1; + char* cp2; + int* ip1; + int* ip2; + long* lp1; + long* lp2; + + cp1 = &c; + cp2 = &c + 3; + + ip1 = &i; + ip2 = &i + 3; + + lp1 = &l; + lp2 = &l + 3; + + printf("%d\n", cp2 - cp1); + printf("%d\n", ip2 - ip1); + printf("%d\n", lp2 - lp1); + + return 0; +} +EOF + diff --git a/tests/predefined_macros.sh b/tests/predefined_macros.sh new file mode 100644 index 0000000..51aeca8 --- /dev/null +++ b/tests/predefined_macros.sh @@ -0,0 +1,55 @@ +cat <<'EOF' > expected +1 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + printf("%d\n", __ducc__); + return 0; +} +EOF + +cat <<'EOF' > expected +main.c +main.c +main.c +EOF + +cat <<'EOF' > header.h +#define A __FILE__ +EOF + +test_diff <<'EOF' +#define B __FILE__ +#include "header.h" +int printf(); +int main() { + printf("%s\n", __FILE__); + printf("%s\n", B); + printf("%s\n", A); +} +EOF + +cat <<'EOF' > expected +5 +6 6 +7 7 +EOF + +cat <<'EOF' > header.h +#define A __LINE__ +EOF + +test_diff <<'EOF' +#define B __LINE__ +#include "header.h" +int printf(); +int main() { + printf("%d\n", __LINE__); + printf("%d %d\n", B, B); + printf("%d %d\n", A, A); +} +EOF + diff --git a/tests/preprocess_flag.sh b/tests/preprocess_flag.sh new file mode 100644 index 0000000..3b52bc0 --- /dev/null +++ b/tests/preprocess_flag.sh @@ -0,0 +1,122 @@ +cat <<'EOF' > expected + +int main () { + int x = 100; + int y = 0; + return 0; +} +EOF + +test_cpp <<'EOF' +#define MAX 100 +#define MIN 0 + +int main() { + int x = MAX; + int y = MIN; + return 0; +} +EOF + +cat <<'EOF' > expected + +int main () { + int sum = ( ( 10) + ( 20)); + int product = ( ( 3) * ( 4)); + return 0; +} +EOF + +test_cpp <<'EOF' +#define ADD(a, b) ((a) + (b)) +#define MUL(x, y) ((x) * (y)) + +int main() { + int sum = ADD(10, 20); + int product = MUL(3, 4); + return 0; +} +EOF + +cat <<'EOF' > expected +int main () { + return 42; +} +EOF + +test_cpp <<'EOF' +#define FOO 42 +int main() { + return FOO; +} +EOF + +cat <<'EOF' > expected +int foo () { return 1; } +EOF + +test_cpp <<'EOF' +#define X 1 +#ifdef X +int foo() { return 1; } +#else +int foo() { return 0; } +#endif +EOF + +cat <<'EOF' > expected + +int main () { + int x = 1 + 2 * 3; + return 0; +} +EOF + +test_cpp <<'EOF' +#define A 1 +#define B A + 2 +#define C B * 3 + +int main() { + int x = C; + return 0; +} +EOF + +cat <<'EOF' > expected + +int main () { + int x = ( 5 + 3); + int y = ( ( 5) + ( 3)); + return 0; +} +EOF + +test_cpp <<'EOF' +#define SIMPLE (5 + 3) +#define COMPLEX(a, b) ((a) + (b)) + +int main() { + int x = SIMPLE; + int y = COMPLEX(5, 3); + return 0; +} +EOF + +cat <<'EOF' > expected + +int main () { + int bar = 0; + bar ++; +} +EOF + +test_cpp <<'EOF' +#define A(a) a +#define B(b) b + +int main() { + A(int)B(bar) = 0; + bar++; +} +EOF diff --git a/tests/preprocessor_comments.sh b/tests/preprocessor_comments.sh new file mode 100644 index 0000000..0fcea83 --- /dev/null +++ b/tests/preprocessor_comments.sh @@ -0,0 +1,11 @@ +cat <<'EOF' > expected +EOF + +test_diff <<'EOF' +#if 1 // a +#else // b +#endif // c + +int main() {} +EOF + diff --git a/tests/printf.sh b/tests/printf.sh new file mode 100644 index 0000000..da35fcb --- /dev/null +++ b/tests/printf.sh @@ -0,0 +1,220 @@ +touch expected +test_diff <<'EOF' +int printf(); + +int main() { + printf(""); + return 0; +} +EOF + +cat <<'EOF' > expected +Hello, World! +EOF +test_diff <<'EOF' +int printf(); + +int main() { + printf("Hello, World!\n"); + return 0; +} +EOF + +cat <<'EOF' > expected +"Hello, World!" +EOF +test_diff <<'EOF' +int printf(); + +int main() { + printf("\"Hello, World!\"\n"); + return 0; +} +EOF + +cat <<'EOF' > expected +1 +1 +1 +1 +1 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +4 +4 +4 +4 +4 +4 +4 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + printf("%d\n", sizeof(char)); + printf("%d\n", sizeof(signed char)); + printf("%d\n", sizeof(char signed)); + printf("%d\n", sizeof(unsigned char)); + printf("%d\n", sizeof(char unsigned)); + + printf("%d\n", sizeof(short)); + printf("%d\n", sizeof(signed short)); + printf("%d\n", sizeof(short signed)); + printf("%d\n", sizeof(short int)); + printf("%d\n", sizeof(int short)); + printf("%d\n", sizeof(signed short int)); + printf("%d\n", sizeof(signed int short)); + printf("%d\n", sizeof(short signed int)); + printf("%d\n", sizeof(short int signed)); + printf("%d\n", sizeof(int short signed)); + printf("%d\n", sizeof(int signed short)); + + printf("%d\n", sizeof(unsigned short)); + printf("%d\n", sizeof(short unsigned)); + printf("%d\n", sizeof(unsigned short int)); + printf("%d\n", sizeof(unsigned int short)); + printf("%d\n", sizeof(short unsigned int)); + printf("%d\n", sizeof(short int unsigned)); + printf("%d\n", sizeof(int short unsigned)); + printf("%d\n", sizeof(int unsigned short)); + + printf("%d\n", sizeof(int)); + printf("%d\n", sizeof(signed)); + printf("%d\n", sizeof(signed int)); + printf("%d\n", sizeof(int signed)); + + printf("%d\n", sizeof(unsigned)); + printf("%d\n", sizeof(unsigned int)); + printf("%d\n", sizeof(int unsigned)); + + printf("%d\n", sizeof(long)); + printf("%d\n", sizeof(signed long)); + printf("%d\n", sizeof(long signed)); + printf("%d\n", sizeof(long int)); + printf("%d\n", sizeof(int long)); + printf("%d\n", sizeof(signed long int)); + printf("%d\n", sizeof(signed int long)); + printf("%d\n", sizeof(long signed int)); + printf("%d\n", sizeof(long int signed)); + printf("%d\n", sizeof(int long signed)); + printf("%d\n", sizeof(int signed long)); + + printf("%d\n", sizeof(unsigned long)); + printf("%d\n", sizeof(long unsigned)); + printf("%d\n", sizeof(unsigned long int)); + printf("%d\n", sizeof(unsigned int long)); + printf("%d\n", sizeof(long unsigned int)); + printf("%d\n", sizeof(long int unsigned)); + printf("%d\n", sizeof(int long unsigned)); + printf("%d\n", sizeof(int unsigned long)); + + printf("%d\n", sizeof(long long)); + printf("%d\n", sizeof(signed long long)); + printf("%d\n", sizeof(long signed long)); + printf("%d\n", sizeof(long long signed)); + printf("%d\n", sizeof(long long int)); + printf("%d\n", sizeof(long int long)); + printf("%d\n", sizeof(int long long)); + printf("%d\n", sizeof(signed long long int)); + printf("%d\n", sizeof(signed long int long)); + printf("%d\n", sizeof(signed int long long)); + printf("%d\n", sizeof(long signed long int)); + printf("%d\n", sizeof(long signed int long)); + printf("%d\n", sizeof(int signed long long)); + printf("%d\n", sizeof(long long signed int)); + printf("%d\n", sizeof(long int signed long)); + printf("%d\n", sizeof(int long signed long)); + printf("%d\n", sizeof(long long int signed)); + printf("%d\n", sizeof(long int long signed)); + printf("%d\n", sizeof(int long long signed)); + + printf("%d\n", sizeof(unsigned long long)); + printf("%d\n", sizeof(long unsigned long)); + printf("%d\n", sizeof(long long unsigned)); + printf("%d\n", sizeof(unsigned long long int)); + printf("%d\n", sizeof(unsigned long int long)); + printf("%d\n", sizeof(unsigned int long long)); + printf("%d\n", sizeof(long unsigned long int)); + printf("%d\n", sizeof(long unsigned int long)); + printf("%d\n", sizeof(int unsigned long long)); + printf("%d\n", sizeof(long long unsigned int)); + printf("%d\n", sizeof(long int unsigned long)); + printf("%d\n", sizeof(int long unsigned long)); + printf("%d\n", sizeof(long long int unsigned)); + printf("%d\n", sizeof(long int long unsigned)); + printf("%d\n", sizeof(int long long unsigned)); +} +EOF + diff --git a/tests/recursive_functions.sh b/tests/recursive_functions.sh new file mode 100644 index 0000000..709cfbe --- /dev/null +++ b/tests/recursive_functions.sh @@ -0,0 +1,15 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int fib(int n) { + if (n <= 1) { + return 1; + } else { + return fib(n - 1) + fib(n - 2); + } +} + +int main() { + ASSERT_EQ(89, fib(10)); +} +EOF diff --git a/tests/short_type.sh b/tests/short_type.sh new file mode 100644 index 0000000..fcbec87 --- /dev/null +++ b/tests/short_type.sh @@ -0,0 +1,17 @@ +cat <<'EOF' > expected +2 42 +8 123 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + short a = 42; + printf("%zu %hd\n", sizeof(a), a); + short* b = &a; + *b = 123; + printf("%zu %hd\n", sizeof(b), *b); +} +EOF + diff --git a/tests/sizeof_operator.sh b/tests/sizeof_operator.sh new file mode 100644 index 0000000..b14e2ca --- /dev/null +++ b/tests/sizeof_operator.sh @@ -0,0 +1,177 @@ +cat <<'EOF' > expected +sizeof(int) = 4 +sizeof(int*) = 8 +sizeof(char) = 1 +sizeof(char*) = 8 +sizeof(long) = 8 +sizeof(long*) = 8 +sizeof(void*) = 8 +sizeof(int**) = 8 +sizeof(char**) = 8 +sizeof(long**) = 8 +sizeof(void**) = 8 +sizeof(int***) = 8 +sizeof(char***) = 8 +sizeof(long***) = 8 +sizeof(void***) = 8 +EOF +test_diff <<'EOF' +int printf(); + +int main() { + printf("sizeof(int) = %d\n", sizeof(int)); + printf("sizeof(int*) = %d\n", sizeof(int*)); + printf("sizeof(char) = %d\n", sizeof(char)); + printf("sizeof(char*) = %d\n", sizeof(char*)); + printf("sizeof(long) = %d\n", sizeof(long)); + printf("sizeof(long*) = %d\n", sizeof(long*)); + printf("sizeof(void*) = %d\n", sizeof(void*)); + printf("sizeof(int**) = %d\n", sizeof(int**)); + printf("sizeof(char**) = %d\n", sizeof(char**)); + printf("sizeof(long**) = %d\n", sizeof(long**)); + printf("sizeof(void**) = %d\n", sizeof(void**)); + printf("sizeof(int***) = %d\n", sizeof(int***)); + printf("sizeof(char***) = %d\n", sizeof(char***)); + printf("sizeof(long***) = %d\n", sizeof(long***)); + printf("sizeof(void***) = %d\n", sizeof(void***)); + return 0; +} +EOF + +cat <<'EOF' > expected +sizeof(struct Token) = 16 +sizeof(struct Define) = 16 +sizeof(struct Type) = 24 +sizeof(struct AstNode) = 128 +sizeof(struct LVar) = 16 +sizeof(struct Func) = 16 +sizeof(struct Parser) = 64 +sizeof(struct CodeGen) = 16 +EOF +test_diff <<'EOF' +struct Token { + int kind; + char* value; +}; + +struct Define { + char* from; + struct Token* to; +}; + +struct AstNode; + +struct Type { + int kind; + struct Type* to; + struct AstNode* members; +}; + +struct AstNode { + int kind; + struct AstNode* next; + struct AstNode* last; + char* name; + struct AstNode* func_params; + struct AstNode* func_body; + int int_value; + struct AstNode* expr1; + struct AstNode* expr2; + struct AstNode* expr3; + int op; + struct Type* ty; + int var_index; + struct AstNode* node1; + struct AstNode* node2; + char** str_literals; +}; + +struct LVar { + char* name; + struct Type* ty; +}; + +struct Func { + char* name; + struct Type* ty; +}; + +struct Parser { + struct Token* tokens; + int pos; + struct LVar* locals; + int n_locals; + struct Func* funcs; + int n_funcs; + char** str_literals; + int n_str_literals; +}; + +struct CodeGen { + int next_label; + int* loop_labels; +}; + +int printf(); + +int main() { + printf("sizeof(struct Token) = %d\n", sizeof(struct Token)); + printf("sizeof(struct Define) = %d\n", sizeof(struct Define)); + printf("sizeof(struct Type) = %d\n", sizeof(struct Type)); + printf("sizeof(struct AstNode) = %d\n", sizeof(struct AstNode)); + printf("sizeof(struct LVar) = %d\n", sizeof(struct LVar)); + printf("sizeof(struct Func) = %d\n", sizeof(struct Func)); + printf("sizeof(struct Parser) = %d\n", sizeof(struct Parser)); + printf("sizeof(struct CodeGen) = %d\n", sizeof(struct CodeGen)); + return 0; +} +EOF + +cat <<'EOF' > expected +24 +EOF + +test_diff <<'EOF' +#include + +int printf(); + +int main() { + printf("%d\n", sizeof(va_list)); +} +EOF + +cat <<'EOF' > expected +sizeof(a) = 4 +sizeof(b) = 8 +sizeof(c) = 123 +sizeof(d) = 16 +sizeof(e) = 8 +sizeof(f) = 1968 +EOF +test_diff <<'EOF' +int printf(); + +struct S { + long x; + long y; +}; + +int main() { + int a; + long b; + char c[123]; + struct S d; + void* e; + struct S f[123]; + + printf("sizeof(a) = %d\n", sizeof(a)); + printf("sizeof(b) = %d\n", sizeof(b)); + printf("sizeof(c) = %d\n", sizeof(c)); + printf("sizeof(d) = %d\n", sizeof(d)); + printf("sizeof(e) = %d\n", sizeof(e)); + printf("sizeof(f) = %d\n", sizeof(f)); + return 0; +} +EOF + diff --git a/tests/stdlib_declarations.sh b/tests/stdlib_declarations.sh new file mode 100644 index 0000000..c919b88 --- /dev/null +++ b/tests/stdlib_declarations.sh @@ -0,0 +1,22 @@ +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +typedef long size_t; + +int atoi(const char*); +void* calloc(size_t, size_t); +void exit(int); +int getchar(void); +int isalnum(int); +int isalpha(int); +int isdigit(int); +int isspace(int); +void* memcpy(void*, void*, size_t); +int printf(const char*, ...); +int sprintf(char*, const char*, ...); +int strcmp(const char*, const char*); +char* strstr(const char*, const char*); + +int main() { return 0; } +EOF + diff --git a/tests/string_operations.sh b/tests/string_operations.sh new file mode 100644 index 0000000..0c9a07e --- /dev/null +++ b/tests/string_operations.sh @@ -0,0 +1,57 @@ +touch expected +test_diff <<'EOF' +int main() { + ""; + return 0; +} +EOF + +touch expected +test_diff <<'EOF' +int main() { + "abc"; + return 0; +} +EOF + +touch expected +test_diff <<'EOF' +int main() { + "\"foo\"bar\\\n\""; + return 0; +} +EOF + +cat <<'EOF' > expected +h +l +, +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + char* h = " hello,world" + 1; + printf("%c\n", *h); + printf("%c\n", h[2]); + printf("%c\n", *(h + 5)); +} +EOF + +cat <<'EOF' > expected +105 +0 +EOF + +test_diff <<'EOF' +int printf(); + +int main() { + char* s = "hi"; + while (*s++) { + printf("%d\n", *s); + } +} +EOF + diff --git a/tests/struct_arrays.sh b/tests/struct_arrays.sh new file mode 100644 index 0000000..ca1e0b2 --- /dev/null +++ b/tests/struct_arrays.sh @@ -0,0 +1,34 @@ +cat <<'EOF' > expected +20 +10 +20 +30 +40 +50 +EOF + +test_diff <<'EOF' +int printf(); + +struct S { + int a[5]; +}; + +int main() { + struct S x; + x.a[0] = 10; + x.a[1] = 20; + x.a[2] = 30; + x.a[3] = 40; + x.a[4] = 50; + + printf("%zu\n", sizeof(struct S)); + + printf("%d\n", x.a[0]); + printf("%d\n", x.a[1]); + printf("%d\n", x.a[2]); + printf("%d\n", x.a[3]); + printf("%d\n", x.a[4]); +} +EOF + diff --git a/tests/struct_basics.sh b/tests/struct_basics.sh new file mode 100644 index 0000000..74e2458 --- /dev/null +++ b/tests/struct_basics.sh @@ -0,0 +1,121 @@ +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +struct Token { + int kind; + char* value; +}; + +struct Define { + char* from; + struct Token* to; +}; + +struct AstNode; + +struct Type { + int kind; + struct Type* to; + struct AstNode* members; +}; + +struct AstNode { + int kind; + struct AstNode* next; + struct AstNode* last; + char* name; + struct AstNode* func_params; + struct AstNode* func_body; + int int_value; + struct AstNode* expr1; + struct AstNode* expr2; + struct AstNode* expr3; + int op; + struct Type* ty; + int var_index; + struct AstNode* node1; + struct AstNode* node2; + char** str_literals; +}; + +struct LVar { + char* name; + struct Type* ty; +}; + +struct Func { + char* name; + struct Type* ty; +}; + +struct Parser { + struct Token* tokens; + int pos; + struct LVar* locals; + int n_locals; + struct Func* funcs; + int n_funcs; + char** str_literals; + int n_str_literals; +}; + +struct CodeGen { + int next_label; + int* loop_labels; +}; + +int main() { + return 0; +} +EOF + +cat <<'EOF' > expected +42 +123 +EOF +test_diff <<'EOF' +struct S { + int a; + int b; +}; + +int printf(); +void* calloc(); + +int main() { + struct S* sp; + sp = calloc(1, sizeof(struct S)); + sp->a = 42; + printf("%d\n", sp->a); + (*sp).b = 123; + printf("%d\n", (*sp).b); + return 0; +} +EOF + +cat <<'EOF' > expected +42 +EOF +test_diff <<'EOF' +void* calloc(); +int printf(); + +struct T; + +struct S { + struct T* a; +}; + +struct T { + int b; +}; + +int main() { + struct S* s = calloc(1, sizeof(struct S)); + s->a = calloc(1, sizeof(struct T)); + s->a->b = 42; + printf("%d\n", s->a->b); + return 0; +} +EOF + diff --git a/tests/struct_initialization.sh b/tests/struct_initialization.sh new file mode 100644 index 0000000..0454c57 --- /dev/null +++ b/tests/struct_initialization.sh @@ -0,0 +1,108 @@ +cat <<'EOF' > expected +0 +1 +EOF +test_diff <<'EOF' +int printf(); +void* calloc(); + +struct S { + int a; + int b; +}; + +int main() { + struct S* s = calloc(1, sizeof(struct S)); + s->b = 1; + printf("%ld\n", s->a); + printf("%ld\n", s->b); + return 0; +} +EOF + +cat <<'EOF' > expected +123 456 +0 0 +123 456 +EOF + +test_diff <<'EOF' +struct S { + int a; + int b; +}; +typedef struct S S; + +void* calloc(); +int printf(); + +int main() { + S* s1 = calloc(1, sizeof(S)); + S* s2 = calloc(1, sizeof(S)); + s1->a = 123; + s1->b = 456; + printf("%d %d\n", s1->a, s1->b); + printf("%d %d\n", s2->a, s2->b); + *s2 = *s1; + printf("%d %d\n", s2->a, s2->b); +} +EOF + +cat <<'EOF' > expected +123 456 +0 0 +123 456 +EOF + +test_diff <<'EOF' +struct S { + long a; + long b; +}; +typedef struct S S; + +void* calloc(); +int printf(); + +int main() { + S* s1 = calloc(1, sizeof(S)); + S* s2 = calloc(1, sizeof(S)); + s1->a = 123; + s1->b = 456; + printf("%d %d\n", s1->a, s1->b); + printf("%d %d\n", s2->a, s2->b); + *s2 = *s1; + printf("%d %d\n", s2->a, s2->b); +} +EOF + +cat <<'EOF' > expected +123 456 +0 0 +123 456 +EOF + +test_diff <<'EOF' +struct S { + long a; + long b; +}; +typedef struct S S; + +void* calloc(); +int printf(); + +int main() { + S s1; + S s2; + s1.a = 123; + s1.b = 456; + s2.a = 0; + s2.b = 0; + printf("%d %d\n", s1.a, s1.b); + printf("%d %d\n", s2.a, s2.b); + s2 = s1; + printf("%d %d\n", s2.a, s2.b); +} +EOF + diff --git a/tests/switch.sh b/tests/switch.sh new file mode 100644 index 0000000..d5d3dae --- /dev/null +++ b/tests/switch.sh @@ -0,0 +1,212 @@ +#!/bin/bash + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int x = 2; + int result = 0; + + switch (x) { + case 1: + result = 10; + break; + case 2: + result = 20; + break; + case 3: + result = 30; + break; + } + + ASSERT_EQ(20, result); +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int x = 5; + int result = 0; + + switch (x) { + case 1: + result = 10; + break; + case 2: + result = 20; + break; + default: + result = 99; + break; + } + + ASSERT_EQ(99, result); +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int x = 2; + int result = 0; + + switch (x) { + case 1: + result = result + 10; + case 2: + result = result + 20; + case 3: + result = result + 30; + break; + } + + ASSERT_EQ(50, result); // 20 + 30 due to fall-through +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int x = 1; + int y = 2; + int result = 0; + + switch (x) { + case 1: + switch (y) { + case 1: + result = 11; + break; + case 2: + result = 12; + break; + } + break; + case 2: + result = 20; + break; + } + + ASSERT_EQ(12, result); +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int a = 3; + int b = 2; + int result = 0; + + switch (a + b) { + case 4: + result = 40; + break; + case 5: + result = 50; + break; + case 6: + result = 60; + break; + } + + ASSERT_EQ(50, result); +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int x = 2; + int result = 0; + int temp = 0; + + switch (x) { + case 1: + temp = 5; + result = temp * 2; + break; + case 2: + temp = 10; + result = temp * 2; + break; + case 3: + temp = 15; + result = temp * 2; + break; + } + + ASSERT_EQ(20, result); + ASSERT_EQ(10, temp); +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int x = 1; + int result = 0; + + switch (x) { + case 1: { + int local = 100; + result = local; + break; + } + case 2: { + int local = 200; + result = local; + break; + } + } + + ASSERT_EQ(100, result); +} +EOF + +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + int x = 10; + int result = 42; + + switch (x) { + case 1: + result = 10; + break; + case 2: + result = 20; + break; + } + + ASSERT_EQ(42, result); +} +EOF + +cat <<'EOF' > expected +main.c:2: 'case' label not within a switch statement +EOF +test_compile_error <<'EOF' +int main() { + case 1: + return 0; +} +EOF + +cat <<'EOF' > expected +main.c:2: 'default' label not within a switch statement +EOF +test_compile_error <<'EOF' +int main() { + default: + return 0; +} +EOF diff --git a/tests/ternary_operator.sh b/tests/ternary_operator.sh new file mode 100644 index 0000000..c63f581 --- /dev/null +++ b/tests/ternary_operator.sh @@ -0,0 +1,8 @@ +test_exit_code 0 <<'EOF' +#include "../../helpers.h" + +int main() { + ASSERT_EQ(2, 1 ? 2 : 3); + ASSERT_EQ(5, 0 ? 4 : 5); +} +EOF diff --git a/tests/test_arithmetic_operators.sh b/tests/test_arithmetic_operators.sh deleted file mode 100644 index a98ca15..0000000 --- a/tests/test_arithmetic_operators.sh +++ /dev/null @@ -1,11 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - ASSERT_EQ(42, 42); - ASSERT_EQ(21, 5+20-4); - ASSERT_EQ(26, 2*3+4*5); - ASSERT_EQ(197, (((3+5)/2) + (5*(9-6)) * (5+6*7)) % 256); - ASSERT_EQ(30, (-10 + 20 * -3) + 100); -} -EOF diff --git a/tests/test_array_basics.sh b/tests/test_array_basics.sh deleted file mode 100644 index 75bb244..0000000 --- a/tests/test_array_basics.sh +++ /dev/null @@ -1,163 +0,0 @@ -cat <<'EOF' > expected -42 -EOF -test_diff <<'EOF' -int printf(); -void* calloc(); - -int main() { - int b; - int* a = &b; - a[0] = 42; - printf("%d\n", *a); - return 0; -} -EOF - -cat <<'EOF' > expected -0 0 -1 1 -2 2 -3 3 -4 4 -5 5 -6 6 -7 7 -8 8 -9 9 -EOF -test_diff <<'EOF' -int printf(); -void* calloc(); - -int main() { - long* a = calloc(10, sizeof(long)); - long i = 0; - for (i = 0; i < 10; i = i + 1) { - a[i] = i; - } - for (i = 0; i < 10; i = i + 1) { - printf("%d %d\n", *(a + i), a[i]); - } - return 0; -} -EOF - -cat <<'EOF' > expected -65 -65 -66 -67 -68 -EOF -test_diff <<'EOF' -void* calloc(); -int printf(); - -int main() { - char* source = calloc(4, sizeof(char)); - - source[0] = 'A'; - source[1] = 'B'; - source[2] = 'C'; - source[3] = 'D'; - - int a = source[0]; - - printf("%d\n", a); - printf("%d\n", source[0]); - printf("%d\n", source[1]); - printf("%d\n", source[2]); - printf("%d\n", source[3]); - - return 0; -} -EOF - -cat <<'EOF' > expected -0 -1 -4 -9 -16 -25 -36 -49 -64 -81 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - int i; - int a[10]; - for (i = 0; i < 10; ++i) { - a[i] = i * i; - } - for (i = 0; i < 10; ++i) { - printf("%d\n", a[i]); - } -} -EOF - -cat <<'EOF' > expected -0 -42 -48 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -123 -EOF - -test_diff <<'EOF' -int printf(); - -int a; -int b[12]; - -int main() { - printf("%d\n", a); - a = 42; - printf("%d\n", a); - printf("%zu\n", sizeof(b)); - int i; - for (i = 0; i < 12; ++i) { - printf("%d\n", b[i]); - } - b[11] = 123; - printf("%d\n", b[11]); -} -EOF - -cat <<'EOF' > expected -400 -80 -16 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - int a[10 * 10]; - int b[10 + 10]; - int c[1 << 2]; - - printf("%zu\n", sizeof(a)); - printf("%zu\n", sizeof(b)); - printf("%zu\n", sizeof(c)); -} -EOF - diff --git a/tests/test_assignment_operators.sh b/tests/test_assignment_operators.sh deleted file mode 100644 index 9b3a33e..0000000 --- a/tests/test_assignment_operators.sh +++ /dev/null @@ -1,64 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int i = 0; - for (; i < 5; i += 1) {} - ASSERT_EQ(5, i); - - for (i = 5; i >= 0; i -= 1); - ASSERT_EQ(-1, i); - - int x = 123; - x *= 456; - ASSERT_EQ(56088, x); - - int y = 120; - y /= 5; - ASSERT_EQ(24, y); - - int z = 17; - z %= 7; - ASSERT_EQ(3, z); - - int a = 0x05; - a |= 0x0A; - ASSERT_EQ(0x0F, a); - - int b = 0x0F; - b &= 0x0A; - ASSERT_EQ(0x0A, b); - - int c = 7; - c |= 8; - ASSERT_EQ(15, c); - - int d = 15; - d &= 6; - ASSERT_EQ(6, d); - - int e = 0x0F; - e ^= 0x05; - ASSERT_EQ(0x0A, e); - - int f = 3; - f <<= 2; - ASSERT_EQ(12, f); - - int g = 16; - g >>= 2; - ASSERT_EQ(4, g); - - int h = -16; - h >>= 2; - ASSERT_EQ(-4, h); - - int j = 1; - j <<= 4; - ASSERT_EQ(16, j); - - int k = 64; - k >>= 3; - ASSERT_EQ(8, k); -} -EOF diff --git a/tests/test_bitwise_operators.sh b/tests/test_bitwise_operators.sh deleted file mode 100644 index f683099..0000000 --- a/tests/test_bitwise_operators.sh +++ /dev/null @@ -1,40 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - ASSERT_EQ(123, 0 | 123); - ASSERT_EQ(460, 12 | 456); - - ASSERT_EQ(8, 1 << 3); - ASSERT_EQ(336, 21 << 4); - ASSERT_EQ(13, 111 >> 3); - ASSERT_EQ(0, 15 >> 14); - - int a = 5; - int b = 3; - ASSERT_EQ(1, a & b); - ASSERT_EQ(7, a | b); - ASSERT_EQ(6, a ^ b); - ASSERT_EQ(4, 2 + 3 & 4); - - int c = 1 + 2 & 3; - int d = 4 & 5 ^ 6; - int e = 1 ^ 2 | 3; - int f = 0 | 1 & 2; - ASSERT_EQ(3, c); - ASSERT_EQ(2, d); - ASSERT_EQ(3, e); - ASSERT_EQ(0, f); - - ASSERT_EQ(-1, ~0); - ASSERT_EQ(-2, ~1); - ASSERT_EQ(-6, ~5); - ASSERT_EQ(0, ~(-1)); - ASSERT_EQ(5, ~(-6)); - - int x = 10; - ASSERT_EQ(-11, ~x); - ASSERT_EQ(-1, ~(x & 0)); - ASSERT_EQ(-16, ~(x | 5)); -} -EOF diff --git a/tests/test_bool_type.sh b/tests/test_bool_type.sh deleted file mode 100644 index d18fd30..0000000 --- a/tests/test_bool_type.sh +++ /dev/null @@ -1,12 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - bool b1 = true, b0 = false; - ASSERT_EQ(1, b1); - ASSERT_EQ(0, b0); - ASSERT_EQ(1, sizeof(b1)); - ASSERT_EQ(1, sizeof(b0)); - ASSERT_EQ(1, sizeof(bool)); -} -EOF diff --git a/tests/test_cast_expressions.sh b/tests/test_cast_expressions.sh deleted file mode 100644 index f6824ad..0000000 --- a/tests/test_cast_expressions.sh +++ /dev/null @@ -1,154 +0,0 @@ -cat <<'EOF' > expected -65 -65 -127 -1 -42 -99 -10 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - char c = 65; - int i = (int)c; - printf("%d\n", i); - - int i2 = 321; - char c2 = (char)i2; - printf("%d\n", c2); - - short s = 127; - int i3 = (int)s; - printf("%d\n", i3); - - int i4 = 65537; - short s2 = (short)i4; - printf("%d\n", s2); - - long l = 42; - int i5 = (int)l; - printf("%d\n", i5); - - int i6 = 99; - long l2 = (long)i6; - printf("%d\n", (int)l2); - - char c3 = 10; - short s3 = (short)c3; - int i7 = (int)s3; - long l3 = (long)i7; - printf("%d\n", (int)l3); - - return 0; -} -EOF - -cat <<'EOF' > expected -Result: 130 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - char c = 65; - int result = (int)c + (int)c; - printf("Result: %d\n", result); - return 0; -} -EOF - -cat <<'EOF' > expected -10 -20 -30 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - char a = 5; - char b = 5; - int sum = (int)a + (int)b; - printf("%d\n", sum); - - short s1 = 10; - short s2 = 10; - int sum2 = (int)s1 + (int)s2; - printf("%d\n", sum2); - - long l1 = 15; - long l2 = 15; - int sum3 = (int)(l1 + l2); - printf("%d\n", sum3); - - return 0; -} -EOF - -cat <<'EOF' > expected -10 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - char c = -10; - int i = (int)c; - printf("%d\n", -i); - return 0; -} -EOF - -cat <<'EOF' > expected -Char: 65 -Int: 65 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -char get_char() { - return 65; -} - -int main() { - char c = get_char(); - int i = (int)get_char(); - printf("Char: %d\n", c); - printf("Int: %d\n", i); - return 0; -} -EOF - -cat <<'EOF' > expected -Equal -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - char c = 42; - int i = 42; - if ((int)c == i) { - printf("Equal\n"); - } else { - printf("Not equal\n"); - } - return 0; -} -EOF - -cat <<'EOF' > expected -55 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - long l = 55; - char c = (char)(short)(int)l; - printf("%d\n", c); - return 0; -} -EOF diff --git a/tests/test_char_literals.sh b/tests/test_char_literals.sh deleted file mode 100644 index c883eba..0000000 --- a/tests/test_char_literals.sh +++ /dev/null @@ -1,25 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - ASSERT_EQ(97, 'a'); - ASSERT_EQ(48, '0'); - ASSERT_EQ(92, '\\'); - ASSERT_EQ(39, '\''); - ASSERT_EQ(10, '\n'); - - ASSERT_EQ(39, '\''); - ASSERT_EQ(34, '\"'); - ASSERT_EQ(63, '\?'); - ASSERT_EQ(92, '\\'); - ASSERT_EQ(7, '\a'); - ASSERT_EQ(8, '\b'); - ASSERT_EQ(12, '\f'); - ASSERT_EQ(10, '\n'); - ASSERT_EQ(13, '\r'); - ASSERT_EQ(9, '\t'); - ASSERT_EQ(11, '\v'); - - ASSERT_EQ(0, '\0'); -} -EOF diff --git a/tests/test_comma_operator.sh b/tests/test_comma_operator.sh deleted file mode 100644 index 7c53845..0000000 --- a/tests/test_comma_operator.sh +++ /dev/null @@ -1,37 +0,0 @@ -cat <<'EOF' > expected -0 0 -1 1 -2 2 -3 3 -4 4 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - int i = 1000; - int j = 1000; - for (i = 0, j = 0; i < 5; i++, j++) { - printf("%d %d\n", i, j); - } -} -EOF - -cat <<'EOF' > expected -1 2 3 4 -0 0 5 -EOF - -test_diff <<'EOF' -int printf(); -int x, y, z = 5; -int main() { - int a, b; - a = 1, b = 2; - int c = 3, d = 4; - printf("%d %d %d %d\n", a, b, c, d); - printf("%d %d %d\n", x, y, z); -} -EOF - diff --git a/tests/test_command_line_args.sh b/tests/test_command_line_args.sh deleted file mode 100644 index 094b36c..0000000 --- a/tests/test_command_line_args.sh +++ /dev/null @@ -1,19 +0,0 @@ -cat <<'EOF' > expected -argc = 4 -argv[1] = hoge -argv[2] = piyo -argv[3] = fuga -EOF -test_diff hoge piyo fuga<<'EOF' -int printf(); - -int main(int argc, char** argv) { - printf("argc = %d\n", argc); - int i; - for (i = 1; i < argc; ++i) { - printf("argv[%d] = %s\n", i, argv[i]); - } - return 0; -} -EOF - diff --git a/tests/test_comments.sh b/tests/test_comments.sh deleted file mode 100644 index 7a43832..0000000 --- a/tests/test_comments.sh +++ /dev/null @@ -1,25 +0,0 @@ -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -// TODO: check if the token is at the beginning of line. -// TODO: check if skipped whitespaces do not contain line breaks. -int main() { - return 0; -} -EOF - -cat <<'EOF' > expected -EOF - -test_diff <<'EOF' -/* lorem ipsum */ -/* -lorem ipsum -*/ -// /* -/* /* - */ -int/**/main() { -} -EOF - diff --git a/tests/test_comparison_operators.sh b/tests/test_comparison_operators.sh deleted file mode 100644 index 7bcb689..0000000 --- a/tests/test_comparison_operators.sh +++ /dev/null @@ -1,14 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - ASSERT_EQ(1, 0 == 0); - ASSERT_EQ(0, 123 != 123); - ASSERT_EQ(1, 123 != 456); - ASSERT_EQ(0, 123 == 124); - ASSERT_EQ(1, 123 < 567); - ASSERT_EQ(1, 123 <= 567); - ASSERT_EQ(1, 123 <= 123); - ASSERT_EQ(0, 123 < 123); -} -EOF diff --git a/tests/test_compile_errors.sh b/tests/test_compile_errors.sh deleted file mode 100644 index 23bf394..0000000 --- a/tests/test_compile_errors.sh +++ /dev/null @@ -1,33 +0,0 @@ -cat <<'EOF' > expected -undefined function: f -EOF -test_compile_error <<'EOF' -int main() { - f(); -} -EOF - -# TODO: improve error message -# cat <<'EOF' > expected -# main.c:1: expected ';' or '{', but got '}' -# EOF -cat <<'EOF' > expected -main.c:1: expected ';', but got '}' -EOF - -test_compile_error <<'EOF' -int main() } -EOF - -# TODO: improve error message -# cat <<'EOF' > expected -# main.c:1: expected ';' or '{', but got '}' -# EOF -cat <<'EOF' > expected -main.c:1: expected ';', but got '123' -EOF - -test_compile_error <<'EOF' -int main() 123 -EOF - diff --git a/tests/test_compiler_output.sh b/tests/test_compiler_output.sh deleted file mode 100644 index 6993c36..0000000 --- a/tests/test_compiler_output.sh +++ /dev/null @@ -1,17 +0,0 @@ -cat > foo.c <<'EOF' -int main() {} -EOF - -"$ducc" -o bar.s foo.c -if [[ $? -ne 0 ]]; then - exit 1 -fi -gcc -o a.out bar.s -./a.out "$@" -exit_code=$? - -if [[ $exit_code -ne 0 ]]; then - echo "invalid exit code: $exit_code" >&2 - exit 1 -fi - diff --git a/tests/test_compiler_version.sh b/tests/test_compiler_version.sh deleted file mode 100644 index 1e22718..0000000 --- a/tests/test_compiler_version.sh +++ /dev/null @@ -1,7 +0,0 @@ -expected="ducc v0.2.0" - -if [[ "$("$ducc" --version)" != "$expected" ]]; then - echo "invalid output" >&2 - exit 1 -fi - diff --git a/tests/test_conditional_errors.sh b/tests/test_conditional_errors.sh deleted file mode 100644 index 5a33e8c..0000000 --- a/tests/test_conditional_errors.sh +++ /dev/null @@ -1,74 +0,0 @@ -cat <<'EOF' > expected -main.c:2: expected '#endif', but got '' -EOF -test_compile_error <<'EOF' -#if 0 -EOF - -cat <<'EOF' > expected -main.c:3: expected '#endif', but got '' -EOF -test_compile_error <<'EOF' -#if 1 -#else -EOF - -cat <<'EOF' > expected -main.c:3: expected '#endif', but got '' -EOF -test_compile_error <<'EOF' -#if 1 -#elif 1 -EOF - -cat <<'EOF' > expected -main.c:1: unexpected '#elif'; no corresponding '#if'* -EOF -test_compile_error <<'EOF' -#elif -#endif - -int main() {} -EOF - -cat <<'EOF' > expected -main.c:1: unexpected '#endif'; no corresponding '#if'* -EOF -test_compile_error <<'EOF' -#endif - -int main() {} -EOF - -cat <<'EOF' > expected -main.c:1: unexpected '#else'; no corresponding '#if'* -EOF -test_compile_error <<'EOF' -#else -#endif - -int main() {} -EOF - -cat <<'EOF' > expected -main.c:4: expected '#endif', but got '' -EOF -test_compile_error <<'EOF' -#if 1 - -int main() {} -EOF - -cat <<'EOF' > a.h -#if 1 -EOF -cat <<'EOF' > expected -./a.h:2: expected '#endif', but got '' -EOF -test_compile_error <<'EOF' -#include "a.h" -#endif - -int main() {} -EOF - diff --git a/tests/test_define_macros.sh b/tests/test_define_macros.sh deleted file mode 100644 index 1b674f1..0000000 --- a/tests/test_define_macros.sh +++ /dev/null @@ -1,92 +0,0 @@ -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -#define A 1 -int main() { - return 0; -} -EOF - -cat <<'EOF' > expected -1,2,3 -EOF -test_diff <<'EOF' -int printf(); - -#define A 1 -#define B 2 -#define C 3 - -int main() { - printf("%d,%d,%d\n", A, B, C); - return 0; -} -EOF - -cat <<'EOF' > expected -0,0,0,0 -EOF -test_diff <<'EOF' -int printf(); - -#define NULL 0 -#define TK_EOF 0 -#define TY_UNKNOWN 0 -#define AST_UNKNOWN 0 - -int main() { - printf("%d,%d,%d,%d\n", NULL, TK_EOF, TY_UNKNOWN, AST_UNKNOWN); - return 0; -} -EOF - -cat <<'EOF' > expected -42,123 -EOF -test_diff <<'EOF' -int printf(); - -#define A foo_a -#define B foo_b - -int main() { - int foo_a = 42; - int foo_b = 123; - - printf("%d,%d\n", A, B); - - return 0; -} -EOF - -cat <<'EOF' > expected -123 -EOF - -test_diff <<'EOF' -#define foo() 123 - -int printf(); - -int main() { - printf("%d\n", foo()); -} -EOF - -cat <<'EOF' > expected -579 -975 -EOF - -test_diff <<'EOF' -#define foo 123 + 456 -#define bar() 321 + 654 - -int printf(); - -int main() { - printf("%d\n", foo); - printf("%d\n", bar()); -} -EOF - diff --git a/tests/test_do_while.sh b/tests/test_do_while.sh deleted file mode 100644 index dbb4815..0000000 --- a/tests/test_do_while.sh +++ /dev/null @@ -1,92 +0,0 @@ -cat <<'EOF' > expected -body 0 -foo 1 -body 1 -foo 2 -body 2 -foo 3 -body 3 -foo 4 -body 4 -foo 5 -EOF -test_diff <<'EOF' -int printf(); - -int foo(int i) { - printf("foo %d\n", i); - return i; -} - -int main() { - int i = 0; - do { - printf("body %d\n", i); - ++i; - } while (foo(i) < 5); - - return 0; -} -EOF - -cat <<'EOF' > expected -body 0 -foo 1 -body 1 -foo 2 -body 2 -EOF -test_diff <<'EOF' -int printf(); - -int foo(int i) { - printf("foo %d\n", i); - return i; -} - -int main() { - int i = 0; - do { - printf("body %d\n", i); - ++i; - if (i == 3) { - break; - } - } while (foo(i) < 5); - - return 0; -} -EOF - -cat <<'EOF' > expected -body 1 -foo 1 -foo 2 -body 3 -foo 3 -foo 4 -body 5 -foo 5 -EOF -test_diff <<'EOF' -int printf(); - -int foo(int i) { - printf("foo %d\n", i); - return i; -} - -int main() { - int i = 0; - do { - ++i; - if (i % 2 == 0) { - continue; - } - printf("body %d\n", i); - } while (foo(i) < 5); - - return 0; -} -EOF - diff --git a/tests/test_empty_preprocessor.sh b/tests/test_empty_preprocessor.sh deleted file mode 100644 index 57e33ad..0000000 --- a/tests/test_empty_preprocessor.sh +++ /dev/null @@ -1,10 +0,0 @@ -touch expected -test_diff <<'EOF' -# -# -# -# -# -int main() {} -EOF - diff --git a/tests/test_empty_statements.sh b/tests/test_empty_statements.sh deleted file mode 100644 index 71caed5..0000000 --- a/tests/test_empty_statements.sh +++ /dev/null @@ -1,7 +0,0 @@ -cat <<'EOF' > expected -EOF - -test_diff <<'EOF' -int main() { ; } -EOF - diff --git a/tests/test_enums.sh b/tests/test_enums.sh deleted file mode 100644 index 3fc15a0..0000000 --- a/tests/test_enums.sh +++ /dev/null @@ -1,48 +0,0 @@ -cat <<'EOF' > expected -4 -0,1,2 -EOF -test_diff <<'EOF' -int printf(); - -enum E { - A, - B, - C, -}; - -int main() { - enum E x = A; - printf("%d\n", sizeof(enum E)); - printf("%d,%d,%d\n", A, B, C); - return 0; -} -EOF - -cat <<'EOF' > expected -10,11,20,21 -0,5,6,6 -EOF - -test_diff <<'EOF' -int printf(); - -enum E1 { - A = 10, - B, - C = 20, - D, -}; - -enum E2 { - E, - F = 5, - G, - H = G, -}; - -int main() { - printf("%d,%d,%d,%d\n", A, B, C, D); - printf("%d,%d,%d,%d\n", E, F, G, H); -} -EOF diff --git a/tests/test_error_directive.sh b/tests/test_error_directive.sh deleted file mode 100644 index a8b3fdb..0000000 --- a/tests/test_error_directive.sh +++ /dev/null @@ -1,27 +0,0 @@ -cat <<'EOF' > expected -main.c:1: foo -EOF -test_compile_error <<'EOF' -#error "foo" -EOF - -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -#if 0 -#error "foo" -#endif -int main() {} -EOF - -cat <<'EOF' > expected -main.c:5: foo -EOF -test_compile_error <<'EOF' -#define \ - A \ - B - -#error "foo" -EOF - diff --git a/tests/test_example_programs.sh b/tests/test_example_programs.sh deleted file mode 100644 index 21b7ca4..0000000 --- a/tests/test_example_programs.sh +++ /dev/null @@ -1,133 +0,0 @@ -cat <<'EOF' > expected -1 -2 -Fizz -4 -Buzz -Fizz -7 -8 -Fizz -Buzz -11 -Fizz -13 -14 -FizzBuzz -16 -17 -Fizz -19 -Buzz -Fizz -22 -23 -Fizz -Buzz -26 -Fizz -28 -29 -FizzBuzz -31 -32 -Fizz -34 -Buzz -Fizz -37 -38 -Fizz -Buzz -41 -Fizz -43 -44 -FizzBuzz -46 -47 -Fizz -49 -Buzz -Fizz -52 -53 -Fizz -Buzz -56 -Fizz -58 -59 -FizzBuzz -61 -62 -Fizz -64 -Buzz -Fizz -67 -68 -Fizz -Buzz -71 -Fizz -73 -74 -FizzBuzz -76 -77 -Fizz -79 -Buzz -Fizz -82 -83 -Fizz -Buzz -86 -Fizz -88 -89 -FizzBuzz -91 -92 -Fizz -94 -Buzz -Fizz -97 -98 -Fizz -Buzz -EOF -test_diff <<'EOF' -int printf(); - -int main() { - int i; - for (i = 1; i <= 100; i = i + 1) { - if (i % 15 == 0) { - printf("FizzBuzz\n"); - } else if (i % 3 == 0) { - printf("Fizz\n"); - } else if (i % 5 == 0) { - printf("Buzz\n"); - } else { - printf("%d\n", i); - } - } - return 0; -} -EOF - -cat <<'EOF' > expected -hello, world -EOF -test_diff <<'EOF' -int printf(); -int main() { - printf("hello, world\n"); - return 0; -} -EOF - diff --git a/tests/test_file_io.sh b/tests/test_file_io.sh deleted file mode 100644 index 07a335e..0000000 --- a/tests/test_file_io.sh +++ /dev/null @@ -1,31 +0,0 @@ -cat <<'EOF' > input -foo -bar -baz -EOF -cat <<'EOF' > expected -foo -foo -bar -bar -baz -baz -EOF -test_diff hoge piyo fuga<<'EOF' -struct FILE; -typedef struct FILE FILE; -extern FILE* stdin; -extern FILE* stdout; -int fprintf(); -char* fgets(); -void* calloc(); - -int main() { - char* buf = calloc(256, sizeof(char)); - while (fgets(buf, 256, stdin)) { - fprintf(stdout, "%s%s", buf, buf); - } - return 0; -} -EOF - diff --git a/tests/test_for_loops.sh b/tests/test_for_loops.sh deleted file mode 100644 index 2e3205f..0000000 --- a/tests/test_for_loops.sh +++ /dev/null @@ -1,141 +0,0 @@ -test_exit_code 45 <<'EOF' -int main() { - int i; - int ret; - i = 0; - ret = 0; - for (i = 0; i < 10; i = i + 1) { - ret = ret + i; - } - return ret; -} -EOF - -test_exit_code 25 <<'EOF' -int main() { - int i; - int ret; - i = 0; - ret = 0; - for (i = 0; i < 10; i = i + 1) { - if (i % 2 == 0) { - continue; - } - ret = ret + i; - } - return ret; -} -EOF - -test_exit_code 66 <<'EOF' -int main() { - int i; - int ret; - i = 0; - ret = 0; - for (i = 0; i < 100; i = i + 1) { - if (i == 12) { - break; - } - ret = ret + i; - } - return ret; -} -EOF - -cat <<'EOF' > expected -0 -1 -2 -3 -4 -10 -11 -12 -13 -14 -20 -21 -22 -23 -24 -25 -EOF -test_diff <<'EOF' -int printf(); - -int main() { - int i = 0; - for (; i < 5; i = i + 1) { - printf("%d\n", i); - } - for (i = 10; i < 15; ) { - printf("%d\n", i); - i = i + 1; - } - for (i = 20; ; i = i + 1) { - printf("%d\n", i); - if (i == 25) break; - } - - return 0; -} -EOF - -cat <<'EOF' > expected -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - for (int i = 0; i < 10; i++) { - printf("%d\n", i); - } -} -EOF - -cat <<'EOF' > expected -0 -1 -2 -3 -4 -0 -1 -2 -3 -4 -43 -42 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - for (int i = 0; i < 5; i++) { - printf("%d\n", i); - } - for (int i = 0; i < 5; i++) { - printf("%d\n", i); - } - int x = 42; - { - int x = 43; - printf("%d\n", x); - } - printf("%d\n", x); -} -EOF - diff --git a/tests/test_function_basics.sh b/tests/test_function_basics.sh deleted file mode 100644 index f064622..0000000 --- a/tests/test_function_basics.sh +++ /dev/null @@ -1,58 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int foo() { - int i; - int ret; - i = 0; - ret = 0; - for (i = 0; i < 100; i = i + 1) { - if (i == 12) { - break; - } - ret = ret + i; - } - return ret; -} - -int f(int a, int b, int c, int d, int e, int f) { - return a; -} - -int f2(int a, int b, int c, int d, int e, int f) { - return b; -} - -int f3(int a, int b, int c, int d, int e, int f) { - return c; -} - -int f4(int a, int b, int c, int d, int e, int f) { - return d; -} - -int f5(int a, int b, int c, int d, int e, int f) { - return e; -} - -int f6(int a, int b, int c, int d, int e, int f) { - return f; -} - -int main() { - ASSERT_EQ(66, foo()); - ASSERT_EQ(10, 10 * f(1, 2, 3, 4, 5, 6)); - ASSERT_EQ(20, 10 * f2(1, 2, 3, 4, 5, 6)); - ASSERT_EQ(30, 10 * f3(1, 2, 3, 4, 5, 6)); - ASSERT_EQ(40, 10 * f4(1, 2, 3, 4, 5, 6)); - ASSERT_EQ(50, 10 * f5(1, 2, 3, 4, 5, 6)); - ASSERT_EQ(60, 10 * f6(1, 2, 3, 4, 5, 6)); -} -EOF - -touch expected -test_diff <<'EOF' -int main() { - return 0; -} -EOF diff --git a/tests/test_function_macros.sh b/tests/test_function_macros.sh deleted file mode 100644 index 1108650..0000000 --- a/tests/test_function_macros.sh +++ /dev/null @@ -1,110 +0,0 @@ -cat <<'EOF' > expected -42 -246 -221 -EOF - -test_diff <<'EOF' -int printf(); - -#define A(x) x -#define B(x) x+x -#define C(x, y) x*y - -int main() { - printf("%d\n", A(42)); - printf("%d\n", B - (123)); - printf("%d\n", C (13, 17)); -} -EOF - -cat <<'EOF' > expected -123 -EOF -test_diff <<'EOF' -#define A(x) x - -int printf(); - -int main() { - printf("%d\n", A ( 123 )); -} -EOF - -cat <<'EOF' > expected -main.c:4: expected ')', but got '' -EOF -test_compile_error <<'EOF' -#define A(x) x - -int main() { -#if A ( - 123) < A(345) - printf("1\n"); -#endif -} -EOF - -cat <<'EOF' > expected -42 -100 200 -300 -0 -1 2 3 -15 -42 -123 -879 -EOF - -test_diff <<'EOF' -int printf(); - -#define ADD(a, b) ((a) + (b)) -#define PRINT_TWO(x, y) printf("%d %d\n", x, y) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define NESTED(x) (x) -#define CONCAT3(a, b, c) a ## b ## c - -int main() { - printf("%d\n", ADD(40, 2)); - PRINT_TWO(100, 200); - printf("%d\n", MAX(100 + 200, 250)); - printf("%d\n"); - NESTED((printf("1 "), printf("2 "), printf("3\n"), 0)); - - int x = 5, y = 10; - printf("%d\n", ADD(x + 2, y - 2)); - printf("%d\n", ADD(MAX(10, 20), MAX(15 + 5, 22))); - printf("%d\n", ADD( 100 , 23 )); - printf("%d\n", ADD(NESTED((100 + 200)), MAX((123 + 456), (111 + 222)))); -} -EOF - -cat <<'EOF' > expected -int printf ( const char *, ...); - -int main () { - int foo = 42; - printf ( %d\n, foo); - - int a = 123; - printf ( %d\n, a); -} -EOF -test_cpp <<'EOF' -int printf(const char*, ...); - -int main() { -#define foo foo - int foo = 42; - printf("%d\n", foo); - -#define a b -#define b c -#define c a - int a = 123; - printf("%d\n", a); -} -EOF diff --git a/tests/test_function_pointers.sh b/tests/test_function_pointers.sh deleted file mode 100644 index cddc623..0000000 --- a/tests/test_function_pointers.sh +++ /dev/null @@ -1,24 +0,0 @@ -cat <<'EOF' > expected -a -h -g -EOF - -test_diff <<'EOF' -int* f1(int a); -int (*f2)(int a); - -extern int atexit (void (*) (void)); -extern int atexit (void (*fn) (void)); - -int printf(const char*, ...); - -void g() { printf("g\n"); } -void h() { printf("h\n"); } - -int main() { - atexit(g); - atexit(h); - printf("a\n"); -} -EOF diff --git a/tests/test_global_variables.sh b/tests/test_global_variables.sh deleted file mode 100644 index 8de1483..0000000 --- a/tests/test_global_variables.sh +++ /dev/null @@ -1,39 +0,0 @@ -cat <<'EOF' > expected -42 123 999 -EOF - -test_diff <<'EOF' -int printf(); - -char a = 42; -short b = 123; -int c = 999; - -int main() { - printf("%d %d %d\n", a, b, c); -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int a; -int* b = &a; -int c[10]; -int* d = c; -int e, *f = e, g[10], *h = g; - -int main() { - *b = 123; - ASSERT_EQ(123, a); - - d[2] = 42; - ASSERT_EQ(42, c[2]); - - *f = 456; - ASSERT_EQ(456, e); - - h[5] = 789; - ASSERT_EQ(789, g[5]); -} -EOF diff --git a/tests/test_goto.sh b/tests/test_goto.sh deleted file mode 100644 index 1e28a51..0000000 --- a/tests/test_goto.sh +++ /dev/null @@ -1,168 +0,0 @@ -touch expected -test_diff <<'EOF' -int main() { - goto end; - return 1; -end: - return 0; -} -EOF - -cat <<'EOF' > expected -1 -2 -3 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - int i = 0; -loop: - i++; - printf("%d\n", i); - if (i < 3) - goto loop; - return 0; -} -EOF - -cat < /dev/null > expected -test_diff <<'EOF' -int main() { - goto skip; - int x = 5; -skip: - return 0; -} -EOF - -cat <<'EOF' > expected -start -middle -end -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - printf("start\n"); - goto middle; -first: - printf("first\n"); - goto end; -middle: - printf("middle\n"); - goto end; -last: - printf("last\n"); -end: - printf("end\n"); - return 0; -} -EOF - -cat <<'EOF' > expected -before -after -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - printf("before\n"); - { - { - goto out; - printf("inside\n"); - } - printf("middle\n"); - } -out: - printf("after\n"); - return 0; -} -EOF - -cat <<'EOF' > expected -x is 5 -x is 10 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - int x = 5; - if (x == 5) { - printf("x is 5\n"); - goto next; - } - printf("x is not 5\n"); -next: - x = 10; - printf("x is %d\n", x); - return 0; -} -EOF - -cat <<'EOF' > expected -case 2 -done -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { - int x = 2; - switch (x) { - case 1: - printf("case 1\n"); - break; - case 2: - printf("case 2\n"); - goto done; - case 3: - printf("case 3\n"); - break; - } - printf("after switch\n"); -done: - printf("done\n"); - return 0; -} -EOF - -# cat <<'EOF' > expected -# error: use of undeclared label 'undefined' -# EOF -# test_compile_error <<'EOF' -# int main() { -# goto undefined; -# return 0; -# } -# EOF - -# cat <<'EOF' > expected -# error: redefinition of label 'duplicate' -# EOF -# test_compile_error <<'EOF' -# int main() { -# duplicate: -# ; -# duplicate: -# return 0; -# } -# EOF - -# cat <<'EOF' > expected -# error: label at end of compound statement -# EOF -# test_compile_error <<'EOF' -# int main() { -# { -# goto end; -# end: -# } -# return 0; -# } -# EOF diff --git a/tests/test_identifiers.sh b/tests/test_identifiers.sh deleted file mode 100644 index 5786ff7..0000000 --- a/tests/test_identifiers.sh +++ /dev/null @@ -1,16 +0,0 @@ -cat <<'EOF' > expected -42,123 -EOF -test_diff <<'EOF' -int printf(); - -int main() { - int _a = 42; - int _b = 123; - - printf("%d,%d\n", _a, _b); - - return 0; -} -EOF - diff --git a/tests/test_if_elif_else.sh b/tests/test_if_elif_else.sh deleted file mode 100644 index e0adee0..0000000 --- a/tests/test_if_elif_else.sh +++ /dev/null @@ -1,215 +0,0 @@ -cat <<'EOF' > expected -int printf (); - - -int main () { - printf ( 1\n); -} -EOF - -test_cpp <<'EOF' -int printf(); - -#define A -#define B - -int main() { -#if defined A && defined(B) - printf("1\n"); -#else - printf("2\n"); -#endif -} -EOF - -cat <<'EOF' > expected -int printf (); - -int main () { - printf ( 1\n); - - printf ( 1\n); - - printf ( 2\n); - - printf ( 3\n); -} -EOF - -test_cpp <<'EOF' -int printf(); - -int main() { -#if 1 - printf("1\n"); -#elif 1 - printf("2\n"); -#else - printf("3\n"); -#endif - -#if 1 - printf("1\n"); -#elif 0 - printf("2\n"); -#else - printf("3\n"); -#endif - -#if 0 - printf("1\n"); -#elif 1 - printf("2\n"); -#else - printf("3\n"); -#endif - -#if 0 - printf("1\n"); -#elif 0 - printf("2\n"); -#else - printf("3\n"); -#endif -} -EOF - -cat <<'EOF' > expected -int printf (); - -int main () { - printf ( 1\n); - - printf ( 1\n); - - printf ( 2\n); - - printf ( 2\n); - - printf ( 3\n); - - printf ( 4\n); - - printf ( 3\n); - - printf ( 4\n); -} -EOF - -test_cpp <<'EOF' -int printf(); - -int main() { -#if 1 -# if 1 - printf("1\n"); -# else - printf("2\n"); -# endif -#else -# if 1 - printf("3\n"); -# else - printf("4\n"); -# endif -#endif - -#if 1 -# if 1 - printf("1\n"); -# else - printf("2\n"); -# endif -#else -# if 0 - printf("3\n"); -# else - printf("4\n"); -# endif -#endif - -#if 1 -# if 0 - printf("1\n"); -# else - printf("2\n"); -# endif -#else -# if 1 - printf("3\n"); -# else - printf("4\n"); -# endif -#endif - -#if 1 -# if 0 - printf("1\n"); -# else - printf("2\n"); -# endif -#else -# if 0 - printf("3\n"); -# else - printf("4\n"); -# endif -#endif - -#if 0 -# if 1 - printf("1\n"); -# else - printf("2\n"); -# endif -#else -# if 1 - printf("3\n"); -# else - printf("4\n"); -# endif -#endif - -#if 0 -# if 1 - printf("1\n"); -# else - printf("2\n"); -# endif -#else -# if 0 - printf("3\n"); -# else - printf("4\n"); -# endif -#endif - -#if 0 -# if 0 - printf("1\n"); -# else - printf("2\n"); -# endif -#else -# if 1 - printf("3\n"); -# else - printf("4\n"); -# endif -#endif - -#if 0 -# if 0 - printf("1\n"); -# else - printf("2\n"); -# endif -#else -# if 0 - printf("3\n"); -# else - printf("4\n"); -# endif -#endif -} -EOF diff --git a/tests/test_if_else.sh b/tests/test_if_else.sh deleted file mode 100644 index f1da0d1..0000000 --- a/tests/test_if_else.sh +++ /dev/null @@ -1,21 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int result1; - if (1) { - result1 = 12; - } else { - result1 = 34; - } - ASSERT_EQ(12, result1); - - int result2; - if (1 + 1 != 2) { - result2 = 12; - } else { - result2 = 34; - } - ASSERT_EQ(34, result2); -} -EOF diff --git a/tests/test_ifdef_ifndef.sh b/tests/test_ifdef_ifndef.sh deleted file mode 100644 index 683a1dc..0000000 --- a/tests/test_ifdef_ifndef.sh +++ /dev/null @@ -1,95 +0,0 @@ -cat <<'EOF' > expected -int printf (); - - -int main () { - - printf ( __ducc__ is defined.\n); - - printf ( A is defined.\n); - - printf ( B is undefined.\n); - - - return 0; -} -EOF - -test_cpp <<'EOF' -int printf(); - -#define A 123 - -int main() { - -#ifdef __ducc__ - printf("__ducc__ is defined.\n"); -#else - printf("__ducc__ is undefined.\n"); -#endif - -#ifdef A - printf("A is defined.\n"); -#else - printf("A is undefined.\n"); -#endif - -#ifdef B - printf("B is defined.\n"); -#else - printf("B is undefined.\n"); -#endif - -#define B 456 - - return 0; -} -EOF - -cat <<'EOF' > expected -int printf (); - - -int main () { - - printf ( __ducc__ is defined.\n); - - printf ( A is defined.\n); - - printf ( B is undefined.\n); - - - return 0; -} -EOF - -test_cpp <<'EOF' -int printf(); - -#define A 123 - -int main() { - -#ifndef __ducc__ - printf("__ducc__ is undefined.\n"); -#else - printf("__ducc__ is defined.\n"); -#endif - -#ifndef A - printf("A is undefined.\n"); -#else - printf("A is defined.\n"); -#endif - -#ifndef B - printf("B is undefined.\n"); -#else - printf("B is defined.\n"); -#endif - -#define B 456 - - return 0; -} -EOF diff --git a/tests/test_implicit_return.sh b/tests/test_implicit_return.sh deleted file mode 100644 index 423844e..0000000 --- a/tests/test_implicit_return.sh +++ /dev/null @@ -1,19 +0,0 @@ -# C99: 5.1.2.2.3 -test_exit_code 0 <<'EOF' -int main() { -} -EOF - -test_exit_code 0 <<'EOF' -int main() { - 1 + 2 + 3; -} -EOF - -test_exit_code 0 <<'EOF' -int main() { - if (1); - else return 1; -} -EOF - diff --git a/tests/test_include_errors.sh b/tests/test_include_errors.sh deleted file mode 100644 index 3ac546f..0000000 --- a/tests/test_include_errors.sh +++ /dev/null @@ -1,52 +0,0 @@ -cat <<'EOF' > expected -main.c:1: cannot open include file: "nonexistent.h" -EOF - -test_compile_error <<'EOF' -#include "nonexistent.h" - -int main() { - return 0; -} -EOF - -cat <<'EOF' > expected -include depth limit exceeded -EOF - -# Create circular include files -cat <<'EOF' > a.h -#include "b.h" -int a() { return 1; } -EOF - -cat <<'EOF' > b.h -#include "a.h" -int b() { return 2; } -EOF - -test_compile_error <<'EOF' -#include "a.h" - -int main() { - a() + b(); - return 0; -} -EOF - -cat <<'EOF' > expected -main.c:1: cannot open include file: "hoge.h" -EOF - -test_compile_error <<'EOF' -#include "hoge.h" -EOF - -cat <<'EOF' > expected -main.c:1: cannot resolve include file name: -EOF - -test_compile_error <<'EOF' -#include -EOF - diff --git a/tests/test_include_guards.sh b/tests/test_include_guards.sh deleted file mode 100644 index 87c44a9..0000000 --- a/tests/test_include_guards.sh +++ /dev/null @@ -1,26 +0,0 @@ -cat <<'EOF' > expected -42 -EOF - -cat <<'EOF' > header.h -#ifndef HEADER_H -#define HEADER_H - -int f() { return 42; } - -#endif -EOF - -test_diff <<'EOF' -#include "header.h" -#include "header.h" -#include "header.h" - -int printf(); - -int main() { - printf("%d\n", HEADER_H HEADER_H HEADER_H f() HEADER_H HEADER_H HEADER_H); - return 0; -} -EOF - diff --git a/tests/test_include_local.sh b/tests/test_include_local.sh deleted file mode 100644 index c9fe755..0000000 --- a/tests/test_include_local.sh +++ /dev/null @@ -1,100 +0,0 @@ -cat <<'EOF' > header.h -int add(int a, int b) { - return a + b; -} - -int printf(const char*, ...); -EOF - -cat <<'EOF' > expected -8 -EOF -test_diff <<'EOF' -#include "header.h" - -int main() { - printf("%d\n", add(5, 3)); - return 0; -} -EOF - -mkdir -p foo -cat <<'EOF' > bar.h -#include "baz.h" -EOF -cat <<'EOF' > baz.h -#define A 123 -EOF -cat <<'EOF' > foo/bar.h -#include "baz.h" -EOF -cat <<'EOF' > foo/baz.h -#define A 456 -EOF - -cat <<'EOF' > expected -123 -456 -EOF -test_diff <<'EOF' -int printf(const char*, ...); - -int main() { -#include "bar.h" - printf("%d\n", A); - -#undef A - -#include "foo/bar.h" - printf("%d\n", A); -} -EOF - -cat <<'EOF' > expected -12 -EOF - -cat <<'EOF' > math.h -int multiply(int a, int b) { - return a * b; -} -EOF - -cat <<'EOF' > calc.h -#include "math.h" - -int calculate(int x) { - return multiply(x, 2); -} - -int printf(const char*, ...); -EOF - -test_diff <<'EOF' -#include "calc.h" - -int main() { - printf("%d\n", calculate(6)); - return 0; -} -EOF - -cat <<'EOF' > expected -42 -EOF - -cat <<'EOF' > header.h -#define A 42 -EOF - -test_diff <<'EOF' -#include "header.h" - -int printf(); - -int main() { - printf("%d\n", A); - return 0; -} -EOF - diff --git a/tests/test_increment_operators.sh b/tests/test_increment_operators.sh deleted file mode 100644 index f6aa2b5..0000000 --- a/tests/test_increment_operators.sh +++ /dev/null @@ -1,29 +0,0 @@ -cat <<'EOF' > expected -44 -44 -46 -46 -44 -42 -42 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - int a = 42; - ++a; - a++; - printf("%d\n", a); - printf("%d\n", a++); - printf("%d\n", ++a); - printf("%d\n", a); - --a; - a--; - printf("%d\n", a--); - printf("%d\n", --a); - printf("%d\n", a); -} -EOF - diff --git a/tests/test_input_operations.sh b/tests/test_input_operations.sh deleted file mode 100644 index 15dfb2f..0000000 --- a/tests/test_input_operations.sh +++ /dev/null @@ -1,37 +0,0 @@ -cat <<'EOF' > input -abc -EOF -cat <<'EOF' > expected -4 -abc -EOF -test_diff <<'EOF' -void* calloc(); -int getchar(); -int printf(); - -int read_all(char* buf) { - int c; - int n = 0; - for (0; 1; 0) { - c = getchar(); - if (c == -1) { - break; - } - buf[n] = c; - n = n + 1; - } - return n; -} - -int main() { - char* source = calloc(1024, sizeof(char)); - int source_len = read_all(source); - - printf("%d\n", source_len); - printf("%s", source); - - return 0; -} -EOF - diff --git a/tests/test_invalid_directives.sh b/tests/test_invalid_directives.sh deleted file mode 100644 index 877f9e9..0000000 --- a/tests/test_invalid_directives.sh +++ /dev/null @@ -1,28 +0,0 @@ -cat <<'EOF' > expected -main.c:1: invalid preprocessing directive, '#foo' -EOF -test_compile_error <<'EOF' -#foo - -int main() {} -EOF - -cat <<'EOF' > expected -main.c:1: invalid preprocessing directive, '#bar' -EOF -test_compile_error <<'EOF' -# bar 1 2 3 - -int main() {} -EOF - -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -#if 0 -#baz -#endif - -int main() {} -EOF - diff --git a/tests/test_keywords.sh b/tests/test_keywords.sh deleted file mode 100644 index 0bdb6f7..0000000 --- a/tests/test_keywords.sh +++ /dev/null @@ -1,14 +0,0 @@ -cat <<'EOF' > expected - - -int printf (); -int main () {} -EOF - -test_cpp <<'EOF' -// A keyword is treated as a normal identifier in preprocessing phase. -#define auto int - -auto printf(); -auto main() {} -EOF diff --git a/tests/test_line_continuation.sh b/tests/test_line_continuation.sh deleted file mode 100644 index c5b5c2e..0000000 --- a/tests/test_line_continuation.sh +++ /dev/null @@ -1,57 +0,0 @@ -cat <<'EOF' > expected -10 20 -100 300 500 -EOF - -test_diff <<'EOF' -int printf(); -in\ -t ma\ -in() { - int x = 1\ -0; - - int y = 2\ -\ -\ -0; - -#def\ -ine X 100 -#define Y 100 + \ -200 -#define Z /* -*/ 500 - // comment \ - x *= 100, y *= 200; - /* comment *\ -/ - - printf("%d %d\n", x, y); - printf("%d %d %d\n", X, Y, Z); -} -EOF - -cat <<'EOF' > expected -main.c:1: expected, but got -EOF - -echo -n 'int main() {}\' | test_compile_error - -cat <<'EOF' > expected -Hello World -Line continues -EOF - -# CRLF -printf 'int printf(const char*, ...);\r\nint main() {\r\n printf("Hello World\\n");\r\n printf("Line con\\\r\ntinues\\n");\r\n return 0;\r\n}\r\n' > main_crlf.c -test_diff < main_crlf.c - -# CR -printf 'int printf(const char*, ...);\rint main() {\r printf("Hello World\\n");\r printf("Line con\\\rtinues\\n");\r return 0;\r}\r' > main_cr.c -test_diff < main_cr.c - -# Mixed -printf 'int printf(const char*, ...);\nint main() {\r\n printf("Hello World\\n");\r printf("Line con\\\r\ntinues\\n");\n return 0;\r\n}\r\n' > main_mixed.c -test_diff < main_mixed.c - diff --git a/tests/test_local_variables.sh b/tests/test_local_variables.sh deleted file mode 100644 index 674899d..0000000 --- a/tests/test_local_variables.sh +++ /dev/null @@ -1,56 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int foo; - foo = 42; - ASSERT_EQ(42, foo); - - int bar; - bar = 28; - ASSERT_EQ(70, foo + bar); - - int a1; - int a2; - int a3; - int a4; - int a5; - int a6; - int a7; - int a8; - int a9; - - a1 = 1; - a2 = 2; - a3 = 3; - a4 = 4; - a5 = 5; - a6 = 6; - a7 = 7; - a8 = 8; - a9 = 9; - - ASSERT_EQ(45, - a1 + - a2 + - a3 + - a4 + - a5 + - a6 + - a7 + - a8 + - a9 + - 0); -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int d = 2, e = d, f = d + e; - ASSERT_EQ(2, d); - ASSERT_EQ(2, e); - ASSERT_EQ(4, f); -} -EOF diff --git a/tests/test_logical_operators.sh b/tests/test_logical_operators.sh deleted file mode 100644 index 1cc6594..0000000 --- a/tests/test_logical_operators.sh +++ /dev/null @@ -1,68 +0,0 @@ -cat <<'EOF' > expected -foo -EOF -test_diff <<'EOF' -int printf(); - -int foo() { - printf("foo\n"); - return 0; -} - -int bar() { - printf("bar\n"); - return 1; -} - -int main() { - if (foo() && bar()) { - printf("baz\n"); - } - - return 0; -} -EOF - -cat <<'EOF' > expected -foo -bar -baz -EOF -test_diff <<'EOF' -int printf(); - -int foo() { - printf("foo\n"); - return 0; -} - -int bar() { - printf("bar\n"); - return 1; -} - -int main() { - if (foo() || bar()) { - printf("baz\n"); - } - - return 0; -} -EOF - -cat <<'EOF' > expected -0 -1 -0 -EOF -test_diff <<'EOF' -int printf(); - -int main() { - printf("%d\n", !1); - printf("%d\n", !0); - printf("%d\n", !23); - return 0; -} -EOF - diff --git a/tests/test_macro_operators.sh b/tests/test_macro_operators.sh deleted file mode 100644 index e456d4f..0000000 --- a/tests/test_macro_operators.sh +++ /dev/null @@ -1,89 +0,0 @@ -cat <<'EOF' > expected -int printf (); - - -int foobar = 100; -int prefix_test = 200; -int test_suffix = 300; - -int var_1 = 10; -int var_2 = 20; - -int var_A = 30; - -int number_12 = 12; - -int bazqux = 42; - -int main () { - printf ( foobar=%d\n, foobar); - printf ( prefix_test=%d\n, prefix_test); - printf ( test_suffix=%d\n, test_suffix); - printf ( var_1=%d\n, var_1); - printf ( var_2=%d\n, var_2); - printf ( var_A=%d\n, var_A); - printf ( number_12=%d\n, number_12); - return 0; -} -EOF - -test_cpp <<'EOF' -int printf(); - -#define CONCAT(a, b) a##b -#define PREFIX(name) prefix_##name -#define SUFFIX(name) name##_suffix - -int CONCAT(foo, bar) = 100; -int PREFIX(test) = 200; -int SUFFIX(test) = 300; - -#define MAKE_VAR(n) var_##n -int MAKE_VAR(1) = 10; -int MAKE_VAR(2) = 20; - -#define A 0 -int MAKE_VAR(A) = 30; - -#define NUMBER(x, y) number_##x##y -int NUMBER(1, 2) = 12; - -#define CONCAT2(a, b) a ## b -int CONCAT2(baz, qux) = 42; - -int main() { - printf("foobar=%d\n", foobar); - printf("prefix_test=%d\n", prefix_test); - printf("test_suffix=%d\n", test_suffix); - printf("var_1=%d\n", var_1); - printf("var_2=%d\n", var_2); - printf("var_A=%d\n", var_A); - printf("number_12=%d\n", number_12); - return 0; -} -EOF - -cat <<'EOF' > expected -int printf ( const char *, ...); - - -int H ( int n) { return n; } - -int main () { - printf ( %d\n, H ( 123)); -} -EOF - -test_cpp <<'EOF' -int printf(const char*, ...); - -#define F(x) CHECK(G(x)) -#define G(x) CHECK(H(x)) -#define CHECK(x) x - -int H(int n) { return n; } - -int main() { - printf("%d\n", F(123)); -} -EOF diff --git a/tests/test_nested_conditionals.sh b/tests/test_nested_conditionals.sh deleted file mode 100644 index d149e07..0000000 --- a/tests/test_nested_conditionals.sh +++ /dev/null @@ -1,12 +0,0 @@ -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -#if 0 -# if 1 -# error "not an error" -# endif -#endif - -int main() {} -EOF - diff --git a/tests/test_pointer_basics.sh b/tests/test_pointer_basics.sh deleted file mode 100644 index 2b0beef..0000000 --- a/tests/test_pointer_basics.sh +++ /dev/null @@ -1,80 +0,0 @@ -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -int main() { - int a1; - int* a2; - char a3; - char* a4; - long a5; - long* a6; - void* a8; - int** a10; - char** a12; - long** a14; - void** a16; - int*** a18; - char*** a20; - long*** a22; - void*** a24; - int* const* const* a25; - return 0; -} -EOF - -cat <<'EOF' > expected -42 42 -EOF -test_diff <<'EOF' -int printf(); - -int main() { - int x; - int* y; - y = &x; - *y = 42; - printf("%d %d\n", x, *y); - return 0; -} -EOF - -cat <<'EOF' > expected -3 -3 -3 -EOF -test_diff <<'EOF' -int printf(); - -int main() { - char c; - int i; - long l; - c = 42; - i = 42*2; - l = 42*3; - - char* cp1; - char* cp2; - int* ip1; - int* ip2; - long* lp1; - long* lp2; - - cp1 = &c; - cp2 = &c + 3; - - ip1 = &i; - ip2 = &i + 3; - - lp1 = &l; - lp2 = &l + 3; - - printf("%d\n", cp2 - cp1); - printf("%d\n", ip2 - ip1); - printf("%d\n", lp2 - lp1); - - return 0; -} -EOF - diff --git a/tests/test_predefined_macros.sh b/tests/test_predefined_macros.sh deleted file mode 100644 index 51aeca8..0000000 --- a/tests/test_predefined_macros.sh +++ /dev/null @@ -1,55 +0,0 @@ -cat <<'EOF' > expected -1 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - printf("%d\n", __ducc__); - return 0; -} -EOF - -cat <<'EOF' > expected -main.c -main.c -main.c -EOF - -cat <<'EOF' > header.h -#define A __FILE__ -EOF - -test_diff <<'EOF' -#define B __FILE__ -#include "header.h" -int printf(); -int main() { - printf("%s\n", __FILE__); - printf("%s\n", B); - printf("%s\n", A); -} -EOF - -cat <<'EOF' > expected -5 -6 6 -7 7 -EOF - -cat <<'EOF' > header.h -#define A __LINE__ -EOF - -test_diff <<'EOF' -#define B __LINE__ -#include "header.h" -int printf(); -int main() { - printf("%d\n", __LINE__); - printf("%d %d\n", B, B); - printf("%d %d\n", A, A); -} -EOF - diff --git a/tests/test_preprocess_flag.sh b/tests/test_preprocess_flag.sh deleted file mode 100644 index 3b52bc0..0000000 --- a/tests/test_preprocess_flag.sh +++ /dev/null @@ -1,122 +0,0 @@ -cat <<'EOF' > expected - -int main () { - int x = 100; - int y = 0; - return 0; -} -EOF - -test_cpp <<'EOF' -#define MAX 100 -#define MIN 0 - -int main() { - int x = MAX; - int y = MIN; - return 0; -} -EOF - -cat <<'EOF' > expected - -int main () { - int sum = ( ( 10) + ( 20)); - int product = ( ( 3) * ( 4)); - return 0; -} -EOF - -test_cpp <<'EOF' -#define ADD(a, b) ((a) + (b)) -#define MUL(x, y) ((x) * (y)) - -int main() { - int sum = ADD(10, 20); - int product = MUL(3, 4); - return 0; -} -EOF - -cat <<'EOF' > expected -int main () { - return 42; -} -EOF - -test_cpp <<'EOF' -#define FOO 42 -int main() { - return FOO; -} -EOF - -cat <<'EOF' > expected -int foo () { return 1; } -EOF - -test_cpp <<'EOF' -#define X 1 -#ifdef X -int foo() { return 1; } -#else -int foo() { return 0; } -#endif -EOF - -cat <<'EOF' > expected - -int main () { - int x = 1 + 2 * 3; - return 0; -} -EOF - -test_cpp <<'EOF' -#define A 1 -#define B A + 2 -#define C B * 3 - -int main() { - int x = C; - return 0; -} -EOF - -cat <<'EOF' > expected - -int main () { - int x = ( 5 + 3); - int y = ( ( 5) + ( 3)); - return 0; -} -EOF - -test_cpp <<'EOF' -#define SIMPLE (5 + 3) -#define COMPLEX(a, b) ((a) + (b)) - -int main() { - int x = SIMPLE; - int y = COMPLEX(5, 3); - return 0; -} -EOF - -cat <<'EOF' > expected - -int main () { - int bar = 0; - bar ++; -} -EOF - -test_cpp <<'EOF' -#define A(a) a -#define B(b) b - -int main() { - A(int)B(bar) = 0; - bar++; -} -EOF diff --git a/tests/test_preprocessor_comments.sh b/tests/test_preprocessor_comments.sh deleted file mode 100644 index 0fcea83..0000000 --- a/tests/test_preprocessor_comments.sh +++ /dev/null @@ -1,11 +0,0 @@ -cat <<'EOF' > expected -EOF - -test_diff <<'EOF' -#if 1 // a -#else // b -#endif // c - -int main() {} -EOF - diff --git a/tests/test_printf.sh b/tests/test_printf.sh deleted file mode 100644 index da35fcb..0000000 --- a/tests/test_printf.sh +++ /dev/null @@ -1,220 +0,0 @@ -touch expected -test_diff <<'EOF' -int printf(); - -int main() { - printf(""); - return 0; -} -EOF - -cat <<'EOF' > expected -Hello, World! -EOF -test_diff <<'EOF' -int printf(); - -int main() { - printf("Hello, World!\n"); - return 0; -} -EOF - -cat <<'EOF' > expected -"Hello, World!" -EOF -test_diff <<'EOF' -int printf(); - -int main() { - printf("\"Hello, World!\"\n"); - return 0; -} -EOF - -cat <<'EOF' > expected -1 -1 -1 -1 -1 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -4 -4 -4 -4 -4 -4 -4 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - printf("%d\n", sizeof(char)); - printf("%d\n", sizeof(signed char)); - printf("%d\n", sizeof(char signed)); - printf("%d\n", sizeof(unsigned char)); - printf("%d\n", sizeof(char unsigned)); - - printf("%d\n", sizeof(short)); - printf("%d\n", sizeof(signed short)); - printf("%d\n", sizeof(short signed)); - printf("%d\n", sizeof(short int)); - printf("%d\n", sizeof(int short)); - printf("%d\n", sizeof(signed short int)); - printf("%d\n", sizeof(signed int short)); - printf("%d\n", sizeof(short signed int)); - printf("%d\n", sizeof(short int signed)); - printf("%d\n", sizeof(int short signed)); - printf("%d\n", sizeof(int signed short)); - - printf("%d\n", sizeof(unsigned short)); - printf("%d\n", sizeof(short unsigned)); - printf("%d\n", sizeof(unsigned short int)); - printf("%d\n", sizeof(unsigned int short)); - printf("%d\n", sizeof(short unsigned int)); - printf("%d\n", sizeof(short int unsigned)); - printf("%d\n", sizeof(int short unsigned)); - printf("%d\n", sizeof(int unsigned short)); - - printf("%d\n", sizeof(int)); - printf("%d\n", sizeof(signed)); - printf("%d\n", sizeof(signed int)); - printf("%d\n", sizeof(int signed)); - - printf("%d\n", sizeof(unsigned)); - printf("%d\n", sizeof(unsigned int)); - printf("%d\n", sizeof(int unsigned)); - - printf("%d\n", sizeof(long)); - printf("%d\n", sizeof(signed long)); - printf("%d\n", sizeof(long signed)); - printf("%d\n", sizeof(long int)); - printf("%d\n", sizeof(int long)); - printf("%d\n", sizeof(signed long int)); - printf("%d\n", sizeof(signed int long)); - printf("%d\n", sizeof(long signed int)); - printf("%d\n", sizeof(long int signed)); - printf("%d\n", sizeof(int long signed)); - printf("%d\n", sizeof(int signed long)); - - printf("%d\n", sizeof(unsigned long)); - printf("%d\n", sizeof(long unsigned)); - printf("%d\n", sizeof(unsigned long int)); - printf("%d\n", sizeof(unsigned int long)); - printf("%d\n", sizeof(long unsigned int)); - printf("%d\n", sizeof(long int unsigned)); - printf("%d\n", sizeof(int long unsigned)); - printf("%d\n", sizeof(int unsigned long)); - - printf("%d\n", sizeof(long long)); - printf("%d\n", sizeof(signed long long)); - printf("%d\n", sizeof(long signed long)); - printf("%d\n", sizeof(long long signed)); - printf("%d\n", sizeof(long long int)); - printf("%d\n", sizeof(long int long)); - printf("%d\n", sizeof(int long long)); - printf("%d\n", sizeof(signed long long int)); - printf("%d\n", sizeof(signed long int long)); - printf("%d\n", sizeof(signed int long long)); - printf("%d\n", sizeof(long signed long int)); - printf("%d\n", sizeof(long signed int long)); - printf("%d\n", sizeof(int signed long long)); - printf("%d\n", sizeof(long long signed int)); - printf("%d\n", sizeof(long int signed long)); - printf("%d\n", sizeof(int long signed long)); - printf("%d\n", sizeof(long long int signed)); - printf("%d\n", sizeof(long int long signed)); - printf("%d\n", sizeof(int long long signed)); - - printf("%d\n", sizeof(unsigned long long)); - printf("%d\n", sizeof(long unsigned long)); - printf("%d\n", sizeof(long long unsigned)); - printf("%d\n", sizeof(unsigned long long int)); - printf("%d\n", sizeof(unsigned long int long)); - printf("%d\n", sizeof(unsigned int long long)); - printf("%d\n", sizeof(long unsigned long int)); - printf("%d\n", sizeof(long unsigned int long)); - printf("%d\n", sizeof(int unsigned long long)); - printf("%d\n", sizeof(long long unsigned int)); - printf("%d\n", sizeof(long int unsigned long)); - printf("%d\n", sizeof(int long unsigned long)); - printf("%d\n", sizeof(long long int unsigned)); - printf("%d\n", sizeof(long int long unsigned)); - printf("%d\n", sizeof(int long long unsigned)); -} -EOF - diff --git a/tests/test_recursive_functions.sh b/tests/test_recursive_functions.sh deleted file mode 100644 index 709cfbe..0000000 --- a/tests/test_recursive_functions.sh +++ /dev/null @@ -1,15 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int fib(int n) { - if (n <= 1) { - return 1; - } else { - return fib(n - 1) + fib(n - 2); - } -} - -int main() { - ASSERT_EQ(89, fib(10)); -} -EOF diff --git a/tests/test_short_type.sh b/tests/test_short_type.sh deleted file mode 100644 index fcbec87..0000000 --- a/tests/test_short_type.sh +++ /dev/null @@ -1,17 +0,0 @@ -cat <<'EOF' > expected -2 42 -8 123 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - short a = 42; - printf("%zu %hd\n", sizeof(a), a); - short* b = &a; - *b = 123; - printf("%zu %hd\n", sizeof(b), *b); -} -EOF - diff --git a/tests/test_sizeof_operator.sh b/tests/test_sizeof_operator.sh deleted file mode 100644 index b14e2ca..0000000 --- a/tests/test_sizeof_operator.sh +++ /dev/null @@ -1,177 +0,0 @@ -cat <<'EOF' > expected -sizeof(int) = 4 -sizeof(int*) = 8 -sizeof(char) = 1 -sizeof(char*) = 8 -sizeof(long) = 8 -sizeof(long*) = 8 -sizeof(void*) = 8 -sizeof(int**) = 8 -sizeof(char**) = 8 -sizeof(long**) = 8 -sizeof(void**) = 8 -sizeof(int***) = 8 -sizeof(char***) = 8 -sizeof(long***) = 8 -sizeof(void***) = 8 -EOF -test_diff <<'EOF' -int printf(); - -int main() { - printf("sizeof(int) = %d\n", sizeof(int)); - printf("sizeof(int*) = %d\n", sizeof(int*)); - printf("sizeof(char) = %d\n", sizeof(char)); - printf("sizeof(char*) = %d\n", sizeof(char*)); - printf("sizeof(long) = %d\n", sizeof(long)); - printf("sizeof(long*) = %d\n", sizeof(long*)); - printf("sizeof(void*) = %d\n", sizeof(void*)); - printf("sizeof(int**) = %d\n", sizeof(int**)); - printf("sizeof(char**) = %d\n", sizeof(char**)); - printf("sizeof(long**) = %d\n", sizeof(long**)); - printf("sizeof(void**) = %d\n", sizeof(void**)); - printf("sizeof(int***) = %d\n", sizeof(int***)); - printf("sizeof(char***) = %d\n", sizeof(char***)); - printf("sizeof(long***) = %d\n", sizeof(long***)); - printf("sizeof(void***) = %d\n", sizeof(void***)); - return 0; -} -EOF - -cat <<'EOF' > expected -sizeof(struct Token) = 16 -sizeof(struct Define) = 16 -sizeof(struct Type) = 24 -sizeof(struct AstNode) = 128 -sizeof(struct LVar) = 16 -sizeof(struct Func) = 16 -sizeof(struct Parser) = 64 -sizeof(struct CodeGen) = 16 -EOF -test_diff <<'EOF' -struct Token { - int kind; - char* value; -}; - -struct Define { - char* from; - struct Token* to; -}; - -struct AstNode; - -struct Type { - int kind; - struct Type* to; - struct AstNode* members; -}; - -struct AstNode { - int kind; - struct AstNode* next; - struct AstNode* last; - char* name; - struct AstNode* func_params; - struct AstNode* func_body; - int int_value; - struct AstNode* expr1; - struct AstNode* expr2; - struct AstNode* expr3; - int op; - struct Type* ty; - int var_index; - struct AstNode* node1; - struct AstNode* node2; - char** str_literals; -}; - -struct LVar { - char* name; - struct Type* ty; -}; - -struct Func { - char* name; - struct Type* ty; -}; - -struct Parser { - struct Token* tokens; - int pos; - struct LVar* locals; - int n_locals; - struct Func* funcs; - int n_funcs; - char** str_literals; - int n_str_literals; -}; - -struct CodeGen { - int next_label; - int* loop_labels; -}; - -int printf(); - -int main() { - printf("sizeof(struct Token) = %d\n", sizeof(struct Token)); - printf("sizeof(struct Define) = %d\n", sizeof(struct Define)); - printf("sizeof(struct Type) = %d\n", sizeof(struct Type)); - printf("sizeof(struct AstNode) = %d\n", sizeof(struct AstNode)); - printf("sizeof(struct LVar) = %d\n", sizeof(struct LVar)); - printf("sizeof(struct Func) = %d\n", sizeof(struct Func)); - printf("sizeof(struct Parser) = %d\n", sizeof(struct Parser)); - printf("sizeof(struct CodeGen) = %d\n", sizeof(struct CodeGen)); - return 0; -} -EOF - -cat <<'EOF' > expected -24 -EOF - -test_diff <<'EOF' -#include - -int printf(); - -int main() { - printf("%d\n", sizeof(va_list)); -} -EOF - -cat <<'EOF' > expected -sizeof(a) = 4 -sizeof(b) = 8 -sizeof(c) = 123 -sizeof(d) = 16 -sizeof(e) = 8 -sizeof(f) = 1968 -EOF -test_diff <<'EOF' -int printf(); - -struct S { - long x; - long y; -}; - -int main() { - int a; - long b; - char c[123]; - struct S d; - void* e; - struct S f[123]; - - printf("sizeof(a) = %d\n", sizeof(a)); - printf("sizeof(b) = %d\n", sizeof(b)); - printf("sizeof(c) = %d\n", sizeof(c)); - printf("sizeof(d) = %d\n", sizeof(d)); - printf("sizeof(e) = %d\n", sizeof(e)); - printf("sizeof(f) = %d\n", sizeof(f)); - return 0; -} -EOF - diff --git a/tests/test_stdlib_declarations.sh b/tests/test_stdlib_declarations.sh deleted file mode 100644 index c919b88..0000000 --- a/tests/test_stdlib_declarations.sh +++ /dev/null @@ -1,22 +0,0 @@ -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -typedef long size_t; - -int atoi(const char*); -void* calloc(size_t, size_t); -void exit(int); -int getchar(void); -int isalnum(int); -int isalpha(int); -int isdigit(int); -int isspace(int); -void* memcpy(void*, void*, size_t); -int printf(const char*, ...); -int sprintf(char*, const char*, ...); -int strcmp(const char*, const char*); -char* strstr(const char*, const char*); - -int main() { return 0; } -EOF - diff --git a/tests/test_string_operations.sh b/tests/test_string_operations.sh deleted file mode 100644 index 0c9a07e..0000000 --- a/tests/test_string_operations.sh +++ /dev/null @@ -1,57 +0,0 @@ -touch expected -test_diff <<'EOF' -int main() { - ""; - return 0; -} -EOF - -touch expected -test_diff <<'EOF' -int main() { - "abc"; - return 0; -} -EOF - -touch expected -test_diff <<'EOF' -int main() { - "\"foo\"bar\\\n\""; - return 0; -} -EOF - -cat <<'EOF' > expected -h -l -, -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - char* h = " hello,world" + 1; - printf("%c\n", *h); - printf("%c\n", h[2]); - printf("%c\n", *(h + 5)); -} -EOF - -cat <<'EOF' > expected -105 -0 -EOF - -test_diff <<'EOF' -int printf(); - -int main() { - char* s = "hi"; - while (*s++) { - printf("%d\n", *s); - } -} -EOF - diff --git a/tests/test_struct_arrays.sh b/tests/test_struct_arrays.sh deleted file mode 100644 index ca1e0b2..0000000 --- a/tests/test_struct_arrays.sh +++ /dev/null @@ -1,34 +0,0 @@ -cat <<'EOF' > expected -20 -10 -20 -30 -40 -50 -EOF - -test_diff <<'EOF' -int printf(); - -struct S { - int a[5]; -}; - -int main() { - struct S x; - x.a[0] = 10; - x.a[1] = 20; - x.a[2] = 30; - x.a[3] = 40; - x.a[4] = 50; - - printf("%zu\n", sizeof(struct S)); - - printf("%d\n", x.a[0]); - printf("%d\n", x.a[1]); - printf("%d\n", x.a[2]); - printf("%d\n", x.a[3]); - printf("%d\n", x.a[4]); -} -EOF - diff --git a/tests/test_struct_basics.sh b/tests/test_struct_basics.sh deleted file mode 100644 index 74e2458..0000000 --- a/tests/test_struct_basics.sh +++ /dev/null @@ -1,121 +0,0 @@ -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -struct Token { - int kind; - char* value; -}; - -struct Define { - char* from; - struct Token* to; -}; - -struct AstNode; - -struct Type { - int kind; - struct Type* to; - struct AstNode* members; -}; - -struct AstNode { - int kind; - struct AstNode* next; - struct AstNode* last; - char* name; - struct AstNode* func_params; - struct AstNode* func_body; - int int_value; - struct AstNode* expr1; - struct AstNode* expr2; - struct AstNode* expr3; - int op; - struct Type* ty; - int var_index; - struct AstNode* node1; - struct AstNode* node2; - char** str_literals; -}; - -struct LVar { - char* name; - struct Type* ty; -}; - -struct Func { - char* name; - struct Type* ty; -}; - -struct Parser { - struct Token* tokens; - int pos; - struct LVar* locals; - int n_locals; - struct Func* funcs; - int n_funcs; - char** str_literals; - int n_str_literals; -}; - -struct CodeGen { - int next_label; - int* loop_labels; -}; - -int main() { - return 0; -} -EOF - -cat <<'EOF' > expected -42 -123 -EOF -test_diff <<'EOF' -struct S { - int a; - int b; -}; - -int printf(); -void* calloc(); - -int main() { - struct S* sp; - sp = calloc(1, sizeof(struct S)); - sp->a = 42; - printf("%d\n", sp->a); - (*sp).b = 123; - printf("%d\n", (*sp).b); - return 0; -} -EOF - -cat <<'EOF' > expected -42 -EOF -test_diff <<'EOF' -void* calloc(); -int printf(); - -struct T; - -struct S { - struct T* a; -}; - -struct T { - int b; -}; - -int main() { - struct S* s = calloc(1, sizeof(struct S)); - s->a = calloc(1, sizeof(struct T)); - s->a->b = 42; - printf("%d\n", s->a->b); - return 0; -} -EOF - diff --git a/tests/test_struct_initialization.sh b/tests/test_struct_initialization.sh deleted file mode 100644 index 0454c57..0000000 --- a/tests/test_struct_initialization.sh +++ /dev/null @@ -1,108 +0,0 @@ -cat <<'EOF' > expected -0 -1 -EOF -test_diff <<'EOF' -int printf(); -void* calloc(); - -struct S { - int a; - int b; -}; - -int main() { - struct S* s = calloc(1, sizeof(struct S)); - s->b = 1; - printf("%ld\n", s->a); - printf("%ld\n", s->b); - return 0; -} -EOF - -cat <<'EOF' > expected -123 456 -0 0 -123 456 -EOF - -test_diff <<'EOF' -struct S { - int a; - int b; -}; -typedef struct S S; - -void* calloc(); -int printf(); - -int main() { - S* s1 = calloc(1, sizeof(S)); - S* s2 = calloc(1, sizeof(S)); - s1->a = 123; - s1->b = 456; - printf("%d %d\n", s1->a, s1->b); - printf("%d %d\n", s2->a, s2->b); - *s2 = *s1; - printf("%d %d\n", s2->a, s2->b); -} -EOF - -cat <<'EOF' > expected -123 456 -0 0 -123 456 -EOF - -test_diff <<'EOF' -struct S { - long a; - long b; -}; -typedef struct S S; - -void* calloc(); -int printf(); - -int main() { - S* s1 = calloc(1, sizeof(S)); - S* s2 = calloc(1, sizeof(S)); - s1->a = 123; - s1->b = 456; - printf("%d %d\n", s1->a, s1->b); - printf("%d %d\n", s2->a, s2->b); - *s2 = *s1; - printf("%d %d\n", s2->a, s2->b); -} -EOF - -cat <<'EOF' > expected -123 456 -0 0 -123 456 -EOF - -test_diff <<'EOF' -struct S { - long a; - long b; -}; -typedef struct S S; - -void* calloc(); -int printf(); - -int main() { - S s1; - S s2; - s1.a = 123; - s1.b = 456; - s2.a = 0; - s2.b = 0; - printf("%d %d\n", s1.a, s1.b); - printf("%d %d\n", s2.a, s2.b); - s2 = s1; - printf("%d %d\n", s2.a, s2.b); -} -EOF - diff --git a/tests/test_switch.sh b/tests/test_switch.sh deleted file mode 100644 index d5d3dae..0000000 --- a/tests/test_switch.sh +++ /dev/null @@ -1,212 +0,0 @@ -#!/bin/bash - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int x = 2; - int result = 0; - - switch (x) { - case 1: - result = 10; - break; - case 2: - result = 20; - break; - case 3: - result = 30; - break; - } - - ASSERT_EQ(20, result); -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int x = 5; - int result = 0; - - switch (x) { - case 1: - result = 10; - break; - case 2: - result = 20; - break; - default: - result = 99; - break; - } - - ASSERT_EQ(99, result); -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int x = 2; - int result = 0; - - switch (x) { - case 1: - result = result + 10; - case 2: - result = result + 20; - case 3: - result = result + 30; - break; - } - - ASSERT_EQ(50, result); // 20 + 30 due to fall-through -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int x = 1; - int y = 2; - int result = 0; - - switch (x) { - case 1: - switch (y) { - case 1: - result = 11; - break; - case 2: - result = 12; - break; - } - break; - case 2: - result = 20; - break; - } - - ASSERT_EQ(12, result); -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int a = 3; - int b = 2; - int result = 0; - - switch (a + b) { - case 4: - result = 40; - break; - case 5: - result = 50; - break; - case 6: - result = 60; - break; - } - - ASSERT_EQ(50, result); -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int x = 2; - int result = 0; - int temp = 0; - - switch (x) { - case 1: - temp = 5; - result = temp * 2; - break; - case 2: - temp = 10; - result = temp * 2; - break; - case 3: - temp = 15; - result = temp * 2; - break; - } - - ASSERT_EQ(20, result); - ASSERT_EQ(10, temp); -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int x = 1; - int result = 0; - - switch (x) { - case 1: { - int local = 100; - result = local; - break; - } - case 2: { - int local = 200; - result = local; - break; - } - } - - ASSERT_EQ(100, result); -} -EOF - -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - int x = 10; - int result = 42; - - switch (x) { - case 1: - result = 10; - break; - case 2: - result = 20; - break; - } - - ASSERT_EQ(42, result); -} -EOF - -cat <<'EOF' > expected -main.c:2: 'case' label not within a switch statement -EOF -test_compile_error <<'EOF' -int main() { - case 1: - return 0; -} -EOF - -cat <<'EOF' > expected -main.c:2: 'default' label not within a switch statement -EOF -test_compile_error <<'EOF' -int main() { - default: - return 0; -} -EOF diff --git a/tests/test_ternary_operator.sh b/tests/test_ternary_operator.sh deleted file mode 100644 index c63f581..0000000 --- a/tests/test_ternary_operator.sh +++ /dev/null @@ -1,8 +0,0 @@ -test_exit_code 0 <<'EOF' -#include "../../helpers.h" - -int main() { - ASSERT_EQ(2, 1 ? 2 : 3); - ASSERT_EQ(5, 0 ? 4 : 5); -} -EOF diff --git a/tests/test_typedef_struct.sh b/tests/test_typedef_struct.sh deleted file mode 100644 index 7181a86..0000000 --- a/tests/test_typedef_struct.sh +++ /dev/null @@ -1,85 +0,0 @@ -cat <<'EOF' > expected -42 -EOF -test_diff <<'EOF' -void* calloc(); -int printf(); - -struct S { - int x; -}; -typedef struct S S; - -int main() { - S* s = calloc(1, sizeof(S)); - s->x = 42; - printf("%d\n", s->x); - return 0; -} -EOF - -cat <<'EOF' > expected -4 -EOF -test_diff <<'EOF' -struct S0; -typedef struct S0 S0; - -struct S1 { - int x; -}; - -struct S2 { - int x; -}; - -struct S3 { - int x; -}; - -struct S4 { - int x; -}; - -struct S0 { - int x; -}; - -int printf(const char*, ...); -int main() { - printf("%zu\n", sizeof(S0)); -} -EOF - -cat <<'EOF' > expected -8 -4 -4 -EOF - -test_diff <<'EOF' -int printf(); - -typedef struct { - int x; - int y; -} S; - -typedef union { - int a; - char b; -} U; - -typedef enum { - RED, - GREEN, - BLUE -} E; - -int main() { - printf("%zu\n", sizeof(S)); - printf("%zu\n", sizeof(U)); - printf("%zu\n", sizeof(E)); -} -EOF - diff --git a/tests/test_undef.sh b/tests/test_undef.sh deleted file mode 100644 index 27d03c6..0000000 --- a/tests/test_undef.sh +++ /dev/null @@ -1,33 +0,0 @@ -cat <<'EOF' > expected -int printf (); - -int main () { - - printf ( A is defined\n); - - - printf ( A is undefined\n); -} -EOF - -test_cpp <<'EOF' -int printf(); - -int main() { -#define A 1 - -#ifdef A - printf("A is defined\n"); -#else - printf("A is undefined\n"); -#endif - -#undef A - -#ifdef A - printf("A is defined\n"); -#else - printf("A is undefined\n"); -#endif -} -EOF diff --git a/tests/test_unions.sh b/tests/test_unions.sh deleted file mode 100644 index b588792..0000000 --- a/tests/test_unions.sh +++ /dev/null @@ -1,23 +0,0 @@ -cat <<'EOF' > expected -8 -42 -42 -EOF - -test_diff <<'EOF' -int printf(); - -union U { - int i; - long l; -}; - -int main() { - union U u; - printf("%zu\n", sizeof(u)); - u.l = 42; - printf("%d\n", u.i); - printf("%ld\n", u.l); -} -EOF - diff --git a/tests/test_variadic_functions.sh b/tests/test_variadic_functions.sh deleted file mode 100644 index ec85d9a..0000000 --- a/tests/test_variadic_functions.sh +++ /dev/null @@ -1,32 +0,0 @@ -cat <<'EOF' > expected -123 -456 789 -EOF - -test_diff <<'EOF' -#include - -int fprintf(); - -struct FILE; -typedef struct FILE FILE; - -extern FILE* stdout; - -int vfprintf(FILE*, const char*, va_list); - -void fatal_error(const char* msg, ...) { - va_list args; - va_start(args, msg); - vfprintf(stdout, msg, args); - va_end(args); - fprintf(stdout, "\n"); -} - -int main() { - fatal_error("%d", 123); - fatal_error("%d %d", 456, 789); - return 0; -} -EOF - diff --git a/tests/test_void_functions.sh b/tests/test_void_functions.sh deleted file mode 100644 index 57116ce..0000000 --- a/tests/test_void_functions.sh +++ /dev/null @@ -1,49 +0,0 @@ -cat <<'EOF' > expected -123 -EOF -test_diff <<'EOF' -int printf(); - -void foo_bar(int hoge_piyo) { - printf("%d\n", hoge_piyo); -} - -int main() { - foo_bar(123); - return 0; -} -EOF - -cat <<'EOF' > expected -EOF -test_diff <<'EOF' -struct S { - int a; -}; - -struct S* f(); - -struct S* g() {} - -int main() { - return 0; -} -EOF - -cat <<'EOF' > expected -hi -EOF -test_diff <<'EOF' -int printf(); - -void f() { - printf("hi\n"); - return; -} - -int main() { - f(); - return 0; -} -EOF - diff --git a/tests/typedef_struct.sh b/tests/typedef_struct.sh new file mode 100644 index 0000000..7181a86 --- /dev/null +++ b/tests/typedef_struct.sh @@ -0,0 +1,85 @@ +cat <<'EOF' > expected +42 +EOF +test_diff <<'EOF' +void* calloc(); +int printf(); + +struct S { + int x; +}; +typedef struct S S; + +int main() { + S* s = calloc(1, sizeof(S)); + s->x = 42; + printf("%d\n", s->x); + return 0; +} +EOF + +cat <<'EOF' > expected +4 +EOF +test_diff <<'EOF' +struct S0; +typedef struct S0 S0; + +struct S1 { + int x; +}; + +struct S2 { + int x; +}; + +struct S3 { + int x; +}; + +struct S4 { + int x; +}; + +struct S0 { + int x; +}; + +int printf(const char*, ...); +int main() { + printf("%zu\n", sizeof(S0)); +} +EOF + +cat <<'EOF' > expected +8 +4 +4 +EOF + +test_diff <<'EOF' +int printf(); + +typedef struct { + int x; + int y; +} S; + +typedef union { + int a; + char b; +} U; + +typedef enum { + RED, + GREEN, + BLUE +} E; + +int main() { + printf("%zu\n", sizeof(S)); + printf("%zu\n", sizeof(U)); + printf("%zu\n", sizeof(E)); +} +EOF + diff --git a/tests/undef.sh b/tests/undef.sh new file mode 100644 index 0000000..27d03c6 --- /dev/null +++ b/tests/undef.sh @@ -0,0 +1,33 @@ +cat <<'EOF' > expected +int printf (); + +int main () { + + printf ( A is defined\n); + + + printf ( A is undefined\n); +} +EOF + +test_cpp <<'EOF' +int printf(); + +int main() { +#define A 1 + +#ifdef A + printf("A is defined\n"); +#else + printf("A is undefined\n"); +#endif + +#undef A + +#ifdef A + printf("A is defined\n"); +#else + printf("A is undefined\n"); +#endif +} +EOF diff --git a/tests/unions.sh b/tests/unions.sh new file mode 100644 index 0000000..b588792 --- /dev/null +++ b/tests/unions.sh @@ -0,0 +1,23 @@ +cat <<'EOF' > expected +8 +42 +42 +EOF + +test_diff <<'EOF' +int printf(); + +union U { + int i; + long l; +}; + +int main() { + union U u; + printf("%zu\n", sizeof(u)); + u.l = 42; + printf("%d\n", u.i); + printf("%ld\n", u.l); +} +EOF + diff --git a/tests/variadic_functions.sh b/tests/variadic_functions.sh new file mode 100644 index 0000000..ec85d9a --- /dev/null +++ b/tests/variadic_functions.sh @@ -0,0 +1,32 @@ +cat <<'EOF' > expected +123 +456 789 +EOF + +test_diff <<'EOF' +#include + +int fprintf(); + +struct FILE; +typedef struct FILE FILE; + +extern FILE* stdout; + +int vfprintf(FILE*, const char*, va_list); + +void fatal_error(const char* msg, ...) { + va_list args; + va_start(args, msg); + vfprintf(stdout, msg, args); + va_end(args); + fprintf(stdout, "\n"); +} + +int main() { + fatal_error("%d", 123); + fatal_error("%d %d", 456, 789); + return 0; +} +EOF + diff --git a/tests/void_functions.sh b/tests/void_functions.sh new file mode 100644 index 0000000..57116ce --- /dev/null +++ b/tests/void_functions.sh @@ -0,0 +1,49 @@ +cat <<'EOF' > expected +123 +EOF +test_diff <<'EOF' +int printf(); + +void foo_bar(int hoge_piyo) { + printf("%d\n", hoge_piyo); +} + +int main() { + foo_bar(123); + return 0; +} +EOF + +cat <<'EOF' > expected +EOF +test_diff <<'EOF' +struct S { + int a; +}; + +struct S* f(); + +struct S* g() {} + +int main() { + return 0; +} +EOF + +cat <<'EOF' > expected +hi +EOF +test_diff <<'EOF' +int printf(); + +void f() { + printf("hi\n"); + return; +} + +int main() { + f(); + return 0; +} +EOF + -- cgit v1.2.3-70-g09d2