|
| 1 | +//! End-to-end tests: Rust → Wasm → Rust (memoised Fibonacci with custom allocator). |
| 2 | +//! |
| 3 | +//! The source module (`data/rust/rust_e2e_heavy_fibo.rs`) uses a bump allocator |
| 4 | +//! to provide `liballoc` in a `no_std` Wasm environment. It caches computed |
| 5 | +//! Fibonacci values in a `Vec<i32>` so that: |
| 6 | +//! |
| 7 | +//! * Calling `fibo(n)` when `n` is already cached costs O(1). |
| 8 | +//! * Calling `fibo(n + m)` after a previous `fibo(n)` only computes the |
| 9 | +//! `m` new values — all earlier results are served from the cache. |
| 10 | +//! |
| 11 | +//! `fibo_cache_len()` exposes the current cache size so tests can verify the |
| 12 | +//! memoisation invariants without inspecting internal state directly. |
| 13 | +
|
| 14 | +use herkos_tests::rust_e2e_heavy_fibo; |
| 15 | + |
| 16 | +fn new_module() -> rust_e2e_heavy_fibo::WasmModule { |
| 17 | + rust_e2e_heavy_fibo::new().expect("module instantiation should succeed") |
| 18 | +} |
| 19 | + |
| 20 | +// ── Reference implementation ────────────────────────────────────────────────── |
| 21 | + |
| 22 | +/// Ground-truth `fibo(n)` using the same wrapping i32 arithmetic as Wasm. |
| 23 | +fn fibo_ref(n: usize) -> i32 { |
| 24 | + let mut a: i32 = 0; |
| 25 | + let mut b: i32 = 1; |
| 26 | + for _ in 0..n { |
| 27 | + let tmp = a.wrapping_add(b); |
| 28 | + a = b; |
| 29 | + b = tmp; |
| 30 | + } |
| 31 | + a |
| 32 | +} |
| 33 | + |
| 34 | +// ── Base cases ──────────────────────────────────────────────────────────────── |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn test_fibo_base_cases() { |
| 38 | + let mut m = new_module(); |
| 39 | + assert_eq!(m.fibo(0).unwrap(), 0); |
| 40 | + assert_eq!(m.fibo(1).unwrap(), 1); |
| 41 | +} |
| 42 | + |
| 43 | +// ── Small known values ──────────────────────────────────────────────────────── |
| 44 | + |
| 45 | +#[test] |
| 46 | +fn test_fibo_small_values() { |
| 47 | + let mut m = new_module(); |
| 48 | + // Classical Fibonacci sequence: 0,1,1,2,3,5,8,13,21,34,55,89,144 |
| 49 | + let expected: &[i32] = &[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]; |
| 50 | + for (n, &want) in expected.iter().enumerate() { |
| 51 | + assert_eq!(m.fibo(n as i32).unwrap(), want, "fibo({n})"); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// ── Cross-validation against reference ─────────────────────────────────────── |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn test_fibo_matches_reference() { |
| 59 | + let mut m = new_module(); |
| 60 | + for n in 0i32..50 { |
| 61 | + assert_eq!(m.fibo(n).unwrap(), fibo_ref(n as usize), "fibo({n})"); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// ── Negative input guard ────────────────────────────────────────────────────── |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn test_fibo_negative_returns_zero() { |
| 69 | + let mut m = new_module(); |
| 70 | + assert_eq!(m.fibo(-1).unwrap(), 0); |
| 71 | + assert_eq!(m.fibo(-100).unwrap(), 0); |
| 72 | +} |
| 73 | + |
| 74 | +// ── Cache growth ────────────────────────────────────────────────────────────── |
| 75 | + |
| 76 | +#[test] |
| 77 | +fn test_cache_seeded_with_two_entries() { |
| 78 | + let mut m = new_module(); |
| 79 | + // The cache is pre-seeded with fibo(0) = 0 and fibo(1) = 1 on first access. |
| 80 | + assert_eq!(m.fibo_cache_len().unwrap(), 2); |
| 81 | +} |
| 82 | + |
| 83 | +#[test] |
| 84 | +fn test_cache_grows_to_cover_requested_index() { |
| 85 | + let mut m = new_module(); |
| 86 | + m.fibo(10).unwrap(); |
| 87 | + // Cache must cover indices 0..=10, i.e. 11 entries. |
| 88 | + assert_eq!(m.fibo_cache_len().unwrap(), 11); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn test_cache_extends_incrementally() { |
| 93 | + let mut m = new_module(); |
| 94 | + |
| 95 | + m.fibo(10).unwrap(); |
| 96 | + assert_eq!(m.fibo_cache_len().unwrap(), 11); |
| 97 | + |
| 98 | + // Requesting fibo(20) only needs to compute 10 new values. |
| 99 | + m.fibo(20).unwrap(); |
| 100 | + assert_eq!(m.fibo_cache_len().unwrap(), 21); |
| 101 | + |
| 102 | + m.fibo(30).unwrap(); |
| 103 | + assert_eq!(m.fibo_cache_len().unwrap(), 31); |
| 104 | +} |
| 105 | + |
| 106 | +#[test] |
| 107 | +fn test_cache_does_not_shrink_on_smaller_query() { |
| 108 | + let mut m = new_module(); |
| 109 | + m.fibo(20).unwrap(); |
| 110 | + let len_after = m.fibo_cache_len().unwrap(); |
| 111 | + |
| 112 | + // A smaller query must not evict cached entries. |
| 113 | + m.fibo(5).unwrap(); |
| 114 | + assert_eq!( |
| 115 | + m.fibo_cache_len().unwrap(), |
| 116 | + len_after, |
| 117 | + "cache must not shrink after a smaller query" |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +// ── Monotone cache invariant ────────────────────────────────────────────────── |
| 122 | + |
| 123 | +#[test] |
| 124 | +fn test_cache_len_is_monotone() { |
| 125 | + let mut m = new_module(); |
| 126 | + let mut prev_len = m.fibo_cache_len().unwrap(); |
| 127 | + |
| 128 | + // Mix of increasing and decreasing n values; cache must never shrink. |
| 129 | + for n in [0i32, 3, 1, 10, 7, 20, 20, 25, 24, 30] { |
| 130 | + m.fibo(n).unwrap(); |
| 131 | + let new_len = m.fibo_cache_len().unwrap(); |
| 132 | + assert!( |
| 133 | + new_len >= prev_len, |
| 134 | + "cache len should be monotone non-decreasing (was {prev_len}, now {new_len} after fibo({n}))" |
| 135 | + ); |
| 136 | + prev_len = new_len; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// ── Wrapping arithmetic beyond i32 overflow ─────────────────────────────────── |
| 141 | + |
| 142 | +#[test] |
| 143 | +fn test_fibo_wrapping_overflow() { |
| 144 | + let mut m = new_module(); |
| 145 | + // fibo(46) = 1_836_311_903 is the last value that fits in i32. |
| 146 | + // fibo(47) and beyond overflow — wrapping behaviour must match the reference. |
| 147 | + for n in 44i32..=55 { |
| 148 | + assert_eq!( |
| 149 | + m.fibo(n).unwrap(), |
| 150 | + fibo_ref(n as usize), |
| 151 | + "fibo({n}) wrapping" |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// ── Idempotency: repeated calls return identical results ────────────────────── |
| 157 | + |
| 158 | +#[test] |
| 159 | +fn test_fibo_idempotent() { |
| 160 | + let mut m = new_module(); |
| 161 | + // Prime the cache up to n=30. |
| 162 | + for n in 0i32..=30 { |
| 163 | + m.fibo(n).unwrap(); |
| 164 | + } |
| 165 | + // All values must be stable on repeated queries. |
| 166 | + for n in 0i32..=30 { |
| 167 | + assert_eq!( |
| 168 | + m.fibo(n).unwrap(), |
| 169 | + fibo_ref(n as usize), |
| 170 | + "fibo({n}) should be idempotent" |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
0 commit comments