1
use std::collections::HashMap;
2

            
3
use super::*;
4
use crate::ast::{Number, PairElement};
5
use crate::runtime::{Symbol, SymbolKind};
6

            
7
2
fn zero() -> Expr {
8
2
    Expr::Number(Number::Index(0))
9
2
}
10

            
11
19
fn key() -> HomeKey {
12
19
    HomeKey::Binder(None, 7, "X".to_string(), 9)
13
19
}
14

            
15
#[test]
16
1
fn a_binder_without_a_fact_has_no_hint() {
17
1
    let mut symbols = SymbolTable::new();
18
1
    symbols.begin_home_attempt(HashMap::new());
19
1
    assert_eq!(symbols.hint(&key()), None);
20
1
}
21

            
22
#[test]
23
1
fn a_binder_store_the_home_cannot_hold_widens_the_next_attempt() {
24
1
    let mut symbols = SymbolTable::new();
25
1
    symbols.begin_home_attempt(HashMap::new());
26
1
    symbols.open_binder(
27
1
        &key(),
28
1
        Arm::NumberLiteral(WasmType::I32),
29
1
        None,
30
1
        WasmType::I32,
31
1
    );
32
1
    symbols.note_store(&key(), Arm::Value(WasmType::I32));
33
1
    assert_eq!(symbols.widened_homes(), None);
34
1
    symbols.note_store(&key(), Arm::Nil);
35
1
    assert_eq!(
36
1
        symbols.widened_homes(),
37
1
        Some(HashMap::from([(key(), WasmType::AnyRef)]))
38
    );
39
1
}
40

            
41
#[test]
42
1
fn a_binder_fact_is_the_hint_of_the_next_attempt_and_joins_with_its_stores() {
43
1
    let mut symbols = SymbolTable::new();
44
1
    let hint = HashMap::from([(key(), WasmType::PairRef(PairElement::I32))]);
45
1
    symbols.begin_home_attempt(hint);
46
1
    assert_eq!(
47
1
        symbols.hint(&key()),
48
1
        Some(WasmType::PairRef(PairElement::I32))
49
    );
50
1
    symbols.open_binder(&key(), Arm::Nil, None, WasmType::PairRef(PairElement::I32));
51
1
    symbols.note_store(&key(), Arm::Value(WasmType::PairRef(PairElement::Ratio)));
52
1
    assert_eq!(
53
1
        symbols.widened_homes(),
54
1
        Some(HashMap::from([(
55
1
            key(),
56
1
            WasmType::PairRef(PairElement::AnyRef)
57
1
        )]))
58
    );
59
1
}
60

            
61
#[test]
62
1
fn the_first_instance_of_a_binder_site_decides_its_home() {
63
1
    let mut symbols = SymbolTable::new();
64
1
    symbols.begin_home_attempt(HashMap::new());
65
1
    symbols.open_binder(&key(), Arm::Value(WasmType::I32), None, WasmType::I32);
66
1
    symbols.open_binder(&key(), Arm::Value(WasmType::Ratio), None, WasmType::Ratio);
67
1
    symbols.note_store(&key(), Arm::Value(WasmType::Ratio));
68
1
    assert_eq!(
69
1
        symbols.widened_homes(),
70
1
        Some(HashMap::from([(key(), WasmType::AnyRef)]))
71
    );
72
1
}
73

            
74
#[test]
75
1
fn binder_homes_are_counted_with_the_sites_and_named_by_their_variable() {
76
1
    let mut symbols = SymbolTable::new();
77
1
    symbols.begin_home_attempt(HashMap::new());
78
1
    symbols.open_binder(&key(), Arm::Nil, None, WasmType::Bool);
79
1
    assert_eq!(symbols.site_home_count(), 1);
80
1
    assert_eq!(key().to_string(), "variable X");
81
1
}
82

            
83
#[test]
84
1
fn a_binder_probe_reads_the_constant_its_init_kept() {
85
1
    let mut symbols = SymbolTable::new();
86
1
    symbols.begin_home_attempt(HashMap::new());
87
1
    symbols.define(
88
1
        Symbol::new("X", SymbolKind::Variable).with_value(Expr::WasmLocal(0, WasmType::I32)),
89
1
    );
90
1
    symbols.open_binder(
91
1
        &key(),
92
1
        Arm::NumberLiteral(WasmType::I32),
93
1
        Some(zero()),
94
1
        WasmType::I32,
95
1
    );
96
1
    let probe = symbols.probe_at_init(&key(), "X").expect("probe");
97
1
    assert_eq!(
98
1
        probe.lookup("X").and_then(|symbol| symbol.value().cloned()),
99
1
        Some(zero())
100
    );
101
1
    assert!(probe.probe_at_init(&key(), "X").is_none());
102
1
}