2018-06-08 21:15:23 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const assert = std.debug.assert;
|
|
|
|
const mem = 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);
|
2018-05-31 07:56:59 -07:00
|
|
|
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" {
|
2018-05-31 07:56:59 -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" {
|
2018-04-30 22:53:04 -07:00
|
|
|
const float: f64 = 5.99999999999994648725e-01;
|
2017-03-14 18:38:27 -07:00
|
|
|
const float_ptr = &float;
|
2018-05-31 07:56:59 -07:00
|
|
|
const int_ptr = @ptrCast(*const i32, float_ptr);
|
2018-04-30 17:35:54 -07:00
|
|
|
const int_val = int_ptr.*;
|
2017-03-14 18:38:27 -07:00
|
|
|
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-05-31 07:56:59 -07:00
|
|
|
fn funcWithConstPtrPtr(x: *const *i32) void {
|
2018-04-30 17:35:54 -07:00
|
|
|
x.*.* += 1;
|
2017-03-31 02:48:15 -07:00
|
|
|
}
|
2017-04-02 15:19:59 -07:00
|
|
|
|
2018-02-23 06:25:42 -08:00
|
|
|
test "implicitly cast a container to a const pointer of it" {
|
2018-05-28 17:23:55 -07:00
|
|
|
const z = Struct(void){ .x = void{} };
|
2018-02-23 06:25:42 -08:00
|
|
|
assert(0 == @sizeOf(@typeOf(z)));
|
|
|
|
assert(void{} == Struct(void).pointer(z).x);
|
|
|
|
assert(void{} == Struct(void).pointer(&z).x);
|
|
|
|
assert(void{} == Struct(void).maybePointer(z).x);
|
|
|
|
assert(void{} == Struct(void).maybePointer(&z).x);
|
|
|
|
assert(void{} == Struct(void).maybePointer(null).x);
|
2018-05-28 17:23:55 -07:00
|
|
|
const s = Struct(u8){ .x = 42 };
|
2018-02-23 06:25:42 -08:00
|
|
|
assert(0 != @sizeOf(@typeOf(s)));
|
|
|
|
assert(42 == Struct(u8).pointer(s).x);
|
|
|
|
assert(42 == Struct(u8).pointer(&s).x);
|
|
|
|
assert(42 == Struct(u8).maybePointer(s).x);
|
|
|
|
assert(42 == Struct(u8).maybePointer(&s).x);
|
|
|
|
assert(0 == Struct(u8).maybePointer(null).x);
|
2018-05-28 17:23:55 -07:00
|
|
|
const u = Union{ .x = 42 };
|
2018-02-23 06:25:42 -08:00
|
|
|
assert(42 == Union.pointer(u).x);
|
|
|
|
assert(42 == Union.pointer(&u).x);
|
|
|
|
assert(42 == Union.maybePointer(u).x);
|
|
|
|
assert(42 == Union.maybePointer(&u).x);
|
|
|
|
assert(0 == Union.maybePointer(null).x);
|
|
|
|
const e = Enum.Some;
|
|
|
|
assert(Enum.Some == Enum.pointer(e));
|
|
|
|
assert(Enum.Some == Enum.pointer(&e));
|
|
|
|
assert(Enum.Some == Enum.maybePointer(e));
|
|
|
|
assert(Enum.Some == Enum.maybePointer(&e));
|
|
|
|
assert(Enum.None == Enum.maybePointer(null));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn Struct(comptime T: type) type {
|
|
|
|
return struct {
|
|
|
|
const Self = this;
|
|
|
|
x: T,
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn pointer(self: *const Self) Self {
|
2018-04-30 17:35:54 -07:00
|
|
|
return self.*;
|
2018-02-23 06:25:42 -08:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn maybePointer(self: ?*const Self) Self {
|
2018-05-28 17:23:55 -07:00
|
|
|
const none = Self{ .x = if (T == void) void{} else 0 };
|
2018-06-09 22:13:51 -07:00
|
|
|
return (self orelse &none).*;
|
2018-02-23 06:25:42 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const Union = union {
|
|
|
|
x: u8,
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn pointer(self: *const Union) Union {
|
2018-04-30 17:35:54 -07:00
|
|
|
return self.*;
|
2018-02-23 06:25:42 -08:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn maybePointer(self: ?*const Union) Union {
|
2018-05-28 17:23:55 -07:00
|
|
|
const none = Union{ .x = 0 };
|
2018-06-09 22:13:51 -07:00
|
|
|
return (self orelse &none).*;
|
2018-02-23 06:25:42 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const Enum = enum {
|
|
|
|
None,
|
|
|
|
Some,
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn pointer(self: *const Enum) Enum {
|
2018-04-30 17:35:54 -07:00
|
|
|
return self.*;
|
2018-02-23 06:25:42 -08:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn maybePointer(self: ?*const Enum) Enum {
|
2018-06-09 22:13:51 -07:00
|
|
|
return (self orelse &Enum.None).*;
|
2018-02-23 06:25:42 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-26 09:40:33 -08:00
|
|
|
test "implicitly cast indirect pointer to maybe-indirect pointer" {
|
|
|
|
const S = struct {
|
|
|
|
const Self = this;
|
|
|
|
x: u8,
|
2018-05-31 07:56:59 -07:00
|
|
|
fn constConst(p: *const *const Self) u8 {
|
2018-06-09 20:42:14 -07:00
|
|
|
return p.*.x;
|
2018-02-26 09:40:33 -08:00
|
|
|
}
|
2018-05-31 07:56:59 -07:00
|
|
|
fn maybeConstConst(p: ?*const *const Self) u8 {
|
2018-06-09 20:42:14 -07:00
|
|
|
return p.?.*.x;
|
2018-02-26 09:40:33 -08:00
|
|
|
}
|
2018-05-31 07:56:59 -07:00
|
|
|
fn constConstConst(p: *const *const *const Self) u8 {
|
2018-06-09 20:42:14 -07:00
|
|
|
return p.*.*.x;
|
2018-02-26 09:40:33 -08:00
|
|
|
}
|
2018-05-31 07:56:59 -07:00
|
|
|
fn maybeConstConstConst(p: ?*const *const *const Self) u8 {
|
2018-06-09 20:42:14 -07:00
|
|
|
return p.?.*.*.x;
|
2018-02-26 09:40:33 -08:00
|
|
|
}
|
|
|
|
};
|
2018-05-28 17:23:55 -07:00
|
|
|
const s = S{ .x = 42 };
|
2018-02-26 09:40:33 -08:00
|
|
|
const p = &s;
|
|
|
|
const q = &p;
|
|
|
|
const r = &q;
|
|
|
|
assert(42 == S.constConst(p));
|
|
|
|
assert(42 == S.constConst(q));
|
|
|
|
assert(42 == S.maybeConstConst(p));
|
|
|
|
assert(42 == S.maybeConstConst(q));
|
|
|
|
assert(42 == S.constConstConst(q));
|
|
|
|
assert(42 == S.constConstConst(r));
|
|
|
|
assert(42 == S.maybeConstConstConst(q));
|
|
|
|
assert(42 == S.maybeConstConstConst(r));
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-06-18 15:48:29 -07:00
|
|
|
const x = @errorToInt(err);
|
|
|
|
const y = @intToError(x);
|
2017-04-02 15:19:59 -07:00
|
|
|
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" {
|
2018-05-31 07:56:59 -07:00
|
|
|
const x: *const i32 = 3;
|
2018-04-30 17:35:54 -07:00
|
|
|
assert(x.* == 3);
|
2017-04-07 12:34:13 -07:00
|
|
|
}
|
2017-04-07 14:56:01 -07:00
|
|
|
|
|
|
|
test "string literal to &const []const u8" {
|
2018-05-31 07:56:59 -07:00
|
|
|
const x: *const []const u8 = "hello";
|
2018-04-30 17:35:54 -07:00
|
|
|
assert(mem.eql(u8, x.*, "hello"));
|
2017-04-07 14:56:01 -07:00
|
|
|
}
|
2017-04-13 02:59:39 -07:00
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
test "implicitly cast from T to error!?T" {
|
2018-06-09 20:42:14 -07:00
|
|
|
castToOptionalTypeError(1);
|
|
|
|
comptime castToOptionalTypeError(1);
|
2017-04-13 02:59:39 -07:00
|
|
|
}
|
|
|
|
const A = struct {
|
|
|
|
a: i32,
|
|
|
|
};
|
2018-06-09 20:42:14 -07:00
|
|
|
fn castToOptionalTypeError(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-06-09 20:42:14 -07: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
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
const a = A{ .a = z };
|
2018-02-02 11:26:14 -08:00
|
|
|
const b: error!?A = a;
|
2018-06-09 20:42:14 -07: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" {
|
2018-06-09 20:42:14 -07:00
|
|
|
implicitIntLitToOptional();
|
|
|
|
comptime implicitIntLitToOptional();
|
2017-04-13 02:59:39 -07:00
|
|
|
}
|
2018-06-09 20:42:14 -07:00
|
|
|
fn implicitIntLitToOptional() 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
|
|
|
}
|
|
|
|
|
2018-02-02 11:26:14 -08:00
|
|
|
test "return null from fn() error!?&T" {
|
2018-06-09 20:42:14 -07:00
|
|
|
const a = returnNullFromOptionalTypeErrorRef();
|
|
|
|
const b = returnNullLitFromOptionalTypeErrorRef();
|
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-06-09 20:42:14 -07:00
|
|
|
fn returnNullFromOptionalTypeErrorRef() error!?*A {
|
2018-05-31 07:56:59 -07:00
|
|
|
const a: ?*A = null;
|
2017-04-13 02:59:39 -07:00
|
|
|
return a;
|
|
|
|
}
|
2018-06-09 20:42:14 -07:00
|
|
|
fn returnNullLitFromOptionalTypeErrorRef() 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" {
|
2018-06-09 20:42:14 -07:00
|
|
|
assert(peerTypeTAndOptionalT(true, false).? == 0);
|
|
|
|
assert(peerTypeTAndOptionalT(false, false).? == 3);
|
2017-04-22 09:19:20 -07:00
|
|
|
comptime {
|
2018-06-09 20:42:14 -07:00
|
|
|
assert(peerTypeTAndOptionalT(true, false).? == 0);
|
|
|
|
assert(peerTypeTAndOptionalT(false, false).? == 3);
|
2017-04-22 09:19:20 -07:00
|
|
|
}
|
2017-04-22 08:45:04 -07:00
|
|
|
}
|
2018-06-09 20:42:14 -07:00
|
|
|
fn peerTypeTAndOptionalT(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) {
|
2018-04-30 17:35:54 -07:00
|
|
|
return []const u8{};
|
2017-04-22 09:19:20 -07:00
|
|
|
}
|
|
|
|
|
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" {
|
2018-06-09 20:42:14 -07:00
|
|
|
assert(mem.eql(u8, castToOptionalSlice().?, "hi"));
|
|
|
|
comptime assert(mem.eql(u8, castToOptionalSlice().?, "hi"));
|
2017-04-26 16:17:05 -07:00
|
|
|
}
|
|
|
|
|
2018-06-09 20:42:14 -07:00
|
|
|
fn castToOptionalSlice() ?[]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;
|
2018-05-31 07:56:59 -07:00
|
|
|
assert(@typeOf(const_array_ref) == *const [4]u8);
|
2017-05-26 13:44:13 -07:00
|
|
|
const slice: []const u8 = const_array_ref;
|
|
|
|
assert(mem.eql(u8, slice, "aoeu"));
|
|
|
|
}
|
|
|
|
|
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"));
|
|
|
|
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
|
|
|
|
2018-06-19 13:06:10 -07:00
|
|
|
test "@floatToInt" {
|
|
|
|
testFloatToInts();
|
|
|
|
comptime testFloatToInts();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testFloatToInts() void {
|
2017-08-07 12:57:41 -07:00
|
|
|
const x = i32(1e4);
|
|
|
|
assert(x == 10000);
|
2018-06-16 23:57:07 -07:00
|
|
|
const y = @floatToInt(i32, f32(1e4));
|
2017-08-07 12:57:41 -07:00
|
|
|
assert(y == 10000);
|
2018-06-19 13:06:10 -07:00
|
|
|
expectFloatToInt(u8, 255.1, 255);
|
|
|
|
expectFloatToInt(i8, 127.2, 127);
|
|
|
|
expectFloatToInt(i8, -128.2, -128);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expectFloatToInt(comptime T: type, f: f32, i: T) void {
|
|
|
|
assert(@floatToInt(T, f) == i);
|
2017-08-07 12:57:41 -07:00
|
|
|
}
|
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" {
|
2018-05-28 17:23:55 -07:00
|
|
|
const bytes align(4) = []u8{
|
2018-04-30 17:35:54 -07:00
|
|
|
0x12,
|
|
|
|
0x12,
|
|
|
|
0x12,
|
|
|
|
0x12,
|
|
|
|
};
|
2017-08-25 16:53:29 -07:00
|
|
|
|
2018-06-18 14:25:29 -07:00
|
|
|
const u32_value = @bytesToSlice(u32, bytes[0..])[0];
|
2017-08-25 16:53:29 -07:00
|
|
|
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
|
|
|
}
|
2018-06-07 19:19:00 -07:00
|
|
|
|
|
|
|
test "single-item pointer of array to slice and to unknown length pointer" {
|
|
|
|
testCastPtrOfArrayToSliceAndPtr();
|
|
|
|
comptime testCastPtrOfArrayToSliceAndPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testCastPtrOfArrayToSliceAndPtr() void {
|
|
|
|
var array = "ao" ++ "eu"; // TODO https://github.com/ziglang/zig/issues/1076
|
|
|
|
const x: [*]u8 = &array;
|
|
|
|
x[0] += 1;
|
|
|
|
assert(mem.eql(u8, array[0..], "boeu"));
|
|
|
|
const y: []u8 = &array;
|
|
|
|
y[0] += 1;
|
|
|
|
assert(mem.eql(u8, array[0..], "coeu"));
|
|
|
|
}
|
|
|
|
|
2018-06-08 21:15:23 -07:00
|
|
|
test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
|
|
|
|
const window_name = [1][*]const u8{c"window name"};
|
|
|
|
const x: [*]const ?[*]const u8 = &window_name;
|
2018-06-09 20:42:14 -07:00
|
|
|
assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name"));
|
2018-06-08 21:15:23 -07:00
|
|
|
}
|
2018-06-16 23:57:07 -07:00
|
|
|
|
|
|
|
test "@intCast comptime_int" {
|
|
|
|
const result = @intCast(i32, 1234);
|
|
|
|
assert(@typeOf(result) == i32);
|
|
|
|
assert(result == 1234);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "@floatCast comptime_int and comptime_float" {
|
2018-06-27 07:20:04 -07:00
|
|
|
{
|
|
|
|
const result = @floatCast(f32, 1234);
|
|
|
|
assert(@typeOf(result) == f32);
|
|
|
|
assert(result == 1234.0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const result = @floatCast(f32, 1234.0);
|
|
|
|
assert(@typeOf(result) == f32);
|
|
|
|
assert(result == 1234.0);
|
|
|
|
}
|
2018-06-16 23:57:07 -07:00
|
|
|
}
|
2018-06-18 08:04:18 -07:00
|
|
|
|
|
|
|
test "comptime_int @intToFloat" {
|
2018-06-27 07:20:04 -07:00
|
|
|
{
|
|
|
|
const result = @intToFloat(f32, 1234);
|
|
|
|
assert(@typeOf(result) == f32);
|
|
|
|
assert(result == 1234.0);
|
|
|
|
}
|
2018-06-18 08:04:18 -07:00
|
|
|
}
|
2018-06-18 14:25:29 -07:00
|
|
|
|
|
|
|
test "@bytesToSlice keeps pointer alignment" {
|
|
|
|
var bytes = []u8{ 0x01, 0x02, 0x03, 0x04 };
|
|
|
|
const numbers = @bytesToSlice(u32, bytes[0..]);
|
|
|
|
comptime assert(@typeOf(numbers) == []align(@alignOf(@typeOf(bytes))) u32);
|
|
|
|
}
|
2018-06-19 13:06:10 -07:00
|
|
|
|
|
|
|
test "@intCast i32 to u7" {
|
|
|
|
var x: u128 = @maxValue(u128);
|
|
|
|
var y: i32 = 120;
|
|
|
|
var z = x >> @intCast(u7, y);
|
|
|
|
assert(z == 0xff);
|
|
|
|
}
|