asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Metamorphic tests for RegionHeap ordering invariants.
//!
//! These tests verify invariant relationships that must hold for correct
//! allocator behavior, focusing on scenarios where computing exact expected
//! final states is intractable due to complex alloc/dealloc sequences.

use crate::runtime::region_heap::{HeapIndex, RegionHeap, global_alloc_count};

/// Test value type for allocator testing.
#[derive(Debug, Clone, PartialEq, Eq)]
struct TestValue {
    id: u32,
    data: Vec<u8>,
}

impl TestValue {
    fn new(id: u32, size: usize) -> Self {
        Self {
            id,
            data: vec![id as u8; size],
        }
    }
}

/// Generate sequences of heap operations.
#[derive(Debug, Clone)]
enum HeapOperation {
    Alloc { id: u32, size: usize },
    Dealloc { target_id: u32 },
    AllocMany { count: u32, base_id: u32 },
    DeallocMany { target_ids: Vec<u32> },
    ReclaimAll,
}

/// Execute a sequence of operations and track allocated indices.
fn execute_operations(heap: &mut RegionHeap, operations: &[HeapOperation]) -> OperationResults {
    let mut allocated_indices: std::collections::HashMap<u32, HeapIndex> =
        std::collections::HashMap::new();
    let mut allocation_count = 0;
    let mut deallocation_count = 0;
    let mut reclaim_count = 0;

    for op in operations {
        match op {
            HeapOperation::Alloc { id, size } => {
                let value = TestValue::new(*id, *size);
                let index = heap.alloc(value);
                allocated_indices.insert(*id, index);
                allocation_count += 1;
            }
            HeapOperation::Dealloc { target_id } => {
                if let Some(index) = allocated_indices.remove(target_id) {
                    if heap.dealloc(index) {
                        deallocation_count += 1;
                    }
                }
            }
            HeapOperation::AllocMany { count, base_id } => {
                for i in 0..*count {
                    let id = base_id + i;
                    let value = TestValue::new(id, 64); // Fixed size for bulk ops
                    let index = heap.alloc(value);
                    allocated_indices.insert(id, index);
                    allocation_count += 1;
                }
            }
            HeapOperation::DeallocMany { target_ids } => {
                for target_id in target_ids {
                    if let Some(index) = allocated_indices.remove(target_id) {
                        if heap.dealloc(index) {
                            deallocation_count += 1;
                        }
                    }
                }
            }
            HeapOperation::ReclaimAll => {
                let live_before = heap.len();
                heap.reclaim_all();
                allocated_indices.clear();
                reclaim_count += live_before;
            }
        }
    }

    OperationResults {
        allocation_count,
        deallocation_count,
        reclaim_count,
        remaining_allocations: allocated_indices,
    }
}

#[derive(Debug)]
struct OperationResults {
    allocation_count: usize,
    deallocation_count: usize,
    reclaim_count: usize,
    remaining_allocations: std::collections::HashMap<u32, HeapIndex>,
}

#[cfg(test)]
mod metamorphic_tests {
    use super::*;

    /// MR1: Statistics Conservation (Additive)
    /// Property: stats.allocations - stats.reclaimed = stats.live
    #[test]
    fn mr_statistics_conservation() {
        crate::test_utils::init_test_logging();
        crate::test_phase!("mr_statistics_conservation");

        let mut heap = RegionHeap::new();

        // Test across various operation sequences
        let operation_sequences = vec![
            vec![
                HeapOperation::Alloc { id: 1, size: 64 },
                HeapOperation::Alloc { id: 2, size: 128 },
                HeapOperation::Dealloc { target_id: 1 },
            ],
            vec![
                HeapOperation::AllocMany {
                    count: 5,
                    base_id: 10,
                },
                HeapOperation::DeallocMany {
                    target_ids: vec![10, 12, 14],
                },
            ],
            vec![
                HeapOperation::Alloc { id: 100, size: 32 },
                HeapOperation::Alloc { id: 101, size: 64 },
                HeapOperation::ReclaimAll,
            ],
        ];

        for (i, ops) in operation_sequences.iter().enumerate() {
            let _results = execute_operations(&mut heap, ops);
            let final_stats = heap.stats();

            // Conservation law: allocations - reclaimed = live
            assert_eq!(
                final_stats.allocations - final_stats.reclaimed,
                final_stats.live,
                "Statistics conservation violated in sequence {}: allocations={}, reclaimed={}, live={}",
                i,
                final_stats.allocations,
                final_stats.reclaimed,
                final_stats.live
            );

            // Length consistency: heap.len() = stats.live
            assert_eq!(
                heap.len() as u64,
                final_stats.live,
                "Length inconsistent with live count in sequence {}: len={}, live={}",
                i,
                heap.len(),
                final_stats.live
            );
        }

        crate::test_complete!("mr_statistics_conservation");
    }

