|
| 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