# Tests of streams: what reads and writes bytes, the buffered readers and writers over # any of them, and what owns or borrows what it reads or writes. import std::io; import system; import std::testing; # A file of a test's own, removed where the test returns. struct scratch : fixture, owning { var at: path; } # Makes a file no other run has, by creating it only where nothing is. def scratch::setup(t: &mut tester): scratch { # Windows keeps its temporary files where `TEMP` says. when os == windows { for n in 0..10000 { val named := path::parse($"{temporary()}\\sakura-streams-{n}.txt") else (_) { break; }; var at := io::absolute(&named) else (_) { break; }; val flags := open_flags::write | open_flags::create | open_flags::exclusive; val made := io::file::open(&at, flags) else (_) { continue; }; return scratch { at = move(at) }; } } for n in 0..10000 { var at := path::parse($"/tmp/sakura-streams-{n}.txt") else (_) { break; }; val flags := open_flags::write | open_flags::create | open_flags::exclusive; val made := io::file::open(&at, flags) else (_) { continue; }; return scratch { at = move(at) }; } system::abort(); return scratch {}; } def scratch::close(&mut this): void { _ = io::remove(&this.at); } # How many counted streams have been closed. var counted_closed: u64 = 0; # A stream that counts what is written to it and gives what it holds to read, and # that closes, as a file does. struct counted : readable, writable, owning { var open: bool; var written: u64; var left: strview; } def counted::read(&mut this, into: slice): result { if !this.open { return io_error::invalid; } return this.left.read(into); } def counted::write(&mut this, data: span): result { if !this.open { return io_error::invalid; } this.written += data.length; return; } def counted::close(&mut this): void { if this.open { counted_closed++; this.open = false; } } # Which of the stream traits a type has, as bits: readable, writable, flushable, and # seekable, from the lowest. def streams_of(): u8 { var bits: u8 = 0; if const T is readable { bits = bits | 1; } if const T is writable { bits = bits | 2; } if const T is flushable { bits = bits | 4; } if const T is seekable { bits = bits | 8; } return bits; } # Reads lines, and writes each with its length, through whatever is given. def answer(input: &mut io::reader, out: &mut writable): void where S: readable { for line in input.lines() { _ = out.write($"{line.length}:{line};"); } } @test def streams_are_what_they_read_and_write(t: &mut tester): void { t.expects(streams_of::() == 11 && streams_of::() == 3); t.expects(streams_of::>() == 1 && streams_of::>() == 6); t.expects(streams_of::() == 2 && streams_of::() == 1); # A reference that writes has the traits of what it refers to, and one that only # reads has none, since reading and writing move a stream along. A pointer has none. t.expects(streams_of::<&mut io::file>() == 11 && streams_of::<&mut io::writer>() == 6); t.expects(streams_of::<&io::file>() == 0 && streams_of::<&strview>() == 0); t.expects(streams_of::<*mut io::file>() == 0); } @test def streams_copy_into_one_another(t: &mut tester, s: scratch): void { var opened := io::file::open(&s.at, open_flags::read | open_flags::write) else (_) { t.expects(false); return; }; var text: strview = "copied through a file\n"; val wrote := io::copy_all(&mut text, &mut opened) else (_) { t.expects(false); return; }; t.expects(wrote == 22 && text.length == 0); _ = opened.seek(0); var said := string::new(nothing {}); val read := io::copy_all(&mut opened, &mut said) else (_) { t.expects(false); return; }; t.expects(read == 22 && said.view() == "copied through a file\n"); # Arguments are referred to as the parameters ask, and a stream behind a pointer is # given through `through`. var again: strview = "again"; val p := (&mut said) as *mut string; _ = io::copy_all(again, through(p)); t.expects(said.view() == "copied through a file\nagain" && again.length == 0); var rest: strview = "all of the rest"; val all := io::read_all(&mut rest) else (_) { t.expects(false); return; }; t.expects(all.view() == "all of the rest"); } @test def readers_read_lines_of_any_stream(t: &mut tester): void { var input := io::reader_of("first\r\nsecond\n\nlast"); var seen := string::new(nothing {}); var count := 0u; for line in input.lines() { seen.append(line.view()); seen.append("|"); count++; } t.expects(seen.view() == "first|second||last|" && count == 4 && input.error is none); var bytes := io::reader_of("0123456789"); val four := bytes.read_exactly(4) else (_) { t.expects(false); return; }; val rest := bytes.read_all() else (_) { t.expects(false); return; }; t.expects(four.view() == "0123" && rest.view() == "456789"); t.expects(bytes.read_exactly(1).error == io_error::unexpected_end); # Borrowed, the view read is the caller's, which the reader moves along. var text: strview = "a\nb\n"; { var borrowed := io::reader_of(&mut text); _ = borrowed.read_line(); } t.expects(text.length == 0); # The same function reads text in a test that reads the console in a program. var asked := io::reader_of("2 + 2\n3 * 4\n"); var said := string::new(nothing {}); answer(&mut asked, &mut said); t.expects(said.view() == "5:2 + 2;5:3 * 4;"); } @test def writers_write_to_any_stream(t: &mut tester): void { var said := string::new(nothing {}); { var out := io::writer_of(&mut said); _ = out.write("held "); _ = out.write_line("then written"); t.expects(said.length == 0); _ = out.flush(); t.expects(said.view() == "held then written\n"); _ = out.write("at close"); } t.expects(said.view() == "held then written\nat close"); val as_trait: &mut writable = &mut said; { var out := io::writer_of(as_trait); _ = out.write("!"); } t.expects(said.view().ends_with("!")); # What is too large to hold goes straight through, after what was held. var big := string::new(nothing {}); var filler := string::new(nothing {}); for _ in 0..20000 { filler.push('x'); } { var out := io::writer_of(&mut big); _ = out.write("a"); _ = out.write(filler.view()); t.expects(big.length == 20001 && big[0] == 'a'); } } @test def writers_flush_what_they_write_to(t: &mut tester): void { var said := string::new(nothing {}); var inner := io::writer_of(&mut said); { var outer := io::writer_of(&mut inner); _ = outer.write("stacked"); t.expects(said.length == 0); _ = outer.flush(); t.expects(said.view() == "stacked"); } } @test def readers_and_writers_close_what_they_own(t: &mut tester): void { counted_closed = 0; { var out := io::writer_of(counted { open = true }); _ = out.write("abc"); } t.expects(counted_closed == 1); # Closed by hand, a writer writes what it holds and closes what it owns at once, # and its scope ending closes nothing twice. { var out := io::writer_of(counted { open = true }); _ = out.write("abcd"); out.close(); t.expects(counted_closed == 2 && out._sink.written == 4); } t.expects(counted_closed == 2); # Borrowing, they close nothing, by hand or not. var kept := counted { open = true, left = "kept\n" }; { var out := io::writer_of(&mut kept); _ = out.write("ef"); out.close(); var input := io::reader_of(&mut kept); val line := input.read_line() else { t.expects(false); return; }; t.expects(line.view() == "kept"); } t.expects(counted_closed == 2 && kept.open && kept.written == 2); # Moved from, one writes nothing and closes nothing, and the one moved to closes. { var a := io::writer_of(counted { open = true }); var b := move(a); t.expects(a.write("x").error == io_error::invalid); _ = b.write("y"); } t.expects(counted_closed == 3); { var input := io::reader_of(counted { open = true, left = "read" }); val all := input.read_all() else (_) { t.expects(false); return; }; t.expects(all.view() == "read"); } t.expects(counted_closed == 4); # Closing the standard output closes nothing of the console, which `output()` # lends again. io::output().close(); var info: file_info; val handle: system::fd = system::fd::stdout; t.expects(handle.stat(&mut info) == 0 && io::output().write("") is value); } @test def readers_and_writers_take_files_over(t: &mut tester, s: scratch): void { { var made := io::file::create(&s.at) else (_) { t.expects(false); return; }; var out := io::writer_of(move(made)); t.expects(!made.is_open); for i in 0..3000 { _ = out.write($"{i % 10}"); } _ = out.write_line("!"); t.expects(io::info(&s.at).value.size == 0); } t.expects(io::info(&s.at).value.size == 3002); var opened := io::file::open(&s.at) else (_) { t.expects(false); return; }; { var input := io::reader_of(&mut opened); val all := input.read_all() else (_) { t.expects(false); return; }; t.expects(all.length == 3002 && all.view().ends_with("789!\n")); } t.expects(opened.is_open); _ = opened.seek(0); var input := io::reader_of(move(opened)); val first := input.read_exactly(3) else (_) { t.expects(false); return; }; t.expects(first.view() == "012" && !opened.is_open); }