| author | |
| committer | |
| log | 4068897b6b167569718c61efcb4371693fe89b74 |
| tree | 4898f945118b9d475a6af5a472660d7911e1518b |
| parent | 3a1defa67b284f8f8be17477aa147db9044db15c |
3 files changed, 171 insertions(+), 171 deletions(-)
CMakeLists.txt+1-1| ... | ... | @@ -37,7 +37,7 @@ set(TEST_SOURCES |
| 37 | 37 | "${CMAKE_SOURCE_DIR}/src/buffer.cpp" |
| 38 | 38 | "${CMAKE_SOURCE_DIR}/src/util.cpp" |
| 39 | 39 | "${CMAKE_SOURCE_DIR}/src/os.cpp" |
| 40 | "${CMAKE_SOURCE_DIR}/test/standalone.cpp" | |
| 40 | "${CMAKE_SOURCE_DIR}/test/run_tests.cpp" | |
| 41 | 41 | ) |
| 42 | 42 | |
| 43 | 43 |
test/run_tests.cpp created+170| ... | ... | @@ -0,0 +1,170 @@ |
| 1 | /* | |
| 2 | * Copyright (c) 2015 Andrew Kelley | |
| 3 | * | |
| 4 | * This file is part of zig, which is MIT licensed. | |
| 5 | * See http://opensource.org/licenses/MIT | |
| 6 | */ | |
| 7 | ||
| 8 | #include "list.hpp" | |
| 9 | #include "buffer.hpp" | |
| 10 | #include "os.hpp" | |
| 11 | ||
| 12 | #include <stdio.h> | |
| 13 | ||
| 14 | struct TestSourceFile { | |
| 15 | const char *relative_path; | |
| 16 | const char *text; | |
| 17 | }; | |
| 18 | ||
| 19 | struct TestCase { | |
| 20 | const char *case_name; | |
| 21 | const char *output; | |
| 22 | const char *source; | |
| 23 | ZigList<const char *> compile_errors; | |
| 24 | ZigList<const char *> compiler_args; | |
| 25 | ZigList<const char *> program_args; | |
| 26 | }; | |
| 27 | ||
| 28 | ZigList<TestCase*> test_cases = {0}; | |
| 29 | const char *tmp_source_path = ".tmp_source.zig"; | |
| 30 | const char *tmp_exe_path = "./.tmp_exe"; | |
| 31 | ||
| 32 | static void add_simple_case(const char *case_name, const char *source, const char *output) { | |
| 33 | TestCase *test_case = allocate<TestCase>(1); | |
| 34 | test_case->case_name = case_name; | |
| 35 | test_case->output = output; | |
| 36 | test_case->source = source; | |
| 37 | ||
| 38 | test_case->compiler_args.append("build"); | |
| 39 | test_case->compiler_args.append(tmp_source_path); | |
| 40 | test_case->compiler_args.append("--output"); | |
| 41 | test_case->compiler_args.append(tmp_exe_path); | |
| 42 | test_case->compiler_args.append("--release"); | |
| 43 | test_case->compiler_args.append("--strip"); | |
| 44 | ||
| 45 | test_cases.append(test_case); | |
| 46 | } | |
| 47 | ||
| 48 | static void add_all_test_cases(void) { | |
| 49 | add_simple_case("hello world with libc", R"SOURCE( | |
| 50 | #link("c") | |
| 51 | extern { | |
| 52 | fn puts(s: *mut u8) -> i32; | |
| 53 | fn exit(code: i32) -> unreachable; | |
| 54 | } | |
| 55 | ||
| 56 | export fn _start() -> unreachable { | |
| 57 | puts("Hello, world!"); | |
| 58 | exit(0); | |
| 59 | } | |
| 60 | )SOURCE", "Hello, world!\n"); | |
| 61 | ||
| 62 | add_simple_case("function call", R"SOURCE( | |
| 63 | #link("c") | |
| 64 | extern { | |
| 65 | fn puts(s: *mut u8) -> i32; | |
| 66 | fn exit(code: i32) -> unreachable; | |
| 67 | } | |
| 68 | ||
| 69 | fn empty_function_1() {} | |
| 70 | fn empty_function_2() { return; } | |
| 71 | ||
| 72 | export fn _start() -> unreachable { | |
| 73 | empty_function_1(); | |
| 74 | empty_function_2(); | |
| 75 | this_is_a_function(); | |
| 76 | } | |
| 77 | ||
| 78 | fn this_is_a_function() -> unreachable { | |
| 79 | puts("OK"); | |
| 80 | exit(0); | |
| 81 | } | |
| 82 | )SOURCE", "OK\n"); | |
| 83 | ||
| 84 | add_simple_case("comments", R"SOURCE( | |
| 85 | #link("c") | |
| 86 | extern { | |
| 87 | fn puts(s: *mut u8) -> i32; | |
| 88 | fn exit(code: i32) -> unreachable; | |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * multi line doc comment | |
| 93 | */ | |
| 94 | fn another_function() {} | |
| 95 | ||
| 96 | /// this is a documentation comment | |
| 97 | /// doc comment line 2 | |
| 98 | export fn _start() -> unreachable { | |
| 99 | puts(/* mid-line comment /* nested */ */ "OK"); | |
| 100 | exit(0); | |
| 101 | } | |
| 102 | )SOURCE", "OK\n"); | |
| 103 | } | |
| 104 | ||
| 105 | static void run_test(TestCase *test_case) { | |
| 106 | os_write_file(buf_create_from_str(tmp_source_path), buf_create_from_str(test_case->source)); | |
| 107 | ||
| 108 | Buf zig_stderr = BUF_INIT; | |
| 109 | Buf zig_stdout = BUF_INIT; | |
| 110 | int return_code; | |
| 111 | static const char *zig_exe = "./zig"; | |
| 112 | os_exec_process(zig_exe, test_case->compiler_args, &return_code, &zig_stderr, &zig_stdout); | |
| 113 | ||
| 114 | if (return_code != 0) { | |
| 115 | printf("\nCompile failed with return code %d:\n", return_code); | |
| 116 | printf("%s", zig_exe); | |
| 117 | for (int i = 0; i < test_case->compiler_args.length; i += 1) { | |
| 118 | printf(" %s", test_case->compiler_args.at(i)); | |
| 119 | } | |
| 120 | printf("\n"); | |
| 121 | printf("%s\n", buf_ptr(&zig_stderr)); | |
| 122 | exit(1); | |
| 123 | } | |
| 124 | ||
| 125 | Buf program_stderr = BUF_INIT; | |
| 126 | Buf program_stdout = BUF_INIT; | |
| 127 | os_exec_process(tmp_exe_path, test_case->program_args, &return_code, &program_stderr, &program_stdout); | |
| 128 | ||
| 129 | if (return_code != 0) { | |
| 130 | printf("\nProgram exited with return code %d:\n", return_code); | |
| 131 | printf("%s", tmp_exe_path); | |
| 132 | for (int i = 0; i < test_case->program_args.length; i += 1) { | |
| 133 | printf(" %s", test_case->program_args.at(i)); | |
| 134 | } | |
| 135 | printf("\n"); | |
| 136 | printf("%s\n", buf_ptr(&program_stderr)); | |
| 137 | exit(1); | |
| 138 | } | |
| 139 | ||
| 140 | if (!buf_eql_str(&program_stdout, test_case->output)) { | |
| 141 | printf("\n"); | |
| 142 | printf("==== Test failed. Expected output: ====\n"); | |
| 143 | printf("%s\n", test_case->output); | |
| 144 | printf("========= Actual output: ==============\n"); | |
| 145 | printf("%s\n", buf_ptr(&program_stdout)); | |
| 146 | printf("=======================================\n"); | |
| 147 | exit(1); | |
| 148 | } | |
| 149 | } | |
| 150 | ||
| 151 | static void run_all_tests(void) { | |
| 152 | for (int i = 0; i < test_cases.length; i += 1) { | |
| 153 | TestCase *test_case = test_cases.at(i); | |
| 154 | printf("Test %d/%d %s...", i + 1, test_cases.length, test_case->case_name); | |
| 155 | run_test(test_case); | |
| 156 | printf("OK\n"); | |
| 157 | } | |
| 158 | printf("%d tests passed.\n", test_cases.length); | |
| 159 | } | |
| 160 | ||
| 161 | static void cleanup(void) { | |
| 162 | remove(tmp_source_path); | |
| 163 | remove(tmp_exe_path); | |
| 164 | } | |
| 165 | ||
| 166 | int main(int argc, char **argv) { | |
| 167 | add_all_test_cases(); | |
| 168 | run_all_tests(); | |
| 169 | cleanup(); | |
| 170 | } |
test/standalone.cpp deleted-170| ... | ... | @@ -1,170 +0,0 @@ |
| 1 | /* | |
| 2 | * Copyright (c) 2015 Andrew Kelley | |
| 3 | * | |
| 4 | * This file is part of zig, which is MIT licensed. | |
| 5 | * See http://opensource.org/licenses/MIT | |
| 6 | */ | |
| 7 | ||
| 8 | #include "list.hpp" | |
| 9 | #include "buffer.hpp" | |
| 10 | #include "os.hpp" | |
| 11 | ||
| 12 | #include <stdio.h> | |
| 13 | ||
| 14 | struct TestSourceFile { | |
| 15 | const char *relative_path; | |
| 16 | const char *text; | |
| 17 | }; | |
| 18 | ||
| 19 | struct TestCase { | |
| 20 | const char *case_name; | |
| 21 | const char *output; | |
| 22 | const char *source; | |
| 23 | ZigList<const char *> compile_errors; | |
| 24 | ZigList<const char *> compiler_args; | |
| 25 | ZigList<const char *> program_args; | |
| 26 | }; | |
| 27 | ||
| 28 | ZigList<TestCase*> test_cases = {0}; | |
| 29 | const char *tmp_source_path = ".tmp_source.zig"; | |
| 30 | const char *tmp_exe_path = "./.tmp_exe"; | |
| 31 | ||
| 32 | static void add_simple_case(const char *case_name, const char *source, const char *output) { | |
| 33 | TestCase *test_case = allocate<TestCase>(1); | |
| 34 | test_case->case_name = case_name; | |
| 35 | test_case->output = output; | |
| 36 | test_case->source = source; | |
| 37 | ||
| 38 | test_case->compiler_args.append("build"); | |
| 39 | test_case->compiler_args.append(tmp_source_path); | |
| 40 | test_case->compiler_args.append("--output"); | |
| 41 | test_case->compiler_args.append(tmp_exe_path); | |
| 42 | test_case->compiler_args.append("--release"); | |
| 43 | test_case->compiler_args.append("--strip"); | |
| 44 | ||
| 45 | test_cases.append(test_case); | |
| 46 | } | |
| 47 | ||
| 48 | static void add_all_test_cases(void) { | |
| 49 | add_simple_case("hello world with libc", R"SOURCE( | |
| 50 | #link("c") | |
| 51 | extern { | |
| 52 | fn puts(s: *mut u8) -> i32; | |
| 53 | fn exit(code: i32) -> unreachable; | |
| 54 | } | |
| 55 | ||
| 56 | export fn _start() -> unreachable { | |
| 57 | puts("Hello, world!"); | |
| 58 | exit(0); | |
| 59 | } | |
| 60 | )SOURCE", "Hello, world!\n"); | |
| 61 | ||
| 62 | add_simple_case("function call", R"SOURCE( | |
| 63 | #link("c") | |
| 64 | extern { | |
| 65 | fn puts(s: *mut u8) -> i32; | |
| 66 | fn exit(code: i32) -> unreachable; | |
| 67 | } | |
| 68 | ||
| 69 | fn empty_function_1() {} | |
| 70 | fn empty_function_2() { return; } | |
| 71 | ||
| 72 | export fn _start() -> unreachable { | |
| 73 | empty_function_1(); | |
| 74 | empty_function_2(); | |
| 75 | this_is_a_function(); | |
| 76 | } | |
| 77 | ||
| 78 | fn this_is_a_function() -> unreachable { | |
| 79 | puts("OK"); | |
| 80 | exit(0); | |
| 81 | } | |
| 82 | )SOURCE", "OK\n"); | |
| 83 | ||
| 84 | add_simple_case("comments", R"SOURCE( | |
| 85 | #link("c") | |
| 86 | extern { | |
| 87 | fn puts(s: *mut u8) -> i32; | |
| 88 | fn exit(code: i32) -> unreachable; | |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * multi line doc comment | |
| 93 | */ | |
| 94 | fn another_function() {} | |
| 95 | ||
| 96 | /// this is a documentation comment | |
| 97 | /// doc comment line 2 | |
| 98 | export fn _start() -> unreachable { | |
| 99 | puts(/* mid-line comment /* nested */ */ "OK"); | |
| 100 | exit(0); | |
| 101 | } | |
| 102 | )SOURCE", "OK\n"); | |
| 103 | } | |
| 104 | ||
| 105 | static void run_test(TestCase *test_case) { | |
| 106 | os_write_file(buf_create_from_str(tmp_source_path), buf_create_from_str(test_case->source)); | |
| 107 | ||
| 108 | Buf zig_stderr = BUF_INIT; | |
| 109 | Buf zig_stdout = BUF_INIT; | |
| 110 | int return_code; | |
| 111 | static const char *zig_exe = "./zig"; | |
| 112 | os_exec_process(zig_exe, test_case->compiler_args, &return_code, &zig_stderr, &zig_stdout); | |
| 113 | ||
| 114 | if (return_code != 0) { | |
| 115 | printf("\nCompile failed with return code %d:\n", return_code); | |
| 116 | printf("%s", zig_exe); | |
| 117 | for (int i = 0; i < test_case->compiler_args.length; i += 1) { | |
| 118 | printf(" %s", test_case->compiler_args.at(i)); | |
| 119 | } | |
| 120 | printf("\n"); | |
| 121 | printf("%s\n", buf_ptr(&zig_stderr)); | |
| 122 | exit(1); | |
| 123 | } | |
| 124 | ||
| 125 | Buf program_stderr = BUF_INIT; | |
| 126 | Buf program_stdout = BUF_INIT; | |
| 127 | os_exec_process(tmp_exe_path, test_case->program_args, &return_code, &program_stderr, &program_stdout); | |
| 128 | ||
| 129 | if (return_code != 0) { | |
| 130 | printf("\nProgram exited with return code %d:\n", return_code); | |
| 131 | printf("%s", tmp_exe_path); | |
| 132 | for (int i = 0; i < test_case->program_args.length; i += 1) { | |
| 133 | printf(" %s", test_case->program_args.at(i)); | |
| 134 | } | |
| 135 | printf("\n"); | |
| 136 | printf("%s\n", buf_ptr(&program_stderr)); | |
| 137 | exit(1); | |
| 138 | } | |
| 139 | ||
| 140 | if (!buf_eql_str(&program_stdout, test_case->output)) { | |
| 141 | printf("\n"); | |
| 142 | printf("==== Test failed. Expected output: ====\n"); | |
| 143 | printf("%s\n", test_case->output); | |
| 144 | printf("========= Actual output: ==============\n"); | |
| 145 | printf("%s\n", buf_ptr(&program_stdout)); | |
| 146 | printf("=======================================\n"); | |
| 147 | exit(1); | |
| 148 | } | |
| 149 | } | |
| 150 | ||
| 151 | static void run_all_tests(void) { | |
| 152 | for (int i = 0; i < test_cases.length; i += 1) { | |
| 153 | TestCase *test_case = test_cases.at(i); | |
| 154 | printf("Test %d/%d %s...", i + 1, test_cases.length, test_case->case_name); | |
| 155 | run_test(test_case); | |
| 156 | printf("OK\n"); | |
| 157 | } | |
| 158 | printf("%d tests passed.\n", test_cases.length); | |
| 159 | } | |
| 160 | ||
| 161 | static void cleanup(void) { | |
| 162 | remove(tmp_source_path); | |
| 163 | remove(tmp_exe_path); | |
| 164 | } | |
| 165 | ||
| 166 | int main(int argc, char **argv) { | |
| 167 | add_all_test_cases(); | |
| 168 | run_all_tests(); | |
| 169 | cleanup(); | |
| 170 | } |