summaryrefslogtreecommitdiff
path: root/anim/test/recordings.zig
blob: 3634ee9681727e34cd8c889b34b4f6efdcd2255b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! 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);
        // From the first call it shows: main, or the one a recording begins in.
        var at = rec.calls[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 "a program recorded by instruction, its variables told: calls, locals, and what they reach" {
    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 recording begins in it (`--from reverse`): the call is open from the first stop.
    try expect(rec.own[reverse.enter] == 17 or rec.own[reverse.enter] == 18);
    // 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);
    // glibc's `open` makes `openat`.
    try std.testing.expectEqualStrings("openat", 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, 0.30 s
    // apart; the call's node when the projection from its way back arrives.
    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, 2 * t.cascade), call.at[1], 1e-4);
    try std.testing.expectApproxEqAbs(@as(f32, 2 * t.cascade + t.project.bracket + t.project.far), call.at[2], 1e-4);
    try expect(call.total > 2.0 and call.total < 3.5);
    // 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);
}