Skip to content

Commit 50764b0

Browse files
xperiandriclaude
andcommitted
fixup! test: contracts of IndexedList and IIndexedList, and the stable sorting contract
Tests of the sequence inputs, shared where every implementer has the function. The shared tests run for `FlatList`, `IndexedList` and `IIndexedList`, and for `Queue`, `IQueue`, `Stack` and `IStack` once they derive the contracts: - `SeqCreationContract`: `ofSeq` rejects a default flat list given as a sequence with what it throws for a null sequence (`FlatList`, `IndexedList`, `Queue`, `Stack`). - `SeqLikeTransformationContract`: `concat` and `transpose`, in one data-row test each, reject a null sequence of collections with `ArgumentNullException` that names the parameter of FSharp.Core (`arrays` for `Array`, `lists` for `List`, mapped to the name of the module), and reject a default flat list given as the sequence with what they throw for a null one. `List.concat` fails with `NullReferenceException` for `null`, so the test asserts the exception of the module and not the one of the oracle. - `SeqLikePartitioningContract`: `except` rejects a default flat list given as the items to exclude with what it throws for null items, on an empty and on a non-empty input. - `IndexedContract`: `insertManyAt` rejects null values and a default flat list with `ArgumentNullException` for `values`, and checks the index before the values as `Array.insertManyAt` and `List.insertManyAt` do. `Inputs.DefaultFlatListAsSeq` is the input of these tests. Only the indexed list and its interface module have the next ones: `ListMembersContract` checks that `removeMany` and `removeManyWith` reject null items and a default flat list, on an empty and on a non-empty collection, and that they check the collection before the items. The rest is per module, for what a contract over integers cannot say: the tables of `IndexedListTests`, `IIndexedListTests` and `IndexedListBuilderTests` have the rows of a default flat list of strings given as a sequence of objects, the order of the checks, and, for `IIndexedList`, a default flat list as an element of the sequence of lists of `concat` and `transpose`. `IndexedListBuilderTests` replaces the table of null values by one with a row for each function and each input, and checks that `insertManyAt` checks the index before the values. `IndexedListComputationExpressionTests` has a table for `yield!` and `for`. The tests that a flat list that is not default still passes as a sequence of objects are in the same classes. `IIndexedListTests.insertManyAt rejects null values with the name of the parameter` is gone: the contract of `insertManyAt` covers it for every module. Breaking each fix once in a copy of the sources (28 changes: the check back to a null check, the check removed, the checks in the old order, the check of an element removed) fails the tests in every case. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 478b508 commit 50764b0

11 files changed

Lines changed: 569 additions & 32 deletions

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ type IndexedContract<'C> (ops : IndexedOps<'C>) =
2323
member _.GetHashCode item = abs item
2424
}
2525

26+
/// The values of a kind that insertManyAt rejects: a null sequence, or a default flat list given as a sequence.
27+
static let invalidValues (kind : string) : seq<int> =
28+
if kind = "null sequence" then
29+
Unchecked.defaultof<seq<int>>
30+
else
31+
Inputs.DefaultFlatListAsSeq<int>()
32+
2633
/// The collection of the input with several elements and the elements that it enumerates.
2734
let several () =
2835
let collection = ops.OfSeq Inputs.Several
@@ -60,6 +67,19 @@ type IndexedContract<'C> (ops : IndexedOps<'C>) =
6067
struct (index, values)
6168
}
6269

70+
/// The sequences that insertManyAt rejects, by kind: a null sequence and a default flat list given as a sequence.
71+
static member InvalidValueKinds = seq {
72+
"null sequence"
73+
"default flat list"
74+
}
75+
76+
/// Each index to insert at that is out of range with each kind of the values that insertManyAt rejects.
77+
static member InsertionIndicesOutOfRangeAndInvalidValues = seq {
78+
for index in IndexedContract<'C>.InsertionIndicesOutOfRange do
79+
for kind in IndexedContract<'C>.InvalidValueKinds do
80+
struct (index, kind)
81+
}
82+
6383
/// The indices to remove at: the first element, one inside and the last one.
6484
static member RemovalIndices = seq {
6585
0
@@ -251,6 +271,31 @@ type IndexedContract<'C> (ops : IndexedOps<'C>) =
251271
"insertManyAt at an index out of range must throw"
252272
)
253273

