diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-09-19 00:59:04 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-09-19 00:59:04 -0400 |
| commit | 380c8ec2c95fa8d732c141c705d9940629eb2012 (patch) | |
| tree | 6f4139367553fea653662d1fb65bd23421bad77a /test | |
| parent | 76f53960778e84ab49730edb77b85490b07fbea2 (diff) | |
| download | zig-380c8ec2c95fa8d732c141c705d9940629eb2012.tar.gz zig-380c8ec2c95fa8d732c141c705d9940629eb2012.zip | |
implement runtime `@byteSwap` and other fixups
* update docs for `@byteSwap`.
* fix hash & eql functions for ZigLLVMFnIdBswap not updated to
include vector len. this was causing incorrect bswap function
being called in unrelated code
* fix `@byteSwap` behavior tests only testing comptime and not
runtime operations
* implement runtime `@byteSwap`
* fix incorrect logic in ir_render_vector_to_array and
ir_render_array_to_vector with regards to whether or not to bitcast
* `@byteSwap` accepts an array operand which it will cast to vector
* simplify `@byteSwap` semantic analysis code and various fixes
Diffstat (limited to 'test')
| -rw-r--r-- | test/stage1/behavior/byteswap.zig | 89 |
1 files changed, 54 insertions, 35 deletions
diff --git a/test/stage1/behavior/byteswap.zig b/test/stage1/behavior/byteswap.zig index 249db155b7..d8fc554808 100644 --- a/test/stage1/behavior/byteswap.zig +++ b/test/stage1/behavior/byteswap.zig @@ -1,43 +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); -test "@byteSwap on vectors" { - comptime testVectorByteSwap(); - testVectorByteSwap(); + 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); - - 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))); -} +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 }); + } -fn testVectorByteSwap() void { - expect((@byteSwap(u8, @Vector(2, u8)([2]u8{0x12, 0x13})) == @Vector(2, u8)([2]u8{0x12, 0x13})).all); - expect((@byteSwap(u16, @Vector(2, u16)([2]u16{0x1234, 0x2345})) == @Vector(2, u16)([2]u16{0x3412, 0x4523})).all); - expect((@byteSwap(u24, @Vector(2, u24)([2]u24{0x123456, 0x234567})) == @Vector(2, u24)([2]u24{0x563412, 0x674523})).all); + 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(); } |
