Skip to content

Commit f8d0f98

Browse files
xperiandriclaude
andcommitted
fixup! test: SortedSet against the set contracts and the order of Set
One data-row test for each group of functions that take a sequence, with a row for every input that they reject: `null`, a default flat list given as a sequence, and a default flat list of strings given as a sequence of objects, which `checkNotNullOrDefaultSeq` recognizes through reflection. - `SortedSetTests`: the functions of the module and the member `ofSeq` of the struct, 15 rows. - `SortedSetComputationExpressionTests`: `yield!` for a sequence, an array and a sorted set, and `for`, 8 rows. `obj` has no F# comparison, which the expression requires, so its rows give the flat list of strings as a sequence of `IComparable`. Each row asserts the exception, its type and the name of the parameter through `FlatListAssert.RejectsNull`, and the rows of the module reuse `FlatListOracles.defaultStringsAsObjects` of the foundation. Two tests check that a flat list that is not default passes as a sequence of objects and as a sequence of comparables. Reverting each of the 9 fixes once failed a test (1 to 3 tests each). The `yield!` for a sequence and the `for` of the expression were also reverted to the check for `null` alone, and 2 tests failed each time. Switching off the covariant case of `checkNotNullOrDefaultSeq` in `Helper.fs` failed 5 tests. Every file was restored. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 2653f2b commit f8d0f98

2 files changed

Lines changed: 180 additions & 0 deletions

File tree

‎tests/FSharp.Collections.Immutable.Tests/SortedSet/SortedSetComputationExpressionTests.fs‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,76 @@ open Microsoft.VisualStudio.TestTools.UnitTesting
99
[<TestClass; SortedSetTestCategory; ComputationExpressionTestCategory>]
1010
type SortedSetComputationExpressionTests () =
1111

12+
/// A flat list of strings as a sequence of IComparable. A flat list of strings is also a sequence of IComparable, by
13+
/// the covariance of seq: the element type of the sequence is not the one of the flat list, and a type test for the flat
14+
/// list of IComparable does not see it. obj has no F# comparison, which the expression requires, and IComparable has.
15+
static let stringsAsComparables (strings : FlatList<string>) : seq<IComparable> =
16+
match box strings with
17+
| :? seq<IComparable> as widened -> widened
18+
| _ -> invalidOp "A flat list of strings must be a sequence of IComparable"
19+
20+
/// <summary>
21+
/// The expressions that reject their input, by name, with the parameter that names the input and the expression.
22+
/// </summary>
23+
/// <remarks>
24+
/// A default flat list converts to a sequence without a check and fails when the sequence is enumerated, and a null
25+
/// input reaches <c>UnionWith</c>, which names its own parameter (ADR 0001, D6). Each expression rejects its input
26+
/// when the buffer is created, not when the expression runs.
27+
/// </remarks>
28+
static let rejections : Map<string, struct (string * (unit -> unit))> =
29+
let nullElements = Unchecked.defaultof<seq<int>>
30+
let defaultElements = Unchecked.defaultof<FlatList<int>> :> seq<int>
31+
let widenedElements = stringsAsComparables Unchecked.defaultof<FlatList<string>>
32+
33+
Map.ofArray [|
34+
"yield! (null sequence)", struct ("items", (fun () -> sortedSet { yield! nullElements } |> ignore))
35+
"yield! (default flat list)", struct ("items", (fun () -> sortedSet { yield! defaultElements } |> ignore))
36+
"yield! (default flat list of strings as comparables)",
37+
struct ("items", (fun () -> sortedSet { yield! widenedElements } |> ignore))
38+
"yield! (null array)",
39+
struct ("items",
40+
(fun () ->
41+
sortedSet { yield! Unchecked.defaultof<int array> }
42+
|> ignore
43+
))
44+
"yield! (null sorted set)",
45+
struct ("items",
46+
(fun () ->
47+
sortedSet { yield! Unchecked.defaultof<SortedSet<int>> }
48+
|> ignore
49+
))
50+
"for (null sequence)",
51+
struct ("source",
52+
(fun () ->
53+
sortedSet {
54+
for item in nullElements do
55+
yield item
56+
}
57+
|> ignore
58+
))
59+
"for (default flat list)",
60+
struct ("source",
61+
(fun () ->
62+
sortedSet {
63+
for item in defaultElements do
64+
yield item
65+
}
66+
|> ignore
67+
))
68+
"for (default flat list of strings as comparables)",
69+
struct ("source",
70+
(fun () ->
71+
sortedSet {
72+
for item in widenedElements do
73+
yield item
74+
}
75+
|> ignore
76+
))
77+
|]
78+
79+
/// The names of the expressions that reject their input, as rows.
80+
static member Rejections = rejections.Keys
81+
1282
[<TestMethod>]
1383
member _.``an expression without elements is empty`` () =
1484
let set : SortedSet<int> = sortedSet { () }
@@ -53,6 +123,28 @@ type SortedSetComputationExpressionTests () =
53123

