Skip to content

Commit 982012f

Browse files
xperiandriclaude
andcommitted
fixup! test: shared contracts of the set modules, run for HashSet and ISet
Follows the reworked ADR 0001 (D2, D8, D12) and the review of #29: - Test methods carry no categories: the classes carry them. - `HashSetOps` and `ISetOps` are the root modules of their own files. `SetInputs` and `SetParamNames` move out of the contract files into root modules of their own, so that the contract files hold types only. - The creation facet also runs the members of the struct that `forComparer` creates, with a comparer that considers two numbers equal when their remainders in the division by 5 are equal: `SetInputs.sameRemainder` for hash and frozen sets, `SetInputs.byRemainder` for sorted sets. It checks one element of each class, the comparer of the struct, the copy of an array, `null` inputs and an existing set that gets the comparer, in terms that hold for `SortedSet` and `FrozenSet` too. The facet asks for the `comparer` function of the module and for a set type that is a sequence of numbers. - `HashSet`: tests of `forComparer` and the members of the struct, of `ofSeq` giving an existing hash set the comparer or returning it, of the default value of the struct, and of the `ArgumentNullException` of `forComparer`, the constructor of the struct and `HashSetCE` for a `null` comparer, with the parameter name `comparer`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 987a145 commit 982012f

18 files changed

Lines changed: 571 additions & 352 deletions

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

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,6 @@ open System
44
open FSharp.Collections.Immutable
55
open Microsoft.VisualStudio.TestTools.UnitTesting
66

7-
/// The inputs that the set contracts run the functions on.
8-
[<RequireQualifiedAccess>]
9-
module SetInputs =
10-
11-
/// A subset of `Inputs.several` that overlaps `Inputs.other`, for the subset and superset predicates.
12-
let subset = [ 5; 8 ]
13-
14-
/// Every input: no elements, one element, elements with duplicates, an input that overlaps it and a subset of it.
15-
let all = [ Inputs.empty; Inputs.singleton; Inputs.several; Inputs.other; subset ]
16-
17-
/// Every ordered pair of inputs, for the functions of two sets: equal, disjoint, overlapping, subsets and supersets.
18-
let pairs = [
19-
for first in all do
20-
for second in all do
21-
struct (first, second)
22-
]
23-
24-
/// The values to look up: in no input, in one input and in several inputs.
25-
let values = [ 42; 5; -3; 8; 1000 ]
26-
277
/// <summary>The helpers that the contract bases of the set modules share.</summary>
288
/// <remarks>
299
/// <para>
@@ -97,14 +77,14 @@ type SetContract<'S> (ops : SetOps<'S>) =
9777

9878
// ---- Properties ----
9979

100-
[<TestMethod; TestCategory(nameof HashSet.count)>]
80+
[<TestMethod>]
10181
member this.``count counts the distinct elements`` () =
10282
this.ForEach (
10383
SetInputs.all,
10484
fun set expected -> Assert.AreEqual<int>(Set.count expected, ops.Count set, $"%s{ops.Name}.count of %A{expected}")
10585
)
10686

107-
[<TestMethod; TestCategory(nameof HashSet.isEmpty)>]
87+
[<TestMethod>]
10888
member this.``isEmpty is true only for a set without elements`` () =
10989
this.ForEach (
11090
SetInputs.all,
@@ -114,7 +94,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
11494

11595
// ---- Access ----
11696

117-
[<TestMethod; TestCategory(nameof HashSet.tryGetValue)>]
97+
[<TestMethod>]
11898
member this.``tryGetValue returns the element equal to the value or nothing`` () =
11999
this.ForEach (
120100
SetInputs.all,
@@ -132,7 +112,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
132112

133113
// ---- Search ----
134114

135-
[<TestMethod; TestCategory(nameof HashSet.contains)>]
115+
[<TestMethod>]
136116
member this.``contains finds an element equal to the value`` () =
137117
this.ForEach (
138118
SetInputs.all,
@@ -145,7 +125,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
145125
)
146126
)
147127

148-
[<TestMethod; TestCategory(nameof HashSet.exists)>]
128+
[<TestMethod>]
149129
member this.``exists finds an element that satisfies the predicate`` () =
150130
this.ForEach (
151131
SetInputs.all,
@@ -158,7 +138,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
158138
)
159139
)
160140

