1
use super::*;
2

            
3
#[tokio::test]
4
1
async fn integer_list_if_branches_unify_with_runtime_index_list() {
5
    // An all-integer-literal LIST is an Index chain (ADR-0028), so it
6
    // unifies with a runtime index list instead of widening to AnyRef.
7
1
    assert_eq!(
8
1
        eval_value_of(
9
1
            "(if (> (rpc-protocol-version) 0) \
10
1
                 (list 1 (* 0 (rpc-protocol-version))) (list 1 2))"
11
1
        )
12
1
        .await,
13
1
        "(:id 4 :value (1 0))"
14
    );
15
1
    assert_eq!(
16
1
        eval_value_of(
17
1
            "(let ((xs (if (> (rpc-protocol-version) 0) \
18
1
                 (list 1 2) (list (rpc-protocol-version))))) (car xs))"
19
1
        )
20
1
        .await,
21
1
        "(:id 4 :value 1)"
22
1
    );
23
1
}
24

            
25
#[tokio::test]
26
1
async fn integer_list_local_accepts_runtime_index_stores() {
27
1
    assert_eq!(
28
1
        eval_value_of("(let ((xs (list 1 2))) (setf xs (list (* 0 (rpc-protocol-version)))) xs)")
29
1
            .await,
30
1
        "(:id 4 :value (0))"
31
    );
32
1
    assert_eq!(
33
1
        eval_value_of(
34
1
            "(let ((xs (list 1 2))) (when (> (rpc-protocol-version) 0) \
35
1
                 (setf xs (cons (* 0 (rpc-protocol-version)) xs))) xs)"
36
1
        )
37
1
        .await,
38
1
        "(:id 4 :value (0 1 2))"
39
1
    );
40
1
}
41

            
42
#[tokio::test]
43
1
async fn cons_runtime_index_onto_integer_lists() {
44
1
    assert_eq!(
45
1
        eval_value_of("(cons (* 0 (rpc-protocol-version)) (list 1 2))").await,
46
1
        "(:id 4 :value (0 1 2))"
47
    );
48
1
    assert_eq!(
49
1
        eval_value_of("(cons (* 0 (rpc-protocol-version)) '(1 2))").await,
50
1
        "(:id 4 :value (0 1 2))"
51
1
    );
52
1
}
53

            
54
#[tokio::test]
55
1
async fn handler_case_integer_list_arms_agree() {
56
1
    assert_eq!(
57
1
        eval_value_of("(handler-case (list 1 2) (t (e) (list (rpc-protocol-version))))").await,
58
1
        "(:id 4 :value (1 2))"
59
1
    );
60
1
}
61

            
62
#[tokio::test]
63
1
async fn mapcar_over_runtime_index_list() {
64
1
    assert_eq!(
65
1
        eval_value_of("(mapcar (lambda (x) (+ x 1)) (list 1 (* 0 (rpc-protocol-version))))").await,
66
1
        "(:id 4 :value (2 1))"
67
1
    );
68
1
}
69

            
70
#[tokio::test]
71
1
async fn integer_literal_at_the_i31_bound_takes_an_index_cell() {
72
1
    assert_eq!(
73
1
        eval_value_of("(list 1073741823 (* 0 (rpc-protocol-version)))").await,
74
1
        "(:id 4 :value (1073741823 0))"
75
1
    );
76
1
}
77

            
78
#[tokio::test]
79
1
async fn runtime_index_beyond_i31_throws_index_overflow() {
80
1
    for form in [
81
1
        "(list (+ 1073741823 (rpc-protocol-version)))",
82
1
        "(list (- -1073741823 (+ 1 (rpc-protocol-version))))",
83
1
        "(cons (* 1073741823 (+ 1 (rpc-protocol-version))) (list 1))",
84
1
    ] {
85
3
        let response = eval_value_of(form).await;
86
3
        assert!(
87
3
            response.contains(":code INDEX-OVERFLOW"),
88
1
            "{form} must raise index-overflow, got: {response}"
89
1
        );
90
1
    }
91
1
}
92

            
93
#[tokio::test]
94
1
async fn index_overflow_is_catchable_by_handler_case() {
95
1
    assert_eq!(
96
1
        eval_value_of(
97
1
            "(handler-case (car (list (+ 1073741823 (rpc-protocol-version)))) \
98
1
                 (index-overflow (e) -1))"
99
1
        )
100
1
        .await,
101
1
        "(:id 4 :value -1)"
102
1
    );
