1
use super::*;
2

            
3
#[tokio::test]
4
1
async fn false_bool_in_an_anyref_slot_is_nil() {
5
1
    for form in [
6
1
        "(null? (car (cdr (if (> (rpc-protocol-version) 0) (list t nil) (list 1 2)))))",
7
1
        "(null? (car (cdr (handler-case (list (> (rpc-protocol-version) 0) nil) \
8
1
         (t (e) (list 1/2 (index->scalar (rpc-protocol-version))))))))",
9
1
        "(null? (car (if (> (rpc-protocol-version) 0) \
10
1
         (list nil (> (rpc-protocol-version) 0)) (list \"a\" (rpc-protocol-version)))))",
11
1
        "(null? (car (cons (> (rpc-protocol-version) 5) (list \"a\"))))",
12
1
        "(null? (car (fold (lambda (acc x) (cons x acc)) '(\"a\") \
13
1
         (list (> (rpc-protocol-version) 5)))))",
14
1
        "(null? (if (> (rpc-protocol-version) 0) (> (rpc-protocol-version) 5) \"s\"))",
15
1
        "(null? (car (cdr (append (list (> (rpc-protocol-version) 0) nil) \
16
1
         (list (rpc-protocol-version))))))",
17
1
    ] {
18
7
        assert_values(&[(form, "t")]).await;
19
1
    }
20
1
    assert_values(&[(
21
1
        "(null? (car (cons (> (rpc-protocol-version) 0) (list \"a\"))))",
22
1
        "nil",
23
1
    )])
24
1
    .await;
25
1
}
26

            
27
#[tokio::test]
28
1
async fn index_result_beyond_i31_crosses_the_eval_boundary_exactly() {
29
1
    assert_values(&[
30
1
        (
31
1
            "(handler-case (+ 1073741823 (rpc-protocol-version)) (index-overflow (e) -1))",
32
1
            "1073741824",
33
1
        ),
34
1
        (
35
1
            "(- -1073741824 1073741823 (rpc-protocol-version))",
36
1
            "-2147483648",
37
1
        ),
38
1
    ])
39
1
    .await;
40
1
}
41

            
42
#[tokio::test]
43
1
async fn car_and_cdr_of_a_runtime_null_pair_never_trap() {
44
1
    assert_values(&[
45
1
        (
46
1
            "(car (if (< (rpc-protocol-version) 0) (list \"a\") nil))",
47
1
            "nil",
48
1
        ),
49
1
        (
50
1
            "(car (if (< (rpc-protocol-version) 0) (list t) nil))",
51
1
            "nil",
52
1
        ),
53
1
        (
54
1
            "(car (if (> (rpc-protocol-version) 0) \
55
1
             (if (> (rpc-protocol-version) 1) (list \"a\") nil) (list \"b\" \"c\")))",
56
1
            "nil",
57
1
        ),
58
1
        (
59
1
            "(cdr (if (< (rpc-protocol-version) 0) (list 1) nil))",
60
1
            "nil",
61
1
        ),
62
1
        (
63
1
            "(handler-case (car (cdr (list (rpc-protocol-version)))) (type-error (e) -1))",
64
1
            "-1",
65
1
        ),
66
1
    ])
67
1
    .await;
68
1
    assert_error_code("(car (cdr (list (rpc-protocol-version))))", "TYPE-ERROR").await;
69
1
    assert_error_code(
70
1
        "(car (if (< (rpc-protocol-version) 0) (list 1) nil))",
71
1
        "TYPE-ERROR",
72
1
    )
73
1
    .await;
74
    // A scalar or money value is never nil either, so an empty numeric list
75
    // has no car. The untaken branch never calls the DB-backed host fn.
76
1
    assert_error_code(
77
1
        "(+ (car (if (< (rpc-protocol-version) 0) (list 1/2) nil)) 1/1)",
78
1
        "TYPE-ERROR",
79
1
    )
80
1
    .await;
81
1
    assert_error_code(
82
1
        "(car (if (< (rpc-protocol-version) 0) (list (account-balance \"cash\")) nil))",
83
1
        "TYPE-ERROR",
84
1
    )
85
1
    .await;
86
1
    assert_values(&[(
87
1
        "(handler-case (car (if (< (rpc-protocol-version) 0) (list 1/2) nil)) \
88
1
         (type-error (e) 7))",
89
1
        "7/1",
90
1
    )])
91
1
    .await;
92
1
}
93

            
94
#[tokio::test]
95
1
async fn filter_and_do_treat_a_runtime_index_as_true() {
96
1
    assert_values(&[
97
1
        ("(filter (lambda (x) (* 0 x)) (list 1 2))", "(1 2)"),
98
1
        (
99
1
            "(filter (lambda (x) (* 0 x)) (list 1 (rpc-protocol-version)))",
100
1
            "(1 1)",
101
1
        ),
102
1
        (
103
1
            "(let ((n 0)) (do ((i (* 0 (rpc-protocol-version)) (+ i 1))) (i n) \
104
1
             (setf n (+ n (rpc-protocol-version)))))",
105
1
            "0",
106
1
        ),
107
1
    ])
