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 {...@@ -110,8 +110,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
110110
111 /// Creates a copy of this ArrayList, using the same allocator.111 /// Creates a copy of this ArrayList, using the same allocator.
112 pub fn clone(self: *Self) !Self {112 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);
114 mem.copy(T, items_copy, self.items);114 mem.copy(T, items_copy, self.items);
115 items_copy.len = self.items.len;
115 return Self{116 return Self{
116 .items = items_copy,117 .items = items_copy,
117 .capacity = self.capacity,118 .capacity = self.capacity,
...@@ -502,8 +503,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ...@@ -502,8 +503,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
502503
503 /// Creates a copy of this ArrayList.504 /// Creates a copy of this ArrayList.
504 pub fn clone(self: *Self, allocator: Allocator) !Self {505 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);
506 mem.copy(T, items_copy, self.items);507 mem.copy(T, items_copy, self.items);
508 items_copy.len = self.items.len;
507 return Self{509 return Self{
508 .items = items_copy,510 .items = items_copy,
509 .capacity = self.capacity,511 .capacity = self.capacity,
...@@ -838,14 +840,15 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {...@@ -838,14 +840,15 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {
838 while (i < array.items.len) : (i += 1) {840 while (i < array.items.len) : (i += 1) {
839 try testing.expectEqual(array.items[i], cloned.items[i]);841 try testing.expectEqual(array.items[i], cloned.items[i]);
840 }842 }
843 try testing.expectEqual(array.items.len, cloned.items.len);
841 try testing.expectEqual(array.capacity, cloned.capacity);844 try testing.expectEqual(array.capacity, cloned.capacity);
842 try testing.expectEqual(array.allocator, cloned.allocator);845 try testing.expectEqual(array.allocator, cloned.allocator);
843846
844 array.deinit();847 array.deinit();
845848
846 try testing.expectEqual(cloned.items[0], -1);849 try testing.expectEqual(@as(i32, -1), cloned.items[0]);
847 try testing.expectEqual(cloned.items[1], 3);850 try testing.expectEqual(@as(i32, 3), cloned.items[1]);
848 try testing.expectEqual(cloned.items[2], 5);851 try testing.expectEqual(@as(i32, 5), cloned.items[2]);
849 }852 }
850 {853 {
851 var array = ArrayListUnmanaged(i32){};854 var array = ArrayListUnmanaged(i32){};
...@@ -860,13 +863,14 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {...@@ -860,13 +863,14 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {
860 while (i < array.items.len) : (i += 1) {863 while (i < array.items.len) : (i += 1) {
861 try testing.expectEqual(array.items[i], cloned.items[i]);864 try testing.expectEqual(array.items[i], cloned.items[i]);
862 }865 }
866 try testing.expectEqual(array.items.len, cloned.items.len);
863 try testing.expectEqual(array.capacity, cloned.capacity);867 try testing.expectEqual(array.capacity, cloned.capacity);
864868
865 array.deinit(a);869 array.deinit(a);
866870
867 try testing.expectEqual(cloned.items[0], -1);871 try testing.expectEqual(@as(i32, -1), cloned.items[0]);
868 try testing.expectEqual(cloned.items[1], 3);872 try testing.expectEqual(@as(i32, 3), cloned.items[1]);
869 try testing.expectEqual(cloned.items[2], 5);873 try testing.expectEqual(@as(i32, 5), cloned.items[2]);
870 }874 }
871}875}
872876