2017-01-05 08:57:48 +00:00
|
|
|
const assert = @import("std").debug.assert;
|
2016-12-22 06:42:30 +00:00
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "intToPtrCast" {
|
2017-01-05 08:57:48 +00:00
|
|
|
const x = isize(13);
|
|
|
|
const y = (&u8)(x);
|
|
|
|
const z = usize(y);
|
|
|
|
assert(z == 13);
|
2016-12-22 06:42:30 +00:00
|
|
|
}
|
2017-01-31 18:38:04 +00:00
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "numLitIntToPtrCast" {
|
2017-01-31 18:38:04 +00:00
|
|
|
const vga_mem = (&u16)(0xB8000);
|
|
|
|
assert(usize(vga_mem) == 0xB8000);
|
|
|
|
}
|
2017-03-15 01:38:27 +00:00
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "pointerReinterpretConstFloatToInt" {
|
2017-03-15 01:38:27 +00:00
|
|
|
const float: f64 = 5.99999999999994648725e-01;
|
|
|
|
const float_ptr = &float;
|
2017-03-31 11:18:20 +01:00
|
|
|
const int_ptr = @ptrcast(&i32, float_ptr);
|
2017-03-15 01:38:27 +00:00
|
|
|
const int_val = *int_ptr;
|
|
|
|
assert(int_val == 858993411);
|
|
|
|
}
|
2017-03-31 10:48:15 +01:00
|
|
|
|
|
|
|
test "implicitly cast a pointer to a const pointer of it" {
|
|
|
|
var x: i32 = 1;
|
|
|
|
const xp = &x;
|
|
|
|
funcWithConstPtrPtr(xp);
|
|
|
|
assert(x == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn funcWithConstPtrPtr(x: &const &i32) {
|
|
|
|
**x += 1;
|
|
|
|
}
|
2017-04-02 23:19:59 +01:00
|
|
|
|
|
|
|
error ItBroke;
|
|
|
|
test "explicit cast from integer to error type" {
|
|
|
|
testCastIntToErr(error.ItBroke);
|
|
|
|
comptime testCastIntToErr(error.ItBroke);
|
|
|
|
}
|
|
|
|
fn testCastIntToErr(err: error) {
|
|
|
|
const x = usize(err);
|
|
|
|
const y = error(x);
|
|
|
|
assert(error.ItBroke == y);
|
|
|
|
}
|