aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-07-16 12:15:46 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-07-16 12:15:46 -0400
commit9dcddc2249217c8c99c9d07bb0187904847d2ae2 (patch)
tree1edf9eddb4d9353a5778c86c69366de963ea7eeb /test
parent92e781baa147a7d07af4cb3c1f08c08bed8613e4 (diff)
downloadzig-9dcddc2249217c8c99c9d07bb0187904847d2ae2.tar.gz
zig-9dcddc2249217c8c99c9d07bb0187904847d2ae2.zip
retire the example/ folder, rename test-build-examples to "standalone"
closes #2759
Diffstat (limited to 'test')
-rw-r--r--test/standalone.zig (renamed from test/build_examples.zig)14
-rw-r--r--test/standalone/cat/main.zig70
-rw-r--r--test/standalone/guess_number/main.zig47
-rw-r--r--test/standalone/hello_world/hello.zig9
-rw-r--r--test/standalone/hello_world/hello_libc.zig13
-rw-r--r--test/standalone/mix_o_files/base64.zig13
-rw-r--r--test/standalone/mix_o_files/build.zig17
-rw-r--r--test/standalone/mix_o_files/test.c20
-rw-r--r--test/standalone/shared_library/build.zig17
-rw-r--r--test/standalone/shared_library/mathtest.zig3
-rw-r--r--test/standalone/shared_library/test.c7
-rw-r--r--test/tests.zig22
12 files changed, 234 insertions, 18 deletions
diff --git a/test/build_examples.zig b/test/standalone.zig
index 4733a25b64..f3a1f735da 100644
--- a/test/build_examples.zig
+++ b/test/standalone.zig
@@ -2,16 +2,16 @@ 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");
+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("example/shared_library/build.zig");
- cases.addBuildFile("example/mix_o_files/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");
diff --git a/test/standalone/cat/main.zig b/test/standalone/cat/main.zig
new file mode 100644
index 0000000000..c57c1e4bcb
--- /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 0000000000..b1e557fd20
--- /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 0000000000..cb7d5ef157
--- /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 0000000000..05356bf529
--- /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 0000000000..7ded9824a0
--- /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 0000000000..7c72cfcc3a
--- /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 0000000000..d821bbe108
--- /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 <assert.h>
+#include <string.h>
+
+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 0000000000..129c5dc1c9
--- /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 0000000000..a04ec1544d
--- /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 0000000000..b60a6a5a75
--- /dev/null
+++ b/test/standalone/shared_library/test.c
@@ -0,0 +1,7 @@
+#include "mathtest.h"
+#include <assert.h>
+
+int main(int argc, char **argv) {
+ assert(add(42, 1337) == 1379);
+ return 0;
+}
diff --git a/test/tests.zig b/test/tests.zig
index 411f16d92b..d94c012ac1 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| {