Add tests for PartialOrd for Listing

This commit is contained in:
Nathan Fisher 2023-12-29 00:31:49 -05:00
parent 9531c0268a
commit 3c708430ae

View File

@ -1,8 +1,10 @@
use std::fmt;
use { use {
crate::{filetype::Flag, Error, Special}, crate::{filetype::Flag, Error, Special},
std::io::{Read, Seek, SeekFrom}, std::{
cmp::Ordering,
fmt,
io::{Read, Seek, SeekFrom},
},
}; };
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -29,24 +31,17 @@ impl Kind {
reader.read_exact(&mut buf)?; reader.read_exact(&mut buf)?;
match buf[0] { match buf[0] {
0 => { 0 => {
let bytes = reader.seek(SeekFrom::Current(len as i64 + 16))?; let _bytes = reader.seek(SeekFrom::Current(len as i64 + 16))?;
if bytes - 16 != len {
return Err(Error::MissingData);
}
} }
1 => { 1 => {
let bytes = reader.seek(SeekFrom::Current(len as i64 + 20))?; let _bytes = reader.seek(SeekFrom::Current(len as i64 + 20))?;
if bytes - 20 != len {
return Err(Error::MissingData);
}
} }
2 => { 2 => {
let bytes = reader.seek(SeekFrom::Current(len as i64 + 32))?; let _bytes = reader.seek(SeekFrom::Current(len as i64 + 32))?;
if bytes - 32 != len {
return Err(Error::MissingData);
} }
_ => {
let _bytes = reader.seek(SeekFrom::Current(len as i64))?;
} }
_ => {}
} }
Ok(Self::Normal(len)) Ok(Self::Normal(len))
} }
@ -96,15 +91,12 @@ pub struct Listing {
} }
impl PartialOrd for Listing { impl PartialOrd for Listing {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if let (Kind::Directory, Kind::Directory) = (&self.kind, &other.kind) { match (&self.kind, &other.kind) {
self.name.partial_cmp(&other.name) (Kind::Directory, Kind::Directory) => self.name.partial_cmp(&other.name),
} else if let Kind::Directory = self.kind { (Kind::Directory, _) => Some(Ordering::Less),
Some(std::cmp::Ordering::Greater) (_, Kind::Directory) => Some(Ordering::Greater),
} else if let Kind::Directory = other.kind { _ => self.name.partial_cmp(&other.name),
Some(std::cmp::Ordering::Less)
} else {
self.name.partial_cmp(&other.name)
} }
} }
} }
@ -224,3 +216,39 @@ impl Listing {
}) })
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::{Algorithm, Node};
use std::{
collections::HashMap,
fs,
io::{BufReader, BufWriter, Write},
sync::Mutex,
};
#[test]
fn ord() {
let links = Mutex::new(HashMap::new());
{
let mut node = Node::from_path("test", Algorithm::Skip, &links).unwrap();
let fd = fs::File::create("test/ord_test.hag").unwrap();
let mut writer = BufWriter::new(fd);
node.write(&mut writer).unwrap();
node = Node::from_path("Cargo.toml", Algorithm::Skip, &links).unwrap();
node.write(&mut writer).unwrap();
node = Node::from_path("Cargo.lock", Algorithm::Skip, &links).unwrap();
node.write(&mut writer).unwrap();
writer.flush().unwrap();
}
let fd = fs::File::open("test/ord_test.hag").unwrap();
let mut reader = BufReader::new(fd);
let test_listing = Listing::read(&mut reader).unwrap();
let cargo_toml_listing = Listing::read(&mut reader).unwrap();
assert!(test_listing < cargo_toml_listing);
let cargo_lock_listing = Listing::read(&mut reader).unwrap();
assert!(test_listing < cargo_lock_listing);
assert!(cargo_lock_listing < cargo_toml_listing);
}
}