Add constant optimization test

This commit is contained in:
coolreader18 2019-08-03 22:44:36 -05:00
parent e9ad1f64ee
commit a01853fad5
2 changed files with 26 additions and 2 deletions

View file

@ -117,7 +117,7 @@ pub(crate) type Label = usize;
impl<O> Default for Compiler<O>
where
O: OutputStream + Default,
O: OutputStream,
{
fn default() -> Self {
Compiler::new(0)
@ -1916,7 +1916,7 @@ mod tests {
use rustpython_parser::parser;
fn compile_exec(source: &str) -> CodeObject {
let mut compiler = Compiler::default();
let mut compiler: Compiler = Default::default();
compiler.source_path = Some("source_path".to_string());
compiler.push_new_code_object("<module>".to_string());
let ast = parser::parse_program(&source.to_string()).unwrap();
@ -2003,4 +2003,24 @@ mod tests {
code.instructions
);
}
#[test]
fn test_constant_optimization() {
let code = compile_exec("1 + 2 + 3 + 4\n1.5 * 2.5");
assert_eq!(
code.instructions,
vec![
LoadConst {
value: Integer { value: 10.into() }
},
Pop,
LoadConst {
value: Float { value: 3.75 }
},
Pop,
LoadConst { value: None },
ReturnValue,
]
);
}
}