103
1
}
104

            
105
#[tokio::test]
106
1
async fn runtime_index_at_i31_bounds_round_trips() {
107
1
    assert_eq!(
108
1
        eval_value_of(
109
1
            "(list (* 1073741823 (rpc-protocol-version)) \
110
1
                 (* -1073741824 (rpc-protocol-version)))"
111
1
        )
112
1
        .await,
113
1
        "(:id 4 :value (1073741823 -1073741824))"
114
1
    );
115
1
}
116

            
117
#[tokio::test]
118
1
async fn constant_nil_never_becomes_a_zero_cell() {
119
1
    assert_eq!(
120
1
        eval_value_of(
121
1
            "(fold (lambda (acc x) (cons x acc)) '(nil) (list (* 0 (rpc-protocol-version))))"
122
1
        )
123
1
        .await,
124
1
        "(:id 4 :value (0 nil))"
125
    );
126
1
    assert_eq!(
127
1
        eval_value_of("(cons nil (list (* 0 (rpc-protocol-version))))").await,
128
1
        "(:id 4 :value (nil 0))"
129
1
    );
130
1
}
131

            
132
#[tokio::test]
133
1
async fn null_p_and_not_see_a_nil_in_an_anyref_cell() {
134
1
    assert_values(&[
135
1
        (
136
1
            "(null? (car (cons nil (list (rpc-protocol-version)))))",
137
1
            "t",
138
1
        ),
139
1
        (
140
1
            "(let ((c (car (cons nil (list (rpc-protocol-version)))))) (null? c))",
141
1
            "t",
142
1
        ),
143
1
        ("(null? (car (list nil (rpc-protocol-version))))", "t"),
144
1
        (
145
1
            "(null? (car (cdr (cons (rpc-protocol-version) (list nil)))))",
146
1
            "t",
147
1
        ),
148
1
        ("(not (car (list nil (rpc-protocol-version))))", "t"),
149
1
        ("(null? (car (list \"a\" (rpc-protocol-version))))", "nil"),
150
1
        ("(not (index->scalar (rpc-protocol-version)))", "nil"),
151
1
    ])
152
1
    .await;
153
1
}
154

            
155
#[tokio::test]
156
1
async fn catch_each_binds_an_index_iteration_variable() {
157
1
    assert_values(&[
158
1
        ("(length (catch-each (list 1 2) x (+ x 1)))", "2"),
159
1
        (
160
1
            "(let ((xs (list 1 2))) (length (catch-each xs x (+ x 1))))",
161
1
            "2",
162
1
        ),
163
1
        ("(catch-each (list 1 2) x (+ x 1))", "((OK 2) (OK 3))"),
164
1
    ])
165
1
    .await;
166
1
}
167

            
168
#[tokio::test]
169
1
async fn filter_of_an_empty_list_agrees_with_its_closure_predicate() {
170
1
    assert_values(&[(
171
1
        "(let ((p (lambda (x) (string= x \"a\")))) (cons \"b\" (filter p '())))",
172
1
        "(\"b\")",
173
1
    )])
174
1
    .await;
175
1
}
176

            
177
#[tokio::test]
178
1
async fn mapcar_and_mapc_accept_let_bound_closures() {
179
1
    assert_values(&[
180
1
        (
181
1
            "(let ((f (lambda (x) (* x 2/3)))) (mapcar f '(1/1 2/1)))",
182
1
            "(2/3 4/3)",
183
1
        ),
184
1
        (
185
1
            "(let ((f (lambda (x) (* x 2/3)))) (mapc f '(1/1 2/1)))",
186
1
            "(1/1 2/1)",
187
1
        ),
188
1
        (
189
1
            "(let ((f (lambda (x) (+ x 1/1)))) \
190
1
                 (map f (list 1/2 (index->scalar (rpc-protocol-version)))))",
191
1
            "(3/2 2/1)",
192
1
        ),
193
1
        (
194
1
            "(let ((f (lambda (x) (+ x 1/1)))) \
195
1
                 (mapcar f (list 1/2 (index->scalar (rpc-protocol-version)))))",
196
1
            "(3/2 2/1)",
197
1
        ),
198
1
        (
199
1
            "(let ((func 5)) (mapcar (lambda (x) (+ x func)) '(1 1)))",
200
1
            "(6 6)",
201
1
        ),
202
1
        (
203
1
            "(let ((f (lambda (x) (+ x 1)))) (map f (list 1 (rpc-protocol-version))))",
204
1
            "(2 2)",
205
1
        ),
206
1
    ])
