1
use super::{FrameKind, ValidationFrame};
2

            
3
#[test]
4
1
fn unreachable_marks_the_current_frame() {
5
1
    let mut frame = ValidationFrame::new();
6
1
    assert!(frame.is_reachable());
7
1
    frame.mark_unreachable();
8
1
    assert!(!frame.is_reachable());
9
1
}
10

            
11
#[test]
12
1
fn br_then_unreachable_then_end_leaves_the_enclosing_frame_reachable() {
13
    // `(block (br 0)) <next>` — the block's OWN body goes unreachable
14
    // after the `br`, but falling off the block's `end` must restore
15
    // whatever reachability the ENCLOSING frame already had.
16
1
    let mut frame = ValidationFrame::new();
17
1
    frame.push_ctrl(FrameKind::Block);
18
1
    frame.mark_unreachable();
19
1
    assert!(!frame.is_reachable(), "body is unreachable after br");
20
1
    frame.pop_ctrl().expect("close the block");
21
1
    assert!(
22
1
        frame.is_reachable(),
23
        "the enclosing frame's reachability must not inherit the closed \
24
         frame's own unreachable flag"
25
    );
26
1
}
27

            
28
#[test]
29
1
fn end_without_incoming_branches_stays_reachable() {
30
    // `(block <plain body>)` — no br ever targets it; falling through its
31
    // `end` must leave the enclosing frame reachable, exactly as the
32
    // "with incoming branches" case above does. Pinning both cases to
33
    // the same outcome is the actual regression: a naive implementation
34
    // that leaked the child's flag upward would pass one and fail the
35
    // other.
36
1
    let mut frame = ValidationFrame::new();
37
1
    frame.push_ctrl(FrameKind::Block);
38
1
    frame.pop_ctrl().expect("close the block");
39
1
    assert!(frame.is_reachable());
40
1
}
41

            
42
#[test]
43
1
fn throw_marks_unreachable_like_br() {
44
1
    let mut frame = ValidationFrame::new();
45
1
    frame.push_ctrl(FrameKind::Block);
46
1
    frame.mark_unreachable();
47
1
    assert!(!frame.is_reachable());
48
1
}
49

            
50
#[test]
51
1
fn else_resets_reachability_for_the_alternate_arm() {
52
1
    let mut frame = ValidationFrame::new();
53
1
    frame.push_ctrl(FrameKind::If);
54
1
    frame.mark_unreachable(); // `then` arm ends with `br` / `unreachable`
55
1
    assert!(!frame.is_reachable());
56
1
    frame.reset_for_else();
57
1
    assert!(
58
1
        frame.is_reachable(),
59
        "the else arm is a fresh branch, not a continuation of `then`'s \
60
         unreachable tail"
61
    );
62
1
}
63

            
64
#[test]
65
1
fn a_frame_entered_from_unreachable_code_still_starts_reachable() {
66
    // Matches the wasm validation algorithm's `push_ctrl`: `unreachable` is
67
    // always seeded `false` on entry, regardless of the enclosing frame's
68
    // own state. Dead code nested inside other dead code is validated
69
    // strictly against its own fresh, polymorphism-free base until
70
    // something inside IT marks it unreachable in turn.
71
1
    let mut frame = ValidationFrame::new();
72
1
    frame.push_ctrl(FrameKind::Block);
73
1
    frame.mark_unreachable();
74
1
    frame.push_ctrl(FrameKind::Block); // entered while already unreachable
75
1
    assert!(
76
1
        frame.is_reachable(),
77
        "a newly entered frame starts reachable even from unreachable code"
78
    );
79
1
}
80

            
81
#[test]
82
1
fn pop_ctrl_never_pops_the_base_frame() {
83
1
    let mut frame = ValidationFrame::new();
84
1
    let err = frame.pop_ctrl().expect_err("no open frame to pop");
85
1
    assert!(matches!(err, crate::error::Error::Compile(_)));
86
1
}
87

            
88
#[test]
89
1
fn a_branch_to_a_loop_does_not_mark_its_end() {
90
1
    let mut frame = ValidationFrame::new();
91
1
    frame.push_ctrl(FrameKind::Block);
92
1
    frame.push_ctrl(FrameKind::Loop);
93
1
    frame.mark_targets([0, 1]);
94
1
    assert!(!frame.nested[1].end_targeted, "a loop's label is its start");
95
1
    assert!(frame.nested[0].end_targeted, "the block's label is its end");
96
1
}
97

            
98
#[test]
99
1
fn every_kind_but_loop_is_marked_at_any_depth() {
100
1
    let mut frame = ValidationFrame::new();
101
1
    for kind in [
102
1
        FrameKind::Block,
103
1
        FrameKind::If,
104
1
        FrameKind::Loop,
105
1
        FrameKind::TryTable,
106
1
        FrameKind::Loop,
107
1
        FrameKind::If,
108
1
        FrameKind::TryTable,
109
1
        FrameKind::Block,
110
1
    ] {
111
8
        frame.push_ctrl(kind);
112
    }
113
1
    frame.mark_targets([0, 1, 1, 2, 3, 4, 5, 6, 7]);
114
8
    let marked: Vec<bool> = frame.nested.iter().map(|f| f.end_targeted).collect();
115
1
    assert_eq!(marked, [true, true, false, true, false, true, true, true]);
116
1
}