Skip to content

Commit 379bddd

Browse files
xperiandriclaude
andcommitted
fixup! test: contracts of Queue and IQueue
`QueueOps` and `IQueueOps` give the stable sorting contract the two functions that sort integers, so that it checks for `Queue` and `IQueue` the allocation of the descending sorts and the order in which `sortByDescending` calls the projection. The allocation test of `QueueTests` and `IQueueTests` and its helper are removed, because the contract runs the same check (743 tests for `Queue` and 703 for `IQueue`). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 5a9b87f commit 379bddd

4 files changed

Lines changed: 13 additions & 54 deletions

File tree

‎tests/FSharp.Collections.Immutable.Tests/Queue/IQueueOps.fs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,7 @@ let stableSort : StableSortOps<IQueue<struct (int * char)>> = {
162162
SortDescending = fun items -> ofSeq items |> IQueue.sortDescending |> IQueue.toArray
163163
SortWith = IQueue.sortWith
164164
SortWithComparer = IQueue.sortWithComparer
165+
166+
SortIntegersDescending = fun items -> ofSeq items |> IQueue.sortDescending |> IQueue.toArray
167+
SortIntegersByDescending = fun items -> ofSeq items |> IQueue.sortByDescending id |> IQueue.toArray
165168
}

‎tests/FSharp.Collections.Immutable.Tests/Queue/IQueueTests.fs‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ type CountingQueue<'T> (items : ImmutableQueue<'T>) =
9191
member this.GetEnumerator () = (this :> IEnumerable<'T>).GetEnumerator() :> IEnumerator
9292

9393
/// The behaviour of `IQueue` that the shared contracts do not cover: the implementation of the results, null inputs, the
94-
/// patterns, the reading of queues of other implementations, the order of strings, the memory of the descending sorts and
95-
/// the compiled names.
94+
/// patterns, the reading of queues of other implementations, the order of strings and the compiled names.
9695
[<TestClass; IQueueTestCategory; PatternMatchingTestCategory>]
9796
type IQueueTests () =
9897

@@ -362,13 +361,6 @@ type IQueueTests () =
362361
nameof IQueue.sortDescending, struct (Array.sortDescending, IQueue.sortDescending)
363362
|]
364363

365-
/// The descending sorts, by name, as functions of a queue of integers.
366-
static let descendingSorts : Map<string, IQueue<int> -> IQueue<int>> =
367-
Map.ofArray [|
368-
nameof IQueue.sortDescending, IQueue.sortDescending
369-
nameof IQueue.sortByDescending, IQueue.sortByDescending id
370-
|]
371-
372364
/// The names of the functions whose result has the element type of their input, as the rows of their test.
373365
static member SameElementType = sameElementType.Keys
374366

@@ -381,9 +373,6 @@ type IQueueTests () =
381373
/// The names of the sorts that compare the elements or their keys, as the rows of the test of strings.
382374
static member StringSorts = stringSorts.Keys
383375

384-
/// The names of the descending sorts, as the rows of the test of their memory.
385-
static member DescendingSorts = descendingSorts.Keys
386-
387376
[<TestMethod; DynamicData(nameof IQueueTests.SameElementType)>]
388377
member _.``functions with the element type of their input keep its implementation`` (name : string) =
389378
let apply = sameElementType[name]
@@ -527,15 +516,6 @@ type IQueueTests () =
527516
$"IQueue.%s{name} must order strings in F# generic comparison, as the List function does"
528517
)
529518

530-
[<TestMethod; DynamicData(nameof IQueueTests.DescendingSorts)>]
531-
member _.``descending sorts of integers do not box the elements`` (name : string) =
532-
// Seq.sortDescending and Seq.sortByDescending, compiled into a generic function, box both arguments of every
533-
// comparison: 7.8 MB for these 10 000 integers. The comparison of the sorts that List mirrors takes 0.9 MB.
534-
let queue : IQueue<int> = Queue.ofArray (Array.init 10000 (fun index -> index * 7919 % 1000))
535-
let bytes = QueueOps.allocatedBytes (fun () -> descendingSorts[name] queue |> ignore)
536-
537-
Assert.IsLessThan (3_000_000L, bytes, $"IQueue.%s{name} must compare integers without boxing them")
538-
539519
[<TestMethod; DataRow 5; DataRow 8; DataRow 7; DataRow 99>]
540520
member _.``tryFind, tryFindIndex and tryPick read a queue of another implementation up to the first match`` (value : int) =
541521
let elements = Inputs.Several

