zig/test/behavior/widening.zig
Andrew Kelley aecebf38ac stage2: progress towards ability to compile compiler-rt
* prepare compiler-rt to support being compiled by stage2
   - put in a few minor workarounds that will be removed later, such as
     using `builtin.stage2_arch` rather than `builtin.cpu.arch`.
   - only try to export a few symbols for now - we'll move more symbols
     over to the "working in stage2" section as they become functional
     and gain test coverage.
   - use `inline fn` at function declarations rather than `@call` with an
     always_inline modifier at the callsites, to avoid depending on the
     anonymous array literal syntax language feature (for now).
 * AIR: replace floatcast instruction with fptrunc and fpext for
   shortening and widening floating point values, respectively.
 * Introduce a new ZIR instruction, `export_value`, which implements
   `@export` for the case when the thing to be exported is a local
   comptime value that points to a function.
   - AstGen: fix `@export` not properly reporting ambiguous decl
     references.
 * Sema: handle ExportOptions linkage. The value is now available to all
   backends.
   - Implement setting global linkage as appropriate in the LLVM
     backend. I did not yet inspect the LLVM IR, so this still needs to
     be audited. There is already a pending task to make sure the alias
     stuff is working as intended, and this is related.
   - Sema almost handles section, just a tiny bit more code is needed in
     `resolveExportOptions`.
 * Sema: implement float widening and shortening for both `@floatCast`
   and float coercion.
   - Implement the LLVM backend code for this as well.
2021-09-21 23:21:07 -07:00

63 lines
1.8 KiB
Zig

const std = @import("std");
const expect = std.testing.expect;
const mem = std.mem;
test "integer widening" {
var a: u8 = 250;
var b: u16 = a;
var c: u32 = b;
var d: u64 = c;
var e: u64 = d;
var f: u128 = e;
try expect(f == a);
}
test "implicit unsigned integer to signed integer" {
var a: u8 = 250;
var b: i16 = a;
try expect(b == 250);
}
test "float widening" {
if (@import("builtin").zig_is_stage2) {
// This test is passing but it depends on compiler-rt symbols, which
// cannot yet be built with stage2 due to
// "TODO implement equality comparison between a union's tag value and an enum literal"
return error.SkipZigTest;
}
var a: f16 = 12.34;
var b: f32 = a;
var c: f64 = b;
var d: f128 = c;
try expect(a == b);
try expect(b == c);
try expect(c == d);
}
test "float widening f16 to f128" {
if (@import("builtin").zig_is_stage2) {
// This test is passing but it depends on compiler-rt symbols, which
// cannot yet be built with stage2 due to
// "TODO implement equality comparison between a union's tag value and an enum literal"
return error.SkipZigTest;
}
// TODO https://github.com/ziglang/zig/issues/3282
if (@import("builtin").stage2_arch == .aarch64) return error.SkipZigTest;
if (@import("builtin").stage2_arch == .powerpc64le) return error.SkipZigTest;
var x: f16 = 12.34;
var y: f128 = x;
try expect(x == y);
}
test "cast small unsigned to larger signed" {
try expect(castSmallUnsignedToLargerSigned1(200) == @as(i16, 200));
try expect(castSmallUnsignedToLargerSigned2(9999) == @as(i64, 9999));
}
fn castSmallUnsignedToLargerSigned1(x: u8) i16 {
return x;
}
fn castSmallUnsignedToLargerSigned2(x: u16) i64 {
return x;
}