Skip to content

Commit

Permalink
Fix bug #7513 (#1136)
Browse files Browse the repository at this point in the history
Add tests List.compare_lengths and List.compare_length_with in tests/lib-list
  • Loading branch information
lefessan committed May 10, 2017
1 parent eb00976 commit 908a381
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -188,6 +188,9 @@ Working version
- GPR#1155: Fix a race condition with WAIT_NOHANG on Windows
(Jérémie Dimino and David Allsopp)

- PR#7513: List.compare_length_with mishandles negative numbers / overflow
(Fabrice Le Fessant, report by Jeremy Yallop)

### Runtime system:

- GPR#938: Stack overflow detection on 64-bit Windows
Expand Down
12 changes: 7 additions & 5 deletions stdlib/list.ml
Expand Up @@ -475,9 +475,11 @@ let rec compare_lengths l1 l2 =
;;

let rec compare_length_with l n =
match l, n with
| [], 0 -> 0
| [], _ -> if n > 0 then -1 else 1
| _, 0 -> 1
| _ :: l, n -> compare_length_with l (n-1)
match l with
| [] ->
if n = 0 then 0 else
if n > 0 then -1 else 1
| _ :: l ->
if n <= 0 then 1 else
compare_length_with l (n-1)
;;
17 changes: 17 additions & 0 deletions testsuite/tests/lib-list/test.ml
Expand Up @@ -16,6 +16,23 @@ let () =
assert (not (List.exists (fun a -> a < 0) l));
assert (not (List.exists (fun a -> a > 9) l));
assert (List.exists (fun _ -> true) l);

assert (List.compare_lengths [] [] = 0);
assert (List.compare_lengths [1;2] ['a';'b'] = 0);
assert (List.compare_lengths [] [1;2] < 0);
assert (List.compare_lengths ['a'] [1;2] < 0);
assert (List.compare_lengths [1;2] [] > 0);
assert (List.compare_lengths [1;2] ['a'] > 0);

assert (List.compare_length_with [] 0 = 0);
assert (List.compare_length_with [] 1 < 0);
assert (List.compare_length_with [] (-1) > 0);
assert (List.compare_length_with [] max_int < 0);
assert (List.compare_length_with [] min_int > 0);
assert (List.compare_length_with [1] 0 > 0);
assert (List.compare_length_with ['1'] 1 = 0);
assert (List.compare_length_with ['1'] 2 < 0);
()
;;

(* Empty test case *)
Expand Down

0 comments on commit 908a381

Please sign in to comment.