Add tests for PartialOrd for Listing
This commit is contained in:
parent
9531c0268a
commit
3c708430ae
@ -1,8 +1,10 @@
|
||||
use std::fmt;
|
||||
|
||||
use {
|
||||
crate::{filetype::Flag, Error, Special},
|
||||
std::io::{Read, Seek, SeekFrom},
|
||||
std::{
|
||||
cmp::Ordering,
|
||||
fmt,
|
||||
io::{Read, Seek, SeekFrom},
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@ -29,24 +31,17 @@ impl Kind {
|
||||
reader.read_exact(&mut buf)?;
|
||||
match buf[0] {
|
||||
0 => {
|
||||
let bytes = reader.seek(SeekFrom::Current(len as i64 + 16))?;
|
||||
if bytes - 16 != len {
|
||||
return Err(Error::MissingData);
|
||||
}
|
||||
let _bytes = reader.seek(SeekFrom::Current(len as i64 + 16))?;
|
||||
}
|
||||
1 => {
|
||||
let bytes = reader.seek(SeekFrom::Current(len as i64 + 20))?;
|
||||
if bytes - 20 != len {
|
||||
return Err(Error::MissingData);
|
||||
}
|
||||
let _bytes = reader.seek(SeekFrom::Current(len as i64 + 20))?;
|
||||
}
|
||||
2 => {
|
||||
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 + 32))?;
|
||||
}
|
||||
_ => {
|
||||
let _bytes = reader.seek(SeekFrom::Current(len as i64))?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(Self::Normal(len))
|
||||
}
|
||||
@ -96,15 +91,12 @@ pub struct Listing {
|
||||
}
|
||||
|
||||
impl PartialOrd for Listing {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
if let (Kind::Directory, Kind::Directory) = (&self.kind, &other.kind) {
|
||||
self.name.partial_cmp(&other.name)
|
||||
} else if let Kind::Directory = self.kind {
|
||||
Some(std::cmp::Ordering::Greater)
|
||||
} else if let Kind::Directory = other.kind {
|
||||
Some(std::cmp::Ordering::Less)
|
||||
} else {
|
||||
self.name.partial_cmp(&other.name)
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
match (&self.kind, &other.kind) {
|
||||
(Kind::Directory, Kind::Directory) => self.name.partial_cmp(&other.name),
|
||||
(Kind::Directory, _) => Some(Ordering::Less),
|
||||
(_, Kind::Directory) => Some(Ordering::Greater),
|
||||
_ => 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user