Skip to content

Commit

Permalink
refactor: move all attribute tests to a dedicated module (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
imrn99 authored May 6, 2024
1 parent a32d2bb commit 6aea900
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 297 deletions.
230 changes: 0 additions & 230 deletions honeycomb-core/src/attributes/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,233 +431,3 @@ impl<T: AttributeBind + AttributeUpdate + Clone> AttrCompactVec<T> {
+ self.data.len() * std::mem::size_of::<T>()
}
}

// ------ TESTS

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

macro_rules! generate_sparse {
($name: ident) => {
let mut $name = AttrSparseVec::<Temperature>::new(10);
$name.insert(&0, Temperature::from(273.0));
$name.insert(&1, Temperature::from(275.0));
$name.insert(&2, Temperature::from(277.0));
$name.insert(&3, Temperature::from(279.0));
$name.insert(&4, Temperature::from(281.0));
$name.insert(&5, Temperature::from(283.0));
$name.insert(&6, Temperature::from(285.0));
$name.insert(&7, Temperature::from(287.0));
$name.insert(&8, Temperature::from(289.0));
$name.insert(&9, Temperature::from(291.0));
};
}

#[test]
fn sparse_vec_n_attributes() {
generate_sparse!(storage);
assert_eq!(storage.n_attributes(), 10);
let _ = storage.remove(&3);
assert_eq!(storage.n_attributes(), 9);
// extend does not affect the number of attributes
storage.extend(10);
assert!(storage.get(&15).is_none());
assert_eq!(storage.n_attributes(), 9);
}

#[test]
fn sparse_vec_get_set_get() {
generate_sparse!(storage);
assert_eq!(storage.get(&3), &Some(Temperature::from(279.0)));
storage.set(&3, Temperature::from(280.0));
assert_eq!(storage.get(&3), &Some(Temperature::from(280.0)));
}

#[test]
fn sparse_vec_get_replace_get() {
generate_sparse!(storage);
assert_eq!(storage.get(&3), &Some(Temperature::from(279.0)));
storage.replace(&3, Temperature::from(280.0));
assert_eq!(storage.get(&3), &Some(Temperature::from(280.0)));
}

#[test]
#[should_panic(expected = "assertion failed: tmp.is_none()")]
fn sparse_vec_insert_already_existing() {
generate_sparse!(storage);
assert_eq!(storage.get(&3), &Some(Temperature::from(279.0)));
storage.insert(&3, Temperature::from(280.0)); // panic
}

#[test]
fn sparse_vec_remove() {
generate_sparse!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
}

#[test]
fn sparse_vec_remove_remove() {
generate_sparse!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
assert!(storage.remove(&3).is_none());
}

#[test]
fn sparse_vec_remove_get() {
generate_sparse!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
assert!(storage.get(&3).is_none());
}

#[test]
fn sparse_vec_remove_set() {
generate_sparse!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
storage.set(&3, Temperature::from(280.0));
assert!(storage.get(&3).is_some());
}

#[test]
fn sparse_vec_remove_insert() {
generate_sparse!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
storage.insert(&3, Temperature::from(280.0));
assert!(storage.get(&3).is_some());
}

#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn sparse_vec_replace_already_removed() {
generate_sparse!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
storage.replace(&3, Temperature::from(280.0)).unwrap(); // panic
}

macro_rules! generate_compact {
($name: ident) => {
let mut $name = AttrCompactVec::<Temperature>::new(10);
$name.insert(&0, Temperature::from(273.0));
$name.insert(&1, Temperature::from(275.0));
$name.insert(&2, Temperature::from(277.0));
$name.insert(&3, Temperature::from(279.0));
$name.insert(&4, Temperature::from(281.0));
$name.insert(&5, Temperature::from(283.0));
$name.insert(&6, Temperature::from(285.0));
$name.insert(&7, Temperature::from(287.0));
$name.insert(&8, Temperature::from(289.0));
$name.insert(&9, Temperature::from(291.0));
};
}

#[test]
fn compact_vec_n_attributes() {
generate_compact!(storage);
assert_eq!(storage.n_attributes(), 10);
let _ = storage.remove(&3);
assert_eq!(storage.n_attributes(), 10);
// extend does not affect the number of attributes
storage.extend(10);
assert!(storage.get(&15).is_none());
assert_eq!(storage.n_attributes(), 10);
}

