diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-07-24 00:43:12 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-07-24 00:43:12 -0400 |
| commit | dd9728c5a03844267bc378c326c353fd2b0e084e (patch) | |
| tree | 5786bd228312976ee482a58463a798bc426d64af /test | |
| parent | 558b0b87913dfb6e6b76f5dbe2c36b920302faab (diff) | |
| parent | 10bdf73a02c90dc375985e49b08b5020cfc20b93 (diff) | |
| download | zig-dd9728c5a03844267bc378c326c353fd2b0e084e.tar.gz zig-dd9728c5a03844267bc378c326c353fd2b0e084e.zip | |
Merge remote-tracking branch 'origin/master' into llvm7
Diffstat (limited to 'test')
| -rw-r--r-- | test/cases/cast.zig | 17 | ||||
| -rw-r--r-- | test/cases/defer.zig | 15 | ||||
| -rw-r--r-- | test/cases/eval.zig | 10 | ||||
| -rw-r--r-- | test/gen_h.zig | 47 | ||||
| -rw-r--r-- | test/stage2/compare_output.zig | 12 | ||||
| -rw-r--r-- | test/stage2/compile_errors.zig | 18 | ||||
| -rw-r--r-- | test/tests.zig | 11 |
7 files changed, 123 insertions, 7 deletions
diff --git a/test/cases/cast.zig b/test/cases/cast.zig index 5688d90e11..63cc6313e1 100644 --- a/test/cases/cast.zig +++ b/test/cases/cast.zig @@ -468,3 +468,20 @@ test "@intCast i32 to u7" { var z = x >> @intCast(u7, y); assert(z == 0xff); } + +test "implicit cast undefined to optional" { + assert(MakeType(void).getNull() == null); + assert(MakeType(void).getNonNull() != null); +} + +fn MakeType(comptime T: type) type { + return struct { + fn getNull() ?T { + return null; + } + + fn getNonNull() ?T { + return T(undefined); + } + }; +} diff --git a/test/cases/defer.zig b/test/cases/defer.zig index d2b00d1f91..7d4d1bc3d8 100644 --- a/test/cases/defer.zig +++ b/test/cases/defer.zig @@ -61,3 +61,18 @@ test "defer and labeled break" { assert(i == 1); } + +test "errdefer does not apply to fn inside fn" { + if (testNestedFnErrDefer()) |_| @panic("expected error") else |e| assert(e == error.Bad); +} + +fn testNestedFnErrDefer() error!void { + var a: i32 = 0; + errdefer a += 1; + const S = struct { + fn baz() error { + return error.Bad; + } + }; + return S.baz(); +} diff --git a/test/cases/eval.zig b/test/cases/eval.zig index 83d2e80176..9da475994d 100644 --- a/test/cases/eval.zig +++ b/test/cases/eval.zig @@ -642,3 +642,13 @@ test "@tagName of @typeId" { const str = @tagName(@typeId(u8)); assert(std.mem.eql(u8, str, "Int")); } + +test "setting backward branch quota just before a generic fn call" { + @setEvalBranchQuota(1001); + loopNTimes(1001); +} + +fn loopNTimes(comptime n: usize) void { + comptime var i = 0; + inline while (i < n) : (i += 1) {} +} diff --git a/test/gen_h.zig b/test/gen_h.zig index e6a757ea6d..b3aaa263d6 100644 --- a/test/gen_h.zig +++ b/test/gen_h.zig @@ -76,4 +76,51 @@ pub fn addCases(cases: *tests.GenHContext) void { \\TEST_EXPORT void entry(struct Foo foo, uint8_t bar[]); \\ ); + + cases.add("ptr to zig struct", + \\const S = struct { + \\ a: u8, + \\}; + \\ + \\export fn a(s: *S) u8 { + \\ return s.a; + \\} + + , + \\struct S; + \\TEST_EXPORT uint8_t a(struct S * s); + \\ + ); + + cases.add("ptr to zig union", + \\const U = union(enum) { + \\ A: u8, + \\ B: u16, + \\}; + \\ + \\export fn a(s: *U) u8 { + \\ return s.A; + \\} + + , + \\union U; + \\TEST_EXPORT uint8_t a(union U * s); + \\ + ); + + cases.add("ptr to zig enum", + \\const E = enum(u8) { + \\ A, + \\ B, + \\}; + \\ + \\export fn a(s: *E) u8 { + \\ return @enumToInt(s.*); + \\} + + , + \\enum E; + \\TEST_EXPORT uint8_t a(enum E * s); + \\ + ); } diff --git a/test/stage2/compare_output.zig b/test/stage2/compare_output.zig new file mode 100644 index 0000000000..35adcbb96b --- /dev/null +++ b/test/stage2/compare_output.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const TestContext = @import("../../src-self-hosted/test.zig").TestContext; + +pub fn addCases(ctx: *TestContext) !void { + try ctx.testCompareOutputLibC( + \\extern fn puts([*]const u8) void; + \\export fn main() c_int { + \\ puts(c"Hello, world!"); + \\ return 0; + \\} + , "Hello, world!" ++ std.cstr.line_sep); +} diff --git a/test/stage2/compile_errors.zig b/test/stage2/compile_errors.zig index 1dca908e69..2cecd78653 100644 --- a/test/stage2/compile_errors.zig +++ b/test/stage2/compile_errors.zig @@ -9,4 +9,22 @@ pub fn addCases(ctx: *TestContext) !void { try ctx.testCompileError( \\fn() void {} , "1.zig", 1, 1, "missing function name"); + + try ctx.testCompileError( + \\comptime { + \\ return; + \\} + , "1.zig", 2, 5, "return expression outside function definition"); + + try ctx.testCompileError( + \\export fn entry() void { + \\ defer return; + \\} + , "1.zig", 2, 11, "cannot return from defer expression"); + + try ctx.testCompileError( + \\export fn entry() c_int { + \\ return 36893488147419103232; + \\} + , "1.zig", 2, 12, "integer value '36893488147419103232' cannot be stored in type 'c_int'"); } diff --git a/test/tests.zig b/test/tests.zig index 3a72f58753..aa5eed17ee 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -89,12 +89,13 @@ pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes: return cases.step; } -pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step { +pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { const cases = b.allocator.create(BuildExamplesContext{ .b = b, .step = b.step("test-build-examples", "Build the examples"), .test_index = 0, .test_filter = test_filter, + .modes = modes, }) catch unreachable; build_examples.addCases(cases); @@ -697,6 +698,7 @@ pub const BuildExamplesContext = struct { step: *build.Step, test_index: usize, test_filter: ?[]const u8, + modes: []const Mode, pub fn addC(self: *BuildExamplesContext, root_src: []const u8) void { self.addAllArgs(root_src, true); @@ -739,12 +741,7 @@ pub const BuildExamplesContext = struct { pub fn addAllArgs(self: *BuildExamplesContext, root_src: []const u8, link_libc: bool) void { const b = self.b; - for ([]Mode{ - Mode.Debug, - Mode.ReleaseSafe, - Mode.ReleaseFast, - Mode.ReleaseSmall, - }) |mode| { + for (self.modes) |mode| { const annotated_case_name = fmt.allocPrint(self.b.allocator, "build {} ({})", root_src, @tagName(mode)) catch unreachable; if (self.test_filter) |filter| { if (mem.indexOf(u8, annotated_case_name, filter) == null) continue; |
