diff --git a/src/memfile/hp.rs b/src/memfile/hp.rs index f10ab6f..a877c29 100644 --- a/src/memfile/hp.rs +++ b/src/memfile/hp.rs @@ -248,7 +248,24 @@ impl ops::BitOrAssign for Mask } } -//TODO: add test for `Mask::new_checked()` above, and `.memfd_create_wrapper{,_flags}()` usage, too with some `MAP_HUGE_` constants as sizes +impl PartialEq for Mask +{ + #[inline] + fn eq(&self, &other: &c_uint) -> bool + { + self.mask() == other + } +} +impl PartialEq for Mask +{ + #[inline] + fn eq(&self, &other: &c_int) -> bool + { + self.raw() == other + } +} + +//TODO: add test `.memfd_create_wrapper{,_flags}()` usage, too with some `MAP_HUGE_` constants as sizes /// Take a directory path and try to parse the hugepage size from it. /// @@ -395,6 +412,24 @@ mod tests libc::MAP_HUGE_2MB, ]; + #[inline] + fn find_constants_from<'a, I, M>(masks: I) -> impl Iterator + 'a + where I: IntoIterator + 'a, + M: PartialEq + 'a + { + #[inline] + fn slow_contains(m: &impl PartialEq) -> Option + { + for c in CONSTANTS { + if m == c { + return Some(*c); + } + } + None + } + masks.into_iter().filter_map(|mask| slow_contains(&mask)) + } + #[inline] fn find_constants_in(path: impl AsRef, checked: bool) -> eyre::Result { @@ -444,6 +479,30 @@ mod tests } } - //TODO: test `get_masks()` + #[test] + fn get_masks_matching_constants() -> eyre::Result<()> + { + let masks: usize = find_constants_from(super::get_masks()? + .inspect(|mask| match mask { + Ok(mask) => eprintln!(" -> mask {mask:x} ({mask:b})"), + Err(e) => eprintln!(" ! failed extraction: {e}") + }).filter_map(Result::ok)) + .count(); + + (masks > 0).then(|| drop(println!("Found {masks} masks matching pre-set constants"))).ok_or(eyre!("Found no masks matching constants")) + } + + #[test] + fn get_masks() -> eyre::Result<()> + { + let masks: usize = super::get_masks()? + .inspect(|mask| match mask { + Ok(mask) => eprintln!(" -> mask {mask:x} ({mask:b})"), + Err(e) => eprintln!(" ! failed extraction: {e}") + }) + .count(); + + (masks > 0).then(|| drop(println!("Found {masks} masks on system"))).ok_or(eyre!("Found no masks")) + } } }