blob: 81a916f80c67f93671bade32ec8b3f33de0dbf28 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
const expect = std.testing.expect;
test "truncate u0 to larger integer allowed and has comptime-known result" {
var x: u0 = 0;
_ = &x;
const y = @as(u8, @truncate(x));
comptime assert(y == 0);
}
test "truncate.u0.literal" {
const z: u0 = @truncate(0);
try expect(z == 0);
}
test "truncate.u0.const" {
const c0: usize = 0;
const z: u0 = @truncate(c0);
try expect(z == 0);
}
test "truncate.u0.var" {
var d: u8 = 2;
_ = &d;
const z: u0 = @truncate(d);
try expect(z == 0);
}
test "truncate i0 to larger integer allowed and has comptime-known result" {
var x: i0 = 0;
_ = &x;
const y: i8 = @truncate(x);
comptime assert(y == 0);
}
test "truncate.i0.literal" {
const z: i0 = @truncate(0);
try expect(z == 0);
}
test "truncate.i0.const" {
const c0: isize = 0;
const z: i0 = @truncate(c0);
try expect(z == 0);
}
test "truncate.i0.var" {
var d: i8 = 2;
_ = &d;
const z: i0 = @truncate(d);
try expect(z == 0);
}
test "truncate on comptime integer" {
const x: u16 = @truncate(9999);
try expect(x == 9999);
const y: u16 = @truncate(-21555);
try expect(y == 0xabcd);
const z: i16 = @truncate(-65537);
try expect(z == -1);
const w: u1 = @truncate(1 << 100);
try expect(w == 0);
}
test "truncate on vectors" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
fn doTheTest() !void {
var v1: @Vector(4, u16) = .{ 0xaabb, 0xccdd, 0xeeff, 0x1122 };
_ = &v1;
const v2: @Vector(4, u8) = @truncate(v1);
try expect(std.mem.eql(u8, &@as([4]u8, v2), &[4]u8{ 0xbb, 0xdd, 0xff, 0x22 }));
}
};
try comptime S.doTheTest();
try S.doTheTest();
}
|