Skip to content

Commit

Permalink
Add locked array unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elftausend committed Oct 22, 2024
1 parent 2ddcaad commit 9c94c26
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ min-cl = { git = "https://github.com/elftausend/min-cl", optional = true }
# min-cl = { version = "0.3.0", optional=true }

[features]
default = ["cpu", "blas", "static-api", "macro", "cached", "autograd", "cuda", "vulkan", "stack"]
default = ["cpu", "blas", "static-api", "macro", "cached", "autograd", "vulkan", "stack"]

# default = ["cpu"]
# default = ["no-std"]
Expand Down
59 changes: 55 additions & 4 deletions src/cache/locking/locked_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::cow_mut::CowMutCell;

use super::{Guard, LockInfo, State};

pub struct LockedArray2<T: Sized, const N: usize = 1000> {
pub struct LockedArray<T: Sized, const N: usize = 1000> {
data: [RefCell<Option<T>>; N],
}

impl<T, const N: usize> Default for LockedArray2<T, N> {
impl<T, const N: usize> Default for LockedArray<T, N> {
#[inline]
fn default() -> Self {
Self {
Expand All @@ -17,15 +17,16 @@ impl<T, const N: usize> Default for LockedArray2<T, N> {
}
}

impl<T, const N: usize> LockedArray2<T, N> {
impl<T, const N: usize> LockedArray<T, N> {
#[inline]
pub fn new() -> Self {
Self::default()
}
}

impl<T, const N: usize> LockedArray2<T, N> {
impl<T, const N: usize> LockedArray<T, N> {
pub fn set(&self, id: usize, data: T) {
// not required to check this
assert!(self.data[id].borrow().is_none());
*self.data[id].borrow_mut() = Some(data);
}
Expand All @@ -48,4 +49,54 @@ impl<T, const N: usize> LockedArray2<T, N> {

#[cfg(test)]
mod tests {
use super::LockedArray;

#[test]
fn test_set_and_get_multiple() {
let locked_array = LockedArray::<Vec<i32>>::new();
locked_array.set(0, vec![0, 0]);
locked_array.set(1, vec![1]);
locked_array.set(2, vec![2]);
locked_array.set(3, vec![3]);

let mut data0 = locked_array.get(0).unwrap();
assert_eq!(data0.as_slice(), [0, 0]);
data0[0] = 1;
assert_eq!(data0.as_slice(), [1, 0]);
let mut data1 = locked_array.get(1).unwrap();
assert_eq!(data1.as_slice(), [1]);
data1.push(2);
assert_eq!(data1.as_slice(), [1, 2]);
}

#[test]
#[should_panic]
fn test_set_same() {
let locked_array = LockedArray::<Vec<i32>>::new();
locked_array.set(1, vec![10]);
locked_array.set(1, vec![10]);
}

#[test]
fn test_get_not_set() {
let locked_array = LockedArray::<Vec<i32>>::new();
{
let _d = locked_array.get(1);
assert!(locked_array.get(1).is_err());
}
let _ = locked_array.get(1);
assert!(locked_array.get(1).is_err());
}

#[test]
fn test_get_same_multiple() {
let locked_array = LockedArray::<Vec<i32>>::new();
locked_array.set(1, vec![10]);
{
let _d = locked_array.get(1);
assert!(locked_array.get(1).is_err());
}
let _ = locked_array.get(1);
assert!(locked_array.get(1).is_ok());
}
}
2 changes: 1 addition & 1 deletion src/cow_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub type CowMutCell<'a, T> = CowMut<T, RefMut<'a, T>, Ref<'a, T>>;
pub type CowMutRef<'a, T> = CowMut<T, &'a T, &'a mut T>;

#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum CowMut<T, M, R> {
Borrowed(R),
BorrowedMut(M),
Expand Down

0 comments on commit 9c94c26

Please sign in to comment.