#[test]
fn compact_vec_n_used_attributes() {
generate_compact!(storage);
assert_eq!(storage.n_used_attributes(), 10);
let _ = storage.remove(&3);
assert_eq!(storage.n_used_attributes(), 9);
// extend does not affect the number of attributes
storage.extend(10);
assert!(storage.get(&15).is_none());
assert_eq!(storage.n_used_attributes(), 9);
}

#[test]
fn compact_vec_extend_through_set() {
generate_compact!(storage);
assert_eq!(storage.n_attributes(), 10);
// extend does not affect the number of attributes
storage.extend(10);
assert_eq!(storage.n_attributes(), 10);
storage.set(&10, Temperature::from(293.0));
assert_eq!(storage.n_attributes(), 11);
storage.set(&11, Temperature::from(295.0));
assert_eq!(storage.n_attributes(), 12);
storage.set(&12, Temperature::from(297.0));
assert_eq!(storage.n_attributes(), 13);
let _ = storage.remove(&3);
assert_eq!(storage.n_attributes(), 13);
assert_eq!(storage.n_used_attributes(), 12);
}

#[test]
fn compact_vec_get_set_get() {
generate_compact!(storage);
assert_eq!(storage.get(&3), Some(&Temperature::from(279.0)));
storage.set(&3, Temperature::from(280.0));
assert_eq!(storage.get(&3), Some(&Temperature::from(280.0)));
}

#[test]
fn compact_vec_get_replace_get() {
generate_compact!(storage);
assert_eq!(storage.get(&3), Some(&Temperature::from(279.0)));
storage.replace(&3, Temperature::from(280.0));
assert_eq!(storage.get(&3), Some(&Temperature::from(280.0)));
}

#[test]
#[should_panic(expected = "assertion failed: idx.is_none()")]
fn compact_vec_insert_already_existing() {
generate_compact!(storage);
assert_eq!(storage.get(&3), Some(&Temperature::from(279.0)));
storage.insert(&3, Temperature::from(280.0)); // panic
}

#[test]
fn compact_vec_remove() {
generate_compact!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
}

#[test]
fn compact_vec_remove_remove() {
generate_compact!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
assert!(storage.remove(&3).is_none());
}

#[test]
fn compact_vec_remove_get() {
generate_compact!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
assert!(storage.get(&3).is_none());
}

#[test]
fn compact_vec_remove_set() {
generate_compact!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
storage.set(&3, Temperature::from(280.0));
assert!(storage.get(&3).is_some());
}

#[test]
fn compact_vec_remove_insert() {
generate_compact!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
storage.insert(&3, Temperature::from(280.0));
assert!(storage.get(&3).is_some());
}

#[test]
#[should_panic(expected = "assertion failed: idx.is_some()")]
fn compact_vec_replace_already_removed() {
generate_compact!(storage);
assert_eq!(storage.remove(&3), Some(Temperature::from(279.0)));
storage.replace(&3, Temperature::from(280.0)); // panic
}
}
38 changes: 1 addition & 37 deletions honeycomb-core/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,5 @@ pub mod collections;
pub mod traits;

// ------ TESTS

#[cfg(test)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Temperature {
pub val: f32,
}

#[cfg(test)]
impl crate::AttributeUpdate for Temperature {
fn merge(attr1: Self, attr2: Self) -> Self {
Temperature {
val: (attr1.val + attr2.val) / 2.0,
}
}

fn split(attr: Self) -> (Self, Self) {
(attr, attr)
}

fn merge_undefined(attr: Option<Self>) -> Self {
attr.unwrap_or(Temperature { val: 0.0 })
}
}

#[cfg(test)]
impl crate::AttributeBind for Temperature {
type IdentifierType = crate::FaceIdentifier;
fn binds_to<'a>() -> crate::OrbitPolicy<'a> {
crate::OrbitPolicy::Face
}
}

#[cfg(test)]
impl From<f32> for Temperature {
fn from(val: f32) -> Self {
Self { val }
}
}
mod tests;
Loading

0 comments on commit 6aea900

Please sign in to comment.