Merge pull request #10034 from Snektron/stage2-slice
stage2: slice and optional improvements
This commit is contained in:
@@ -109,3 +109,60 @@ test "slice of type" {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test "generic malloc free" {
|
||||
const a = memAlloc(u8, 10) catch unreachable;
|
||||
memFree(u8, a);
|
||||
}
|
||||
var some_mem: [100]u8 = undefined;
|
||||
fn memAlloc(comptime T: type, n: usize) anyerror![]T {
|
||||
return @ptrCast([*]T, &some_mem[0])[0..n];
|
||||
}
|
||||
fn memFree(comptime T: type, memory: []T) void {
|
||||
_ = memory;
|
||||
}
|
||||
|
||||
test "slice of hardcoded address to pointer" {
|
||||
const S = struct {
|
||||
fn doTheTest() !void {
|
||||
const pointer = @intToPtr([*]u8, 0x04)[0..2];
|
||||
comptime try expect(@TypeOf(pointer) == *[2]u8);
|
||||
const slice: []const u8 = pointer;
|
||||
try expect(@ptrToInt(slice.ptr) == 4);
|
||||
try expect(slice.len == 2);
|
||||
}
|
||||
};
|
||||
|
||||
try S.doTheTest();
|
||||
}
|
||||
|
||||
test "comptime slice of pointer preserves comptime var" {
|
||||
comptime {
|
||||
var buff: [10]u8 = undefined;
|
||||
var a = @ptrCast([*]u8, &buff);
|
||||
a[0..1][0] = 1;
|
||||
try expect(buff[0..][0..][0] == 1);
|
||||
}
|
||||
}
|
||||
|
||||
test "comptime pointer cast array and then slice" {
|
||||
const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
|
||||
|
||||
const ptrA: [*]const u8 = @ptrCast([*]const u8, &array);
|
||||
const sliceA: []const u8 = ptrA[0..2];
|
||||
|
||||
const ptrB: [*]const u8 = &array;
|
||||
const sliceB: []const u8 = ptrB[0..2];
|
||||
|
||||
try expect(sliceA[1] == 2);
|
||||
try expect(sliceB[1] == 2);
|
||||
}
|
||||
|
||||
test "slicing zero length array" {
|
||||
const s1 = ""[0..];
|
||||
const s2 = ([_]u32{})[0..];
|
||||
try expect(s1.len == 0);
|
||||
try expect(s2.len == 0);
|
||||
try expect(mem.eql(u8, s1, ""));
|
||||
try expect(mem.eql(u32, s2, &[_]u32{}));
|
||||
}
|
||||
|
||||
@@ -4,15 +4,6 @@ const expectEqualSlices = std.testing.expectEqualSlices;
|
||||
const expectEqual = std.testing.expectEqual;
|
||||
const mem = std.mem;
|
||||
|
||||
test "slicing zero length array" {
|
||||
const s1 = ""[0..];
|
||||
const s2 = ([_]u32{})[0..];
|
||||
try expect(s1.len == 0);
|
||||
try expect(s2.len == 0);
|
||||
try expect(mem.eql(u8, s1, ""));
|
||||
try expect(mem.eql(u32, s2, &[_]u32{}));
|
||||
}
|
||||
|
||||
test "slice string literal has correct type" {
|
||||
comptime {
|
||||
try expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);
|
||||
@@ -25,18 +16,6 @@ test "slice string literal has correct type" {
|
||||
comptime try expect(@TypeOf(array[runtime_zero..]) == []const i32);
|
||||
}
|
||||
|
||||
test "generic malloc free" {
|
||||
const a = memAlloc(u8, 10) catch unreachable;
|
||||
memFree(u8, a);
|
||||
}
|
||||
var some_mem: [100]u8 = undefined;
|
||||
fn memAlloc(comptime T: type, n: usize) anyerror![]T {
|
||||
return @ptrCast([*]T, &some_mem[0])[0..n];
|
||||
}
|
||||
fn memFree(comptime T: type, memory: []T) void {
|
||||
_ = memory;
|
||||
}
|
||||
|
||||
test "result location zero sized array inside struct field implicit cast to slice" {
|
||||
const E = struct {
|
||||
entries: []u32,
|
||||
@@ -307,20 +286,6 @@ test "slice syntax resulting in pointer-to-array" {
|
||||
comptime try S.doTheTest();
|
||||
}
|
||||
|
||||
test "slice of hardcoded address to pointer" {
|
||||
const S = struct {
|
||||
fn doTheTest() !void {
|
||||
const pointer = @intToPtr([*]u8, 0x04)[0..2];
|
||||
comptime try expect(@TypeOf(pointer) == *[2]u8);
|
||||
const slice: []const u8 = pointer;
|
||||
try expect(@ptrToInt(slice.ptr) == 4);
|
||||
try expect(slice.len == 2);
|
||||
}
|
||||
};
|
||||
|
||||
try S.doTheTest();
|
||||
}
|
||||
|
||||
test "type coercion of pointer to anon struct literal to pointer to slice" {
|
||||
const S = struct {
|
||||
const U = union {
|
||||
@@ -352,15 +317,6 @@ test "type coercion of pointer to anon struct literal to pointer to slice" {
|
||||
comptime try S.doTheTest();
|
||||
}
|
||||
|
||||
test "comptime slice of pointer preserves comptime var" {
|
||||
comptime {
|
||||
var buff: [10]u8 = undefined;
|
||||
var a = @ptrCast([*]u8, &buff);
|
||||
a[0..1][0] = 1;
|
||||
try expect(buff[0..][0..][0] == 1);
|
||||
}
|
||||
}
|
||||
|
||||
test "array concat of slices gives slice" {
|
||||
comptime {
|
||||
var a: []const u8 = "aoeu";
|
||||
@@ -370,19 +326,6 @@ test "array concat of slices gives slice" {
|
||||
}
|
||||
}
|
||||
|
||||
test "comptime pointer cast array and then slice" {
|
||||
const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
|
||||
|
||||
const ptrA: [*]const u8 = @ptrCast([*]const u8, &array);
|
||||
const sliceA: []const u8 = ptrA[0..2];
|
||||
|
||||
const ptrB: [*]const u8 = &array;
|
||||
const sliceB: []const u8 = ptrB[0..2];
|
||||
|
||||
try expect(sliceA[1] == 2);
|
||||
try expect(sliceB[1] == 2);
|
||||
}
|
||||
|
||||
test "slice bounds in comptime concatenation" {
|
||||
const bs = comptime blk: {
|
||||
const b = "........1........";
|
||||
|
||||
12
test/behavior/slice_stage2.zig
Normal file
12
test/behavior/slice_stage2.zig
Normal file
@@ -0,0 +1,12 @@
|
||||
const std = @import("std");
|
||||
const expect = std.testing.expect;
|
||||
|
||||
const x = @intToPtr([*]i32, 0x1000)[0..0x500];
|
||||
const y = x[0x100..];
|
||||
test "compile time slice of pointer to hard coded address" {
|
||||
try expect(@ptrToInt(x) == 0x1000);
|
||||
try expect(x.len == 0x500);
|
||||
|
||||
try expect(@ptrToInt(y) == 0x1400);
|
||||
try expect(y.len == 0x400);
|
||||
}
|
||||
Reference in New Issue
Block a user