authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2021-12-13 01:33:22+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-01-27 19:35:08+02:00
logda8e8b89e0aa73d6036443954d0ec6280ebe2402
tree7e5671fa567f6f41a52d4ae2891a57888743ca44
parent554734f9f81fb16201e1c3826c601d1a0a3d7c91

Set len on copied items array

Also fix the argument order for `expectEquals`.

1 files changed, 12 insertions(+), 8 deletions(-)

lib/std/array_list.zig+12-8
......@@ -110,8 +110,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
110110
111111 /// Creates a copy of this ArrayList, using the same allocator.
112112 pub fn clone(self: *Self) !Self {
113 const items_copy = try self.allocator.alloc(T, self.capacity);
113 var items_copy = try self.allocator.alloc(T, self.capacity);
114114 mem.copy(T, items_copy, self.items);
115 items_copy.len = self.items.len;
115116 return Self{
116117 .items = items_copy,
117118 .capacity = self.capacity,
......@@ -502,8 +503,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
502503
503504 /// Creates a copy of this ArrayList.
504505 pub fn clone(self: *Self, allocator: Allocator) !Self {
505 const items_copy = try allocator.alloc(T, self.capacity);
506 var items_copy = try allocator.alloc(T, self.capacity);
506507 mem.copy(T, items_copy, self.items);
508 items_copy.len = self.items.len;
507509 return Self{
508510 .items = items_copy,
509511 .capacity = self.capacity,
......@@ -838,14 +840,15 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {
838840 while (i < array.items.len) : (i += 1) {
839841 try testing.expectEqual(array.items[i], cloned.items[i]);
840842 }
843 try testing.expectEqual(array.items.len, cloned.items.len);
841844 try testing.expectEqual(array.capacity, cloned.capacity);
842845 try testing.expectEqual(array.allocator, cloned.allocator);
843846
844847 array.deinit();
845848
846 try testing.expectEqual(cloned.items[0], -1);
847 try testing.expectEqual(cloned.items[1], 3);
848 try testing.expectEqual(cloned.items[2], 5);
849 try testing.expectEqual(@as(i32, -1), cloned.items[0]);
850 try testing.expectEqual(@as(i32, 3), cloned.items[1]);
851 try testing.expectEqual(@as(i32, 5), cloned.items[2]);
849852 }
850853 {
851854 var array = ArrayListUnmanaged(i32){};
......@@ -860,13 +863,14 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {
860863 while (i < array.items.len) : (i += 1) {
861864 try testing.expectEqual(array.items[i], cloned.items[i]);
862865 }
866 try testing.expectEqual(array.items.len, cloned.items.len);
863867 try testing.expectEqual(array.capacity, cloned.capacity);
864868
865869 array.deinit(a);
866870
867 try testing.expectEqual(cloned.items[0], -1);
868 try testing.expectEqual(cloned.items[1], 3);
869 try testing.expectEqual(cloned.items[2], 5);
871 try testing.expectEqual(@as(i32, -1), cloned.items[0]);
872 try testing.expectEqual(@as(i32, 3), cloned.items[1]);
873 try testing.expectEqual(@as(i32, 5), cloned.items[2]);
870874 }
871875}
872876