161-
[<TestMethod; TestCategory(nameof HashSet.exists)>]
141+
[<TestMethod>]
162142
member _.``exists stops at the first element that satisfies the predicate`` () =
163143
let set = ops.OfSeq Inputs.several
164144
let mutable calls = 0
@@ -174,7 +154,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
174154
Assert.IsTrue (found, $"%s{ops.Name}.exists must find an element")
175155
Assert.AreEqual<int>(1, calls, $"%s{ops.Name}.exists must stop at the first element that satisfies the predicate")
176156

177-
[<TestMethod; TestCategory(nameof HashSet.forall)>]
157+
[<TestMethod>]
178158
member this.``forall tests every element`` () =
179159
this.ForEach (
180160
SetInputs.all,
@@ -187,7 +167,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
187167
)
188168
)
189169

190-
[<TestMethod; TestCategory(nameof HashSet.forall)>]
170+
[<TestMethod>]
191171
member _.``forall stops at the first element that does not satisfy the predicate`` () =
192172
let set = ops.OfSeq Inputs.several
193173
let mutable calls = 0
@@ -203,7 +183,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
203183
Assert.IsFalse (satisfied, $"%s{ops.Name}.forall must find an element that does not satisfy the predicate")
204184
Assert.AreEqual<int>(1, calls, $"%s{ops.Name}.forall must stop at the first element that does not satisfy the predicate")
205185

206-
[<TestMethod; TestCategory(nameof HashSet.isProperSubset)>]
186+
[<TestMethod>]
207187
member this.``isProperSubset tests whether the first set is a proper subset of the second one`` () =
208188
this.ForEachPair (fun set1 set2 expected1 expected2 ->
209189
Assert.AreEqual<bool>(
@@ -213,7 +193,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
213193
)
214194
)
215195

216-
[<TestMethod; TestCategory(nameof HashSet.isProperSuperset)>]
196+
[<TestMethod>]
217197
member this.``isProperSuperset tests whether the first set is a proper superset of the second one`` () =
218198
this.ForEachPair (fun set1 set2 expected1 expected2 ->
219199
Assert.AreEqual<bool>(
@@ -223,7 +203,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
223203
)
224204
)
225205

226-
[<TestMethod; TestCategory(nameof HashSet.isSubset)>]
206+
[<TestMethod>]
227207
member this.``isSubset tests whether the first set is a subset of the second one`` () =
228208
this.ForEachPair (fun set1 set2 expected1 expected2 ->
229209
Assert.AreEqual<bool>(
@@ -233,7 +213,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
233213
)
234214
)
235215

236-
[<TestMethod; TestCategory(nameof HashSet.isSuperset)>]
216+
[<TestMethod>]
237217
member this.``isSuperset tests whether the first set is a superset of the second one`` () =
238218
this.ForEachPair (fun set1 set2 expected1 expected2 ->
239219
Assert.AreEqual<bool>(
@@ -243,7 +223,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
243223
)
244224
)
245225

246-
[<TestMethod; TestCategory(nameof HashSet.overlaps)>]
226+
[<TestMethod>]
247227
member this.``overlaps tests whether the sets have an element in common`` () =
248228
this.ForEachPair (fun set1 set2 expected1 expected2 ->
249229
Assert.AreEqual<bool>(
@@ -253,7 +233,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
253233
)
254234
)
255235

