Closed MatthewIreland closed 4 years ago
Below is an example set of unit tests. The commands issued to compile them are (on Linux):
opam install -y ocamlfind ounit
ocamlfind ocamlc -o test -package oUnit -linkpkg -g $CANDIDATECODE $TESTCODE
./test
open OUnit2;;
Random.self_init ();;
let rec rep n x =
match n with
| 0 -> []
| n -> x::(rep (n-1) x)
;;
let intsTo n =
let rec intsFrom m =
if m>n then [] else m::(intsFrom (m+1))
in
intsFrom 1
;;
let testSubmissionOnEmptyList test_ctxt = assert_equal [] (Submission.filter (fun _ -> true) []);;
let testSubmissionCanReturnEmptyList test_ctxt =
let rndList = (List.sort (fun _ _ -> ((Random.int 2) - 1)) (intsTo (Random.int 10000))) @ (intsTo (Random.int 10000))
in
assert_equal [] (Submission.filter (fun x -> x < (-1)) rndList)
;;
let testSubmissionOnListOfInts test_ctxt =
let rndList = (List.sort (fun _ _ -> (Random.int 2) - 1) (intsTo (Random.int 1000))) @ (List.rev (intsTo (Random.int 1000))) @ (List.sort (fun _ _ -> (Random.int 2) - 1) (intsTo (Random.int 1000)))
in
assert_equal (List.filter (fun x -> x mod 2 = 0) rndList) (Submission.filter (fun x -> x mod 2 = 0) rndList)
;;
let testSubmissionOnListOfStrings test_ctxt =
let list = ["abcd"; "abce"; "zzz"; "hello"; "abcc"; "hello"; "xxx"]
in
assert_equal ["abcd"; "hello"; "abcc"; "hello"] (Submission.filter (fun l -> l<="abcd" || l="hello") list)
;;
let rec orderItems n l =
match l with
| [] -> []
| x::xs -> (x,n)::(orderItems (n+1) xs)
;;
let first (a,_) = a;;
let second (_,b) = b;;
let rec checkOrder prev l =
match l with
| [] -> true
| x::xs -> (second x)>prev && (checkOrder (second x) xs)
;;
let testSubmissionKeepsItemsInSameOrderAsInput test_ctxt =
let rndList = orderItems 1 ((List.sort (fun _ _ -> (Random.int 2) - 1) (intsTo (Random.int 1000))) @ (List.rev (intsTo (Random.int 1000))) @ (List.sort (fun _ _ -> (Random.int 2) - 1) (intsTo (Random.int 1000))))
in
assert_equal true (checkOrder 0 (Submission.filter (fun t -> (first t) < 100) rndList))
;;
let testQuicksortOnEmptyList test_ctxt = assert_equal [] (Submission.qs []);;
let testQuicksortOnListOfInts test_ctxt =
let rndList = (List.sort (fun _ _ -> (Random.int 2) - 1) (intsTo (Random.int 800))) @ (List.rev (intsTo (Random.int 800))) @ (List.sort (fun _ _ -> (Random.int 2) - 1) (intsTo (Random.int 800)))
in
assert_equal (List.sort compare rndList) (Submission.qs rndList)
;;
let testQuicksortOnListOfStrings test_ctxt =
let rndList = List.map string_of_int ((List.sort (fun _ _ -> (Random.int 2) - 1) (intsTo (Random.int 800))) @ (List.rev (intsTo (Random.int 800))) @ (List.sort (fun _ _ -> (Random.int 2) - 1) (intsTo (Random.int 800))))
in
assert_equal (List.sort compare rndList) (Submission.qs rndList)
;;
let suite =
"filterqsTestSuite">:::
[
OUnitTest.TestLabel("testSubmissionOnEmptyList", OUnitTest.TestCase(OUnitTest.Short, testSubmissionOnEmptyList));
OUnitTest.TestLabel("testSubmissionCanReturnEmptyList", OUnitTest.TestCase(OUnitTest.Short, testSubmissionCanReturnEmptyList));
OUnitTest.TestLabel("testSubmissionOnListOfInts", OUnitTest.TestCase(OUnitTest.Short, testSubmissionOnListOfInts));
OUnitTest.TestLabel("testSubmissionOnListOfStrings", OUnitTest.TestCase(OUnitTest.Short, testSubmissionOnListOfStrings));
OUnitTest.TestLabel("testSubmissionKeepsItemsInSameOrderAsInput", OUnitTest.TestCase(OUnitTest.Short, testSubmissionKeepsItemsInSameOrderAsInput));
OUnitTest.TestLabel("testQuicksortOnEmptyList", OUnitTest.TestCase(OUnitTest.Short, testQuicksortOnEmptyList));
OUnitTest.TestLabel("testQuicksortOnListOfInts", OUnitTest.TestCase(OUnitTest.Short, testQuicksortOnListOfInts));
OUnitTest.TestLabel("testQuicksortOnListOfStrings", OUnitTest.TestCase(OUnitTest.Short, testQuicksortOnListOfStrings));
];;
let () =
run_test_tt_main suite
;;
Need unit tests for question 3 on this sheet: http://ireland.cx/teaching/1920/svwork/ia/focs/FoCSsv2work.pdf
Need unit tests for questions 1 and 2 on this sheet: http://ireland.cx/teaching/1920/svwork/ia/focs/FoCSsv3work.pdf
There are more needed for examples classes, but let's start with the above.