Skip to content

Instantly share code, notes, and snippets.

@mwhittaker
Last active December 18, 2015 20:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwhittaker/251af16d764e90f55f21 to your computer and use it in GitHub Desktop.
Save mwhittaker/251af16d764e90f55f21 to your computer and use it in GitHub Desktop.
OCaml Testing
type nat =
| O
| S of nat
let incr (n: nat) =
S n
let rec int_of_nat (n: nat) =
match n with
| O -> 0
| S n' -> 1 + (int_of_nat n')
let rec nat_of_int (i: int) =
match i with
| 0 -> O
| _ -> S (nat_of_int (i - 1))
open OUnit
open Main
let test_incr () =
assert_equal (S O) (incr O);
assert_equal (S (S O)) (incr (incr O));
assert_equal (S (S (S O))) (incr (incr (incr O)));
()
let test_int_of_nat () =
assert_equal 0 (int_of_nat O);
assert_equal 1 (int_of_nat (S O));
assert_equal 2 (int_of_nat (S (S O)));
()
let test_nat_of_int () =
assert_equal (nat_of_int 0) (O);
assert_equal (nat_of_int 1) (S O);
assert_equal (nat_of_int 2) (S (S O));
()
let main () =
"suite" >::: [
"test_incr" >:: test_incr;
"test_int_of_nat" >:: test_int_of_nat;
"test_nat_of_int" >:: test_nat_of_int;
] |> run_test_tt_main
let _= main ()
open OUnit
open Main
TEST_UNIT "test_incr" =
assert_equal (S O) (incr O);
assert_equal (S (S O)) (incr (incr O));
assert_equal (S (S (S O))) (incr (incr (incr O)));
()
TEST_UNIT "test_int_of_nat" =
assert_equal 0 (int_of_nat O);
assert_equal 1 (int_of_nat (S O));
assert_equal 2 (int_of_nat (S (S O)));
()
TEST_UNIT "test_nat_of_int" =
assert_equal (nat_of_int 0) (O);
assert_equal (nat_of_int 1) (S O);
assert_equal (nat_of_int 2) (S (S O));
()
let () =
Pa_ounit_lib.Runtime.summarize ()
open QCheck
open Main
let main () =
mk_test Arbitrary.(0 -- 1000)
(fun i -> int_of_nat (nat_of_int i) = i) |> run |> ignore;
mk_test Arbitrary.(fix ~base:(return O) (fun n -> map n (fun n -> S n)))
(fun n -> nat_of_int (int_of_nat n) = n) |> run |> ignore;
()
let () = main ()
all: main_ounit.byte main_qcheck.byte main_pa_ounit.byte
main_ounit.byte: main_ounit.ml
corebuild -pkg oUnit main_ounit.byte
main_pa_ounit.byte: main_pa_ounit.ml
corebuild -pkg oUnit,pa_ounit,pa_ounit.syntax main_pa_ounit.byte
main_qcheck.byte: main_qcheck.ml
corebuild -pkg qcheck main_qcheck.byte
test: all
./main_ounit.byte
./main_pa_ounit.byte inline-test-runner dummy -verbose
./main_qcheck.byte
clean:
rm -f main_ounit.byte
rm -f main_pa_ounit.byte
rm -f main_qcheck.byte
rm -rf _build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment