From 3c708430aef2ce68d60b7aefbe6c6f182a011637 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Fri, 29 Dec 2023 00:31:49 -0500 Subject: [PATCH] Add tests for PartialOrd for Listing --- src/listing.rs | 78 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/src/listing.rs b/src/listing.rs index a29483e..090f0d4 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -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 { - 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 { + 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); + } +}