54124
CollectionAssert.AreEqual ([| 1..7 |], SortedSet.toArray set, "sortedSet must add the elements of yield!")
55125

126+
[<TestMethod; DynamicData(nameof SortedSetComputationExpressionTests.Rejections)>]
127+
member _.``yield! and for reject a null input and a default flat list given as a sequence`` (name : string) =
128+
let struct (paramName, actual) = rejections[name]
129+
FlatListAssert.RejectsNull ($"sortedSet {{ %s{name} }}", paramName, actual)
130+
131+
[<TestMethod>]
132+
member _.``yield! and for take a flat list of strings that is not default as a sequence of comparables`` () =
133+
let widened = stringsAsComparables (FlatList.ofArray [| "b"; "a" |])
134+
135+
let set = sortedSet {
136+
yield! widened
137+
138+
for item in widened do
139+
yield item
140+
}
141+
142+
CollectionAssert.AreEqual (
143+
[| ("a" :> IComparable); ("b" :> IComparable) |],
144+
SortedSet.toArray set,
145+
"sortedSet must take the elements of a flat list of strings as comparables in yield! and for"
146+
)
147+
56148
[<TestMethod>]
57149
member _.``for adds the elements of the body for each item`` () =
58150
let set = sortedSet {

‎tests/FSharp.Collections.Immutable.Tests/SortedSet/SortedSetTests.fs‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,60 @@ type SortedSetTests () =
280280
nameof SortedSet.update, struct ("set", (fun () -> SortedSet.update ignore nullSet |> ignore))
281281
|]
282282

