diff options
| author | Loris Cro <kappaloris@gmail.com> | 2023-06-18 09:06:40 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-18 09:06:40 +0200 |
| commit | 216ef10dc471e4db60a30208be178d6c59efeaaf (patch) | |
| tree | 8c239dab283ae9cb3b7fe099bae240bcc53f894e /test/behavior/slice.zig | |
| parent | 0fc1d396495c1ab482197021dedac8bea3f9401c (diff) | |
| parent | 729a051e9e38674233190aea23c0ac8c134f2d67 (diff) | |
| download | zig-216ef10dc471e4db60a30208be178d6c59efeaaf.tar.gz zig-216ef10dc471e4db60a30208be178d6c59efeaaf.zip | |
Merge branch 'master' into autodoc-searchkey
Diffstat (limited to 'test/behavior/slice.zig')
| -rw-r--r-- | test/behavior/slice.zig | 225 |
1 files changed, 178 insertions, 47 deletions
diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig index 237ddd195b..ae5fbf0951 100644 --- a/test/behavior/slice.zig +++ b/test/behavior/slice.zig @@ -29,6 +29,7 @@ comptime { test "slicing" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var array: [20]i32 = undefined; @@ -64,6 +65,7 @@ test "comptime slice of undefined pointer of length 0" { test "implicitly cast array of size 0 to slice" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var msg = [_]u8{}; try assertLenIsZero(&msg); @@ -85,7 +87,7 @@ test "access len index of sentinel-terminated slice" { } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "comptime slice of slice preserves comptime var" { @@ -120,6 +122,7 @@ test "slice of type" { test "generic malloc free" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const a = memAlloc(u8, 10) catch unreachable; memFree(u8, a); @@ -136,7 +139,7 @@ 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); + try comptime expect(@TypeOf(pointer) == *[2]u8); const slice: []const u8 = pointer; try expect(@ptrToInt(slice.ptr) == 4); try expect(slice.len == 2); @@ -171,6 +174,7 @@ test "comptime pointer cast array and then slice" { test "slicing zero length array" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const s1 = ""[0..]; const s2 = ([_]u32{})[0..]; @@ -180,6 +184,19 @@ test "slicing zero length array" { try expect(mem.eql(u32, s2, &[_]u32{})); } +test "slicing pointer by length" { + if (builtin.zig_backend == .stage2_spirv64) 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" { @@ -197,13 +214,14 @@ test "slice string literal has correct type" { try expect(@TypeOf(array[0..]) == *const [4]i32); } var runtime_zero: usize = 0; - comptime try expect(@TypeOf("aoeu"[runtime_zero..]) == [:0]const u8); + try comptime expect(@TypeOf("aoeu"[runtime_zero..]) == [:0]const u8); const array = [_]i32{ 1, 2, 3, 4 }; - comptime try expect(@TypeOf(array[runtime_zero..]) == []const i32); + try comptime expect(@TypeOf(array[runtime_zero..]) == []const i32); } test "result location zero sized array inside struct field implicit cast to slice" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const E = struct { entries: []u32, @@ -216,6 +234,7 @@ test "runtime safety lets us slice from len..len" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var an_array = [_]u8{ 1, 2, 3 }; try expect(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), "")); @@ -226,8 +245,9 @@ fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 { } test "C pointer" { - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var buf: [*c]const u8 = "kjdhfkjdhfdkjhfkfjhdfkjdhfkdjhfdkjhf"; var len: u32 = 10; @@ -239,13 +259,14 @@ test "C pointer slice access" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var buf: [10]u32 = [1]u32{42} ** 10; const c_ptr = @ptrCast([*c]const u32, &buf); var runtime_zero: usize = 0; - comptime try expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1])); - comptime try expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1])); + try comptime expectEqual([]const u32, @TypeOf(c_ptr[runtime_zero..1])); + try comptime expectEqual(*const [1]u32, @TypeOf(c_ptr[0..1])); for (c_ptr[0..5]) |*cl| { try expect(@as(u32, 42) == cl.*); @@ -268,6 +289,7 @@ fn sliceSum(comptime q: []const u8) i32 { test "slice type with custom alignment" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const LazilyResolvedType = struct { anything: i32, @@ -281,6 +303,7 @@ test "slice type with custom alignment" { test "obtaining a null terminated slice" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // here we have a normal array var buf: [50]u8 = undefined; @@ -297,10 +320,10 @@ test "obtaining a null terminated slice" { var runtime_len: usize = 3; const ptr2 = buf[0..runtime_len :0]; // ptr2 is a null-terminated slice - comptime try expect(@TypeOf(ptr2) == [:0]u8); - comptime try expect(@TypeOf(ptr2[0..2]) == *[2]u8); + try comptime expect(@TypeOf(ptr2) == [:0]u8); + try comptime expect(@TypeOf(ptr2[0..2]) == *[2]u8); var runtime_zero: usize = 0; - comptime try expect(@TypeOf(ptr2[runtime_zero..2]) == []u8); + try comptime expect(@TypeOf(ptr2[runtime_zero..2]) == []u8); } test "empty array to slice" { @@ -317,12 +340,13 @@ test "empty array to slice" { }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "@ptrCast slice to pointer" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn doTheTest() !void { @@ -334,13 +358,14 @@ test "@ptrCast slice to pointer" { }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "slice syntax resulting in pointer-to-array" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn doTheTest() !void { @@ -353,36 +378,44 @@ test "slice syntax resulting in pointer-to-array" { try testPointer0(); try testPointerAlign(); try testSlice(); + try testSliceZ(); try testSliceOpt(); try testSliceAlign(); + try testConcatStrLiterals(); + try testSliceLength(); + try testSliceLengthZ(); + try testArrayLength(); + try testArrayLengthZ(); + try testMultiPointer(); + try testMultiPointerLengthZ(); } fn testArray() !void { var array = [5]u8{ 1, 2, 3, 4, 5 }; var slice = array[1..3]; - comptime try expect(@TypeOf(slice) == *[2]u8); + try comptime expect(@TypeOf(slice) == *[2]u8); try expect(slice[0] == 2); try expect(slice[1] == 3); } fn testArrayZ() !void { var array = [5:0]u8{ 1, 2, 3, 4, 5 }; - comptime try expect(@TypeOf(array[1..3]) == *[2]u8); - comptime try expect(@TypeOf(array[1..5]) == *[4:0]u8); - comptime try expect(@TypeOf(array[1..]) == *[4:0]u8); - comptime try expect(@TypeOf(array[1..3 :4]) == *[2:4]u8); + try comptime expect(@TypeOf(array[1..3]) == *[2]u8); + try comptime expect(@TypeOf(array[1..5]) == *[4:0]u8); + try comptime expect(@TypeOf(array[1..]) == *[4:0]u8); + try comptime expect(@TypeOf(array[1..3 :4]) == *[2:4]u8); } fn testArray0() !void { { var array = [0]u8{}; var slice = array[0..0]; - comptime try expect(@TypeOf(slice) == *[0]u8); + try comptime expect(@TypeOf(slice) == *[0]u8); } { var array = [0:0]u8{}; var slice = array[0..0]; - comptime try expect(@TypeOf(slice) == *[0:0]u8); + try comptime expect(@TypeOf(slice) == *[0:0]u8); try expect(slice[0] == 0); } } @@ -390,16 +423,16 @@ test "slice syntax resulting in pointer-to-array" { fn testArrayAlign() !void { var array align(4) = [5]u8{ 1, 2, 3, 4, 5 }; var slice = array[4..5]; - comptime try expect(@TypeOf(slice) == *align(4) [1]u8); + try comptime expect(@TypeOf(slice) == *align(4) [1]u8); try expect(slice[0] == 5); - comptime try expect(@TypeOf(array[0..2]) == *align(4) [2]u8); + try comptime expect(@TypeOf(array[0..2]) == *align(4) [2]u8); } fn testPointer() !void { var array = [5]u8{ 1, 2, 3, 4, 5 }; var pointer: [*]u8 = &array; var slice = pointer[1..3]; - comptime try expect(@TypeOf(slice) == *[2]u8); + try comptime expect(@TypeOf(slice) == *[2]u8); try expect(slice[0] == 2); try expect(slice[1] == 3); } @@ -407,14 +440,14 @@ test "slice syntax resulting in pointer-to-array" { fn testPointerZ() !void { var array = [5:0]u8{ 1, 2, 3, 4, 5 }; var pointer: [*:0]u8 = &array; - comptime try expect(@TypeOf(pointer[1..3]) == *[2]u8); - comptime try expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8); + try comptime expect(@TypeOf(pointer[1..3]) == *[2]u8); + try comptime expect(@TypeOf(pointer[1..3 :4]) == *[2:4]u8); } fn testPointer0() !void { var pointer: [*]const u0 = &[1]u0{0}; var slice = pointer[0..1]; - comptime try expect(@TypeOf(slice) == *const [1]u0); + try comptime expect(@TypeOf(slice) == *const [1]u0); try expect(slice[0] == 0); } @@ -422,16 +455,16 @@ test "slice syntax resulting in pointer-to-array" { var array align(4) = [5]u8{ 1, 2, 3, 4, 5 }; var pointer: [*]align(4) u8 = &array; var slice = pointer[4..5]; - comptime try expect(@TypeOf(slice) == *align(4) [1]u8); + try comptime expect(@TypeOf(slice) == *align(4) [1]u8); try expect(slice[0] == 5); - comptime try expect(@TypeOf(pointer[0..2]) == *align(4) [2]u8); + try comptime expect(@TypeOf(pointer[0..2]) == *align(4) [2]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 try expect(@TypeOf(slice) == *[2]u8); + try comptime expect(@TypeOf(slice) == *[2]u8); try expect(slice[0] == 2); try expect(slice[1] == 3); } @@ -439,41 +472,107 @@ test "slice syntax resulting in pointer-to-array" { fn testSliceZ() !void { var array = [5:0]u8{ 1, 2, 3, 4, 5 }; var slice: [:0]u8 = &array; - comptime try expect(@TypeOf(slice[1..3]) == *[2]u8); - comptime try expect(@TypeOf(slice[1..]) == [:0]u8); - comptime try expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8); + try comptime expect(@TypeOf(slice[1..3]) == *[2]u8); + try comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8); + if (@inComptime()) { + try comptime expect(@TypeOf(slice[1..]) == *[4:0]u8); + } else { + try comptime expect(@TypeOf(slice[1..]) == [:0]u8); + } } fn testSliceOpt() !void { var array: [2]u8 = [2]u8{ 1, 2 }; var slice: ?[]u8 = &array; - comptime try expect(@TypeOf(&array, slice) == ?[]u8); - comptime try expect(@TypeOf(slice, &array) == ?[]u8); - comptime try expect(@TypeOf(slice.?[0..2]) == *[2]u8); + try comptime expect(@TypeOf(&array, slice) == ?[]u8); + try comptime expect(@TypeOf(slice, &array) == ?[]u8); + try comptime expect(@TypeOf(slice.?[0..2]) == *[2]u8); } fn testSliceAlign() !void { var array align(4) = [5]u8{ 1, 2, 3, 4, 5 }; var src_slice: []align(4) u8 = &array; var slice = src_slice[4..5]; - comptime try expect(@TypeOf(slice) == *align(4) [1]u8); + try comptime expect(@TypeOf(slice) == *align(4) [1]u8); try expect(slice[0] == 5); - comptime try expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8); + try comptime expect(@TypeOf(src_slice[0..2]) == *align(4) [2]u8); } fn testConcatStrLiterals() !void { - try expectEqualSlices("a"[0..] ++ "b"[0..], "ab"); - try expectEqualSlices("a"[0.. :0] ++ "b"[0.. :0], "ab"); + try expectEqualSlices(u8, "ab", "a"[0..] ++ "b"[0..]); + try expectEqualSlices(u8, "ab", "a"[0.. :0] ++ "b"[0.. :0]); + } + + fn testSliceLength() !void { + var array = [5]u8{ 1, 2, 3, 4, 5 }; + var slice: []u8 = &array; + try comptime expect(@TypeOf(slice[1..][0..2]) == *[2]u8); + try comptime expect(@TypeOf(slice[1..][0..4]) == *[4]u8); + try comptime 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; + try comptime expect(@TypeOf(slice[1..][0..2]) == *[2]u8); + try comptime expect(@TypeOf(slice[1..][0..2 :4]) == *[2:4]u8); + try comptime expect(@TypeOf(slice[1.. :0][0..2]) == *[2]u8); + try comptime expect(@TypeOf(slice[1.. :0][0..2 :4]) == *[2:4]u8); + } + + fn testArrayLength() !void { + var array = [5]u8{ 1, 2, 3, 4, 5 }; + try comptime expect(@TypeOf(array[1..][0..2]) == *[2]u8); + try comptime expect(@TypeOf(array[1..][0..4]) == *[4]u8); + try comptime expect(@TypeOf(array[1..][0..2 :4]) == *[2:4]u8); + } + + fn testArrayLengthZ() !void { + var array = [5:0]u8{ 1, 2, 3, 4, 5 }; + try comptime expect(@TypeOf(array[1..][0..2]) == *[2]u8); + try comptime expect(@TypeOf(array[1..][0..4]) == *[4:0]u8); + try comptime expect(@TypeOf(array[1..][0..2 :4]) == *[2:4]u8); + try comptime expect(@TypeOf(array[1.. :0][0..2]) == *[2]u8); + try comptime expect(@TypeOf(array[1.. :0][0..4]) == *[4:0]u8); + try comptime 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; + try comptime expect(@TypeOf(ptr[1..][0..2]) == *[2]u8); + try comptime expect(@TypeOf(ptr[1..][0..4]) == *[4]u8); + try comptime 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; + try comptime expect(@TypeOf(ptr[1..][0..2]) == *[2]u8); + try comptime expect(@TypeOf(ptr[1..][0..4]) == *[4]u8); + try comptime expect(@TypeOf(ptr[1..][0..2 :4]) == *[2:4]u8); + try comptime expect(@TypeOf(ptr[1.. :0][0..2]) == *[2]u8); + try comptime expect(@TypeOf(ptr[1.. :0][0..4]) == *[4]u8); + try comptime expect(@TypeOf(ptr[1.. :0][0..2 :4]) == *[2:4]u8); + + var ptr_z: [*:0]u8 = &array; + try comptime expect(@TypeOf(ptr_z[1..][0..2]) == *[2]u8); + try comptime expect(@TypeOf(ptr_z[1..][0..4]) == *[4]u8); + try comptime expect(@TypeOf(ptr_z[1..][0..2 :4]) == *[2:4]u8); + try comptime expect(@TypeOf(ptr_z[1.. :0][0..2]) == *[2]u8); + try comptime expect(@TypeOf(ptr_z[1.. :0][0..4]) == *[4]u8); + try comptime expect(@TypeOf(ptr_z[1.. :0][0..2 :4]) == *[2:4]u8); } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "slice pointer-to-array null terminated" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; comptime { var array = [5:0]u8{ 1, 2, 3, 4, 5 }; @@ -485,13 +584,14 @@ test "slice pointer-to-array null terminated" { var array = [5:0]u8{ 1, 2, 3, 4, 5 }; var slice: [:0]u8 = &array; - comptime try expect(@TypeOf(slice[1..3]) == *[2]u8); - comptime try expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8); - comptime try expect(@TypeOf(slice[1..]) == [:0]u8); + try comptime expect(@TypeOf(slice[1..3]) == *[2]u8); + try comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8); + try comptime expect(@TypeOf(slice[1..]) == [:0]u8); } test "slice pointer-to-array zero length" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; comptime { { @@ -512,13 +612,13 @@ test "slice pointer-to-array zero length" { var array = [0]u8{}; var src_slice: []u8 = &array; var slice = src_slice[0..0]; - comptime try expect(@TypeOf(slice) == *[0]u8); + try comptime expect(@TypeOf(slice) == *[0]u8); } { var array = [0:0]u8{}; var src_slice: [:0]u8 = &array; var slice = src_slice[0..0]; - comptime try expect(@TypeOf(slice) == *[0]u8); + try comptime expect(@TypeOf(slice) == *[0]u8); } } @@ -526,6 +626,7 @@ test "type coercion of pointer to anon struct literal to pointer to slice" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { const U = union { @@ -554,7 +655,7 @@ test "type coercion of pointer to anon struct literal to pointer to slice" { } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "array concat of slices gives ptr to array" { @@ -613,6 +714,7 @@ test "slice sentinel access at comptime" { test "slicing array with sentinel as end index" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn do() !void { @@ -625,12 +727,13 @@ test "slicing array with sentinel as end index" { }; try S.do(); - comptime try S.do(); + try comptime S.do(); } test "slicing slice with sentinel as end index" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn do() !void { @@ -644,7 +747,7 @@ test "slicing slice with sentinel as end index" { }; try S.do(); - comptime try S.do(); + try comptime S.do(); } test "slice len modification at comptime" { @@ -659,6 +762,8 @@ test "slice len modification at comptime" { } test "slice field ptr const" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + const const_slice: []const u8 = "string"; const const_ptr_const_slice = &const_slice; @@ -672,6 +777,7 @@ test "slice field ptr const" { test "slice field ptr var" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var var_slice: []const u8 = "string"; @@ -688,6 +794,7 @@ test "global slice field access" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { var slice: []const u8 = undefined; @@ -699,6 +806,8 @@ test "global slice field access" { } test "slice of void" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + var n: usize = 10; var arr: [12]void = undefined; const slice = @as([]void, &arr)[0..n]; @@ -706,6 +815,8 @@ test "slice of void" { } test "slice with dereferenced value" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + var a: usize = 0; var idx: *usize = &a; _ = blk: { @@ -721,6 +832,7 @@ test "slice with dereferenced value" { test "empty slice ptr is non null" { if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; { const empty_slice: []u8 = &[_]u8{}; @@ -739,6 +851,7 @@ test "empty slice ptr is non null" { test "slice decays to many pointer" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; var buf: [8]u8 = "abcdefg\x00".*; const p: [*:0]const u8 = buf[0..7 :0]; @@ -749,6 +862,7 @@ test "write through pointer to optional slice arg" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn bar(foo: *?[]const u8) !void { @@ -763,3 +877,20 @@ test "write through pointer to optional slice arg" { try S.bar(&foo); try expectEqualStrings(foo.?, "ok"); } + +test "modify slice length at comptime" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const arr: [2]u8 = .{ 10, 20 }; + comptime var s: []const u8 = arr[0..0]; + s.len += 1; + const a = s; + s.len += 1; + const b = s; + + try expectEqualSlices(u8, &.{10}, a); + try expectEqualSlices(u8, &.{ 10, 20 }, b); +} |