207
1
    .await;
208
    // A let-bound closure keeps its Scalar parameter, so a runtime index
209
    // list is refused identically through MAP and its MAPCAR alias.
210
1
    for head in ["map", "mapcar"] {
211
2
        assert_error_code(
212
2
            &format!(
213
2
                "(let ((f (lambda (x) (* x 2/3)))) ({head} f (list 1 (rpc-protocol-version))))"
214
2
            ),
215
2
            "compile",
216
2
        )
217
2
        .await;
218
1
    }
219
1
}
220

            
221
#[tokio::test]
222
1
async fn runtime_index_beyond_i31_throws_in_an_anyref_arm() {
223
1
    assert_error_code(
224
1
        "(if (> (rpc-protocol-version) 0) (+ 1073741823 (rpc-protocol-version)) \"a\")",
225
1
        "INDEX-OVERFLOW",
226
1
    )
227
1
    .await;
228
1
    assert_values(&[
229
1
        ("(+ 1073741823 (rpc-protocol-version))", "1073741824"),
230
1
        (
231
1
            "(let ((x (+ 1073741823 (rpc-protocol-version)))) x)",
232
1
            "1073741824",
233
1
        ),
234
1
        ("(+ 1073741822 (rpc-protocol-version))", "1073741823"),
235
1
        ("(- -1073741823 (rpc-protocol-version))", "-1073741824"),
236
1
        (
237
1
            "(null? (if (> (rpc-protocol-version) 0) (* 1073741823 (rpc-protocol-version)) \"a\"))",
238
1
            "nil",
239
1
        ),
240
1
    ])
241
1
    .await;
242
1
}
243

            
244
#[tokio::test]
245
1
async fn index_division_by_zero_and_overflow_are_catchable() {
246
1
    assert_values(&[
247
1
        (
248
1
            "(handler-case (/ 7 (* 0 (rpc-protocol-version))) (t (e) 0))",
249
1
            "0",
250
1
        ),
251
1
        (
252
1
            "(handler-case (mod 7 (* 0 (rpc-protocol-version))) (division-by-zero (e) -1))",
253
1
            "-1",
254
1
        ),
255
1
        ("(/ 7 (* 2 (rpc-protocol-version)))", "3"),
256
1
        ("(mod 7 (* 2 (rpc-protocol-version)))", "1"),
257
1
    ])
258
1
    .await;
259
1
    assert_error_code("(/ (* 0 (rpc-protocol-version)))", "DIVISION-BY-ZERO").await;
260
1
    assert_error_code(
261
1
        "(/ (- -1073741824 1073741823 (rpc-protocol-version)) (- 0 (rpc-protocol-version)))",
262
1
        "INDEX-OVERFLOW",
263
1
    )
264
1
    .await;
265
1
}
266

            
267
#[tokio::test]
268
1
async fn if_over_differently_typed_lists_stays_a_list() {
269
1
    assert_values(&[
270
1
        (
271
1
            "(if (> (rpc-protocol-version) 0) (list 1 2) (list \"a\" \"b\"))",
272
1
            "(1 2)",
273
1
        ),
274
1
        (
275
1
            "(length (if (> (rpc-protocol-version) 0) (list 1 2) (list \"a\" \"b\")))",
276
1
            "2",
277
1
        ),
278
1
    ])
279
1
    .await;
280
1
}
281

            
282
#[tokio::test]
283
1
async fn filter_and_mapcar_literal_paths_share_the_chain_rule() {
284
1
    assert_values(&[
285
1
        (
286
1
            "(filter (lambda (x) (> (rpc-protocol-version) 0)) '(1 1/2))",
287
1
            "(1/1 1/2)",
288
1
        ),
289
1
        (
290
1
            "(mapcar (lambda (x) (+ x 1/2)) (list 1/1 1/2))",
291
1
            "(3/2 1/1)",
292
1
        ),
293
1
    ])
294
1
    .await;
295
1
}
296

            
297
#[tokio::test]
298
1
async fn a_mapcar_that_adds_a_fraction_to_an_index_element_is_a_mix() {
299
1
    let reply = eval_value_of("(mapcar (lambda (x) (+ x 1/2)) (list 1 1/2))").await;
300
1
    assert!(
301
1
        reply.contains("+ cannot mix an index (count) with a scalar"),
302
1
        "CL (3/2 1): {reply}"
303
1
    );
304
1
}
305

            
306
#[tokio::test]
307
1
async fn a_tagged_integral_scalar_fold_rides_a_heterogeneous_cell() {
308
1
    assert_values(&[("(list (* 1/2 2/1) \"a\")", "(1/1 \"a\")")]).await;
309
1
}
310

            
311
#[tokio::test]
312
1
async fn handler_case_reconciles_literal_and_nil_arms() {
313
1
    assert_values(&[
314
1
        (
315
1
            "(handler-case (index->scalar (rpc-protocol-version)) (t (e) 7))",
316
1
            "1/1",
317
1
        ),
318
1
        (
319
1
            "(handler-case (mapcar (lambda (x) (* x (+ 1073741823 (rpc-protocol-version)))) \
320
1
                 (list 1 (rpc-protocol-version))) (index-overflow (e) nil))",
321
1
            "nil",
322
1
        ),
323
1
        (
324
1
            "(handler-case (mapcar (lambda (x) (* x 2)) \
325
1
                 (list 1 (rpc-protocol-version))) (index-overflow (e) nil))",
