Skip to content

Commit

Permalink
Refactor property tests
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Jul 29, 2015
1 parent 6c0ee01 commit a7b2072
Showing 1 changed file with 51 additions and 65 deletions.
116 changes: 51 additions & 65 deletions src/fsharp/FSharp.Core.PropertyTests/CollectionModulesConsistency.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,100 +9,86 @@ open NUnit.Framework
open FsCheck

let append<'a when 'a : equality> (xs : list<'a>) (xs2 : list<'a>) =
let s = xs |> Seq.append xs2
let l = xs |> List.append xs2
let a = xs |> Seq.toArray |> Array.append (Seq.toArray xs2)
s |> Seq.toArray = a &&
l |> List.toArray = a
let s = xs |> Seq.append xs2
let l = xs |> List.append xs2
let a = xs |> Seq.toArray |> Array.append (Seq.toArray xs2)
Seq.toArray s = a && List.toArray l = a

[<Test>]
let ``append is consistent`` () =
Check.QuickThrowOnFailure append<int>
Check.QuickThrowOnFailure append<string>
Check.QuickThrowOnFailure append<NormalFloat>

let contains<'a when 'a : equality> (xs : 'a []) x =
let s = xs |> Seq.contains x
let l = xs |> List.ofArray |> List.contains x
let a = xs |> Array.contains x
s = a &&
l = a
s = a && l = a

[<Test>]
let ``contains is consistent`` () =
Check.QuickThrowOnFailure contains<int>
Check.QuickThrowOnFailure contains<string>
Check.QuickThrowOnFailure contains<float>

let choose<'a when 'a : equality> (xs : 'a []) f =
let s = xs |> Seq.choose f
let l = xs |> List.ofArray |> List.choose f
let a = xs |> Array.choose f
s |> Seq.toArray = a &&
l |> List.toArray = a
Seq.toArray s = a && List.toArray l = a

[<Test>]
let ``choose is consistent`` () =
Check.QuickThrowOnFailure contains<int>
Check.QuickThrowOnFailure contains<string>
Check.QuickThrowOnFailure contains<float>

let collect<'a> (xs : 'a []) f =
let s = xs |> Seq.collect f
let l = xs |> List.ofArray |> List.collect (fun x -> f x |> List.ofArray)
let a = xs |> Array.collect f
s |> Seq.toArray = a &&
l |> List.toArray = a
Seq.toArray s = a && List.toArray l = a

[<Test>]
let ``collect is consistent`` () =
Check.QuickThrowOnFailure collect<int>
Check.QuickThrowOnFailure collect<string>
Check.QuickThrowOnFailure collect<float>

let compareWith<'a>(xs : 'a []) (xs2 : 'a []) f =
let s = (xs, xs2) ||> Seq.compareWith f
let l = (List.ofArray xs, List.ofArray xs2) ||> List.compareWith f
let a = (xs, xs2) ||> Array.compareWith f
s = a &&
l = a
s = a && l = a

[<Test>]
let ``compareWith is consistent`` () =
Check.QuickThrowOnFailure compareWith<int>
Check.QuickThrowOnFailure compareWith<string>
Check.QuickThrowOnFailure compareWith<float>

let concat<'a when 'a : equality> (xs : 'a [][]) =
let s = xs |> Seq.concat
let l = xs |> List.ofArray |> List.map List.ofArray |> List.concat
let a = xs |> Array.concat
s |> Seq.toArray = a &&
l |> List.toArray = a
Seq.toArray s = a && List.toArray l = a

[<Test>]
let ``concat is consistent`` () =
Check.QuickThrowOnFailure concat<int>
Check.QuickThrowOnFailure concat<string>
Check.QuickThrowOnFailure concat<NormalFloat>

let countBy<'a> (xs : 'a []) f =
let s = xs |> Seq.countBy f
let l = xs |> List.ofArray |> List.countBy f
let a = xs |> Array.countBy f
s |> Seq.toArray = a &&
l |> List.toArray = a

type Properties =

static member appendInt xs xs2 = append<int> xs xs2
static member appendFloat xs xs2 = append<NormalFloat> xs xs2
static member appendString xs xs2 = append<string> xs xs2

static member average (xs : NonEmptyArray<NormalFloat>) =
let xs = xs.Get |> Array.map (fun x -> x.Get)
let s = xs |> Seq.average
let l = xs |> List.ofArray |> List.average
let a = xs |> Array.average
s = a &&
l = a

static member averageBy (xs : NonEmptyArray<NormalFloat>) =
let xs = xs.Get |> Array.map (fun x -> x.Get)
let s = xs |> Seq.averageBy id
let l = xs |> List.ofArray |> List.averageBy id
let a = xs |> Array.averageBy id
s = a &&
l = a

static member containsInt xs x = contains<int> xs x
static member containsString xs x = contains<string> xs x
static member containsFloat xs x = contains<float> xs x

static member chooseInt xs f = choose<int> xs f
static member chooseString xs f = choose<string> xs f
static member chooseFloat xs f = choose<float> xs f

static member collectInt xs f = collect<int> xs f
static member collectString xs f = collect<string> xs f
static member collectFloat xs f = collect<float> xs f

static member compareWithInt xs f = compareWith<int> xs f
static member compareWithString xs f = compareWith<string> xs f
static member compareWithFloat xs f = compareWith<float> xs f

static member concatInt xs = concat<int> xs
static member concatString xs = concat<string> xs
static member concatFloat xs = concat<NormalFloat> xs

static member countByInt xs f = countBy<int> xs f
static member countByString xs f = countBy<string> xs f
static member countByFloat xs f = countBy<float> xs f
Seq.toArray s = a && List.toArray l = a

[<Test>]
let ``modules consistency`` () =
Check.QuickThrowOnFailureAll<Properties>()
let ``countBy is consistent`` () =
Check.QuickThrowOnFailure countBy<int>
Check.QuickThrowOnFailure countBy<string>
Check.QuickThrowOnFailure countBy<float>

0 comments on commit a7b2072

Please sign in to comment.