prelude
import Init.System.IO
import Init.Data.List.Control

new_frontend

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 ();
let 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
    let ys ← h.read 10;
    check_eq "2" [1,2,3,4,1,2,3,4,5,6] ys.toList;
    let ys ← h.read 2;
    check_eq "3" [7,8] ys.toList;
    let b ← h.isEof;
    unless (!b)
      (throw $ IO.userError $ "wrong (4): ");
    let ys ← h.read 2;
    check_eq "5" [] ys.toList;
    let 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 () };
let 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 () };
let ys ← withFile fn2 Mode.read $ fun h => do
  { let ys ← (List.iota 4).mapM $ fun i => do
    { let ln ← h.getLine;
      IO.println i;
      IO.println ∘ repr $ ln;
      let 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;
let 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 ()
};
let 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₃ };
let ys ← lines fn3;
IO.println $ repr ys;
check_eq "2" ys #[xs₀, xs₁, xs₂, xs₃];
pure ()

#eval test3
