use lb::mat;
use lb::mat::Matrix;
use lb::mat::MatrixMut;
#[test]
fn matrix_vec_new() {
let vec = vec![0, 1, 2, 3];
let mat = mat::MatrixVec::new(vec.clone(), (2, 2).into()).unwrap();
assert_eq!(mat.as_slice(), [0, 1, 2, 3]);
assert_eq!(mat.size(), (2, 2).into());
let err = mat::MatrixVec::<i32>::new(vec.clone(), (99, 99).into());
assert_eq!(err, Err(mat::Error::InvalidSize));
}
#[test]
fn matrix_ref_new() {
let values = [
0, 1, 2, 3, 4, 5, ];
let mat = mat::MatrixRef::new(&values, (3, 2).into()).unwrap();
assert_eq!(mat.as_slice(), [0, 1, 2, 3, 4, 5]);
assert_eq!(mat.size(), (3, 2).into());
let err = mat::MatrixRef::new(&values[1..], (3, 2).into());
assert_eq!(err, Err(mat::Error::InvalidSize));
}
#[test]
fn matrix_mut_new() {
let mut values = [
0, 1, 2, 3, 4, 5, ];
let mat = mat::MatrixRefMut::new(&mut values, (3, 2).into()).unwrap();
assert_eq!(mat.as_slice(), [0, 1, 2, 3, 4, 5]);
assert_eq!(mat.size(), (3, 2).into());
let err = mat::MatrixRefMut::new(&mut values[1..], (3, 2).into());
assert_eq!(err, Err(mat::Error::InvalidSize));
}
#[test]
fn matrix_to_owned() {
let mat = lb::MatrixRef::from(&[
[00, 01, 02, 03], [04, 05, 06, 07], [08, 09, 10, 11], ]);
let own0 = mat.to_owned();
assert_eq!(
own0,
lb::MatrixVec::from([
[00, 01, 02, 03], [04, 05, 06, 07], [08, 09, 10, 11], ])
);
let window = mat.window((1, 0), (2, 2).into()).unwrap();
let own1 = window.to_owned();
assert_eq!(
own1,
lb::MatrixVec::from([
[01, 02], [05, 06], ])
);
}
#[test]
fn matrix_vec_with_default() {
let mat = mat::MatrixVec::<i32>::with_default((3, 2).into());
assert_eq!(mat.as_slice(), [0i32; 6]);
assert_eq!(mat.size(), (3, 2).into());
}
#[test]
fn matrix_vec_with() {
let mat = mat::MatrixVec::with(42, (2, 2).into());
assert_eq!(mat.as_slice(), [42, 42, 42, 42]);
assert_eq!(mat.size(), (2, 2).into());
}
#[test]
fn matrix_index() {
let values = [
0, 1, 2, 3, 4, 5, ];
let mat = mat::MatrixRef::new(&values, (3, 2).into()).unwrap();
assert_eq!(mat[(2, 1)], 5);
}
#[test]
fn matrix_index_mut() {
let mut values = [
0, 1, 2, 3, 4, 5, ];
let mut mat = mat::MatrixRefMut::new(&mut values, (2, 3).into()).unwrap();
mat[(0, 2)] = 42;
assert_eq!(values, [0, 1, 2, 3, 42, 5]);
}
#[test]
fn matrix_get_pair_mut() {
let mut values = [
0, 1, 2, 3, 4, 5, ];
let mut mat = mat::MatrixRefMut::new(&mut values, (2, 3).into()).unwrap();
let pair = mat.get_pair_mut((1, 2), (1, 0));
assert_eq!(pair, Some((&mut 5, &mut 1)));
let pair = mat.get_pair_mut((1, 3), (1, 0));
assert_eq!(pair, None);
let pair = mat.get_pair_mut((0, 0), (0, 0));
assert_eq!(pair, None);
}
#[test]
fn matrix_vec_from() {
let mat = lb::MatrixVec::from([
[00, 01, 02], [03, 04, 05], [06, 07, 08], [09, 10, 11], ]);
assert_eq!(mat.as_slice(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
assert_eq!(mat.size(), (3, 4).into());
assert_eq!(mat.stride(), 3);
let mat = lb::MatrixVec::<i32>::from([[]]);
assert_eq!(mat.as_slice(), []);
assert_eq!(mat.size(), (0, 1).into());
assert_eq!(mat.stride(), 0);
}
#[test]
fn matrix_ref_from() {
let mat = lb::MatrixRef::from(&[
[00, 01, 02], [03, 04, 05], [06, 07, 08], [09, 10, 11], ]);
assert_eq!(mat.as_slice(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
assert_eq!(mat.size(), (3, 4).into());
assert_eq!(mat.stride(), 3);
let mat = lb::MatrixRef::<i32>::from(&[[]]);
assert_eq!(mat.as_slice(), []);
assert_eq!(mat.size(), (0, 1).into());
assert_eq!(mat.stride(), 0);
}
#[test]
fn matrix_ref_mut_from() {
let mut arr = [
[00, 01, 02], [03, 04, 05], [06, 07, 08], [09, 10, 11], ];
let mat = lb::MatrixRefMut::from(&mut arr);
assert_eq!(mat.as_slice(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
assert_eq!(mat.size(), (3, 4).into());
assert_eq!(mat.stride(), 3);
let mut arr = [[]];
let mat = lb::MatrixRefMut::<i32>::from(&mut arr);
assert_eq!(mat.as_slice(), []);
assert_eq!(mat.size(), (0, 1).into());
assert_eq!(mat.stride(), 0);
}
#[test]
fn window() {
let mat = lb::MatrixRef::from(&[
[00, 01, 02, 03, 04, 05, 06], [07, 08, 09, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], ]);
assert_eq!(mat.window((0, 0), (7, 4).into()), Ok(mat.as_ref()));
assert_eq!(
mat.window((1, 2), (3, 2).into()),
Ok(lb::MatrixRef::from(&[
[15, 16, 17], [22, 23, 24], ]))
);
assert_eq!(
mat.window((0, 0), (1, 1).into()),
Ok(lb::MatrixRef::from(&[[00]]))
);
assert_eq!(
mat.window((6, 3), (1, 1).into()),
Ok(lb::MatrixRef::from(&[[27]]))
);
use lb::mat::Error::OutOfBounds;
assert_eq!(mat.window((7, 0), (1, 1).into()), Err(OutOfBounds));
assert_eq!(mat.window((0, 4), (1, 1).into()), Err(OutOfBounds));
use lb::mat::Error::InvalidSize;
assert_eq!(mat.window((0, 0), (8, 4).into()), Err(InvalidSize));
assert_eq!(mat.window((0, 0), (7, 5).into()), Err(InvalidSize));
assert_eq!(
mat.window((0, 0), (0, 0).into()),
lb::MatrixRef::<i32>::new(&[], (0, 0).into())
);
assert_eq!(
mat.window((0, 1), (0, 0).into()),
lb::MatrixRef::<i32>::new(&[], (0, 0).into())
);
assert_eq!(
mat.window((1, 0), (0, 0).into()),
lb::MatrixRef::<i32>::new(&[], (0, 0).into())
);
assert_eq!(
mat.window((0, 0), (0, 1).into()),
lb::MatrixRef::<i32>::new(&[], (0, 1).into())
);
assert_eq!(
mat.window((0, 0), (1, 0).into()),
lb::MatrixRef::<i32>::new(&[], (1, 0).into())
);
}
#[test]
fn window_mut() {
let mut mat = lb::MatrixVec::from([
[00, 01, 02, 03, 04, 05, 06], [07, 08, 09, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], ]);
assert_eq!(
mat.window_mut((0, 0), (7, 4).into()),
Ok(lb::MatrixRefMut::from(&mut [
[00, 01, 02, 03, 04, 05, 06], [07, 08, 09, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], ]))
);
assert_eq!(
mat.window_mut((1, 2), (3, 2).into()),
Ok(lb::MatrixRefMut::from(&mut [
[15, 16, 17], [22, 23, 24], ]))
);
assert_eq!(
mat.window_mut((0, 0), (1, 1).into()),
Ok(lb::MatrixRefMut::from(&mut [[00]]))
);
assert_eq!(
mat.window_mut((6, 3), (1, 1).into()),
Ok(lb::MatrixRefMut::from(&mut [[27]]))
);
use lb::mat::Error::OutOfBounds;
assert_eq!(mat.window_mut((7, 0), (1, 1).into()), Err(OutOfBounds));
assert_eq!(mat.window_mut((0, 4), (1, 1).into()), Err(OutOfBounds));
use lb::mat::Error::InvalidSize;
assert_eq!(mat.window_mut((0, 0), (8, 4).into()), Err(InvalidSize));
assert_eq!(mat.window_mut((0, 0), (7, 5).into()), Err(InvalidSize));
assert_eq!(
mat.window_mut((0, 0), (0, 0).into()),
lb::MatrixRefMut::<i32>::new(&mut [], (0, 0).into())
);
assert_eq!(
mat.window_mut((0, 1), (0, 0).into()),
lb::MatrixRefMut::<i32>::new(&mut [], (0, 0).into())
);
assert_eq!(
mat.window_mut((1, 0), (0, 0).into()),
lb::MatrixRefMut::<i32>::new(&mut [], (0, 0).into())
);
assert_eq!(
mat.window_mut((0, 0), (0, 1).into()),
lb::MatrixRefMut::<i32>::new(&mut [], (0, 1).into())
);
assert_eq!(
mat.window_mut((0, 0), (1, 0).into()),
lb::MatrixRefMut::<i32>::new(&mut [], (1, 0).into())
);
}
#[test]
fn rows_size_hint() {
let mat = mat::MatrixVec::<i32>::from([[]]);
assert_eq!(mat.rows().size_hint(), (0, Some(0)));
let mat = mat::MatrixVec::from([
[00, 01, 02], [03, 04, 05], [06, 07, 08], [09, 10, 11], ]);
assert_eq!(mat.rows().size_hint(), (4, Some(4)));
let win = mat.window((1, 1), (2, 3).into()).unwrap();
assert_eq!(win.rows().size_hint(), (3, Some(3)));
}
#[test]
fn matrix_ref_rows() {
let mat = mat::MatrixVec::<i32>::from([[]]);
let mut rows = mat.rows();
assert_eq!(rows.next(), None);
let values = [
0, 1, 2, 3, 4, 5, 6, 7, 8, ];
let mat = mat::MatrixRef::new(&values, (3, 3).into()).unwrap();
let mut rows = mat.rows();
assert_eq!(rows.next().unwrap(), [0, 1, 2]);
assert_eq!(rows.next().unwrap(), [3, 4, 5]);
assert_eq!(rows.next().unwrap(), [6, 7, 8]);
assert_eq!(rows.next(), None);
let mut rows = mat.rows();
assert_eq!(rows.next_back().unwrap(), [6, 7, 8]);
assert_eq!(rows.next_back().unwrap(), [3, 4, 5]);
assert_eq!(rows.next_back().unwrap(), [0, 1, 2]);
assert_eq!(rows.next_back(), None);
{
let win = mat.window((1, 1), (2, 2).into()).unwrap();
let mut rows = win.rows();
assert_eq!(rows.next().unwrap(), [4, 5]);
assert_eq!(rows.next().unwrap(), [7, 8]);
assert_eq!(rows.next(), None);
}
}
#[test]
fn rows_mut_size_hint() {
let mut mat = mat::MatrixVec::<i32>::from([[]]);
assert_eq!(mat.rows_mut().size_hint(), (0, Some(0)));
let mut mat = mat::MatrixVec::from([
[00, 01, 02], [03, 04, 05], [06, 07, 08], [09, 10, 11], ]);
assert_eq!(mat.rows_mut().size_hint(), (4, Some(4)));
let mut win = mat.window_mut((1, 1), (2, 3).into()).unwrap();
assert_eq!(win.rows_mut().size_hint(), (3, Some(3)));
}
#[test]
fn matrix_mut_rows_mut() {
let mut mat = mat::MatrixVec::<i32>::from([[]]);
let mut rows = mat.rows_mut();
assert_eq!(rows.next(), None);
let mut values = [0, 1, 2, 3, 4, 5, 6, 7, 8];
let mut mat = mat::MatrixRefMut::new(&mut values, (3, 3).into()).unwrap();
{
let mut rows = mat.rows_mut();
assert_eq!(rows.next_back().unwrap(), [6, 7, 8]);
assert_eq!(rows.next_back().unwrap(), [3, 4, 5]);
assert_eq!(rows.next_back().unwrap(), [0, 1, 2]);
assert_eq!(rows.next_back(), None);
}
{
let win = mat.window_mut((1, 1), (2, 2).into()).unwrap();
let mut rows = win.rows();
assert_eq!(rows.next().unwrap(), [4, 5]);
assert_eq!(rows.next().unwrap(), [7, 8]);
assert_eq!(rows.next(), None);
}
let mut rows = mat.rows_mut();
assert_eq!(rows.next().unwrap(), [0, 1, 2]);
let middle = rows.next().unwrap();
assert_eq!(middle, [3, 4, 5]);
middle[1] = 42;
assert_eq!(rows.next().unwrap(), [6, 7, 8]);
assert_eq!(rows.next(), None);
assert_eq!(values, [0, 1, 2, 3, 42, 5, 6, 7, 8]);
}
#[test]
fn splits_size_hint() {
let mat = mat::MatrixVec::from([
[00, 01, 02], [03, 04, 05], [06, 07, 08], [09, 10, 11], ]);
assert_eq!(mat.splits(1).size_hint(), (4, Some(4)));
assert_eq!(mat.splits(2).size_hint(), (2, Some(2)));
assert_eq!(mat.splits(3).size_hint(), (2, Some(2)));
assert_eq!(mat.splits(4).size_hint(), (1, Some(1)));
assert_eq!(mat.splits(5).size_hint(), (1, Some(1)));
let win = mat.window((1, 1), (2, 3).into()).unwrap();
assert_eq!(win.splits(1).size_hint(), (3, Some(3)));
assert_eq!(win.splits(2).size_hint(), (2, Some(2)));
assert_eq!(win.splits(3).size_hint(), (1, Some(1)));
assert_eq!(win.splits(4).size_hint(), (1, Some(1)));
}
#[test]
fn splits_next() {
let mat = mat::MatrixVec::from([
[00, 01, 02, 03],
[04, 05, 06, 07],
[08, 09, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
]);
{
let win = mat.window((1, 1), (2, 5).into()).unwrap();
let mut splits = win.splits(2);
assert_eq!(
splits.next(),
Some(lb::MatrixRef::from(&[
[05, 06], [09, 10], ]))
);
assert_eq!(
splits.next(),
Some(lb::MatrixRef::from(&[
[13, 14], [17, 18], ]))
);
assert_eq!(splits.next(), Some(lb::MatrixRef::from(&[[21, 22]])));
assert_eq!(splits.next(), None);
}
{
let mut splits = mat.splits(2);
assert_eq!(
splits.next(),
Some(lb::MatrixRef::from(&[
[00, 01, 02, 03], [04, 05, 06, 07], ]))
);
assert_eq!(
splits.next(),
Some(lb::MatrixRef::from(&[
[08, 09, 10, 11], [12, 13, 14, 15], ]))
);
assert_eq!(
splits.next(),
Some(lb::MatrixRef::from(&[
[16, 17, 18, 19], [20, 21, 22, 23], ]))
);
assert_eq!(splits.next(), None);
}
{
let mut splits = mat.splits(4);
assert_eq!(
splits.next(),
Some(lb::MatrixRef::from(&[
[00, 01, 02, 03], [04, 05, 06, 07], [08, 09, 10, 11], [12, 13, 14, 15], ]))
);
assert_eq!(
splits.next(),
Some(lb::MatrixRef::from(&[
[16, 17, 18, 19], [20, 21, 22, 23], ]))
);
assert_eq!(splits.next(), None);
}
let mut splits = mat.splits(7);
assert_eq!(splits.next(), Some(mat.as_ref()));
assert_eq!(splits.next(), None);
let mat = mat::MatrixVec::from([[0]]);
let mut splits = mat.splits(0);
assert_eq!(splits.next(), None);
let mat = mat::MatrixVec::<i32>::from([[]]);
let mut splits = mat.splits(1);
assert_eq!(splits.next(), None);
}
#[test]
fn splits_mut_size_hint() {
let mut mat = mat::MatrixVec::from([
[00, 01, 02], [03, 04, 05], [06, 07, 08], [09, 10, 11], ]);
assert_eq!(mat.splits_mut(1).size_hint(), (4, Some(4)));
assert_eq!(mat.splits_mut(2).size_hint(), (2, Some(2)));
assert_eq!(mat.splits_mut(3).size_hint(), (2, Some(2)));
assert_eq!(mat.splits_mut(4).size_hint(), (1, Some(1)));
assert_eq!(mat.splits_mut(5).size_hint(), (1, Some(1)));
let mut win = mat.window_mut((1, 1), (2, 3).into()).unwrap();
assert_eq!(win.splits_mut(1).size_hint(), (3, Some(3)));
assert_eq!(win.splits_mut(2).size_hint(), (2, Some(2)));
assert_eq!(win.splits_mut(3).size_hint(), (1, Some(1)));
assert_eq!(win.splits_mut(4).size_hint(), (1, Some(1)));
}
#[test]
fn splits_mut_next() {
let mut mat = mat::MatrixVec::from([
[00, 01, 02, 03],
[04, 05, 06, 07],
[08, 09, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
]);
{
let mut win = mat.window_mut((1, 1), (2, 5).into()).unwrap();
let mut splits = win.splits_mut(2);
assert_eq!(
splits.next(),
Some(lb::MatrixRefMut::from(&mut [
[05, 06], [09, 10], ]))
);
assert_eq!(
splits.next(),
Some(lb::MatrixRefMut::from(&mut [
[13, 14], [17, 18], ]))
);
assert_eq!(splits.next(), Some(lb::MatrixRefMut::from(&mut [[21, 22]])));
assert_eq!(splits.next(), None);
}
{
let mut splits = mat.splits_mut(2);
assert_eq!(
splits.next(),
Some(lb::MatrixRefMut::from(&mut [
[00, 01, 02, 03], [04, 05, 06, 07], ]))
);
assert_eq!(
splits.next(),
Some(lb::MatrixRefMut::from(&mut [
[08, 09, 10, 11], [12, 13, 14, 15], ]))
);
assert_eq!(
splits.next(),
Some(lb::MatrixRefMut::from(&mut [
[16, 17, 18, 19], [20, 21, 22, 23], ]))
);
assert_eq!(splits.next(), None);
}
{
let mut splits = mat.splits_mut(4);
assert_eq!(
splits.next(),
Some(lb::MatrixRefMut::from(&mut [
[00, 01, 02, 03], [04, 05, 06, 07], [08, 09, 10, 11], [12, 13, 14, 15], ]))
);
assert_eq!(
splits.next(),
Some(lb::MatrixRefMut::from(&mut [
[16, 17, 18, 19], [20, 21, 22, 23], ]))
);
assert_eq!(splits.next(), None);
}
let mut expected = mat.to_owned();
let mut splits = mat.splits_mut(7);
assert_eq!(splits.next(), Some(expected.as_mut()));
assert_eq!(splits.next(), None);
let mut mat = mat::MatrixVec::from([[0]]);
let mut splits = mat.splits_mut(0);
assert_eq!(splits.next(), None);
let mut mat = mat::MatrixVec::<i32>::from([[]]);
let mut splits = mat.splits_mut(1);
assert_eq!(splits.next(), None);
}
#[test]
fn windows_size_hint() {
let mat = lb::MatrixRef::from(&[
[00, 01, 02, 03, 04, 05], [06, 07, 08, 09, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], ]);
assert_eq!(mat.windows((0, 0).into()).size_hint(), (0, Some(0)));
assert_eq!(mat.windows((0, 1).into()).size_hint(), (0, Some(0)));
assert_eq!(mat.windows((1, 0).into()).size_hint(), (0, Some(0)));
assert_eq!(mat.windows((6, 4).into()).size_hint(), (1, Some(1)));
assert_eq!(mat.windows((5, 4).into()).size_hint(), (2, Some(2)));
assert_eq!(mat.windows((6, 3).into()).size_hint(), (2, Some(2)));
assert_eq!(mat.windows((2, 3).into()).size_hint(), (10, Some(10)));
let win = mat.window((1, 1), (4, 2).into()).unwrap();
let mut windows = win.windows((2, 2).into());
assert_eq!(windows.size_hint(), (3, Some(3)));
_ = windows.next();
assert_eq!(windows.size_hint(), (2, Some(2)));
_ = windows.next();
assert_eq!(windows.size_hint(), (1, Some(1)));
_ = windows.next();
assert_eq!(windows.size_hint(), (0, Some(0)));
_ = windows.next();
assert_eq!(windows.size_hint(), (0, Some(0)));
assert_eq!(windows.count(), 0);
}
#[test]
fn windows_next() {
let mat = lb::MatrixRef::from(&[
[00, 01, 02], [04, 05, 06], [08, 09, 10], ]);
{
let mut windows = mat.windows((3, 3).into());
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[00, 01, 02], [04, 05, 06], [08, 09, 10], ]))
);
assert_eq!(windows.next(), None);
}
{
assert_eq!(mat.windows((0, 0).into()).next(), None);
assert_eq!(mat.windows((0, 1).into()).next(), None);
assert_eq!(mat.windows((1, 0).into()).next(), None);
}
{
let mut windows = mat.windows((2, 2).into());
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[00, 01], [04, 05], ]))
);
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[01, 02], [05, 06], ]))
);
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[04, 05], [08, 09], ]))
);
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[05, 06], [09, 10], ]))
);
assert_eq!(windows.next(), None);
}
{
let arr: [[i32; 0]; 0] = [];
let mat = lb::MatrixRef::<i32>::from(&arr);
assert_eq!(mat.windows((0, 0).into()).next(), None);
assert_eq!(mat.windows((0, 1).into()).next(), None);
assert_eq!(mat.windows((1, 0).into()).next(), None);
}
{
let mat = lb::MatrixRef::<i32>::from(&[[]]);
assert_eq!(mat.windows((0, 0).into()).next(), None);
assert_eq!(mat.windows((0, 1).into()).next(), None);
assert_eq!(mat.windows((1, 0).into()).next(), None);
}
{
let mat = lb::MatrixRef::from(&[
[00, 00, 00, 00],
[00, 00, 01, 02],
[00, 03, 04, 05],
[00, 06, 07, 08],
[00, 09, 10, 11],
[00, 00, 00, 00],
]);
let win = mat.window((1, 1), (3, 4).into()).unwrap();
let mut windows = win.windows((2, 3).into());
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[00, 01], [03, 04], [06, 07], ]))
);
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[01, 02], [04, 05], [07, 08], ]))
);
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[03, 04], [06, 07], [09, 10], ]))
);
assert_eq!(
windows.next(),
Some(lb::MatrixRef::from(&[
[04, 05], [07, 08], [10, 11], ]))
);
assert_eq!(windows.next(), None);
}
}