Skip to content

Commit

Permalink
Array sort is not stable. This is expected
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Jul 29, 2015
1 parent f7dd128 commit 662618c
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions src/fsharp/FSharp.Core.PropertyTests/SortByStability.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,17 @@ let sortByStableSeq<'a when 'a : comparison> (xs : 'a []) =
let sorted = indexed |> Seq.sortBy snd
sorted |> Seq.pairwise |> Seq.forall (fun ((ia, a),(ib, b)) -> if a = b then ia < ib else true)

let sortByStableArray<'a when 'a : comparison> (xs : 'a []) =
let indexed = xs |> Seq.indexed |> Seq.toArray
let sorted = indexed |> Array.sortBy snd
sorted |> Seq.pairwise |> Seq.forall (fun ((ia, a),(ib, b)) -> if a = b then ia < ib else true)

let sortByStableList<'a when 'a : comparison> (xs : 'a []) =
let indexed = xs |> Seq.indexed |> Seq.toList
let sorted = indexed |> List.sortBy snd
sorted |> Seq.pairwise |> Seq.forall (fun ((ia, a),(ib, b)) -> if a = b then ia < ib else true)

type Properties =

static member SortByStableInt xs = sortByStableSeq<int> xs
static member SortByStableArrayInt xs = sortByStableArray<int> xs
static member SortByStableListInt xs = sortByStableList<int> xs

[<Test>]
let ``Seq.sortBy is stable`` () =
Check.QuickThrowOnFailure(fun xs -> sortByStableSeq<int> xs)
Check.QuickThrowOnFailure sortByStableSeq<int>
Check.QuickThrowOnFailure sortByStableSeq<string>

[<Test>]
let ``Array.sortBy is stable`` () =
Check.QuickThrowOnFailure(fun xs -> sortByStableArray<int> xs)

[<Test>]
let ``List.sortBy is stable`` () =
Check.QuickThrowOnFailure(fun xs -> sortByStableList<int> xs)
Check.QuickThrowOnFailure sortByStableList<int>
Check.QuickThrowOnFailure sortByStableSeq<string>

0 comments on commit 662618c

Please sign in to comment.