aboutsummaryrefslogtreecommitdiff
path: root/test/behavior
diff options
context:
space:
mode:
authordrew <reserveblue@protonmail.com>2021-11-14 18:28:44 -0800
committerAndrew Kelley <andrew@ziglang.org>2021-11-16 16:51:31 -0700
commit9bf1681990fe87a6b2e5fc644a89f1aece304579 (patch)
tree45617b8500c741e8de902852c0495b5c20279dbf /test/behavior
parent952d865bd231834adad30905c469edc5a46d000a (diff)
downloadzig-9bf1681990fe87a6b2e5fc644a89f1aece304579.tar.gz
zig-9bf1681990fe87a6b2e5fc644a89f1aece304579.zip
C backend: basic big ints, fix airPtrToInt, array references, pointer arithmetic UB with NULL, implement airPtrElemPtr/Val, fix redundant indirection/references with arrays
-add additional test cases that were found to be passing -add basic int128 test cases which previously did not pass but weren't covered -most test cases in cast.zig now pass -i128/u128 or smaller int constants can now be rendered -unsigned int constants are now always suffixed with 'u' to prevent random compile errors -pointers with a val tag of 'zero' now just emit a 0 constant which coerces to the pointer type and fixes some warnings with ordered comparisons -pointers with a val tag of 'one' are now casted back to the pointer type -support pointers with a u64 val -fix bug where rendering an array's type will emit more indirection than is needed -render uint128_t/int128_t manually when needed -implement ptr_add/sub AIR handlers manually so they manually cast to int types which avoids UB if the result or ptr operand is NULL -implement airPtrElemVal/Ptr -airAlloc for arrays will not allocate a ref as the local for the array is already a reference/pointer to the array itself -fix airPtrToInt by casting to the int type
Diffstat (limited to 'test/behavior')
-rw-r--r--test/behavior/cast.zig234
-rw-r--r--test/behavior/cast_c.zig249
-rw-r--r--test/behavior/int128.zig43
-rw-r--r--test/behavior/pointers.zig12
4 files changed, 300 insertions, 238 deletions
diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig
index 83230c64d0..6c41152fe9 100644
--- a/test/behavior/cast.zig
+++ b/test/behavior/cast.zig
@@ -2,70 +2,8 @@ const std = @import("std");
const expect = std.testing.expect;
const mem = std.mem;
const maxInt = std.math.maxInt;
-const Vector = std.meta.Vector;
const native_endian = @import("builtin").target.cpu.arch.endian();
-test "int to ptr cast" {
- const x = @as(usize, 13);
- const y = @intToPtr(*u8, x);
- const z = @ptrToInt(y);
- try expect(z == 13);
-}
-
-test "integer literal to pointer cast" {
- const vga_mem = @intToPtr(*u16, 0xB8000);
- try expect(@ptrToInt(vga_mem) == 0xB8000);
-}
-
-test "peer type resolution: ?T and T" {
- try expect(peerTypeTAndOptionalT(true, false).? == 0);
- try expect(peerTypeTAndOptionalT(false, false).? == 3);
- comptime {
- try expect(peerTypeTAndOptionalT(true, false).? == 0);
- try expect(peerTypeTAndOptionalT(false, false).? == 3);
- }
-}
-fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
- if (c) {
- return if (b) null else @as(usize, 0);
- }
-
- return @as(usize, 3);
-}
-
-test "resolve undefined with integer" {
- try testResolveUndefWithInt(true, 1234);
- comptime try testResolveUndefWithInt(true, 1234);
-}
-fn testResolveUndefWithInt(b: bool, x: i32) !void {
- const value = if (b) x else undefined;
- if (b) {
- try expect(value == x);
- }
-}
-
-test "@intCast i32 to u7" {
- var x: u128 = maxInt(u128);
- var y: i32 = 120;
- var z = x >> @intCast(u7, y);
- try expect(z == 0xff);
-}
-
-test "@intCast to comptime_int" {
- try expect(@intCast(comptime_int, 0) == 0);
-}
-
-test "implicit cast comptime numbers to any type when the value fits" {
- const a: u64 = 255;
- var b: u8 = a;
- try expect(b == 255);
-}
-
-test "implicit cast comptime_int to comptime_float" {
- comptime try expect(@as(comptime_float, 10) == @as(f32, 10));
- try expect(2 == 2.0);
-}
-
test "pointer reinterpret const float to int" {
// The hex representation is 0x3fe3333333333303.
const float: f64 = 5.99999999999994648725e-01;
@@ -78,51 +16,15 @@ test "pointer reinterpret const float to int" {
try expect(int_val == 0x3fe33333);
}
-test "comptime_int @intToFloat" {
- {
- const result = @intToFloat(f16, 1234);
- try expect(@TypeOf(result) == f16);
- try expect(result == 1234.0);
- }
- {
- const result = @intToFloat(f32, 1234);
- try expect(@TypeOf(result) == f32);
- try expect(result == 1234.0);
- }
- {
- const result = @intToFloat(f64, 1234);
- try expect(@TypeOf(result) == f64);
- try expect(result == 1234.0);
- }
- {
- const result = @intToFloat(f128, 1234);
- try expect(@TypeOf(result) == f128);
- try expect(result == 1234.0);
- }
- // big comptime_int (> 64 bits) to f128 conversion
- {
- const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
- try expect(@TypeOf(result) == f128);
- try expect(result == 0x1_0000_0000_0000_0000.0);
- }
-}
-
test "@floatToInt" {
try testFloatToInts();
comptime try testFloatToInts();
}
fn testFloatToInts() !void {
- const x = @as(i32, 1e4);
- try expect(x == 10000);
- const y = @floatToInt(i32, @as(f32, 1e4));
- try expect(y == 10000);
try expectFloatToInt(f16, 255.1, u8, 255);
try expectFloatToInt(f16, 127.2, i8, 127);
try expectFloatToInt(f16, -128.2, i8, -128);
- try expectFloatToInt(f32, 255.1, u8, 255);
- try expectFloatToInt(f32, 127.2, i8, 127);
- try expectFloatToInt(f32, -128.2, i8, -128);
}
fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) !void {
@@ -143,95 +45,6 @@ fn incrementVoidPtrArray(array: ?*c_void, len: usize) void {
}
}
-test "implicitly cast indirect pointer to maybe-indirect pointer" {
- const S = struct {
- const Self = @This();
- x: u8,
- fn constConst(p: *const *const Self) u8 {
- return p.*.x;
- }
- fn maybeConstConst(p: ?*const *const Self) u8 {
- return p.?.*.x;
- }
- fn constConstConst(p: *const *const *const Self) u8 {
- return p.*.*.x;
- }
- fn maybeConstConstConst(p: ?*const *const *const Self) u8 {
- return p.?.*.*.x;
- }
- };
- const s = S{ .x = 42 };
- const p = &s;
- const q = &p;
- const r = &q;
- try expect(42 == S.constConst(q));
- try expect(42 == S.maybeConstConst(q));
- try expect(42 == S.constConstConst(r));
- try expect(42 == S.maybeConstConstConst(r));
-}
-
-test "@intCast comptime_int" {
- const result = @intCast(i32, 1234);
- try expect(@TypeOf(result) == i32);
- try expect(result == 1234);
-}
-
-test "@floatCast comptime_int and comptime_float" {
- {
- const result = @floatCast(f16, 1234);
- try expect(@TypeOf(result) == f16);
- try expect(result == 1234.0);
- }
- {
- const result = @floatCast(f16, 1234.0);
- try expect(@TypeOf(result) == f16);
- try expect(result == 1234.0);
- }
- {
- const result = @floatCast(f32, 1234);
- try expect(@TypeOf(result) == f32);
- try expect(result == 1234.0);
- }
- {
- const result = @floatCast(f32, 1234.0);
- try expect(@TypeOf(result) == f32);
- try expect(result == 1234.0);
- }
-}
-
-test "coerce undefined to optional" {
- try expect(MakeType(void).getNull() == null);
- try expect(MakeType(void).getNonNull() != null);
-}
-
-fn MakeType(comptime T: type) type {
- return struct {
- fn getNull() ?T {
- return null;
- }
-
- fn getNonNull() ?T {
- return @as(T, undefined);
- }
- };
-}
-
-test "implicit cast from *[N]T to [*c]T" {
- var x: [4]u16 = [4]u16{ 0, 1, 2, 3 };
- var y: [*c]u16 = &x;
-
- try expect(std.mem.eql(u16, x[0..4], y[0..4]));
- x[0] = 8;
- y[3] = 6;
- try expect(std.mem.eql(u16, x[0..4], y[0..4]));
-}
-
-test "*usize to *void" {
- var i = @as(usize, 0);
- var v = @ptrCast(*void, &i);
- v.* = {};
-}
-
test "compile time int to ptr of function" {
try foobar(FUNCTION_CONSTANT);
}
@@ -252,50 +65,3 @@ test "implicit ptr to *c_void" {
var c: *u32 = @ptrCast(*u32, ptr2.?);
try expect(c.* == 1);
}
-
-test "@intToEnum passed a comptime_int to an enum with one item" {
- const E = enum { A };
- const x = @intToEnum(E, 0);
- try expect(x == E.A);
-}
-
-test "@intCast to u0 and use the result" {
- const S = struct {
- fn doTheTest(zero: u1, one: u1, bigzero: i32) !void {
- try expect((one << @intCast(u0, bigzero)) == 1);
- try expect((zero << @intCast(u0, bigzero)) == 0);
- }
- };
- try S.doTheTest(0, 1, 0);
- comptime try S.doTheTest(0, 1, 0);
-}
-
-test "peer result null and comptime_int" {
- const S = struct {
- fn blah(n: i32) ?i32 {
- if (n == 0) {
- return null;
- } else if (n < 0) {
- return -1;
- } else {
- return 1;
- }
- }
- };
-
- try expect(S.blah(0) == null);
- comptime try expect(S.blah(0) == null);
- try expect(S.blah(10).? == 1);
- comptime try expect(S.blah(10).? == 1);
- try expect(S.blah(-10).? == -1);
- comptime try expect(S.blah(-10).? == -1);
-}
-
-test "*const ?[*]const T to [*c]const [*c]const T" {
- var array = [_]u8{ 'o', 'k' };
- const opt_array_ptr: ?[*]const u8 = &array;
- const a: *const ?[*]const u8 = &opt_array_ptr;
- const b: [*c]const [*c]const u8 = a;
- try expect(b.*[0] == 'o');
- try expect(b[0][1] == 'k');
-}
diff --git a/test/behavior/cast_c.zig b/test/behavior/cast_c.zig
new file mode 100644
index 0000000000..e634103d42
--- /dev/null
+++ b/test/behavior/cast_c.zig
@@ -0,0 +1,249 @@
+const std = @import("std");
+const expect = std.testing.expect;
+const mem = std.mem;
+const maxInt = std.math.maxInt;
+
+test "int to ptr cast" {
+ const x = @as(usize, 13);
+ const y = @intToPtr(*u8, x);
+ const z = @ptrToInt(y);
+ try expect(z == 13);
+}
+
+test "integer literal to pointer cast" {
+ const vga_mem = @intToPtr(*u16, 0xB8000);
+ try expect(@ptrToInt(vga_mem) == 0xB8000);
+}
+
+test "peer type resolution: ?T and T" {
+ try expect(peerTypeTAndOptionalT(true, false).? == 0);
+ try expect(peerTypeTAndOptionalT(false, false).? == 3);
+ comptime {
+ try expect(peerTypeTAndOptionalT(true, false).? == 0);
+ try expect(peerTypeTAndOptionalT(false, false).? == 3);
+ }
+}
+fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
+ if (c) {
+ return if (b) null else @as(usize, 0);
+ }
+
+ return @as(usize, 3);
+}
+
+test "resolve undefined with integer" {
+ try testResolveUndefWithInt(true, 1234);
+ comptime try testResolveUndefWithInt(true, 1234);
+}
+fn testResolveUndefWithInt(b: bool, x: i32) !void {
+ const value = if (b) x else undefined;
+ if (b) {
+ try expect(value == x);
+ }
+}
+
+test "@intCast i32 to u7" {
+ var x: u128 = maxInt(u128);
+ var y: i32 = 120;
+ var z = x >> @intCast(u7, y);
+ try expect(z == 0xff);
+}
+
+test "@intCast to comptime_int" {
+ try expect(@intCast(comptime_int, 0) == 0);
+}
+
+test "implicit cast comptime numbers to any type when the value fits" {
+ const a: u64 = 255;
+ var b: u8 = a;
+ try expect(b == 255);
+}
+
+test "implicit cast comptime_int to comptime_float" {
+ comptime try expect(@as(comptime_float, 10) == @as(f32, 10));
+ try expect(2 == 2.0);
+}
+
+test "comptime_int @intToFloat" {
+ {
+ const result = @intToFloat(f16, 1234);
+ try expect(@TypeOf(result) == f16);
+ try expect(result == 1234.0);
+ }
+ {
+ const result = @intToFloat(f32, 1234);
+ try expect(@TypeOf(result) == f32);
+ try expect(result == 1234.0);
+ }
+ {
+ const result = @intToFloat(f64, 1234);
+ try expect(@TypeOf(result) == f64);
+ try expect(result == 1234.0);
+ }
+ {
+ const result = @intToFloat(f128, 1234);
+ try expect(@TypeOf(result) == f128);
+ try expect(result == 1234.0);
+ }
+ // big comptime_int (> 64 bits) to f128 conversion
+ {
+ const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
+ try expect(@TypeOf(result) == f128);
+ try expect(result == 0x1_0000_0000_0000_0000.0);
+ }
+}
+
+test "@floatToInt" {
+ try testFloatToInts();
+ comptime try testFloatToInts();
+}
+
+fn testFloatToInts() !void {
+ const x = @as(i32, 1e4);
+ try expect(x == 10000);
+ const y = @floatToInt(i32, @as(f32, 1e4));
+ try expect(y == 10000);
+ try expectFloatToInt(f32, 255.1, u8, 255);
+ try expectFloatToInt(f32, 127.2, i8, 127);
+ try expectFloatToInt(f32, -128.2, i8, -128);
+}
+
+fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) !void {
+ try expect(@floatToInt(I, f) == i);
+}
+
+test "implicitly cast indirect pointer to maybe-indirect pointer" {
+ const S = struct {
+ const Self = @This();
+ x: u8,
+ fn constConst(p: *const *const Self) u8 {
+ return p.*.x;
+ }
+ fn maybeConstConst(p: ?*const *const Self) u8 {
+ return p.?.*.x;
+ }
+ fn constConstConst(p: *const *const *const Self) u8 {
+ return p.*.*.x;
+ }
+ fn maybeConstConstConst(p: ?*const *const *const Self) u8 {
+ return p.?.*.*.x;
+ }
+ };
+ const s = S{ .x = 42 };
+ const p = &s;
+ const q = &p;
+ const r = &q;
+ try expect(42 == S.constConst(q));
+ try expect(42 == S.maybeConstConst(q));
+ try expect(42 == S.constConstConst(r));
+ try expect(42 == S.maybeConstConstConst(r));
+}
+
+test "@intCast comptime_int" {
+ const result = @intCast(i32, 1234);
+ try expect(@TypeOf(result) == i32);
+ try expect(result == 1234);
+}
+
+test "@floatCast comptime_int and comptime_float" {
+ {
+ const result = @floatCast(f16, 1234);
+ try expect(@TypeOf(result) == f16);
+ try expect(result == 1234.0);
+ }
+ {
+ const result = @floatCast(f16, 1234.0);
+ try expect(@TypeOf(result) == f16);
+ try expect(result == 1234.0);
+ }
+ {
+ const result = @floatCast(f32, 1234);
+ try expect(@TypeOf(result) == f32);
+ try expect(result == 1234.0);
+ }
+ {
+ const result = @floatCast(f32, 1234.0);
+ try expect(@TypeOf(result) == f32);
+ try expect(result == 1234.0);
+ }
+}
+
+test "coerce undefined to optional" {
+ try expect(MakeType(void).getNull() == null);
+ try expect(MakeType(void).getNonNull() != null);
+}
+
+fn MakeType(comptime T: type) type {
+ return struct {
+ fn getNull() ?T {
+ return null;
+ }
+
+ fn getNonNull() ?T {
+ return @as(T, undefined);
+ }
+ };
+}
+
+test "implicit cast from *[N]T to [*c]T" {
+ var x: [4]u16 = [4]u16{ 0, 1, 2, 3 };
+ var y: [*c]u16 = &x;
+
+ try expect(std.mem.eql(u16, x[0..4], y[0..4]));
+ x[0] = 8;
+ y[3] = 6;
+ try expect(std.mem.eql(u16, x[0..4], y[0..4]));
+}
+
+test "*usize to *void" {
+ var i = @as(usize, 0);
+ var v = @ptrCast(*void, &i);
+ v.* = {};
+}
+
+test "@intToEnum passed a comptime_int to an enum with one item" {
+ const E = enum { A };
+ const x = @intToEnum(E, 0);
+ try expect(x == E.A);
+}
+
+test "@intCast to u0 and use the result" {
+ const S = struct {
+ fn doTheTest(zero: u1, one: u1, bigzero: i32) !void {
+ try expect((one << @intCast(u0, bigzero)) == 1);
+ try expect((zero << @intCast(u0, bigzero)) == 0);
+ }
+ };
+ try S.doTheTest(0, 1, 0);
+ comptime try S.doTheTest(0, 1, 0);
+}
+
+test "peer result null and comptime_int" {
+ const S = struct {
+ fn blah(n: i32) ?i32 {
+ if (n == 0) {
+ return null;
+ } else if (n < 0) {
+ return -1;
+ } else {
+ return 1;
+ }
+ }
+ };
+
+ try expect(S.blah(0) == null);
+ comptime try expect(S.blah(0) == null);
+ try expect(S.blah(10).? == 1);
+ comptime try expect(S.blah(10).? == 1);
+ try expect(S.blah(-10).? == -1);
+ comptime try expect(S.blah(-10).? == -1);
+}
+
+test "*const ?[*]const T to [*c]const [*c]const T" {
+ var array = [_]u8{ 'o', 'k' };
+ const opt_array_ptr: ?[*]const u8 = &array;
+ const a: *const ?[*]const u8 = &opt_array_ptr;
+ const b: [*c]const [*c]const u8 = a;
+ try expect(b.*[0] == 'o');
+ try expect(b[0][1] == 'k');
+}
diff --git a/test/behavior/int128.zig b/test/behavior/int128.zig
new file mode 100644
index 0000000000..444096ff18
--- /dev/null
+++ b/test/behavior/int128.zig
@@ -0,0 +1,43 @@
+const std = @import("std");
+const expect = std.testing.expect;
+const maxInt = std.math.maxInt;
+const minInt = std.math.minInt;
+
+test "uint128" {
+ var buff: u128 = maxInt(u128);
+ try expect(buff == maxInt(u128));
+
+ const magic_const = 0x12341234123412341234123412341234;
+ buff = magic_const;
+
+ try expect(buff == magic_const);
+ try expect(magic_const == 0x12341234123412341234123412341234);
+
+ buff = 0;
+ try expect(buff == @as(u128, 0));
+}
+
+test "undefined 128 bit int" {
+ @setRuntimeSafety(true);
+
+ var undef: u128 = undefined;
+ var undef_signed: i128 = undefined;
+ try expect(undef == 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa and @bitCast(u128, undef_signed) == undef);
+}
+
+test "int128" {
+ var buff: i128 = -1;
+ try expect(buff < 0 and (buff + 1) == 0);
+ try expect(@intCast(i8, buff) == @as(i8, -1));
+
+ buff = minInt(i128);
+ try expect(buff < 0);
+
+ // This should be uncommented once wrapping arithmetic is implemented for 128 bit ints:
+ // try expect(buff < 0 and (buff -% 1) > 0)
+}
+
+test "truncate int128" {
+ var buff: u128 = maxInt(u128);
+ try expect(@truncate(u64, buff) == maxInt(u64));
+} \ No newline at end of file
diff --git a/test/behavior/pointers.zig b/test/behavior/pointers.zig
index 69f9e2af2a..32b88a2522 100644
--- a/test/behavior/pointers.zig
+++ b/test/behavior/pointers.zig
@@ -61,12 +61,16 @@ test "initialize const optional C pointer to null" {
test "assigning integer to C pointer" {
var x: i32 = 0;
+ var y: i32 = 1;
var ptr: [*c]u8 = 0;
var ptr2: [*c]u8 = x;
- if (false) {
- ptr;
- ptr2;
- }
+ var ptr3: [*c]u8 = 1;
+ var ptr4: [*c]u8 = y;
+
+ try expect(ptr == ptr2);
+ try expect(ptr3 == ptr4);
+ try expect(ptr3 > ptr and ptr4 > ptr2 and y > x);
+ try expect(1 > ptr and y > ptr2 and 0 < ptr3 and x < ptr4);
}
test "C pointer comparison and arithmetic" {