use std::fs::File; use std::io::Cursor; use std::path::Path; use buffed::{BuffedRead, BuffedReader, Error, FromBytes}; #[test] fn empty_input() { const CAP: usize = 8; let mut r = BuffedReader::with_capacity(Cursor::new(""), CAP).unwrap(); let b: &str = r.data(); assert_eq!( "", b, "`BuffedRead::buffer()` just after reader initialization must return an empty slice" ); let b = r .fill_buf() .expect("`BuffedRead::fill_buf(..)` must always be successful when no data is left"); assert_eq!( "", b, "`BuffedRead::fill_buf(..)` must return an empty slice once no data is left" ); assert_eq!( "", r.data(), "`BuffedRead::fill_buf(..)` must return an empty slice once no data is left" ); let b = r .consume(0) .expect("`BuffedRead::try_consume(0)` must always be successful"); assert_eq!( "", b, "`BuffedRead::try_consume(..)` must return an empty slice once no data is left" ); let e = r .consume(1) .expect_err("`BuffedRead::try_consume(1)` must fail when the buffer is empty"); assert!( matches!( e, Error::ConsumedTooMuch { available: 0, .. } ), "`BuffedRead::try_consume(..)`, when the buffer is empty, must return `Error::ConsumedTooMuch {{ available: 1, .. }}`, but returned {:?}", e ); } fn read_file_to_end(path: impl AsRef, capacity: Option) { let file = File::open(path).unwrap(); let mut r = if let Some(cap) = capacity { BuffedReader::with_capacity(file, cap).unwrap() } else { BuffedReader::new(file) }; let mut expected_len = 0; let mut offset = 0; loop { let b: &str = r.data(); assert_eq!( expected_len, b.len(), "expected buffer len of {} at offset {}, but it is {}", expected_len, offset, b.len() ); if offset > 0 && expected_len == 0 { return; } let b = core::hint::black_box(r.fill_buf().expect(&format!("offset: {:?}", offset))); expected_len = b.len(); let b = r.data(); assert_eq!(expected_len, b.len()); let first_char_len = b .char_indices() .nth(1) .unwrap_or_else(|| (b.len(), '\x00')) .0; let b = core::hint::black_box(r.consume(first_char_len).unwrap()); expected_len -= first_char_len; assert_eq!(expected_len, b.len()); offset += first_char_len; } } #[test] fn ar_wikipedia_utf8_default() { read_file_to_end("tests/assets/ar.wikipedia.org_wiki_utf-8.html", None); } #[test] fn en_wikipedia_emoji_default() { read_file_to_end("tests/assets/en.wikipedia.org_wiki_Emoji.html", None); } #[test] fn capital_ru_default() { read_file_to_end("tests/assets/capital-ru.txt", None); } #[test] fn ar_wikipedia_utf8_smallest() { read_file_to_end( "tests/assets/ar.wikipedia.org_wiki_utf-8.html", Some(::max_chunk_size().get()), ); } #[test] fn en_wikipedia_emoji_smallest() { read_file_to_end( "tests/assets/en.wikipedia.org_wiki_Emoji.html", Some(::max_chunk_size().get()), ); } #[test] fn capital_ru_smallest() { read_file_to_end( "tests/assets/capital-ru.txt", Some(::max_chunk_size().get()), ); } #[test] fn ar_wikipedia_utf8_dyn_prime() { read_file_to_end( "tests/assets/ar.wikipedia.org_wiki_utf-8.html", Some(core::hint::black_box(443)), ); } #[test] fn en_wikipedia_emoji_dyn_prime() { read_file_to_end( "tests/assets/en.wikipedia.org_wiki_Emoji.html", Some(core::hint::black_box(443)), ); } #[test] fn capital_ru_dyn_prime() { read_file_to_end( "tests/assets/capital-ru.txt", Some(core::hint::black_box(443)), ); }