flense 0.4.1

Purpose-oriented lensing
Documentation
//! Tests for [`LensesSlice`] and [`LensesSliceMut`] impls on
//! `Box<[T]>`, `Rc<[T]>`, `Arc<[T]>`, and `Vec<T>`.

#![cfg(feature = "alloc")]
#![allow(clippy::float_cmp)]

extern crate alloc;

use alloc::{
    boxed::Box,
    rc::Rc,
    sync::Arc,
    vec::Vec,
};

use assert2::assert;
use flense::prelude::*;

use super::*;

#[test]
fn lens_slice_box() {
    let v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xAA,
    };
    let boxed: Box<[Vertex]> = Box::new([v]);
    let lens: LensSlice<'_, (Position, Color)> = boxed.lens_slice();
    assert!(*lens.get::<Position, _>(0).unwrap() == v.position);
    assert!(*lens.get::<Color, _>(0).unwrap() == v.color);
}

#[test]
fn lens_slice_rc() {
    let v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xAA,
    };
    let rc: Rc<[Vertex]> = Rc::new([v]);
    let lens: LensSlice<'_, (Position, Color)> = rc.lens_slice();
    assert!(*lens.get::<Position, _>(0).unwrap() == v.position);
    assert!(*lens.get::<Color, _>(0).unwrap() == v.color);
}

#[test]
fn lens_slice_arc() {
    let v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xAA,
    };
    let arc: Arc<[Vertex]> = Arc::new([v]);
    let lens: LensSlice<'_, (Position, Color)> = arc.lens_slice();
    assert!(*lens.get::<Position, _>(0).unwrap() == v.position);
    assert!(*lens.get::<Color, _>(0).unwrap() == v.color);
}

#[test]
fn lens_slice_vec() {
    let verts: Vec<Vertex> = vec![
        Vertex {
            position: [1.0, 2.0, 3.0],
            color: [4.0, 5.0, 6.0],
            normal: [7, 8],
            other: 0xAA,
        },
        Vertex {
            position: [10.0, 20.0, 30.0],
            color: [40.0, 50.0, 60.0],
            normal: [70, 80],
            other: 0xBB,
        },
    ];
    let lens: LensSlice<'_, (Position, Color)> = verts.lens_slice();
    for (i, v) in verts.iter().enumerate() {
        assert!(*lens.get::<Position, _>(i).unwrap() == v.position);
        assert!(*lens.get::<Color, _>(i).unwrap() == v.color);
    }
}

#[test]
fn lens_slice_box_split() {
    let v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xAA,
    };
    let boxed: Box<[Vertex]> = Box::new([v]);
    let lens: LensSlice<'_, (Normal, Other, Color, Position)> = boxed.lens_slice();
    let (lhs, rhs) = lens.split::<(Position,), _>();

    assert!(*lhs.get::<Position, _>(0).unwrap() == v.position);
    assert!(*rhs.get::<Color, _>(0).unwrap() == v.color);
    assert!(*rhs.get::<Normal, _>(0).unwrap() == v.normal);
    assert!(*rhs.get::<Other, _>(0).unwrap() == v.other);
}

#[test]
fn lens_slice_vec_empty() {
    let verts: Vec<Vertex> = Vec::new();
    let lens: LensSlice<'_, (Position,)> = verts.lens_slice();
    assert!(lens.is_empty());
    assert!(lens.get::<Position, _>(0).is_none());
}

#[test]
fn lens_slice_mut_box() {
    let v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xAA,
    };
    let mut boxed: Box<[Vertex]> = Box::new([v]);
    let lens: LensSliceMut<'_, (Position, Color)> = boxed.lens_slice_mut();
    assert!(*lens.get::<Position, _>(0).unwrap() == v.position);
    assert!(*lens.get::<Color, _>(0).unwrap() == v.color);
}

#[test]
fn lens_slice_mut_vec() {
    let mut verts: Vec<Vertex> = vec![
        Vertex {
            position: [1.0, 2.0, 3.0],
            color: [4.0, 5.0, 6.0],
            normal: [7, 8],
            other: 0xAA,
        },
        Vertex {
            position: [10.0, 20.0, 30.0],
            color: [40.0, 50.0, 60.0],
            normal: [70, 80],
            other: 0xBB,
        },
    ];
    let expected = verts.clone();
    let lens: LensSliceMut<'_, (Position, Color)> = verts.as_mut_slice().lens_slice_mut();
    for (i, v) in expected.iter().enumerate() {
        assert!(*lens.get::<Position, _>(i).unwrap() == v.position);
        assert!(*lens.get::<Color, _>(i).unwrap() == v.color);
    }
}

#[test]
fn lens_slice_mut_box_split() {
    let v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xAA,
    };
    let mut boxed: Box<[Vertex]> = Box::new([v]);
    let lens: LensSliceMut<'_, (Normal, Other, Color, Position)> = boxed.lens_slice_mut();
    let (lhs, rhs) = lens.split::<(Position,), _>();

    assert!(*lhs.get::<Position, _>(0).unwrap() == v.position);
    assert!(*rhs.get::<Color, _>(0).unwrap() == v.color);
    assert!(*rhs.get::<Normal, _>(0).unwrap() == v.normal);
    assert!(*rhs.get::<Other, _>(0).unwrap() == v.other);
}

#[test]
fn lens_slice_mut_vec_empty() {
    let mut verts: Vec<Vertex> = Vec::new();
    let lens: LensSliceMut<'_, (Position,)> = verts.as_mut_slice().lens_slice_mut();
    assert!(lens.is_empty());
    assert!(lens.get::<Position, _>(0).is_none());
}