提交 e10b8d04 编写于 作者: P Patrick McDonald 提交者: latkin

truncate should not throw ArgumentException when count is negative

fixes #453
closes #454
上级 2044977c
......@@ -939,7 +939,8 @@ type ArrayModule2() =
CheckThrowsArgumentNullException(fun() -> Array.truncate 1 null |> ignore)
// negative count
CheckThrowsArgumentException(fun() -> Array.truncate -1 [|1..5|] |> ignore)
Assert.AreEqual([| |], Array.truncate -1 [|1..5|])
Assert.AreEqual([| |], Array.truncate System.Int32.MinValue [|1..5|])
()
......
......@@ -815,7 +815,8 @@ type ListModule02() =
Assert.AreEqual([], List.truncate 1 [])
// negative count
CheckThrowsArgumentException(fun() -> List.truncate -1 [1..5] |> ignore)
Assert.AreEqual([], List.truncate -1 [1..5])
Assert.AreEqual([], List.truncate System.Int32.MinValue [1..5])
()
......
......@@ -1533,6 +1533,11 @@ type SeqModule2() =
// null Seq
CheckThrowsArgumentNullException(fun() -> Seq.truncate 1 null |> ignore)
// negative count
VerifySeqsEqual Seq.empty <| Seq.truncate -1 (seq [1;2;4;5;7])
VerifySeqsEqual Seq.empty <| Seq.truncate System.Int32.MinValue (seq [1;2;4;5;7])
()
[<Test>]
......
......@@ -1001,8 +1001,7 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("Truncate")>]
let truncate count (array:'T[]) =
checkNonNull "array" array
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
if count = 0 then empty
if count <= 0 then empty
else
let len = array.Length
let count' = Operators.min count len
......
......@@ -981,7 +981,6 @@ namespace Microsoft.FSharp.Collections
/// <param name="array">The input array.</param>
/// <returns>The result array.</returns>
/// <exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
/// <exception cref="System.ArgumentException">Thrown when the count is negative.</exception>
[<CompiledName("Truncate")>]
val truncate: count:int -> array:'T[] -> 'T[]
......
......@@ -778,7 +778,6 @@ namespace Microsoft.FSharp.Collections
/// <param name="count">The maximum number of items to return.</param>
/// <param name="array">The input list.</param>
/// <returns>The result list.</returns>
/// <exception cref="System.ArgumentException">Thrown when the count is negative.</exception>
[<CompiledName("Truncate")>]
val truncate: count:int -> list:'T list -> 'T list
......
......@@ -441,17 +441,16 @@ module internal List =
truncateToFreshConsTail cons2 (count-1) t
let truncate count list =
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
match list with
| [] -> list
| _ :: ([] as nil) -> if count > 0 then list else nil
| h::t ->
if count = 0 then []
if count <= 0 then []
else
let cons = freshConsNoTail h
truncateToFreshConsTail cons (count-1) t
cons
let rec unfoldToFreshConsTail cons f s =
match f s with
| None -> setFreshConsTail cons []
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册