oxedyne/fe2o3/fe2o3_text/tests/bounded.rs
2.8 KiB, 1 run
created by r153942159:77363, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `bounded::line`: a line's kept bytes and true length, read in memory bounded by the cap. |
| 2 | //! |
| 3 | //! Written for Daimond's `file_read` (13 Nov 2026), where one 300 MB line or a gigabyte of NULs |
| 4 | //! took the server past 1 GB. |
| 5 | |
| 6 | use oxedyne_fe2o3_text::{ |
| 7 | bounded::{line, Line}, |
| 8 | outline::outline, |
| 9 | }; |
| 10 | |
| 11 | use std::io::{BufReader, Cursor, Read}; |
| 12 | |
| 13 | |
| 14 | // `n` bytes of `x` with no newline, then "tail\n", made as read: never held whole. |
| 15 | struct Long { n: u64, at: u64 } |
| 16 | |
| 17 | impl Read for Long { |
| 18 | fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> { |
| 19 | let tail = b"\ntail\n"; |
| 20 | let end = self.n + tail.len() as u64; |
| 21 | let k = (out.len() as u64).min(end - self.at) as usize; |
| 22 | for (i, b) in out[..k].iter_mut().enumerate() { |
| 23 | let p = self.at + i as u64; |
| 24 | *b = if p < self.n { b'x' } else { tail[(p - self.n) as usize] }; |
| 25 | } |
| 26 | self.at += k as u64; |
| 27 | Ok(k) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn all(text: &[u8], skip: u64, cap: usize) -> Vec<(Line, String)> { |
| 32 | let mut r = Cursor::new(text.to_vec()); |
| 33 | let mut buf = Vec::new(); |
| 34 | let mut v = Vec::new(); |
| 35 | while let Some(l) = line(&mut r, &mut buf, skip, cap).expect("an in-memory read") { |
| 36 | v.push((l, String::from_utf8_lossy(&buf).into_owned())); |
| 37 | } |
| 38 | v |
| 39 | } |
| 40 | |
| 41 | fn at(len: u64, nul: bool, s: &str) -> (Line, String) { |
| 42 | (Line { len, nul }, s.to_string()) |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn lines_keep_at_most_the_cap_and_give_their_true_length() { |
| 47 | assert_eq!(all(b"ab\ncdef\n\nxyz", 0, 2), [at(2, false, "ab"), at(4, false, "cd"), at(0, false, ""), at(3, false, "xy")]); |
| 48 | assert_eq!(all(b"0123456789\n", 3, 4), [at(10, false, "3456")]); |
| 49 | assert_eq!(all(b"0123456789\n", 12, 4), [at(10, false, "")]); |
| 50 | assert_eq!(all(b"", 0, 4), []); |
| 51 | assert_eq!(all(b"\n", 0, 4), [at(0, false, "")]); |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn a_nul_ends_the_line_it_is_in() { |
| 56 | assert_eq!(all(b"ab\0cd\nef", 0, 8)[0], at(2, true, "ab")); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn a_line_longer_than_memory_needs_is_passed_over_unheld() { |
| 61 | let n = 16 << 20; |
| 62 | let mut r = BufReader::new(Long { n, at: 0 }); |
| 63 | let mut buf = Vec::new(); |
| 64 | let first = line(&mut r, &mut buf, 0, 16).expect("a generated read"); |
| 65 | assert_eq!(first, Some(Line { len: n, nul: false })); |
| 66 | assert!(buf.len() == 16 && buf.capacity() < 1024, "kept {} of capacity {}", buf.len(), buf.capacity()); |
| 67 | let next = line(&mut r, &mut buf, 0, 16).expect("a generated read"); |
| 68 | assert_eq!((next, buf.as_slice()), (Some(Line { len: 4, nul: false }), b"tail".as_slice())); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn the_outline_of_a_long_line_is_its_head_and_of_a_nul_none() { |
| 73 | let long = format!("fn a() {{\n\tlet s = \"{}\";\n}}\nfn b() {{\n}}\n", "y".repeat(1 << 20)); |
| 74 | let o = outline(&mut Cursor::new(long.into_bytes()), 4096).expect("an in-memory read").expect("an outline"); |
| 75 | assert_eq!(o.text.lines().nth(1), Some(format!("2\t\tlet s = \"{}…", "y".repeat(90)).as_str())); |
| 76 | let nul = b"fn a() {\n\tx\n}\nfn b() {\n\0\n}\n".to_vec(); |
| 77 | assert_eq!(outline(&mut Cursor::new(nul), 4096).expect("an in-memory read"), None); |
| 78 | } |