zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit f72a0a290735636775f83eaebf689357bee50778 (tree)
parent 97063efb69d11bfe7210555948b1f59ab6fb9d08
Author: Jakub Konka <kubkon@jakubkonka.com>
Date:   Mon, 17 Jan 2022 12:29:11 +0100

Merge branch 'Jarred-Sumner-patch-1'

Diffstat:
Mlib/std/multi_array_list.zig | 21++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig @@ -188,7 +188,7 @@ pub fn MultiArrayList(comptime S: type) type { /// after and including the specified index back by one and /// sets the given index to the specified element. May reallocate /// and invalidate iterators. - pub fn insert(self: *Self, gpa: Allocator, index: usize, elem: S) void { + pub fn insert(self: *Self, gpa: Allocator, index: usize, elem: S) !void { try self.ensureUnusedCapacity(gpa, 1); self.insertAssumeCapacity(index, elem); } @@ -602,3 +602,22 @@ test "ensure capacity on empty list" { try testing.expectEqualSlices(u32, &[_]u32{ 9, 11 }, list.items(.a)); try testing.expectEqualSlices(u8, &[_]u8{ 10, 12 }, list.items(.b)); } + +test "insert elements" { + const ally = testing.allocator; + + const Foo = struct { + a: u8, + b: u32, + }; + + var list = MultiArrayList(Foo){}; + defer list.deinit(ally); + + try list.insert(ally, 0, .{ .a = 1, .b = 2 }); + try list.ensureUnusedCapacity(ally, 1); + list.insertAssumeCapacity(1, .{ .a = 2, .b = 3 }); + + try testing.expectEqualSlices(u8, &[_]u8{ 1, 2 }, list.items(.a)); + try testing.expectEqualSlices(u32, &[_]u32{ 2, 3 }, list.items(.b)); +}