256-
[<TestMethod; TestCategory(nameof HashSet.setEquals)>]
236+
[<TestMethod>]
257237
member this.``setEquals tests whether the sets have the same elements`` () =
258238
this.ForEachPair (fun set1 set2 expected1 expected2 ->
259239
Assert.AreEqual<bool>(
@@ -265,7 +245,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
265245

266246
// ---- Folding ----
267247

268-
[<TestMethod; TestCategory(nameof HashSet.fold)>]
248+
[<TestMethod>]
269249
member this.``fold visits every element once in the order of enumeration`` () =
270250
this.ForEach (
271251
SetInputs.all,
@@ -283,7 +263,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
283263
)
284264
)
285265

286-
[<TestMethod; TestCategory(nameof HashSet.foldBack)>]
266+
[<TestMethod>]
287267
member this.``foldBack visits every element once in the reverse order of enumeration`` () =
288268
this.ForEach (
289269
SetInputs.all,
@@ -301,7 +281,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
301281
)
302282
)
303283

304-
[<TestMethod; TestCategory(nameof HashSet.iter)>]
284+
[<TestMethod>]
305285
member this.``iter visits every element once in the order of enumeration`` () =
306286
this.ForEach (
307287
SetInputs.all,
@@ -317,7 +297,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
317297

318298
// ---- Conversion ----
319299

320-
[<TestMethod; TestCategory(nameof HashSet.toArray)>]
300+
[<TestMethod>]
321301
member this.``toArray returns the elements in the order of enumeration`` () =
322302
this.ForEach (
323303
SetInputs.all,
@@ -335,7 +315,7 @@ type SetContract<'S> (ops : SetOps<'S>) =
335315
)
336316
)
337317

338-
[<TestMethod; TestCategory(nameof HashSet.toArray)>]
318+
[<TestMethod>]
339319
member this.``toArray returns a new array`` () =
340320
let set = ops.OfSeq Inputs.several
341321
let array = ops.ToArray set
@@ -347,15 +327,15 @@ type SetContract<'S> (ops : SetOps<'S>) =
347327
$"%s{ops.Name}.toArray must return a copy that does not change the set"
348328
)
349329

350-
[<TestMethod; TestCategory(nameof HashSet.toList)>]
330+
[<TestMethod>]
351331
member this.``toList returns every element once`` () =
352332
this.ForEach (
353333
SetInputs.all,
354334
fun set expected ->
355335
Assert.AreEqual<int list>(Set.toList expected, this.Sorted set, $"%s{ops.Name}.toList of %A{expected}")
356336
)
357337

