Skip to content

Commit 430f3fb

Browse files
xperiandriclaude
andcommitted
fixup! test: contracts of IndexedList and IIndexedList, and the stable sorting contract
- The stable sorting contract checks that `sortDescending` and `sortByDescending` allocate less than 3 MB for 10 000 integers: 1.2 MB with the cached comparer, 8 MB with the inline `Seq` sorts. The second run of a call is measured, after the first has compiled the code. `StableSortOps` gets the functions `SortIntegersDescending` and `SortIntegersByDescending` for it. - It checks that `sortByDescending` calls the projection in the comparisons, in the order of `List.sortByDescending`; a sort that computes the keys first fails it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 1163fec commit 430f3fb

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

‎tests/FSharp.Collections.Immutable.Tests/Contracts/StableSortContract.fs‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ type SortDirection =
7373
/// shows the order of the elements with equal keys.
7474
/// <c>Sort</c> and <c>SortDescending</c> compare whole elements and pairs never compare equal, so they take
7575
/// <see cref="T:FSharp.Collections.Immutable.Tests.KeyedItem"/> elements instead, and the instance creates and reads the
76-
/// collection itself, as <c>SeqOps.Unzip</c> does for its pairs.
76+
/// collection itself, as <c>SeqOps.Unzip</c> does for its pairs. The same holds for the two functions that sort integers:
77+
/// the tests of the allocation of the descending sorts need elements that a comparison boxes.
7778
/// </remarks>
7879
type StableSortOps<'C> = {
7980
/// The name of the module, for the messages of the assertions.
@@ -91,6 +92,11 @@ type StableSortOps<'C> = {
9192
SortDescending : KeyedItem array -> KeyedItem array
9293
SortWith : (struct (int * char) -> struct (int * char) -> int) -> 'C -> 'C
9394
SortWithComparer : IComparer<struct (int * char)> -> 'C -> 'C
95+
96+
/// Sorts a collection of the given integers with <c>sortDescending</c>.
97+
SortIntegersDescending : int array -> int array
98+
/// Sorts a collection of the given integers with <c>sortByDescending</c>, by the integers themselves.
99+
SortIntegersByDescending : int array -> int array
94100
}
95101

96102
/// <summary>
@@ -130,6 +136,23 @@ type StableSortContract<'C> (ops : StableSortOps<'C>) =
130136
/// A projection that throws.
131137
static let throwingProjection (_ : struct (int * char)) : int = raise (NotSupportedException "The projection fails")
132138

139+
/// 10 000 integers with many duplicates, in a fixed pseudo-random order.
140+
static let manyIntegers () =
141+
let random = Random 42
142+
Array.init 10_000 (fun _ -> random.Next 1000)
143+
144+
/// What a sort of 10 000 integers may allocate: the arrays of the sort and the tree of the result need about 1.2 MB,
145+
/// and the boxes of the integers, which a comparison allocates when it goes through the generic operator, 6.8 MB more.
146+
static let allocationLimit = 3_000_000L
147+
148+
/// The bytes that an action allocates on the current thread when it runs a second time, after the first run has
149+
/// compiled the code.
150+
static let allocatedBy (action : unit -> unit) =
151+
action ()
152+
let before = GC.GetAllocatedBytesForCurrentThread ()
153+
action ()
154+
GC.GetAllocatedBytesForCurrentThread () - before
155+
133156
/// The collection of the input pairs and the elements that it enumerates.
134157
let pairs () =
135158
let collection = ops.OfSeq Inputs.ManyPairs
@@ -277,3 +300,51 @@ type StableSortContract<'C> (ops : StableSortOps<'C>) =
277300
(fun () -> ops.SortWith throwingComparer collection |> ignore),
278301
$"%s{ops.Name}.sortWith must throw what List.sortWith throws when the comparer throws"
279302
)
303+
304+
// The projection of sortByDescending runs in the comparisons, twice for each of them, as it does in
305+
// List.sortByDescending. A sort that computed the keys first would call it once for each element.
306+
307+
[<TestMethod>]
308+
member _.``sortByDescending calls the projection in the comparisons as List.sortByDescending does`` () =
309+
let struct (collection, elements) = pairs ()
310+
311+
let record (calls : ResizeArray<struct (int * char)>) (pair : struct (int * char)) =
312+
calls.Add pair
313+
key pair
314+
315+
let expected = ResizeArray ()
316+
List.sortByDescending (record expected) (List.ofArray elements)
317+
|> ignore
318+
let actual = ResizeArray ()
319+
ops.SortByDescending (record actual) collection |> ignore
320+
321+
CollectionAssert.AreEqual (
322+
expected.ToArray (),
323+
actual.ToArray (),
324+
$"%s{ops.Name}.sortByDescending must call the projection in the comparisons, as List.sortByDescending does"
325+
)
326+
327+
// Seq.sortDescending and Seq.sortByDescending are inline: in a generic function they compare with the generic
328+
// operator, which boxes the integers in every comparison and allocates 7.5 MB for 10 000 of them
329+
330+
[<TestMethod>]
331+
member _.``sortDescending allocates no box for an integer in a comparison`` () =
332+
let items = manyIntegers ()
333+
let allocated = allocatedBy (fun () -> ops.SortIntegersDescending items |> ignore)
334+
335+
Assert.IsLessThan (
336+
allocationLimit,
337+
allocated,
338+
$"%s{ops.Name}.sortDescending of 10 000 integers must not box them in the comparisons"
339+
)
340+
341+
[<TestMethod>]
342+
member _.``sortByDescending allocates no box for a key in a comparison`` () =
343+
let items = manyIntegers ()
344+
let allocated = allocatedBy (fun () -> ops.SortIntegersByDescending items |> ignore)
345+
346+
Assert.IsLessThan (
347+
allocationLimit,
348+
allocated,
349+
$"%s{ops.Name}.sortByDescending of 10 000 integers must not box the keys in the comparisons"
350+
)

‎tests/FSharp.Collections.Immutable.Tests/IndexedList/IIndexedListOps.fs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,15 @@ let stableSort : StableSortOps<IImmutableList<struct (int * char)>> = {
198198
|> IIndexedList.toArray
199199
SortWith = IIndexedList.sortWith
200200
SortWithComparer = IIndexedList.sortWithComparer
201+
202+
SortIntegersDescending =
203+
fun items ->
204+
ofSeq items
205+
|> IIndexedList.sortDescending
206+
|> IIndexedList.toArray
207+
SortIntegersByDescending =
208+
fun items ->
209+
ofSeq items
210+
|> IIndexedList.sortByDescending id
211+
|> IIndexedList.toArray
201212
}

‎tests/FSharp.Collections.Immutable.Tests/IndexedList/IndexedListOps.fs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,15 @@ let stableSort : StableSortOps<IndexedList<struct (int * char)>> = {
218218
|> IndexedList.toArray
219219
SortWith = IndexedList.sortWith
220220
SortWithComparer = IndexedList.sortWithComparer
221+
222+
SortIntegersDescending =
223+
fun items ->
224+
IndexedList.ofArray items
225+
|> IndexedList.sortDescending
226+
|> IndexedList.toArray
227+
SortIntegersByDescending =
228+
fun items ->
229+
IndexedList.ofArray items
230+
|> IndexedList.sortByDescending id
231+
|> IndexedList.toArray
221232
}

0 commit comments

Comments
 (0)