|
| 1 | +unit test.multiarray |
| 2 | + |
| 3 | +local isUnderThreshold(value: int, threshold: int) :: ok: bool { |
| 4 | + ~> (ok: value < threshold) |
| 5 | +} |
| 6 | + |
| 7 | +share main() { |
| 8 | + out("=== Conditional formula optimization ===") |
| 9 | + |
| 10 | + optimized := [0 to 60] |
| 11 | + for i of [0 to 60] { |
| 12 | + if i % 2 == 0 { |
| 13 | + base := i * 3 |
| 14 | + optimized[i] = base + 1 |
| 15 | + } elif i % 5 == 0 { |
| 16 | + shifted := i + 7 |
| 17 | + optimized[i] = shifted * 2 |
| 18 | + } else { |
| 19 | + optimized[i] = i - 1 |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + out("optimized[10]=" + optimized[10] + " expected=31") |
| 24 | + out("optimized[25]=" + optimized[25] + " expected=64") |
| 25 | + out("optimized[7]=" + optimized[7] + " expected=6") |
| 26 | + |
| 27 | + fallback := [0 to 40] |
| 28 | + for i of [0 to 40] { |
| 29 | + if isUnderThreshold(i, 12) { |
| 30 | + fallback[i] = i + 100 |
| 31 | + } else { |
| 32 | + fallback[i] = i - 100 |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + out("fallback[5]=" + fallback[5] + " expected=105") |
| 37 | + out("fallback[20]=" + fallback[20] + " expected=-80") |
| 38 | + |
| 39 | + constantTrue := [0 to 12] |
| 40 | + for i of [0 to 12] { |
| 41 | + if true { |
| 42 | + constantTrue[i] = i + 9 |
| 43 | + } else { |
| 44 | + constantTrue[i] = i - 9 |
| 45 | + } |
| 46 | + } |
| 47 | + out("constantTrue[3]=" + constantTrue[3] + " expected=12") |
| 48 | + |
| 49 | + constantFalse := [0 to 12] |
| 50 | + for i of [0 to 12] { |
| 51 | + if false { |
| 52 | + constantFalse[i] = i + 9 |
| 53 | + } else { |
| 54 | + constantFalse[i] = i - 9 |
| 55 | + } |
| 56 | + } |
| 57 | + out("constantFalse[3]=" + constantFalse[3] + " expected=-6") |
| 58 | + |
| 59 | + fusedMapped := optimized.map(\(x) x + 3).map(\(x) x * 2) |
| 60 | + fusedFiltered := fusedMapped.filter(">=", 40).filter("<=", 150) |
| 61 | + out("fusedMapped[0]=" + fusedMapped[0] + " expected=8") |
| 62 | + out("fusedMapped[1]=" + fusedMapped[1] + " expected=6") |
| 63 | + out("fusedFiltered[0]=" + fusedFiltered[0] + " expected=54") |
| 64 | + |
| 65 | + zippedFused := optimized.map(\(x) x + 1).zip(optimized.map(\(x) x + 2), \(left, right) left + right) |
| 66 | + out("zippedFused[0]=" + zippedFused[0] + " expected=5") |
| 67 | + out("zippedFused[1]=" + zippedFused[1] + " expected=3") |
| 68 | +} |
0 commit comments