Skip to content

Instantly share code, notes, and snippets.

@takscape
Created September 4, 2014 11:13
Show Gist options
  • Save takscape/96d154b022176455be3c to your computer and use it in GitHub Desktop.
Save takscape/96d154b022176455be3c to your computer and use it in GitHub Desktop.
ocamlscript with inline unit tests
#!/usr/bin/env ocamlscript
Ocaml.ocamlflags := ["-g"; "-thread"];
Ocaml.ppopt := ["-pa-ounit-lib"; "fibo"];
Ocaml.packs := ["core"; "cfstream"; "num"; "pa_ounit.syntax"]
--
open Core.Std
open CFStream
let fibo_stream () =
Stream.unfold
(Num.num_of_int 0, Num.num_of_int 1)
(fun (cur, next) -> Some (cur, (next, Num.add_num cur next)))
TEST =
let answer = fibo_stream () |> Stream.take ~n:10 |> Stream.to_list in
answer = ([0;1;1;2;3;5;8;13;21;34] |> List.map ~f:Num.num_of_int)
let main count () =
let strm = fibo_stream () in
Stream.iter (Stream.take strm count) (fun v ->
Printf.printf "%s\n" (Num.string_of_num v))
let () =
if (Array.length Sys.argv) >= 2 && (Sys.argv.(1) = "inline-test-runner") then
Pa_ounit_lib.Runtime.summarize ()
else
let spec =
let open Command.Spec in
empty
+> (anon ("COUNT" %: int))
in
Command.run @@ Command.basic "Fibonacci sequence" spec main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment