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.zig194
1 files changed, 127 insertions, 67 deletions
diff --git a/anim/test/recordings.zig b/anim/test/recordings.zig
index e3af983..7c3a26e 100644
--- a/anim/test/recordings.zig
+++ b/anim/test/recordings.zig
@@ -115,71 +115,121 @@ fn firstIn(rec: *const Recording, name: []const u8) u64 {
unreachable;
}
-test "sort's graph: two loops in their lanes, the hot path straight down, the jle never taken" {
+/// The `blocks` lens's pane, its body: what the graph is laid out for.
+const lens_room = [2]f32{ 880 - 4, 344 - 24 - 2 };
+
+/// The moment the sort scene opens at: the inner loop's test, about to compare 31 with 12.
+fn sortStart(rec: *const Recording) u64 {
+ var start = rec.call("sort", 0).enter;
+ while (rec.next(.over, rec.next(.over, start)) < rec.call("swap", 0).enter) start = rec.next(.over, start);
+ return start;
+}
+
+/// Whether node `n`'s centre is on ring `r`.
+fn onRing(n: canim.graph.Node, r: canim.graph.Ring) bool {
+ const dx = n.x + n.w / 2 - r.c[0];
+ const dy = n.y + n.h / 2 - r.c[1];
+ return @abs(@sqrt(dx * dx + dy * dy) - r.r) < 0.5;
+}
+
+test "sort's graph: two rings from the code alone, the inner touching the outer at its header, whole in the lens" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const graph = canim.graph;
const rec = canim.open(recordings.sort);
- const at = firstIn(rec, "sort");
- var g = graph.read(arena.allocator(), rec, rec.end(), rec.registers[at].rip).?;
+ const start = sortStart(rec);
+ const pc = rec.registers[start].rip;
+ const g = graph.layout(arena.allocator(), rec, start, pc, .rings, lens_room, start).?;
try expect(g.exact and g.text);
try expectEqual(@as(usize, 11), g.nodes.len);
- for (g.nodes) |n| try expect(n.reached);
- // The inner loop's `j > 0` is never false: its jle never taken.
- const jle = g.edgeOf(g.nodeOf(g.start + 0x8a).?, g.nodeOf(g.start + 0xbc).?).?;
- try expect(g.edges[jle].kind == .taken and g.edges[jle].total == 0);
- // The compare's jne to the swap, ten times; the swap entered as often.
- const swap = g.nodeOf(g.start + 0xc5).?;
- try expectEqual(@as(u32, 10), g.edges[g.edgeOf(g.nodeOf(g.start + 0xbc).?, swap).?].count);
- try expectEqual(@as(u32, 10), g.nodes[swap].visits);
- graph.sizes(rec, rec.end(), &g, .map);
- graph.place(&g, graph.metricOf(.map));
- var backs: usize = 0;
- for (g.edges) |e| backs += @intFromBool(e.back);
- try expectEqual(@as(usize, 2), backs);
- // The outer loop (i++ back to the test) outermost, the inner (the swap back to `j > 0`) inside it.
- const outer = g.edges[g.edgeOf(g.nodeOf(g.start + 0xff).?, g.nodeOf(g.start + 0x78).?).?];
- const inner = g.edges[g.edgeOf(swap, g.nodeOf(g.start + 0x8a).?).?];
- try expect(outer.back and inner.back and outer.lane == 0 and inner.lane == 1);
- // The swap straight under the compare that goes to it ten times in ten; the way on beside it.
- try expectEqual(g.nodes[g.nodeOf(g.start + 0xbc).?].col, g.nodes[swap].col);
- try expect(g.nodes[g.nodeOf(g.start + 0xc3).?].col > g.nodes[swap].col);
- // The jle and the loop's exit would cross blocks: the right channel.
- try expectEqual(@as(u8, 6), g.edges[jle].n);
- try expectEqual(g.channel, g.edges[jle].points[2][0]);
- // On the map (1688 high): too tall; the long prologue folds, and the outer loop is panned into view whole.
- var buf: [1 << 20]u8 = undefined;
- var fba = std.heap.FixedBufferAllocator.init(&buf);
- const room = 1688 - 24;
- const in_loop = rec.lineMoment(12, at);
- const laid = graph.layout(fba.allocator(), rec, in_loop, rec.registers[in_loop].rip, .map, room).?;
- try expectEqual(@as(u32, 9), laid.nodes[0].shown);
- const off = graph.pan(laid, room, graph.metricOf(.map));
- for (laid.nodes[1 .. laid.nodes.len - 1]) |n| try expect(n.y >= off and n.y + n.h <= off + room);
+ try expectEqual(@as(usize, 0), g.foldedBlocks());
+ const at = struct {
+ fn of(gr: canim.graph.Graph, off: u64) canim.graph.Node {
+ return gr.nodes[gr.nodeOf(gr.start + off).?];
+ }
+ }.of;
+ // Two rings: the outer loop's (the test at +78), the inner's (its test at +8a).
+ try expectEqual(@as(usize, 2), g.rings.len);
+ const outer = if (g.nodes[g.rings[0].header].start == g.start + 0x78) g.rings[0] else g.rings[1];
+ const inner = if (g.nodes[g.rings[0].header].start == g.start + 0x8a) g.rings[0] else g.rings[1];
+ try expect(g.nodes[outer.header].start == g.start + 0x78 and g.nodes[inner.header].start == g.start + 0x8a);
+ for ([_]u64{ 0x78, 0x84, 0x8a, 0xc3, 0xfd, 0xff }) |off| try expect(onRing(at(g, off), outer));
+ for ([_]u64{ 0x8a, 0x95, 0xbc, 0xc5 }) |off| try expect(onRing(at(g, off), inner));
+ for ([_]u64{ 0x0, 0x10d }) |off| try expect(!onRing(at(g, off), outer) and !onRing(at(g, off), inner));
+ // The inner ring outside the outer, touching it at +8a: the outer's 3 o'clock, the inner's 9.
+ const h = at(g, 0x8a);
+ try std.testing.expectApproxEqAbs(outer.c[0] + outer.r, h.x + h.w / 2, 0.5);
+ try std.testing.expectApproxEqAbs(inner.c[0] - inner.r, h.x + h.w / 2, 0.5);
+ try std.testing.expectApproxEqAbs(outer.c[1], inner.c[1], 0.5);
+ // The headers at 9 o'clock; +0 left of +78 on its line, +10d under it.
+ const o = at(g, 0x78);
+ try std.testing.expectApproxEqAbs(outer.c[0] - outer.r, o.x + o.w / 2, 0.5);
+ const entry = at(g, 0x0);
+ try expect(entry.y == o.y and entry.x + entry.w < o.x);
+ const exit = at(g, 0x10d);
+ try expect(exit.y > o.y + o.h and exit.x < o.x + o.w and exit.x + exit.w > o.x);
+ // Any two pills 12 apart.
+ for (g.nodes, 0..) |a, i| for (g.nodes[i + 1 ..]) |b| {
+ const dx = @max(b.x - (a.x + a.w), a.x - (b.x + b.w), 0);
+ const dy = @max(b.y - (a.y + a.h), a.y - (b.y + b.h), 0);
+ try expect(dx * dx + dy * dy >= 12 * 12 - 0.01);
+ };
+ // Whole in the lens's pane, no pan.
+ try expect(g.w <= lens_room[0] and g.h <= lens_room[1]);
}
-test "reverse's graph: one loop, its exit in the right channel" {
+test "sort's graph says nothing from after the moment: the same layout at every moment, quiet until reached" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const graph = canim.graph;
+ const rec = canim.open(recordings.sort);
+ const start = sortStart(rec);
+ const later = rec.call("swap", 5).enter - 1;
+ const a = graph.layout(arena.allocator(), rec, start, rec.registers[start].rip, .rings, lens_room, start).?;
+ const b = graph.layout(arena.allocator(), rec, later, rec.registers[later].rip, .rings, lens_room, start).?;
+ // Where every block is, and every edge's way: the code's, not the run's.
+ try expectEqual(a.nodes.len, b.nodes.len);
+ for (a.nodes, b.nodes) |x, y| try expect(x.x == y.x and x.y == y.y and x.w == y.w);
+ for (a.edges, b.edges) |x, y| {
+ try expect(x.n == y.n);
+ for (x.path(), y.path()) |p, q| try expect(p[0] == q[0] and p[1] == q[1]);
+ }
+ // At the opening: every block reached but the swap and the way out; no count, no edge, into either.
+ for (a.nodes) |n| {
+ const off = n.start - a.start;
+ try expectEqual(off == 0xc5 or off == 0x10d, n.visits == 0);
+ }
+ for (a.edges) |e| if (a.nodes[e.to].visits == 0) try expectEqual(@as(u32, 0), e.count);
+ // The inner loop's `jle` not taken so far; its turns none yet.
+ const jle = a.edgeOf(a.nodeOf(a.start + 0x8a).?, a.nodeOf(a.start + 0xbc).?).?;
+ try expectEqual(@as(u32, 0), a.edges[jle].count);
+ const turn = a.edgeOf(a.nodeOf(a.start + 0xc5).?, a.nodeOf(a.start + 0x8a).?).?;
+ try expect(a.edges[turn].back and a.edges[turn].count == 0 and b.edges[turn].count == 5);
+ // The trail: taken edges only, the newest into the pc's block.
+ for (a.trail) |maybe| try expect(a.edges[maybe.?].count > 0);
+ // Three different edges: the last three taken, round the outer loop and into the inner.
+ try expect(a.trail[0].? != a.trail[1].? and a.trail[1].? != a.trail[2].? and a.trail[0].? != a.trail[2].?);
+ try expectEqual(b.here.?, b.edges[b.trail[0].?].to);
+}
+
+test "reverse's graph: one ring of two blocks, the way in from the left, the way out down" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const graph = canim.graph;
const rec = canim.open(recordings.list);
const at = firstIn(rec, "reverse");
- var g = graph.read(arena.allocator(), rec, rec.end(), rec.registers[at].rip).?;
+ const g = graph.layout(arena.allocator(), rec, at, rec.registers[at].rip, .rings, lens_room, at).?;
try expectEqual(@as(usize, 4), g.nodes.len);
- graph.sizes(rec, rec.end(), &g, .map);
- graph.place(&g, graph.metricOf(.map));
- const layers = [_]u32{ 0, 1, 2, 3 };
- for (g.nodes, layers) |n, l| try expect(n.layer == l and n.col == 0);
- // The body's jmp back to the test: the one edge that climbs, three turns.
- const turn = g.edges[g.edgeOf(2, 1).?];
- try expect(turn.back and turn.count == 3);
- try expectEqual(@as(u8, 6), g.edges[g.edgeOf(1, 3).?].n);
+ try expectEqual(@as(usize, 1), g.rings.len);
+ var on: usize = 0;
+ for (g.nodes) |n| on += @intFromBool(onRing(n, g.rings[0]));
+ try expectEqual(@as(usize, 2), on);
// The recording begins inside it: its first block was entered once.
try expectEqual(@as(u32, 1), g.nodes[0].visits);
- try expect(g.h <= 1688 - 24);
+ try expect(g.w <= lens_room[0] and g.h <= lens_room[1]);
}
-test "libc's open at its syscall: the blocks never reached folded, no loop made by folding" {
+test "libc's open at its syscall: what it has not reached folded, a fold opening as the run gets there" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const graph = canim.graph;
@@ -187,28 +237,38 @@ test "libc's open at its syscall: the blocks never reached folded, no loop made
var at = firstIn(rec, "libc.so.6!open");
var buf: [256]u8 = undefined;
while (!std.mem.eql(u8, rec.instruction(at, rec.registers[at].rip, &buf).?.text, "syscall")) at += 1;
- const g = graph.layout(arena.allocator(), rec, at, rec.registers[at].rip, .map, 1688 - 24).?;
- // Twenty blocks (two of `nop` padding left out), eight of them reached.
- try expectEqual(@as(usize, 20), g.blocks);
- var reached: usize = 0;
+ const a = graph.layout(arena.allocator(), rec, at, rec.registers[at].rip, .rings, lens_room, at).?;
+ // Twenty blocks (two of `nop` padding left out); no loop: layers.
+ try expectEqual(@as(usize, 20), a.blocks);
+ try expectEqual(@as(usize, 0), a.rings.len);
+ for (a.edges) |e| try expect(!e.back);
+ // Folded: only blocks not reached by the moment, two folds (the cancellable call's, the way back from the kernel).
var folds: usize = 0;
- for (g.nodes) |n| {
- reached += @intFromBool(n.reached);
- folds += @intFromBool(n.folded > 0);
- }
- try expectEqual(@as(usize, 8), reached);
- try expectEqual(@as(usize, 12), g.foldedBlocks());
- try expect(folds >= 4 and folds < 12);
- // open has no loop, and the folds make none.
- for (g.edges) |e| try expect(!e.back);
+ for (a.nodes) |n| if (n.folded > 0) {
+ folds += 1;
+ try expectEqual(@as(u32, 0), n.visits);
+ };
+ try expectEqual(@as(usize, 2), folds);
+ try expectEqual(@as(usize, 15), a.foldedBlocks());
+ // `+5b … · 7`, the cancellable call's; `+be … · 8`, the way back from the kernel; +d0 alone stays a quiet pill.
+ try expectEqual(@as(u32, 7), a.nodes[a.nodeOf(a.start + 0x5b).?].folded);
+ try expectEqual(@as(u32, 8), a.nodes[a.nodeOf(a.start + 0xbe).?].folded);
+ try expectEqual(@as(u32, 0), a.nodes[a.nodeOf(a.start + 0xd0).?].folded);
+ try expect(a.w <= lens_room[0] and a.h <= lens_room[1]);
// At its syscall: the blocks before entered once, those after not yet.
- try expectEqual(g.nodeOf(rec.registers[at].rip).?, g.here.?);
- for (g.nodes) |n| if (n.reached) try expectEqual(@as(u32, @intFromBool(n.start <= rec.registers[at].rip)), n.visits);
- // Still taller than the map: it pans, the pc's block in view; as wide as two columns at most.
- const room = 1688 - 24;
- const off = graph.pan(g, room, graph.metricOf(.map));
- const here = g.nodes[g.here.?];
- try expect(here.y >= off and here.y + here.h <= off + room and g.w <= 1792);
+ try expectEqual(a.nodeOf(rec.registers[at].rip).?, a.here.?);
+ for (a.nodes) |n| if (n.folded == 0 and n.visits > 0) try expectEqual(@as(u32, 1), n.visits);
+ // On to its block's last stop: the same layout. A step on, into the way back from the kernel: its fold open, the other still folded.
+ const end = graph.blockEnd(rec, at);
+ const b = graph.layout(arena.allocator(), rec, end, rec.registers[end].rip, .rings, lens_room, at).?;
+ for (a.nodes, b.nodes) |x, y| try expect(x.x == y.x and x.y == y.y and x.folded == y.folded);
+ const c = graph.layout(arena.allocator(), rec, end + 1, rec.registers[end + 1].rip, .rings, lens_room, at).?;
+ try expect(c.open != a.open);
+ try expectEqual(@as(u32, 0), c.nodes[c.here.?].folded);
+ try expectEqual(@as(u32, 1), c.nodes[c.here.?].visits);
+ var still: usize = 0;
+ for (c.nodes) |n| still += @intFromBool(n.folded > 0);
+ try expectEqual(@as(usize, 1), still);
}
test "the source follows the pc into musl's own files and back to main's line" {