zig-smolstr/tests/root.zig

70 lines
1.8 KiB
Zig
Raw Normal View History

2025-04-07 22:23:32 +00:00
const std = @import("std");
2025-04-08 00:33:12 +02:00
const SmolStr = @import("smolstr").SmolStr;
test "inline" {
const alloc = std.testing.allocator;
const s = "abcdefghijklmnopqrstuvwxyz";
for (1..SmolStr.max_inline_len + 1) |i| {
const sub_s: []const u8 = s[0..i];
const str = try SmolStr.init(alloc, sub_s);
defer str.deinit(alloc);
try std.testing.expectEqual(i, str.inner.inl.len);
try std.testing.expectEqualStrings(s[0..i], str.inner.inl.data[0..str.inner.inl.len]);
try std.testing.expectEqual(i, str.len());
try std.testing.expectEqualStrings(s[0..i], str.str());
try std.testing.expectEqual(s[0], str.str()[0]);
}
}
test "heap" {
const alloc = std.testing.allocator;
const s = "abcdefghijklmnopqrstuvwxyz";
const str = try SmolStr.init(alloc, s);
defer str.deinit(alloc);
try std.testing.expectEqualStrings(s, str.inner.heap);
try std.testing.expectEqual(s.len, str.len());
try std.testing.expectEqualStrings(s, str.str());
}
2025-04-14 16:34:33 +02:00
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);
}
}