Skip to content

Commit

Permalink
Tests \o/
Browse files Browse the repository at this point in the history
Which fail /o\
  • Loading branch information
Freaky committed Apr 20, 2018
1 parent 1fa5267 commit 84913f4
Showing 1 changed file with 54 additions and 10 deletions.
64 changes: 54 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ impl<R: io::Read> LineReader<R> {
/// 1 MiB and delimiter of `\n`.
///
/// ```no_run
/// # use std::fs::file;
/// let reader = LineReader::new(File::new("myfile.txt")?);
/// # use linereader::LineReader;
/// # use std::fs::File;
/// # use std::io;
/// # fn x() -> io::Result<()> {
/// let reader = LineReader::new(File::open("myfile.txt").unwrap());
/// # Ok(())
/// # }
/// ```
pub fn new(inner: R) -> Self {
Self::with_delimiter_and_capacity(NEWLINE, DEFAULT_CAPACITY, inner)
Expand All @@ -52,8 +57,13 @@ impl<R: io::Read> LineReader<R> {
/// delimiter of `\n`.
///
/// ```no_run
/// # use std::fs::file;
/// let reader = LineReader::with_capacity(1024*64, File::new("myfile.txt")?);
/// # use linereader::LineReader;
/// # use std::fs::File;
/// # use std::io;
/// # fn x() -> io::Result<()> {
/// let mut reader = LineReader::with_capacity(1024*64, File::open("myfile.txt").unwrap());
/// # Ok(())
/// # }
/// ```
pub fn with_capacity(capacity: usize, inner: R) -> Self {
Self::with_delimiter_and_capacity(NEWLINE, capacity, inner)
Expand All @@ -63,8 +73,13 @@ impl<R: io::Read> LineReader<R> {
/// 1 MiB and the given delimiter.
///
/// ```no_run
/// # use std::fs::file;
/// let reader = LineReader::with_delimiter(b'\t', File::new("myfile.txt")?);
/// # use linereader::LineReader;
/// # use std::fs::File;
/// # use std::io;
/// # fn x() -> io::Result<()> {
/// let mut reader = LineReader::with_delimiter(b'\t', File::open("myfile.txt").unwrap());
/// # Ok(())
/// # }
/// ```
pub fn with_delimiter(delimiter: u8, inner: R) -> Self {
Self::with_delimiter_and_capacity(delimiter, DEFAULT_CAPACITY, inner)
Expand All @@ -74,8 +89,13 @@ impl<R: io::Read> LineReader<R> {
/// delimiter.
///
/// ```no_run
/// # use std::fs::file;
/// let reader = LineReader::with_delimiter_and_capacity(b'\t', 1024*64, File::new("myfile.txt")?);
/// # use linereader::LineReader;
/// # use std::fs::File;
/// # use std::io;
/// # fn x() -> io::Result<()> {
/// let mut reader = LineReader::with_delimiter_and_capacity(b'\t', 1024*64, File::open("myfile.txt")?);
/// # Ok(())
/// # }
/// ```
pub fn with_delimiter_and_capacity(delimiter: u8, capacity: usize, inner: R) -> Self {
Self {
Expand All @@ -93,11 +113,16 @@ impl<R: io::Read> LineReader<R> {
/// truncated to the buffer size due to length.
///
/// ```no_run
/// # use std::fs::file;
/// # let reader = LineReader::new(File::new("myfile.txt")?);
/// # use linereader::LineReader;
/// # use std::fs::File;
/// # use std::io;
/// # fn x() -> io::Result<()> {
/// # let mut reader = LineReader::new(File::open("myfile.txt")?);
/// while let Some(line) = reader.next_line() {
/// let line = line?; // unwrap io::Result to &[u8]
/// }
/// # Ok(())
/// # }
/// ```
pub fn next_line(&mut self) -> Option<io::Result<&[u8]>> {
let end = cmp::min(self.end_of_complete, self.end_of_buffer);
Expand Down Expand Up @@ -190,3 +215,22 @@ impl<R: io::Read> LineReader<R> {
self.inner
}
}

#[cfg(test)]
mod tests {
use LineReader;

#[test]
fn test_next_line() {
let buf: &[u8] = b"0a0\n1bb1\n2ccc2\n3dddd3\n4eeeee4\n5ffffffff5\n6xxx6";
let mut reader = LineReader::with_capacity(8, buf);
assert_eq!(b"0a0\n", reader.next_line().unwrap().unwrap());
assert_eq!(b"1bb1\n", reader.next_line().unwrap().unwrap());
assert_eq!(b"2ccc2\n", reader.next_line().unwrap().unwrap());
assert_eq!(b"3dddd3\n", reader.next_line().unwrap().unwrap());
assert_eq!(b"4eeeee4\n", reader.next_line().unwrap().unwrap());
assert_eq!(b"5fffffff", reader.next_line().unwrap().unwrap());
assert_eq!(b"f5\n", reader.next_line().unwrap().unwrap());
assert_eq!(b"6xxx6", reader.next_line().unwrap().unwrap());
}
}

0 comments on commit 84913f4

Please sign in to comment.