Files
RustAst/tests/optimizer.rs
T
Brummel 49ad76e7c3 Refactor: Remove integration tests and organize into modules
This commit refactors the test suite by removing the monolithic
`integration_test.rs` file.
The tests have been categorized and moved into dedicated modules within
the `tests/` directory:

- `tests/parser.rs`: Contains tests for parsing various literal values.
- `tests/destructuring.rs`: Houses tests related to destructuring
  syntax.
- `tests/closures.rs`: Includes tests for closure behavior and related
  optimizations.
- `tests/optimizer.rs`: Contains tests specifically targeting optimizer
  logic and regressions.
- `tests/records.rs`: Holds tests for record syntax and associated
  optimizations.
- `tests/rtl.rs`: Contains tests for runtime library operators and
  functions.
- `tests/pipeline.rs`: Tests for pipeline functionality.
- `tests/macros.rs`: Tests for macro expansion and related issues.
- `tests/error_recovery.rs`: Includes tests for compiler error handling
  and recovery mechanisms.
- `tests/examples.rs`: Verifies the execution of example scripts.

This reorganization improves test discoverability and maintainability.
The `lib.rs` file has also been updated to reflect these changes by
removing the reference to the old `integration_test` module.
The `type_checker.rs` file has had some tests removed, as they are now
covered by the more specific tests in `tests/error_recovery.rs`.
2026-03-21 15:56:07 +01:00

138 lines
3.7 KiB
Rust

use myc::ast::environment::Environment;
#[test]
fn test_optimizer_destructuring_inlining_and_mutation() {
let env = Environment::new();
// 1. Destructuring definition should allow inlining if not mutated
let source_inline = "(do (def [x y] [10 20]) (+ x y))";
let res_inline = env.run_script(source_inline).unwrap();
assert_eq!(format!("{}", res_inline), "30");
// 2. Destructuring definition should NOT be inlined if mutated (regression test)
let source_mutation = r#"
(do
(def [a b] [1 2])
(def f (fn [] (assign a (+ a b))))
(f)
a)
"#;
let res_mutation = env.run_script(source_mutation).unwrap();
assert_eq!(format!("{}", res_mutation), "3");
}
#[test]
fn test_inline_two_lambdas_same_slots() {
let env = Environment::new();
// Two lambdas each binding a local at slot 0 get inlined into the same scope.
// Slot remapping must prevent collision between them.
let source = r#"
(do
(def add1 (fn [x] (+ x 1)))
(def add2 (fn [x] (+ x 2)))
(+ (add1 10) (add2 20)))
"#;
assert_eq!(format!("{}", env.run_script(source).unwrap()), "33");
let dump = env.dump_ast(source).unwrap();
assert!(
dump.contains("Constant: 33"),
"Should be fully folded to Constant 33. Dump:\n{}",
dump
);
}
#[test]
fn test_multipass_const_propagation() {
let env = Environment::new();
// A constant chain where each step is only resolvable after the previous has been folded.
// Requires multiple optimizer passes.
let source = r#"
(do
(def a 1)
(def b (+ a 1))
(def c (+ b 1))
(+ c 1))
"#;
assert_eq!(format!("{}", env.run_script(source).unwrap()), "4");
let dump = env.dump_ast(source).unwrap();
assert!(
dump.contains("Constant: 4"),
"Multi-pass chain should fold to Constant 4. Dump:\n{}",
dump
);
}
#[test]
fn test_dead_def_destructuring_correctness() {
// A dead destructuring def must not change the result — with or without optimization.
let source = "(do (def [x y] [1 2]) 42)";
let mut env_opt = Environment::new();
env_opt.optimization = true;
let mut env_no = Environment::new();
env_no.optimization = false;
assert_eq!(
format!("{}", env_opt.run_script(source).unwrap()),
format!("{}", env_no.run_script(source).unwrap())
);
}
#[test]
fn test_dead_destructuring_def_eliminated() {
let env = Environment::new();
// A dead destructuring def with a pure RHS should be eliminated by the optimizer.
let dump = env.dump_ast("(do (def [x y] [1 2]) 42)").unwrap();
assert!(
!dump.contains("Def"),
"Dead destructuring def should be eliminated from AST. Dump:\n{}",
dump
);
}
#[test]
fn test_optimizer_inlining_slot_clash_repro() {
let mut env = Environment::new();
env.optimization = true;
let source = r#"
(do
(def outer (fn [length]
(do
(def history (series 100 :float))
(fn [val] length)
)
))
((outer 20) 5)
)
"#;
let res = env.run_script(source).unwrap();
assert_eq!(format!("{}", res), "20");
}
#[test]
fn test_reproduce_inlining_slot_clash_crash() {
let mut env = Environment::new();
env.optimization = true;
let source = r#"
(do
(def MY_SMA
(fn [length]
(do
(def history (series 100 :float))
(fn [val]
(do
(push history val)
(len history))))))
(def src (create-random-ohlc 42 100))
(def s (.close src))
[(pipe [s] (MY_SMA 5))]
[(pipe [s] (MY_SMA 20))])
"#;
let res = env.run_script(source);
assert!(res.is_ok(), "Inlining slot clash triggered: {:?}", res.err());
}