326
1
            "(2 2)",
327
1
        ),
328
1
    ])
329
1
    .await;
330
1
}
331

            
332
#[tokio::test]
333
1
async fn fold_seed_members_widen_the_accumulator() {
334
1
    assert_eq!(
335
1
        eval_value_of(
336
1
            "(fold (lambda (acc x) (cons x acc)) '(1) (list (* 0 (rpc-protocol-version))))"
337
1
        )
338
1
        .await,
339
1
        "(:id 4 :value (0 1))"
340
    );
341
    // ADR-0033: an index element decodes as its Number, a `t` element by
342
    // identity against the `$t` singleton, and (Phase 6) a string element by
343
    // its `$i8_array` kind.
344
1
    for (seed, expected) in [("'(t)", "(1 t)"), ("'(\"a\")", "(1 \"a\")")] {
345
2
        assert_eq!(
346
2
            eval_value_of(&format!(
347
2
                "(fold (lambda (acc x) (cons x acc)) {seed} (list (rpc-protocol-version)))"
348
2
            ))
349
2
            .await,
350
2
            format!("(:id 4 :value {expected})"),
351
1
            "seed {seed}"
352
1
        );
353
1
    }
354
1
}
355

            
356
#[tokio::test]
357
1
async fn list_of_literals_renders_elements_on_eval_path() {
358
    // Regression: an all-literal LIST at stack position defaulted its cells
359
    // to AnyRef, so the host rendered `(<anyref> <anyref>)`.
360
1
    assert_eq!(
361
1
        handle_form_smoke("(:id 4 :form (list 1 2))").await,
362
1
        "(:id 4 :value (1 2))"
363
    );
364
1
    assert_eq!(
365
1
        handle_form_smoke("(:id 4 :form (list (> 2 1) (= 1 2)))").await,
366
1
        "(:id 4 :value (t nil))"
367
    );
368
1
    assert_eq!(
369
1
        handle_form_smoke("(:id 4 :form (list 1/2 2))").await,
370
1
        "(:id 4 :value (1/2 2/1))"
371
1
    );
372
1
}
373

            
374
#[tokio::test]
375
1
async fn list_mixing_runtime_and_literal_elements_unifies_cells() {
376
    // An integer literal rides its runtime index neighbor's slot on either
377
    // side; a disagreeing literal tail widens instead of failing to compile.
378
1
    assert_eq!(
379
1
        handle_form_smoke("(:id 4 :form (list (* 0 (rpc-protocol-version)) 2))").await,
380
1
        "(:id 4 :value (0 2))"
381
    );
382
1
    assert_eq!(
383
1
        handle_form_smoke("(:id 4 :form (list 2 (* 0 (rpc-protocol-version))))").await,
384
1
        "(:id 4 :value (2 0))"
385
    );
386
1
    assert_eq!(
387
1
        handle_form_smoke(
388
1
            "(:id 4 :form (list (* 0/1 (index->scalar (rpc-protocol-version))) \"a\"))"
389
1
        )
390
1
        .await,
391
1
        "(:id 4 :value (0/1 \"a\"))"
392
    );
393
1
    assert_eq!(
394
1
        handle_form_smoke("(:id 4 :form (list (* 0 (rpc-protocol-version)) nil))").await,
395
1
        "(:id 4 :value (0 nil))"
396
1
    );
397
1
}
398

            
399
#[tokio::test]
400
1
async fn append_runtime_list_onto_constant_list_renders_elements() {
401
    // Regression: the constant suffix was materialized as AnyRef cells under
402
    // a concrete result label, so a ratio label panicked the host decoder.
403
1
    assert_eq!(
404
1
        handle_form_smoke(
405
1
            "(:id 4 :form (append (list (* 0/1 (index->scalar (rpc-protocol-version)))) '(1/1 2/1)))"
406
1
        )
407
1
        .await,
408
1
        "(:id 4 :value (0/1 1/1 2/1))"
409
    );
410
1
    assert_eq!(
411
1
        handle_form_smoke("(:id 4 :form (append (list (* 0 (rpc-protocol-version))) '(1 2)))")
412
1
            .await,
413
1
        "(:id 4 :value (0 1 2))"
414
    );
415
1
    assert_eq!(
416
1
        handle_form_smoke("(:id 4 :form (append (list (> (rpc-protocol-version) 0)) '(t nil)))")
417
1
            .await,
418
1
        "(:id 4 :value (t t nil))"
419
1
    );
420
1
}
421

            
422
#[tokio::test]
423
1
async fn fold_with_constant_list_seed_renders_elements() {
424
    // Regression: the constant seed chain was built as AnyRef cells under the
425
    // accumulator's ratio label, panicking the host decoder.
426
1
    assert_eq!(
427
1
        handle_form_smoke(
428
1
            "(:id 4 :form (fold (lambda (acc x) (cons x acc)) '(1/1 2/1) \
429
1
                 (list (* 0/1 (index->scalar (rpc-protocol-version))))))"
430
1
        )
