diff options
Diffstat (limited to 'anim/test/recordings.zig')
| -rw-r--r-- | anim/test/recordings.zig | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/anim/test/recordings.zig b/anim/test/recordings.zig index 38798af..ce5a9db 100644 --- a/anim/test/recordings.zig +++ b/anim/test/recordings.zig @@ -105,3 +105,108 @@ test "a walked step is as long as what it changed: fire, a slot a level, the hol 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); } + +/// The first moment whose pc is in the function named `name` (as the recording names it: `libc.so.6!open`). +fn firstIn(rec: *const Recording, name: []const u8) u64 { + for (rec.registers, 0..) |regs, i| { + const at = rec.strings.get(regs.rip) orelse continue; + if (std.mem.startsWith(u8, at, name) and at.len > name.len and at[name.len] == '+') return i; + } + unreachable; +} + +test "sort's graph: two loops in their lanes, the hot path straight down, the jle never taken" { + 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).?; + 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); +} + +test "reverse's graph: one loop, its exit in the right channel" { + 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).?; + 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); + // 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); +} + +test "libc's open at its syscall: the blocks never reached folded, no loop made by folding" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const graph = canim.graph; + const rec = canim.open(recordings.hello); + 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; + 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); + // 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); +} |
