Skip to content

Commit afee68b

Browse files
xperiandriclaude
andcommitted
test: shared contracts of the map modules, run for HashMap and IMap
- `MapOps`, `MapTransformOps`, `MapMutationOps` and `MapCreationOps` record the functions of a map module, and `MapContract`, `MapTransformContract`, `MapMutationContract` and `MapCreationContract` are the facets on `MapContractBase`. - Expected values come from the FSharp.Core `Map` module and compare as bindings sorted by key, because a hash map enumerates in an unspecified order; the functions that visit the bindings one by one are checked against the order in which the map enumerates them. - Expected exceptions come from `Map` through `ExceptionAssert.same`, so `Mirror` stays about the sequence-like modules. - `HashMap` derives all four facets and `IMap` all but the creation one, over hash maps. Their own tests cover null inputs, comparers, results that share the input, the builder functions, the computation expression and the `Option` and `Tuple` namespaces. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bb7707e commit afee68b

18 files changed

Lines changed: 2779 additions & 0 deletions

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

Lines changed: 384 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
namespace FSharp.Collections.Immutable.Tests
2+
3+
open System.Collections.Generic
4+
open FSharp.Collections.Immutable
5+
open Microsoft.VisualStudio.TestTools.UnitTesting
6+
7+
/// <summary>
8+
/// The contract of the creation functions of the concrete map modules: <c>HashMap</c>, <c>SortedMap</c> and
9+
/// <c>FrozenMap</c> (ADR 0001, Appendix B).
10+
/// </summary>
11+
/// <remarks>
12+
/// <c>ofArray</c>, <c>ofList</c> and <c>ofSeq</c> have the semantics of the FSharp.Core <c>Map</c> module: a later binding
13+
/// of a key wins. The functions that wrap the BCL keep its handling of a repeated key, which differs between the immutable
14+
/// and the frozen dictionaries, so the contract runs them on distinct keys and the collections test the repeated keys.
15+
/// </remarks>
16+
[<AbstractClass>]
17+
type MapCreationContract<'M> (ops : MapCreationOps<'M>) =
18+
inherit MapContractBase<'M> (ops.Name, ops.ParamName, ops.OfSeq, ops.ToList)
19+
20+
/// The inputs whose keys are distinct, with the bindings that repeat keys.
21+
static let withRepeatedKeys = MapInputs.all @ [ MapInputs.repeatedKeys ]
22+
23+
/// Bindings whose keys have distinct absolute values, for the key comparer that compares the absolute values.
24+
static let distinctAbsoluteKeys = [ struct (1, "one"); struct (-2, "minus two"); struct (3, "three") ]
25+
26+
static let toKeyValuePairs (bindings : struct (int * string) list) =
27+
bindings
28+
|> List.map (fun struct (key, value) -> KeyValuePair (key, value))
29+
30+
[<TestMethod; TestCategory(nameof HashMap.empty)>]
31+
member _.``empty has no bindings`` () =
32+
Assert.IsEmpty (ops.ToList (ops.Empty ()), $"%s{ops.Name}.empty must have no bindings")
33+
34+
[<TestMethod; TestCategory(nameof HashMap.ofArray)>]
35+
member this.``ofArray keeps the last binding of each key as Map does`` () =
36+
for input in withRepeatedKeys do
37+
let elements = List.toArray input
38+
39+
this.AreBindings (
40+
Map.ofArray (
41+
elements
42+
|> Array.map (fun struct (key, value) -> key, value)
43+
),
44+
ops.ToList (ops.OfArray elements),
45+
$"ofArray of %A{input}"
46+
)
47+
48+
[<TestMethod; TestCategory(nameof HashMap.ofArray)>]
49+
member _.``ofArray copies the bindings`` () =
50+
let elements = List.toArray MapInputs.several
51+
let table = ops.OfArray elements
52+
elements[0] <- struct (1000, "thousand")
53+
Assert.IsFalse (ops.ContainsKey 1000 table, $"%s{ops.Name}.ofArray must copy the bindings")
54+
55+
[<TestMethod; TestCategory(nameof HashMap.ofKeyValuePairs)>]
56+
member this.``ofKeyValuePairs creates the bindings of distinct keys`` () =
57+
for input in MapInputs.all do
58+
this.AreBindings (
59+
MapInputs.toMap input,
60+
ops.ToList (ops.OfKeyValuePairs (toKeyValuePairs input)),
61+
$"ofKeyValuePairs of %A{input}"
62+
)
63+
64+
[<TestMethod; TestCategory(nameof HashMap.ofKeyValuePairsWithKeyComparer)>]
65+
member this.``ofKeyValuePairsWithKeyComparer looks the keys up with the comparer`` () =
66+
let table = ops.OfKeyValuePairsWithKeyComparer (toKeyValuePairs distinctAbsoluteKeys)
67+
68+
this.AreBindings (
69+
MapInputs.toMap distinctAbsoluteKeys,
70+
ops.ToList table,
71+
"ofKeyValuePairsWithKeyComparer must create the bindings"
72+
)
73+
74+
for struct (key, _) in distinctAbsoluteKeys do
75+
Assert.IsTrue (
76+
ops.ContainsKey (-key) table,
77+
$"%s{ops.Name}.ofKeyValuePairsWithKeyComparer must find %d{-key} with the comparer of absolute values"
78+
)
79+
80+
[<TestMethod; TestCategory(nameof HashMap.ofList)>]
81+
member this.``ofList keeps the last binding of each key as Map does`` () =
82+
for input in withRepeatedKeys do
83+
this.AreBindings (
84+
Map.ofList (input |> List.map (fun struct (key, value) -> key, value)),
85+
ops.ToList (ops.OfList input),
86+
$"ofList of %A{input}"
87+
)
88+
89+
[<TestMethod; TestCategory(nameof HashMap.ofSeq)>]
90+
member this.``ofSeq keeps the last binding of each key as Map does`` () =
91+
for input in withRepeatedKeys do
92+
let expected = Map.ofSeq (input |> Seq.map (fun struct (key, value) -> key, value))
93+
this.AreBindings (expected, ops.ToList (ops.OfSeq input), $"ofSeq of the list %A{input}")
94+
this.AreBindings (expected, ops.ToList (ops.OfSeq (List.toArray input)), $"ofSeq of the array %A{input}")
95+
this.AreBindings (expected, ops.ToList (ops.OfSeq (Seq.map id input)), $"ofSeq of a lazy sequence of %A{input}")
96+
97+
[<TestMethod; TestCategory(nameof HashMap.ofSeqBy)>]
98+
member this.``ofSeqBy binds the values to the keys of the projection`` () =
99+
for source in [ []; [ "one" ]; [ "a"; "three"; "be"; "seven!" ] ] do
100+
this.AreBindings (
101+
source
102+
|> List.map (fun value -> value.Length, value)
103+
|> Map.ofList,
104+
ops.ToList (ops.OfSeqBy String.length source),
105+
$"ofSeqBy of %A{source}"
106+
)
107+
108+
[<TestMethod; TestCategory(nameof HashMap.ofSeqWithKeyComparer)>]
109+
member this.``ofSeqWithKeyComparer looks the keys up with the comparer`` () =
110+
let table = ops.OfSeqWithKeyComparer distinctAbsoluteKeys
111+
this.AreBindings (MapInputs.toMap distinctAbsoluteKeys, ops.ToList table, "ofSeqWithKeyComparer must create the bindings")
112+
113+
for struct (key, _) in distinctAbsoluteKeys do
114+
Assert.IsTrue (
115+
ops.ContainsKey (-key) table,
116+
$"%s{ops.Name}.ofSeqWithKeyComparer must find %d{-key} with the comparer of absolute values"
117+
)
118+
119+
[<TestMethod; TestCategory(nameof HashMap.singleton)>]
120+
member this.``singleton has one binding`` () =
121+
this.AreBindings (
122+
Map.add 42 "answer" Map.empty,
123+
ops.ToList (ops.Singleton 42 "answer"),
124+
"singleton must have one binding"
125+
)
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
namespace FSharp.Collections.Immutable.Tests
2+
3+
open System
4+
open System.Collections.Generic
5+
open FSharp.Collections.Immutable
6+
open Microsoft.VisualStudio.TestTools.UnitTesting
7+
8+
/// <summary>
9+
/// The contract of the incremental changes of the map modules that have them: <c>HashMap</c>, <c>SortedMap</c> and
10+
/// <c>IMap</c> (ADR 0001, Appendix B).
11+
/// </summary>
12+
/// <remarks>
13+
/// <c>add</c>, <c>change</c> and <c>remove</c> have the semantics of the FSharp.Core <c>Map</c> module. <c>addRange</c>,
14+
/// <c>removeRange</c>, <c>setItem</c> and <c>setItems</c> wrap the immutable dictionaries of the BCL, which share their
15+
/// semantics: <c>addRange</c> rejects another value of a key, <c>setItems</c> replaces it.
16+
/// </remarks>
17+
[<AbstractClass>]
18+
type MapMutationContract<'M> (ops : MapMutationOps<'M>) =
19+
inherit MapContractBase<'M> (ops.Name, ops.ParamName, ops.OfSeq, ops.ToList)
20+
21+
[<TestMethod; TestCategory(nameof HashMap.add)>]
22+
member this.``add adds a new key and replaces the value of an existing key as Map does`` () =
23+
this.ForEach (
24+
MapInputs.all,
25+
fun table bindings oracle ->
26+
for key in MapInputs.absentKeys @ MapInputs.keysOf bindings do
27+
this.AreBindings (
28+
Map.add key "added" oracle,
29+
ops.ToList (ops.Add key "added" table),
30+
$"add %d{key} to %A{oracle}"
31+
)
32+
)
33+
34+
[<TestMethod; TestCategory(nameof HashMap.addRange)>]
35+
member this.``addRange adds the bindings of new keys`` () =
36+
let pairs = [ struct (1, "one"); struct (-5, "minus five"); struct (100, "hundred") ]
37+
38+
this.ForEach (
39+
MapInputs.all,
40+
fun table _ oracle ->
41+
let expected =
42+
(oracle, pairs)
43+
||> List.fold (fun map struct (key, value) -> Map.add key value map)
44+
45+
this.AreBindings (expected, ops.ToList (ops.AddRange pairs table), $"addRange to %A{oracle}")
46+
)
47+
48+
[<TestMethod; TestCategory(nameof HashMap.addRange)>]
49+
member this.``addRange accepts a binding that the map already has`` () =
50+
this.ForEach (
51+
MapInputs.nonEmpty,
52+
fun table bindings oracle ->
53+
this.AreBindings (oracle, ops.ToList (ops.AddRange bindings table), $"addRange of the bindings of %A{oracle}")
54+
)
55+
56+
[<TestMethod; TestCategory(nameof HashMap.addRange)>]
57+
member _.``addRange throws for another value of a key as the BCL AddRange does`` () =
58+
let table = ops.OfSeq MapInputs.several
59+
60+
Assert.ThrowsExactly<ArgumentException>(
61+
Action (fun () -> ops.AddRange [ struct (5, "other") ] table |> ignore),
62+
$"%s{ops.Name}.addRange must reject another value of a key of the map"
63+
)
64+
|> ignore
65+
66+
Assert.ThrowsExactly<ArgumentException>(
67+
Action (fun () ->
68+
ops.AddRange [ struct (1, "one"); struct (1, "uno") ] table
69+
|> ignore
70+
),
71+
$"%s{ops.Name}.addRange must reject two values of a new key"
72+
)
73+
|> ignore
74+
75+
[<TestMethod; TestCategory(nameof HashMap.change)>]
76+
member this.``change adds, replaces and removes a binding as Map does`` () =
77+
let changes : struct (string * (string voption -> string voption)) list = [
78+
struct ("keeps nothing", (fun _ -> ValueNone))
79+
struct ("sets a value", (fun _ -> ValueSome "changed"))
80+
struct ("extends the value", (fun current -> current |> ValueOption.map (fun value -> value + "!")))
81+
struct ("adds a missing value", (fun current -> current |> ValueOption.orElse (ValueSome "new")))
82+
]
83+
84+
this.ForEach (
85+
MapInputs.all,
86+
fun table bindings oracle ->
87+
for key in MapInputs.absentKeys @ MapInputs.keysOf bindings do
88+
for struct (description, f) in changes do
89+
let expected =
90+
Map.change key (fun current -> f (ValueOption.ofOption current) |> ValueOption.toOption) oracle
91+
92+
this.AreBindings (
93+
expected,
94+
ops.ToList (ops.Change key f table),
95+
$"change %d{key} that %s{description} in %A{oracle}"
96+
)
97+
)
98+
99+
[<TestMethod; TestCategory(nameof HashMap.change)>]
100+
member _.``change calls the function once with the current value`` () =
101+
let table = ops.OfSeq MapInputs.several
102+
103+
for struct (key, expected) in [ struct (5, ValueSome "five"); struct (1, ValueNone) ] do
104+
let received = ResizeArray<string voption>()
105+
106+
ops.Change
107+
key
108+
(fun current ->
109+
received.Add current
110+
current
111+
)
112+
table
113+
|> ignore
114+
115+
Assert.AreEqual<string voption list>(
116+
[ expected ],
117+
List.ofSeq received,
118+
$"%s{ops.Name}.change %d{key} must pass the current value once"
119+
)
120+
121+
[<TestMethod; TestCategory(nameof HashMap.clear)>]
122+
member this.``clear removes every binding`` () =
123+
this.ForEach (
124+
MapInputs.all,
125+
fun table _ oracle -> Assert.IsEmpty (ops.ToList (ops.Clear table), $"%s{ops.Name}.clear of %A{oracle}")
126+
)
127+
128+
[<TestMethod; TestCategory(nameof HashMap.remove)>]
129+
member this.``remove removes the binding of a key as Map does`` () =
130+
this.ForEach (
131+
MapInputs.all,
132+
fun table bindings oracle ->
133+
for key in MapInputs.keysOf bindings @ MapInputs.absentKeys do
134+
this.AreBindings (Map.remove key oracle, ops.ToList (ops.Remove key table), $"remove %d{key} from %A{oracle}")
135+
)
136+
137+
[<TestMethod; TestCategory(nameof HashMap.removeRange)>]
138+
member this.``removeRange removes the bindings of keys and skips the other keys`` () =
139+
let keys = [ 5; 1; -7; 100; 5 ]
140+
141+
this.ForEach (
142+
MapInputs.all,
143+
fun table _ oracle ->
144+
let expected =
145+
(oracle, keys)
146+
||> List.fold (fun map key -> Map.remove key map)
147+
148+
this.AreBindings (expected, ops.ToList (ops.RemoveRange keys table), $"removeRange from %A{oracle}")
149+
)
150+
151+
[<TestMethod; TestCategory(nameof HashMap.setItem)>]
152+
member this.``setItem adds a new key and replaces the value of an existing key`` () =
153+
this.ForEach (
154+
MapInputs.all,
155+
fun table bindings oracle ->
156+
for key in MapInputs.absentKeys @ MapInputs.keysOf bindings do
157+
this.AreBindings (
158+
Map.add key "set" oracle,
159+
ops.ToList (ops.SetItem key "set" table),
160+
$"setItem %d{key} in %A{oracle}"
161+
)
162+
)
163+
164+
[<TestMethod; TestCategory(nameof HashMap.setItems)>]
165+
member this.``setItems sets the pairs in order, so that a later pair of a key wins`` () =
166+
let pairs = [
167+
KeyValuePair (5, "five again")
168+
KeyValuePair (1, "one")
169+
KeyValuePair (1, "uno")
170+
KeyValuePair (-3, "minus three")
171+
]
172+
173+
this.ForEach (
174+
MapInputs.all,
175+
fun table _ oracle ->
176+
let expected =
177+
(oracle, pairs)
178+
||> List.fold (fun map pair -> Map.add pair.Key pair.Value map)
179+
180+
this.AreBindings (expected, ops.ToList (ops.SetItems pairs table), $"setItems in %A{oracle}")
181+
)

0 commit comments

Comments
 (0)