1
use super::*;
2
use crate::ast::{PairElement, StructId};
3
use crate::reader::Reader;
4

            
5
15
fn guess(source: &str, args: &[Option<WasmType>]) -> WasmType {
6
15
    let body = Reader::parse(source)
7
15
        .ok()
8
15
        .and_then(|program| program.exprs.first().cloned())
9
15
        .unwrap_or(Expr::Nil);
10
15
    recursive_ret_guess(
11
15
        "f",
12
15
        &["p".to_string(), "n".to_string()],
13
15
        &body,
14
15
        args.iter().copied(),
15
15
    )
16
15
}
17

            
18
#[test]
19
1
fn the_non_recursive_arm_decides_the_guess() {
20
1
    let body = "(if (= n 0) 1/2 (+ 1/2 (f p (- n 1))))";
21
1
    assert_eq!(guess(body, &[Some(WasmType::I32)]), WasmType::Ratio);
22
1
}
23

            
24
#[test]
25
1
fn an_arm_of_a_cond_decides_the_guess() {
26
1
    let body = "(cond ((= n 0) nil) (t (cons n (f p (- n 1)))))";
27
1
    assert_eq!(guess(body, &[Some(WasmType::Ratio)]), WasmType::Bool);
28
1
}
29

            
30
#[test]
31
1
fn a_non_numeric_first_argument_is_not_the_guess() {
32
1
    let body = "(+ 1 (f p (- n 1)))";
33
1
    let args = [Some(WasmType::StringRef), Some(WasmType::I32)];
34
1
    assert_eq!(guess(body, &args), WasmType::Ratio);
35
1
}
36

            
37
#[test]
38
1
fn an_integer_literal_exit_is_an_index_unless_a_scalar_is_accumulated() {
39
1
    let count = "(if (= n 0) 0 (+ 1 (f p (- n 1))))";
40
1
    assert_eq!(guess(count, &[Some(WasmType::StringRef)]), WasmType::I32);
41
1
    let scalar = "(if (= n 0) 0 (+ 1/2 (f p (- n 1))))";
42
1
    assert_eq!(guess(scalar, &[Some(WasmType::StringRef)]), WasmType::Ratio);
43
1
}
44

            
45
#[test]
46
1
fn no_numeric_argument_defaults_to_ratio() {
47
1
    assert_eq!(guess("(+ 1 (f p))", &[None]), WasmType::Ratio);
48
1
}
49

            
50
#[test]
51
1
fn a_symbol_exit_is_not_known_so_the_arguments_decide() {
52
1
    let body = "(if (= n 0) p (f p (- n 1)))";
53
1
    assert_eq!(
54
1
        guess(body, &[Some(WasmType::Commodity)]),
55
1
        WasmType::Commodity
56
    );
57
1
}
58

            
59
#[test]
60
1
fn a_scalar_argument_makes_an_integer_literal_exit_a_scalar() {
61
1
    let body = "(if (= n 0) 0 (+ (funcall g n) (f g (- n 1))))";
62
1
    let args = [
63
1
        Some(WasmType::Closure(crate::ast::ClosureSigId(0))),
64
1
        Some(WasmType::Ratio),
65
1
    ];
66
1
    assert_eq!(guess(body, &args), WasmType::Ratio);
67
1
}
68

            
69
#[test]
70
1
fn a_bare_parameter_exit_has_its_arguments_type() {
71
1
    let body = "(if (= n 0) p (f p (- n 1)))";
72
1
    let args = [Some(WasmType::StructRef(StructId(3))), Some(WasmType::I32)];
73
1
    assert_eq!(guess(body, &args), WasmType::StructRef(StructId(3)));
74
1
}
75

            
76
#[test]
77
1
fn a_list_of_literals_exit_is_a_pair() {
78
1
    let body = "(if (= n 0) (list 0) (cons n (f p (- n 1))))";
79
1
    let args = [Some(WasmType::StructRef(StructId(3))), Some(WasmType::I32)];
80
1
    assert_eq!(guess(body, &args), WasmType::PairRef(PairElement::I32));
81
1
}
82

            
83
#[test]
84
1
fn a_let_rebinding_a_parameter_hides_its_argument_type() {
85
1
    let body = "(if (> n 0) (let* ((q 1) (p (+ q (f p (- n 1))))) p) 0)";
86
1
    assert_eq!(guess(body, &[Some(WasmType::StringRef)]), WasmType::I32);
87
1
    let parallel = "(if (> n 0) (let ((p (* 2 (f p (- n 1))))) p) 0)";
88
1
    assert_eq!(guess(parallel, &[Some(WasmType::StringRef)]), WasmType::I32);
89
1
}
90

            
91
#[test]
92
1
fn a_let_binding_another_name_keeps_the_parameter_type() {
93
1
    let body = "(if (> n 0) (let ((q (f p (- n 1)))) p) 0)";
94
1
    assert_eq!(
95
1
        guess(body, &[Some(WasmType::Commodity)]),
96
1
        WasmType::Commodity
97
    );
98
1
}
99

            
100
#[test]
101
1
fn a_list_of_a_parameter_is_a_pair_of_its_argument_type() {
102
1
    let body = "(if (= n 0) (list n) (cons n (f p (- n 1))))";
103
1
    assert_eq!(
104
1
        guess(body, &[Some(WasmType::StringRef), Some(WasmType::I32)]),
105
1
        WasmType::PairRef(PairElement::I32)
106
    );
107
1
}
108

            
109
#[test]
110
1
fn a_list_of_a_rebound_parameter_is_not_known() {
111
1
    let body = "(if (= n 0) (let ((n 1/2)) (list n)) (cons n (f p (- n 1))))";
112
1
    assert_eq!(
113
1
        guess(body, &[Some(WasmType::StringRef), Some(WasmType::I32)]),
114
1
        WasmType::Ratio
115
    );
116
1
}