diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-09-19 17:02:32 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-09-19 17:02:32 -0400 |
| commit | 8a30edcde82dfcd36c3eec2fb7bcd5af549325cf (patch) | |
| tree | 9d45053a816d85e72daee5c168a20259be585871 /test/stage1 | |
| parent | 5e34fb35972b8ff2ec0a420779b689229d05c659 (diff) | |
| parent | 925ffbce7f424548be9eb42eb3914d5035066003 (diff) | |
| download | zig-8a30edcde82dfcd36c3eec2fb7bcd5af549325cf.tar.gz zig-8a30edcde82dfcd36c3eec2fb7bcd5af549325cf.zip | |
Merge remote-tracking branch 'origin/master' into llvm9
Diffstat (limited to 'test/stage1')
| -rw-r--r-- | test/stage1/behavior.zig | 1 | ||||
| -rw-r--r-- | test/stage1/behavior/byteswap.zig | 80 | ||||
| -rw-r--r-- | test/stage1/behavior/misc.zig | 20 | ||||
| -rw-r--r-- | test/stage1/behavior/shuffle.zig | 57 | ||||
| -rw-r--r-- | test/stage1/behavior/vector.zig | 75 |
5 files changed, 208 insertions, 25 deletions
diff --git a/test/stage1/behavior.zig b/test/stage1/behavior.zig index db6cdad3b1..e56fc7ba7f 100644 --- a/test/stage1/behavior.zig +++ b/test/stage1/behavior.zig @@ -80,6 +80,7 @@ comptime { _ = @import("behavior/pub_enum.zig"); _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig"); _ = @import("behavior/reflection.zig"); + _ = @import("behavior/shuffle.zig"); _ = @import("behavior/sizeof_and_typeof.zig"); _ = @import("behavior/slice.zig"); _ = @import("behavior/slicetobytes.zig"); diff --git a/test/stage1/behavior/byteswap.zig b/test/stage1/behavior/byteswap.zig index 3e7c34cb85..d8fc554808 100644 --- a/test/stage1/behavior/byteswap.zig +++ b/test/stage1/behavior/byteswap.zig @@ -1,32 +1,62 @@ const std = @import("std"); const expect = std.testing.expect; -test "@byteSwap" { - comptime testByteSwap(); - testByteSwap(); +test "@byteSwap integers" { + const ByteSwapIntTest = struct { + fn run() void { + t(u0, 0, 0); + t(u8, 0x12, 0x12); + t(u16, 0x1234, 0x3412); + t(u24, 0x123456, 0x563412); + t(u32, 0x12345678, 0x78563412); + t(u40, 0x123456789a, 0x9a78563412); + t(i48, 0x123456789abc, @bitCast(i48, u48(0xbc9a78563412))); + t(u56, 0x123456789abcde, 0xdebc9a78563412); + t(u64, 0x123456789abcdef1, 0xf1debc9a78563412); + t(u128, 0x123456789abcdef11121314151617181, 0x8171615141312111f1debc9a78563412); + + t(u0, u0(0), 0); + t(i8, i8(-50), -50); + t(i16, @bitCast(i16, u16(0x1234)), @bitCast(i16, u16(0x3412))); + t(i24, @bitCast(i24, u24(0x123456)), @bitCast(i24, u24(0x563412))); + t(i32, @bitCast(i32, u32(0x12345678)), @bitCast(i32, u32(0x78563412))); + t(u40, @bitCast(i40, u40(0x123456789a)), u40(0x9a78563412)); + t(i48, @bitCast(i48, u48(0x123456789abc)), @bitCast(i48, u48(0xbc9a78563412))); + t(i56, @bitCast(i56, u56(0x123456789abcde)), @bitCast(i56, u56(0xdebc9a78563412))); + t(i64, @bitCast(i64, u64(0x123456789abcdef1)), @bitCast(i64, u64(0xf1debc9a78563412))); + t( + i128, + @bitCast(i128, u128(0x123456789abcdef11121314151617181)), + @bitCast(i128, u128(0x8171615141312111f1debc9a78563412)), + ); + } + fn t(comptime I: type, input: I, expected_output: I) void { + std.testing.expectEqual(expected_output, @byteSwap(I, input)); + } + }; + comptime ByteSwapIntTest.run(); + ByteSwapIntTest.run(); } -fn testByteSwap() void { - expect(@byteSwap(u0, 0) == 0); - expect(@byteSwap(u8, 0x12) == 0x12); - expect(@byteSwap(u16, 0x1234) == 0x3412); - expect(@byteSwap(u24, 0x123456) == 0x563412); - expect(@byteSwap(u32, 0x12345678) == 0x78563412); - expect(@byteSwap(u40, 0x123456789a) == 0x9a78563412); - expect(@byteSwap(i48, 0x123456789abc) == @bitCast(i48, u48(0xbc9a78563412))); - expect(@byteSwap(u56, 0x123456789abcde) == 0xdebc9a78563412); - expect(@byteSwap(u64, 0x123456789abcdef1) == 0xf1debc9a78563412); - expect(@byteSwap(u128, 0x123456789abcdef11121314151617181) == 0x8171615141312111f1debc9a78563412); +test "@byteSwap vectors" { + const ByteSwapVectorTest = struct { + fn run() void { + t(u8, 2, [_]u8{ 0x12, 0x13 }, [_]u8{ 0x12, 0x13 }); + t(u16, 2, [_]u16{ 0x1234, 0x2345 }, [_]u16{ 0x3412, 0x4523 }); + t(u24, 2, [_]u24{ 0x123456, 0x234567 }, [_]u24{ 0x563412, 0x674523 }); + } - expect(@byteSwap(u0, u0(0)) == 0); - expect(@byteSwap(i8, i8(-50)) == -50); - expect(@byteSwap(i16, @bitCast(i16, u16(0x1234))) == @bitCast(i16, u16(0x3412))); - expect(@byteSwap(i24, @bitCast(i24, u24(0x123456))) == @bitCast(i24, u24(0x563412))); - expect(@byteSwap(i32, @bitCast(i32, u32(0x12345678))) == @bitCast(i32, u32(0x78563412))); - expect(@byteSwap(u40, @bitCast(i40, u40(0x123456789a))) == u40(0x9a78563412)); - expect(@byteSwap(i48, @bitCast(i48, u48(0x123456789abc))) == @bitCast(i48, u48(0xbc9a78563412))); - expect(@byteSwap(i56, @bitCast(i56, u56(0x123456789abcde))) == @bitCast(i56, u56(0xdebc9a78563412))); - expect(@byteSwap(i64, @bitCast(i64, u64(0x123456789abcdef1))) == @bitCast(i64, u64(0xf1debc9a78563412))); - expect(@byteSwap(i128, @bitCast(i128, u128(0x123456789abcdef11121314151617181))) == - @bitCast(i128, u128(0x8171615141312111f1debc9a78563412))); + fn t( + comptime I: type, + comptime n: comptime_int, + input: @Vector(n, I), + expected_vector: @Vector(n, I), + ) void { + const actual_output: [n]I = @byteSwap(I, input); + const expected_output: [n]I = expected_vector; + std.testing.expectEqual(expected_output, actual_output); + } + }; + comptime ByteSwapVectorTest.run(); + ByteSwapVectorTest.run(); } diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index c122b18e0a..613bb9ac54 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -721,3 +721,23 @@ test "global variable assignment with optional unwrapping with var initialized t }; expect(global_foo.* == 1234); } + +test "peer result location with typed parent, runtime condition, comptime prongs" { + const S = struct { + fn doTheTest(arg: i32) i32 { + const st = Structy{ + .bleh = if (arg == 1) 1 else 1, + }; + + if (st.bleh == 1) + return 1234; + return 0; + } + + const Structy = struct { + bleh: i32, + }; + }; + expect(S.doTheTest(0) == 1234); + expect(S.doTheTest(1) == 1234); +} diff --git a/test/stage1/behavior/shuffle.zig b/test/stage1/behavior/shuffle.zig new file mode 100644 index 0000000000..2029ec582f --- /dev/null +++ b/test/stage1/behavior/shuffle.zig @@ -0,0 +1,57 @@ +const std = @import("std"); +const mem = std.mem; +const expect = std.testing.expect; + +test "@shuffle" { + const S = struct { + fn doTheTest() void { + var v: @Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 }; + var x: @Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 }; + const mask: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 3, ~i32(3) }; + var res = @shuffle(i32, v, x, mask); + expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 })); + + // Implicit cast from array (of mask) + res = @shuffle(i32, v, x, [4]i32{ 0, ~i32(2), 3, ~i32(3) }); + expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 })); + + // Undefined + const mask2: @Vector(4, i32) = [4]i32{ 3, 1, 2, 0 }; + res = @shuffle(i32, v, undefined, mask2); + expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 40, -2, 30, 2147483647 })); + + // Upcasting of b + var v2: @Vector(2, i32) = [2]i32{ 2147483647, undefined }; + const mask3: @Vector(4, i32) = [4]i32{ ~i32(0), 2, ~i32(0), 3 }; + res = @shuffle(i32, x, v2, mask3); + expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 2147483647, 4 })); + + // Upcasting of a + var v3: @Vector(2, i32) = [2]i32{ 2147483647, -2 }; + const mask4: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 1, ~i32(3) }; + res = @shuffle(i32, v3, x, mask4); + expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, -2, 4 })); + + // bool + { + var x2: @Vector(4, bool) = [4]bool{ false, true, false, true }; + var v4: @Vector(2, bool) = [2]bool{ true, false }; + const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2 }; + var res2 = @shuffle(bool, x2, v4, mask5); + expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false })); + } + + // TODO re-enable when LLVM codegen is fixed + // https://github.com/ziglang/zig/issues/3246 + if (false) { + var x2: @Vector(3, bool) = [3]bool{ false, true, false }; + var v4: @Vector(2, bool) = [2]bool{ true, false }; + const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2 }; + var res2 = @shuffle(bool, x2, v4, mask5); + expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false })); + } + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} diff --git a/test/stage1/behavior/vector.zig b/test/stage1/behavior/vector.zig index 431e3fe272..d3a771fca8 100644 --- a/test/stage1/behavior/vector.zig +++ b/test/stage1/behavior/vector.zig @@ -2,6 +2,18 @@ const std = @import("std"); const mem = std.mem; const expect = std.testing.expect; +test "implicit cast vector to array - bool" { + const S = struct { + fn doTheTest() void { + const a: @Vector(4, bool) = [_]bool{ true, false, true, false }; + const result_array: [4]bool = a; + expect(mem.eql(bool, result_array, [4]bool{ true, false, true, false })); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} + test "vector wrap operators" { const S = struct { fn doTheTest() void { @@ -18,6 +30,23 @@ test "vector wrap operators" { comptime S.doTheTest(); } +test "vector bin compares with mem.eql" { + const S = struct { + fn doTheTest() void { + var v: @Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 }; + var x: @Vector(4, i32) = [4]i32{ 1, 2147483647, 30, 4 }; + expect(mem.eql(bool, ([4]bool)(v == x), [4]bool{ false, false, true, false })); + expect(mem.eql(bool, ([4]bool)(v != x), [4]bool{ true, true, false, true })); + expect(mem.eql(bool, ([4]bool)(v < x), [4]bool{ false, true, false, false })); + expect(mem.eql(bool, ([4]bool)(v > x), [4]bool{ true, false, false, true })); + expect(mem.eql(bool, ([4]bool)(v <= x), [4]bool{ false, true, true, false })); + expect(mem.eql(bool, ([4]bool)(v >= x), [4]bool{ true, false, true, true })); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} + test "vector int operators" { const S = struct { fn doTheTest() void { @@ -80,3 +109,49 @@ test "array to vector" { var arr = [4]f32{ foo, 1.5, 0.0, 0.0 }; var vec: @Vector(4, f32) = arr; } + +test "vector casts of sizes not divisable by 8" { + const S = struct { + fn doTheTest() void { + { + var v: @Vector(4, u3) = [4]u3{ 5, 2, 3, 0 }; + var x: [4]u3 = v; + expect(mem.eql(u3, x, ([4]u3)(v))); + } + { + var v: @Vector(4, u2) = [4]u2{ 1, 2, 3, 0 }; + var x: [4]u2 = v; + expect(mem.eql(u2, x, ([4]u2)(v))); + } + { + var v: @Vector(4, u1) = [4]u1{ 1, 0, 1, 0 }; + var x: [4]u1 = v; + expect(mem.eql(u1, x, ([4]u1)(v))); + } + { + var v: @Vector(4, bool) = [4]bool{ false, false, true, false }; + var x: [4]bool = v; + expect(mem.eql(bool, x, ([4]bool)(v))); + } + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} + +test "vector @splat" { + const S = struct { + fn doTheTest() void { + var v: u32 = 5; + var x = @splat(4, v); + expect(@typeOf(x) == @Vector(4, u32)); + var array_x: [4]u32 = x; + expect(array_x[0] == 5); + expect(array_x[1] == 5); + expect(array_x[2] == 5); + expect(array_x[3] == 5); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} |
