From 84b7d1ac8b40a477e75a7e50ad1608ea9434ccb3 Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:10:04 +0000 Subject: [PATCH] test(index): add unit tests to `oxc_index` (#5979) Trying to improve `oxc_index`'s code coverage. --- crates/oxc_index/src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/crates/oxc_index/src/lib.rs b/crates/oxc_index/src/lib.rs index 36025b919..c075c08c6 100644 --- a/crates/oxc_index/src/lib.rs +++ b/crates/oxc_index/src/lib.rs @@ -795,3 +795,48 @@ impl<'de, I: Idx, T: serde::de::Deserialize<'de>> serde::de::Deserialize<'de> fo Box::<[T]>::deserialize(deserializer).map(Into::into) } } + +#[cfg(test)] +#[allow(clippy::legacy_numeric_constants)] +mod test { + use super::*; + + define_index_type! { + pub struct TestIdx = u32; + } + + #[test] + fn test_resize() { + let mut v = IndexVec::::with_capacity(10); + assert_eq!(v.len(), 0); + assert!(v.is_empty()); + + v.push(1); + assert_eq!(v.len(), 1); + + v.resize(5, 1); + assert_eq!(v.len(), 5); + assert_eq!(v.as_slice(), &[1, 1, 1, 1, 1]); + + v.shrink_to_fit(); + assert_eq!(v.len(), 5); + } + + #[test] + fn test_push_pop() { + let mut v = IndexVec::::new(); + v.push(1); + assert_eq!(v.pop(), Some(1)); + } + + #[test] + fn test_clear() { + let mut v: IndexVec = [1, 2, 3].into_iter().collect(); + assert_eq!(v.len(), 3); + + v.clear(); + assert_eq!(v.len(), 0); + assert_eq!(v.as_slice(), &[]); + assert_eq!(v, IndexVec::::new()); + } +}