use super::*;
use crate::array::Array;
#[test]
fn test_ellipsis_2d_first_position() {
let arr = Array::from_vec(vec![
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
])
.reshape(&[3, 4]);
let result = arr
.index(&[IndexSpec::Ellipsis, IndexSpec::Index(0)])
.expect("indexing with ellipsis should succeed");
assert_eq!(result.shape(), &[3]);
assert_eq!(result.to_vec(), vec![1.0, 5.0, 9.0]);
}
#[test]
fn test_ellipsis_2d_last_position() {
let arr = Array::from_vec(vec![
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
])
.reshape(&[3, 4]);
let result = arr
.index(&[IndexSpec::Index(0), IndexSpec::Ellipsis])
.expect("indexing with ellipsis should succeed");
assert_eq!(result.shape(), &[4]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
}
#[test]
fn test_ellipsis_3d_middle() {
let arr: Array<f64> = Array::from_vec((0..24).map(|x| x as f64).collect()).reshape(&[2, 3, 4]);
let result = arr
.index(&[
IndexSpec::Index(0),
IndexSpec::Ellipsis,
IndexSpec::Index(0),
])
.expect("indexing with ellipsis should succeed");
assert_eq!(result.shape(), &[3]);
assert_eq!(result.to_vec(), vec![0.0, 4.0, 8.0]);
}
#[test]
fn test_ellipsis_alone() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
let result = arr
.index(&[IndexSpec::Ellipsis])
.expect("indexing with ellipsis should succeed");
assert_eq!(result.shape(), &[2, 2]);
assert_eq!(result.to_vec(), arr.to_vec());
}
#[test]
fn test_ellipsis_with_slice() {
let arr = Array::from_vec(vec![
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
])
.reshape(&[3, 4]);
let result = arr
.index(&[IndexSpec::Ellipsis, IndexSpec::Slice(1, Some(3), None)])
.expect("indexing with ellipsis and slice should succeed");
assert_eq!(result.shape(), &[3, 2]);
assert_eq!(result.to_vec(), vec![2.0, 3.0, 6.0, 7.0, 10.0, 11.0]);
}
#[test]
fn test_newaxis_1d_to_2d_row() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr
.index(&[IndexSpec::NewAxis, IndexSpec::All])
.expect("indexing with newaxis should succeed");
assert_eq!(result.shape(), &[1, 3]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0]);
}
#[test]
fn test_newaxis_1d_to_2d_column() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr
.index(&[IndexSpec::All, IndexSpec::NewAxis])
.expect("indexing with newaxis should succeed");
assert_eq!(result.shape(), &[3, 1]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0]);
}
#[test]
fn test_newaxis_2d_add_front() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);
let result = arr
.index(&[IndexSpec::NewAxis, IndexSpec::All, IndexSpec::All])
.expect("indexing with newaxis should succeed");
assert_eq!(result.shape(), &[1, 2, 3]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
}
#[test]
fn test_newaxis_2d_add_middle() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);
let result = arr
.index(&[IndexSpec::All, IndexSpec::NewAxis, IndexSpec::All])
.expect("indexing with newaxis should succeed");
assert_eq!(result.shape(), &[2, 1, 3]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
}
#[test]
fn test_newaxis_2d_add_end() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);
let result = arr
.index(&[IndexSpec::All, IndexSpec::All, IndexSpec::NewAxis])
.expect("indexing with newaxis should succeed");
assert_eq!(result.shape(), &[2, 3, 1]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
}
#[test]
fn test_newaxis_multiple() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0]);
let result = arr
.index(&[IndexSpec::NewAxis, IndexSpec::All, IndexSpec::NewAxis])
.expect("indexing with multiple newaxis should succeed");
assert_eq!(result.shape(), &[1, 3, 1]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0]);
}
#[test]
fn test_newaxis_with_ellipsis() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);
let result = arr
.index(&[IndexSpec::NewAxis, IndexSpec::Ellipsis])
.expect("indexing with newaxis and ellipsis should succeed");
assert_eq!(result.shape(), &[1, 2, 3]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
}
#[test]
fn test_ellipsis_with_newaxis() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);
let result = arr
.index(&[IndexSpec::Ellipsis, IndexSpec::NewAxis])
.expect("indexing with ellipsis and newaxis should succeed");
assert_eq!(result.shape(), &[2, 3, 1]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
}
#[test]
fn test_newaxis_with_index() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);
let result = arr
.index(&[IndexSpec::Index(0), IndexSpec::NewAxis, IndexSpec::All])
.expect("indexing with index and newaxis should succeed");
assert_eq!(result.shape(), &[1, 3]);
assert_eq!(result.to_vec(), vec![1.0, 2.0, 3.0]);
}
#[test]
fn test_helper_insert_newaxis() {
let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);
let result = insert_newaxis(&arr, &[0]).expect("insert_newaxis at position 0 should succeed");
assert_eq!(result.shape(), &[1, 2, 3]);
let result = insert_newaxis(&arr, &[1]).expect("insert_newaxis at position 1 should succeed");
assert_eq!(result.shape(), &[2, 1, 3]);
let result = insert_newaxis(&arr, &[2]).expect("insert_newaxis at position 2 should succeed");
assert_eq!(result.shape(), &[2, 3, 1]);
let result =
insert_newaxis(&arr, &[0, 2]).expect("insert_newaxis at multiple positions should succeed");
assert_eq!(result.shape(), &[1, 2, 1, 3]);
}