#How to do TDD in zig?

1 messages · Page 1 of 1 (latest)

zenith tide
#

Hi, I want write my zig code and debug it while I write it. One of the things this cause is I do not want variables to be optimized away while I write them. Is there a way to ensure this with the build?

My file looks something like this.

const std = @import("std");

fn readEntireFile(filename: []const u8) !void {
    const file = try std.fs.cwd().openFile(filename, .{});
    const stat = try file.stat();
    _ = stat; // I include a breakpoint here in VScode to debug.
}

test {
    _ = try readEntireFile("data_1_flex.json");
}

The thing stat gets optimized away. How can I prevent this? Right now the only thing that worked was adding.

std.debug.print("{d}", .{stat.mode});

But I'd prefer not to.

zealous oak
#

what happens if you do _ = &stat;?

#

although part of the problem may be that there are no lines of code that produce runtime code after const stat

final dagger
#

there's also std.mem.doNotOptimizeAway

zenith tide
#

std.mem.doNotOptimizeAway didn't work :/

#

Huh, interesting. The _ = &stat; worked. Why?

zealous oak
#

Unused variables don't show up in the debugger, _ = &stat; references the address of the variable forcing it to be stored on the stack and be easily accessible by the debugger