File: bigarrays.ml

package info (click to toggle)
js-of-ocaml 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (21 lines) | stat: -rw-r--r-- 711 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(* TEST *)

let filename = "test.out"

let bigarray_of_string s =
  Bigarray.Array1.init Bigarray.char Bigarray.c_layout (String.length s)
    (String.get s)

let string_of_bigarray buf =
  String.init (Bigarray.Array1.dim buf) (Bigarray.Array1.get buf)

let () =
  let oc = Out_channel.open_bin filename in
  let str = ">hello, world<" in
  let buf = bigarray_of_string str in
  Out_channel.output_bigarray oc buf 1 (String.length str - 2);
  Out_channel.close oc;
  let ic = In_channel.open_bin filename in
  let buf = bigarray_of_string (String.map (fun _ -> 'X') str) in
  assert (Option.is_some (In_channel.really_input_bigarray ic buf 1 (String.length str - 2)));
  print_endline (string_of_bigarray buf)