108
1
    .await;
109
1
}
110

            
111
#[tokio::test]
112
1
async fn handler_case_reconciles_an_anyref_body_with_literal_and_nil_arms() {
113
1
    assert_values(&[
114
1
        (
115
1
            "(handler-case (if (> (rpc-protocol-version) 0) (rpc-protocol-version)) (t (e) 0))",
116
1
            "1",
117
1
        ),
118
1
        ("(handler-case (rpc-protocol-version) (t (e) nil))", "1"),
119
1
    ])
120
1
    .await;
121
1
}
122

            
123
#[tokio::test]
124
1
async fn fractional_literal_never_crosses_into_an_index_arm() {
125
1
    assert_values(&[
126
1
        (
127
1
            "(if (> (rpc-protocol-version) 0) (rpc-protocol-version) 1/2)",
128
1
            "1",
129
1
        ),
130
1
        (
131
1
            "(if (> (rpc-protocol-version) 0) (if (> (rpc-protocol-version) 1) 1 2) 1/2)",
132
1
            "2",
133
1
        ),
134
1
        (
135
1
            "(if (> (rpc-protocol-version) 0) (index->scalar (rpc-protocol-version)) 1/2)",
136
1
            "1/1",
137
1
        ),
138
1
        ("(handler-case (rpc-protocol-version) (t (e) 1/2))", "1"),
139
1
    ])
140
1
    .await;
141
1
}
142

            
143
#[tokio::test]
144
1
async fn a_runtime_index_is_never_nil() {
145
1
    assert_values(&[
146
1
        ("(null? (* 0 (rpc-protocol-version)))", "nil"),
147
1
        ("(not (* 0 (rpc-protocol-version)))", "nil"),
148
1
        ("(if (* 0 (rpc-protocol-version)) 1 2)", "1"),
149
1
        ("(if (< (rpc-protocol-version) 0) 5)", "nil"),
150
1
        ("(null? (if (> (rpc-protocol-version) 0) 5))", "nil"),
151
1
        ("(null? (> (rpc-protocol-version) 5))", "t"),
152
1
        (
153
1
            "(and (* 0 (rpc-protocol-version)) (> (rpc-protocol-version) 0))",
154
1
            "t",
155
1
        ),
156
1
        // ADR-0033: `or` returns VALUES — a `Never`-typed (index) first
157
1
        // operand short-circuits statically and IS the result (its own
158
1
        // value, `0`), not a collapsed `t`.
159
1
        (
160
1
            "(or (* 0 (rpc-protocol-version)) (> (rpc-protocol-version) 5))",
161
1
            "0",
162
1
        ),
163
1
    ])
164
1
    .await;
165
1
}
166

            
167
#[tokio::test]
168
1
async fn constant_list_arms_materialize_as_chains() {
169
1
    assert_values(&[
170
1
        ("(if (> (rpc-protocol-version) 0) '(1 2) '(\"a\"))", "(1 2)"),
171
1
        (
172
1
            "(let ((xs (list 1 2))) (if (> (rpc-protocol-version) 0) xs nil))",
173
1
            "(1 2)",
174
1
        ),
175
1
        ("(let ((xs '(1 2))) (handler-case xs (t (e) nil)))", "(1 2)"),
176
1
        (
177
1
            "(let ((xs '(1 2))) (car (if (> (rpc-protocol-version) 0) xs \
178
1
             (list (rpc-protocol-version)))))",
179
1
            "1",
180
1
        ),
181
1
    ])
182
1
    .await;
183
1
}
184

            
185
#[tokio::test]
186
1
async fn nil_arm_after_runtime_effects_reconciles() {
187
1
    assert_values(&[(
188
1
        "(let ((n 0)) (handler-case (progn (setf n (rpc-protocol-version)) nil) \
189
1
         (t (e) (list 1))) n)",
190
1
        "1",
191
1
    )])
192
1
    .await;
193
1
}
194

            
195
#[tokio::test]
196
1
async fn map_applies_a_quoted_defun_over_a_runtime_list() {
197
1
    for head in ["map", "mapcar"] {
198
2
        assert_values(&[(
199
2
            &format!("(progn (defun g2 (x) (+ x 1)) ({head} 'g2 (list (rpc-protocol-version))))"),
200
2
            "(2)",
201
2
        )])
202
2
        .await;
203
1
    }
204
1
}