Skip to content

Commit

Permalink
csv: fix equality check for raw records
Browse files Browse the repository at this point in the history
This commit fixes the equality check for the StringRecord and
ByteRecord types such that the field boundaries are respected.
That is, [123, 4] should not be equal to [1, 234], but these
previously were treated as equivalent.

Fixes #138
  • Loading branch information
aedoq authored and BurntSushi committed Dec 16, 2018
1 parent 3518426 commit efc4a51
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/byte_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct ByteRecord(Box<ByteRecordInner>);

impl PartialEq for ByteRecord {
fn eq(&self, other: &ByteRecord) -> bool {
self.as_slice() == other.as_slice()
self.iter().zip(other.iter()).all(|e| e.0 == e.1)

This comment has been minimized.

Copy link
@niklasf

niklasf Dec 16, 2018

Is there a problem when the iterators are not equally long?

This comment has been minimized.

Copy link
@BurntSushi

BurntSushi Dec 16, 2018

Owner

Yup, great catch! Thanks for noticing! I've pushed a fix to crates.io in csv 1.0.5.

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/string_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct StringRecord(ByteRecord);

impl PartialEq for StringRecord {
fn eq(&self, other: &StringRecord) -> bool {
self.as_slice() == other.as_slice()
self.iter().zip(other.iter()).all(|e| e.0 == e.1)
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#![allow(dead_code)]

extern crate csv;

use csv::ByteRecord;
use csv::StringRecord;

use std::env;
use std::io::{self, Write};
use std::path::PathBuf;
Expand Down Expand Up @@ -333,6 +338,24 @@ fn tutorial_perf_core_01() {
assert_eq!(out.stdout(), "11\n");
}

///Check for correctness of ```impl PartialEq for ByteRecord``` (Issue #138)
#[test]
fn byte_record_eq_field_boundaries() {
let test1 = ByteRecord::from(vec!["12","34"]);
let test2 = ByteRecord::from(vec!["123","4"]);

assert_ne!(test1, test2);
}

///Check for correctness of ```impl PartialEq for StringRecord``` (Issue #138)
#[test]
fn string_record_eq_field_boundaries() {
let test1 = StringRecord::from(vec!["12","34"]);
let test2 = StringRecord::from(vec!["123","4"]);

assert_ne!(test1, test2);
}

// Helper functions follow.

/// Return the target/debug directory path.
Expand Down

0 comments on commit efc4a51

Please sign in to comment.