    /// MR2: Global Count Consistency (Equivalence)
    /// Property: Global allocation count should track actual allocations
    #[test]
    fn mr_global_count_consistency() {
        crate::test_utils::init_test_logging();
        crate::test_phase!("mr_global_count_consistency");

        // Get baseline global count
        let initial_global_count = global_alloc_count();

        let mut heap1 = RegionHeap::new();
        let mut heap2 = RegionHeap::new();

        // Scenario 1: Single heap operations
        let idx1 = heap1.alloc(TestValue::new(1, 64));
        let _idx2 = heap1.alloc(TestValue::new(2, 128));

        let after_heap1_allocs = global_alloc_count();
        assert_eq!(
            after_heap1_allocs,
            initial_global_count + 2,
            "Global count not updated after heap1 allocations"
        );

        // Scenario 2: Multiple heap operations
        let _idx3 = heap2.alloc(TestValue::new(3, 32));

        let after_heap2_alloc = global_alloc_count();
        assert_eq!(
            after_heap2_alloc,
            initial_global_count + 3,
            "Global count inconsistent with multiple heaps"
        );

        // Scenario 3: Deallocations
        heap1.dealloc(idx1);
        let after_dealloc = global_alloc_count();
        assert_eq!(
            after_dealloc,
            initial_global_count + 2,
            "Global count not decremented after dealloc"
        );

        // Scenario 4: Reclaim all
        heap2.reclaim_all();
        let after_reclaim = global_alloc_count();
        assert_eq!(
            after_reclaim,
            initial_global_count + 1, // idx2 still allocated in heap1
            "Global count inconsistent after reclaim_all"
        );

        // Cleanup
        heap1.reclaim_all();
        let final_global_count = global_alloc_count();
        assert_eq!(
            final_global_count, initial_global_count,
            "Global count not restored to baseline after cleanup"
        );

        crate::test_complete!("mr_global_count_consistency");
    }

    /// MR3: Generation Monotonicity (Permutative)
    /// Property: Generations should only increase when slots are reused
    #[test]
    fn mr_generation_monotonicity() {
        crate::test_utils::init_test_logging();
        crate::test_phase!("mr_generation_monotonicity");

        let mut heap = RegionHeap::new();

        // Allocate and track original generation
        let idx1 = heap.alloc(TestValue::new(1, 64));
        let original_gen = idx1.generation();

        // Deallocate to create a free slot
        assert!(heap.dealloc(idx1), "Failed to deallocate idx1");

        // Reallocate - should reuse the slot with incremented generation
        let idx2 = heap.alloc(TestValue::new(2, 128));

        if idx2.index() == idx1.index() {
            // Same slot reused
            assert!(
                idx2.generation() > original_gen,
                "Generation did not increase on slot reuse: original={}, reused={}",
                original_gen,
                idx2.generation()
            );
        }

        // Multiple reuse cycles
        assert!(heap.dealloc(idx2), "Failed to deallocate idx2");
        let idx3 = heap.alloc(TestValue::new(3, 64));

        if idx3.index() == idx1.index() {
            assert!(
                idx3.generation() > idx2.generation(),
                "Generation not monotonic across multiple reuses"
            );
        }

        crate::test_complete!("mr_generation_monotonicity");
    }

