185 lines
5 KiB
OCaml
185 lines
5 KiB
OCaml
open Alcotest
|
|
open Solutions
|
|
|
|
let test_last_good () =
|
|
check (option string) ""
|
|
(last ["a" ; "b" ; "c" ; "d"]) (Some "d")
|
|
|
|
let test_last_bad () =
|
|
check (option string) ""
|
|
(last []) None
|
|
|
|
let test_last_two_good () =
|
|
check (option (pair string string)) ""
|
|
(last_two ["a" ; "b" ; "c" ; "d"]) (Some ("c", "d"))
|
|
|
|
let test_last_two_bad () =
|
|
check (option (pair string string)) ""
|
|
(last_two ["a"]) None
|
|
|
|
let test_at_good () =
|
|
check (option string) ""
|
|
(at 2 ["a" ; "b" ; "c" ; "d"; "e"]) (Some "c")
|
|
|
|
let test_at_bad () =
|
|
check (option string) ""
|
|
(at 2 ["a"]) None
|
|
|
|
let test_length () =
|
|
check int ""
|
|
(length ["a" ; "b" ; "c"]) 3
|
|
|
|
let test_length_empty () =
|
|
check int ""
|
|
(length []) 0
|
|
|
|
let test_rev () =
|
|
check (list string) ""
|
|
(rev ["a" ; "b" ; "c"]) ["c"; "b"; "a"]
|
|
|
|
let test_is_palindrome () =
|
|
check bool ""
|
|
(is_palindrome ["x"; "a"; "m"; "a"; "x"]) true
|
|
|
|
let test_not_is_palindrome () =
|
|
check bool ""
|
|
(not (is_palindrome ["a"; "b"])) true
|
|
|
|
let test_flatten () =
|
|
check (list string) ""
|
|
(flatten [One "a"; Many [One "b"; Many [One "c" ;One "d"]; One "e"]])
|
|
["a"; "b"; "c"; "d"; "e"]
|
|
|
|
|
|
let test_compress () =
|
|
check (list string) ""
|
|
(compress ["a"; "a"; "a"; "a"; "b"; "c"; "c"; "a"; "a"; "d"; "e"; "e"; "e"; "e"])
|
|
["a"; "b"; "c"; "a"; "d"; "e"]
|
|
|
|
let test_pack () =
|
|
check (list (list string)) ""
|
|
(pack ["a"; "a"; "a"; "a"; "b"; "c"; "c"; "a"; "a"; "d"; "d"; "e"; "e"; "e"; "e"])
|
|
[["a"; "a"; "a"; "a"]; ["b"]; ["c"; "c"]; ["a"; "a"]; ["d"; "d"];
|
|
["e"; "e"; "e"; "e"]]
|
|
|
|
let test_encode () =
|
|
(check (list (pair int string))) ""
|
|
(encode ["a"; "a"; "a"; "a"; "b"; "c"; "c"; "a"; "a"; "d"; "e"; "e"; "e"; "e"])
|
|
[(4, "a"); (1, "b"); (2, "c"); (2, "a"); (1, "d"); (4, "e")]
|
|
|
|
let rle =
|
|
testable pp_rle eq_rle
|
|
|
|
let test_encode' () =
|
|
(check (list rle)) ""
|
|
(encode' ["a"; "a"; "a"; "a"; "b"; "c"; "c"; "a"; "a"; "d"; "e"; "e"; "e"; "e"])
|
|
[Many (4, "a"); One "b"; Many (2, "c"); Many (2, "a"); One "d"; Many (4, "e")]
|
|
|
|
let test_decode () =
|
|
(check (list string)) ""
|
|
(decode [Many (4, "a"); One "b"; Many (2, "c"); Many (2, "a"); One "d"; Many (4, "e")])
|
|
["a"; "a"; "a"; "a"; "b"; "c"; "c"; "a"; "a"; "d"; "e"; "e"; "e"; "e"]
|
|
|
|
let test_duplicate () =
|
|
(check (list string)) ""
|
|
(duplicate ["a"; "b"; "c"; "c"; "d"])
|
|
["a"; "a"; "b"; "b"; "c"; "c"; "c"; "c"; "d"; "d"]
|
|
|
|
let test_replicate () =
|
|
(check (list string)) ""
|
|
(replicate ["a"; "b"; "c"] 3)
|
|
["a"; "a"; "a"; "b"; "b"; "b"; "c"; "c"; "c"]
|
|
|
|
let test_drop () =
|
|
(check (list string)) ""
|
|
(drop ["a"; "b"; "c"; "d"; "e"; "f"; "g"; "h"; "i"; "j"] 3)
|
|
["a"; "b"; "d"; "e"; "g"; "h"; "j"]
|
|
|
|
let test_split () =
|
|
(check (pair (list string) (list string))) ""
|
|
(split ["a"; "b"; "c"; "d"; "e"; "f"; "g"; "h"; "i"; "j"] 3)
|
|
(["a"; "b"; "c"], ["d"; "e"; "f"; "g"; "h"; "i"; "j"])
|
|
|
|
let test_split_large () =
|
|
(check (pair (list string) (list string))) ""
|
|
(split ["a"; "b"; "c"; "d"] 5)
|
|
(["a"; "b"; "c"; "d"], [])
|
|
|
|
let test_slice () =
|
|
(check (list string)) ""
|
|
(slice ["a"; "b"; "c"; "d"; "e"; "f"; "g"; "h"; "i"; "j"] 2 6)
|
|
["c"; "d"; "e"; "f"; "g"]
|
|
|
|
let test_rotate () =
|
|
(check (list string)) ""
|
|
(rotate ["a"; "b"; "c"; "d"; "e"; "f"; "g"; "h"] 3)
|
|
["d"; "e"; "f"; "g"; "h"; "a"; "b"; "c"]
|
|
|
|
let test_remove_at () =
|
|
(check (list string)) ""
|
|
(remove_at 1 ["a"; "b"; "c"; "d"])
|
|
["a"; "c"; "d"]
|
|
|
|
let test_insert_at () =
|
|
(check (list string)) ""
|
|
(insert_at "alfa" 1 ["a"; "b"; "c"; "d"])
|
|
["a"; "alfa"; "b"; "c"; "d"]
|
|
|
|
let test_range () =
|
|
(check (list int)) ""
|
|
(range 4 9)
|
|
[4; 5; 6; 7; 8; 9]
|
|
|
|
|
|
let () =
|
|
run "OCaml99" [
|
|
"Lists", [
|
|
test_case {|last: good|} `Quick test_last_good;
|
|
test_case {|last: bad|} `Quick test_last_bad;
|
|
|
|
test_case {|last_two: good|} `Quick test_last_two_good;
|
|
test_case {|last_two: bad|} `Quick test_last_two_bad;
|
|
|
|
test_case {|at: good|} `Quick test_at_good;
|
|
test_case {|at: bad|} `Quick test_at_bad;
|
|
|
|
test_case {|length: non-empty|} `Quick test_length;
|
|
test_case {|length: empty|} `Quick test_length_empty;
|
|
|
|
test_case {|rev|} `Quick test_rev;
|
|
|
|
test_case {|is_palindrome|} `Quick test_is_palindrome;
|
|
test_case {|is_palindrome: not|} `Quick test_not_is_palindrome;
|
|
|
|
test_case {|flatten|} `Quick test_flatten;
|
|
|
|
test_case {|compress|} `Quick test_compress;
|
|
|
|
test_case {|pack|} `Quick test_pack;
|
|
|
|
test_case {|encode|} `Quick test_encode;
|
|
|
|
test_case {|encode'|} `Quick test_encode';
|
|
|
|
test_case {|decode|} `Quick test_decode;
|
|
|
|
test_case {|duplicate|} `Quick test_duplicate;
|
|
|
|
test_case {|replicate|} `Quick test_replicate;
|
|
|
|
test_case {|drop|} `Quick test_drop;
|
|
|
|
test_case {|split|} `Quick test_split;
|
|
test_case {|split_large|} `Quick test_split_large;
|
|
|
|
test_case {|slice|} `Quick test_slice;
|
|
|
|
test_case {|rotate|} `Quick test_rotate;
|
|
|
|
test_case {|remove_at|} `Quick test_remove_at;
|
|
|
|
test_case {|insert_at|} `Quick test_insert_at;
|
|
|
|
test_case {|range|} `Quick test_range;
|
|
];
|
|
]
|