use numrs2::array::Array;
use numrs2::indexing::*;
#[test]
fn test_ellipsis_indexing() {
let mut array = Array::<f64>::zeros(&[2, 3, 4]);
for i in 0..2 {
for j in 0..3 {
for k in 0..4 {
let value = (i * 100 + j * 10 + k) as f64;
array.set(&[i, j, k], value).unwrap();
}
}
}
let result1 = array
.index(&[IndexSpec::Ellipsis, IndexSpec::Index(0)])
.unwrap();
assert_eq!(result1.shape(), vec![2, 3]);
for i in 0..2 {
for j in 0..3 {
assert_eq!(result1.get(&[i, j]).unwrap(), (i * 100 + j * 10) as f64);
}
}
let result2 = array
.index(&[IndexSpec::Index(0), IndexSpec::Ellipsis])
.unwrap();
assert_eq!(result2.shape(), vec![3, 4]);
for j in 0..3 {
for k in 0..4 {
assert_eq!(result2.get(&[j, k]).unwrap(), (j * 10 + k) as f64);
}
}
let result3 = array
.index(&[
IndexSpec::Ellipsis,
IndexSpec::Index(0),
IndexSpec::Index(0),
])
.unwrap();
assert_eq!(result3.shape(), vec![2]);
for i in 0..2 {
assert_eq!(result3.get(&[i]).unwrap(), (i * 100) as f64);
}
}
#[test]
fn test_fancy_indexing() {
let mut array = Array::<i32>::zeros(&[3, 4]);
for i in 0..3 {
for j in 0..4 {
let value = (i * 10 + j) as i32;
array.set(&[i, j], value).unwrap();
}
}
let result1 = array
.index(&[IndexSpec::Indices(vec![0, 2]), IndexSpec::All])
.unwrap();
assert_eq!(result1.shape(), vec![2, 4]);
for j in 0..4 {
assert_eq!(result1.get(&[0, j]).unwrap(), j as i32);
assert_eq!(result1.get(&[1, j]).unwrap(), (20 + j) as i32);
}
let result2 = array
.index(&[IndexSpec::All, IndexSpec::Indices(vec![1, 3])])
.unwrap();
assert_eq!(result2.shape(), vec![3, 2]);
for i in 0..3 {
assert_eq!(result2.get(&[i, 0]).unwrap(), (i * 10 + 1) as i32);
assert_eq!(result2.get(&[i, 1]).unwrap(), (i * 10 + 3) as i32);
}
}
#[test]
fn test_mask_indices() {
let indices = mask_indices(&[3, 3], |idx| idx[0] <= idx[1]).unwrap();
assert_eq!(indices.len(), 2);
assert_eq!(indices[0].to_vec(), vec![0, 0, 0, 1, 1, 2]);
assert_eq!(indices[1].to_vec(), vec![0, 1, 2, 1, 2, 2]);
}
#[test]
fn test_ix_function() {
let indices = indices_grid::<usize>(&[2, 3]).unwrap();
assert_eq!(indices.len(), 2);
assert_eq!(indices[0].shape(), vec![2, 1]);
assert_eq!(indices[1].shape(), vec![1, 3]);
assert_eq!(indices[0].get(&[0, 0]).unwrap(), 0);
assert_eq!(indices[0].get(&[1, 0]).unwrap(), 1);
assert_eq!(indices[1].get(&[0, 0]).unwrap(), 0);
assert_eq!(indices[1].get(&[0, 1]).unwrap(), 1);
assert_eq!(indices[1].get(&[0, 2]).unwrap(), 2);
}