431
1
        .await,
432
1
        "(:id 4 :value (0/1 1/1 2/1))"
433
    );
434
1
    assert_eq!(
435
1
        handle_form_smoke(
436
1
            "(:id 4 :form (fold (lambda (acc x) (cons x acc)) '(t) \
437
1
                 (list (> (rpc-protocol-version) 0))))"
438
1
        )
439
1
        .await,
440
1
        "(:id 4 :value (t t))"
441
1
    );
442
1
}
443

            
444
#[tokio::test]
445
1
async fn fold_nil_seed_over_a_mixed_type_list_stays_a_pair() {
446
    // ADR-0033/Phase 3: the `nil` seed used to size `Bool` even when the
447
    // list's elements are `AnyRef`, so `CONS` on the first iteration failed
448
    // with "cdr must be a pair or nil, got bool". The seed must size to the
449
    // element's own `PairRef` element instead.
450
1
    assert_values(&[(
451
1
        "(fold (lambda (acc x) (cons x acc)) nil (mapcar (lambda (x) x) (list 1 nil)))",
452
1
        "(nil 1)",
453
1
    )])
454
1
    .await;
455
1
}
456

            
457
#[tokio::test]
458
1
async fn fold_nil_seed_probes_the_unified_element_not_just_the_first() {
459
    // Fable review (Phase 3): `sample_element` used to probe the fold body
460
    // with only the constant list's FIRST element (`1`), so a body wrapping
461
    // it in a runtime `if` (forcing the SECOND element's `AnyRef`-typed
462
    // slot) probed as if the list were homogeneous `I32`, then failed with
463
    // "FOLD: closure return type pair<any> doesn't match accumulator type
464
    // pair<i32>". `sample_element` must probe with the chain's UNIFIED
465
    // element instead.
466
1
    assert_values(&[(
467
1
        "(let* ((n (rpc-protocol-version))) \
468
1
           (fold (lambda (acc x) (cons (if (> n 0) x x) acc)) nil (list 1 nil)))",
469
1
        "(nil 1)",
470
1
    )])
471
1
    .await;
472
1
}
473

            
474
#[test]
475
1
fn a_long_list_response_drops_on_a_small_stack() {
476
1
    let handle = std::thread::Builder::new()
477
1
        .stack_size(512 * 1024)
478
1
        .spawn(|| {
479
1
            let runtime = tokio::runtime::Builder::new_current_thread()
480
1
                .enable_all()
481
1
                .build()
482
1
                .expect("runtime");
483
1
            runtime.block_on(eval_value_of(
484
1
                "(do ((i 0 (+ i 1)) (x nil (cons 1 x))) ((= i 16000) x))",
485
1
            ))
486
1
        })
487
1
        .expect("spawn");
488
1
    let reply = handle.join().expect("no stack overflow");
489
1
    assert!(reply.starts_with("(:id 4 :value (1 1 1"), "{reply:.80}");
490
1
}