zig/test/behavior/bitcast.zig
Andrew Kelley 069c83d58c stage2: change @bitCast to always be by-value
After a discussion about language specs, this seems like the best way to
go, because it's simpler to reason about both for humans and compilers.

The `bitcast_result_ptr` ZIR instruction is no longer needed.

This commit also implements writing enums, arrays, and vectors to
virtual memory at compile-time.

This unlocked some more of compiler-rt being able to build, which
in turn unlocks saturating arithmetic behavior tests.

There was also a memory leak in the comptime closure system which is now
fixed.
2021-10-22 15:35:35 -07:00

67 lines
1.5 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const maxInt = std.math.maxInt;
const native_endian = builtin.target.cpu.arch.endian();
test "@bitCast i32 -> u32" {
try testBitCast_i32_u32();
comptime try testBitCast_i32_u32();
}
fn testBitCast_i32_u32() !void {
try expect(conv(-1) == maxInt(u32));
try expect(conv2(maxInt(u32)) == -1);
}
fn conv(x: i32) u32 {
return @bitCast(u32, x);
}
fn conv2(x: u32) i32 {
return @bitCast(i32, x);
}
test "bitcast result to _" {
_ = @bitCast(u8, @as(i8, 1));
}
test "nested bitcast" {
const S = struct {
fn moo(x: isize) !void {
try expect(@intCast(isize, 42) == x);
}
fn foo(x: isize) !void {
try @This().moo(
@bitCast(isize, if (x != 0) @bitCast(usize, x) else @bitCast(usize, x)),
);
}
};
try S.foo(42);
comptime try S.foo(42);
}
test "@bitCast enum to its integer type" {
const SOCK = enum(c_int) {
A,
B,
fn testBitCastExternEnum() !void {
var SOCK_DGRAM = @This().B;
var sock_dgram = @bitCast(c_int, SOCK_DGRAM);
try expect(sock_dgram == 1);
}
};
try SOCK.testBitCastExternEnum();
comptime try SOCK.testBitCastExternEnum();
}
// issue #3010: compiler segfault
test "bitcast literal [4]u8 param to u32" {
const ip = @bitCast(u32, [_]u8{ 255, 255, 255, 255 });
try expect(ip == maxInt(u32));
}