added dupe method

This commit is contained in:
Moritz Gmeiner 2025-04-14 16:34:33 +02:00
commit cdcf63d314
2 changed files with 42 additions and 1 deletions

View file

@ -36,3 +36,35 @@ test "heap" {
try std.testing.expectEqual(s.len, str.len());
try std.testing.expectEqualStrings(s, str.str());
}
test "dupe inline" {
const alloc = std.testing.allocator;
{
const s = "abcd";
const str1 = try SmolStr.init(alloc, s);
defer str1.deinit(alloc);
const str2 = try str1.dupe(alloc);
defer str2.deinit(alloc);
try std.testing.expectEqualSlices(u8, std.mem.asBytes(&str1), std.mem.asBytes(&str2));
}
{
const s = "abcdefghijklmnopqrstuvwxyz";
const str1 = try SmolStr.init(alloc, s);
defer str1.deinit(alloc);
const str2 = try str1.dupe(alloc);
defer str2.deinit(alloc);
// try std.testing.expectEqualSlices(u8, std.mem.asBytes(&str1), std.mem.asBytes(&str2));
try std.testing.expectEqualStrings(str1.str(), str2.str());
try std.testing.expect(str1.inner.heap.ptr != str2.inner.heap.ptr);
}
}