aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/slice.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behavior/slice.zig')
-rw-r--r--test/behavior/slice.zig57
1 files changed, 57 insertions, 0 deletions
diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig
index 19c9e7e773..0332cff802 100644
--- a/test/behavior/slice.zig
+++ b/test/behavior/slice.zig
@@ -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{}));
+}