diff options
| author | dweiller <4678790+dweiller@users.noreplay.github.com> | 2023-05-03 12:51:40 +1000 |
|---|---|---|
| committer | dweiller <4678790+dweiller@users.noreplay.github.com> | 2023-05-07 15:55:21 +1000 |
| commit | e507f0c0aa2267f003ba2afb30448fc723d54418 (patch) | |
| tree | 8022d112bece2242c97662d0ca4111c36b851170 /test/behavior/slice.zig | |
| parent | cba9077cc12b953dd00369f9c0340d7667af1611 (diff) | |
| download | zig-e507f0c0aa2267f003ba2afb30448fc723d54418.tar.gz zig-e507f0c0aa2267f003ba2afb30448fc723d54418.zip | |
test: add behavior tests for pointer slice-by-length
Diffstat (limited to 'test/behavior/slice.zig')
| -rw-r--r-- | test/behavior/slice.zig | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig index 51538d9b10..575665cf7f 100644 --- a/test/behavior/slice.zig +++ b/test/behavior/slice.zig @@ -180,6 +180,17 @@ test "slicing zero length array" { try expect(mem.eql(u32, s2, &[_]u32{})); } +test "slicing pointer by length" { + 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" { @@ -503,6 +514,33 @@ test "slice syntax resulting in pointer-to-array" { 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(); |