283+
/// <summary>
284+
/// The functions that take a sequence, by name and by the input that they reject, with the parameter that names the
285+
/// input and the function on it.
286+
/// </summary>
287+
/// <remarks>
288+
/// A default flat list converts to a sequence without a check and fails when the sequence is enumerated, so the other
289+
/// inputs are valid and the function would enumerate the sequence (ADR 0001, D6).
290+
/// </remarks>
291+
static let sequenceRejections : Map<string, struct (string * (unit -> unit))> =
292+
let set = SortedSet.ofArray Inputs.Several
293+
let ints = SortedSet.forComparer (Comparer<int>.Default :> IComparer<int>)
294+
let objectComparer = SortedSet.forComparer (Comparer<obj>.Default :> IComparer<obj>)
295+
let nullElements = Unchecked.defaultof<seq<int>>
296+
let defaultElements = Unchecked.defaultof<FlatList<int>> :> seq<int>
297+
let nullSets = Unchecked.defaultof<seq<SortedSet<int>>>
298+
let defaultSets = Unchecked.defaultof<FlatList<SortedSet<int>>> :> seq<SortedSet<int>>
299+
300+
// The flat list of strings is a sequence of objects, which a type test for the flat list of objects does not see. obj
301+
// has no F# comparison, so the sorted sets of objects and the creation from objects come from the struct.
302+
let widenedElements = FlatListOracles.defaultStringsAsObjects
303+
let objects = objectComparer.ofArray [| ("a" :> obj) |]
304+
305+
Map.ofArray [|
306+
$"%s{nameof SortedSet.addRange} (null elements)",
307+
struct ("elements", (fun () -> SortedSet.addRange nullElements set |> ignore))
308+
$"%s{nameof SortedSet.addRange} (default flat list)",
309+
struct ("elements", (fun () -> SortedSet.addRange defaultElements set |> ignore))
310+
$"%s{nameof SortedSet.addRange} (default flat list of strings as objects)",
311+
struct ("elements", (fun () -> SortedSet.addRange widenedElements objects |> ignore))
312+
$"%s{nameof SortedSet.intersectMany} (null sets)",
313+
struct ("sets", (fun () -> SortedSet.intersectMany nullSets |> ignore))
314+
$"%s{nameof SortedSet.intersectMany} (default flat list)",
315+
struct ("sets", (fun () -> SortedSet.intersectMany defaultSets |> ignore))
316+
$"%s{nameof SortedSet.ofSeq} (null elements)", struct ("elements", (fun () -> SortedSet.ofSeq nullElements |> ignore))
317+
$"%s{nameof SortedSet.ofSeq} (default flat list)",
318+
struct ("elements", (fun () -> SortedSet.ofSeq defaultElements |> ignore))
319+
$"%s{nameof SortedSet.removeRange} (null elements)",
320+
struct ("elements", (fun () -> SortedSet.removeRange nullElements set |> ignore))
321+
$"%s{nameof SortedSet.removeRange} (default flat list)",
322+
struct ("elements", (fun () -> SortedSet.removeRange defaultElements set |> ignore))
323+
$"%s{nameof SortedSet.removeRange} (default flat list of strings as objects)",
324+
struct ("elements", (fun () -> SortedSet.removeRange widenedElements objects |> ignore))
325+
$"%s{nameof SortedSet.unionMany} (null sets)", struct ("sets", (fun () -> SortedSet.unionMany nullSets |> ignore))
326+
$"%s{nameof SortedSet.unionMany} (default flat list)",
327+
struct ("sets", (fun () -> SortedSet.unionMany defaultSets |> ignore))
328+
// The struct of forComparer
329+
$"forComparer %s{nameof ints.ofSeq} (null elements)",
330+
struct ("elements", (fun () -> ints.ofSeq nullElements |> ignore))
331+
$"forComparer %s{nameof ints.ofSeq} (default flat list)",
332+
struct ("elements", (fun () -> ints.ofSeq defaultElements |> ignore))
333+
$"forComparer %s{nameof ints.ofSeq} (default flat list of strings as objects)",
334+
struct ("elements", (fun () -> objectComparer.ofSeq widenedElements |> ignore))
335+
|]
336+
283337
/// The creation functions that create a sorted set of the elements, as rows.
284338
static member Creations = seq {
285339
OfArray
@@ -329,6 +383,9 @@ type SortedSetTests () =
329383
/// The names of the functions that take a sorted set, with the parameter where a function takes two, as rows.
330384
static member NullRejections = nullRejections.Keys
331385

386+
/// The names of the functions that take a sequence, with the input that they reject, as rows.
387+
static member SequenceRejections = sequenceRejections.Keys
388+
332389
/// The indices outside the numbers: before the first one, the number of distinct numbers and the largest index.
333390
static member OutOfRangeIndices = seq {
334391
-1
@@ -1025,6 +1082,37 @@ type SortedSetTests () =
10251082

10261083
Assert.AreEqual<string | null>(paramName, ex.ParamName, $"SortedSet.%s{name} must name the parameter of the sorted set")
10271084

1085+
[<TestMethod; DynamicData(nameof SortedSetTests.SequenceRejections)>]
1086+
member _.``functions that take a sequence reject a null sequence and a default flat list given as the sequence``
1087+
(name : string)
1088+
=
1089+
let struct (paramName, actual) = sequenceRejections[name]
1090+
FlatListAssert.RejectsNull ($"SortedSet.%s{name}", paramName, actual)
1091+
1092+
[<TestMethod>]
1093+
member _.``a flat list of strings that is not default passes as a sequence of objects`` () =
1094+
let widened = FlatListOracles.stringsAsObjects (FlatList.ofArray [| "a"; "b" |])
1095+
let objectComparer = SortedSet.forComparer (Comparer<obj>.Default :> IComparer<obj>)
1096+
let objects = objectComparer.ofArray [| ("b" :> obj); ("c" :> obj) |]
1097+
1098+
CollectionAssert.AreEqual (
1099+
[| ("a" :> obj); ("b" :> obj) |],
1100+
SortedSet.toArray (objectComparer.ofSeq widened),
1101+
"CustomComparerSortedSet.ofSeq must take the elements of a flat list of strings as objects"
1102+
)
1103+
1104+
CollectionAssert.AreEqual (
1105+
[| ("a" :> obj); ("b" :> obj); ("c" :> obj) |],
1106+
SortedSet.toArray (SortedSet.addRange widened objects),
1107+
"SortedSet.addRange must take the elements of a flat list of strings as objects"
1108+
)
1109+
1110+
CollectionAssert.AreEqual (
1111+
[| ("c" :> obj) |],
1112+
SortedSet.toArray (SortedSet.removeRange widened objects),
1113+
"SortedSet.removeRange must take the elements of a flat list of strings as objects"
1114+
)
1115+
10281116
// The members of the struct reject null inputs in the creation contract
10291117
[<TestMethod>]
10301118
member _.``ofBuilder rejects a null builder`` () =

0 commit comments

Comments
 (0)