2017-01-05 00:57:48 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
2017-04-06 15:07:38 -07:00
|
|
|
const mem = @import("std").mem;
|
2016-12-21 22:42:30 -08:00
|
|
|
|
2017-04-07 12:34:13 -07:00
|
|
|
test "int to ptr cast" {
|
|
|
|
const x = usize(13);
|
|
|
|
const y = @intToPtr(&u8, x);
|
2017-01-05 00:57:48 -08:00
|
|
|
const z = usize(y);
|
|
|
|
assert(z == 13);
|
2016-12-21 22:42:30 -08:00
|
|
|
}
|
2017-01-31 10:38:04 -08:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "numLitIntToPtrCast" {
|
2017-04-07 12:34:13 -07:00
|
|
|
const vga_mem = @intToPtr(&u16, 0xB8000);
|
2017-01-31 10:38:04 -08:00
|
|
|
assert(usize(vga_mem) == 0xB8000);
|
|
|
|
}
|
2017-03-14 18:38:27 -07:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "pointerReinterpretConstFloatToInt" {
|
2017-03-14 18:38:27 -07:00
|
|
|
const float: f64 = 5.99999999999994648725e-01;
|
|
|
|
const float_ptr = &float;
|
2017-04-21 07:39:13 -07:00
|
|
|
const int_ptr = @ptrCast(&i32, float_ptr);
|
2017-03-14 18:38:27 -07:00
|
|
|
const int_val = *int_ptr;
|
|
|
|
assert(int_val == 858993411);
|
|
|
|
}
|
2017-03-31 02:48:15 -07: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 15:19:59 -07: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);
|
|
|
|
}
|
2017-04-06 15:07:38 -07:00
|
|
|
|
|
|
|
test "peer resolve arrays of different size to const slice" {
|
|
|
|
assert(mem.eql(u8, boolToStr(true), "true"));
|
|
|
|
assert(mem.eql(u8, boolToStr(false), "false"));
|
|
|
|
comptime assert(mem.eql(u8, boolToStr(true), "true"));
|
|
|
|
comptime assert(mem.eql(u8, boolToStr(false), "false"));
|
|
|
|
}
|
|
|
|
fn boolToStr(b: bool) -> []const u8 {
|
|
|
|
if (b) "true" else "false"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test "peer resolve array and const slice" {
|
|
|
|
testPeerResolveArrayConstSlice(true);
|
|
|
|
comptime testPeerResolveArrayConstSlice(true);
|
|
|
|
}
|
|
|
|
fn testPeerResolveArrayConstSlice(b: bool) {
|
|
|
|
const value1 = if (b) "aoeu" else ([]const u8)("zz");
|
|
|
|
const value2 = if (b) ([]const u8)("zz") else "aoeu";
|
|
|
|
assert(mem.eql(u8, value1, "aoeu"));
|
|
|
|
assert(mem.eql(u8, value2, "zz"));
|
|
|
|
}
|
2017-04-07 12:34:13 -07:00
|
|
|
|
|
|
|
test "integer literal to &const int" {
|
|
|
|
const x: &const i32 = 3;
|
|
|
|
assert(*x == 3);
|
|
|
|
}
|
2017-04-07 14:56:01 -07:00
|
|
|
|
|
|
|
test "string literal to &const []const u8" {
|
|
|
|
const x: &const []const u8 = "hello";
|
|
|
|
assert(mem.eql(u8, *x, "hello"));
|
|
|
|
}
|
2017-04-13 02:59:39 -07:00
|
|
|
|
|
|
|
test "implicitly cast from T to %?T" {
|
|
|
|
castToMaybeTypeError(1);
|
|
|
|
comptime castToMaybeTypeError(1);
|
|
|
|
}
|
|
|
|
const A = struct {
|
|
|
|
a: i32,
|
|
|
|
};
|
|
|
|
fn castToMaybeTypeError(z: i32) {
|
|
|
|
const x = i32(1);
|
|
|
|
const y: %?i32 = x;
|
|
|
|
assert(??%%y == 1);
|
|
|
|
|
|
|
|
const f = z;
|
|
|
|
const g: %?i32 = f;
|
|
|
|
|
2017-04-20 04:36:59 -07:00
|
|
|
const a = A{ .a = z };
|
2017-04-13 02:59:39 -07:00
|
|
|
const b: %?A = a;
|
|
|
|
assert((??%%b).a == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "implicitly cast from int to %?T" {
|
2017-04-20 04:36:59 -07:00
|
|
|
implicitIntLitToMaybe();
|
|
|
|
comptime implicitIntLitToMaybe();
|
2017-04-13 02:59:39 -07:00
|
|
|
}
|
2017-04-20 04:36:59 -07:00
|
|
|
fn implicitIntLitToMaybe() {
|
|
|
|
const f: ?i32 = 1;
|
|
|
|
const g: %?i32 = 1;
|
|
|
|
}
|
|
|
|
|
2017-04-13 02:59:39 -07:00
|
|
|
|
|
|
|
test "return null from fn() -> %?&T" {
|
|
|
|
const a = returnNullFromMaybeTypeErrorRef();
|
|
|
|
const b = returnNullLitFromMaybeTypeErrorRef();
|
|
|
|
assert(%%a == null and %%b == null);
|
|
|
|
}
|
|
|
|
fn returnNullFromMaybeTypeErrorRef() -> %?&A {
|
|
|
|
const a: ?&A = null;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
fn returnNullLitFromMaybeTypeErrorRef() -> %?&A {
|
|
|
|
return null;
|
2017-04-22 08:45:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "peer type resolution: ?T and T" {
|
|
|
|
assert(??peerTypeTAndMaybeT(true, false) == 0);
|
2017-04-22 09:19:20 -07:00
|
|
|
assert(??peerTypeTAndMaybeT(false, false) == 3);
|
|
|
|
comptime {
|
|
|
|
assert(??peerTypeTAndMaybeT(true, false) == 0);
|
|
|
|
assert(??peerTypeTAndMaybeT(false, false) == 3);
|
|
|
|
}
|
2017-04-22 08:45:04 -07:00
|
|
|
}
|
|
|
|
fn peerTypeTAndMaybeT(c: bool, b: bool) -> ?usize {
|
|
|
|
if (c) {
|
|
|
|
return if (b) null else usize(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return usize(3);
|
|
|
|
}
|
2017-04-22 09:19:20 -07:00
|
|
|
|
|
|
|
|
|
|
|
test "peer type resolution: [0]u8 and []const u8" {
|
|
|
|
assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
|
|
|
|
assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
|
|
|
|
comptime {
|
|
|
|
assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
|
|
|
|
assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) -> []const u8 {
|
|
|
|
if (a) {
|
|
|
|
return []const u8 {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return slice[0...1];
|
|
|
|
}
|
2017-04-26 16:17:05 -07:00
|
|
|
|
|
|
|
test "implicitly cast from [N]T to ?[]const T" {
|
|
|
|
assert(mem.eql(u8, ??castToMaybeSlice(), "hi"));
|
|
|
|
comptime assert(mem.eql(u8, ??castToMaybeSlice(), "hi"));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn castToMaybeSlice() -> ?[]const u8 {
|
|
|
|
return "hi";
|
|
|
|
}
|