#[cfg(feature = "local-rt")] pub(crate) mod tests { use jlrs::{ data::{ managed::array::data::accessor::Accessor, types::construct_type::UnionTypeConstructor, }, prelude::*, }; use crate::util::JULIA; fn union_data() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { unsafe { let arr = Value::eval_string(&mut frame, "Union{Int, Float64}[1 2.0]").unwrap(); let arr = arr .cast::>>() .unwrap(); let accessor = arr.union_data(); assert_eq!(accessor.get::([0, 0]).unwrap().unwrap(), 1); assert_eq!(accessor.get::([0, 1]).unwrap().unwrap(), 2.0); } Ok(()) }) .unwrap(); }); } fn union_data_get_unchecked() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { unsafe { let arr = Value::eval_string(&mut frame, "Union{Int, Float64}[1 2.0]").unwrap(); let arr = arr .cast::>>() .unwrap(); let accessor = arr.union_data(); assert_eq!(accessor.get_unchecked::([0, 0]), 1); assert_eq!(accessor.get_unchecked::([0, 1]), 2.0); } Ok(()) }) .unwrap(); }); } fn union_data_get_value() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { unsafe { let arr = Value::eval_string(&mut frame, "Union{Int, Float64}[1 2.0]").unwrap(); let arr = arr .cast::>>() .unwrap(); let accessor = arr.union_data(); assert_eq!( accessor .get_value(&mut frame, [0, 0]) .unwrap() .unwrap() .unbox::() .unwrap(), 1 ); assert_eq!( accessor .get_value(&mut frame, [0, 1]) .unwrap() .unwrap() .unbox::() .unwrap(), 2.0 ); } Ok(()) }) .unwrap(); }); } fn union_data_get_value_unchecked() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { unsafe { let arr = Value::eval_string(&mut frame, "Union{Int, Float64}[1 2.0]").unwrap(); let arr = arr .cast::>>() .unwrap(); let accessor = arr.union_data(); assert_eq!( accessor .get_value_unchecked(&mut frame, [0, 0]) .unbox::() .unwrap(), 1 ); assert_eq!( accessor .get_value_unchecked(&mut frame, [0, 1]) .unbox::() .unwrap(), 2.0 ); } Ok(()) }) .unwrap(); }); } fn try_union_data() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { unsafe { let arr = Value::eval_string(&mut frame, "Union{Int, Float64}[1 2.0]").unwrap(); let arr = arr .cast::>>() .unwrap(); let accessor = arr.try_union_data(); assert!(accessor.is_ok()); } Ok(()) }) .unwrap(); }); } fn try_union_data_err() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { unsafe { let arr = Value::eval_string(&mut frame, "Int[1 2]").unwrap(); let arr = arr.cast::().unwrap(); let accessor = arr.try_union_data(); assert!(accessor.is_err()); } Ok(()) }) .unwrap(); }); } fn union_data_unchecked() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { unsafe { let arr = Value::eval_string(&mut frame, "Union{Int, Float64}[1 2.0]").unwrap(); let arr = arr.cast::().unwrap(); let accessor = arr.union_data_unchecked(); assert_eq!(accessor.get::([0, 0]).unwrap().unwrap(), 1); assert_eq!(accessor.get::([0, 1]).unwrap().unwrap(), 2.0); } Ok(()) }) .unwrap(); }); } pub(crate) fn array_union_data_tests() { union_data(); union_data_get_unchecked(); union_data_get_value(); union_data_get_value_unchecked(); try_union_data(); try_union_data_err(); union_data_unchecked(); } }