diff --git a/build.zig b/build.zig index 2cd080489450a2cff0c8b81724ad92455034baf5..07041d026fddd2c8696789572c31dac03d0aff10 100644 --- a/build.zig +++ b/build.zig @@ -134,7 +134,7 @@ pub fn build(b: *Builder) !void { test_step.dependOn(tests.addPkgTests(b, test_filter, "std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes, skip_non_native)); test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes)); - test_step.dependOn(tests.addBuildExampleTests(b, test_filter, modes)); + test_step.dependOn(tests.addStandaloneTests(b, test_filter, modes)); test_step.dependOn(tests.addCliTests(b, test_filter, modes)); test_step.dependOn(tests.addCompileErrorTests(b, test_filter, modes)); test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes)); diff --git a/example/README.md b/example/README.md deleted file mode 100644 index 188ece00a2770e481ce051a0b5fe5835c1372a92..0000000000000000000000000000000000000000 --- a/example/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# Zig Examples - - * **Tetris** - A simple Tetris clone written in Zig. See - [andrewrk/tetris](https://github.com/andrewrk/tetris). - * **hello_world** - demonstration of a printing a single line to stdout. - One version depends on libc; one does not. - * **guess_number** - simple console game where you guess the number the - computer is thinking of and it says higher or lower. No dependency on - libc. - * **cat** - implementation of the `cat` UNIX utility in Zig, with no dependency - on libc. - * **shared_library** - demonstration of building a shared library and generating - a header file for interop with C code. - * **mix_o_files** - how to mix .zig and .c files together as object files diff --git a/example/cat/main.zig b/example/cat/main.zig deleted file mode 100644 index c57c1e4bcbffa7b8b33a258ea6075184c97bf909..0000000000000000000000000000000000000000 --- a/example/cat/main.zig +++ /dev/null @@ -1,70 +0,0 @@ -const std = @import("std"); -const io = std.io; -const process = std.process; -const File = std.fs.File; -const mem = std.mem; -const warn = std.debug.warn; -const allocator = std.debug.global_allocator; - -pub fn main() !void { - var args_it = process.args(); - const exe = try unwrapArg(args_it.next(allocator).?); - var catted_anything = false; - var stdout_file = try io.getStdOut(); - - while (args_it.next(allocator)) |arg_or_err| { - const arg = try unwrapArg(arg_or_err); - if (mem.eql(u8, arg, "-")) { - catted_anything = true; - var stdin_file = try io.getStdIn(); - try cat_file(&stdout_file, &stdin_file); - } else if (arg[0] == '-') { - return usage(exe); - } else { - var file = File.openRead(arg) catch |err| { - warn("Unable to open file: {}\n", @errorName(err)); - return err; - }; - defer file.close(); - - catted_anything = true; - try cat_file(&stdout_file, &file); - } - } - if (!catted_anything) { - var stdin_file = try io.getStdIn(); - try cat_file(&stdout_file, &stdin_file); - } -} - -fn usage(exe: []const u8) !void { - warn("Usage: {} [FILE]...\n", exe); - return error.Invalid; -} - -fn cat_file(stdout: *File, file: *File) !void { - var buf: [1024 * 4]u8 = undefined; - - while (true) { - const bytes_read = file.read(buf[0..]) catch |err| { - warn("Unable to read from stream: {}\n", @errorName(err)); - return err; - }; - - if (bytes_read == 0) { - break; - } - - stdout.write(buf[0..bytes_read]) catch |err| { - warn("Unable to write to stdout: {}\n", @errorName(err)); - return err; - }; - } -} - -fn unwrapArg(arg: anyerror![]u8) ![]u8 { - return arg catch |err| { - warn("Unable to parse command line: {}\n", err); - return err; - }; -} diff --git a/example/guess_number/main.zig b/example/guess_number/main.zig deleted file mode 100644 index b1e557fd20c51cd6a39766236bb9772a772ea114..0000000000000000000000000000000000000000 --- a/example/guess_number/main.zig +++ /dev/null @@ -1,47 +0,0 @@ -const builtin = @import("builtin"); -const std = @import("std"); -const io = std.io; -const fmt = std.fmt; - -pub fn main() !void { - var stdout_file = try io.getStdOut(); - const stdout = &stdout_file.outStream().stream; - - try stdout.print("Welcome to the Guess Number Game in Zig.\n"); - - var seed_bytes: [@sizeOf(u64)]u8 = undefined; - std.crypto.randomBytes(seed_bytes[0..]) catch |err| { - std.debug.warn("unable to seed random number generator: {}", err); - return err; - }; - const seed = std.mem.readIntNative(u64, &seed_bytes); - var prng = std.rand.DefaultPrng.init(seed); - - const answer = prng.random.range(u8, 0, 100) + 1; - - while (true) { - try stdout.print("\nGuess a number between 1 and 100: "); - var line_buf: [20]u8 = undefined; - - const line = io.readLineSlice(line_buf[0..]) catch |err| switch (err) { - error.OutOfMemory => { - try stdout.print("Input too long.\n"); - continue; - }, - else => return err, - }; - - const guess = fmt.parseUnsigned(u8, line, 10) catch { - try stdout.print("Invalid number.\n"); - continue; - }; - if (guess > answer) { - try stdout.print("Guess lower.\n"); - } else if (guess < answer) { - try stdout.print("Guess higher.\n"); - } else { - try stdout.print("You win!\n"); - return; - } - } -} diff --git a/example/hello_world/hello.zig b/example/hello_world/hello.zig deleted file mode 100644 index cb7d5ef157e051c5f286a1014562caeab65c5bb4..0000000000000000000000000000000000000000 --- a/example/hello_world/hello.zig +++ /dev/null @@ -1,9 +0,0 @@ -const std = @import("std"); - -pub fn main() !void { - // If this program is run without stdout attached, exit with an error. - const stdout_file = try std.io.getStdOut(); - // If this program encounters pipe failure when printing to stdout, exit - // with an error. - try stdout_file.write("Hello, world!\n"); -} diff --git a/example/hello_world/hello_libc.zig b/example/hello_world/hello_libc.zig deleted file mode 100644 index 05356bf5294f5f61986738fd770ec770c2c59100..0000000000000000000000000000000000000000 --- a/example/hello_world/hello_libc.zig +++ /dev/null @@ -1,13 +0,0 @@ -const c = @cImport({ - // See https://github.com/ziglang/zig/issues/515 - @cDefine("_NO_CRT_STDIO_INLINE", "1"); - @cInclude("stdio.h"); - @cInclude("string.h"); -}); - -const msg = c"Hello, world!\n"; - -export fn main(argc: c_int, argv: **u8) c_int { - if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1; - return 0; -} diff --git a/example/hello_world/hello_windows.zig b/example/hello_world/hello_windows.zig deleted file mode 100644 index 8016c3c4907525133c21bbb23a49a3215ae9d4c0..0000000000000000000000000000000000000000 --- a/example/hello_world/hello_windows.zig +++ /dev/null @@ -1,8 +0,0 @@ -use @import("std").os.windows; - -extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) c_int; - -export fn WinMain(hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: PWSTR, nCmdShow: INT) INT { - _ = MessageBoxA(null, c"hello", c"title", 0); - return 0; -} diff --git a/example/mix_o_files/base64.zig b/example/mix_o_files/base64.zig deleted file mode 100644 index 7ded9824a08de04d15b1088d94ff25371b20b104..0000000000000000000000000000000000000000 --- a/example/mix_o_files/base64.zig +++ /dev/null @@ -1,13 +0,0 @@ -const base64 = @import("std").base64; - -export fn decode_base_64(dest_ptr: [*]u8, dest_len: usize, source_ptr: [*]const u8, source_len: usize) usize { - const src = source_ptr[0..source_len]; - const dest = dest_ptr[0..dest_len]; - const base64_decoder = base64.standard_decoder_unsafe; - const decoded_size = base64_decoder.calcSize(src); - base64_decoder.decode(dest[0..decoded_size], src); - return decoded_size; -} - -var x: c_int = 1234; -export var x_ptr = &x; diff --git a/example/mix_o_files/build.zig b/example/mix_o_files/build.zig deleted file mode 100644 index 7c72cfcc3abec05242d03e308bda4c6de0ce967a..0000000000000000000000000000000000000000 --- a/example/mix_o_files/build.zig +++ /dev/null @@ -1,17 +0,0 @@ -const Builder = @import("std").build.Builder; - -pub fn build(b: *Builder) void { - const obj = b.addObject("base64", "base64.zig"); - - const exe = b.addExecutable("test", null); - exe.addCSourceFile("test.c", [_][]const u8{"-std=c99"}); - exe.addObject(obj); - exe.linkSystemLibrary("c"); - - b.default_step.dependOn(&exe.step); - - const run_cmd = exe.run(); - - const test_step = b.step("test", "Test the program"); - test_step.dependOn(&run_cmd.step); -} diff --git a/example/mix_o_files/test.c b/example/mix_o_files/test.c deleted file mode 100644 index d821bbe108420861b21383fe23f44202936476c3..0000000000000000000000000000000000000000 --- a/example/mix_o_files/test.c +++ /dev/null @@ -1,20 +0,0 @@ -// This header is generated by zig from base64.zig -#include "base64.h" - -#include -#include - -extern int *x_ptr; - -int main(int argc, char **argv) { - const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz"; - char buf[200]; - - size_t len = decode_base_64((uint8_t *)buf, 200, (uint8_t *)encoded, strlen(encoded)); - buf[len] = 0; - assert(strcmp(buf, "all your base are belong to us") == 0); - - assert(*x_ptr == 1234); - - return 0; -} diff --git a/example/shared_library/build.zig b/example/shared_library/build.zig deleted file mode 100644 index 129c5dc1c9fa6e76520df88987c66fe5f7ecbebf..0000000000000000000000000000000000000000 --- a/example/shared_library/build.zig +++ /dev/null @@ -1,17 +0,0 @@ -const Builder = @import("std").build.Builder; - -pub fn build(b: *Builder) void { - const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0)); - - const exe = b.addExecutable("test", null); - exe.addCSourceFile("test.c", [_][]const u8{"-std=c99"}); - exe.linkLibrary(lib); - exe.linkSystemLibrary("c"); - - b.default_step.dependOn(&exe.step); - - const run_cmd = exe.run(); - - const test_step = b.step("test", "Test the program"); - test_step.dependOn(&run_cmd.step); -} diff --git a/example/shared_library/mathtest.zig b/example/shared_library/mathtest.zig deleted file mode 100644 index a04ec1544dc82e27afb137de2672a48d726a72fc..0000000000000000000000000000000000000000 --- a/example/shared_library/mathtest.zig +++ /dev/null @@ -1,3 +0,0 @@ -export fn add(a: i32, b: i32) i32 { - return a + b; -} diff --git a/example/shared_library/test.c b/example/shared_library/test.c deleted file mode 100644 index b60a6a5a752f2ecb4cb9c549046a5ad930616900..0000000000000000000000000000000000000000 --- a/example/shared_library/test.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "mathtest.h" -#include - -int main(int argc, char **argv) { - assert(add(42, 1337) == 1379); - return 0; -} diff --git a/test/build_examples.zig b/test/build_examples.zig deleted file mode 100644 index 4733a25b643d5f0f5f6695eb320d481c7ba78e2e..0000000000000000000000000000000000000000 --- a/test/build_examples.zig +++ /dev/null @@ -1,31 +0,0 @@ -const tests = @import("tests.zig"); -const builtin = @import("builtin"); -const is_windows = builtin.os == builtin.Os.windows; - -pub fn addCases(cases: *tests.BuildExamplesContext) void { - cases.add("example/hello_world/hello.zig"); - cases.addC("example/hello_world/hello_libc.zig"); - cases.add("example/cat/main.zig"); - cases.add("example/guess_number/main.zig"); - cases.add("test/standalone/main_return_error/error_u8.zig"); - cases.add("test/standalone/main_return_error/error_u8_non_zero.zig"); - cases.addBuildFile("test/standalone/main_pkg_path/build.zig"); - cases.addBuildFile("example/shared_library/build.zig"); - cases.addBuildFile("example/mix_o_files/build.zig"); - cases.addBuildFile("test/standalone/static_c_lib/build.zig"); - cases.addBuildFile("test/standalone/issue_339/build.zig"); - cases.addBuildFile("test/standalone/issue_794/build.zig"); - cases.addBuildFile("test/standalone/pkg_import/build.zig"); - cases.addBuildFile("test/standalone/use_alias/build.zig"); - cases.addBuildFile("test/standalone/brace_expansion/build.zig"); - cases.addBuildFile("test/standalone/empty_env/build.zig"); - if (builtin.os == builtin.Os.linux) { - // TODO hook up the DynLib API for windows using LoadLibraryA - // TODO figure out how to make this work on darwin - probably libSystem has dlopen/dlsym in it - cases.addBuildFile("test/standalone/load_dynamic_library/build.zig"); - } - - if (builtin.arch == builtin.Arch.x86_64) { // TODO add C ABI support for other architectures - cases.addBuildFile("test/stage1/c_abi/build.zig"); - } -} diff --git a/test/standalone.zig b/test/standalone.zig new file mode 100644 index 0000000000000000000000000000000000000000..f3a1f735daada842a961cce8f581ae57520ef1bb --- /dev/null +++ b/test/standalone.zig @@ -0,0 +1,31 @@ +const tests = @import("tests.zig"); +const builtin = @import("builtin"); +const is_windows = builtin.os == builtin.Os.windows; + +pub fn addCases(cases: *tests.StandaloneContext) void { + cases.add("test/standalone/hello_world/hello.zig"); + cases.addC("test/standalone/hello_world/hello_libc.zig"); + cases.add("test/standalone/cat/main.zig"); + cases.add("test/standalone/guess_number/main.zig"); + cases.add("test/standalone/main_return_error/error_u8.zig"); + cases.add("test/standalone/main_return_error/error_u8_non_zero.zig"); + cases.addBuildFile("test/standalone/main_pkg_path/build.zig"); + cases.addBuildFile("test/standalone/shared_library/build.zig"); + cases.addBuildFile("test/standalone/mix_o_files/build.zig"); + cases.addBuildFile("test/standalone/static_c_lib/build.zig"); + cases.addBuildFile("test/standalone/issue_339/build.zig"); + cases.addBuildFile("test/standalone/issue_794/build.zig"); + cases.addBuildFile("test/standalone/pkg_import/build.zig"); + cases.addBuildFile("test/standalone/use_alias/build.zig"); + cases.addBuildFile("test/standalone/brace_expansion/build.zig"); + cases.addBuildFile("test/standalone/empty_env/build.zig"); + if (builtin.os == builtin.Os.linux) { + // TODO hook up the DynLib API for windows using LoadLibraryA + // TODO figure out how to make this work on darwin - probably libSystem has dlopen/dlsym in it + cases.addBuildFile("test/standalone/load_dynamic_library/build.zig"); + } + + if (builtin.arch == builtin.Arch.x86_64) { // TODO add C ABI support for other architectures + cases.addBuildFile("test/stage1/c_abi/build.zig"); + } +} diff --git a/test/standalone/cat/main.zig b/test/standalone/cat/main.zig new file mode 100644 index 0000000000000000000000000000000000000000..c57c1e4bcbffa7b8b33a258ea6075184c97bf909 --- /dev/null +++ b/test/standalone/cat/main.zig @@ -0,0 +1,70 @@ +const std = @import("std"); +const io = std.io; +const process = std.process; +const File = std.fs.File; +const mem = std.mem; +const warn = std.debug.warn; +const allocator = std.debug.global_allocator; + +pub fn main() !void { + var args_it = process.args(); + const exe = try unwrapArg(args_it.next(allocator).?); + var catted_anything = false; + var stdout_file = try io.getStdOut(); + + while (args_it.next(allocator)) |arg_or_err| { + const arg = try unwrapArg(arg_or_err); + if (mem.eql(u8, arg, "-")) { + catted_anything = true; + var stdin_file = try io.getStdIn(); + try cat_file(&stdout_file, &stdin_file); + } else if (arg[0] == '-') { + return usage(exe); + } else { + var file = File.openRead(arg) catch |err| { + warn("Unable to open file: {}\n", @errorName(err)); + return err; + }; + defer file.close(); + + catted_anything = true; + try cat_file(&stdout_file, &file); + } + } + if (!catted_anything) { + var stdin_file = try io.getStdIn(); + try cat_file(&stdout_file, &stdin_file); + } +} + +fn usage(exe: []const u8) !void { + warn("Usage: {} [FILE]...\n", exe); + return error.Invalid; +} + +fn cat_file(stdout: *File, file: *File) !void { + var buf: [1024 * 4]u8 = undefined; + + while (true) { + const bytes_read = file.read(buf[0..]) catch |err| { + warn("Unable to read from stream: {}\n", @errorName(err)); + return err; + }; + + if (bytes_read == 0) { + break; + } + + stdout.write(buf[0..bytes_read]) catch |err| { + warn("Unable to write to stdout: {}\n", @errorName(err)); + return err; + }; + } +} + +fn unwrapArg(arg: anyerror![]u8) ![]u8 { + return arg catch |err| { + warn("Unable to parse command line: {}\n", err); + return err; + }; +} diff --git a/test/standalone/guess_number/main.zig b/test/standalone/guess_number/main.zig new file mode 100644 index 0000000000000000000000000000000000000000..b1e557fd20c51cd6a39766236bb9772a772ea114 --- /dev/null +++ b/test/standalone/guess_number/main.zig @@ -0,0 +1,47 @@ +const builtin = @import("builtin"); +const std = @import("std"); +const io = std.io; +const fmt = std.fmt; + +pub fn main() !void { + var stdout_file = try io.getStdOut(); + const stdout = &stdout_file.outStream().stream; + + try stdout.print("Welcome to the Guess Number Game in Zig.\n"); + + var seed_bytes: [@sizeOf(u64)]u8 = undefined; + std.crypto.randomBytes(seed_bytes[0..]) catch |err| { + std.debug.warn("unable to seed random number generator: {}", err); + return err; + }; + const seed = std.mem.readIntNative(u64, &seed_bytes); + var prng = std.rand.DefaultPrng.init(seed); + + const answer = prng.random.range(u8, 0, 100) + 1; + + while (true) { + try stdout.print("\nGuess a number between 1 and 100: "); + var line_buf: [20]u8 = undefined; + + const line = io.readLineSlice(line_buf[0..]) catch |err| switch (err) { + error.OutOfMemory => { + try stdout.print("Input too long.\n"); + continue; + }, + else => return err, + }; + + const guess = fmt.parseUnsigned(u8, line, 10) catch { + try stdout.print("Invalid number.\n"); + continue; + }; + if (guess > answer) { + try stdout.print("Guess lower.\n"); + } else if (guess < answer) { + try stdout.print("Guess higher.\n"); + } else { + try stdout.print("You win!\n"); + return; + } + } +} diff --git a/test/standalone/hello_world/hello.zig b/test/standalone/hello_world/hello.zig new file mode 100644 index 0000000000000000000000000000000000000000..cb7d5ef157e051c5f286a1014562caeab65c5bb4 --- /dev/null +++ b/test/standalone/hello_world/hello.zig @@ -0,0 +1,9 @@ +const std = @import("std"); + +pub fn main() !void { + // If this program is run without stdout attached, exit with an error. + const stdout_file = try std.io.getStdOut(); + // If this program encounters pipe failure when printing to stdout, exit + // with an error. + try stdout_file.write("Hello, world!\n"); +} diff --git a/test/standalone/hello_world/hello_libc.zig b/test/standalone/hello_world/hello_libc.zig new file mode 100644 index 0000000000000000000000000000000000000000..05356bf5294f5f61986738fd770ec770c2c59100 --- /dev/null +++ b/test/standalone/hello_world/hello_libc.zig @@ -0,0 +1,13 @@ +const c = @cImport({ + // See https://github.com/ziglang/zig/issues/515 + @cDefine("_NO_CRT_STDIO_INLINE", "1"); + @cInclude("stdio.h"); + @cInclude("string.h"); +}); + +const msg = c"Hello, world!\n"; + +export fn main(argc: c_int, argv: **u8) c_int { + if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1; + return 0; +} diff --git a/test/standalone/mix_o_files/base64.zig b/test/standalone/mix_o_files/base64.zig new file mode 100644 index 0000000000000000000000000000000000000000..7ded9824a08de04d15b1088d94ff25371b20b104 --- /dev/null +++ b/test/standalone/mix_o_files/base64.zig @@ -0,0 +1,13 @@ +const base64 = @import("std").base64; + +export fn decode_base_64(dest_ptr: [*]u8, dest_len: usize, source_ptr: [*]const u8, source_len: usize) usize { + const src = source_ptr[0..source_len]; + const dest = dest_ptr[0..dest_len]; + const base64_decoder = base64.standard_decoder_unsafe; + const decoded_size = base64_decoder.calcSize(src); + base64_decoder.decode(dest[0..decoded_size], src); + return decoded_size; +} + +var x: c_int = 1234; +export var x_ptr = &x; diff --git a/test/standalone/mix_o_files/build.zig b/test/standalone/mix_o_files/build.zig new file mode 100644 index 0000000000000000000000000000000000000000..7c72cfcc3abec05242d03e308bda4c6de0ce967a --- /dev/null +++ b/test/standalone/mix_o_files/build.zig @@ -0,0 +1,17 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const obj = b.addObject("base64", "base64.zig"); + + const exe = b.addExecutable("test", null); + exe.addCSourceFile("test.c", [_][]const u8{"-std=c99"}); + exe.addObject(obj); + exe.linkSystemLibrary("c"); + + b.default_step.dependOn(&exe.step); + + const run_cmd = exe.run(); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&run_cmd.step); +} diff --git a/test/standalone/mix_o_files/test.c b/test/standalone/mix_o_files/test.c new file mode 100644 index 0000000000000000000000000000000000000000..d821bbe108420861b21383fe23f44202936476c3 --- /dev/null +++ b/test/standalone/mix_o_files/test.c @@ -0,0 +1,20 @@ +// This header is generated by zig from base64.zig +#include "base64.h" + +#include +#include + +extern int *x_ptr; + +int main(int argc, char **argv) { + const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz"; + char buf[200]; + + size_t len = decode_base_64((uint8_t *)buf, 200, (uint8_t *)encoded, strlen(encoded)); + buf[len] = 0; + assert(strcmp(buf, "all your base are belong to us") == 0); + + assert(*x_ptr == 1234); + + return 0; +} diff --git a/test/standalone/shared_library/build.zig b/test/standalone/shared_library/build.zig new file mode 100644 index 0000000000000000000000000000000000000000..129c5dc1c9fa6e76520df88987c66fe5f7ecbebf --- /dev/null +++ b/test/standalone/shared_library/build.zig @@ -0,0 +1,17 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0)); + + const exe = b.addExecutable("test", null); + exe.addCSourceFile("test.c", [_][]const u8{"-std=c99"}); + exe.linkLibrary(lib); + exe.linkSystemLibrary("c"); + + b.default_step.dependOn(&exe.step); + + const run_cmd = exe.run(); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&run_cmd.step); +} diff --git a/test/standalone/shared_library/mathtest.zig b/test/standalone/shared_library/mathtest.zig new file mode 100644 index 0000000000000000000000000000000000000000..a04ec1544dc82e27afb137de2672a48d726a72fc --- /dev/null +++ b/test/standalone/shared_library/mathtest.zig @@ -0,0 +1,3 @@ +export fn add(a: i32, b: i32) i32 { + return a + b; +} diff --git a/test/standalone/shared_library/test.c b/test/standalone/shared_library/test.c new file mode 100644 index 0000000000000000000000000000000000000000..b60a6a5a752f2ecb4cb9c549046a5ad930616900 --- /dev/null +++ b/test/standalone/shared_library/test.c @@ -0,0 +1,7 @@ +#include "mathtest.h" +#include + +int main(int argc, char **argv) { + assert(add(42, 1337) == 1379); + return 0; +} diff --git a/test/tests.zig b/test/tests.zig index 411f16d92b3a3808c1360ecbc9c91a47733f9ab0..d94c012ac14655af305e6ded27e2569e8ad89e82 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -13,7 +13,7 @@ const Mode = builtin.Mode; const LibExeObjStep = build.LibExeObjStep; const compare_output = @import("compare_output.zig"); -const build_examples = @import("build_examples.zig"); +const standalone = @import("standalone.zig"); const compile_errors = @import("compile_errors.zig"); const assemble_and_link = @import("assemble_and_link.zig"); const runtime_safety = @import("runtime_safety.zig"); @@ -91,17 +91,17 @@ pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes: return cases.step; } -pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { - const cases = b.allocator.create(BuildExamplesContext) catch unreachable; - cases.* = BuildExamplesContext{ +pub fn addStandaloneTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { + const cases = b.allocator.create(StandaloneContext) catch unreachable; + cases.* = StandaloneContext{ .b = b, - .step = b.step("test-build-examples", "Build the examples"), + .step = b.step("test-standalone", "Run the standalone tests"), .test_index = 0, .test_filter = test_filter, .modes = modes, }; - build_examples.addCases(cases); + standalone.addCases(cases); return cases.step; } @@ -830,22 +830,22 @@ pub const CompileErrorContext = struct { } }; -pub const BuildExamplesContext = struct { +pub const StandaloneContext = struct { b: *build.Builder, step: *build.Step, test_index: usize, test_filter: ?[]const u8, modes: []const Mode, - pub fn addC(self: *BuildExamplesContext, root_src: []const u8) void { + pub fn addC(self: *StandaloneContext, root_src: []const u8) void { self.addAllArgs(root_src, true); } - pub fn add(self: *BuildExamplesContext, root_src: []const u8) void { + pub fn add(self: *StandaloneContext, root_src: []const u8) void { self.addAllArgs(root_src, false); } - pub fn addBuildFile(self: *BuildExamplesContext, build_file: []const u8) void { + pub fn addBuildFile(self: *StandaloneContext, build_file: []const u8) void { const b = self.b; const annotated_case_name = b.fmt("build {} (Debug)", build_file); @@ -875,7 +875,7 @@ pub const BuildExamplesContext = struct { self.step.dependOn(&log_step.step); } - pub fn addAllArgs(self: *BuildExamplesContext, root_src: []const u8, link_libc: bool) void { + pub fn addAllArgs(self: *StandaloneContext, root_src: []const u8, link_libc: bool) void { const b = self.b; for (self.modes) |mode| {