aboutsummaryrefslogtreecommitdiff
path: root/test/behavior
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-05-11 08:59:44 -0700
committerGitHub <noreply@github.com>2023-05-11 08:59:44 -0700
commit7f7bd206dc8309ca767b47fb97bb9e7c2dc882c3 (patch)
tree2d24acb80954ec2d33863befa9ce48ecd3075e72 /test/behavior
parent5512455974a9dda5d2a86a81e4a5cc520cb7afa8 (diff)
parent4d296debefccbd80f2007685a27386b7434464dd (diff)
downloadzig-7f7bd206dc8309ca767b47fb97bb9e7c2dc882c3.tar.gz
zig-7f7bd206dc8309ca767b47fb97bb9e7c2dc882c3.zip
Merge pull request #15519 from dweiller/issue-15482
Optimize lowering of `s[start..][0..len]`
Diffstat (limited to 'test/behavior')
-rw-r--r--test/behavior/slice.zig77
1 files changed, 77 insertions, 0 deletions
diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig
index 237ddd195b..29ceb061c1 100644
--- a/test/behavior/slice.zig
+++ b/test/behavior/slice.zig
@@ -180,6 +180,18 @@ test "slicing zero length array" {
try expect(mem.eql(u32, s2, &[_]u32{}));
}
+test "slicing pointer by length" {
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+ const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
+ const ptr: [*]const u8 = @ptrCast([*]const u8, &array);
+ const slice = ptr[1..][0..5];
+ try expect(slice.len == 5);
+ var i: usize = 0;
+ while (i < slice.len) : (i += 1) {
+ try expect(slice[i] == i + 2);
+ }
+}
+
const x = @intToPtr([*]i32, 0x1000)[0..0x500];
const y = x[0x100..];
test "compile time slice of pointer to hard coded address" {
@@ -355,6 +367,10 @@ test "slice syntax resulting in pointer-to-array" {
try testSlice();
try testSliceOpt();
try testSliceAlign();
+ try testSliceLength();
+ try testSliceLengthZ();
+ try testArrayLength();
+ try testArrayLengthZ();
}
fn testArray() !void {
@@ -465,6 +481,67 @@ test "slice syntax resulting in pointer-to-array" {
try expectEqualSlices("a"[0..] ++ "b"[0..], "ab");
try expectEqualSlices("a"[0.. :0] ++ "b"[0.. :0], "ab");
}
+
+ fn testSliceLength() !void {
+ var array = [5]u8{ 1, 2, 3, 4, 5 };
+ var slice: []u8 = &array;
+ comptime try expect(@TypeOf(slice[1..][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(slice[1..][0..4]) == *[4]u8);
+ comptime try expect(@TypeOf(slice[1..][0..2 :4]) == *[2:4]u8);
+ }
+
+ fn testSliceLengthZ() !void {
+ var array = [5:0]u8{ 1, 2, 3, 4, 5 };
+ var slice: [:0]u8 = &array;
+ comptime try expect(@TypeOf(slice[1..][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(slice[1..][0..2 :4]) == *[2:4]u8);
+ comptime try expect(@TypeOf(slice[1.. :0][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(slice[1.. :0][0..2 :4]) == *[2:4]u8);
+ }
+
+ fn testArrayLength() !void {
+ var array = [5]u8{ 1, 2, 3, 4, 5 };
+ comptime try expect(@TypeOf(array[1..][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(array[1..][0..4]) == *[4]u8);
+ comptime try expect(@TypeOf(array[1..][0..2 :4]) == *[2:4]u8);
+ }
+
+ fn testArrayLengthZ() !void {
+ var array = [5:0]u8{ 1, 2, 3, 4, 5 };
+ comptime try expect(@TypeOf(array[1..][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(array[1..][0..4]) == *[4:0]u8);
+ comptime try expect(@TypeOf(array[1..][0..2 :4]) == *[2:4]u8);
+ comptime try expect(@TypeOf(array[1.. :0][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(array[1.. :0][0..4]) == *[4:0]u8);
+ comptime try expect(@TypeOf(array[1.. :0][0..2 :4]) == *[2:4]u8);
+ }
+
+ fn testMultiPointer() !void {
+ var array = [5]u8{ 1, 2, 3, 4, 5 };
+ var ptr: [*]u8 = &array;
+ comptime try expect(@TypeOf(ptr[1..][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(ptr[1..][0..4]) == *[4]u8);
+ comptime try expect(@TypeOf(ptr[1..][0..2 :4]) == *[2:4]u8);
+ }
+
+ fn testMultiPointerLengthZ() !void {
+ var array = [5:0]u8{ 1, 2, 3, 4, 5 };
+ var ptr: [*]u8 = &array;
+ comptime try expect(@TypeOf(ptr[1..][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(ptr[1..][0..4]) == *[4:0]u8);
+ comptime try expect(@TypeOf(ptr[1..][0..2 :4]) == *[2:4]u8);
+ comptime try expect(@TypeOf(ptr[1.. :0][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(ptr[1.. :0][0..4]) == *[4:0]u8);
+ comptime try expect(@TypeOf(ptr[1.. :0][0..2 :4]) == *[2:4]u8);
+
+ var ptr_z: [*:0]u8 = &array;
+ comptime try expect(@TypeOf(ptr_z[1..][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(ptr_z[1..][0..4]) == *[4:0]u8);
+ comptime try expect(@TypeOf(ptr_z[1..][0..2 :4]) == *[2:4]u8);
+ comptime try expect(@TypeOf(ptr_z[1.. :0][0..2]) == *[2]u8);
+ comptime try expect(@TypeOf(ptr_z[1.. :0][0..4]) == *[4:0]u8);
+ comptime try expect(@TypeOf(ptr_z[1.. :0][0..2 :4]) == *[2:4]u8);
+ }
};
try S.doTheTest();