#[cfg(feature = "ndarray")] mod ndarray_try_from_tests { use extendr_api::prelude::*; #[test] fn test_1d_view_from_robj() { test! { let robj = R!("c(1L, 2L, 3L, 4L, 5L)")?; let view = >::try_from(robj)?; assert_eq!(view.dim(), 5); } } #[test] fn integer_matrix_2d() { test! { let robj = R!("matrix(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), ncol = 2, nrow = 4, byrow = TRUE)")?; let view = >::try_from(&robj)?; assert_eq!(view.dim(), (4, 2)); for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected); } let view = >::try_from(&robj)?; assert_eq!(view.dim(), (4, 2)); for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected); } for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected); } } } #[test] fn real_matrix_2d() { test! { let robj = R!("matrix(c(1, 2, 3, 4, 5, 6, 7, 8), ncol = 2, nrow = 4, byrow = TRUE)")?; let view = >::try_from(&robj)?; assert_eq!(view.dim(), (4, 2)); for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected as f64); } let view = >::try_from(&robj)?; assert_eq!(view.dim(), (4, 2)); for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected as f64); } } } #[test] fn logical_matrix_2d() { test! { let robj = R!("matrix(c(TRUE, FALSE, TRUE, FALSE, FALSE, TRUE), ncol = 2, nrow = 3, byrow = TRUE)")?; let view = >::try_from(&robj)?; assert_eq!(view.dim(), (3, 2)); for (&mapped, expected) in view.iter().zip(vec![true, false, true, false, false, true]) { assert_eq!(mapped, expected); } } } #[test] fn complex_matrix_2d() { test! { let robj = R!("matrix(c(1, 2, 3i, 4i, 5 + 5i, 6 - 6i), ncol = 2, nrow = 3, byrow = TRUE)")?; let expected = vec![1, 2, 0, 0, 5, 6].iter().zip(vec![0, 0, 3, 4, 5, -6]).map(|(&re, im)| ::new(re as f64, im as f64)) .collect::>(); let view = >::try_from(&robj)?; assert_eq!(view.dim(), (3, 2)); for (&mapped, &expected) in view.iter().zip(expected.iter()) { assert_eq!(mapped, ::from(expected)); } let view = >::try_from(&robj)?; assert_eq!(view.dim(), (3, 2)); for (&mapped, &expected) in view.iter().zip(expected.iter()) { assert_eq!(mapped, expected); } } } #[test] fn character_matrix_2d() { test! { let robj = R!("matrix(c(\"Hello\", \"World\"), ncol = 1, nrow = 2, byrow = TRUE)")?; let view = >::try_from(&robj)?; assert_eq!(view.dim(), (2, 1)); for (mapped, expected) in view.iter().zip(vec!["Hello", "World"]) { assert_eq!(mapped, expected); } } } #[test] fn integer_matrix_1d() { test! { let robj = R!("matrix(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), ncol = 1, nrow = 8)")?; let view = >::try_from(&robj)?; assert_eq!(view.dim(), 8); for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected); } let view = >::try_from(&robj)?; assert_eq!(view.dim(), 8); for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected); } } } #[test] fn real_matrix_1d() { test! { let robj = R!("matrix(c(1, 2, 3, 4, 5, 6, 7, 8), ncol = 1, nrow = 8)")?; let view = >::try_from(&robj)?; assert_eq!(view.dim(), 8); for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected as f64); } let view = >::try_from(&robj)?; assert_eq!(view.dim(), 8); for (&mapped, expected) in view.iter().zip(1..=8) { assert_eq!(mapped, expected as f64); } } } #[test] fn logical_matrix_1d() { test! { let robj = R!("matrix(c(TRUE, FALSE, TRUE, FALSE, FALSE, TRUE), ncol = 1, nrow = 6)")?; let view = >::try_from(&robj)?; assert_eq!(view.dim(), 6); for (&mapped, expected) in view.iter().zip(vec![true, false, true, false, false, true]) { assert_eq!(mapped, expected); } } } #[test] fn complex_matrix_1d() { test! { let robj = R!("matrix(c(1, 2, 3i, 4i, 5 + 5i, 6 - 6i), ncol = 1, nrow = 6)")?; let expected = vec![1, 2, 0, 0, 5, 6].iter().zip(vec![0, 0, 3, 4, 5, -6]).map(|(&re, im)| ::new(re as f64, im as f64)) .collect::>(); let view = >::try_from(&robj)?; assert_eq!(view.dim(), 6); for (&mapped, &expected) in view.iter().zip(expected.iter()) { assert_eq!(mapped, ::from(expected)); } let view = >::try_from(&robj)?; assert_eq!(view.dim(), 6); for (&mapped, &expected) in view.iter().zip(expected.iter()) { assert_eq!(mapped, expected); } } } #[test] fn character_matrix_1d() { test! { let robj = R!("matrix(c(\"Hello\", \"World\"), ncol = 1, nrow = 2)")?; let view = >::try_from(&robj)?; assert_eq!(view.dim(), 2); for (mapped, expected) in view.iter().zip(vec!["Hello", "World"]) { assert_eq!(mapped, expected); } } } #[test] fn integer_matrix_1d_type_mismatch() { test! { let robj = R!("matrix(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), ncol = 1, nrow = 8)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn real_matrix_1d_type_mismatch() { test! { let robj = R!("matrix(c(1, 2, 3, 4, 5, 6, 7, 8), ncol = 1, nrow = 8)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn logical_matrix_1d_type_mismatch() { test! { let robj = R!("matrix(c(TRUE, FALSE, TRUE, FALSE, FALSE, TRUE), ncol = 1, nrow = 6)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn complex_matrix_1d_type_mismatch() { test! { let robj = R!("matrix(c(1, 2, 3i, 4i, 5 + 5i, 6 - 6i), ncol = 1, nrow = 6)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn character_matrix_1d_type_mismatch() { test! { let robj = R!("matrix(c(\"Hello\", \"World\"), ncol = 1, nrow = 2)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn integer_matrix_2d_type_mismatch() { test! { let robj = R!("matrix(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), ncol = 2, nrow = 4, byrow = TRUE)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn real_matrix_2d_type_mismatch() { test! { let robj = R!("matrix(c(1, 2, 3, 4, 5, 6, 7, 8), ncol = 2, nrow = 4, byrow = TRUE)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn logical_matrix_2d_type_mismatch() { test! { let robj = R!("matrix(c(TRUE, FALSE, TRUE, FALSE, FALSE, TRUE), ncol = 2, nrow = 3, byrow = TRUE)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn complex_matrix_2d_type_mismatch() { test! { let robj = R!("matrix(c(1, 2, 3i, 4i, 5 + 5i, 6 - 6i), ncol = 2, nrow = 3, byrow = TRUE)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } #[test] fn character_matrix_2d_type_mismatch() { test! { let robj = R!("matrix(c(\"Hello\", \"World\"), ncol = 1, nrow = 2, byrow = TRUE)")?; assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); assert!(>::try_from(&robj).is_err()); } } }