mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Clippy rules (test related) (#7968)
This commit is contained in:
1 parent
b28fec108b
commit
9a0291188c
30 files changed
+1737
-1706
No files matched your search
@@ -333,8 +333,11 @@ similar_names = "allow"
|
||||
|
||||
# restriction lints
|
||||
alloc_instead_of_core = "warn"
|
||||
cfg_not_test = "warn"
|
||||
redundant_test_prefix = "warn"
|
||||
std_instead_of_alloc = "warn"
|
||||
std_instead_of_core = "warn"
|
||||
tests_outside_test_module = "warn"
|
||||
|
||||
# nursery lints to enforce gradually
|
||||
debug_assert_with_mut_call = "warn"
|
||||
|
||||
@@ -305,7 +305,7 @@ mod tests {
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn test_raised_exception() {
|
||||
fn raised_exception() {
|
||||
Python::attach(|py| {
|
||||
PyTypeError::new_err(py.None()).restore(py);
|
||||
assert!(PyErr::occurred(py));
|
||||
@@ -315,7 +315,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_is_instance() {
|
||||
fn error_is_instance() {
|
||||
Python::attach(|py| {
|
||||
let err = PyTypeError::new_err(py.None());
|
||||
assert!(err.is_instance_of::<PyTypeError>(py));
|
||||
|
||||
@@ -58,7 +58,7 @@ mod tests {
|
||||
use rustpython_vm::vm::thread::{current_vm_is_set, with_current_vm};
|
||||
|
||||
#[test]
|
||||
fn test_new_thread() {
|
||||
fn new_thread() {
|
||||
Python::attach(|_py| {
|
||||
with_current_vm(|_vm| {
|
||||
assert!(
|
||||
@@ -83,7 +83,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_current_vm_main_thread() {
|
||||
fn current_vm_main_thread() {
|
||||
Python::initialize();
|
||||
|
||||
// let RustPython create a vm for this thread.
|
||||
@@ -105,7 +105,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gilstate_release_detaches_external_thread() {
|
||||
fn gilstate_release_detaches_external_thread() {
|
||||
Python::initialize();
|
||||
|
||||
std::thread::spawn(|| {
|
||||
|
||||
+639
-639
File diff suppressed because it is too large.
Load diff
@@ -866,7 +866,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_fill_and_align() {
|
||||
fn fill_and_align() {
|
||||
assert_eq!(
|
||||
"%10s"
|
||||
.parse::<CFormatSpec>()
|
||||
@@ -898,7 +898,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_key() {
|
||||
fn parse_key() {
|
||||
let expected = Ok(CFormatSpecKeyed {
|
||||
mapping_key: Some("amount".to_owned()),
|
||||
spec: CFormatSpec {
|
||||
@@ -926,7 +926,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_parse_key_fail() {
|
||||
fn format_parse_key_fail() {
|
||||
assert_eq!(
|
||||
"%(aged".parse::<CFormatString>(),
|
||||
Err(CFormatError {
|
||||
@@ -937,7 +937,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_parse_type_fail() {
|
||||
fn format_parse_type_fail() {
|
||||
assert_eq!(
|
||||
"Hello %n".parse::<CFormatString>(),
|
||||
Err(CFormatError {
|
||||
@@ -948,7 +948,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_incomplete_format_fail() {
|
||||
fn incomplete_format_fail() {
|
||||
assert_eq!(
|
||||
"Hello %".parse::<CFormatString>(),
|
||||
Err(CFormatError {
|
||||
@@ -959,7 +959,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_flags() {
|
||||
fn parse_flags() {
|
||||
let expected = Ok(CFormatSpec {
|
||||
format_type: CFormatType::Number(CNumberType::DecimalD),
|
||||
min_field_width: Some(CFormatQuantity::Amount(10)),
|
||||
@@ -975,7 +975,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_and_format_string() {
|
||||
fn parse_and_format_string() {
|
||||
assert_eq!(
|
||||
"%5.4s"
|
||||
.parse::<CFormatSpec>()
|
||||
@@ -1007,7 +1007,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_and_format_unicode_string() {
|
||||
fn parse_and_format_unicode_string() {
|
||||
assert_eq!(
|
||||
"%.2s"
|
||||
.parse::<CFormatSpec>()
|
||||
@@ -1018,7 +1018,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_and_format_number() {
|
||||
fn parse_and_format_number() {
|
||||
assert_eq!(
|
||||
"%5d"
|
||||
.parse::<CFormatSpec>()
|
||||
@@ -1092,7 +1092,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_and_format_float() {
|
||||
fn parse_and_format_float() {
|
||||
assert_eq!(
|
||||
"%f".parse::<CFormatSpec>().unwrap().format_float(1.2345),
|
||||
"1.234500"
|
||||
@@ -1130,7 +1130,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_parse() {
|
||||
fn format_parse() {
|
||||
let fmt = "Hello, my name is %s and I'm %d years old";
|
||||
let expected = Ok(CFormatString {
|
||||
parts: vec![
|
||||
|
||||
+17
-17
@@ -1447,7 +1447,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_fill_and_align() {
|
||||
fn fill_and_align() {
|
||||
let parse_fill_and_align = |text| {
|
||||
let (fill, align, rest) = parse_fill_and_align(str::as_ref(text));
|
||||
(
|
||||
@@ -1479,7 +1479,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_width_only() {
|
||||
fn width_only() {
|
||||
let expected = Ok(FormatSpec {
|
||||
conversion: None,
|
||||
fill: None,
|
||||
@@ -1495,7 +1495,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fill_and_width() {
|
||||
fn fill_and_width() {
|
||||
let expected = Ok(FormatSpec {
|
||||
conversion: None,
|
||||
fill: Some('<'.into()),
|
||||
@@ -1511,7 +1511,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all() {
|
||||
fn all() {
|
||||
let expected = Ok(FormatSpec {
|
||||
conversion: None,
|
||||
fill: Some('<'.into()),
|
||||
@@ -1531,7 +1531,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_bool() {
|
||||
fn format_bool_basic() {
|
||||
assert_eq!(format_bool("b", true), Ok("1".to_owned()));
|
||||
assert_eq!(format_bool("b", false), Ok("0".to_owned()));
|
||||
assert_eq!(format_bool("d", true), Ok("1".to_owned()));
|
||||
@@ -1563,7 +1563,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_int() {
|
||||
fn format_int() {
|
||||
assert_eq!(
|
||||
FormatSpec::parse("d")
|
||||
.unwrap()
|
||||
@@ -1609,7 +1609,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_int_sep() {
|
||||
fn format_int_sep() {
|
||||
let spec = FormatSpec::parse(",").expect("");
|
||||
assert_eq!(spec.grouping_option, Some(FormatGrouping::Comma));
|
||||
assert_eq!(
|
||||
@@ -1619,7 +1619,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_int_width_and_grouping() {
|
||||
fn format_int_width_and_grouping() {
|
||||
// issue #5922: width + comma grouping should pad left, not inside the number
|
||||
let spec = FormatSpec::parse("10,").unwrap();
|
||||
let result = spec.format_int(&BigInt::from(1234)).unwrap();
|
||||
@@ -1627,7 +1627,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_int_padding_with_grouping() {
|
||||
fn format_int_padding_with_grouping() {
|
||||
// CPython behavior: f'{1234:010,}' results in "00,001,234"
|
||||
let spec1 = FormatSpec::parse("010,").unwrap();
|
||||
let result1 = spec1.format_int(&BigInt::from(1234)).unwrap();
|
||||
@@ -1650,7 +1650,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_int_non_aftersign_zero_padding() {
|
||||
fn format_int_non_aftersign_zero_padding() {
|
||||
// CPython behavior: f'{1234:0>10,}' results in "000001,234"
|
||||
let spec = FormatSpec::parse("0>10,").unwrap();
|
||||
let result = spec.format_int(&BigInt::from(1234)).unwrap();
|
||||
@@ -1658,7 +1658,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_parse() {
|
||||
fn format_parse() {
|
||||
let expected = Ok(FormatString {
|
||||
format_parts: vec![
|
||||
FormatPart::Literal("abcd".into()),
|
||||
@@ -1680,12 +1680,12 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_parse_multi_byte_char() {
|
||||
fn format_parse_multi_byte_char() {
|
||||
assert!(FormatString::from_str("{a:%ЫйЯЧ}".as_ref()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_parse_fail() {
|
||||
fn format_parse_fail() {
|
||||
assert_eq!(
|
||||
FormatString::from_str("{s".as_ref()),
|
||||
Err(FormatParseError::UnmatchedBracket)
|
||||
@@ -1693,7 +1693,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_square_brackets_inside_format() {
|
||||
fn square_brackets_inside_format() {
|
||||
assert_eq!(
|
||||
FormatString::from_str("{[:123]}".as_ref()),
|
||||
Ok(FormatString {
|
||||
@@ -1721,7 +1721,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_parse_escape() {
|
||||
fn format_parse_escape() {
|
||||
let expected = Ok(FormatString {
|
||||
format_parts: vec![
|
||||
FormatPart::Literal("{".into()),
|
||||
@@ -1738,7 +1738,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_invalid_specification() {
|
||||
fn format_invalid_specification() {
|
||||
assert_eq!(
|
||||
FormatSpec::parse("%3"),
|
||||
Err(FormatSpecError::InvalidFormatSpecifier)
|
||||
@@ -1770,7 +1770,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_field_name() {
|
||||
fn parse_field_name() {
|
||||
let parse = |s: &str| FieldName::parse(s.as_ref());
|
||||
assert_eq!(
|
||||
parse(""),
|
||||
|
||||
@@ -664,7 +664,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_chars() {
|
||||
fn get_chars_basic() {
|
||||
let s = "0123456789";
|
||||
assert_eq!(get_chars(s, 3..7), "3456");
|
||||
assert_eq!(get_chars(s, 3..7), &s[3..7]);
|
||||
|
||||
@@ -1287,7 +1287,7 @@ mod tests {
|
||||
use alloc::{vec, vec::Vec};
|
||||
|
||||
#[test]
|
||||
fn test_exception_table_encode_decode() {
|
||||
fn exception_table_encode_decode() {
|
||||
let entries = vec![
|
||||
ExceptionTableEntry::new(0, 10, 20, 2, false),
|
||||
ExceptionTableEntry::new(15, 25, 30, 1, true),
|
||||
@@ -1325,7 +1325,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_exception_table_empty() {
|
||||
fn exception_table_empty() {
|
||||
let entries: Vec<ExceptionTableEntry> = vec![];
|
||||
let encoded = encode_exception_table(&entries);
|
||||
assert!(encoded.is_empty());
|
||||
@@ -1333,7 +1333,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_exception_table_single_entry() {
|
||||
fn exception_table_single_entry() {
|
||||
let entries = vec![ExceptionTableEntry::new(5, 15, 100, 3, true)];
|
||||
let encoded = encode_exception_table(&entries);
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_le_varint_roundtrip() {
|
||||
fn le_varint_roundtrip() {
|
||||
// Little-endian is only used internally in linetable,
|
||||
// no read function needed outside of linetable parsing.
|
||||
let mut buf = Vec::new();
|
||||
@@ -118,7 +118,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_be_varint_roundtrip() {
|
||||
fn be_varint_roundtrip() {
|
||||
for &val in &[0u32, 1, 63, 64, 127, 128, 4095, 4096, 1_000_000] {
|
||||
let mut buf = Vec::new();
|
||||
write_varint_be(&mut buf, val);
|
||||
@@ -129,7 +129,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_be_varint_with_start() {
|
||||
fn be_varint_with_start() {
|
||||
let mut buf = Vec::new();
|
||||
write_varint_with_start(&mut buf, 42);
|
||||
write_varint_with_start(&mut buf, 100);
|
||||
|
||||
+79
-74
@@ -339,29 +339,33 @@ pub fn _compile_symtable(
|
||||
res.map_err(|e| e.into_codegen_error(source_file.name().to_owned()).into())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile() {
|
||||
let code = "x = 'abc'";
|
||||
let compiled = compile(code, Mode::Single, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_compile_phello() {
|
||||
let code = r#"
|
||||
#[test]
|
||||
fn basic_compile() {
|
||||
let code = "x = 'abc'";
|
||||
let compiled = compile(code, Mode::Single, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_phello() {
|
||||
let code = r#"
|
||||
initialized = True
|
||||
def main():
|
||||
print("Hello world!")
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
"#;
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile_if_elif_else() {
|
||||
let code = r#"
|
||||
#[test]
|
||||
fn compile_if_elif_else() {
|
||||
let code = r#"
|
||||
if False:
|
||||
pass
|
||||
elif False:
|
||||
@@ -371,31 +375,31 @@ elif False:
|
||||
else:
|
||||
pass
|
||||
"#;
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile_lambda() {
|
||||
let code = r#"
|
||||
#[test]
|
||||
fn compile_lambda() {
|
||||
let code = r#"
|
||||
lambda: 'a'
|
||||
"#;
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile_lambda2() {
|
||||
let code = r#"
|
||||
#[test]
|
||||
fn compile_lambda2() {
|
||||
let code = r#"
|
||||
(lambda x: f'hello, {x}')('world}')
|
||||
"#;
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile_lambda3() {
|
||||
let code = r#"
|
||||
#[test]
|
||||
fn compile_lambda3() {
|
||||
let code = r#"
|
||||
def g():
|
||||
pass
|
||||
def f():
|
||||
@@ -406,69 +410,69 @@ def f():
|
||||
else:
|
||||
return g
|
||||
"#;
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile_int() {
|
||||
let code = r#"
|
||||
#[test]
|
||||
fn compile_int() {
|
||||
let code = r#"
|
||||
a = 0xFF
|
||||
"#;
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile_bigint() {
|
||||
let code = r#"
|
||||
#[test]
|
||||
fn compile_bigint() {
|
||||
let code = r#"
|
||||
a = 0xFFFFFFFFFFFFFFFFFFFFFFFF
|
||||
"#;
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
let compiled = compile(code, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile_fstring() {
|
||||
let code1 = r#"
|
||||
#[test]
|
||||
fn compile_fstring() {
|
||||
let code1 = r#"
|
||||
assert f"1" == '1'
|
||||
"#;
|
||||
let compiled = compile(code1, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
let compiled = compile(code1, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
|
||||
let code2 = r#"
|
||||
let code2 = r#"
|
||||
assert f"{1}" == '1'
|
||||
"#;
|
||||
let compiled = compile(code2, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
let code3 = r#"
|
||||
let compiled = compile(code2, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
let code3 = r#"
|
||||
assert f"{1+1}" == '2'
|
||||
"#;
|
||||
let compiled = compile(code3, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
let compiled = compile(code3, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
|
||||
let code4 = r#"
|
||||
let code4 = r#"
|
||||
assert f"{{{(lambda: f'{1}')}" == '{1'
|
||||
"#;
|
||||
let compiled = compile(code4, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
let compiled = compile(code4, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
|
||||
let code5 = r#"
|
||||
let code5 = r#"
|
||||
assert f"a{1}" == 'a1'
|
||||
"#;
|
||||
let compiled = compile(code5, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
let compiled = compile(code5, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
|
||||
let code6 = r#"
|
||||
let code6 = r#"
|
||||
assert f"{{{(lambda x: f'hello, {x}')('world}')}" == '{hello, world}'
|
||||
"#;
|
||||
let compiled = compile(code6, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
let compiled = compile(code6, Mode::Exec, "<>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple_enum() {
|
||||
let code = r#"
|
||||
#[test]
|
||||
fn simple_enum() {
|
||||
let code = r#"
|
||||
import enum
|
||||
@enum._simple_enum(enum.IntFlag, boundary=enum.KEEP)
|
||||
class RegexFlag:
|
||||
@@ -476,6 +480,7 @@ class RegexFlag:
|
||||
DEBUG = 1
|
||||
print(RegexFlag.NOFLAG & RegexFlag.DEBUG)
|
||||
"#;
|
||||
let compiled = compile(code, Mode::Exec, "<string>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
let compiled = compile(code, Mode::Exec, "<string>", CompileOpts::default());
|
||||
dbg!(compiled.expect("compile error"));
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ mod test {
|
||||
use super::DB;
|
||||
|
||||
#[test]
|
||||
fn test_db() {
|
||||
fn db_basic() {
|
||||
let doc = DB.get("array._array_reconstructor");
|
||||
assert!(doc.is_some());
|
||||
}
|
||||
|
||||
+107
-104
@@ -1,43 +1,45 @@
|
||||
#[test]
|
||||
fn test_return() {
|
||||
let return_ = jit_function! { return_(a: bool) -> bool => r##"
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn basic_return() {
|
||||
let return_ = jit_function! { return_(a: bool) -> bool => r##"
|
||||
def return_(a: bool):
|
||||
return a
|
||||
"## };
|
||||
|
||||
assert_eq!(return_(true), Ok(true));
|
||||
assert_eq!(return_(false), Ok(false));
|
||||
}
|
||||
assert_eq!(return_(true), Ok(true));
|
||||
assert_eq!(return_(false), Ok(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_const() {
|
||||
let const_true = jit_function! { const_true(a: i64) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_const() {
|
||||
let const_true = jit_function! { const_true(a: i64) -> bool => r##"
|
||||
def const_true(a: int):
|
||||
return True
|
||||
"## };
|
||||
assert_eq!(const_true(0), Ok(true));
|
||||
assert_eq!(const_true(0), Ok(true));
|
||||
|
||||
let const_false = jit_function! { const_false(a: i64) -> bool => r##"
|
||||
let const_false = jit_function! { const_false(a: i64) -> bool => r##"
|
||||
def const_false(a: int):
|
||||
return False
|
||||
"## };
|
||||
assert_eq!(const_false(0), Ok(false));
|
||||
}
|
||||
assert_eq!(const_false(0), Ok(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not() {
|
||||
let not_ = jit_function! { not_(a: bool) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_not() {
|
||||
let not_ = jit_function! { not_(a: bool) -> bool => r##"
|
||||
def not_(a: bool):
|
||||
return not a
|
||||
"## };
|
||||
|
||||
assert_eq!(not_(true), Ok(false));
|
||||
assert_eq!(not_(false), Ok(true));
|
||||
}
|
||||
assert_eq!(not_(true), Ok(false));
|
||||
assert_eq!(not_(false), Ok(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_not() {
|
||||
let if_not = jit_function! { if_not(a: bool) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_if_not() {
|
||||
let if_not = jit_function! { if_not(a: bool) -> i64 => r##"
|
||||
def if_not(a: bool):
|
||||
if not a:
|
||||
return 0
|
||||
@@ -47,156 +49,157 @@ fn test_if_not() {
|
||||
return -1
|
||||
"## };
|
||||
|
||||
assert_eq!(if_not(true), Ok(1));
|
||||
assert_eq!(if_not(false), Ok(0));
|
||||
}
|
||||
assert_eq!(if_not(true), Ok(1));
|
||||
assert_eq!(if_not(false), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eq() {
|
||||
let eq = jit_function! { eq(a:bool, b:bool) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_eq() {
|
||||
let eq = jit_function! { eq(a:bool, b:bool) -> i64 => r##"
|
||||
def eq(a: bool, b: bool):
|
||||
if a == b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(eq(false, false), Ok(1));
|
||||
assert_eq!(eq(true, true), Ok(1));
|
||||
assert_eq!(eq(false, true), Ok(0));
|
||||
assert_eq!(eq(true, false), Ok(0));
|
||||
}
|
||||
assert_eq!(eq(false, false), Ok(1));
|
||||
assert_eq!(eq(true, true), Ok(1));
|
||||
assert_eq!(eq(false, true), Ok(0));
|
||||
assert_eq!(eq(true, false), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eq_with_integers() {
|
||||
let eq = jit_function! { eq(a:bool, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn eq_with_integers() {
|
||||
let eq = jit_function! { eq(a:bool, b:i64) -> i64 => r##"
|
||||
def eq(a: bool, b: int):
|
||||
if a == b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(eq(false, 0), Ok(1));
|
||||
assert_eq!(eq(true, 1), Ok(1));
|
||||
assert_eq!(eq(false, 1), Ok(0));
|
||||
assert_eq!(eq(true, 0), Ok(0));
|
||||
}
|
||||
assert_eq!(eq(false, 0), Ok(1));
|
||||
assert_eq!(eq(true, 1), Ok(1));
|
||||
assert_eq!(eq(false, 1), Ok(0));
|
||||
assert_eq!(eq(true, 0), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gt() {
|
||||
let gt = jit_function! { gt(a:bool, b:bool) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_gt() {
|
||||
let gt = jit_function! { gt(a:bool, b:bool) -> i64 => r##"
|
||||
def gt(a: bool, b: bool):
|
||||
if a > b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(gt(false, false), Ok(0));
|
||||
assert_eq!(gt(true, true), Ok(0));
|
||||
assert_eq!(gt(false, true), Ok(0));
|
||||
assert_eq!(gt(true, false), Ok(1));
|
||||
}
|
||||
assert_eq!(gt(false, false), Ok(0));
|
||||
assert_eq!(gt(true, true), Ok(0));
|
||||
assert_eq!(gt(false, true), Ok(0));
|
||||
assert_eq!(gt(true, false), Ok(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gt_with_integers() {
|
||||
let gt = jit_function! { gt(a:i64, b:bool) -> i64 => r##"
|
||||
#[test]
|
||||
fn gt_with_integers() {
|
||||
let gt = jit_function! { gt(a:i64, b:bool) -> i64 => r##"
|
||||
def gt(a: int, b: bool):
|
||||
if a > b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(gt(0, false), Ok(0));
|
||||
assert_eq!(gt(1, true), Ok(0));
|
||||
assert_eq!(gt(0, true), Ok(0));
|
||||
assert_eq!(gt(1, false), Ok(1));
|
||||
}
|
||||
assert_eq!(gt(0, false), Ok(0));
|
||||
assert_eq!(gt(1, true), Ok(0));
|
||||
assert_eq!(gt(0, true), Ok(0));
|
||||
assert_eq!(gt(1, false), Ok(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lt() {
|
||||
let lt = jit_function! { lt(a:bool, b:bool) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_lt() {
|
||||
let lt = jit_function! { lt(a:bool, b:bool) -> i64 => r##"
|
||||
def lt(a: bool, b: bool):
|
||||
if a < b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(lt(false, false), Ok(0));
|
||||
assert_eq!(lt(true, true), Ok(0));
|
||||
assert_eq!(lt(false, true), Ok(1));
|
||||
assert_eq!(lt(true, false), Ok(0));
|
||||
}
|
||||
assert_eq!(lt(false, false), Ok(0));
|
||||
assert_eq!(lt(true, true), Ok(0));
|
||||
assert_eq!(lt(false, true), Ok(1));
|
||||
assert_eq!(lt(true, false), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lt_with_integers() {
|
||||
let lt = jit_function! { lt(a:i64, b:bool) -> i64 => r##"
|
||||
#[test]
|
||||
fn lt_with_integers() {
|
||||
let lt = jit_function! { lt(a:i64, b:bool) -> i64 => r##"
|
||||
def lt(a: int, b: bool):
|
||||
if a < b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(lt(0, false), Ok(0));
|
||||
assert_eq!(lt(1, true), Ok(0));
|
||||
assert_eq!(lt(0, true), Ok(1));
|
||||
assert_eq!(lt(1, false), Ok(0));
|
||||
}
|
||||
assert_eq!(lt(0, false), Ok(0));
|
||||
assert_eq!(lt(1, true), Ok(0));
|
||||
assert_eq!(lt(0, true), Ok(1));
|
||||
assert_eq!(lt(1, false), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gte() {
|
||||
let gte = jit_function! { gte(a:bool, b:bool) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_gte() {
|
||||
let gte = jit_function! { gte(a:bool, b:bool) -> i64 => r##"
|
||||
def gte(a: bool, b: bool):
|
||||
if a >= b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(gte(false, false), Ok(1));
|
||||
assert_eq!(gte(true, true), Ok(1));
|
||||
assert_eq!(gte(false, true), Ok(0));
|
||||
assert_eq!(gte(true, false), Ok(1));
|
||||
}
|
||||
assert_eq!(gte(false, false), Ok(1));
|
||||
assert_eq!(gte(true, true), Ok(1));
|
||||
assert_eq!(gte(false, true), Ok(0));
|
||||
assert_eq!(gte(true, false), Ok(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gte_with_integers() {
|
||||
let gte = jit_function! { gte(a:bool, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn gte_with_integers() {
|
||||
let gte = jit_function! { gte(a:bool, b:i64) -> i64 => r##"
|
||||
def gte(a: bool, b: int):
|
||||
if a >= b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(gte(false, 0), Ok(1));
|
||||
assert_eq!(gte(true, 1), Ok(1));
|
||||
assert_eq!(gte(false, 1), Ok(0));
|
||||
assert_eq!(gte(true, 0), Ok(1));
|
||||
}
|
||||
assert_eq!(gte(false, 0), Ok(1));
|
||||
assert_eq!(gte(true, 1), Ok(1));
|
||||
assert_eq!(gte(false, 1), Ok(0));
|
||||
assert_eq!(gte(true, 0), Ok(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lte() {
|
||||
let lte = jit_function! { lte(a:bool, b:bool) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_lte() {
|
||||
let lte = jit_function! { lte(a:bool, b:bool) -> i64 => r##"
|
||||
def lte(a: bool, b: bool):
|
||||
if a <= b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(lte(false, false), Ok(1));
|
||||
assert_eq!(lte(true, true), Ok(1));
|
||||
assert_eq!(lte(false, true), Ok(1));
|
||||
assert_eq!(lte(true, false), Ok(0));
|
||||
}
|
||||
assert_eq!(lte(false, false), Ok(1));
|
||||
assert_eq!(lte(true, true), Ok(1));
|
||||
assert_eq!(lte(false, true), Ok(1));
|
||||
assert_eq!(lte(true, false), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lte_with_integers() {
|
||||
let lte = jit_function! { lte(a:bool, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn lte_with_integers() {
|
||||
let lte = jit_function! { lte(a:bool, b:i64) -> i64 => r##"
|
||||
def lte(a: bool, b: int):
|
||||
if a <= b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(lte(false, 0), Ok(1));
|
||||
assert_eq!(lte(true, 1), Ok(1));
|
||||
assert_eq!(lte(false, 1), Ok(1));
|
||||
assert_eq!(lte(true, 0), Ok(0));
|
||||
assert_eq!(lte(false, 0), Ok(1));
|
||||
assert_eq!(lte(true, 1), Ok(1));
|
||||
assert_eq!(lte(false, 1), Ok(1));
|
||||
assert_eq!(lte(true, 0), Ok(0));
|
||||
}
|
||||
}
|
||||
+294
-291
@@ -1,379 +1,382 @@
|
||||
macro_rules! assert_approx_eq {
|
||||
($left:expr, $right:expr) => {
|
||||
match ($left, $right) {
|
||||
(Ok(lhs), Ok(rhs)) => approx::assert_relative_eq!(lhs, rhs),
|
||||
(lhs, rhs) => assert_eq!(lhs, rhs),
|
||||
}
|
||||
};
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
macro_rules! assert_approx_eq {
|
||||
($left:expr, $right:expr) => {
|
||||
match ($left, $right) {
|
||||
(Ok(lhs), Ok(rhs)) => approx::assert_relative_eq!(lhs, rhs),
|
||||
(lhs, rhs) => assert_eq!(lhs, rhs),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! assert_bits_eq {
|
||||
($left:expr, $right:expr) => {
|
||||
match ($left, $right) {
|
||||
(Ok(lhs), Ok(rhs)) => assert!(lhs.to_bits() == rhs.to_bits()),
|
||||
(lhs, rhs) => assert_eq!(lhs, rhs),
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! assert_bits_eq {
|
||||
($left:expr, $right:expr) => {
|
||||
match ($left, $right) {
|
||||
(Ok(lhs), Ok(rhs)) => assert!(lhs.to_bits() == rhs.to_bits()),
|
||||
(lhs, rhs) => assert_eq!(lhs, rhs),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add() {
|
||||
let add = jit_function! { add(a:f64, b:f64) -> f64 => r##"
|
||||
#[test]
|
||||
fn basic_add() {
|
||||
let add = jit_function! { add(a:f64, b:f64) -> f64 => r##"
|
||||
def add(a: float, b: float):
|
||||
return a + b
|
||||
"## };
|
||||
|
||||
assert_approx_eq!(add(5.5, 10.2), Ok(15.7));
|
||||
assert_approx_eq!(add(-4.5, 7.6), Ok(3.1));
|
||||
assert_approx_eq!(add(-5.2, -3.9), Ok(-9.1));
|
||||
assert_bits_eq!(add(-5.2, f64::NAN), Ok(f64::NAN));
|
||||
assert_eq!(add(2.0, f64::INFINITY), Ok(f64::INFINITY));
|
||||
assert_eq!(add(-2.0, f64::NEG_INFINITY), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(add(1.0, f64::NEG_INFINITY), Ok(f64::NEG_INFINITY));
|
||||
}
|
||||
assert_approx_eq!(add(5.5, 10.2), Ok(15.7));
|
||||
assert_approx_eq!(add(-4.5, 7.6), Ok(3.1));
|
||||
assert_approx_eq!(add(-5.2, -3.9), Ok(-9.1));
|
||||
assert_bits_eq!(add(-5.2, f64::NAN), Ok(f64::NAN));
|
||||
assert_eq!(add(2.0, f64::INFINITY), Ok(f64::INFINITY));
|
||||
assert_eq!(add(-2.0, f64::NEG_INFINITY), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(add(1.0, f64::NEG_INFINITY), Ok(f64::NEG_INFINITY));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_with_integer() {
|
||||
let add = jit_function! { add(a:f64, b:i64) -> f64 => r##"
|
||||
#[test]
|
||||
fn add_with_integer() {
|
||||
let add = jit_function! { add(a:f64, b:i64) -> f64 => r##"
|
||||
def add(a: float, b: int):
|
||||
return a + b
|
||||
"## };
|
||||
|
||||
assert_approx_eq!(add(5.5, 10), Ok(15.5));
|
||||
assert_approx_eq!(add(-4.6, 7), Ok(2.4));
|
||||
assert_approx_eq!(add(-5.2, -3), Ok(-8.2));
|
||||
}
|
||||
assert_approx_eq!(add(5.5, 10), Ok(15.5));
|
||||
assert_approx_eq!(add(-4.6, 7), Ok(2.4));
|
||||
assert_approx_eq!(add(-5.2, -3), Ok(-8.2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sub() {
|
||||
let sub = jit_function! { sub(a:f64, b:f64) -> f64 => r##"
|
||||
#[test]
|
||||
fn basic_sub() {
|
||||
let sub = jit_function! { sub(a:f64, b:f64) -> f64 => r##"
|
||||
def sub(a: float, b: float):
|
||||
return a - b
|
||||
"## };
|
||||
|
||||
assert_approx_eq!(sub(5.2, 3.6), Ok(1.6));
|
||||
assert_approx_eq!(sub(3.4, 4.2), Ok(-0.8));
|
||||
assert_approx_eq!(sub(-2.1, 1.3), Ok(-3.4));
|
||||
assert_approx_eq!(sub(3.1, -1.3), Ok(4.4));
|
||||
assert_bits_eq!(sub(-5.2, f64::NAN), Ok(f64::NAN));
|
||||
assert_eq!(sub(f64::INFINITY, 2.0), Ok(f64::INFINITY));
|
||||
assert_eq!(sub(-2.0, f64::NEG_INFINITY), Ok(f64::INFINITY));
|
||||
assert_eq!(sub(1.0, f64::INFINITY), Ok(f64::NEG_INFINITY));
|
||||
}
|
||||
assert_approx_eq!(sub(5.2, 3.6), Ok(1.6));
|
||||
assert_approx_eq!(sub(3.4, 4.2), Ok(-0.8));
|
||||
assert_approx_eq!(sub(-2.1, 1.3), Ok(-3.4));
|
||||
assert_approx_eq!(sub(3.1, -1.3), Ok(4.4));
|
||||
assert_bits_eq!(sub(-5.2, f64::NAN), Ok(f64::NAN));
|
||||
assert_eq!(sub(f64::INFINITY, 2.0), Ok(f64::INFINITY));
|
||||
assert_eq!(sub(-2.0, f64::NEG_INFINITY), Ok(f64::INFINITY));
|
||||
assert_eq!(sub(1.0, f64::INFINITY), Ok(f64::NEG_INFINITY));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sub_with_integer() {
|
||||
let sub = jit_function! { sub(a:i64, b:f64) -> f64 => r##"
|
||||
#[test]
|
||||
fn sub_with_integer() {
|
||||
let sub = jit_function! { sub(a:i64, b:f64) -> f64 => r##"
|
||||
def sub(a: int, b: float):
|
||||
return a - b
|
||||
"## };
|
||||
|
||||
assert_approx_eq!(sub(5, 3.6), Ok(1.4));
|
||||
assert_approx_eq!(sub(3, -4.2), Ok(7.2));
|
||||
assert_approx_eq!(sub(-2, 1.3), Ok(-3.3));
|
||||
assert_approx_eq!(sub(-3, -1.3), Ok(-1.7));
|
||||
}
|
||||
assert_approx_eq!(sub(5, 3.6), Ok(1.4));
|
||||
assert_approx_eq!(sub(3, -4.2), Ok(7.2));
|
||||
assert_approx_eq!(sub(-2, 1.3), Ok(-3.3));
|
||||
assert_approx_eq!(sub(-3, -1.3), Ok(-1.7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mul() {
|
||||
let mul = jit_function! { mul(a:f64, b:f64) -> f64 => r##"
|
||||
#[test]
|
||||
fn basic_mul() {
|
||||
let mul = jit_function! { mul(a:f64, b:f64) -> f64 => r##"
|
||||
def mul(a: float, b: float):
|
||||
return a * b
|
||||
"## };
|
||||
|
||||
assert_approx_eq!(mul(5.2, 2.0), Ok(10.4));
|
||||
assert_approx_eq!(mul(3.4, -1.7), Ok(-5.779999999999999));
|
||||
assert_bits_eq!(mul(1.0, 0.0), Ok(0.0f64));
|
||||
assert_bits_eq!(mul(1.0, -0.0), Ok(-0.0f64));
|
||||
assert_bits_eq!(mul(-1.0, 0.0), Ok(-0.0f64));
|
||||
assert_bits_eq!(mul(-1.0, -0.0), Ok(0.0f64));
|
||||
assert_bits_eq!(mul(-5.2, f64::NAN), Ok(f64::NAN));
|
||||
assert_eq!(mul(1.0, f64::INFINITY), Ok(f64::INFINITY));
|
||||
assert_eq!(mul(1.0, f64::NEG_INFINITY), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(mul(-1.0, f64::INFINITY), Ok(f64::NEG_INFINITY));
|
||||
assert!(mul(0.0, f64::INFINITY).unwrap().is_nan());
|
||||
assert_eq!(mul(f64::NEG_INFINITY, f64::INFINITY), Ok(f64::NEG_INFINITY));
|
||||
}
|
||||
assert_approx_eq!(mul(5.2, 2.0), Ok(10.4));
|
||||
assert_approx_eq!(mul(3.4, -1.7), Ok(-5.779999999999999));
|
||||
assert_bits_eq!(mul(1.0, 0.0), Ok(0.0f64));
|
||||
assert_bits_eq!(mul(1.0, -0.0), Ok(-0.0f64));
|
||||
assert_bits_eq!(mul(-1.0, 0.0), Ok(-0.0f64));
|
||||
assert_bits_eq!(mul(-1.0, -0.0), Ok(0.0f64));
|
||||
assert_bits_eq!(mul(-5.2, f64::NAN), Ok(f64::NAN));
|
||||
assert_eq!(mul(1.0, f64::INFINITY), Ok(f64::INFINITY));
|
||||
assert_eq!(mul(1.0, f64::NEG_INFINITY), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(mul(-1.0, f64::INFINITY), Ok(f64::NEG_INFINITY));
|
||||
assert!(mul(0.0, f64::INFINITY).unwrap().is_nan());
|
||||
assert_eq!(mul(f64::NEG_INFINITY, f64::INFINITY), Ok(f64::NEG_INFINITY));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mul_with_integer() {
|
||||
let mul = jit_function! { mul(a:f64, b:i64) -> f64 => r##"
|
||||
#[test]
|
||||
fn mul_with_integer() {
|
||||
let mul = jit_function! { mul(a:f64, b:i64) -> f64 => r##"
|
||||
def mul(a: float, b: int):
|
||||
return a * b
|
||||
"## };
|
||||
|
||||
assert_approx_eq!(mul(5.2, 2), Ok(10.4));
|
||||
assert_approx_eq!(mul(3.4, -1), Ok(-3.4));
|
||||
assert_bits_eq!(mul(1.0, 0), Ok(0.0f64));
|
||||
assert_bits_eq!(mul(-0.0, 1), Ok(-0.0f64));
|
||||
assert_bits_eq!(mul(0.0, -1), Ok(-0.0f64));
|
||||
assert_bits_eq!(mul(-0.0, -1), Ok(0.0f64));
|
||||
}
|
||||
assert_approx_eq!(mul(5.2, 2), Ok(10.4));
|
||||
assert_approx_eq!(mul(3.4, -1), Ok(-3.4));
|
||||
assert_bits_eq!(mul(1.0, 0), Ok(0.0f64));
|
||||
assert_bits_eq!(mul(-0.0, 1), Ok(-0.0f64));
|
||||
assert_bits_eq!(mul(0.0, -1), Ok(-0.0f64));
|
||||
assert_bits_eq!(mul(-0.0, -1), Ok(0.0f64));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_power() {
|
||||
let pow = jit_function! { pow(a:f64, b:f64) -> f64 => r##"
|
||||
#[test]
|
||||
fn basic_power() {
|
||||
let pow = jit_function! { pow(a:f64, b:f64) -> f64 => r##"
|
||||
def pow(a:float, b: float):
|
||||
return a**b
|
||||
"##};
|
||||
// Test base cases
|
||||
assert_approx_eq!(pow(0.0, 0.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(0.0, 1.0), Ok(0.0));
|
||||
assert_approx_eq!(pow(1.0, 0.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(1.0, 1.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(1.0, -1.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(-1.0, 0.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(-1.0, 1.0), Ok(-1.0));
|
||||
assert_approx_eq!(pow(-1.0, -1.0), Ok(-1.0));
|
||||
// Test base cases
|
||||
assert_approx_eq!(pow(0.0, 0.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(0.0, 1.0), Ok(0.0));
|
||||
assert_approx_eq!(pow(1.0, 0.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(1.0, 1.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(1.0, -1.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(-1.0, 0.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(-1.0, 1.0), Ok(-1.0));
|
||||
assert_approx_eq!(pow(-1.0, -1.0), Ok(-1.0));
|
||||
|
||||
// NaN and Infinity cases
|
||||
assert_approx_eq!(pow(f64::NAN, 0.0), Ok(1.0));
|
||||
//assert_approx_eq!(pow(f64::NAN, 1.0), Ok(f64::NAN)); // Return the correct answer but fails compare
|
||||
//assert_approx_eq!(pow(0.0, f64::NAN), Ok(f64::NAN)); // Return the correct answer but fails compare
|
||||
assert_approx_eq!(pow(f64::INFINITY, 0.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(f64::INFINITY, 1.0), Ok(f64::INFINITY));
|
||||
assert_approx_eq!(pow(f64::INFINITY, f64::INFINITY), Ok(f64::INFINITY));
|
||||
// Negative infinity cases:
|
||||
// For any exponent of 0.0, the result is 1.0.
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, 0.0), Ok(1.0));
|
||||
// For negative infinity base, when b is an odd integer, result is -infinity;
|
||||
// when b is even, result is +infinity.
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, 1.0), Ok(f64::NEG_INFINITY));
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, 2.0), Ok(f64::INFINITY));
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, 3.0), Ok(f64::NEG_INFINITY));
|
||||
// Exponent -infinity gives 0.0.
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, f64::NEG_INFINITY), Ok(0.0));
|
||||
// NaN and Infinity cases
|
||||
assert_approx_eq!(pow(f64::NAN, 0.0), Ok(1.0));
|
||||
//assert_approx_eq!(pow(f64::NAN, 1.0), Ok(f64::NAN)); // Return the correct answer but fails compare
|
||||
//assert_approx_eq!(pow(0.0, f64::NAN), Ok(f64::NAN)); // Return the correct answer but fails compare
|
||||
assert_approx_eq!(pow(f64::INFINITY, 0.0), Ok(1.0));
|
||||
assert_approx_eq!(pow(f64::INFINITY, 1.0), Ok(f64::INFINITY));
|
||||
assert_approx_eq!(pow(f64::INFINITY, f64::INFINITY), Ok(f64::INFINITY));
|
||||
// Negative infinity cases:
|
||||
// For any exponent of 0.0, the result is 1.0.
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, 0.0), Ok(1.0));
|
||||
// For negative infinity base, when b is an odd integer, result is -infinity;
|
||||
// when b is even, result is +infinity.
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, 1.0), Ok(f64::NEG_INFINITY));
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, 2.0), Ok(f64::INFINITY));
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, 3.0), Ok(f64::NEG_INFINITY));
|
||||
// Exponent -infinity gives 0.0.
|
||||
assert_approx_eq!(pow(f64::NEG_INFINITY, f64::NEG_INFINITY), Ok(0.0));
|
||||
|
||||
// Test positive float base, positive float exponent
|
||||
assert_approx_eq!(pow(2.0, 2.0), Ok(4.0));
|
||||
assert_approx_eq!(pow(3.0, 3.0), Ok(27.0));
|
||||
assert_approx_eq!(pow(4.0, 4.0), Ok(256.0));
|
||||
assert_approx_eq!(pow(2.0, 3.0), Ok(8.0));
|
||||
assert_approx_eq!(pow(2.0, 4.0), Ok(16.0));
|
||||
// Test negative float base, positive float exponent (integral exponents only)
|
||||
assert_approx_eq!(pow(-2.0, 2.0), Ok(4.0));
|
||||
assert_approx_eq!(pow(-3.0, 3.0), Ok(-27.0));
|
||||
assert_approx_eq!(pow(-4.0, 4.0), Ok(256.0));
|
||||
assert_approx_eq!(pow(-2.0, 3.0), Ok(-8.0));
|
||||
assert_approx_eq!(pow(-2.0, 4.0), Ok(16.0));
|
||||
// Test positive float base, positive float exponent
|
||||
assert_approx_eq!(pow(2.5, 2.0), Ok(6.25));
|
||||
assert_approx_eq!(pow(3.5, 3.0), Ok(42.875));
|
||||
assert_approx_eq!(pow(4.5, 4.0), Ok(410.0625));
|
||||
assert_approx_eq!(pow(2.5, 3.0), Ok(15.625));
|
||||
assert_approx_eq!(pow(2.5, 4.0), Ok(39.0625));
|
||||
// Test negative float base, positive float exponent (integral exponents only)
|
||||
assert_approx_eq!(pow(-2.5, 2.0), Ok(6.25));
|
||||
assert_approx_eq!(pow(-3.5, 3.0), Ok(-42.875));
|
||||
assert_approx_eq!(pow(-4.5, 4.0), Ok(410.0625));
|
||||
assert_approx_eq!(pow(-2.5, 3.0), Ok(-15.625));
|
||||
assert_approx_eq!(pow(-2.5, 4.0), Ok(39.0625));
|
||||
// Test positive float base, positive float exponent with non-integral exponents
|
||||
assert_approx_eq!(pow(2.0, 2.5), Ok(5.656854249492381));
|
||||
assert_approx_eq!(pow(3.0, 3.5), Ok(46.76537180435969));
|
||||
assert_approx_eq!(pow(4.0, 4.5), Ok(512.0));
|
||||
assert_approx_eq!(pow(2.0, 3.5), Ok(11.313708498984761));
|
||||
assert_approx_eq!(pow(2.0, 4.5), Ok(22.627416997969522));
|
||||
// Test positive float base, negative float exponent
|
||||
assert_approx_eq!(pow(2.0, -2.5), Ok(0.1767766952966369));
|
||||
assert_approx_eq!(pow(3.0, -3.5), Ok(0.021383343303319473));
|
||||
assert_approx_eq!(pow(4.0, -4.5), Ok(0.001953125));
|
||||
assert_approx_eq!(pow(2.0, -3.5), Ok(0.08838834764831845));
|
||||
assert_approx_eq!(pow(2.0, -4.5), Ok(0.04419417382415922));
|
||||
// Test negative float base, negative float exponent (integral exponents only)
|
||||
assert_approx_eq!(pow(-2.0, -2.0), Ok(0.25));
|
||||
assert_approx_eq!(pow(-3.0, -3.0), Ok(-0.037037037037037035));
|
||||
assert_approx_eq!(pow(-4.0, -4.0), Ok(0.00390625));
|
||||
assert_approx_eq!(pow(-2.0, -3.0), Ok(-0.125));
|
||||
assert_approx_eq!(pow(-2.0, -4.0), Ok(0.0625));
|
||||
// Test positive float base, positive float exponent
|
||||
assert_approx_eq!(pow(2.0, 2.0), Ok(4.0));
|
||||
assert_approx_eq!(pow(3.0, 3.0), Ok(27.0));
|
||||
assert_approx_eq!(pow(4.0, 4.0), Ok(256.0));
|
||||
assert_approx_eq!(pow(2.0, 3.0), Ok(8.0));
|
||||
assert_approx_eq!(pow(2.0, 4.0), Ok(16.0));
|
||||
// Test negative float base, positive float exponent (integral exponents only)
|
||||
assert_approx_eq!(pow(-2.0, 2.0), Ok(4.0));
|
||||
assert_approx_eq!(pow(-3.0, 3.0), Ok(-27.0));
|
||||
assert_approx_eq!(pow(-4.0, 4.0), Ok(256.0));
|
||||
assert_approx_eq!(pow(-2.0, 3.0), Ok(-8.0));
|
||||
assert_approx_eq!(pow(-2.0, 4.0), Ok(16.0));
|
||||
// Test positive float base, positive float exponent
|
||||
assert_approx_eq!(pow(2.5, 2.0), Ok(6.25));
|
||||
assert_approx_eq!(pow(3.5, 3.0), Ok(42.875));
|
||||
assert_approx_eq!(pow(4.5, 4.0), Ok(410.0625));
|
||||
assert_approx_eq!(pow(2.5, 3.0), Ok(15.625));
|
||||
assert_approx_eq!(pow(2.5, 4.0), Ok(39.0625));
|
||||
// Test negative float base, positive float exponent (integral exponents only)
|
||||
assert_approx_eq!(pow(-2.5, 2.0), Ok(6.25));
|
||||
assert_approx_eq!(pow(-3.5, 3.0), Ok(-42.875));
|
||||
assert_approx_eq!(pow(-4.5, 4.0), Ok(410.0625));
|
||||
assert_approx_eq!(pow(-2.5, 3.0), Ok(-15.625));
|
||||
assert_approx_eq!(pow(-2.5, 4.0), Ok(39.0625));
|
||||
// Test positive float base, positive float exponent with non-integral exponents
|
||||
assert_approx_eq!(pow(2.0, 2.5), Ok(5.656854249492381));
|
||||
assert_approx_eq!(pow(3.0, 3.5), Ok(46.76537180435969));
|
||||
assert_approx_eq!(pow(4.0, 4.5), Ok(512.0));
|
||||
assert_approx_eq!(pow(2.0, 3.5), Ok(11.313708498984761));
|
||||
assert_approx_eq!(pow(2.0, 4.5), Ok(22.627416997969522));
|
||||
// Test positive float base, negative float exponent
|
||||
assert_approx_eq!(pow(2.0, -2.5), Ok(0.1767766952966369));
|
||||
assert_approx_eq!(pow(3.0, -3.5), Ok(0.021383343303319473));
|
||||
assert_approx_eq!(pow(4.0, -4.5), Ok(0.001953125));
|
||||
assert_approx_eq!(pow(2.0, -3.5), Ok(0.08838834764831845));
|
||||
assert_approx_eq!(pow(2.0, -4.5), Ok(0.04419417382415922));
|
||||
// Test negative float base, negative float exponent (integral exponents only)
|
||||
assert_approx_eq!(pow(-2.0, -2.0), Ok(0.25));
|
||||
assert_approx_eq!(pow(-3.0, -3.0), Ok(-0.037037037037037035));
|
||||
assert_approx_eq!(pow(-4.0, -4.0), Ok(0.00390625));
|
||||
assert_approx_eq!(pow(-2.0, -3.0), Ok(-0.125));
|
||||
assert_approx_eq!(pow(-2.0, -4.0), Ok(0.0625));
|
||||
|
||||
// Currently negative float base with non-integral exponent is not supported:
|
||||
// assert_approx_eq!(pow(-2.0, 2.5), Ok(5.656854249492381));
|
||||
// assert_approx_eq!(pow(-3.0, 3.5), Ok(-46.76537180435969));
|
||||
// assert_approx_eq!(pow(-4.0, 4.5), Ok(512.0));
|
||||
// assert_approx_eq!(pow(-2.0, -2.5), Ok(0.1767766952966369));
|
||||
// assert_approx_eq!(pow(-3.0, -3.5), Ok(0.021383343303319473));
|
||||
// assert_approx_eq!(pow(-4.0, -4.5), Ok(0.001953125));
|
||||
// Currently negative float base with non-integral exponent is not supported:
|
||||
// assert_approx_eq!(pow(-2.0, 2.5), Ok(5.656854249492381));
|
||||
// assert_approx_eq!(pow(-3.0, 3.5), Ok(-46.76537180435969));
|
||||
// assert_approx_eq!(pow(-4.0, 4.5), Ok(512.0));
|
||||
// assert_approx_eq!(pow(-2.0, -2.5), Ok(0.1767766952966369));
|
||||
// assert_approx_eq!(pow(-3.0, -3.5), Ok(0.021383343303319473));
|
||||
// assert_approx_eq!(pow(-4.0, -4.5), Ok(0.001953125));
|
||||
|
||||
// Extra cases **NOTE** these are not all working:
|
||||
// * If they are commented in then they work
|
||||
// * If they are commented out with a number that is the current return value it throws vs the expected value
|
||||
// * If they are commented out with a "fail to run" that means I couldn't get them to work, could add a case for really big or small values
|
||||
// 1e308^2.0
|
||||
assert_approx_eq!(pow(1e308, 2.0), Ok(f64::INFINITY));
|
||||
// 1e308^(1e-2)
|
||||
assert_approx_eq!(pow(1e308, 1e-2), Ok(1202.2644346174131));
|
||||
// 1e-308^2.0
|
||||
//assert_approx_eq!(pow(1e-308, 2.0), Ok(0.0)); // --8.403311421507407
|
||||
// 1e-308^-2.0
|
||||
assert_approx_eq!(pow(1e-308, -2.0), Ok(f64::INFINITY));
|
||||
// 1e100^(1e50)
|
||||
//assert_approx_eq!(pow(1e100, 1e50), Ok(1.0000000000000002e+150)); // fail to run (Crashes as "illegal hardware instruction")
|
||||
// 1e50^(1e-100)
|
||||
assert_approx_eq!(pow(1e50, 1e-100), Ok(1.0));
|
||||
// 1e308^(-1e2)
|
||||
//assert_approx_eq!(pow(1e308, -1e2), Ok(0.0)); // 2.961801792837933e25
|
||||
// 1e-308^(1e2)
|
||||
//assert_approx_eq!(pow(1e-308, 1e2), Ok(f64::INFINITY)); // 1.6692559244043896e46
|
||||
// 1e308^(-1e308)
|
||||
// assert_approx_eq!(pow(1e308, -1e308), Ok(0.0)); // fail to run (Crashes as "illegal hardware instruction")
|
||||
// 1e-308^(1e308)
|
||||
// assert_approx_eq!(pow(1e-308, 1e308), Ok(0.0)); // fail to run (Crashes as "illegal hardware instruction")
|
||||
}
|
||||
// Extra cases **NOTE** these are not all working:
|
||||
// * If they are commented in then they work
|
||||
// * If they are commented out with a number that is the current return value it throws vs the expected value
|
||||
// * If they are commented out with a "fail to run" that means I couldn't get them to work, could add a case for really big or small values
|
||||
// 1e308^2.0
|
||||
assert_approx_eq!(pow(1e308, 2.0), Ok(f64::INFINITY));
|
||||
// 1e308^(1e-2)
|
||||
assert_approx_eq!(pow(1e308, 1e-2), Ok(1202.2644346174131));
|
||||
// 1e-308^2.0
|
||||
//assert_approx_eq!(pow(1e-308, 2.0), Ok(0.0)); // --8.403311421507407
|
||||
// 1e-308^-2.0
|
||||
assert_approx_eq!(pow(1e-308, -2.0), Ok(f64::INFINITY));
|
||||
// 1e100^(1e50)
|
||||
//assert_approx_eq!(pow(1e100, 1e50), Ok(1.0000000000000002e+150)); // fail to run (Crashes as "illegal hardware instruction")
|
||||
// 1e50^(1e-100)
|
||||
assert_approx_eq!(pow(1e50, 1e-100), Ok(1.0));
|
||||
// 1e308^(-1e2)
|
||||
//assert_approx_eq!(pow(1e308, -1e2), Ok(0.0)); // 2.961801792837933e25
|
||||
// 1e-308^(1e2)
|
||||
//assert_approx_eq!(pow(1e-308, 1e2), Ok(f64::INFINITY)); // 1.6692559244043896e46
|
||||
// 1e308^(-1e308)
|
||||
// assert_approx_eq!(pow(1e308, -1e308), Ok(0.0)); // fail to run (Crashes as "illegal hardware instruction")
|
||||
// 1e-308^(1e308)
|
||||
// assert_approx_eq!(pow(1e-308, 1e308), Ok(0.0)); // fail to run (Crashes as "illegal hardware instruction")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div() {
|
||||
let div = jit_function! { div(a:f64, b:f64) -> f64 => r##"
|
||||
#[test]
|
||||
fn basic_div() {
|
||||
let div = jit_function! { div(a:f64, b:f64) -> f64 => r##"
|
||||
def div(a: float, b: float):
|
||||
return a / b
|
||||
"## };
|
||||
|
||||
assert_approx_eq!(div(5.2, 2.0), Ok(2.6));
|
||||
assert_approx_eq!(div(3.4, -1.7), Ok(-2.0));
|
||||
assert_eq!(div(1.0, 0.0), Ok(f64::INFINITY));
|
||||
assert_eq!(div(1.0, -0.0), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(div(-1.0, 0.0), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(div(-1.0, -0.0), Ok(f64::INFINITY));
|
||||
assert_bits_eq!(div(-5.2, f64::NAN), Ok(f64::NAN));
|
||||
assert_eq!(div(f64::INFINITY, 2.0), Ok(f64::INFINITY));
|
||||
assert_bits_eq!(div(-2.0, f64::NEG_INFINITY), Ok(0.0f64));
|
||||
assert_bits_eq!(div(1.0, f64::INFINITY), Ok(0.0f64));
|
||||
assert_bits_eq!(div(2.0, f64::NEG_INFINITY), Ok(-0.0f64));
|
||||
assert_bits_eq!(div(-1.0, f64::INFINITY), Ok(-0.0f64));
|
||||
}
|
||||
assert_approx_eq!(div(5.2, 2.0), Ok(2.6));
|
||||
assert_approx_eq!(div(3.4, -1.7), Ok(-2.0));
|
||||
assert_eq!(div(1.0, 0.0), Ok(f64::INFINITY));
|
||||
assert_eq!(div(1.0, -0.0), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(div(-1.0, 0.0), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(div(-1.0, -0.0), Ok(f64::INFINITY));
|
||||
assert_bits_eq!(div(-5.2, f64::NAN), Ok(f64::NAN));
|
||||
assert_eq!(div(f64::INFINITY, 2.0), Ok(f64::INFINITY));
|
||||
assert_bits_eq!(div(-2.0, f64::NEG_INFINITY), Ok(0.0f64));
|
||||
assert_bits_eq!(div(1.0, f64::INFINITY), Ok(0.0f64));
|
||||
assert_bits_eq!(div(2.0, f64::NEG_INFINITY), Ok(-0.0f64));
|
||||
assert_bits_eq!(div(-1.0, f64::INFINITY), Ok(-0.0f64));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div_with_integer() {
|
||||
let div = jit_function! { div(a:f64, b:i64) -> f64 => r##"
|
||||
#[test]
|
||||
fn div_with_integer() {
|
||||
let div = jit_function! { div(a:f64, b:i64) -> f64 => r##"
|
||||
def div(a: float, b: int):
|
||||
return a / b
|
||||
"## };
|
||||
|
||||
assert_approx_eq!(div(5.2, 2), Ok(2.6));
|
||||
assert_approx_eq!(div(3.4, -1), Ok(-3.4));
|
||||
assert_eq!(div(1.0, 0), Ok(f64::INFINITY));
|
||||
assert_eq!(div(1.0, -0), Ok(f64::INFINITY));
|
||||
assert_eq!(div(-1.0, 0), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(div(-1.0, -0), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(div(f64::INFINITY, 2), Ok(f64::INFINITY));
|
||||
assert_eq!(div(f64::NEG_INFINITY, 3), Ok(f64::NEG_INFINITY));
|
||||
}
|
||||
assert_approx_eq!(div(5.2, 2), Ok(2.6));
|
||||
assert_approx_eq!(div(3.4, -1), Ok(-3.4));
|
||||
assert_eq!(div(1.0, 0), Ok(f64::INFINITY));
|
||||
assert_eq!(div(1.0, -0), Ok(f64::INFINITY));
|
||||
assert_eq!(div(-1.0, 0), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(div(-1.0, -0), Ok(f64::NEG_INFINITY));
|
||||
assert_eq!(div(f64::INFINITY, 2), Ok(f64::INFINITY));
|
||||
assert_eq!(div(f64::NEG_INFINITY, 3), Ok(f64::NEG_INFINITY));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_bool() {
|
||||
let if_bool = jit_function! { if_bool(a:f64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_if_bool() {
|
||||
let if_bool = jit_function! { if_bool(a:f64) -> i64 => r##"
|
||||
def if_bool(a: float):
|
||||
if a:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(if_bool(5.2), Ok(1));
|
||||
assert_eq!(if_bool(-3.4), Ok(1));
|
||||
assert_eq!(if_bool(f64::NAN), Ok(1));
|
||||
assert_eq!(if_bool(f64::INFINITY), Ok(1));
|
||||
assert_eq!(if_bool(5.2), Ok(1));
|
||||
assert_eq!(if_bool(-3.4), Ok(1));
|
||||
assert_eq!(if_bool(f64::NAN), Ok(1));
|
||||
assert_eq!(if_bool(f64::INFINITY), Ok(1));
|
||||
|
||||
assert_eq!(if_bool(0.0), Ok(0));
|
||||
}
|
||||
assert_eq!(if_bool(0.0), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float_eq() {
|
||||
let float_eq = jit_function! { float_eq(a: f64, b: f64) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_float_eq() {
|
||||
let float_eq = jit_function! { float_eq(a: f64, b: f64) -> bool => r##"
|
||||
def float_eq(a: float, b: float):
|
||||
return a == b
|
||||
"## };
|
||||
|
||||
assert_eq!(float_eq(2.0, 2.0), Ok(true));
|
||||
assert_eq!(float_eq(3.4, -1.7), Ok(false));
|
||||
assert_eq!(float_eq(0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_eq(-0.0, -0.0), Ok(true));
|
||||
assert_eq!(float_eq(-0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_eq(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_eq(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_eq(f64::INFINITY, f64::NEG_INFINITY), Ok(false));
|
||||
}
|
||||
assert_eq!(float_eq(2.0, 2.0), Ok(true));
|
||||
assert_eq!(float_eq(3.4, -1.7), Ok(false));
|
||||
assert_eq!(float_eq(0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_eq(-0.0, -0.0), Ok(true));
|
||||
assert_eq!(float_eq(-0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_eq(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_eq(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_eq(f64::INFINITY, f64::NEG_INFINITY), Ok(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float_ne() {
|
||||
let float_ne = jit_function! { float_ne(a: f64, b: f64) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_float_ne() {
|
||||
let float_ne = jit_function! { float_ne(a: f64, b: f64) -> bool => r##"
|
||||
def float_ne(a: float, b: float):
|
||||
return a != b
|
||||
"## };
|
||||
|
||||
assert_eq!(float_ne(2.0, 2.0), Ok(false));
|
||||
assert_eq!(float_ne(3.4, -1.7), Ok(true));
|
||||
assert_eq!(float_ne(0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_ne(-0.0, -0.0), Ok(false));
|
||||
assert_eq!(float_ne(-0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_ne(-5.2, f64::NAN), Ok(true));
|
||||
assert_eq!(float_ne(f64::NAN, f64::NAN), Ok(true));
|
||||
assert_eq!(float_ne(f64::INFINITY, f64::NEG_INFINITY), Ok(true));
|
||||
}
|
||||
assert_eq!(float_ne(2.0, 2.0), Ok(false));
|
||||
assert_eq!(float_ne(3.4, -1.7), Ok(true));
|
||||
assert_eq!(float_ne(0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_ne(-0.0, -0.0), Ok(false));
|
||||
assert_eq!(float_ne(-0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_ne(-5.2, f64::NAN), Ok(true));
|
||||
assert_eq!(float_ne(f64::NAN, f64::NAN), Ok(true));
|
||||
assert_eq!(float_ne(f64::INFINITY, f64::NEG_INFINITY), Ok(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float_gt() {
|
||||
let float_gt = jit_function! { float_gt(a: f64, b: f64) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_float_gt() {
|
||||
let float_gt = jit_function! { float_gt(a: f64, b: f64) -> bool => r##"
|
||||
def float_gt(a: float, b: float):
|
||||
return a > b
|
||||
"## };
|
||||
|
||||
assert_eq!(float_gt(2.0, 2.0), Ok(false));
|
||||
assert_eq!(float_gt(3.4, -1.7), Ok(true));
|
||||
assert_eq!(float_gt(0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_gt(-0.0, -0.0), Ok(false));
|
||||
assert_eq!(float_gt(-0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_gt(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_gt(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_gt(f64::INFINITY, f64::NEG_INFINITY), Ok(true));
|
||||
}
|
||||
assert_eq!(float_gt(2.0, 2.0), Ok(false));
|
||||
assert_eq!(float_gt(3.4, -1.7), Ok(true));
|
||||
assert_eq!(float_gt(0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_gt(-0.0, -0.0), Ok(false));
|
||||
assert_eq!(float_gt(-0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_gt(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_gt(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_gt(f64::INFINITY, f64::NEG_INFINITY), Ok(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float_gte() {
|
||||
let float_gte = jit_function! { float_gte(a: f64, b: f64) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_float_gte() {
|
||||
let float_gte = jit_function! { float_gte(a: f64, b: f64) -> bool => r##"
|
||||
def float_gte(a: float, b: float):
|
||||
return a >= b
|
||||
"## };
|
||||
|
||||
assert_eq!(float_gte(2.0, 2.0), Ok(true));
|
||||
assert_eq!(float_gte(3.4, -1.7), Ok(true));
|
||||
assert_eq!(float_gte(0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_gte(-0.0, -0.0), Ok(true));
|
||||
assert_eq!(float_gte(-0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_gte(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_gte(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_gte(f64::INFINITY, f64::NEG_INFINITY), Ok(true));
|
||||
}
|
||||
assert_eq!(float_gte(2.0, 2.0), Ok(true));
|
||||
assert_eq!(float_gte(3.4, -1.7), Ok(true));
|
||||
assert_eq!(float_gte(0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_gte(-0.0, -0.0), Ok(true));
|
||||
assert_eq!(float_gte(-0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_gte(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_gte(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_gte(f64::INFINITY, f64::NEG_INFINITY), Ok(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float_lt() {
|
||||
let float_lt = jit_function! { float_lt(a: f64, b: f64) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_float_lt() {
|
||||
let float_lt = jit_function! { float_lt(a: f64, b: f64) -> bool => r##"
|
||||
def float_lt(a: float, b: float):
|
||||
return a < b
|
||||
"## };
|
||||
|
||||
assert_eq!(float_lt(2.0, 2.0), Ok(false));
|
||||
assert_eq!(float_lt(3.4, -1.7), Ok(false));
|
||||
assert_eq!(float_lt(0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_lt(-0.0, -0.0), Ok(false));
|
||||
assert_eq!(float_lt(-0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_lt(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_lt(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_lt(f64::INFINITY, f64::NEG_INFINITY), Ok(false));
|
||||
}
|
||||
assert_eq!(float_lt(2.0, 2.0), Ok(false));
|
||||
assert_eq!(float_lt(3.4, -1.7), Ok(false));
|
||||
assert_eq!(float_lt(0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_lt(-0.0, -0.0), Ok(false));
|
||||
assert_eq!(float_lt(-0.0, 0.0), Ok(false));
|
||||
assert_eq!(float_lt(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_lt(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_lt(f64::INFINITY, f64::NEG_INFINITY), Ok(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float_lte() {
|
||||
let float_lte = jit_function! { float_lte(a: f64, b: f64) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_float_lte() {
|
||||
let float_lte = jit_function! { float_lte(a: f64, b: f64) -> bool => r##"
|
||||
def float_lte(a: float, b: float):
|
||||
return a <= b
|
||||
"## };
|
||||
|
||||
assert_eq!(float_lte(2.0, 2.0), Ok(true));
|
||||
assert_eq!(float_lte(3.4, -1.7), Ok(false));
|
||||
assert_eq!(float_lte(0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_lte(-0.0, -0.0), Ok(true));
|
||||
assert_eq!(float_lte(-0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_lte(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_lte(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_lte(f64::INFINITY, f64::NEG_INFINITY), Ok(false));
|
||||
assert_eq!(float_lte(2.0, 2.0), Ok(true));
|
||||
assert_eq!(float_lte(3.4, -1.7), Ok(false));
|
||||
assert_eq!(float_lte(0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_lte(-0.0, -0.0), Ok(true));
|
||||
assert_eq!(float_lte(-0.0, 0.0), Ok(true));
|
||||
assert_eq!(float_lte(-5.2, f64::NAN), Ok(false));
|
||||
assert_eq!(float_lte(f64::NAN, f64::NAN), Ok(false));
|
||||
assert_eq!(float_lte(f64::INFINITY, f64::NEG_INFINITY), Ok(false));
|
||||
}
|
||||
}
|
||||
+213
-210
@@ -1,326 +1,329 @@
|
||||
use core::f64;
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use core::f64;
|
||||
|
||||
#[test]
|
||||
fn test_add() {
|
||||
let add = jit_function! { add(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_add() {
|
||||
let add = jit_function! { add(a:i64, b:i64) -> i64 => r##"
|
||||
def add(a: int, b: int):
|
||||
return a + b
|
||||
"## };
|
||||
|
||||
assert_eq!(add(5, 10), Ok(15));
|
||||
assert_eq!(add(-5, 12), Ok(7));
|
||||
assert_eq!(add(-5, -3), Ok(-8));
|
||||
}
|
||||
assert_eq!(add(5, 10), Ok(15));
|
||||
assert_eq!(add(-5, 12), Ok(7));
|
||||
assert_eq!(add(-5, -3), Ok(-8));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sub() {
|
||||
let sub = jit_function! { sub(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_sub() {
|
||||
let sub = jit_function! { sub(a:i64, b:i64) -> i64 => r##"
|
||||
def sub(a: int, b: int):
|
||||
return a - b
|
||||
"## };
|
||||
|
||||
assert_eq!(sub(5, 10), Ok(-5));
|
||||
assert_eq!(sub(12, 10), Ok(2));
|
||||
assert_eq!(sub(7, 10), Ok(-3));
|
||||
assert_eq!(sub(-3, -10), Ok(7));
|
||||
}
|
||||
assert_eq!(sub(5, 10), Ok(-5));
|
||||
assert_eq!(sub(12, 10), Ok(2));
|
||||
assert_eq!(sub(7, 10), Ok(-3));
|
||||
assert_eq!(sub(-3, -10), Ok(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mul() {
|
||||
let mul = jit_function! { mul(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_mul() {
|
||||
let mul = jit_function! { mul(a:i64, b:i64) -> i64 => r##"
|
||||
def mul(a: int, b: int):
|
||||
return a * b
|
||||
"## };
|
||||
|
||||
assert_eq!(mul(5, 10), Ok(50));
|
||||
assert_eq!(mul(0, 5), Ok(0));
|
||||
assert_eq!(mul(5, 0), Ok(0));
|
||||
assert_eq!(mul(0, 0), Ok(0));
|
||||
assert_eq!(mul(-5, 10), Ok(-50));
|
||||
assert_eq!(mul(5, -10), Ok(-50));
|
||||
assert_eq!(mul(-5, -10), Ok(50));
|
||||
assert_eq!(mul(999999, 999999), Ok(999998000001));
|
||||
assert_eq!(mul(i64::MAX, 1), Ok(i64::MAX));
|
||||
assert_eq!(mul(1, i64::MAX), Ok(i64::MAX));
|
||||
}
|
||||
assert_eq!(mul(5, 10), Ok(50));
|
||||
assert_eq!(mul(0, 5), Ok(0));
|
||||
assert_eq!(mul(5, 0), Ok(0));
|
||||
assert_eq!(mul(0, 0), Ok(0));
|
||||
assert_eq!(mul(-5, 10), Ok(-50));
|
||||
assert_eq!(mul(5, -10), Ok(-50));
|
||||
assert_eq!(mul(-5, -10), Ok(50));
|
||||
assert_eq!(mul(999999, 999999), Ok(999998000001));
|
||||
assert_eq!(mul(i64::MAX, 1), Ok(i64::MAX));
|
||||
assert_eq!(mul(1, i64::MAX), Ok(i64::MAX));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div() {
|
||||
let div = jit_function! { div(a:i64, b:i64) -> f64 => r##"
|
||||
#[test]
|
||||
fn basic_div() {
|
||||
let div = jit_function! { div(a:i64, b:i64) -> f64 => r##"
|
||||
def div(a: int, b: int):
|
||||
return a / b
|
||||
"## };
|
||||
|
||||
assert_eq!(div(0, 1), Ok(0.0));
|
||||
assert_eq!(div(5, 1), Ok(5.0));
|
||||
assert_eq!(div(5, 10), Ok(0.5));
|
||||
assert_eq!(div(5, 2), Ok(2.5));
|
||||
assert_eq!(div(12, 10), Ok(1.2));
|
||||
assert_eq!(div(7, 10), Ok(0.7));
|
||||
assert_eq!(div(-3, -1), Ok(3.0));
|
||||
assert_eq!(div(-3, 1), Ok(-3.0));
|
||||
assert_eq!(div(1, 1000), Ok(0.001));
|
||||
assert_eq!(div(1, 100000), Ok(0.00001));
|
||||
assert_eq!(div(2, 3), Ok(0.6666666666666666));
|
||||
assert_eq!(div(1, 3), Ok(0.3333333333333333));
|
||||
assert_eq!(div(i64::MAX, 2), Ok(4611686018427387904.0));
|
||||
assert_eq!(div(i64::MIN, 2), Ok(-4611686018427387904.0));
|
||||
assert_eq!(div(i64::MIN, -1), Ok(9223372036854775808.0)); // Overflow case
|
||||
assert_eq!(div(i64::MIN, i64::MAX), Ok(-1.0));
|
||||
}
|
||||
assert_eq!(div(0, 1), Ok(0.0));
|
||||
assert_eq!(div(5, 1), Ok(5.0));
|
||||
assert_eq!(div(5, 10), Ok(0.5));
|
||||
assert_eq!(div(5, 2), Ok(2.5));
|
||||
assert_eq!(div(12, 10), Ok(1.2));
|
||||
assert_eq!(div(7, 10), Ok(0.7));
|
||||
assert_eq!(div(-3, -1), Ok(3.0));
|
||||
assert_eq!(div(-3, 1), Ok(-3.0));
|
||||
assert_eq!(div(1, 1000), Ok(0.001));
|
||||
assert_eq!(div(1, 100000), Ok(0.00001));
|
||||
assert_eq!(div(2, 3), Ok(0.6666666666666666));
|
||||
assert_eq!(div(1, 3), Ok(0.3333333333333333));
|
||||
assert_eq!(div(i64::MAX, 2), Ok(4611686018427387904.0));
|
||||
assert_eq!(div(i64::MIN, 2), Ok(-4611686018427387904.0));
|
||||
assert_eq!(div(i64::MIN, -1), Ok(9223372036854775808.0)); // Overflow case
|
||||
assert_eq!(div(i64::MIN, i64::MAX), Ok(-1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_floor_div() {
|
||||
let floor_div = jit_function! { floor_div(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_floor_div() {
|
||||
let floor_div = jit_function! { floor_div(a:i64, b:i64) -> i64 => r##"
|
||||
def floor_div(a: int, b: int):
|
||||
return a // b
|
||||
"## };
|
||||
|
||||
assert_eq!(floor_div(5, 10), Ok(0));
|
||||
assert_eq!(floor_div(5, 2), Ok(2));
|
||||
assert_eq!(floor_div(12, 10), Ok(1));
|
||||
assert_eq!(floor_div(7, 10), Ok(0));
|
||||
assert_eq!(floor_div(-3, -1), Ok(3));
|
||||
}
|
||||
assert_eq!(floor_div(5, 10), Ok(0));
|
||||
assert_eq!(floor_div(5, 2), Ok(2));
|
||||
assert_eq!(floor_div(12, 10), Ok(1));
|
||||
assert_eq!(floor_div(7, 10), Ok(0));
|
||||
assert_eq!(floor_div(-3, -1), Ok(3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[test]
|
||||
|
||||
fn test_exp() {
|
||||
let exp = jit_function! { exp(a: i64, b: i64) -> i64 => r##"
|
||||
fn basic_exp() {
|
||||
let exp = jit_function! { exp(a: i64, b: i64) -> i64 => r##"
|
||||
def exp(a: int, b: int):
|
||||
return a ** b
|
||||
"## };
|
||||
|
||||
assert_eq!(exp(2, 3), Ok(8));
|
||||
assert_eq!(exp(3, 2), Ok(9));
|
||||
assert_eq!(exp(5, 0), Ok(1));
|
||||
assert_eq!(exp(0, 0), Ok(1));
|
||||
assert_eq!(exp(-5, 0), Ok(1));
|
||||
assert_eq!(exp(0, 1), Ok(0));
|
||||
assert_eq!(exp(0, 5), Ok(0));
|
||||
assert_eq!(exp(-2, 2), Ok(4));
|
||||
assert_eq!(exp(-3, 4), Ok(81));
|
||||
assert_eq!(exp(-2, 3), Ok(-8));
|
||||
assert_eq!(exp(-3, 3), Ok(-27));
|
||||
assert_eq!(exp(1000, 2), Ok(1000000));
|
||||
}
|
||||
assert_eq!(exp(2, 3), Ok(8));
|
||||
assert_eq!(exp(3, 2), Ok(9));
|
||||
assert_eq!(exp(5, 0), Ok(1));
|
||||
assert_eq!(exp(0, 0), Ok(1));
|
||||
assert_eq!(exp(-5, 0), Ok(1));
|
||||
assert_eq!(exp(0, 1), Ok(0));
|
||||
assert_eq!(exp(0, 5), Ok(0));
|
||||
assert_eq!(exp(-2, 2), Ok(4));
|
||||
assert_eq!(exp(-3, 4), Ok(81));
|
||||
assert_eq!(exp(-2, 3), Ok(-8));
|
||||
assert_eq!(exp(-3, 3), Ok(-27));
|
||||
assert_eq!(exp(1000, 2), Ok(1000000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mod() {
|
||||
let modulo = jit_function! { modulo(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_mod() {
|
||||
let modulo = jit_function! { modulo(a:i64, b:i64) -> i64 => r##"
|
||||
def modulo(a: int, b: int):
|
||||
return a % b
|
||||
"## };
|
||||
|
||||
assert_eq!(modulo(5, 10), Ok(5));
|
||||
assert_eq!(modulo(5, 2), Ok(1));
|
||||
assert_eq!(modulo(12, 10), Ok(2));
|
||||
assert_eq!(modulo(7, 10), Ok(7));
|
||||
assert_eq!(modulo(-3, 1), Ok(0));
|
||||
assert_eq!(modulo(-5, 10), Ok(-5));
|
||||
}
|
||||
assert_eq!(modulo(5, 10), Ok(5));
|
||||
assert_eq!(modulo(5, 2), Ok(1));
|
||||
assert_eq!(modulo(12, 10), Ok(2));
|
||||
assert_eq!(modulo(7, 10), Ok(7));
|
||||
assert_eq!(modulo(-3, 1), Ok(0));
|
||||
assert_eq!(modulo(-5, 10), Ok(-5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_power() {
|
||||
let power = jit_function! { power(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_power() {
|
||||
let power = jit_function! { power(a:i64, b:i64) -> i64 => r##"
|
||||
def power(a: int, b: int):
|
||||
return a ** b
|
||||
"##
|
||||
};
|
||||
assert_eq!(power(10, 2), Ok(100));
|
||||
assert_eq!(power(5, 1), Ok(5));
|
||||
assert_eq!(power(1, 0), Ok(1));
|
||||
}
|
||||
};
|
||||
assert_eq!(power(10, 2), Ok(100));
|
||||
assert_eq!(power(5, 1), Ok(5));
|
||||
assert_eq!(power(1, 0), Ok(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lshift() {
|
||||
let lshift = jit_function! { lshift(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_lshift() {
|
||||
let lshift = jit_function! { lshift(a:i64, b:i64) -> i64 => r##"
|
||||
def lshift(a: int, b: int):
|
||||
return a << b
|
||||
"## };
|
||||
|
||||
assert_eq!(lshift(5, 10), Ok(5120));
|
||||
assert_eq!(lshift(5, 2), Ok(20));
|
||||
assert_eq!(lshift(12, 10), Ok(12288));
|
||||
assert_eq!(lshift(7, 10), Ok(7168));
|
||||
assert_eq!(lshift(-3, 1), Ok(-6));
|
||||
assert_eq!(lshift(-10, 2), Ok(-40));
|
||||
}
|
||||
assert_eq!(lshift(5, 10), Ok(5120));
|
||||
assert_eq!(lshift(5, 2), Ok(20));
|
||||
assert_eq!(lshift(12, 10), Ok(12288));
|
||||
assert_eq!(lshift(7, 10), Ok(7168));
|
||||
assert_eq!(lshift(-3, 1), Ok(-6));
|
||||
assert_eq!(lshift(-10, 2), Ok(-40));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rshift() {
|
||||
let rshift = jit_function! { rshift(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_rshift() {
|
||||
let rshift = jit_function! { rshift(a:i64, b:i64) -> i64 => r##"
|
||||
def rshift(a: int, b: int):
|
||||
return a >> b
|
||||
"## };
|
||||
|
||||
assert_eq!(rshift(5120, 10), Ok(5));
|
||||
assert_eq!(rshift(20, 2), Ok(5));
|
||||
assert_eq!(rshift(12288, 10), Ok(12));
|
||||
assert_eq!(rshift(7168, 10), Ok(7));
|
||||
assert_eq!(rshift(-3, 1), Ok(-2));
|
||||
assert_eq!(rshift(-10, 2), Ok(-3));
|
||||
}
|
||||
assert_eq!(rshift(5120, 10), Ok(5));
|
||||
assert_eq!(rshift(20, 2), Ok(5));
|
||||
assert_eq!(rshift(12288, 10), Ok(12));
|
||||
assert_eq!(rshift(7168, 10), Ok(7));
|
||||
assert_eq!(rshift(-3, 1), Ok(-2));
|
||||
assert_eq!(rshift(-10, 2), Ok(-3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_and() {
|
||||
let bitand = jit_function! { bitand(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_and() {
|
||||
let bitand = jit_function! { bitand(a:i64, b:i64) -> i64 => r##"
|
||||
def bitand(a: int, b: int):
|
||||
return a & b
|
||||
"## };
|
||||
|
||||
assert_eq!(bitand(5120, 10), Ok(0));
|
||||
assert_eq!(bitand(20, 16), Ok(16));
|
||||
assert_eq!(bitand(12488, 4249), Ok(4232));
|
||||
assert_eq!(bitand(7168, 2), Ok(0));
|
||||
assert_eq!(bitand(-3, 1), Ok(1));
|
||||
assert_eq!(bitand(-10, 2), Ok(2));
|
||||
}
|
||||
assert_eq!(bitand(5120, 10), Ok(0));
|
||||
assert_eq!(bitand(20, 16), Ok(16));
|
||||
assert_eq!(bitand(12488, 4249), Ok(4232));
|
||||
assert_eq!(bitand(7168, 2), Ok(0));
|
||||
assert_eq!(bitand(-3, 1), Ok(1));
|
||||
assert_eq!(bitand(-10, 2), Ok(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_or() {
|
||||
let bitor = jit_function! { bitor(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_or() {
|
||||
let bitor = jit_function! { bitor(a:i64, b:i64) -> i64 => r##"
|
||||
def bitor(a: int, b: int):
|
||||
return a | b
|
||||
"## };
|
||||
|
||||
assert_eq!(bitor(5120, 10), Ok(5130));
|
||||
assert_eq!(bitor(20, 16), Ok(20));
|
||||
assert_eq!(bitor(12488, 4249), Ok(12505));
|
||||
assert_eq!(bitor(7168, 2), Ok(7170));
|
||||
assert_eq!(bitor(-3, 1), Ok(-3));
|
||||
assert_eq!(bitor(-10, 2), Ok(-10));
|
||||
}
|
||||
assert_eq!(bitor(5120, 10), Ok(5130));
|
||||
assert_eq!(bitor(20, 16), Ok(20));
|
||||
assert_eq!(bitor(12488, 4249), Ok(12505));
|
||||
assert_eq!(bitor(7168, 2), Ok(7170));
|
||||
assert_eq!(bitor(-3, 1), Ok(-3));
|
||||
assert_eq!(bitor(-10, 2), Ok(-10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_xor() {
|
||||
let bitxor = jit_function! { bitxor(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_xor() {
|
||||
let bitxor = jit_function! { bitxor(a:i64, b:i64) -> i64 => r##"
|
||||
def bitxor(a: int, b: int):
|
||||
return a ^ b
|
||||
"## };
|
||||
|
||||
assert_eq!(bitxor(5120, 10), Ok(5130));
|
||||
assert_eq!(bitxor(20, 16), Ok(4));
|
||||
assert_eq!(bitxor(12488, 4249), Ok(8273));
|
||||
assert_eq!(bitxor(7168, 2), Ok(7170));
|
||||
assert_eq!(bitxor(-3, 1), Ok(-4));
|
||||
assert_eq!(bitxor(-10, 2), Ok(-12));
|
||||
}
|
||||
assert_eq!(bitxor(5120, 10), Ok(5130));
|
||||
assert_eq!(bitxor(20, 16), Ok(4));
|
||||
assert_eq!(bitxor(12488, 4249), Ok(8273));
|
||||
assert_eq!(bitxor(7168, 2), Ok(7170));
|
||||
assert_eq!(bitxor(-3, 1), Ok(-4));
|
||||
assert_eq!(bitxor(-10, 2), Ok(-12));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eq() {
|
||||
let eq = jit_function! { eq(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_eq() {
|
||||
let eq = jit_function! { eq(a:i64, b:i64) -> i64 => r##"
|
||||
def eq(a: int, b: int):
|
||||
if a == b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(eq(0, 0), Ok(1));
|
||||
assert_eq!(eq(1, 1), Ok(1));
|
||||
assert_eq!(eq(0, 1), Ok(0));
|
||||
assert_eq!(eq(-200, 200), Ok(0));
|
||||
}
|
||||
assert_eq!(eq(0, 0), Ok(1));
|
||||
assert_eq!(eq(1, 1), Ok(1));
|
||||
assert_eq!(eq(0, 1), Ok(0));
|
||||
assert_eq!(eq(-200, 200), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gt() {
|
||||
let gt = jit_function! { gt(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_gt() {
|
||||
let gt = jit_function! { gt(a:i64, b:i64) -> i64 => r##"
|
||||
def gt(a: int, b: int):
|
||||
if a > b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(gt(5, 2), Ok(1));
|
||||
assert_eq!(gt(2, 5), Ok(0));
|
||||
assert_eq!(gt(2, 2), Ok(0));
|
||||
assert_eq!(gt(5, 5), Ok(0));
|
||||
assert_eq!(gt(-1, -10), Ok(1));
|
||||
assert_eq!(gt(1, -1), Ok(1));
|
||||
}
|
||||
assert_eq!(gt(5, 2), Ok(1));
|
||||
assert_eq!(gt(2, 5), Ok(0));
|
||||
assert_eq!(gt(2, 2), Ok(0));
|
||||
assert_eq!(gt(5, 5), Ok(0));
|
||||
assert_eq!(gt(-1, -10), Ok(1));
|
||||
assert_eq!(gt(1, -1), Ok(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lt() {
|
||||
let lt = jit_function! { lt(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_lt() {
|
||||
let lt = jit_function! { lt(a:i64, b:i64) -> i64 => r##"
|
||||
def lt(a: int, b: int):
|
||||
if a < b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(lt(-1, -5), Ok(0));
|
||||
assert_eq!(lt(10, 0), Ok(0));
|
||||
assert_eq!(lt(0, 1), Ok(1));
|
||||
assert_eq!(lt(-10, -1), Ok(1));
|
||||
assert_eq!(lt(100, 100), Ok(0));
|
||||
}
|
||||
assert_eq!(lt(-1, -5), Ok(0));
|
||||
assert_eq!(lt(10, 0), Ok(0));
|
||||
assert_eq!(lt(0, 1), Ok(1));
|
||||
assert_eq!(lt(-10, -1), Ok(1));
|
||||
assert_eq!(lt(100, 100), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gte() {
|
||||
let gte = jit_function! { gte(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_gte() {
|
||||
let gte = jit_function! { gte(a:i64, b:i64) -> i64 => r##"
|
||||
def gte(a: int, b: int):
|
||||
if a >= b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(gte(-64, -64), Ok(1));
|
||||
assert_eq!(gte(100, -1), Ok(1));
|
||||
assert_eq!(gte(1, 2), Ok(0));
|
||||
assert_eq!(gte(1, 0), Ok(1));
|
||||
}
|
||||
assert_eq!(gte(-64, -64), Ok(1));
|
||||
assert_eq!(gte(100, -1), Ok(1));
|
||||
assert_eq!(gte(1, 2), Ok(0));
|
||||
assert_eq!(gte(1, 0), Ok(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lte() {
|
||||
let lte = jit_function! { lte(a:i64, b:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_lte() {
|
||||
let lte = jit_function! { lte(a:i64, b:i64) -> i64 => r##"
|
||||
def lte(a: int, b: int):
|
||||
if a <= b:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(lte(-100, -100), Ok(1));
|
||||
assert_eq!(lte(-100, 100), Ok(1));
|
||||
assert_eq!(lte(10, 1), Ok(0));
|
||||
assert_eq!(lte(0, -2), Ok(0));
|
||||
}
|
||||
assert_eq!(lte(-100, -100), Ok(1));
|
||||
assert_eq!(lte(-100, 100), Ok(1));
|
||||
assert_eq!(lte(10, 1), Ok(0));
|
||||
assert_eq!(lte(0, -2), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_minus() {
|
||||
let minus = jit_function! { minus(a:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_minus() {
|
||||
let minus = jit_function! { minus(a:i64) -> i64 => r##"
|
||||
def minus(a: int):
|
||||
return -a
|
||||
"## };
|
||||
|
||||
assert_eq!(minus(5), Ok(-5));
|
||||
assert_eq!(minus(12), Ok(-12));
|
||||
assert_eq!(minus(-7), Ok(7));
|
||||
assert_eq!(minus(-3), Ok(3));
|
||||
assert_eq!(minus(0), Ok(0));
|
||||
}
|
||||
assert_eq!(minus(5), Ok(-5));
|
||||
assert_eq!(minus(12), Ok(-12));
|
||||
assert_eq!(minus(-7), Ok(7));
|
||||
assert_eq!(minus(-3), Ok(3));
|
||||
assert_eq!(minus(0), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_plus() {
|
||||
let plus = jit_function! { plus(a:i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_plus() {
|
||||
let plus = jit_function! { plus(a:i64) -> i64 => r##"
|
||||
def plus(a: int):
|
||||
return +a
|
||||
"## };
|
||||
|
||||
assert_eq!(plus(5), Ok(5));
|
||||
assert_eq!(plus(12), Ok(12));
|
||||
assert_eq!(plus(-7), Ok(-7));
|
||||
assert_eq!(plus(-3), Ok(-3));
|
||||
assert_eq!(plus(0), Ok(0));
|
||||
}
|
||||
assert_eq!(plus(5), Ok(5));
|
||||
assert_eq!(plus(12), Ok(12));
|
||||
assert_eq!(plus(-7), Ok(-7));
|
||||
assert_eq!(plus(-3), Ok(-3));
|
||||
assert_eq!(plus(0), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not() {
|
||||
let not_ = jit_function! { not_(a: i64) -> bool => r##"
|
||||
#[test]
|
||||
fn basic_not() {
|
||||
let not_ = jit_function! { not_(a: i64) -> bool => r##"
|
||||
def not_(a: int):
|
||||
return not a
|
||||
"## };
|
||||
|
||||
assert_eq!(not_(0), Ok(true));
|
||||
assert_eq!(not_(1), Ok(false));
|
||||
assert_eq!(not_(-1), Ok(false));
|
||||
assert_eq!(not_(0), Ok(true));
|
||||
assert_eq!(not_(1), Ok(false));
|
||||
assert_eq!(not_(-1), Ok(false));
|
||||
}
|
||||
}
|
||||
@@ -1,74 +1,76 @@
|
||||
use rustpython_jit::{AbiValue, JitArgumentError};
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
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(()));
|
||||
// }
|
||||
// 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##"
|
||||
#[test]
|
||||
fn 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)))
|
||||
);
|
||||
}
|
||||
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##"
|
||||
#[test]
|
||||
fn 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!(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 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)));
|
||||
}
|
||||
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##"
|
||||
#[test]
|
||||
fn basic_if_else() {
|
||||
let if_else = jit_function! { if_else(a:i64) -> i64 => r##"
|
||||
def if_else(a: int):
|
||||
if a:
|
||||
return 42
|
||||
@@ -79,15 +81,15 @@ fn test_if_else() {
|
||||
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));
|
||||
}
|
||||
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##"
|
||||
#[test]
|
||||
fn basic_while_loop() {
|
||||
let while_loop = jit_function! { while_loop(a:i64) -> i64 => r##"
|
||||
def while_loop(a: int):
|
||||
b = 0
|
||||
while a > 0:
|
||||
@@ -95,32 +97,33 @@ fn test_while_loop() {
|
||||
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));
|
||||
}
|
||||
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##"
|
||||
#[test]
|
||||
fn basic_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));
|
||||
}
|
||||
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##"
|
||||
#[test]
|
||||
fn 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));
|
||||
assert_eq!(fib(10), Ok(89));
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,18 @@
|
||||
#[test]
|
||||
fn test_not() {
|
||||
let not_ = jit_function! { not_(x: i64) -> bool => r##"
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn basic_not() {
|
||||
let not_ = jit_function! { not_(x: i64) -> bool => r##"
|
||||
def not_(x: int):
|
||||
return not None
|
||||
"## };
|
||||
|
||||
assert_eq!(not_(0), Ok(true));
|
||||
}
|
||||
assert_eq!(not_(0), Ok(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_not() {
|
||||
let if_not = jit_function! { if_not(x: i64) -> i64 => r##"
|
||||
#[test]
|
||||
fn basic_if_not() {
|
||||
let if_not = jit_function! { if_not(x: i64) -> i64 => r##"
|
||||
def if_not(x: int):
|
||||
if not None:
|
||||
return 1
|
||||
@@ -20,5 +22,6 @@ fn test_if_not() {
|
||||
return -1
|
||||
"## };
|
||||
|
||||
assert_eq!(if_not(0), Ok(1));
|
||||
assert_eq!(if_not(0), Ok(1));
|
||||
}
|
||||
}
|
||||
+179
-176
@@ -1,200 +1,203 @@
|
||||
// spell-checker:disable
|
||||
use rustpython_sre_engine::{Request, State, StrDrive};
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rustpython_sre_engine::{Request, State, StrDrive};
|
||||
|
||||
struct Pattern {
|
||||
#[allow(unused)]
|
||||
pattern: &'static str,
|
||||
code: &'static [u32],
|
||||
}
|
||||
|
||||
impl Pattern {
|
||||
fn state<'a, S: StrDrive>(&self, string: S) -> (Request<'a, S>, State) {
|
||||
let req = Request::new(string, 0, usize::MAX, self.code, false);
|
||||
let state = State::default();
|
||||
(req, state)
|
||||
struct Pattern {
|
||||
#[expect(unused, reason = "Needed for automated script")]
|
||||
pattern: &'static str,
|
||||
code: &'static [u32],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2427() {
|
||||
// pattern lookbehind = re.compile(r'(?<!\.)x\b')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let lookbehind = Pattern { pattern: "(?<!\\.)x\\b", code: &[14, 4, 0, 1, 1, 5, 5, 1, 16, 46, 1, 16, 120, 6, 10, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = lookbehind.state("x");
|
||||
assert!(state.py_match(&req));
|
||||
}
|
||||
impl Pattern {
|
||||
fn state<'a, S: StrDrive>(&self, string: S) -> (Request<'a, S>, State) {
|
||||
let req = Request::new(string, 0, usize::MAX, self.code, false);
|
||||
let state = State::default();
|
||||
(req, state)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_assert() {
|
||||
// pattern positive_lookbehind = re.compile(r'(?<=abc)def')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let positive_lookbehind = Pattern { pattern: "(?<=abc)def", code: &[14, 4, 0, 3, 3, 4, 9, 3, 16, 97, 16, 98, 16, 99, 1, 16, 100, 16, 101, 16, 102, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = positive_lookbehind.state("abcdef");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
#[test]
|
||||
fn gh_2427() {
|
||||
// pattern lookbehind = re.compile(r'(?<!\.)x\b')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let lookbehind = Pattern { pattern: "(?<!\\.)x\\b", code: &[14, 4, 0, 1, 1, 5, 5, 1, 16, 46, 1, 16, 120, 6, 10, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = lookbehind.state("x");
|
||||
assert!(state.py_match(&req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string_boundaries() {
|
||||
// pattern big_b = re.compile(r'\B')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let big_b = Pattern { pattern: "\\B", code: &[14, 4, 0, 0, 0, 6, 11, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = big_b.state("");
|
||||
assert!(!state.search(req));
|
||||
}
|
||||
#[test]
|
||||
fn basic_assert() {
|
||||
// pattern positive_lookbehind = re.compile(r'(?<=abc)def')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let positive_lookbehind = Pattern { pattern: "(?<=abc)def", code: &[14, 4, 0, 3, 3, 4, 9, 3, 16, 97, 16, 98, 16, 99, 1, 16, 100, 16, 101, 16, 102, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = positive_lookbehind.state("abcdef");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zerowidth() {
|
||||
// pattern p = re.compile(r'\b|:+')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "\\b|:+", code: &[14, 4, 0, 0, 4294967295, 7, 5, 6, 10, 15, 12, 10, 24, 6, 1, 4294967295, 16, 58, 1, 15, 2, 0, 1] };
|
||||
// END GENERATED
|
||||
let (mut req, mut state) = p.state("a:");
|
||||
req.must_advance = true;
|
||||
assert!(state.search(req));
|
||||
assert_eq!(state.cursor.position, 1);
|
||||
}
|
||||
#[test]
|
||||
fn string_boundaries() {
|
||||
// pattern big_b = re.compile(r'\B')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let big_b = Pattern { pattern: "\\B", code: &[14, 4, 0, 0, 0, 6, 11, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = big_b.state("");
|
||||
assert!(!state.search(req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repeat_context_panic() {
|
||||
use optional::Optioned;
|
||||
// pattern p = re.compile(r'(?:a*?(xx)??z)*')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "(?:a*?(xx)??z)*", code: &[14, 4, 0, 0, 4294967295, 23, 25, 0, 4294967295, 26, 6, 0, 4294967295, 16, 97, 1, 23, 11, 0, 1, 17, 0, 16, 120, 16, 120, 17, 1, 19, 16, 122, 18, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("axxzaz");
|
||||
assert!(state.py_match(&req));
|
||||
assert_eq!(
|
||||
*state.marks.raw(),
|
||||
vec![Optioned::some(1), Optioned::some(3)]
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn zerowidth() {
|
||||
// pattern p = re.compile(r'\b|:+')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "\\b|:+", code: &[14, 4, 0, 0, 4294967295, 7, 5, 6, 10, 15, 12, 10, 24, 6, 1, 4294967295, 16, 58, 1, 15, 2, 0, 1] };
|
||||
// END GENERATED
|
||||
let (mut req, mut state) = p.state("a:");
|
||||
req.must_advance = true;
|
||||
assert!(state.search(req));
|
||||
assert_eq!(state.cursor.position, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_double_max_until() {
|
||||
// pattern p = re.compile(r'((1)?)*')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "((1)?)*", code: &[14, 4, 0, 0, 4294967295, 23, 18, 0, 4294967295, 17, 0, 23, 9, 0, 1, 17, 2, 16, 49, 17, 3, 18, 17, 1, 18, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("1111");
|
||||
assert!(state.py_match(&req));
|
||||
assert_eq!(state.cursor.position, 4);
|
||||
}
|
||||
#[test]
|
||||
fn repeat_context_panic() {
|
||||
use optional::Optioned;
|
||||
// pattern p = re.compile(r'(?:a*?(xx)??z)*')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "(?:a*?(xx)??z)*", code: &[14, 4, 0, 0, 4294967295, 23, 25, 0, 4294967295, 26, 6, 0, 4294967295, 16, 97, 1, 23, 11, 0, 1, 17, 0, 16, 120, 16, 120, 17, 1, 19, 16, 122, 18, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("axxzaz");
|
||||
assert!(state.py_match(&req));
|
||||
assert_eq!(
|
||||
*state.marks.raw(),
|
||||
vec![Optioned::some(1), Optioned::some(3)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_info_single() {
|
||||
// pattern p = re.compile(r'aa*')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "aa*", code: &[14, 8, 1, 1, 4294967295, 1, 1, 97, 0, 16, 97, 24, 6, 0, 4294967295, 16, 97, 1, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("baaaa");
|
||||
assert!(state.search(req));
|
||||
assert_eq!(state.start, 1);
|
||||
assert_eq!(state.cursor.position, 5);
|
||||
}
|
||||
#[test]
|
||||
fn double_max_until() {
|
||||
// pattern p = re.compile(r'((1)?)*')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "((1)?)*", code: &[14, 4, 0, 0, 4294967295, 23, 18, 0, 4294967295, 17, 0, 23, 9, 0, 1, 17, 2, 16, 49, 17, 3, 18, 17, 1, 18, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("1111");
|
||||
assert!(state.py_match(&req));
|
||||
assert_eq!(state.cursor.position, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_info_single2() {
|
||||
// pattern p = re.compile(r'Python|Perl')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "Python|Perl", code: &[14, 8, 1, 4, 6, 1, 1, 80, 0, 16, 80, 7, 13, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 11, 9, 16, 101, 16, 114, 16, 108, 15, 2, 0, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("Perl");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
#[test]
|
||||
fn info_single() {
|
||||
// pattern p = re.compile(r'aa*')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "aa*", code: &[14, 8, 1, 1, 4294967295, 1, 1, 97, 0, 16, 97, 24, 6, 0, 4294967295, 16, 97, 1, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("baaaa");
|
||||
assert!(state.search(req));
|
||||
assert_eq!(state.start, 1);
|
||||
assert_eq!(state.cursor.position, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_info_literal() {
|
||||
// pattern p = re.compile(r'ababc+')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "ababc+", code: &[14, 14, 1, 5, 4294967295, 4, 4, 97, 98, 97, 98, 0, 0, 1, 2, 16, 97, 16, 98, 16, 97, 16, 98, 24, 6, 1, 4294967295, 16, 99, 1, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("!ababc");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
#[test]
|
||||
fn info_single2() {
|
||||
// pattern p = re.compile(r'Python|Perl')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "Python|Perl", code: &[14, 8, 1, 4, 6, 1, 1, 80, 0, 16, 80, 7, 13, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 15, 11, 9, 16, 101, 16, 114, 16, 108, 15, 2, 0, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("Perl");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_info_literal2() {
|
||||
// pattern p = re.compile(r'(python)\1')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "(python)\\1", code: &[14, 18, 1, 12, 12, 6, 0, 112, 121, 116, 104, 111, 110, 0, 0, 0, 0, 0, 0, 17, 0, 16, 112, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 17, 1, 11, 0, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("pythonpython");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
#[test]
|
||||
fn info_literal() {
|
||||
// pattern p = re.compile(r'ababc+')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "ababc+", code: &[14, 14, 1, 5, 4294967295, 4, 4, 97, 98, 97, 98, 0, 0, 1, 2, 16, 97, 16, 98, 16, 97, 16, 98, 24, 6, 1, 4294967295, 16, 99, 1, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("!ababc");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repeat_in_assertions() {
|
||||
// pattern p = re.compile('^([ab]*?)(?=(b)?)c', re.IGNORECASE)
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "^([ab]*?)(?=(b)?)c", code: &[14, 4, 0, 1, 4294967295, 6, 0, 17, 0, 26, 10, 0, 4294967295, 39, 5, 22, 97, 98, 0, 1, 17, 1, 4, 14, 0, 23, 9, 0, 1, 17, 2, 40, 98, 17, 3, 18, 1, 40, 99, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("abc");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
#[test]
|
||||
fn info_literal2() {
|
||||
// pattern p = re.compile(r'(python)\1')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "(python)\\1", code: &[14, 18, 1, 12, 12, 6, 0, 112, 121, 116, 104, 111, 110, 0, 0, 0, 0, 0, 0, 17, 0, 16, 112, 16, 121, 16, 116, 16, 104, 16, 111, 16, 110, 17, 1, 11, 0, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("pythonpython");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_possessive_quantifier() {
|
||||
// pattern p = re.compile('e++a')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "e++a", code: &[14, 4, 0, 2, 4294967295, 29, 6, 1, 4294967295, 16, 101, 1, 16, 97, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("eeea");
|
||||
assert!(state.py_match(&req));
|
||||
}
|
||||
#[test]
|
||||
fn repeat_in_assertions() {
|
||||
// pattern p = re.compile('^([ab]*?)(?=(b)?)c', re.IGNORECASE)
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "^([ab]*?)(?=(b)?)c", code: &[14, 4, 0, 1, 4294967295, 6, 0, 17, 0, 26, 10, 0, 4294967295, 39, 5, 22, 97, 98, 0, 1, 17, 1, 4, 14, 0, 23, 9, 0, 1, 17, 2, 40, 98, 17, 3, 18, 1, 40, 99, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("abc");
|
||||
assert!(state.search(req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_possessive_repeat_fullmatch() {
|
||||
// pattern p = re.compile("([0-9]++(?:\.[0-9]+)*+)", re.I )
|
||||
// [INFO, 4, 0, 1, 4294967295, MARK, 0, POSSESSIVE_REPEAT_ONE, 10, 1, MAXREPEAT, IN, 5, RANGE, 48, 57, FAILURE, SUCCESS, POSSESSIVE_REPEAT, 16, 0, MAXREPEAT, LITERAL, 46, REPEAT_ONE, 10, 1, MAXREPEAT, IN, 5, RANGE, 48, 57, FAILURE, SUCCESS, SUCCESS, MARK, 1, SUCCESS]
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "([0-9]++(?:\\.[0-9]+)*+)", code: &[14, 4, 0, 1, 4294967295, 17, 0, 29, 10, 1, 4294967295, 13, 5, 22, 48, 57, 0, 1, 28, 16, 0, 4294967295, 16, 46, 24, 10, 1, 4294967295, 13, 5, 22, 48, 57, 0, 1, 1, 17, 1, 1] };
|
||||
// END GENERATED
|
||||
let (mut req, mut state) = p.state("1.25.38");
|
||||
req.match_all = true;
|
||||
assert!(state.py_match(&req), "should match");
|
||||
}
|
||||
#[test]
|
||||
fn possessive_quantifier() {
|
||||
// pattern p = re.compile('e++a')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "e++a", code: &[14, 4, 0, 2, 4294967295, 29, 6, 1, 4294967295, 16, 101, 1, 16, 97, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("eeea");
|
||||
assert!(state.py_match(&req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_possessive_atomic_group() {
|
||||
// pattern p = re.compile('(?>x)++x')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "(?>x)++x", code: &[14, 4, 0, 2, 4294967295, 28, 8, 1, 4294967295, 27, 4, 16, 120, 1, 1, 16, 120, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("xxx");
|
||||
assert!(!state.py_match(&req));
|
||||
}
|
||||
#[test]
|
||||
fn possessive_repeat_fullmatch() {
|
||||
// pattern p = re.compile("([0-9]++(?:\.[0-9]+)*+)", re.I )
|
||||
// [INFO, 4, 0, 1, 4294967295, MARK, 0, POSSESSIVE_REPEAT_ONE, 10, 1, MAXREPEAT, IN, 5, RANGE, 48, 57, FAILURE, SUCCESS, POSSESSIVE_REPEAT, 16, 0, MAXREPEAT, LITERAL, 46, REPEAT_ONE, 10, 1, MAXREPEAT, IN, 5, RANGE, 48, 57, FAILURE, SUCCESS, SUCCESS, MARK, 1, SUCCESS]
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "([0-9]++(?:\\.[0-9]+)*+)", code: &[14, 4, 0, 1, 4294967295, 17, 0, 29, 10, 1, 4294967295, 13, 5, 22, 48, 57, 0, 1, 28, 16, 0, 4294967295, 16, 46, 24, 10, 1, 4294967295, 13, 5, 22, 48, 57, 0, 1, 1, 17, 1, 1] };
|
||||
// END GENERATED
|
||||
let (mut req, mut state) = p.state("1.25.38");
|
||||
req.match_all = true;
|
||||
assert!(state.py_match(&req), "should match");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bug_20998() {
|
||||
// pattern p = re.compile('[a-c]+', re.I)
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "[a-c]+", code: &[14, 4, 0, 1, 4294967295, 24, 10, 1, 4294967295, 39, 5, 22, 97, 99, 0, 1, 1] };
|
||||
// END GENERATED
|
||||
let (mut req, mut state) = p.state("ABC");
|
||||
req.match_all = true;
|
||||
assert!(state.py_match(&req));
|
||||
assert_eq!(state.cursor.position, 3);
|
||||
}
|
||||
#[test]
|
||||
fn possessive_atomic_group() {
|
||||
// pattern p = re.compile('(?>x)++x')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "(?>x)++x", code: &[14, 4, 0, 2, 4294967295, 28, 8, 1, 4294967295, 27, 4, 16, 120, 1, 1, 16, 120, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("xxx");
|
||||
assert!(!state.py_match(&req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bigcharset() {
|
||||
// pattern p = re.compile('[a-z]*', re.I)
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "[a-z]*", code: &[14, 4, 0, 0, 4294967295, 24, 97, 0, 4294967295, 39, 92, 10, 3, 33685760, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 0, 0, 0, 134217726, 0, 0, 0, 0, 0, 131072, 0, 2147483648, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("x ");
|
||||
assert!(state.py_match(&req));
|
||||
assert_eq!(state.cursor.position, 1);
|
||||
}
|
||||
#[test]
|
||||
fn bug_20998() {
|
||||
// pattern p = re.compile('[a-c]+', re.I)
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "[a-c]+", code: &[14, 4, 0, 1, 4294967295, 24, 10, 1, 4294967295, 39, 5, 22, 97, 99, 0, 1, 1] };
|
||||
// END GENERATED
|
||||
let (mut req, mut state) = p.state("ABC");
|
||||
req.match_all = true;
|
||||
assert!(state.py_match(&req));
|
||||
assert_eq!(state.cursor.position, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_search_nonascii() {
|
||||
#[allow(unused)]
|
||||
#[test]
|
||||
fn bigcharset() {
|
||||
// pattern p = re.compile('[a-z]*', re.I)
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "[a-z]*", code: &[14, 4, 0, 0, 4294967295, 24, 97, 0, 4294967295, 39, 92, 10, 3, 33685760, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 33686018, 0, 0, 0, 134217726, 0, 0, 0, 0, 0, 131072, 0, 2147483648, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] };
|
||||
// END GENERATED
|
||||
let (req, mut state) = p.state("x ");
|
||||
assert!(state.py_match(&req));
|
||||
assert_eq!(state.cursor.position, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_nonascii() {
|
||||
#[allow(unused)]
|
||||
// pattern p = re.compile('\xe0+')
|
||||
// START GENERATED by generate_tests.py
|
||||
#[rustfmt::skip] let p = Pattern { pattern: "\u{e0}+", code: &[14, 4, 0, 1, 4294967295, 24, 6, 1, 4294967295, 16, 224, 1, 1] };
|
||||
// END GENERATED
|
||||
// END GENERATED
|
||||
}
|
||||
}
|
||||
@@ -238,7 +238,7 @@ output = re.sub(r'(<code object \w+ at )0x[0-9a-fA-F]+', r'\g<1>0xdeadbeef', tmp
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_ors() {
|
||||
fn if_ors() {
|
||||
assert_dis_snapshot!(
|
||||
r#"
|
||||
if True or False or False:
|
||||
@@ -248,7 +248,7 @@ if True or False or False:
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_ands() {
|
||||
fn if_ands() {
|
||||
assert_dis_snapshot!(
|
||||
r#"
|
||||
if True and False and False:
|
||||
@@ -258,7 +258,7 @@ if True and False and False:
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_mixed() {
|
||||
fn if_mixed() {
|
||||
assert_dis_snapshot!(
|
||||
r#"
|
||||
if (True and False) or (False and True):
|
||||
@@ -268,7 +268,7 @@ if (True and False) or (False and True):
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_bool_op() {
|
||||
fn nested_bool_op() {
|
||||
assert_dis_snapshot!(
|
||||
r#"
|
||||
x = Test() and False or False
|
||||
@@ -277,7 +277,7 @@ x = Test() and False or False
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_const_no_op() {
|
||||
fn const_no_op() {
|
||||
assert_dis_snapshot!(
|
||||
r#"
|
||||
x = not True
|
||||
@@ -286,7 +286,7 @@ x = not True
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constant_true_if_pass_keeps_line_anchor_nop() {
|
||||
fn constant_true_if_pass_keeps_line_anchor_nop() {
|
||||
assert_dis_snapshot!(
|
||||
r#"
|
||||
if 1:
|
||||
@@ -296,7 +296,7 @@ if 1:
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_double_async_with() {
|
||||
fn nested_double_async_with() {
|
||||
assert_dis_snapshot!(
|
||||
r#"
|
||||
async def test():
|
||||
@@ -314,7 +314,7 @@ async def test():
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bare_function_annotations_check_attribute_and_subscript_expressions() {
|
||||
fn bare_function_annotations_check_attribute_and_subscript_expressions() {
|
||||
assert_dis_snapshot!(
|
||||
r#"
|
||||
def f(one: int):
|
||||
|
||||
@@ -394,7 +394,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_find_by_nid() {
|
||||
fn find_by_nid_ok() {
|
||||
let entry = find_by_nid(13).unwrap();
|
||||
assert_eq!(entry.short_name, "CN");
|
||||
assert_eq!(entry.long_name, "commonName");
|
||||
@@ -402,48 +402,48 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_by_oid_string() {
|
||||
fn find_by_oid_string_ok() {
|
||||
let entry = find_by_oid_string("2.5.4.3").unwrap();
|
||||
assert_eq!(entry.nid, 13);
|
||||
assert_eq!(entry.short_name, "CN");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_by_name_short() {
|
||||
fn find_by_name_short() {
|
||||
let entry = find_by_name("CN").unwrap();
|
||||
assert_eq!(entry.nid, 13);
|
||||
assert_eq!(entry.oid_string(), "2.5.4.3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_by_name_long() {
|
||||
fn find_by_name_long() {
|
||||
let entry = find_by_name("commonName").unwrap();
|
||||
assert_eq!(entry.nid, 13);
|
||||
assert_eq!(entry.short_name, "CN");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_by_name_case_insensitive() {
|
||||
fn find_by_name_case_insensitive() {
|
||||
let entry = find_by_name("COMMONNAME").unwrap();
|
||||
assert_eq!(entry.nid, 13);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_subject_alt_name() {
|
||||
fn subject_alt_name() {
|
||||
let entry = find_by_nid(85).unwrap();
|
||||
assert_eq!(entry.short_name, "subjectAltName");
|
||||
assert_eq!(entry.oid_string(), "2.5.29.17");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_server_auth_eku() {
|
||||
fn server_auth_eku() {
|
||||
let entry = find_by_nid(129).unwrap();
|
||||
assert_eq!(entry.short_name, "serverAuth");
|
||||
assert_eq!(entry.oid_string(), "1.3.6.1.5.5.7.3.1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_duplicate_nids() {
|
||||
fn no_duplicate_nids() {
|
||||
let table = &*OID_TABLE;
|
||||
assert_eq!(
|
||||
table.entries.len(),
|
||||
@@ -453,7 +453,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oid_count() {
|
||||
fn oid_count() {
|
||||
let table = &*OID_TABLE;
|
||||
// We should have 50+ OIDs defined
|
||||
assert!(
|
||||
|
||||
@@ -2962,7 +2962,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_linearise() {
|
||||
fn linearise() {
|
||||
let context = Context::genesis();
|
||||
let object = context.types.object_type.to_owned();
|
||||
let type_type = context.types.type_type.to_owned();
|
||||
|
||||
@@ -1245,7 +1245,7 @@ mod tests {
|
||||
use crate::{Interpreter, common::ascii};
|
||||
|
||||
#[test]
|
||||
fn test_insert() {
|
||||
fn insert_basic() {
|
||||
Interpreter::without_stdlib(Default::default()).enter(|vm| {
|
||||
let dict = Dict::default();
|
||||
assert_eq!(0, dict.len());
|
||||
@@ -1290,8 +1290,8 @@ mod tests {
|
||||
}
|
||||
|
||||
hash_tests! {
|
||||
test_abc: "abc",
|
||||
test_x: "x",
|
||||
abc: "abc",
|
||||
x: "x",
|
||||
}
|
||||
|
||||
fn check_hash_equivalence(text: &str) {
|
||||
|
||||
@@ -16,7 +16,7 @@ mod tests {
|
||||
use crate::Interpreter;
|
||||
|
||||
#[test]
|
||||
fn test_print_42() {
|
||||
fn print_42() {
|
||||
Interpreter::without_stdlib(Default::default()).enter(|vm| {
|
||||
let source = String::from("print('Hello world')");
|
||||
let vars = vm.new_scope_with_builtins();
|
||||
|
||||
@@ -221,7 +221,7 @@ mod tests {
|
||||
use core::mem::size_of_val;
|
||||
|
||||
#[test]
|
||||
fn test_into_native_fn_noalloc() {
|
||||
fn into_native_fn_noalloc() {
|
||||
fn py_func(_b: bool, _vm: &crate::VirtualMachine) -> i32 {
|
||||
1
|
||||
}
|
||||
|
||||
@@ -874,7 +874,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_gc_state_default() {
|
||||
fn gc_state_default() {
|
||||
let state = GcState::new();
|
||||
assert!(state.is_enabled());
|
||||
assert_eq!(state.get_debug(), GcDebugFlags::empty());
|
||||
@@ -883,7 +883,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gc_enable_disable() {
|
||||
fn gc_enable_disable() {
|
||||
let state = GcState::new();
|
||||
assert!(state.is_enabled());
|
||||
state.disable();
|
||||
@@ -893,14 +893,14 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gc_threshold() {
|
||||
fn gc_threshold() {
|
||||
let state = GcState::new();
|
||||
state.set_threshold(100, Some(20), Some(30));
|
||||
assert_eq!(state.get_threshold(), (100, 20, 30));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gc_debug_flags() {
|
||||
fn gc_debug_flags() {
|
||||
let state = GcState::new();
|
||||
state.set_debug(GcDebugFlags::STATS | GcDebugFlags::COLLECTABLE);
|
||||
assert_eq!(
|
||||
|
||||
@@ -391,7 +391,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_init_path_config() {
|
||||
fn init_path_config_basic() {
|
||||
let settings = Settings::default();
|
||||
let paths = init_path_config(&settings);
|
||||
// Just verify it doesn't panic and returns valid paths
|
||||
@@ -399,7 +399,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_search_up() {
|
||||
fn search_up() {
|
||||
// Test with a path that doesn't have any landmarks
|
||||
let result = search_up_file(
|
||||
crate::host_env::os::temp_dir(),
|
||||
@@ -409,7 +409,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_prefix() {
|
||||
fn default_prefix_basic() {
|
||||
let prefix = default_prefix();
|
||||
assert!(!prefix.is_empty());
|
||||
}
|
||||
|
||||
@@ -5261,7 +5261,7 @@ mod _io {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_buffered_read() {
|
||||
fn buffered_read() {
|
||||
let data = vec![1, 2, 3, 4];
|
||||
let bytes = None;
|
||||
let mut buffered = BufferedIO {
|
||||
@@ -5272,7 +5272,7 @@ mod _io {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_buffered_seek() {
|
||||
fn buffered_seek() {
|
||||
let data = vec![1, 2, 3, 4];
|
||||
let count: u64 = 2;
|
||||
let mut buffered = BufferedIO {
|
||||
@@ -5284,7 +5284,7 @@ mod _io {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_buffered_value() {
|
||||
fn buffered_value() {
|
||||
let data = vec![1, 2, 3, 4];
|
||||
let buffered = BufferedIO {
|
||||
cursor: Cursor::new(data.clone()),
|
||||
|
||||
@@ -1501,7 +1501,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_find_by_name() {
|
||||
fn find_by_name() {
|
||||
// __len__ appears in both sequence and mapping
|
||||
let len_defs: Vec<_> = find_slot_defs_by_name("__len__").collect();
|
||||
assert_eq!(len_defs.len(), 2);
|
||||
@@ -1516,7 +1516,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slot_op() {
|
||||
fn slot_op() {
|
||||
// Test comparison ops
|
||||
assert_eq!(SlotOp::Lt.as_compare_op(), Some(PyComparisonOp::Lt));
|
||||
assert_eq!(SlotOp::Eq.as_compare_op(), Some(PyComparisonOp::Eq));
|
||||
|
||||
@@ -573,7 +573,7 @@ mod tests {
|
||||
use malachite_bigint::ToBigInt;
|
||||
|
||||
#[test]
|
||||
fn test_add_py_integers() {
|
||||
fn add_py_integers() {
|
||||
Interpreter::without_stdlib(Default::default()).enter(|vm| {
|
||||
let a: PyObjectRef = vm.ctx.new_int(33_i32).into();
|
||||
let b: PyObjectRef = vm.ctx.new_int(12_i32).into();
|
||||
@@ -584,7 +584,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiply_str() {
|
||||
fn multiply_str() {
|
||||
Interpreter::without_stdlib(Default::default()).enter(|vm| {
|
||||
let a = vm.new_pyobj(crate::common::ascii!("Hello "));
|
||||
let b = vm.new_pyobj(4_i32);
|
||||
|
||||
+48
-43
@@ -2266,52 +2266,57 @@ pub fn resolve_frozen_alias(name: &str) -> &str {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_frozen() {
|
||||
use rustpython_vm as vm;
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
vm::Interpreter::builder(Default::default())
|
||||
.add_frozen_modules(rustpython_vm::py_freeze!(
|
||||
dir = "../../../../extra_tests/snippets"
|
||||
))
|
||||
.build()
|
||||
.enter(|vm| {
|
||||
let scope = vm.new_scope_with_builtins();
|
||||
#[test]
|
||||
fn nested_frozen() {
|
||||
use rustpython_vm as vm;
|
||||
|
||||
let source = "from dir_module.dir_module_inner import value2";
|
||||
let code_obj = vm
|
||||
.compile(source, vm::compiler::Mode::Exec, "<embedded>".to_owned())
|
||||
.map_err(|err| vm.new_syntax_error(&err, Some(source)))
|
||||
.unwrap();
|
||||
vm::Interpreter::builder(Default::default())
|
||||
.add_frozen_modules(rustpython_vm::py_freeze!(
|
||||
dir = "../../../../extra_tests/snippets"
|
||||
))
|
||||
.build()
|
||||
.enter(|vm| {
|
||||
let scope = vm.new_scope_with_builtins();
|
||||
|
||||
if let Err(e) = vm.run_code_obj(code_obj, scope) {
|
||||
vm.print_exception(e);
|
||||
panic!();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frozen_origname_matches() {
|
||||
use rustpython_vm as vm;
|
||||
|
||||
vm::Interpreter::builder(Default::default())
|
||||
.build()
|
||||
.enter(|vm| {
|
||||
let check = |name, expected| {
|
||||
let module = import::import_frozen(vm, name).unwrap();
|
||||
let origname: PyStrRef = module
|
||||
.get_attr("__origname__", vm)
|
||||
.unwrap()
|
||||
.try_into_value(vm)
|
||||
let source = "from dir_module.dir_module_inner import value2";
|
||||
let code_obj = vm
|
||||
.compile(source, vm::compiler::Mode::Exec, "<embedded>".to_owned())
|
||||
.map_err(|err| vm.new_syntax_error(&err, Some(source)))
|
||||
.unwrap();
|
||||
assert_eq!(origname.as_wtf8(), expected);
|
||||
};
|
||||
|
||||
check("_frozen_importlib", "importlib._bootstrap");
|
||||
check(
|
||||
"_frozen_importlib_external",
|
||||
"importlib._bootstrap_external",
|
||||
);
|
||||
});
|
||||
if let Err(e) = vm.run_code_obj(code_obj, scope) {
|
||||
vm.print_exception(e);
|
||||
panic!();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frozen_origname_matches() {
|
||||
use rustpython_vm as vm;
|
||||
|
||||
vm::Interpreter::builder(Default::default())
|
||||
.build()
|
||||
.enter(|vm| {
|
||||
let check = |name, expected| {
|
||||
let module = import::import_frozen(vm, name).unwrap();
|
||||
let origname: PyStrRef = module
|
||||
.get_attr("__origname__", vm)
|
||||
.unwrap()
|
||||
.try_into_value(vm)
|
||||
.unwrap();
|
||||
assert_eq!(origname.as_wtf8(), expected);
|
||||
};
|
||||
|
||||
check("_frozen_importlib", "importlib._bootstrap");
|
||||
check(
|
||||
"_frozen_importlib_external",
|
||||
"importlib._bootstrap_external",
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -184,7 +184,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_block_expr_return_const() {
|
||||
fn block_expr_return_const() {
|
||||
interpreter().enter(|vm| {
|
||||
let scope = vm.new_scope_with_builtins();
|
||||
let value = vm.unwrap_pyresult(vm.run_block_expr(scope, "1"));
|
||||
@@ -195,7 +195,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_block_expr_return_nonconst() {
|
||||
fn block_expr_return_nonconst() {
|
||||
interpreter().enter(|vm| {
|
||||
let scope = vm.new_scope_with_builtins();
|
||||
vm.unwrap_pyresult(scope.globals.set_item("x", vm.new_pyobj(3), vm));
|
||||
|
||||
Reference in new issue
Block a user