diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-09-16 10:51:58 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-09-16 10:51:58 -0400 |
| commit | a2abdb185f9e47b663edce1bdfa3fa525502f321 (patch) | |
| tree | 9027e6f6886937afa463563dae176e5757cf006e /test | |
| parent | a6bf37f8ca5a2eabc7cacb22696d2a2c622a993d (diff) | |
| parent | 780e5674467ebac4534cd3d3f2199ccaf1d0922c (diff) | |
| download | zig-a2abdb185f9e47b663edce1bdfa3fa525502f321.tar.gz zig-a2abdb185f9e47b663edce1bdfa3fa525502f321.zip | |
Merge remote-tracking branch 'origin/master' into llvm7
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior.zig | 3 | ||||
| -rw-r--r-- | test/cases/align.zig | 7 | ||||
| -rw-r--r-- | test/cases/bugs/1322.zig | 19 | ||||
| -rw-r--r-- | test/cases/bugs/1381.zig | 21 | ||||
| -rw-r--r-- | test/cases/bugs/1442.zig | 11 | ||||
| -rw-r--r-- | test/cases/cast.zig | 15 | ||||
| -rw-r--r-- | test/cases/eval.zig | 4 | ||||
| -rw-r--r-- | test/cases/misc.zig | 3 | ||||
| -rw-r--r-- | test/cases/reflection.zig | 2 | ||||
| -rw-r--r-- | test/cases/struct.zig | 4 | ||||
| -rw-r--r-- | test/cases/this.zig | 13 | ||||
| -rw-r--r-- | test/cases/type_info.zig | 4 | ||||
| -rw-r--r-- | test/cases/union.zig | 39 | ||||
| -rw-r--r-- | test/compile_errors.zig | 47 | ||||
| -rw-r--r-- | test/standalone/brace_expansion/main.zig | 2 |
15 files changed, 152 insertions, 42 deletions
diff --git a/test/behavior.zig b/test/behavior.zig index 25997a2a4b..3223131d08 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -10,7 +10,10 @@ comptime { _ = @import("cases/bool.zig"); _ = @import("cases/bugs/1111.zig"); _ = @import("cases/bugs/1277.zig"); + _ = @import("cases/bugs/1322.zig"); + _ = @import("cases/bugs/1381.zig"); _ = @import("cases/bugs/1421.zig"); + _ = @import("cases/bugs/1442.zig"); _ = @import("cases/bugs/394.zig"); _ = @import("cases/bugs/655.zig"); _ = @import("cases/bugs/656.zig"); diff --git a/test/cases/align.zig b/test/cases/align.zig index 64f0788efc..e052c3c3ad 100644 --- a/test/cases/align.zig +++ b/test/cases/align.zig @@ -212,3 +212,10 @@ fn fnWithAlignedStack() i32 { @setAlignStack(256); return 1234; } + +test "alignment of structs" { + assert(@alignOf(struct { + a: i32, + b: *i32, + }) == @alignOf(usize)); +} diff --git a/test/cases/bugs/1322.zig b/test/cases/bugs/1322.zig new file mode 100644 index 0000000000..2de92191ec --- /dev/null +++ b/test/cases/bugs/1322.zig @@ -0,0 +1,19 @@ +const std = @import("std"); + +const B = union(enum) { + c: C, + None, +}; + +const A = struct { + b: B, +}; + +const C = struct {}; + +test "tagged union with all void fields but a meaningful tag" { + var a: A = A{ .b = B{ .c = C{} } }; + std.debug.assert(@TagType(B)(a.b) == @TagType(B).c); + a = A{ .b = B.None }; + std.debug.assert(@TagType(B)(a.b) == @TagType(B).None); +} diff --git a/test/cases/bugs/1381.zig b/test/cases/bugs/1381.zig new file mode 100644 index 0000000000..2d452da156 --- /dev/null +++ b/test/cases/bugs/1381.zig @@ -0,0 +1,21 @@ +const std = @import("std"); + +const B = union(enum) { + D: u8, + E: u16, +}; + +const A = union(enum) { + B: B, + C: u8, +}; + +test "union that needs padding bytes inside an array" { + var as = []A{ + A{ .B = B{ .D = 1 } }, + A{ .B = B{ .D = 1 } }, + }; + + const a = as[0].B; + std.debug.assertOrPanic(a.D == 1); +} diff --git a/test/cases/bugs/1442.zig b/test/cases/bugs/1442.zig new file mode 100644 index 0000000000..d7057d9ed1 --- /dev/null +++ b/test/cases/bugs/1442.zig @@ -0,0 +1,11 @@ +const std = @import("std"); + +const Union = union(enum) { + Text: []const u8, + Color: u32, +}; + +test "const error union field alignment" { + var union_or_err: error!Union = Union{ .Color = 1234 }; + std.debug.assertOrPanic((union_or_err catch unreachable).Color == 1234); +} diff --git a/test/cases/cast.zig b/test/cases/cast.zig index 94fbda7899..db482a124a 100644 --- a/test/cases/cast.zig +++ b/test/cases/cast.zig @@ -64,7 +64,7 @@ test "implicitly cast a container to a const pointer of it" { fn Struct(comptime T: type) type { return struct { - const Self = this; + const Self = @This(); x: T, fn pointer(self: *const Self) Self { @@ -106,7 +106,7 @@ const Enum = enum { test "implicitly cast indirect pointer to maybe-indirect pointer" { const S = struct { - const Self = this; + const Self = @This(); x: u8, fn constConst(p: *const *const Self) u8 { return p.*.x; @@ -526,3 +526,14 @@ test "*usize to *void" { var v = @ptrCast(*void, &i); v.* = {}; } + +test "compile time int to ptr of function" { + foobar(FUNCTION_CONSTANT); +} + +pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, @maxValue(usize)); +pub const PFN_void = extern fn (*c_void) void; + +fn foobar(func: PFN_void) void { + std.debug.assert(@ptrToInt(func) == @maxValue(usize)); +} diff --git a/test/cases/eval.zig b/test/cases/eval.zig index b8d80bda02..7e9c9a739d 100644 --- a/test/cases/eval.zig +++ b/test/cases/eval.zig @@ -275,7 +275,7 @@ test "eval @setFloatMode at compile-time" { } fn fnWithFloatMode() f32 { - @setFloatMode(this, builtin.FloatMode.Strict); + @setFloatMode(builtin.FloatMode.Strict); return 1234.0; } @@ -628,7 +628,7 @@ test "call method with comptime pass-by-non-copying-value self parameter" { const S = struct { a: u8, - fn b(comptime s: this) u8 { + fn b(comptime s: @This()) u8 { return s.a; } }; diff --git a/test/cases/misc.zig b/test/cases/misc.zig index 1c0189571b..4343cb3b90 100644 --- a/test/cases/misc.zig +++ b/test/cases/misc.zig @@ -510,9 +510,6 @@ test "@typeId" { assert(@typeId(AUnion) == Tid.Union); assert(@typeId(fn () void) == Tid.Fn); assert(@typeId(@typeOf(builtin)) == Tid.Namespace); - assert(@typeId(@typeOf(x: { - break :x this; - })) == Tid.Block); // TODO bound fn // TODO arg tuple // TODO opaque diff --git a/test/cases/reflection.zig b/test/cases/reflection.zig index 3d3af3c889..677fd90192 100644 --- a/test/cases/reflection.zig +++ b/test/cases/reflection.zig @@ -1,6 +1,6 @@ const assert = @import("std").debug.assert; const mem = @import("std").mem; -const reflection = this; +const reflection = @This(); test "reflection: array, pointer, optional, error union type child" { comptime { diff --git a/test/cases/struct.zig b/test/cases/struct.zig index 20d46999d5..163c69e670 100644 --- a/test/cases/struct.zig +++ b/test/cases/struct.zig @@ -423,10 +423,10 @@ fn alloc(comptime T: type) []T { test "call method with mutable reference to struct with no fields" { const S = struct { - fn doC(s: *const this) bool { + fn doC(s: *const @This()) bool { return true; } - fn do(s: *this) bool { + fn do(s: *@This()) bool { return true; } }; diff --git a/test/cases/this.zig b/test/cases/this.zig index ba51d0ac90..c7be074f36 100644 --- a/test/cases/this.zig +++ b/test/cases/this.zig @@ -1,10 +1,10 @@ const assert = @import("std").debug.assert; -const module = this; +const module = @This(); fn Point(comptime T: type) type { return struct { - const Self = this; + const Self = @This(); x: T, y: T, @@ -19,11 +19,6 @@ fn add(x: i32, y: i32) i32 { return x + y; } -fn factorial(x: i32) i32 { - const selfFn = this; - return if (x == 0) 1 else x * selfFn(x - 1); -} - test "this refer to module call private fn" { assert(module.add(1, 2) == 3); } @@ -37,7 +32,3 @@ test "this refer to container" { assert(pt.x == 13); assert(pt.y == 35); } - -test "this refer to fn" { - assert(factorial(5) == 120); -} diff --git a/test/cases/type_info.zig b/test/cases/type_info.zig index b8fc4cf14e..6f99268c08 100644 --- a/test/cases/type_info.zig +++ b/test/cases/type_info.zig @@ -166,7 +166,7 @@ fn testUnion() void { assert(TypeId(typeinfo_info) == TypeId.Union); assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto); assert(typeinfo_info.Union.tag_type.? == TypeId); - assert(typeinfo_info.Union.fields.len == 25); + assert(typeinfo_info.Union.fields.len == 24); assert(typeinfo_info.Union.fields[4].enum_field != null); assert(typeinfo_info.Union.fields[4].enum_field.?.value == 4); assert(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int)); @@ -217,7 +217,7 @@ fn testStruct() void { } const TestStruct = packed struct { - const Self = this; + const Self = @This(); fieldA: usize, fieldB: void, diff --git a/test/cases/union.zig b/test/cases/union.zig index 0c55fb92e7..c93e769c5e 100644 --- a/test/cases/union.zig +++ b/test/cases/union.zig @@ -324,3 +324,42 @@ test "tagged union with no payloads" { @TagType(UnionEnumNoPayloads).B => {}, } } + +test "union with only 1 field casted to its enum type" { + const Literal = union(enum) { + Number: f64, + Bool: bool, + }; + + const Expr = union(enum) { + Literal: Literal, + }; + + var e = Expr{ .Literal = Literal{ .Bool = true } }; + const Tag = @TagType(Expr); + comptime assert(@TagType(Tag) == comptime_int); + var t = Tag(e); + assert(t == Expr.Literal); +} + +test "union with only 1 field casted to its enum type which has enum value specified" { + const Literal = union(enum) { + Number: f64, + Bool: bool, + }; + + const Tag = enum { + Literal = 33, + }; + + const Expr = union(Tag) { + Literal: Literal, + }; + + var e = Expr{ .Literal = Literal{ .Bool = true } }; + comptime assert(@TagType(Tag) == comptime_int); + var t = Tag(e); + assert(t == Expr.Literal); + assert(@enumToInt(t) == 33); + comptime assert(@enumToInt(t) == 33); +} diff --git a/test/compile_errors.zig b/test/compile_errors.zig index dda45a5897..40afc6df2d 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,19 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( + "non error sets used in merge error sets operator", + \\export fn foo() void { + \\ const Errors = u8 || u16; + \\} + \\export fn bar() void { + \\ const Errors = error{} || u16; + \\} + , + ".tmp_source.zig:2:20: error: expected error set type, found 'u8'", + ".tmp_source.zig:5:31: error: expected error set type, found 'u16'", + ); + + cases.add( "variable initialization compile error then referenced", \\fn Undeclared() type { \\ return T; @@ -3431,7 +3444,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ \\export fn entry() usize { return @sizeOf(@typeOf(foo)); } , - ".tmp_source.zig:8:26: error: expected type '*const u3', found '*align(1:3:6) const u3'", + ".tmp_source.zig:8:26: error: expected type '*const u3', found '*align(:3:6) const u3'", ); cases.add( @@ -3800,11 +3813,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ return struct { \\ b: B(), \\ - \\ const Self = this; + \\ const Self = @This(); \\ \\ fn B() type { \\ return struct { - \\ const Self = this; + \\ const Self = @This(); \\ }; \\ } \\ }; @@ -3983,8 +3996,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( "@setFloatMode twice for same scope", \\export fn foo() void { - \\ @setFloatMode(this, @import("builtin").FloatMode.Optimized); - \\ @setFloatMode(this, @import("builtin").FloatMode.Optimized); + \\ @setFloatMode(@import("builtin").FloatMode.Optimized); + \\ @setFloatMode(@import("builtin").FloatMode.Optimized); \\} , ".tmp_source.zig:3:5: error: float mode set twice for same scope", @@ -4301,12 +4314,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ var a = undefined; \\ var b = 1; \\ var c = 1.0; - \\ var d = this; - \\ var e = null; - \\ var f = opaque.*; - \\ var g = i32; - \\ var h = @import("std",); - \\ var i = (Foo {}).bar; + \\ var d = null; + \\ var e = opaque.*; + \\ var f = i32; + \\ var g = @import("std",); + \\ var h = (Foo {}).bar; \\ \\ var z: noreturn = return; \\} @@ -4319,13 +4331,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ".tmp_source.zig:7:4: error: variable of type '(undefined)' must be const or comptime", ".tmp_source.zig:8:4: error: variable of type 'comptime_int' must be const or comptime", ".tmp_source.zig:9:4: error: variable of type 'comptime_float' must be const or comptime", - ".tmp_source.zig:10:4: error: variable of type '(block)' must be const or comptime", - ".tmp_source.zig:11:4: error: variable of type '(null)' must be const or comptime", - ".tmp_source.zig:12:4: error: variable of type 'Opaque' not allowed", - ".tmp_source.zig:13:4: error: variable of type 'type' must be const or comptime", - ".tmp_source.zig:14:4: error: variable of type '(namespace)' must be const or comptime", - ".tmp_source.zig:15:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime", - ".tmp_source.zig:17:4: error: unreachable code", + ".tmp_source.zig:10:4: error: variable of type '(null)' must be const or comptime", + ".tmp_source.zig:11:4: error: variable of type 'Opaque' not allowed", + ".tmp_source.zig:12:4: error: variable of type 'type' must be const or comptime", + ".tmp_source.zig:13:4: error: variable of type '(namespace)' must be const or comptime", + ".tmp_source.zig:14:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime", + ".tmp_source.zig:16:4: error: unreachable code", ); cases.add( diff --git a/test/standalone/brace_expansion/main.zig b/test/standalone/brace_expansion/main.zig index ccb4f6dd45..36b4f8bd4f 100644 --- a/test/standalone/brace_expansion/main.zig +++ b/test/standalone/brace_expansion/main.zig @@ -191,7 +191,7 @@ pub fn main() !void { var stdin_buf = try Buffer.initSize(global_allocator, 0); defer stdin_buf.deinit(); - var stdin_adapter = io.FileInStream.init(&stdin_file); + var stdin_adapter = io.FileInStream.init(stdin_file); try stdin_adapter.stream.readAllBuffer(&stdin_buf, @maxValue(usize)); var result_buf = try Buffer.initSize(global_allocator, 0); |
