Add ListUnmanaged.print#2995
Conversation
| // we use the list as print destination, we want to make sure it retains normal list behavior | ||
| // by inserting a single element. | ||
| list.append(main.stackAllocator, '\n'); | ||
| list.print(main.stackAllocator, "BarFooSpam {}", .{main.vec.Vec2d{0.3, 0.4}}); |
There was a problem hiding this comment.
The way how more complex objects are printed is possibly fragile, and I don't think we should rely on it in tests.
Plus the dependence on Vec2d (which is going to change once Zig adds glsl-style vectors) is unnecessary.
There was a problem hiding this comment.
Ok, would a array be fine? I would want something more than just a single value tbh
There was a problem hiding this comment.
Why though, what do you intend to test with a composite type?
There was a problem hiding this comment.
Ok, yeah I guess it doesn't make that much sense. I was somewhat aiming to test if realloc works and if weirder things can still be printed properly.
What about I replace it with a test that checks if .initCapactiy preserves the items pointer?
I would also test if items pointer changes when not using .initCapacity.
There was a problem hiding this comment.
@@ -421,12 +421,28 @@ pub fn ListUnmanaged(comptime T: type) type {
};
}
+test "ListUnmanaged.print buffer preserved" {
+ var list: ListUnmanaged(u8) = .initCapacity(main.stackAllocator, 20);
+ const oldAddress = list.items.ptr;
+ defer list.deinit(main.stackAllocator);
+
+ list.print(main.stackAllocator, "foo {}", .{34});
+ const newAddress = list.items.ptr;
+
+ try std.testing.expect(oldAddress == newAddress);
+ try std.testing.expectEqualStrings("foo 34", list.items);
+ try std.testing.expect(list.items.len <= list.capacity);
+}
+
test "ListUnmanaged.print single call" {
var list: ListUnmanaged(u8) = .{};
+ const oldAddress = list.items.ptr;
defer list.deinit(main.stackAllocator);
- list.print(main.stackAllocator, "foo {}", .{34});
+ list.print(main.stackAllocator, "foo {d:.1}", .{34});
+ const newAddress = list.items.ptr;
+ try std.testing.expect(oldAddress != newAddress);
try std.testing.expectEqualStrings("foo 34", list.items);
try std.testing.expect(list.items.len <= list.capacity);
}75f0ff5 to
8aa164b
Compare
8aa164b to
53ff61e
Compare
<3 |
This pull request adds
printmethod toListUnmanagedstruct.To avoid copying a leftover TODO from
printfunction inList, it also makes an attempt on addressing it:https://github.com/Argmaster/Cubyz/blob/ListUnmanaged.print/src/utils/list.zig#L213
We might as well just leave it for different time tho
Resolves: #1425 (comment)