diff --git a/infer/src/istd/IList.ml b/infer/src/istd/IList.ml index 48fc0b502..a3a370a58 100644 --- a/infer/src/istd/IList.ml +++ b/infer/src/istd/IList.ml @@ -90,7 +90,7 @@ let rec merge_sorted_nodup ~cmp ~res xs1 xs2 = let inter ~cmp xs ys = - let rev_sort xs = List.sort ~compare:(fun x y -> cmp y x) xs in + let rev_sort xs = List.dedup_and_sort ~compare:(fun x y -> cmp y x) xs in let rev_xs = rev_sort xs in let rev_ys = rev_sort ys in let rec inter_ is rev_xxs rev_yys = @@ -100,7 +100,7 @@ let inter ~cmp xs ys = | x :: rev_xs, y :: rev_ys -> let c = cmp x y in if Int.equal c 0 then inter_ (x :: is) rev_xs rev_ys - else if c < 0 then inter_ is rev_xs rev_yys + else if c > 0 then inter_ is rev_xs rev_yys else inter_ is rev_xxs rev_ys in inter_ [] rev_xs rev_ys diff --git a/infer/src/unit/IListTests.ml b/infer/src/unit/IListTests.ml new file mode 100644 index 000000000..1a4ca85a5 --- /dev/null +++ b/infer/src/unit/IListTests.ml @@ -0,0 +1,39 @@ +(* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + *) + +open! IStd +open OUnit2 + +let inputs = + [ ("empty", []) + ; ("0", [0]) + ; ("0s", [0; 0; 0]) + ; ("1", [1]) + ; ("1s", [1; 1]) + ; ("1_2", [1; 2]) + ; ("1_2_3", [1; 2; 3]) + ; ("1_3_2s", [1; 3; 2; 2; 2; 2]) + ; ("3_1_2", [3; 1; 2]) + ; ("0_3_2", [0; 3; 2]) + ; ("3s_1s", [3; 1; 3; 1; 3]) + ; ("4", [4]) ] + + +let tests = + let inter_test input1 input2 _ = + let using_list = IList.inter ~cmp:Int.compare input1 input2 in + let using_set = + IntSet.inter (IntSet.of_list input1) (IntSet.of_list input2) |> IntSet.elements + in + assert_equal using_list using_set + in + let tests_ = + List.concat_map inputs ~f:(fun (name1, input1) -> + List.map inputs ~f:(fun (name2, input2) -> + "inter_" ^ name1 ^ "_with_" ^ name2 >:: inter_test input1 input2 ) ) + in + "IList_tests" >::: tests_ diff --git a/infer/src/unit/inferunit.ml b/infer/src/unit/inferunit.ml index a2f55528b..585f221b6 100644 --- a/infer/src/unit/inferunit.ml +++ b/infer/src/unit/inferunit.ml @@ -34,6 +34,7 @@ let () = ; DifferentialTests.tests ; DifferentialFiltersTests.tests ; FileDiffTests.tests + ; IListTests.tests ; JavaProfilerSamplesTest.tests ; PerfProfilerATDParserTest.tests ; ProcCfgTests.tests