diff --git a/src/util/trackedString.zig b/src/util/trackedString.zig index 0c9a364..b015bdf 100644 --- a/src/util/trackedString.zig +++ b/src/util/trackedString.zig @@ -6,7 +6,7 @@ pub const TrackedString = struct { pub fn initAlloc(value: []const u8, allocator: std.mem.Allocator) !TrackedString { return .{ - .data = try allocator.dupe(value), + .data = try allocator.dupe(u8, value), .kind = .{ .Allocated = allocator }, }; } @@ -40,4 +40,42 @@ pub const AllocKind = union(enum) { Constant: void, Allocated: std.mem.Allocator, Dead: void, + + fn eql(self: AllocKind, other: AllocKind) bool { + return switch (self) { + .Constant => switch (other) { + .Constant => true, + else => false, + }, + .Allocated => |a| switch (other) { + .Allocated => |b| std.meta.eql(a, b), + else => false, + }, + .Dead => switch (other) { + .Dead => true, + else => false, + }, + }; + } }; + +const t = std.testing; + +test "the different kinds work" { + const a = t.allocator; + + var strOne = try TrackedString.initAlloc("hello, world", a); + defer strOne.deinit(); + + try t.expectEqualStrings("hello, world", strOne.data); + try t.expectEqual(AllocKind{ .Allocated = a }, strOne.kind); + + var strTwo = TrackedString.initConst("hello, world"); + defer strTwo.deinit(); + + try t.expectEqualStrings("hello, world", strTwo.data); + try t.expectEqual(AllocKind{ .Constant = {} }, strTwo.kind); + + try t.expectEqualStrings(strOne.data, strTwo.data); + try t.expect(!strOne.kind.eql(strTwo.kind)); +} diff --git a/src/util/utils.zig b/src/util/utils.zig index 06e1560..4e5a491 100644 --- a/src/util/utils.zig +++ b/src/util/utils.zig @@ -1 +1,10 @@ pub const str = @import("trackedString.zig"); + +comptime { + const std = @import("std"); + const builtin = @import("builtin"); + + if (builtin.is_test) { + std.mem.doNotOptimizeAway(str); + } +}