358-
[<TestMethod; TestCategory(nameof HashSet.toSeq)>]
338+
[<TestMethod>]
359339
member this.``toSeq returns the elements in the order of enumeration`` () =
360340
this.ForEach (
361341
SetInputs.all,

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

Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,47 @@ open FSharp.Collections.Immutable
44
open Microsoft.VisualStudio.TestTools.UnitTesting
55

66
/// <summary>
7-
/// The contract of the creation functions of the concrete set modules: <c>HashSet</c>, <c>SortedSet</c> and
8-
/// <c>FrozenSet</c> (ADR 0001, Appendix C).
7+
/// The contract of the creation functions of the concrete set modules, <c>HashSet</c>, <c>SortedSet</c> and
8+
/// <c>FrozenSet</c> (ADR 0001, Appendix C), and of the members of the struct that creates their sets with a comparer (D12).
99
/// </summary>
10+
/// <remarks>
11+
/// A set is a sequence of its elements, so the struct can give an existing set its comparer: the contract needs the set
12+
/// type to be a sequence of numbers for that check.
13+
/// </remarks>
1014
[<AbstractClass>]
11-
type SetCreationContract<'S> (ops : SetCreationOps<'S>) =
15+
type SetCreationContract<'S when 'S :> seq<int>> (ops : SetCreationOps<'S>) =
1216
inherit SetContractBase<'S> (ops.Name, ops.ParamName, ops.OfSeq, ops.ToList)
1317

14-
[<TestMethod; TestCategory(nameof HashSet.empty)>]
18+
let custom = ops.CustomComparer
19+
20+
/// The struct in the messages of the assertions.
21+
let customName = $"%s{ops.Name}.forComparer"
22+
23+
/// Asserts that a set compares with the comparer of the struct and holds one element of the input for each remainder,
24+
/// the classes of the comparer.
25+
let holdsOnePerRemainder (input : int list) (set : 'S) (message : string) =
26+
Assert.AreSame (custom.Comparer, ops.Comparer set, $"%s{message} must compare with the comparer of the struct")
27+
let elements = ops.ToList set
28+
29+
Assert.AreEqual<int list>(
30+
input
31+
|> List.map SetInputs.remainder
32+
|> List.distinct
33+
|> List.sort,
34+
elements |> List.map SetInputs.remainder |> List.sort,
35+
$"%s{message} must hold one element for each remainder of %A{input}"
36+
)
37+
38+
for element in elements do
39+
Assert.Contains (element, input, $"%s{message} must hold elements of %A{input}")
40+
41+
// ---- Creation ----
42+
43+
[<TestMethod>]
1544
member this.``empty has no elements`` () =
1645
Assert.IsEmpty (this.Sorted (ops.Empty ()), $"%s{ops.Name}.empty must have no elements")
1746

18-
[<TestMethod; TestCategory(nameof HashSet.ofArray)>]
47+
[<TestMethod>]
1948
member this.``ofArray copies the distinct elements`` () =
2049
for input in SetInputs.all do
2150
let source = List.toArray input
@@ -30,13 +59,13 @@ type SetCreationContract<'S> (ops : SetCreationOps<'S>) =
3059
$"%s{ops.Name}.ofArray must copy the distinct elements of %A{input}"
3160
)
3261

33-
[<TestMethod; TestCategory(nameof HashSet.ofArray)>]
62+
[<TestMethod>]
3463
member this.``ofArray throws for a null array as FSharp.Core does`` () =
3564
let source = Unchecked.defaultof<int array>
3665

3766
this.ThrowsLikeSet ((fun () -> Set.ofArray source), (fun () -> ops.OfArray source), "ofArray of a null array must throw")
3867

39-
[<TestMethod; TestCategory(nameof HashSet.ofList)>]
68+
[<TestMethod>]
4069
member this.``ofList keeps the distinct elements`` () =
4170
for input in SetInputs.all do
4271
Assert.AreEqual<int list>(
@@ -46,15 +75,15 @@ type SetCreationContract<'S> (ops : SetCreationOps<'S>) =
4675
)
4776

4877
// Set.ofList dereferences a null list without a check, so the function throws ArgumentNullException instead (D6)
49-
[<TestMethod; TestCategory(nameof HashSet.ofList)>]
78+
[<TestMethod>]
5079
member this.``ofList throws ArgumentNullException for a null list`` () =
5180
this.ThrowsArgumentNull (
5281
"elements",
5382
(fun () -> ops.OfList (Unchecked.defaultof<int list>)),
5483
"ofList of a null list must throw"
5584
)
5685

57-
[<TestMethod; TestCategory(nameof HashSet.ofSeq)>]
86+
[<TestMethod>]
5887
member this.``ofSeq keeps the distinct elements`` () =
5988
for input in SetInputs.all do
6089
let expected = Set.toList (Set.ofList input)
@@ -73,14 +102,88 @@ type SetCreationContract<'S> (ops : SetCreationOps<'S>) =
73102
)
74103

75104
// Set.ofSeq dereferences a null sequence without a check, so the function throws ArgumentNullException instead (D6)
76-
[<TestMethod; TestCategory(nameof HashSet.ofSeq)>]
105+
[<TestMethod>]
77106
member this.``ofSeq throws ArgumentNullException for a null sequence`` () =
78107
this.ThrowsArgumentNull (
79108
"elements",
80109
(fun () -> ops.OfSeq (Unchecked.defaultof<seq<int>>)),
81110
"ofSeq of a null sequence must throw"
82111
)
83112