    /// MR4: Access Stability (Equivalence)
    /// Property: Valid indices should return same value until deallocated
    #[test]
    fn mr_access_stability() {
        crate::test_utils::init_test_logging();
        crate::test_phase!("mr_access_stability");

        let mut heap = RegionHeap::new();

        // Allocate test values
        let test_value = TestValue::new(42, 256);
        let idx = heap.alloc(test_value.clone());

        // Multiple accesses should return the same value
        for i in 0..10 {
            let retrieved = heap.get::<TestValue>(idx);
            assert!(retrieved.is_some(), "Access failed on iteration {}", i);
            assert_eq!(
                *retrieved.unwrap(),
                test_value,
                "Value changed between accesses on iteration {}",
                i
            );
        }

        // Access should remain stable across other operations
        let other_idx = heap.alloc(TestValue::new(99, 64));

        // Original value should still be stable
        let retrieved = heap.get::<TestValue>(idx);
        assert!(retrieved.is_some(), "Access failed after other allocation");
        assert_eq!(
            *retrieved.unwrap(),
            test_value,
            "Value corrupted by other allocation"
        );

        // Deallocate the other value
        heap.dealloc(other_idx);

        // Original value should still be accessible
        let retrieved = heap.get::<TestValue>(idx);
        assert!(
            retrieved.is_some(),
            "Access failed after other deallocation"
        );
        assert_eq!(
            *retrieved.unwrap(),
            test_value,
            "Value corrupted by other deallocation"
        );

        crate::test_complete!("mr_access_stability");
    }

    /// MR5: Free List Integrity (Inclusive/Exclusive)
    /// Property: Free list should contain no cycles and valid indices only
    #[test]
    fn mr_free_list_integrity() {
        crate::test_utils::init_test_logging();
        crate::test_phase!("mr_free_list_integrity");

        let mut heap = RegionHeap::new();

        // Create a pattern that exercises the free list
        let mut indices = Vec::new();
        for i in 0..10 {
            let idx = heap.alloc(TestValue::new(i, 64));
            indices.push(idx);
        }

        // Deallocate some indices to create free list
        let dealloc_indices = [0, 2, 4, 6, 8];
        for &i in &dealloc_indices {
            heap.dealloc(indices[i]);
        }

        // Verify that we can still allocate (free list working)
        let new_idx = heap.alloc(TestValue::new(100, 64));
        assert!(heap.contains(new_idx), "New allocation not accessible");

        // Verify remaining allocated values are still accessible
        let remaining_indices = [1, 3, 5, 7, 9];
        for &i in &remaining_indices {
            assert!(
                heap.contains(indices[i]),
                "Remaining allocation {} not accessible",
                i
            );
            let value = heap.get::<TestValue>(indices[i]);
            assert!(value.is_some(), "Cannot access remaining value {}", i);
            assert_eq!(
                value.unwrap().id,
                i as u32,
                "Value corrupted for index {}",
                i
            );
        }

        crate::test_complete!("mr_free_list_integrity");
    }

    /// MR6: Allocation/Deallocation Symmetry (Invertive)
    /// Property: Allocating then deallocating should restore heap state
    #[test]
    fn mr_allocation_deallocation_symmetry() {
        crate::test_utils::init_test_logging();
        crate::test_phase!("mr_allocation_deallocation_symmetry");

        let mut heap = RegionHeap::new();

        // Record initial state
        let initial_stats = heap.stats();
        let initial_len = heap.len();
        let initial_global = global_alloc_count();

        // Perform allocation followed by immediate deallocation
        let test_sizes = [32, 64, 128, 256, 512];
        for (i, &size) in test_sizes.iter().enumerate() {
            let value = TestValue::new(i as u32, size);
            let idx = heap.alloc(value);

            // Verify allocation happened
            assert!(heap.contains(idx), "Allocation {} not present", i);

            // Immediately deallocate
            assert!(heap.dealloc(idx), "Failed to deallocate {}", i);

            // Verify deallocation happened
            assert!(
                !heap.contains(idx),
                "Index {} still accessible after dealloc",
                i
            );
        }

        // Check symmetry: state should be equivalent to initial
        let final_stats = heap.stats();
        let final_len = heap.len();
        let final_global = global_alloc_count();

        assert_eq!(
            final_len, initial_len,
            "Heap length not symmetric after alloc/dealloc cycle"
        );
        assert_eq!(
            final_stats.live, initial_stats.live,
            "Live count not symmetric: initial={}, final={}",
            initial_stats.live, final_stats.live
        );
        assert_eq!(
            final_global, initial_global,
            "Global count not symmetric: initial={}, final={}",
            initial_global, final_global
        );

        crate::test_complete!("mr_allocation_deallocation_symmetry");
    }

