Skip to content

Commit

Permalink
DynArray.{left,right,tail} user input checks (#987)
Browse files Browse the repository at this point in the history
* DynArray.{left,right,tail} need user input checks
  • Loading branch information
fccm authored Nov 16, 2020
1 parent 5ff27f6 commit ab54823
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/batDynArray.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ let last d =
*)

let left a n =
if n < 0 || n > a.len then invalid_arg n "left" "len";
let arr = imake n in
for i = 0 to n - 1 do
iset arr i (iget a.arr i)
Expand All @@ -243,6 +244,7 @@ let left a n =
}

let right a n =
if n < 0 || n > a.len then invalid_arg n "right" "len";
let arr = imake n in
(* for i = a.len - n to a.len - 1 do *)
let i = ref 0 in
Expand All @@ -260,11 +262,18 @@ let right a n =
(*$T
let v = left (of_list [1;2;3;4;5;6;7;8]) 3 in to_list v = [1;2;3]
let v = right (of_list [1;2;3;4;5;6;7;8]) 3 in to_list v = [6;7;8]
try let v = left (of_list [1;2;3]) 9 in ignore v; false with Invalid_arg _ -> true
try let v = right (of_list [1;2;3]) 9 in ignore v; false with Invalid_arg _ -> true
try let v = left (of_list [1;2;3]) (-1) in ignore v; false with Invalid_arg _ -> true
try let v = right (of_list [1;2;3]) (-1) in ignore v; false with Invalid_arg _ -> true
*)

let head = left

let tail a n =
if n < 0 || n > a.len then invalid_arg n "tail" "pos";
let len = a.len - n in
let arr = imake len in
(* for i = n to a.len - 1 do *)
Expand All @@ -283,6 +292,9 @@ let tail a n =
(*$T
let v = head (of_list [1;2;3;4;5;6;7;8]) 3 in to_list v = [1;2;3]
let v = tail (of_list [1;2;3;4;5;6;7;8]) 3 in to_list v = [4;5;6;7;8]
try let v = tail (of_list [1;2;3]) 9 in ignore v; false with Invalid_arg _ -> true
try let v = tail (of_list [1;2;3]) (-1) in ignore v; false with Invalid_arg _ -> true
*)

let insert d idx v =
Expand Down

0 comments on commit ab54823

Please sign in to comment.