aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2022-08-18 20:52:07 +0200
committerJulien Dessaux2022-08-18 20:52:07 +0200
commit800c61339432258e8dad0ca7c56ddface84cab87 (patch)
tree97a170c70284fa2c9bc2145f8925acb135cccbde
parentFixed bugs reported by the mycology test suite (diff)
downloadzigfunge98-800c61339432258e8dad0ca7c56ddface84cab87.tar.gz
zigfunge98-800c61339432258e8dad0ca7c56ddface84cab87.tar.bz2
zigfunge98-800c61339432258e8dad0ca7c56ddface84cab87.zip
Made the timestamp function mockable for testing purposes
-rw-r--r--src/interpreter.zig4
-rw-r--r--src/main.zig5
-rw-r--r--src/pointer.zig13
3 files changed, 15 insertions, 7 deletions
diff --git a/src/interpreter.zig b/src/interpreter.zig
index c378c11..9fb98a1 100644
--- a/src/interpreter.zig
+++ b/src/interpreter.zig
@@ -13,13 +13,13 @@ pub const Interpreter = struct {
self.field.deinit();
self.allocator.destroy(self);
}
- pub fn init(allocator: std.mem.Allocator, fileReader: anytype, args: []const []const u8, env: []const [*:0]const u8) !*Interpreter {
+ pub fn init(allocator: std.mem.Allocator, fileReader: anytype, timestamp: fn () i64, args: []const []const u8, env: []const [*:0]const u8) !*Interpreter {
var i = try allocator.create(Interpreter);
errdefer allocator.destroy(i);
i.allocator = allocator;
i.field = try field.Field.init_from_reader(allocator, fileReader);
errdefer i.field.deinit();
- i.pointer = try pointer.Pointer.init(std.testing.allocator, i.field, args, env);
+ i.pointer = try pointer.Pointer.init(std.testing.allocator, i.field, timestamp, args, env);
errdefer i.pointer.deinit();
return i;
}
diff --git a/src/main.zig b/src/main.zig
index 1608b87..4408283 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -16,13 +16,16 @@ pub fn main() anyerror!void {
defer file.close();
const env: []const [*:0]const u8 = std.os.environ;
- var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), args, env[0..]);
+ var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), std.time.timestamp, args, env[0..]);
defer i.deinit();
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
std.os.exit(@intCast(u8, try i.run(&ioContext)));
}
+fn testTimestamp() i64 {
+ return 1660681247;
+}
test "all" {
std.testing.refAllDecls(@This());
}
diff --git a/src/pointer.zig b/src/pointer.zig
index 2f13049..eedae33 100644
--- a/src/pointer.zig
+++ b/src/pointer.zig
@@ -26,6 +26,7 @@ pub const Pointer = struct {
env: []const [*:0]const u8,
argv: []const []const u8,
rand: *std.rand.Random,
+ timestamp: fn () i64,
pub fn deinit(self: *Pointer) void {
self.ss.deinit();
@@ -243,7 +244,7 @@ pub const Pointer = struct {
// 17
try p.ss.toss.push(@intCast(i64, p.ss.data.items.len) + 1);
// 16
- const now = std.time.epoch.EpochSeconds{ .secs = @intCast(u64, std.time.timestamp()) };
+ const now = std.time.epoch.EpochSeconds{ .secs = @intCast(u64, p.timestamp()) };
const epochDay = now.getEpochDay();
const daySeconds = now.getDaySeconds();
try p.ss.toss.push(@intCast(i64, daySeconds.getHoursIntoDay()) * 256 * 256 + @intCast(i64, daySeconds.getMinutesIntoHour()) * 256 + @intCast(i64, daySeconds.getSecondsIntoMinute()));
@@ -347,7 +348,7 @@ pub const Pointer = struct {
self.step();
return result;
}
- pub fn init(allocator: std.mem.Allocator, f: *field.Field, argv: []const []const u8, env: []const [*:0]const u8) !*Pointer {
+ pub fn init(allocator: std.mem.Allocator, f: *field.Field, timestamp: fn () i64, argv: []const []const u8, env: []const [*:0]const u8) !*Pointer {
var p = try allocator.create(Pointer);
errdefer allocator.destroy(p);
p.allocator = allocator;
@@ -368,6 +369,7 @@ pub const Pointer = struct {
try std.os.getrandom(std.mem.asBytes(&seed));
var prng = std.rand.DefaultPrng.init(seed);
p.rand = &prng.random();
+ p.timestamp = timestamp;
return p;
}
inline fn redirect(p: *Pointer, c: i64) bool {
@@ -429,6 +431,9 @@ pub const Pointer = struct {
}
};
+fn testTimestamp() i64 {
+ return 1660681247;
+}
test "all" {
std.testing.refAllDecls(@This());
}
@@ -438,7 +443,7 @@ test "minimal" {
defer f.deinit();
const argv = [_][]const u8{"minimal"};
const env = [_][*:0]const u8{"ENV=TEST"};
- var p = try Pointer.init(std.testing.allocator, f, argv[0..], env[0..]);
+ var p = try Pointer.init(std.testing.allocator, f, testTimestamp, argv[0..], env[0..]);
defer p.deinit();
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{});
@@ -449,7 +454,7 @@ test "almost minimal" {
defer f.deinit();
const argv = [_][]const u8{"minimal"};
const env = [_][*:0]const u8{"ENV=TEST"};
- var p = try Pointer.init(std.testing.allocator, f, argv[0..], env[0..]);
+ var p = try Pointer.init(std.testing.allocator, f, testTimestamp, argv[0..], env[0..]);
defer p.deinit();
var ioContext = io.context(std.io.getStdIn().reader(), std.io.getStdOut().writer());
try std.testing.expectEqual(p.exec(&ioContext), pointerReturn{});