    /// MR7: Composite Operation Invariants (Multiplicative Power)
    /// Property: Complex sequences should maintain all invariants simultaneously
    #[test]
    fn mr_composite_operation_invariants() {
        crate::test_utils::init_test_logging();
        crate::test_phase!("mr_composite_operation_invariants");

        let initial_global = global_alloc_count();
        let mut heap = RegionHeap::new();

        // Execute complex operation sequence
        let operations = vec![
            HeapOperation::AllocMany {
                count: 5,
                base_id: 1000,
            },
            HeapOperation::Alloc {
                id: 2000,
                size: 256,
            },
            HeapOperation::DeallocMany {
                target_ids: vec![1001, 1003],
            },
            HeapOperation::AllocMany {
                count: 3,
                base_id: 3000,
            },
            HeapOperation::Dealloc { target_id: 2000 },
            HeapOperation::Alloc {
                id: 4000,
                size: 512,
            },
        ];

        let results = execute_operations(&mut heap, &operations);
        let final_stats = heap.stats();
        assert_eq!(
            results.allocation_count,
            results.deallocation_count
                + results.reclaim_count
                + results.remaining_allocations.len(),
            "operation accounting must explain every allocation"
        );

        // MR1: Statistics conservation
        assert_eq!(
            final_stats.allocations - final_stats.reclaimed,
            final_stats.live,
            "Statistics conservation violated in composite operations"
        );

        // MR2: Length consistency
        assert_eq!(
            heap.len() as u64,
            final_stats.live,
            "Length inconsistent with live count in composite operations"
        );

        // MR4: All remaining allocations should be accessible
        for (&id, &index) in &results.remaining_allocations {
            assert!(
                heap.contains(index),
                "Remaining allocation {} not accessible",
                id
            );
            let value = heap.get::<TestValue>(index);
            assert!(value.is_some(), "Cannot retrieve remaining value {}", id);
            assert_eq!(value.unwrap().id, id, "Value corrupted for id {}", id);
        }

        // MR2: Global count should reflect actual state
        let expected_global = initial_global + (results.remaining_allocations.len() as u64);
        let actual_global = global_alloc_count();
        assert_eq!(
            actual_global, expected_global,
            "Global count inconsistent after composite operations: expected={}, actual={}",
            expected_global, actual_global
        );

        // Cleanup and verify final symmetry
        heap.reclaim_all();
        let final_global = global_alloc_count();
        assert_eq!(
            final_global, initial_global,
            "Global count not restored after reclaim_all: initial={}, final={}",
            initial_global, final_global
        );

        crate::test_complete!("mr_composite_operation_invariants");
    }

    /// MR8: Type Safety Invariant (Equivalence)
    /// Property: Type mismatches should consistently return None
    #[test]
    fn mr_type_safety_invariant() {
        crate::test_utils::init_test_logging();
        crate::test_phase!("mr_type_safety_invariant");

        let mut heap = RegionHeap::new();

        // Allocate different types
        let string_idx = heap.alloc("hello".to_string());
        let int_idx = heap.alloc(42u32);
        let vec_idx = heap.alloc(vec![1, 2, 3]);

        // Correct type access should work
        assert!(heap.get::<String>(string_idx).is_some());
        assert!(heap.get::<u32>(int_idx).is_some());
        assert!(heap.get::<Vec<i32>>(vec_idx).is_some());

        // Type mismatches should consistently return None
        assert!(
            heap.get::<u32>(string_idx).is_none(),
            "Type mismatch allowed for string->u32"
        );
        assert!(
            heap.get::<String>(int_idx).is_none(),
            "Type mismatch allowed for u32->String"
        );
        assert!(
            heap.get::<Vec<i32>>(string_idx).is_none(),
            "Type mismatch allowed for string->Vec"
        );
        assert!(
            heap.get::<String>(vec_idx).is_none(),
            "Type mismatch allowed for Vec->String"
        );

        // Type safety should be stable across operations
        let _another_idx = heap.alloc(TestValue::new(99, 64));

        // Original type checks should still fail consistently
        assert!(
            heap.get::<u32>(string_idx).is_none(),
            "Type safety changed after other alloc"
        );
        assert!(
            heap.get::<String>(int_idx).is_none(),
            "Type safety changed after other alloc"
        );

        crate::test_complete!("mr_type_safety_invariant");
    }
}