lean4-htt/tests/lean/run/IO_test.lean
2020-03-23 14:53:27 -07:00

108 lines
2.8 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

prelude
import Init.System.IO
import Init.Data.List.Control
open IO.FS
instance : HasRepr UInt8 := ⟨ toString ⟩
def check_eq {α} [HasBeq α] [HasRepr α] (tag : String) (expected actual : α) : IO Unit :=
unless (expected == actual) $ throw $ IO.userError $
"assertion failure \"" ++ tag ++
"\":\n expected: " ++ repr expected ++
"\n actual: " ++ repr actual
def test : IO Unit := do
let xs : ByteArray := ⟨#[1,2,3,4]⟩;
let fn := "foo.txt";
withFile fn Mode.write $ fun h => do
{ h.write xs;
h.write xs;
pure () };
ys ← withFile "foo.txt" Mode.read $ fun h => h.read 10;
check_eq "1" (xs.toList ++ xs.toList) ys.toList;
withFile fn Mode.append $ fun h => do
{ h.write ⟨#[5,6,7,8]⟩;
pure () };
withFile "foo.txt" Mode.read $ fun h => do
{ ys ← h.read 10;
check_eq "2" [1,2,3,4,1,2,3,4,5,6] ys.toList;
ys ← h.read 2;
check_eq "3" [7,8] ys.toList;
b ← h.isEof;
unless (!b)
(throw $ IO.userError $ "wrong (4): ");
ys ← h.read 2;
check_eq "5" [] ys.toList;
b ← h.isEof;
unless b
(throw $ IO.userError $ "wrong (6): ") };
pure ()
#eval test
def test2 : IO Unit := do
let fn2 := "foo2.txt";
let xs₀ : String := "⟨[₂,α]⟩";
let xs₁ := "⟨[6,8,@]⟩";
let xs₂ := "/* Handle.getLine : Handle → IO Unit */" ++
"/* The line returned by `lean_io_prim_handle_get_line` */" ++
"/* is truncated at the first \'\\0\' character and the */" ++
"/* rest of the line is discarded. */";
-- multi-buffer line
withFile fn2 Mode.write $ fun h => pure ();
withFile fn2 Mode.write $ fun h => do
{ h.putStr xs₀;
h.putStrLn xs₀;
h.putStrLn xs₂;
h.putStrLn xs₁;
pure () };
ys ← withFile fn2 Mode.read $ fun h => h.getLine;
IO.println ys;
check_eq "1" (xs₀ ++ xs₀ ++ "\n") ys;
IO.println ys;
withFile fn2 Mode.append $ fun h => do
{ h.putStrLn xs₁;
pure () };
ys ← withFile fn2 Mode.read $ fun h => do
{ ys ← (List.iota 4).mapM $ fun i => do
{ ln ← h.getLine;
IO.println i;
IO.println ∘ repr $ ln;
b ← h.isEof;
unless (i == 1 || !b) (throw $ IO.userError "isEof");
pure ln };
pure ys };
IO.println ys;
let rs := [xs₀ ++ xs₀ ++ "\n", xs₂ ++ "\n", xs₁ ++ "\n", xs₁ ++ "\n"];
check_eq "2" rs ys;
ys ← readFile fn2;
check_eq "3" (String.join rs) ys;
pure ()
#eval test2
def test3 : IO Unit := do
let fn3 := "foo3.txt";
let xs₀ := "abc";
let xs₁ := "";
let xs₂ := "hello";
let xs₃ := "world";
withFile fn3 Mode.write $ fun h => do {
pure ()
};
ys ← lines fn3;
IO.println $ repr ys;
check_eq "1" ys #[];
withFile fn3 Mode.write $ fun h => do
{ h.putStrLn xs₀;
h.putStrLn xs₁;
h.putStrLn xs₂;
h.putStrLn xs₃ };
ys ← lines fn3;
IO.println $ repr ys;
check_eq "2" ys #[xs₀, xs₁, xs₂, xs₃];
pure ()
#eval test3