open OUnit2 open Vylist (* Searching for an element that is there gives Some that_element *) let test_find_existent test_ctxt = let xs = [1; 2; 3; 4] in assert_equal (find (fun x -> x = 3) xs) (Some 3) (* Searching for an element that is not there gives None *) let test_find_nonexistent test_ctxt = let xs = [1; 2; 4] in assert_equal (find (fun x -> x = 3) xs) None (* Removing a list that is there makes a list without that element *) let test_remove_existent test_ctct = let xs = [1; 2; 3; 4] in assert_equal (remove (fun x -> x = 3) xs) [1; 2; 4] (* Removing an element that is already not there returns the same list *) let test_remove_nonexistent test_ctct = let xs = [1; 2; 4] in assert_equal (remove (fun x -> x = 3) xs) [1; 2; 4] (* Replacing an element works *) let test_replace_element_existent test_ctxt = let xs = [1; 2; 3; 4] in assert_equal (replace ((=) 3) 7 xs) [1; 2; 7; 4] (* Attempt to replace a nonexisten child rauses an exception *) let test_replace_element_nonexistent test_ctxt = let xs = [1; 2; 3] in assert_raises Not_found (fun () -> replace ((=) 4) 7 xs) (* insert_before works if the element is there *) let test_insert_before_existent test_ctxt = let xs = [1; 2; 3] in assert_equal (insert_before ((=) 2) 7 xs) [1; 7; 2; 3] (* insert_before raises Not_found if there's not such element *) let test_insert_before_nonexistent test_ctxt = let xs = [1; 2; 3] in assert_raises Not_found (fun () -> insert_before ((=) 9) 7 xs) (* complement returns correct result when one list contains another, in any order *) let test_complement_first_is_longer test_ctxt = let xs = [1;2;3;4;5] and ys = [1;2;3] in assert_equal (complement xs ys) (Some [4;5]) let test_complement_second_is_longer test_ctxt = let xs = [1;2] and ys = [1;2;3;4;5] in assert_equal (complement xs ys) (Some [3;4;5]) (* complement returns None if one list doesn't contain another *) let test_complement_doesnt_contain test_ctxt = let xs = [1;2;3] and ys = [1;4;5;6] in assert_equal (complement xs ys) None let suite = "VyConf list tests" >::: [ "test_find_existent" >:: test_find_existent; "test_find_nonexistent" >:: test_find_nonexistent; "test_remove_existent" >:: test_remove_existent; "test_remove_nonexistent" >:: test_remove_nonexistent; "test_replace_element_existent" >:: test_replace_element_existent; "test_replace_element_nonexistent" >:: test_replace_element_nonexistent; "test_insert_before_existent" >:: test_insert_before_existent; "test_insert_before_nonexistent" >:: test_insert_before_nonexistent; "test_complement_first_is_longer" >:: test_complement_first_is_longer; "test_complement_second_is_longer" >:: test_complement_second_is_longer; "test_complement_doesnt_contain" >:: test_complement_doesnt_contain; ] let () = run_test_tt_main suite