‎tests/FSharp.Collections.Immutable.Tests/Queue/QueueOps.fs‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@ let sourceName (moduleName : string) (compiledName : string) : string | null =
6464
| null -> null
6565
| attribute -> attribute.SourceName
6666

67-
/// <summary>
68-
/// The bytes that an action allocates on the current thread.
69-
/// </summary>
70-
/// <remarks>
71-
/// The action runs once before the measurement, so that the measurement does not count what the first call of generic code
72-
/// does.
73-
/// </remarks>
74-
let allocatedBytes (action : unit -> unit) : int64 =
75-
action ()
76-
let before = GC.GetAllocatedBytesForCurrentThread ()
77-
action ()
78-
GC.GetAllocatedBytesForCurrentThread () - before
7967

8068
let private toArrays (queues : Queue<Queue<int>>) = queues |> Queue.toArray |> Array.map Queue.toArray
8169

@@ -250,4 +238,11 @@ let stableSort : StableSortOps<Queue<struct (int * char)>> = {
250238
SortDescending = fun items -> Queue.ofArray items |> Queue.sortDescending |> Queue.toArray
251239
SortWith = Queue.sortWith
252240
SortWithComparer = Queue.sortWithComparer
241+
242+
SortIntegersDescending = fun items -> Queue.ofArray items |> Queue.sortDescending |> Queue.toArray
243+
SortIntegersByDescending =
244+
fun items ->
245+
Queue.ofArray items
246+
|> Queue.sortByDescending id
247+
|> Queue.toArray
253248
}

‎tests/FSharp.Collections.Immutable.Tests/Queue/QueueTests.fs‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ open FSharp.Collections.Immutable
77
open Microsoft.VisualStudio.TestTools.UnitTesting
88

99
/// The behaviour of `Queue` that the shared contracts do not cover: the first-in, first-out order, the extras, null inputs,
10-
/// the results that are their inputs, the details of the semantics of the List module, the order of strings, the memory of
11-
/// the descending sorts and the compiled names.
10+
/// the results that are their inputs, the details of the semantics of the List module, the order of strings and the
11+
/// compiled names.
1212
[<TestClass; QueueTestCategory; PatternMatchingTestCategory>]
1313
type QueueTests () =
1414

@@ -270,13 +270,6 @@ type QueueTests () =
270270
nameof Queue.sortDescending, struct (Array.sortDescending, Queue.sortDescending)
271271
|]
272272

273-
/// The descending sorts, by name, as functions of a queue of integers.
274-
static let descendingSorts : Map<string, Queue<int> -> Queue<int>> =
275-
Map.ofArray [|
276-
nameof Queue.sortDescending, Queue.sortDescending
277-
nameof Queue.sortByDescending, Queue.sortByDescending id
278-
|]
279-
280273
/// The functions of two queues, by name, with the List function that they mirror, as functions of the action that
281274
/// records the pairs; both run on a shorter and a longer queue.
282275
static let commonPrefixWalks =
@@ -412,9 +405,6 @@ type QueueTests () =
412405
/// The names of the sorts that compare the elements or their keys, as the rows of the test of strings.
413406
static member StringSorts = stringSorts.Keys
414407

415-
/// The names of the descending sorts, as the rows of the test of their memory.
416-
static member DescendingSorts = descendingSorts.Keys
417-
418408
/// The names of the functions of two queues, as the rows of the test of the common prefix.
419409
static member CommonPrefixWalks = commonPrefixWalks.Keys
420410

@@ -671,15 +661,6 @@ type QueueTests () =
671661
$"Queue.%s{name} must order strings in F# generic comparison, as the List function does"
672662
)
673663

674-
[<TestMethod; DynamicData(nameof QueueTests.DescendingSorts)>]
675-
member _.``descending sorts of integers do not box the elements`` (name : string) =
676-
// Seq.sortDescending and Seq.sortByDescending, compiled into a generic function, box both arguments of every
677-
// comparison: 7.8 MB for these 10 000 integers. The comparison of the sorts that List mirrors takes 0.8 MB.
678-
let queue = Queue.ofArray (Array.init 10000 (fun index -> index * 7919 % 1000))
679-
let bytes = QueueOps.allocatedBytes (fun () -> descendingSorts[name] queue |> ignore)
680-
681-
Assert.IsLessThan (3_000_000L, bytes, $"Queue.%s{name} must compare integers without boxing them")
682-
683664
[<TestMethod>]
684665
member _.``contains compares in F# structural equality`` () =
685666
Assert.IsTrue (

0 commit comments

Comments
 (0)