Skip to content

Commit

Permalink
Add util.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Apr 14, 2021
1 parent 6b9631e commit 6b0a77e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod table_collection;
mod table_iterator;
mod trees;
pub mod types;
mod util;

// re-export fundamental constants that
// we can't live without
Expand Down
58 changes: 58 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
pub(crate) fn metadata_like_are_equal(a: &Option<Vec<u8>>, b: &Option<Vec<u8>>) -> bool {
match a {
Some(x) => match b {
Some(y) => x == y,
None => false,
},
None => match b.is_none() {
true => true,
false => false,
},
}
}

pub(crate) fn f64_partial_cmp_equal(a: &f64, b: &f64) -> bool {
match a.partial_cmp(b) {
Some(std::cmp::Ordering::Equal) => true,
Some(std::cmp::Ordering::Less) => false,
Some(std::cmp::Ordering::Greater) => false,
None => false,
}
}

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

#[test]
fn compare_some_to_none() {
let v: Vec<u8> = vec![1, 2, 3];
assert!(!metadata_like_are_equal(&Some(v), &None));
}

#[test]
fn compare_none_to_some() {
let v: Vec<u8> = vec![1, 2, 3];
assert!(!metadata_like_are_equal(&None, &Some(v)));
}

#[test]
fn compare_none_to_none() {
assert!(metadata_like_are_equal(&None, &None));
}

#[test]
fn compare_some_to_some_are_equal() {
let v: Vec<u8> = vec![1, 2, 3];
let vc = v.clone();
assert!(metadata_like_are_equal(&Some(v), &Some(vc)));
}

#[test]
fn compare_some_to_some_are_not_equal() {
let v: Vec<u8> = vec![1, 2, 3];
let mut vc = v.clone();
vc.push(11);
assert!(!metadata_like_are_equal(&Some(v), &Some(vc)));
}
}

0 comments on commit 6b0a77e

Please sign in to comment.