274+
[<TestMethod; DynamicData(nameof IndexedContract<'C>.InvalidValueKinds)>]
275+
member _.``insertManyAt rejects null values and a default flat list given as the values`` (kind : string) =
276+
let struct (collection, _) = several ()
277+
278+
let ex =
279+
Assert.ThrowsExactly<ArgumentNullException>(
280+
Action (fun () -> ops.InsertManyAt 0 (invalidValues kind) collection |> ignore),
281+
$"%s{ops.Name}.insertManyAt must reject a %s{kind}"
282+
)
283+
284+
Assert.AreEqual<string | null>("values", ex.ParamName, $"%s{ops.Name}.insertManyAt must name the values for a %s{kind}")
285+
286+
[<TestMethod; DynamicData(nameof IndexedContract<'C>.InsertionIndicesOutOfRangeAndInvalidValues)>]
287+
member this.``insertManyAt checks the index before the values as FSharp.Core does`` (index : int, kind : string) =
288+
let struct (collection, elements) = several ()
289+
let values = invalidValues kind
290+
291+
// The oracles check the index first and never enumerate the default flat list
292+
this.ThrowsLikeFSharpCore (
293+
(fun () -> Array.insertManyAt index values elements),
294+
(fun () -> List.insertManyAt index values (List.ofArray elements)),
295+
(fun () -> ops.InsertManyAt index values collection),
296+
$"insertManyAt at an index out of range with a %s{kind} must throw for the index"
297+
)
298+
254299
[<TestMethod; DynamicData(nameof IndexedContract<'C>.RemovalIndices)>]
255300
member _.``removeAt removes the element at the index`` (index : int) =
256301
let struct (collection, elements) = several ()

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace FSharp.Collections.Immutable.Tests
22

3+
open FSharp.Collections.Immutable
4+
35
/// The inputs that the contract tests run the functions on.
46
/// Expectations are computed with FSharp.Core over the order in which the collection enumerates its elements (ADR 0001, D8).
57
/// Every property builds new arrays on each read: MSTest hands the arrays of a row source to the tests as they are, so an
@@ -51,3 +53,7 @@ type Inputs =
5153
/// order of equal keys, so only a longer input tells a stable sort from an unstable one; this one is reordered by it
5254
/// ascending and descending.
5355
static member ManyPairs = [| for index in 0..19 -> struct (index % 3, char (int 'a' + index)) |]
56+
57+
/// A default flat list given as a sequence: the input that a function rejects as it rejects a null sequence, because
58+
/// the sequence fails with `InvalidOperationException` as soon as the function enumerates it (ADR 0001, D6).
59+
static member DefaultFlatListAsSeq<'T> () : seq<'T> = Unchecked.defaultof<FlatList<'T>> :> seq<'T>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ type SeqCreationContract<'C> (ops : SeqCreationOps<'C>) =
103103
"ofSeq of a null sequence must throw"
104104
)
105105

106+
[<TestMethod>]
107+
member _.``ofSeq rejects a default flat list given as a sequence as it rejects a null sequence`` () =
108+
let nullSource = Unchecked.defaultof<seq<int>>
109+
110+
// The default flat list is a boxed struct, not null: it fails with InvalidOperationException when it is enumerated
111+
ExceptionAssert.Same (
112+
id,
113+
(fun () -> ops.OfSeq nullSource |> ignore),
114+
(fun () -> ops.OfSeq (Inputs.DefaultFlatListAsSeq<int>()) |> ignore),
115+
$"%s{ops.Name}.ofSeq of a default flat list must throw what it throws for a null sequence"
116+
)
117+
106118
[<TestMethod; DynamicData(nameof Inputs.All, typeof<Inputs>)>]
107119
member _.``ofSpan copies the elements in order`` (input : int array) =
108120
CollectionAssert.AreEqual (input, ops.ToArray (ops.OfSpan input), $"%s{ops.Name}.ofSpan must copy the elements in order")

‎tests/FSharp.Collections.Immutable.Tests/Contracts/SeqLike/SeqLikePartitioningContract.fs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ type SeqLikePartitioningContract<'C> (ops : SeqOps<'C>) =
9999
"except of null items must throw"
100100
)
101101

102+
[<TestMethod; DynamicData(nameof Inputs.All, typeof<Inputs>)>]
103+
member this.``except rejects a default flat list given as the items to exclude as null items`` (input : int array) =
104+
let struct (collection, _) = this.Create input
105+
106+
// FSharp.Core checks the items to exclude before it looks at the input, which an empty input does not even read
107+
ExceptionAssert.Same (
108+
id,
109+
(fun () ->
110+
ops.Except Unchecked.defaultof<seq<int>> collection
111+
|> ignore
112+
),
113+
(fun () ->
114+
ops.Except (Inputs.DefaultFlatListAsSeq<int>()) collection
115+
|> ignore
116+
),
117+
$"%s{ops.Name}.except of a default flat list must throw what it throws for null items"
118+
)
119+
102120
[<TestMethod; DynamicData(nameof Inputs.All, typeof<Inputs>)>]
103121
member this.``groupBy groups the elements by key in the order of the first occurrence`` (input : int array) =
104122
let struct (collection, elements) = this.Create input

‎tests/FSharp.Collections.Immutable.Tests/Contracts/SeqLike/SeqLikeTransformationContract.fs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace FSharp.Collections.Immutable.Tests
22

3+
open System
34
open FSharp.Collections.Immutable
45
open Microsoft.VisualStudio.TestTools.UnitTesting
56

@@ -16,6 +17,20 @@ type SeqLikeTransformationContract<'C> (ops : SeqOps<'C>) =
1617
triples
1718
|> Array.map (fun (first, second, third) -> struct (first, second, third))
1819

20+
/// The functions of the module that take a sequence of collections, by name, applied to a sequence.
21+
let sequenceFunctions : Map<string, seq<'C> -> unit> =
22+
Map.ofArray [|
23+
"concat", (fun collections -> ops.Concat collections |> ignore)
24+
"transpose", (fun collections -> ops.Transpose collections |> ignore)
25+
|]
26+
27+
/// The names of the functions that take a sequence of collections, as the rows of the tests of the sequences that they
28+
/// reject.
29+
static member SequenceFunctions = seq {
30+
"concat"
31+
"transpose"
32+
}
33+
1934
/// Every input with every input, for the functions of two collections of any lengths.
2035
static member InputPairs = seq {
2136
for first in Inputs.All do
@@ -110,6 +125,34 @@ type SeqLikeTransformationContract<'C> (ops : SeqOps<'C>) =
110125

111126
Assert.IsEmpty (ops.ToArray (ops.Concat Array.empty), $"%s{ops.Name}.concat of no collections must be empty")
112127

128+
[<TestMethod; DynamicData(nameof SeqLikeTransformationContract<'C>.SequenceFunctions)>]
129+
member this.``functions of a sequence of collections reject a null sequence and name the parameter of FSharp.Core``
130+
(name : string)
131+
=
132+
// Array names the sequence "arrays" and List names it "lists". List.concat does not check it and fails with
133+
// NullReferenceException, while a null input throws ArgumentNullException (ADR 0001, D6), as Array.concat does
134+
let paramName = ops.ParamName (this.Mirrored ((fun () -> "arrays"), (fun () -> "lists")))
135+
136+
let ex =
137+
Assert.ThrowsExactly<ArgumentNullException>(
138+
Action (fun () -> sequenceFunctions[name] Unchecked.defaultof<seq<'C>>),
139+
$"%s{ops.Name}.%s{name} must reject a null sequence"
140+
)
141+
142+
Assert.AreEqual<string | null>(paramName, ex.ParamName, $"%s{ops.Name}.%s{name} must name the sequence")
143+
144+
[<TestMethod; DynamicData(nameof SeqLikeTransformationContract<'C>.SequenceFunctions)>]
145+
member _.``functions of a sequence of collections reject a default flat list given as the sequence as a null sequence``
146+
(name : string)
147+
=
148+
// The default flat list is a boxed struct, not null: it fails with InvalidOperationException when it is enumerated
149+
ExceptionAssert.Same (
150+
id,
151+
(fun () -> sequenceFunctions[name] Unchecked.defaultof<seq<'C>>),
152+
(fun () -> sequenceFunctions[name](Inputs.DefaultFlatListAsSeq<'C>())),
153+
$"%s{ops.Name}.%s{name} of a default flat list must throw what it throws for a null sequence"
154+
)
155+
113156
[<TestMethod; DynamicData(nameof Inputs.All, typeof<Inputs>)>]
114157
member this.``filter keeps the elements that satisfy the predicate`` (input : int array) =
115158
let struct (collection, elements) = this.Create input

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

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,47 @@ type IIndexedListTests () =
426426
|> ignore)
427427
|]
428428

429+
/// The functions that take a sequence, by name and by the input that they reject, with the parameter that names the
430+
/// input and the function on it. The shared contracts cover a null sequence and a default flat list given as a
431+
/// sequence of integers; the rows are a default flat list of strings given as a sequence of objects, which a type test
432+
/// for the flat list of objects does not see, the order of the checks, and a default flat list as an element of a
433+
/// sequence of lists: it implements the interface, so it can be one. A default flat list converts to a sequence
434+
/// without a check and fails when the sequence is enumerated, so the other inputs are valid.
435+
static let sequenceRejections : Map<string, struct (string * (unit -> unit))> =
436+
let widenedItems = FlatListOracles.defaultStringsAsObjects
437+
let objects = IndexedList.ofArray [| "a" :> obj |] :> IImmutableList<obj>
438+
let defaultItems = Inputs.DefaultFlatListAsSeq<int>()
439+
let defaultList = Unchecked.defaultof<FlatList<int>> :> IImmutableList<int>
440+
441+
Map.ofArray [|
442+
"concat (default flat list as an element)",
443+
struct ("lists", (fun () -> IIndexedList.concat [| several (); defaultList |] |> ignore))
444+
"transpose (default flat list as an element)",
445+
struct ("lists",
446+
(fun () ->
447+
IIndexedList.transpose [| several (); defaultList |]
448+
|> ignore
449+
))
450+
"insertManyAt (default flat list of strings as objects)",
451+
struct ("values", (fun () -> IIndexedList.insertManyAt 0 widenedItems objects |> ignore))
452+
// List.insertManyAt checks the source before the values
453+
"insertManyAt (null list and default flat list)",
454+
struct ("source", (fun () -> IIndexedList.insertManyAt 0 defaultItems nullList |> ignore))
455+
"except (default flat list of strings as objects)",
456+
struct ("itemsToExclude", (fun () -> IIndexedList.except widenedItems objects |> ignore))
457+
// List.except checks the items to exclude before the list
458+
"except (default flat list and null list)",
459+
struct ("itemsToExclude", (fun () -> IIndexedList.except defaultItems nullList |> ignore))
460+
"removeMany (default flat list of strings as objects)",
461+
struct ("items", (fun () -> IIndexedList.removeMany widenedItems objects |> ignore))
462+
"removeManyWith (default flat list of strings as objects)",
463+
struct ("items",
464+
(fun () ->
465+
IIndexedList.removeManyWith EqualityComparer<obj>.Default widenedItems objects
466+
|> ignore
467+
))
468+
|]
469+
429470
/// The strings of the searches in a range, in an implementation that is not an indexed list.
430471
static let taggedNames () : IImmutableList<string> = TaggedList (ImmutableList.CreateRange ListNames.Several)
431472

@@ -435,6 +476,9 @@ type IIndexedListTests () =
435476
/// The names of the functions that take the values to remove, as the rows of the test of null values.
436477
static member NullItemRejections = nullItemRejections.Keys
437478

479+
/// The names of the rows of the test of the sequences that the functions reject besides the ones of the contracts.
480+
static member SequenceRejections = sequenceRejections.Keys
481+
438482
/// The names of the functions that take a list, as the rows of their test.
439483
static member NullListRejections = nullListRejections.Keys
440484

@@ -474,15 +518,60 @@ type IIndexedListTests () =
474518
let struct (paramName, action) = nullListRejections[name]
475519
FlatListAssert.RejectsNull ($"IIndexedList.%s{name}", paramName, action)
476520

521+
[<TestMethod; DynamicData(nameof IIndexedListTests.SequenceRejections)>]
522+
member _.``functions that take a sequence reject a default flat list as an element or as a sequence of objects and check their inputs in order``
523+
(name : string)
524+
=
525+
let struct (paramName, actual) = sequenceRejections[name]
526+
FlatListAssert.RejectsNull ($"IIndexedList.%s{name}", paramName, actual)
527+
528+
[<TestMethod>]
529+
member _.``concat and transpose take a flat list that is not default as a list`` () =
530+
let flat = FlatList.ofArray [| 1; 2 |] :> IImmutableList<int>
531+
532+
CollectionAssert.AreEqual (
533+
[| 1; 2; 1; 2 |],
534+
IndexedList.toArray (IIndexedList.concat [| flat; flat |]),
535+
"IIndexedList.concat must take the elements of a flat list that is not default"
536+
)
537+
538+
let columns = IIndexedList.transpose [| flat; flat |]
539+
540+
CollectionAssert.AreEqual (
541+
[| 1; 1 |],
542+
IndexedList.toArray columns[0],
543+
"IIndexedList.transpose must take the elements of a flat list that is not default"
544+
)
545+
477546
[<TestMethod>]
478-
member _.``insertManyAt rejects null values with the name of the parameter`` () =
479-
FlatListAssert.RejectsNull (
480-
"IIndexedList.insertManyAt",
481-
"values",
482-
(fun () ->
483-
IIndexedList.insertManyAt 0 (Unchecked.defaultof<seq<int>>) (several ())
484-
|> ignore
485-
)
547+
member _.``a flat list of strings that is not default passes as a sequence of objects`` () =
548+
let widened = FlatListOracles.stringsAsObjects (FlatList.ofArray [| "a"; "b" |])
549+
let asObjects (strings : string array) = strings |> Array.map (fun item -> item :> obj)
550+
let objects = IndexedList.ofArray (asObjects [| "c" |]) :> IImmutableList<obj>
551+
let removable = IndexedList.ofArray (asObjects [| "a"; "c"; "b" |]) :> IImmutableList<obj>
552+
553+
CollectionAssert.AreEqual (
554+
asObjects [| "a"; "b"; "c" |],
555+
IIndexedList.toArray (IIndexedList.insertManyAt 0 widened objects),
556+
"IIndexedList.insertManyAt must take the elements of a flat list of strings as objects"
557+
)
558+
559+
CollectionAssert.AreEqual (
560+
asObjects [| "c" |],
561+
IIndexedList.toArray (IIndexedList.except widened objects),
562+
"IIndexedList.except must take the elements of a flat list of strings as objects"
563+
)
564+
565+
CollectionAssert.AreEqual (
566+
asObjects [| "c" |],
567+
IIndexedList.toArray (IIndexedList.removeMany widened removable),
568+
"IIndexedList.removeMany must take the elements of a flat list of strings as objects"
569+
)
570+
571+
CollectionAssert.AreEqual (
572+
asObjects [| "c" |],
573+
IIndexedList.toArray (IIndexedList.removeManyWith EqualityComparer<obj>.Default widened removable),
574+
"IIndexedList.removeManyWith must take the elements of a flat list of strings as objects"
486575
)
487576

488577
[<TestMethod; DynamicData(nameof IIndexedListTests.NullItemRejections)>]

0 commit comments

Comments
 (0)