Skip to content

Commit

Permalink
Add more tests for the line reader.
Browse files Browse the repository at this point in the history
  • Loading branch information
panhania committed Nov 2, 2024
1 parent e9d0146 commit 7c07059
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/rrg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ version = "0.8.5"
[dev-dependencies.tempfile]
version = "3.3.0"

[dev-dependencies.quickcheck]
version = "1.0.3"

[target.'cfg(target_family = "windows")'.dev-dependencies.windows-sys]
version = "0.45.0"
features = [
Expand Down
57 changes: 57 additions & 0 deletions crates/rrg/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ mod tests {

use super::*;

use quickcheck::quickcheck;

#[test]
fn test_copy_until_with_empty_buffer() {
let mut reader: &[u8] = b"";
Expand Down Expand Up @@ -464,6 +466,61 @@ mod tests {
assert_eq!(line, "prefixcontent");
}

#[test]
fn line_reader_empty_lines() {
let mut reader = LineReader::new("\n\nfoo\n\n".as_bytes());
let mut line = String::new();

line.clear();
assert_eq!(reader.read_line_lossy(&mut line).unwrap(), 1);
assert_eq!(line, "\n");

line.clear();
assert_eq!(reader.read_line_lossy(&mut line).unwrap(), 1);
assert_eq!(line, "\n");

line.clear();
assert_eq!(reader.read_line_lossy(&mut line).unwrap(), 4);
assert_eq!(line, "foo\n");

line.clear();
assert_eq!(reader.read_line_lossy(&mut line).unwrap(), 1);
assert_eq!(line, "\n");

line.clear();
assert_eq!(reader.read_line_lossy(&mut line).unwrap(), 0);
assert_eq!(line, "");
}

quickcheck! {

fn line_reader_joined_lines(strings: Vec<String>) -> quickcheck::TestResult {
// This property holds only for strings without line feed chars as
// otherwise an input string can get an extra split when reading.
if strings.iter().any(|string| string.contains('\n')) {
return quickcheck::TestResult::discard();
}

let mut content = strings.join("\n");
content.push('\n');

let mut reader = LineReader::new(content.as_bytes());
let mut line = String::new();

for string in &strings {
line.clear();
if reader.read_line_lossy(&mut line).unwrap() != string.len() + 1 {
return quickcheck::TestResult::failed();
}
if line != format!("{string}\n") {
return quickcheck::TestResult::failed();
}
}

quickcheck::TestResult::passed()
}
}

#[test]
fn test_iter_reader_with_empty_iter() {
let mut reader = IterReader::new(std::iter::empty::<&[u8]>());
Expand Down

0 comments on commit 7c07059

Please sign in to comment.