summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-unix/common/truncate.ml
blob: 50998367d80cc49dd3f978fc71a42ca19ab03bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(* TEST
 include unix;
 hasunix;
 {
   bytecode;
 }{
   native;
 }
*)

let str = "Hello, OCaml!"
let txt = "truncate.txt"

let test file openfile stat truncate delta close =
  let () =
    let c = open_out_bin file in
    output_string c str;
    close_out c
  in
  let size file =
    (stat file).Unix.st_size
  in
  let file = openfile file in
  Printf.printf "initial size: %d\n%!" (size file);
  truncate file (size file - delta);
  Printf.printf "new size: %d\n%!" (size file);
  truncate file 0;
  Printf.printf "final size: %d\n%!" (size file);
  close file

let () =
  test "truncate.txt" (fun x -> x) Unix.stat Unix.truncate 2 ignore

let () =
  let open_it file = Unix.openfile file [O_RDWR] 0 in
  test "ftruncate.txt" open_it Unix.fstat Unix.ftruncate 3 Unix.close