84-
[<TestMethod; TestCategory(nameof HashSet.singleton)>]
113+
[<TestMethod>]
85114
member this.``singleton has one element`` () =
86115
Assert.AreEqual<int list>([ 42 ], this.Sorted (ops.Singleton 42), $"%s{ops.Name}.singleton must have one element")
116+
117+
// ---- The struct of forComparer ----
118+
119+
[<TestMethod>]
120+
member _.``the struct creates an empty set with its comparer`` () =
121+
let set = custom.Empty ()
122+
Assert.IsEmpty (ops.ToList set, $"%s{customName}.empty must have no elements")
123+
Assert.AreSame (custom.Comparer, ops.Comparer set, $"%s{customName}.empty must compare with the comparer of the struct")
124+
125+
[<TestMethod>]
126+
member _.``the struct copies one element of the array for each class of its comparer`` () =
127+
for input in SetInputs.all do
128+
let source = List.toArray input
129+
let set = custom.OfArray source
130+
131+
if source.Length > 0 then
132+
source[0] <- 1000
133+
134+
holdsOnePerRemainder input set $"%s{customName}.ofArray"
135+
136+
[<TestMethod>]
137+
member this.``the struct throws for a null array as FSharp.Core does`` () =
138+
let source = Unchecked.defaultof<int array>
139+
140+
this.ThrowsLikeSet (
141+
(fun () -> Set.ofArray source),
142+
(fun () -> custom.OfArray source),
143+
"forComparer ofArray of a null array must throw"
144+
)
145+
146+
[<TestMethod>]
147+
member _.``the struct keeps one element of the list for each class of its comparer`` () =
148+
for input in SetInputs.all do
149+
holdsOnePerRemainder input (custom.OfList input) $"%s{customName}.ofList"
150+
151+
[<TestMethod>]
152+
member this.``the struct throws ArgumentNullException for a null list`` () =
153+
this.ThrowsArgumentNull (
154+
"elements",
155+
(fun () -> custom.OfList (Unchecked.defaultof<int list>)),
156+
"forComparer ofList of a null list must throw"
157+
)
158+
159+
[<TestMethod>]
160+
member _.``the struct keeps one element of the sequence for each class of its comparer`` () =
161+
for input in SetInputs.all do
162+
holdsOnePerRemainder input (custom.OfSeq input) $"%s{customName}.ofSeq of the list"
163+
holdsOnePerRemainder input (custom.OfSeq (List.toArray input)) $"%s{customName}.ofSeq of the array"
164+
holdsOnePerRemainder input (custom.OfSeq (Seq.map id input)) $"%s{customName}.ofSeq of a lazy sequence"
165+
166+
[<TestMethod>]
167+
member this.``the struct gives an existing set its comparer`` () =
168+
for input in SetInputs.all do
169+
let existing = ops.OfSeq input
170+
171+
holdsOnePerRemainder input (custom.OfSeq (existing :> seq<int>)) $"%s{customName}.ofSeq of a set"
172+
173+
Assert.AreEqual<int list>(
174+
Set.toList (Set.ofList input),
175+
this.Sorted existing,
176+
$"%s{customName}.ofSeq must not change the set of %A{input}"
177+
)
178+
179+
[<TestMethod>]
180+
member this.``the struct throws ArgumentNullException for a null sequence`` () =
181+
this.ThrowsArgumentNull (
182+
"elements",
183+
(fun () -> custom.OfSeq (Unchecked.defaultof<seq<int>>)),
184+
"forComparer ofSeq of a null sequence must throw"
185+
)
186+
187+
[<TestMethod>]
188+
member _.``the struct creates a set of one element with its comparer`` () =
189+
holdsOnePerRemainder [ 42 ] (custom.Singleton 42) $"%s{customName}.singleton"

0 commit comments

Comments
 (0)