Files
RustPython/jit/tests/misc_tests.rs
T
+1 45c0fa0e77 Floating Point Power and While Loop JIT (#5614)
* Initial commit for power function

* Float power jit developed

* Addded support for Floats and Ints

* Integration Testing for JITPower implementation.

* Update instructions.rs

* Update int_tests.rs

* Update float_tests.rs

* Updated cranelift and power function to use magic bs

* Updated the compile_fpow accuracy

* updating while loop test

* Update instructions.rs

cranelift more like painlift

* Update instructions.rs

* Update instructions.rs

* initial commit for making stable PR ready features

* fixed final edge case for compile_ipow

* fixed final edge case for compile_ipow

* commenting out compile_ipow

* fixed spelling errors

* removed unused tests

* forgot to run clippy

* Cleaned the branch

* While loop implementation for Cranelift 0.116.1

* Floating Point

* Removed testing print statement

* Resolved some formatting

* Fixed cargo fmt warning

* Fixed int div and int exp issues

* Fixed formatting

---------

Co-authored-by: Nick <nick@Samanthas-MBP.wi.rr.com>
Co-authored-by: JoeLoparco <loparcojoseph@gmail.com>
Co-authored-by: Daniel O'Hear <149127239+dohear@users.noreply.github.com>
Co-authored-by: Joseph Loparco <149088810+JoeLoparco@users.noreply.github.com>
Co-authored-by: Nick <nick@pcp090057pcs.mu.edu>
Co-authored-by: dohear <daniel.ohear@marquette.edu>
Co-authored-by: Nathan Rusch <nathan.rusch@icloud.com>
Co-authored-by: Nick <nick@pcp093574pcs.mu.edu>
Co-authored-by: Joseph Loparco <--global loparcoJoseph@gmail.com>
Co-authored-by: Nick <nick@Samanthas-MacBook-Pro.local>
2025-03-19 13:50:42 -05:00

127 lines
3.3 KiB
Rust

use rustpython_jit::{AbiValue, JitArgumentError};
// TODO currently broken
// #[test]
// fn test_no_return_value() {
// let func = jit_function! { func() => r##"
// def func():
// pass
// "## };
//
// assert_eq!(func(), Ok(()));
// }
#[test]
fn test_invoke() {
let func = jit_function! { func => r##"
def func(a: int, b: float):
return 1
"## };
assert_eq!(
func.invoke(&[AbiValue::Int(1)]),
Err(JitArgumentError::WrongNumberOfArguments)
);
assert_eq!(
func.invoke(&[AbiValue::Int(1), AbiValue::Float(2.0), AbiValue::Int(0)]),
Err(JitArgumentError::WrongNumberOfArguments)
);
assert_eq!(
func.invoke(&[AbiValue::Int(1), AbiValue::Int(1)]),
Err(JitArgumentError::ArgumentTypeMismatch)
);
assert_eq!(
func.invoke(&[AbiValue::Int(1), AbiValue::Float(2.0)]),
Ok(Some(AbiValue::Int(1)))
);
}
#[test]
fn test_args_builder() {
let func = jit_function! { func=> r##"
def func(a: int, b: float):
return 1
"## };
let mut args_builder = func.args_builder();
assert_eq!(args_builder.set(0, AbiValue::Int(1)), Ok(()));
assert!(args_builder.is_set(0));
assert!(!args_builder.is_set(1));
assert_eq!(
args_builder.set(1, AbiValue::Int(1)),
Err(JitArgumentError::ArgumentTypeMismatch)
);
assert!(args_builder.is_set(0));
assert!(!args_builder.is_set(1));
assert!(args_builder.into_args().is_none());
let mut args_builder = func.args_builder();
assert_eq!(args_builder.set(0, AbiValue::Int(1)), Ok(()));
assert_eq!(args_builder.set(1, AbiValue::Float(1.0)), Ok(()));
assert!(args_builder.is_set(0));
assert!(args_builder.is_set(1));
let args = args_builder.into_args();
assert!(args.is_some());
assert_eq!(args.unwrap().invoke(), Some(AbiValue::Int(1)));
}
#[test]
fn test_if_else() {
let if_else = jit_function! { if_else(a:i64) -> i64 => r##"
def if_else(a: int):
if a:
return 42
else:
return 0
# Prevent type failure from implicit `return None`
return 0
"## };
assert_eq!(if_else(0), Ok(0));
assert_eq!(if_else(1), Ok(42));
assert_eq!(if_else(-1), Ok(42));
assert_eq!(if_else(100), Ok(42));
}
#[test]
fn test_while_loop() {
let while_loop = jit_function! { while_loop(a:i64) -> i64 => r##"
def while_loop(a: int):
b = 0
while a > 0:
b += 1
a -= 1
return b
"## };
assert_eq!(while_loop(0), Ok(0));
assert_eq!(while_loop(-1), Ok(0));
assert_eq!(while_loop(1), Ok(1));
assert_eq!(while_loop(10), Ok(10));
}
#[test]
fn test_unpack_tuple() {
let unpack_tuple = jit_function! { unpack_tuple(a:i64, b:i64) -> i64 => r##"
def unpack_tuple(a: int, b: int):
a, b = b, a
return a
"## };
assert_eq!(unpack_tuple(0, 1), Ok(1));
assert_eq!(unpack_tuple(1, 2), Ok(2));
}
#[test]
fn test_recursive_fib() {
let fib = jit_function! { fib(n: i64) -> i64 => r##"
def fib(n: int) -> int:
if n == 0 or n == 1:
return 1
return fib(n-1) + fib(n-2)
"## };
assert_eq!(fib(10), Ok(89));
}