Skip to content

Commit

Permalink
findBack and find work in reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Jul 29, 2015
1 parent 75727b7 commit ff49e7a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,7 @@ open System.Collections.Generic

open NUnit.Framework
open FsCheck

type Result<'a> =
| Success of 'a
| Error of string

let run f =
try
Success(f())
with
| exn -> Error(exn.Message)

let runAndCheckErrorType f =
try
Success(f())
with
| exn -> Error(exn.GetType().ToString())
open Utils

let append<'a when 'a : equality> (xs : list<'a>) (xs2 : list<'a>) =
let s = xs |> Seq.append xs2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Reference Include="System.Observable" Condition="'$(TargetFramework)' == 'sl3-wp' " />
</ItemGroup>
<ItemGroup>
<Compile Include="Utils.fs" />
<Compile Include="ListProperties.fs" />
<Compile Include="SeqProperties.fs" />
<Compile Include="ArrayProperties.fs" />
Expand Down
27 changes: 27 additions & 0 deletions src/fsharp/FSharp.Core.PropertyTests/ListProperties.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open System.Collections.Generic

open NUnit.Framework
open FsCheck
open Utils

let chunkBySize_and_collect<'a when 'a : equality> (xs : 'a list) size =
size > 0 ==> (lazy
Expand Down Expand Up @@ -109,6 +110,32 @@ let ``find and exists work similar`` () =
Check.QuickThrowOnFailure find_and_exists<string>
Check.QuickThrowOnFailure find_and_exists<NormalFloat>

let findBack_and_exists<'a when 'a : comparison> (xs : list<'a>) f =
let a =
try
List.findBack f xs |> ignore
true
with
| _ -> false
let b = List.exists f xs
a = b

[<Test>]
let ``findBack and exists work similar`` () =
Check.QuickThrowOnFailure findBack_and_exists<int>
Check.QuickThrowOnFailure findBack_and_exists<string>
Check.QuickThrowOnFailure findBack_and_exists<NormalFloat>

let findBack_and_find<'a when 'a : comparison> (xs : list<'a>) predicate =
let a = run (fun () -> xs |> List.findBack predicate)
let b = run (fun () -> xs |> List.rev |> List.find predicate)
a = b

[<Test>]
let ``findBack and find work in reverse`` () =
Check.QuickThrowOnFailure findBack_and_find<int>
Check.QuickThrowOnFailure findBack_and_find<string>
Check.QuickThrowOnFailure findBack_and_find<NormalFloat>

let distinct_works_like_set<'a when 'a : comparison> (xs : 'a list) =
let a = List.distinct xs
Expand Down
19 changes: 19 additions & 0 deletions src/fsharp/FSharp.Core.PropertyTests/Utils.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

module FSharp.Core.PropertyTests.Utils

type Result<'a> =
| Success of 'a
| Error of string

let run f =
try
Success(f())
with
| exn -> Error(exn.Message)

let runAndCheckErrorType f =
try
Success(f())
with
| exn -> Error(exn.GetType().ToString())

0 comments on commit ff49e7a

Please sign in to comment.