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-08-08 14:38:25 -07:00
|
|
|
const z = @ptrToInt(y);
|
2017-01-05 00:57:48 -08:00
|
|
|
assert(z == 13);
|
2016-12-21 22:42:30 -08:00
|
|
|
}
|
2017-01-31 10:38:04 -08:00
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "integer literal to pointer cast" {
|
2017-04-07 12:34:13 -07:00
|
|
|
const vga_mem = @intToPtr(&u16, 0xB8000);
|
2017-08-08 14:38:25 -07:00
|
|
|
assert(@ptrToInt(vga_mem) == 0xB8000);
|
2017-01-31 10:38:04 -08:00
|
|
|
}
|
2017-03-14 18:38:27 -07:00
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "pointer reinterpret const float to int" {
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn funcWithConstPtrPtr(x: &const &i32) void {
|
2017-03-31 02:48:15 -07:00
|
|
|
**x += 1;
|
|
|
|
}
|
2017-04-02 15:19:59 -07:00
|
|
|
|
|
|
|
test "explicit cast from integer to error type" {
|
|
|
|
testCastIntToErr(error.ItBroke);
|
|
|
|
comptime testCastIntToErr(error.ItBroke);
|
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testCastIntToErr(err: error) void {
|
2017-04-02 15:19:59 -07:00
|
|
|
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"));
|
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn boolToStr(b: bool) []const u8 {
|
2017-12-21 21:50:30 -08:00
|
|
|
return if (b) "true" else "false";
|
2017-04-06 15:07:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test "peer resolve array and const slice" {
|
|
|
|
testPeerResolveArrayConstSlice(true);
|
|
|
|
comptime testPeerResolveArrayConstSlice(true);
|
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testPeerResolveArrayConstSlice(b: bool) void {
|
2017-04-06 15:07:38 -07:00
|
|
|
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
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
test "implicitly cast from T to error!?T" {
|
2017-04-13 02:59:39 -07:00
|
|
|
castToMaybeTypeError(1);
|
|
|
|
comptime castToMaybeTypeError(1);
|
|
|
|
}
|
|
|
|
const A = struct {
|
|
|
|
a: i32,
|
|
|
|
};
|
2018-01-25 01:10:11 -08:00
|
|
|
fn castToMaybeTypeError(z: i32) void {
|
2017-04-13 02:59:39 -07:00
|
|
|
const x = i32(1);
|
2018-02-02 11:26:14 -08:00
|
|
|
const y: error!?i32 = x;
|
2018-01-08 21:07:01 -08:00
|
|
|
assert(??(try y) == 1);
|
2017-04-13 02:59:39 -07:00
|
|
|
|
|
|
|
const f = z;
|
2018-02-02 11:26:14 -08:00
|
|
|
const g: error!?i32 = f;
|
2017-04-13 02:59:39 -07:00
|
|
|
|
2017-04-20 04:36:59 -07:00
|
|
|
const a = A{ .a = z };
|
2018-02-02 11:26:14 -08:00
|
|
|
const b: error!?A = a;
|
2018-01-08 21:07:01 -08:00
|
|
|
assert((??(b catch unreachable)).a == 1);
|
2017-04-13 02:59:39 -07:00
|
|
|
}
|
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
test "implicitly cast from int to error!?T" {
|
2017-04-20 04:36:59 -07:00
|
|
|
implicitIntLitToMaybe();
|
|
|
|
comptime implicitIntLitToMaybe();
|
2017-04-13 02:59:39 -07:00
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn implicitIntLitToMaybe() void {
|
2017-04-20 04:36:59 -07:00
|
|
|
const f: ?i32 = 1;
|
2018-02-02 11:26:14 -08:00
|
|
|
const g: error!?i32 = 1;
|
2017-04-20 04:36:59 -07:00
|
|
|
}
|
|
|
|
|
2017-04-13 02:59:39 -07:00
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
test "return null from fn() error!?&T" {
|
2017-04-13 02:59:39 -07:00
|
|
|
const a = returnNullFromMaybeTypeErrorRef();
|
|
|
|
const b = returnNullLitFromMaybeTypeErrorRef();
|
2018-01-08 21:07:01 -08:00
|
|
|
assert((try a) == null and (try b) == null);
|
2017-04-13 02:59:39 -07:00
|
|
|
}
|
2018-02-02 11:26:14 -08:00
|
|
|
fn returnNullFromMaybeTypeErrorRef() error!?&A {
|
2017-04-13 02:59:39 -07:00
|
|
|
const a: ?&A = null;
|
|
|
|
return a;
|
|
|
|
}
|
2018-02-02 11:26:14 -08:00
|
|
|
fn returnNullLitFromMaybeTypeErrorRef() error!?&A {
|
2017-04-13 02:59:39 -07:00
|
|
|
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
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn peerTypeTAndMaybeT(c: bool, b: bool) ?usize {
|
2017-04-22 08:45:04 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
|
2017-04-22 09:19:20 -07:00
|
|
|
if (a) {
|
|
|
|
return []const u8 {};
|
|
|
|
}
|
|
|
|
|
2017-05-19 07:39:59 -07:00
|
|
|
return slice[0..1];
|
2017-04-22 09:19:20 -07:00
|
|
|
}
|
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"));
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn castToMaybeSlice() ?[]const u8 {
|
2017-04-26 16:17:05 -07:00
|
|
|
return "hi";
|
|
|
|
}
|
2017-04-29 16:23:33 -07:00
|
|
|
|
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
test "implicitly cast from [0]T to error![]T" {
|
2017-04-29 16:23:33 -07:00
|
|
|
testCastZeroArrayToErrSliceMut();
|
|
|
|
comptime testCastZeroArrayToErrSliceMut();
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testCastZeroArrayToErrSliceMut() void {
|
2018-01-08 21:07:01 -08:00
|
|
|
assert((gimmeErrOrSlice() catch unreachable).len == 0);
|
2017-04-29 16:23:33 -07:00
|
|
|
}
|
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
fn gimmeErrOrSlice() error![]u8 {
|
2017-04-29 16:23:33 -07:00
|
|
|
return []u8{};
|
|
|
|
}
|
2017-04-30 09:21:24 -07:00
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
test "peer type resolution: [0]u8, []const u8, and error![]u8" {
|
2017-04-30 09:21:24 -07:00
|
|
|
{
|
|
|
|
var data = "hi";
|
2017-05-19 07:39:59 -07:00
|
|
|
const slice = data[0..];
|
2018-01-08 21:07:01 -08:00
|
|
|
assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
|
|
|
|
assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
|
2017-04-30 09:21:24 -07:00
|
|
|
}
|
|
|
|
comptime {
|
|
|
|
var data = "hi";
|
2017-05-19 07:39:59 -07:00
|
|
|
const slice = data[0..];
|
2018-01-08 21:07:01 -08:00
|
|
|
assert((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
|
|
|
|
assert((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
|
2017-04-30 09:21:24 -07:00
|
|
|
}
|
|
|
|
}
|
2018-02-02 11:26:14 -08:00
|
|
|
fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) error![]u8 {
|
2017-04-30 09:21:24 -07:00
|
|
|
if (a) {
|
|
|
|
return []u8{};
|
|
|
|
}
|
|
|
|
|
2017-05-19 07:39:59 -07:00
|
|
|
return slice[0..1];
|
2017-04-30 09:21:24 -07:00
|
|
|
}
|
2017-05-01 19:37:34 -07:00
|
|
|
|
|
|
|
test "resolve undefined with integer" {
|
|
|
|
testResolveUndefWithInt(true, 1234);
|
|
|
|
comptime testResolveUndefWithInt(true, 1234);
|
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testResolveUndefWithInt(b: bool, x: i32) void {
|
2017-05-01 19:37:34 -07:00
|
|
|
const value = if (b) x else undefined;
|
|
|
|
if (b) {
|
|
|
|
assert(value == x);
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 13:44:13 -07:00
|
|
|
|
|
|
|
test "implicit cast from &const [N]T to []const T" {
|
|
|
|
testCastConstArrayRefToConstSlice();
|
|
|
|
comptime testCastConstArrayRefToConstSlice();
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testCastConstArrayRefToConstSlice() void {
|
2017-05-26 13:44:13 -07:00
|
|
|
const blah = "aoeu";
|
|
|
|
const const_array_ref = &blah;
|
|
|
|
assert(@typeOf(const_array_ref) == &const [4]u8);
|
|
|
|
const slice: []const u8 = const_array_ref;
|
|
|
|
assert(mem.eql(u8, slice, "aoeu"));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "var args implicitly casts by value arg to const ref" {
|
|
|
|
foo("hello");
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn foo(args: ...) void {
|
2017-05-26 13:44:13 -07:00
|
|
|
assert(@typeOf(args[0]) == &const [5]u8);
|
|
|
|
}
|
2017-06-17 07:28:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
test "peer type resolution: error and [N]T" {
|
2018-02-02 11:26:14 -08:00
|
|
|
// TODO: implicit error!T to error!U where T can implicitly cast to U
|
2018-01-08 21:07:01 -08:00
|
|
|
//assert(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
|
|
|
|
//comptime assert(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
|
2017-06-17 07:28:42 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
assert(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
|
|
|
|
comptime assert(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
|
2017-06-17 07:28:42 -07:00
|
|
|
}
|
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
//fn testPeerErrorAndArray(x: u8) error![]const u8 {
|
2018-01-02 17:52:24 -08:00
|
|
|
// return switch (x) {
|
|
|
|
// 0x00 => "OK",
|
|
|
|
// else => error.BadValue,
|
|
|
|
// };
|
|
|
|
//}
|
2018-02-02 11:26:14 -08:00
|
|
|
fn testPeerErrorAndArray2(x: u8) error![]const u8 {
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (x) {
|
2017-06-17 07:28:42 -07:00
|
|
|
0x00 => "OK",
|
|
|
|
0x01 => "OKK",
|
|
|
|
else => error.BadValue,
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-17 07:28:42 -07:00
|
|
|
}
|
2017-08-07 12:57:41 -07:00
|
|
|
|
|
|
|
test "explicit cast float number literal to integer if no fraction component" {
|
|
|
|
const x = i32(1e4);
|
|
|
|
assert(x == 10000);
|
|
|
|
const y = i32(f32(1e4));
|
|
|
|
assert(y == 10000);
|
|
|
|
}
|
2017-08-17 19:52:12 -07:00
|
|
|
|
|
|
|
test "cast u128 to f128 and back" {
|
|
|
|
comptime testCast128();
|
|
|
|
testCast128();
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testCast128() void {
|
2017-08-17 19:52:12 -07:00
|
|
|
assert(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn cast128Int(x: f128) u128 {
|
2017-12-21 21:50:30 -08:00
|
|
|
return @bitCast(u128, x);
|
2017-08-17 19:52:12 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn cast128Float(x: u128) f128 {
|
2017-12-21 21:50:30 -08:00
|
|
|
return @bitCast(f128, x);
|
2017-08-17 19:52:12 -07:00
|
|
|
}
|
2017-08-25 16:53:29 -07:00
|
|
|
|
|
|
|
test "const slice widen cast" {
|
2017-08-30 01:54:33 -07:00
|
|
|
const bytes align(4) = []u8{0x12, 0x12, 0x12, 0x12};
|
2017-08-25 16:53:29 -07:00
|
|
|
|
|
|
|
const u32_value = ([]const u32)(bytes[0..])[0];
|
|
|
|
assert(u32_value == 0x12121212);
|
|
|
|
|
2017-08-25 17:05:10 -07:00
|
|
|
assert(@bitCast(u32, bytes) == 0x12121212);
|
2017-08-25 16:53:29 -07:00
|
|
|
}
|