commit da8e8b89e0aa73d6036443954d0ec6280ebe2402 (tree)
parent 554734f9f81fb16201e1c3826c601d1a0a3d7c91
Author: John Schmidt <john.schmidt.h@gmail.com>
Date: Mon, 13 Dec 2021 01:33:22 +0100
Set len on copied items array
Also fix the argument order for `expectEquals`.
Diffstat:
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
@@ -110,8 +110,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
/// Creates a copy of this ArrayList, using the same allocator.
pub fn clone(self: *Self) !Self {
- const items_copy = try self.allocator.alloc(T, self.capacity);
+ var items_copy = try self.allocator.alloc(T, self.capacity);
mem.copy(T, items_copy, self.items);
+ items_copy.len = self.items.len;
return Self{
.items = items_copy,
.capacity = self.capacity,
@@ -502,8 +503,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
/// Creates a copy of this ArrayList.
pub fn clone(self: *Self, allocator: Allocator) !Self {
- const items_copy = try allocator.alloc(T, self.capacity);
+ var items_copy = try allocator.alloc(T, self.capacity);
mem.copy(T, items_copy, self.items);
+ items_copy.len = self.items.len;
return Self{
.items = items_copy,
.capacity = self.capacity,
@@ -838,14 +840,15 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {
while (i < array.items.len) : (i += 1) {
try testing.expectEqual(array.items[i], cloned.items[i]);
}
+ try testing.expectEqual(array.items.len, cloned.items.len);
try testing.expectEqual(array.capacity, cloned.capacity);
try testing.expectEqual(array.allocator, cloned.allocator);
array.deinit();
- try testing.expectEqual(cloned.items[0], -1);
- try testing.expectEqual(cloned.items[1], 3);
- try testing.expectEqual(cloned.items[2], 5);
+ try testing.expectEqual(@as(i32, -1), cloned.items[0]);
+ try testing.expectEqual(@as(i32, 3), cloned.items[1]);
+ try testing.expectEqual(@as(i32, 5), cloned.items[2]);
}
{
var array = ArrayListUnmanaged(i32){};
@@ -860,13 +863,14 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {
while (i < array.items.len) : (i += 1) {
try testing.expectEqual(array.items[i], cloned.items[i]);
}
+ try testing.expectEqual(array.items.len, cloned.items.len);
try testing.expectEqual(array.capacity, cloned.capacity);
array.deinit(a);
- try testing.expectEqual(cloned.items[0], -1);
- try testing.expectEqual(cloned.items[1], 3);
- try testing.expectEqual(cloned.items[2], 5);
+ try testing.expectEqual(@as(i32, -1), cloned.items[0]);
+ try testing.expectEqual(@as(i32, 3), cloned.items[1]);
+ try testing.expectEqual(@as(i32, 5), cloned.items[2]);
}
}