aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-03-19 10:54:20 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-03-19 10:54:20 -0400
commitf824658e136738ea75c8bb3b53d9a67b2c4402b7 (patch)
treeb4dfb95c0146e5d2c8f34110a550e98c2a766609 /test
parent61266d26212e89e97d285eb61416d625303704bc (diff)
downloadzig-f824658e136738ea75c8bb3b53d9a67b2c4402b7.tar.gz
zig-f824658e136738ea75c8bb3b53d9a67b2c4402b7.zip
slicing sentinel-terminated slice without end
now results in a sentinel-terminated slice.
Diffstat (limited to 'test')
-rw-r--r--test/stage1/behavior/slice.zig79
1 files changed, 79 insertions, 0 deletions
diff --git a/test/stage1/behavior/slice.zig b/test/stage1/behavior/slice.zig
index 26ad8425f2..203a3b72d3 100644
--- a/test/stage1/behavior/slice.zig
+++ b/test/stage1/behavior/slice.zig
@@ -130,3 +130,82 @@ test "empty array to slice" {
S.doTheTest();
comptime S.doTheTest();
}
+
+test "@ptrCast slice to pointer" {
+ const S = struct {
+ fn doTheTest() void {
+ var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };
+ var slice: []u8 = &array;
+ var ptr = @ptrCast(*u16, slice);
+ expect(ptr.* == 65535);
+ }
+ };
+
+ S.doTheTest();
+ comptime S.doTheTest();
+}
+
+test "slicing producing an array" {
+ const S = struct {
+ fn doTheTest() void {
+ testArray();
+ testArrayZ();
+ testPointer();
+ testPointerZ();
+ testSlice();
+ testSliceZ();
+ }
+
+ fn testArray() void {
+ var array = [5]u8{ 1, 2, 3, 4, 5 };
+ var slice = array[1..3];
+ comptime expect(@TypeOf(slice) == *[2]u8);
+ expect(slice[0] == 2);
+ expect(slice[1] == 3);
+ }
+
+ fn testArrayZ() void {
+ var array = [5:0]u8{ 1, 2, 3, 4, 5 };
+ comptime expect(@TypeOf(array[1..3]) == *[2]u8);
+ comptime expect(@TypeOf(array[1..5]) == *[4:0]u8);
+ comptime expect(@TypeOf(array[1..]) == *[4:0]u8);
+ comptime expect(@TypeOf(array[1..3 :4]) == *[2:4]u8);
+ }
+
+ fn testPointer() void {
+ var array = [5]u8{ 1, 2, 3, 4, 5 };
+ var pointer: [*]u8 = &array;
+ var slice = pointer[1..3];
+ comptime expect(@TypeOf(slice) == *[2]u8);
+ expect(slice[0] == 2);
+ expect(slice[1] == 3);
+ }
+
+ fn testPointerZ() void {
+ var array = [5:0]u8{ 1, 2, 3, 4, 5 };
+ var pointer: [*:0]u8 = &array;
+ comptime expect(@TypeOf(pointer[1..3]) == *[2]u8);
+ comptime expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8);
+ }
+
+ fn testSlice() void {
+ var array = [5]u8{ 1, 2, 3, 4, 5 };
+ var src_slice: []u8 = &array;
+ var slice = src_slice[1..3];
+ comptime expect(@TypeOf(slice) == *[2]u8);
+ expect(slice[0] == 2);
+ expect(slice[1] == 3);
+ }
+
+ fn testSliceZ() void {
+ var array = [5:0]u8{ 1, 2, 3, 4, 5 };
+ var slice: [:0]u8 = &array;
+ comptime expect(@TypeOf(slice[1..3]) == *[2]u8);
+ comptime expect(@TypeOf(slice[1..]) == [:0]u8);
+ comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
+ }
+ };
+
+ S.doTheTest();
+ comptime S.doTheTest();
+}