1const builtin = @import("builtin");
2const std = @import("std");
3const expect = std.testing.expect;
4const expectEqual = std.testing.expectEqual;
5const maxInt = std.math.maxInt;
6const minInt = std.math.minInt;
7
8test "@intCast i32 to u7" {
9 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
11 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
12
13 var x: u128 = maxInt(u128);
14 var y: i32 = 120;
15 _ = .{ &x, &y };
16 const z = x >> @as(u7, @intCast(y));
17 try expect(z == 0xff);
18}
19
20test "coerce i8 to i32 and @intCast back" {
21 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
22 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
23
24 var x: i8 = -5;
25 var y: i32 = -5;
26 _ = .{ &x, &y };
27 try expect(y == x);
28
29 var x2: i32 = -5;
30 var y2: i8 = -5;
31 _ = .{ &x2, &y2 };
32 try expect(y2 == @as(i8, @intCast(x2)));
33}
34
35test "coerce non byte-sized integers accross 32bits boundary" {
36 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO
37
38 {
39 var v: u21 = 6417;
40 _ = &v;
41 const a: u32 = v;
42 const b: u64 = v;
43 const c: u64 = a;
44 var w: u64 = 0x1234567812345678;
45 _ = &w;
46 const d: u21 = @truncate(w);
47 const e: u60 = d;
48 try expectEqual(@as(u32, 6417), a);
49 try expectEqual(@as(u64, 6417), b);
50 try expectEqual(@as(u64, 6417), c);
51 try expectEqual(@as(u21, 0x145678), d);
52 try expectEqual(@as(u60, 0x145678), e);
53 }
54
55 {
56 var v: u10 = 234;
57 _ = &v;
58 const a: u32 = v;
59 const b: u64 = v;
60 const c: u64 = a;
61 var w: u64 = 0x1234567812345678;
62 _ = &w;
63 const d: u10 = @truncate(w);
64 const e: u60 = d;
65 try expectEqual(@as(u32, 234), a);
66 try expectEqual(@as(u64, 234), b);
67 try expectEqual(@as(u64, 234), c);
68 try expectEqual(@as(u21, 0x278), d);
69 try expectEqual(@as(u60, 0x278), e);
70 }
71 {
72 var v: u7 = 11;
73 _ = &v;
74 const a: u32 = v;
75 const b: u64 = v;
76 const c: u64 = a;
77 var w: u64 = 0x1234567812345678;
78 _ = &w;
79 const d: u7 = @truncate(w);
80 const e: u60 = d;
81 try expectEqual(@as(u32, 11), a);
82 try expectEqual(@as(u64, 11), b);
83 try expectEqual(@as(u64, 11), c);
84 try expectEqual(@as(u21, 0x78), d);
85 try expectEqual(@as(u60, 0x78), e);
86 }
87
88 {
89 var v: i21 = -6417;
90 _ = &v;
91 const a: i32 = v;
92 const b: i64 = v;
93 const c: i64 = a;
94 var w: i64 = -12345;
95 _ = &w;
96 const d: i21 = @intCast(w);
97 const e: i60 = d;
98 try expectEqual(@as(i32, -6417), a);
99 try expectEqual(@as(i64, -6417), b);
100 try expectEqual(@as(i64, -6417), c);
101 try expectEqual(@as(i21, -12345), d);
102 try expectEqual(@as(i60, -12345), e);
103 }
104
105 {
106 var v: i10 = -234;
107 _ = &v;
108 const a: i32 = v;
109 const b: i64 = v;
110 const c: i64 = a;
111 var w: i64 = -456;
112 _ = &w;
113 const d: i10 = @intCast(w);
114 const e: i60 = d;
115 try expectEqual(@as(i32, -234), a);
116 try expectEqual(@as(i64, -234), b);
117 try expectEqual(@as(i64, -234), c);
118 try expectEqual(@as(i10, -456), d);
119 try expectEqual(@as(i60, -456), e);
120 }
121 {
122 var v: i7 = -11;
123 _ = &v;
124 const a: i32 = v;
125 const b: i64 = v;
126 const c: i64 = a;
127 var w: i64 = -42;
128 _ = &w;
129 const d: i7 = @intCast(w);
130 const e: i60 = d;
131 try expectEqual(@as(i32, -11), a);
132 try expectEqual(@as(i64, -11), b);
133 try expectEqual(@as(i64, -11), c);
134 try expectEqual(@as(i7, -42), d);
135 try expectEqual(@as(i60, -42), e);
136 }
137}
138
139fn testIntCast(comptime S: type, a: S, comptime D: type, expected: D) !void {
140 const actual: D = @intCast(a);
141 try expect(actual == expected);
142}
143
144test "@intCast <= 64 bits" {
145 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
146 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
147 try testIntCast(i32, minInt(i32), i64, minInt(i32));
148 try testIntCast(i32, maxInt(i32), i64, maxInt(i32));
149 try testIntCast(u32, maxInt(u32), u64, maxInt(u32));
150 try testIntCast(u32, maxInt(i32), i64, maxInt(i32));
151 try testIntCast(u32, maxInt(u32), i64, maxInt(u32));
152
153 try testIntCast(i32, 0, u32, 0);
154 try testIntCast(i32, maxInt(i32), u32, maxInt(i32));
155 try testIntCast(i64, 0, u64, 0);
156 try testIntCast(i64, maxInt(i64), u64, maxInt(i64));
157
158 try testIntCast(u32, 0, i32, 0);
159 try testIntCast(u32, maxInt(i32), i32, maxInt(i32));
160 try testIntCast(u64, 0, i64, 0);
161 try testIntCast(u64, maxInt(i64), i64, maxInt(i64));
162
163 try testIntCast(i64, minInt(i32), i32, minInt(i32));
164 try testIntCast(i64, maxInt(i32), i32, maxInt(i32));
165 try testIntCast(u64, maxInt(u32), u32, maxInt(u32));
166 try testIntCast(i64, maxInt(u32), u32, maxInt(u32));
167}
168
169test "@intCast > 128 bits" {
170 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
171 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
172
173 try testIntCast(u8, 123, u140, 123);
174 try testIntCast(u64, 1 << 63, u140, 1 << 63);
175 try testIntCast(u127, maxInt(u127), u140, maxInt(u127));
176 try testIntCast(i8, -42, i140, -42);
177 try testIntCast(i64, minInt(i64), i140, minInt(i64));
178 try testIntCast(i127, maxInt(i127), i140, maxInt(i127));
179 try testIntCast(u127, 1 << 100, i140, 1 << 100);
180 try testIntCast(i127, 1 << 100, u140, 1 << 100);
181
182 try testIntCast(u140, 0, u128, 0);
183 try testIntCast(u140, 1 << 100, u128, 1 << 100);
184 try testIntCast(u140, maxInt(u128), u128, maxInt(u128));
185 try testIntCast(i140, -1, i128, -1);
186 try testIntCast(i140, minInt(i128), i128, minInt(i128));
187 try testIntCast(i140, maxInt(i128), i128, maxInt(i128));
188 try testIntCast(u140, 1 << 100, i128, 1 << 100);
189 try testIntCast(i140, 1 << 100, u128, 1 << 100);
190
191 try testIntCast(u16, 255, u256, 255);
192 try testIntCast(u128, 1 << 127, u256, 1 << 127);
193 try testIntCast(i16, -7, i256, -7);
194 try testIntCast(i128, minInt(i128), i256, minInt(i128));
195 try testIntCast(u128, maxInt(i128), i256, maxInt(i128));
196 try testIntCast(i128, 1 << 100, u256, 1 << 100);
197
198 try testIntCast(u256, 1 << 139, u140, 1 << 139);
199 try testIntCast(u256, maxInt(u140), u140, maxInt(u140));
200 try testIntCast(i256, -1, i140, -1);
201 try testIntCast(i256, minInt(i140), i140, minInt(i140));
202 try testIntCast(i256, maxInt(i140), i140, maxInt(i140));
203 try testIntCast(u256, 1 << 120, i140, 1 << 120);
204 try testIntCast(i256, 1 << 120, u140, 1 << 120);
205
206 try testIntCast(i257, maxInt(i256), i256, maxInt(i256));
207 try testIntCast(i257, minInt(i256), i256, minInt(i256));
208 try testIntCast(u257, maxInt(u256), u256, maxInt(u256));
209 try testIntCast(u257, 1 << 255, u256, 1 << 255);
210
211 try testIntCast(u32, maxInt(u32), i255, maxInt(u32));
212 try testIntCast(u64, maxInt(u64), i255, maxInt(u64));
213 try testIntCast(u128, maxInt(u128), i255, maxInt(u128));
214}