Skip to content

Commit

Permalink
use PartialEq only on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Sep 19, 2023
1 parent 237952a commit 45e3b05
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 43 deletions.
3 changes: 2 additions & 1 deletion crates/storage/nippy-jar/src/compression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub trait Compression: Serialize + for<'a> Deserialize<'a> {
}
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Compressors {
Zstd(Zstd),
// Avoids irrefutable let errors. Remove this after adding another one.
Expand Down
19 changes: 6 additions & 13 deletions crates/storage/nippy-jar/src/filter/cuckoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,14 @@ impl std::fmt::Debug for Cuckoo {
}
}

#[cfg(test)]
impl PartialEq for Cuckoo {
fn eq(&self, _other: &Self) -> bool {
self.remaining == _other.remaining &&
{
#[cfg(not(test))]
{
unimplemented!("No way to figure it out without exporting (expensive), so only allow direct comparison on a test")
}
#[cfg(test)]
{
let f1 = self.filter.export();
let f2 = _other.filter.export();
return f1.length == f2.length && f1.values == f2.values
}
}
self.remaining == _other.remaining && {
let f1 = self.filter.export();
let f2 = _other.filter.export();
f1.length == f2.length && f1.values == f2.values
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/storage/nippy-jar/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub trait Filter {
fn contains(&self, element: &[u8]) -> Result<bool, NippyJarError>;
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Filters {
Cuckoo(Cuckoo),
// Avoids irrefutable let errors. Remove this after adding another one.
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/nippy-jar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use phf::{Fmph, Functions, GoFmph, KeySet};

const NIPPY_JAR_VERSION: usize = 1;

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct NippyJar {
/// Version
version: usize,
Expand Down
22 changes: 8 additions & 14 deletions crates/storage/nippy-jar/src/phf/fmph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,21 @@ impl KeySet for Fmph {
}
}

#[cfg(test)]
impl PartialEq for Fmph {
fn eq(&self, other: &Self) -> bool {
match (&self.function, &other.function) {
fn eq(&self, _other: &Self) -> bool {
match (&self.function, &_other.function) {
(Some(func1), Some(func2)) => {
func1.level_sizes() == func2.level_sizes() &&
func1.write_bytes() == func2.write_bytes() &&
{
#[cfg(not(test))]
{
unimplemented!("No way to figure it out without exporting (potentially expensive), so only allow direct comparison on a test")
}
#[cfg(test)]
{
let mut f1 = Vec::with_capacity(func1.write_bytes());
func1.write(&mut f1).expect("enough capacity");
let mut f1 = Vec::with_capacity(func1.write_bytes());
func1.write(&mut f1).expect("enough capacity");

let mut f2 = Vec::with_capacity(func2.write_bytes());
func2.write(&mut f2).expect("enough capacity");
let mut f2 = Vec::with_capacity(func2.write_bytes());
func2.write(&mut f2).expect("enough capacity");

return f1 == f2
}
f1 == f2
}
}
(None, None) => true,
Expand Down
18 changes: 6 additions & 12 deletions crates/storage/nippy-jar/src/phf/go_fmph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,21 @@ impl KeySet for GoFmph {
}
}

#[cfg(test)]
impl PartialEq for GoFmph {
fn eq(&self, other: &Self) -> bool {
match (&self.function, &other.function) {
(Some(func1), Some(func2)) => {
func1.level_sizes() == func2.level_sizes() &&
func1.write_bytes() == func2.write_bytes() &&
{
#[cfg(not(test))]
{
unimplemented!("No way to figure it out without exporting ( potentially expensive), so only allow direct comparison on a test")
}
#[cfg(test)]
{
let mut f1 = Vec::with_capacity(func1.write_bytes());
func1.write(&mut f1).expect("enough capacity");
let mut f1 = Vec::with_capacity(func1.write_bytes());
func1.write(&mut f1).expect("enough capacity");

let mut f2 = Vec::with_capacity(func2.write_bytes());
func2.write(&mut f2).expect("enough capacity");
let mut f2 = Vec::with_capacity(func2.write_bytes());
func2.write(&mut f2).expect("enough capacity");

return f1 == f2
}
f1 == f2
}
}
(None, None) => true,
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/nippy-jar/src/phf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub trait KeySet: Serialize + for<'a> Deserialize<'a> {
}

/// Enumerates all types of perfect hashing functions.
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Functions {
Fmph(Fmph),
GoFmph(GoFmph),
Expand Down

0 comments on commit 45e3b05

Please sign in to comment.