forked from ziglang/zig
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
behavior: add test coverage for slice and array-related issues
Closes ziglang#10684 Closes ziglang#6905 Closes ziglang#8646
- Loading branch information
Showing
4 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const std = @import("std"); | ||
const expectEqualStrings = std.testing.expectEqualStrings; | ||
|
||
test "slicing slices" { | ||
const foo = "1234"; | ||
const bar = foo[0..4]; | ||
try expectEqualStrings("1234", bar); | ||
try expectEqualStrings("2", bar[1..2]); | ||
try expectEqualStrings("3", bar[2..3]); | ||
try expectEqualStrings("4", bar[3..4]); | ||
try expectEqualStrings("34", bar[2..4]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const expect = @import("std").testing.expect; | ||
|
||
test "sentinel-terminated 0-length slices" { | ||
var u32s: [4]u32 = [_]u32{0, 1, 2, 3}; | ||
|
||
var index: u8 = 2; | ||
var slice = u32s[index..index:2]; | ||
var array_ptr = u32s[2..2:2]; | ||
const comptime_known_array_value = u32s[2..2:2].*; | ||
var runtime_array_value = u32s[2..2:2].*; | ||
|
||
try expect(slice[0] == 2); | ||
try expect(array_ptr[0] == 2); | ||
try expect(comptime_known_array_value[0] == 2); | ||
try expect(runtime_array_value[0] == 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const std = @import("std"); | ||
|
||
const array = [_][]const []const u8{ | ||
&.{ "hello" }, | ||
&.{ "world", "hello" }, | ||
}; | ||
|
||
test { | ||
try std.testing.expect(array[0].len == 1); | ||
try std.testing.expectEqualStrings("hello", array[0][0]); | ||
} |