summaryrefslogtreecommitdiff
path: root/anim/test/recordings.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/test/recordings.zig')
-rw-r--r--anim/test/recordings.zig101
1 files changed, 101 insertions, 0 deletions
diff --git a/anim/test/recordings.zig b/anim/test/recordings.zig
new file mode 100644
index 0000000..ef97d31
--- /dev/null
+++ b/anim/test/recordings.zig
@@ -0,0 +1,101 @@
+//! canim on its real recordings (made by the build, under ztrace's
+//! debugger): finding moments, what a step changed, calls, pointers
+//! followed, and the cursor's timetable for a step.
+const std = @import("std");
+const canim = @import("canim");
+const anim = @import("anim");
+const recordings = @import("recordings");
+
+const Recording = canim.Recording;
+const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
+
+test "steps by line stop on lines of the program's own source, and no step stands still" {
+ inline for (.{ recordings.steps, recordings.hello, recordings.sort, recordings.list }) |program| {
+ const rec = canim.open(program);
+ var at = rec.call("main", 0).enter;
+ var stops: usize = 0;
+ while (at < rec.end() and stops < 400) : (stops += 1) {
+ const next = rec.next(.line, at);
+ try expect(next > at);
+ if (next < rec.end()) try expect(rec.reached[next]);
+ at = next;
+ }
+ try expect(stops > 4);
+ }
+}
+
+test "calls are a tree: fib(5) makes fifteen, each with what it was given and gave back" {
+ const rec = canim.open(recordings.steps);
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const nodes = try canim.calls.layout(arena.allocator(), rec, "fib");
+ try expectEqual(@as(usize, 15), nodes.len);
+ try expectEqual(@as(?u64, 5), rec.call("fib", 0).arg);
+ try expectEqual(@as(u64, 5), rec.call("fib", 0).result);
+ try expectEqual(@as(?u64, 1), rec.call("fib", 4).arg);
+ // A node is where it will always be: a parent over the middle of its children.
+ for (nodes) |n| if (n.parent) |p| try expect(nodes[p].level + 1 == n.level);
+}
+
+test "an instrumented program under the debugger tells its calls, variables and stores" {
+ const rec = canim.open(recordings.list);
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const reverse = rec.call("reverse", 0);
+ try expectEqual(@as(usize, 3), rec.allocations.len);
+ // The call begins where the debugger stopped on entering it: the line of its header.
+ try expectEqual(@as(u32, 17), rec.own[reverse.enter]);
+ // A local is not its value until its call has stored to it.
+ const first = rec.lineMoment(18, reverse.enter);
+ const cur = rec.variable(arena.allocator(), rec.moment(first), "cur").?;
+ try expect(!rec.assigned(cur, first));
+ try expect(rec.assigned(cur, rec.lineMoment(21, reverse.enter)));
+ // Three nodes are reached from the program's pointers, each in the place its allocation gives it.
+ const things = canim.objects.reach(arena.allocator(), rec, rec.moment(rec.lineMoment(21, reverse.enter)), null);
+ try expectEqual(@as(usize, 3), things.len);
+ for (things) |o| try expect(o.slot.? < 3);
+}
+
+test "the program's four system calls are found, with what each was given" {
+ const rec = canim.open(recordings.hello);
+ var names: [4][]const u8 = undefined;
+ var n: usize = 0;
+ for (rec.syscalls) |call| {
+ if (call.enter < rec.call("main", 0).enter or n == names.len) continue;
+ names[n] = @import("ztrace").syscalls.name(call.nr) orelse "?";
+ n += 1;
+ }
+ try expectEqual(@as(usize, 4), n);
+ try std.testing.expectEqualStrings("open", names[0]);
+ try std.testing.expectEqualStrings("read", names[1]);
+ try std.testing.expectEqualStrings("close", names[2]);
+ try std.testing.expectEqualStrings("write", names[3]);
+}
+
+test "a walked step is as long as what it changed: fire, a slot a level, the hold, the advance, the rest" {
+ const rec = canim.open(recordings.steps);
+ const scene = struct {
+ var at: canim.Cursor = undefined;
+ fn frame(s: *anim.Frame) void {
+ const r = canim.open(recordings.steps);
+ at = canim.cursor(s, r, r.lineMoment(10, r.call("main", 0).enter));
+ at.levels = .{ .idea = .calls };
+ }
+ };
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ var frame: anim.Frame = .{ .t = 0, .clock = 0, .in = undefined, .pointer = .{ 0, 0 }, .arena = arena.allocator() };
+ scene.frame(&frame);
+ const t = canim.theme.time;
+ // main's line calls fib(5): registers, the stack, the call. Three levels, 0.30 s apart.
+ const from = rec.lineMoment(10, rec.call("main", 0).enter);
+ const call = scene.at.reading(from, rec.next(.line, from));
+ try std.testing.expectApproxEqAbs(@as(f32, 3 * t.cascade), call.at[2], 1e-4);
+ try expect(call.total > 2.0 and call.total < 3.0);
+ // A line that changes nothing shown: fire, advance, rest.
+ var quiet = scene.at;
+ quiet.levels = .{ .cpu = false, .memory = false };
+ const line = quiet.reading(rec.lineMoment(4, from), rec.lineMoment(6, from));
+ try std.testing.expectApproxEqAbs(t.fire + t.step + t.rest + t.step_per_row, line.total, 1e-3);
+}