|
| 1 | +namespace FSharp.Collections.Immutable.Tests |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Collections.Frozen |
| 5 | +open System.Collections.Generic |
| 6 | +open FSharp.Collections.Immutable |
| 7 | +open Microsoft.VisualStudio.TestTools.UnitTesting |
| 8 | + |
| 9 | +/// The `frozenSet { }` computation expression and the builders with a comparer. |
| 10 | +[<TestClass; FrozenSetTestCategory; ComputationExpressionTestCategory>] |
| 11 | +type FrozenSetComputationExpressionTests () = |
| 12 | + |
| 13 | + /// The elements of a frozen set, sorted in the order of F# generic comparison. |
| 14 | + static let sorted (set : FrozenSet<'T>) = FrozenSet.toArray set |> Array.sort |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// The expressions that reject their input, by name, with the parameter that names the input and the expression. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// A default flat list converts to a sequence without a check and fails when the sequence is enumerated. A null |
| 21 | + /// sequence or frozen set reaches <c>AddRange</c>, which names its own parameter, and a null array passes |
| 22 | + /// <c>AddRange</c> without an exception (ADR 0001, D6). Each expression rejects its input when the buffer is created, |
| 23 | + /// not when the expression runs. |
| 24 | + /// </remarks> |
| 25 | + static let rejections : Map<string, struct (string * (unit -> unit))> = |
| 26 | + let nullElements = Unchecked.defaultof<seq<int>> |
| 27 | + let defaultElements = Unchecked.defaultof<FlatList<int>> :> seq<int> |
| 28 | + |
| 29 | + // The flat list of strings is a sequence of objects, which a type test for the flat list of objects does not see |
| 30 | + let widenedElements = FlatListOracles.defaultStringsAsObjects |
| 31 | + |
| 32 | + Map.ofArray [| |
| 33 | + "yield! (null sequence)", struct ("items", (fun () -> frozenSet { yield! nullElements } |> ignore)) |
| 34 | + "yield! (default flat list)", struct ("items", (fun () -> frozenSet { yield! defaultElements } |> ignore)) |
| 35 | + "yield! (default flat list of strings as objects)", |
| 36 | + struct ("items", (fun () -> frozenSet { yield! widenedElements } |> ignore)) |
| 37 | + "yield! (null array)", |
| 38 | + struct ("items", |
| 39 | + (fun () -> |
| 40 | + frozenSet { yield! Unchecked.defaultof<int array> } |
| 41 | + |> ignore |
| 42 | + )) |
| 43 | + "yield! (null frozen set)", |
| 44 | + struct ("items", |
| 45 | + (fun () -> |
| 46 | + frozenSet { yield! Unchecked.defaultof<FrozenSet<int>> } |
| 47 | + |> ignore |
| 48 | + )) |
| 49 | + "for (null sequence)", |
| 50 | + struct ("source", |
| 51 | + (fun () -> |
| 52 | + frozenSet { |
| 53 | + for item in nullElements do |
| 54 | + yield item |
| 55 | + } |
| 56 | + |> ignore |
| 57 | + )) |
| 58 | + "for (default flat list)", |
| 59 | + struct ("source", |
| 60 | + (fun () -> |
| 61 | + frozenSet { |
| 62 | + for item in defaultElements do |
| 63 | + yield item |
| 64 | + } |
| 65 | + |> ignore |
| 66 | + )) |
| 67 | + "for (default flat list of strings as objects)", |
| 68 | + struct ("source", |
| 69 | + (fun () -> |
| 70 | + frozenSet { |
| 71 | + for item in widenedElements do |
| 72 | + yield item |
| 73 | + } |
| 74 | + |> ignore |
| 75 | + )) |
| 76 | + |] |
| 77 | + |
| 78 | + /// The names of the expressions that reject their input, as rows. |
| 79 | + static member Rejections = rejections.Keys |
| 80 | + |
| 81 | + [<TestMethod>] |
| 82 | + member _.``an expression without elements is empty`` () = |
| 83 | + let set : FrozenSet<int> = frozenSet { () } |
| 84 | + Assert.IsTrue (FrozenSet.isEmpty set, "frozenSet { () } must be empty") |
| 85 | + |
| 86 | + [<TestMethod>] |
| 87 | + member _.``yield adds each distinct element once`` () = |
| 88 | + let set = frozenSet { |
| 89 | + yield 1 |
| 90 | + yield 2 |
| 91 | + yield 1 |
| 92 | + } |
| 93 | + |
| 94 | + CollectionAssert.AreEqual ([| 1; 2 |], sorted set, "frozenSet must add each distinct element once") |
| 95 | + |
| 96 | + [<TestMethod>] |
| 97 | + member _.``implicit yields add the elements`` () = |
| 98 | + let set = frozenSet { |
| 99 | + 3 |
| 100 | + 1 |
| 101 | + 2 |
| 102 | + } |
| 103 | + |
| 104 | + CollectionAssert.AreEqual ([| 1; 2; 3 |], sorted set, "frozenSet must add the elements of implicit yields") |
| 105 | + |
| 106 | + [<TestMethod>] |
| 107 | + member _.``yield! adds a sequence, an array, a frozen set and another expression`` () = |
| 108 | + let inner = frozenSet { yield 7 } |
| 109 | + |
| 110 | + let set = frozenSet { |
| 111 | + // A list is the sequence that the overload of sequences takes |
| 112 | + yield! [ 1; 2 ] |
| 113 | + yield! [| 3; 4 |] |
| 114 | + yield! FrozenSet.ofArray [| 5; 6; 1 |] |
| 115 | + yield! inner |
| 116 | + } |
| 117 | + |
| 118 | + CollectionAssert.AreEqual ([| 1..7 |], sorted set, "frozenSet must add the elements of yield!") |
| 119 | + |
| 120 | + [<TestMethod; DynamicData(nameof FrozenSetComputationExpressionTests.Rejections)>] |
| 121 | + member _.``yield! and for reject a null input and a default flat list given as a sequence`` (name : string) = |
| 122 | + let struct (paramName, actual) = rejections[name] |
| 123 | + FlatListAssert.RejectsNull ($"frozenSet {{ %s{name} }}", paramName, actual) |
| 124 | + |
| 125 | + [<TestMethod>] |
| 126 | + member _.``yield! and for take a flat list of strings that is not default as a sequence of objects`` () = |
| 127 | + let widened = FlatListOracles.stringsAsObjects (FlatList.ofArray [| "a"; "b" |]) |
| 128 | + |
| 129 | + let set = frozenSet { |
| 130 | + yield! widened |
| 131 | + |
| 132 | + for item in widened do |
| 133 | + yield item |
| 134 | + } |
| 135 | + |
| 136 | + CollectionAssert.AreEquivalent ( |
| 137 | + [| ("a" :> obj); ("b" :> obj) |], |
| 138 | + FrozenSet.toArray set, |
| 139 | + "frozenSet must take the elements of a flat list of strings as objects in yield! and for" |
| 140 | + ) |
| 141 | + |
| 142 | + [<TestMethod>] |
| 143 | + member _.``for adds the elements of the body for each item`` () = |
| 144 | + let set = frozenSet { |
| 145 | + for item in -2 .. 2 do |
| 146 | + yield item * item |
| 147 | + } |
| 148 | + |
| 149 | + CollectionAssert.AreEqual ([| 0; 1; 4 |], sorted set, "frozenSet must run the body of for for each item") |
| 150 | + |
| 151 | + [<TestMethod>] |
| 152 | + member _.``while adds the elements while the guard holds`` () = |
| 153 | + let mutable count = 0 |
| 154 | + |
| 155 | + let set = frozenSet { |
| 156 | + while count < 3 do |
| 157 | + count <- count + 1 |
| 158 | + yield count |
| 159 | + } |
| 160 | + |
| 161 | + CollectionAssert.AreEqual ([| 1; 2; 3 |], sorted set, "frozenSet must run the body of while while the guard holds") |
| 162 | + |
| 163 | + [<TestMethod>] |
| 164 | + member _.``if without else and following elements combine`` () = |
| 165 | + let set = frozenSet { |
| 166 | + if false then |
| 167 | + yield 0 |
| 168 | + |
| 169 | + yield 1 |
| 170 | + |
| 171 | + if true then |
| 172 | + yield 2 |
| 173 | + } |
| 174 | + |
| 175 | + CollectionAssert.AreEqual ([| 1; 2 |], sorted set, "frozenSet must combine conditional and plain elements") |
| 176 | + |
| 177 | + [<TestMethod>] |
| 178 | + member _.``try with adds the elements of the handler after an exception`` () = |
| 179 | + let set = frozenSet { |
| 180 | + try |
| 181 | + yield 1 |
| 182 | + failwith "fails" |
| 183 | + with _ -> |
| 184 | + yield 2 |
| 185 | + } |
| 186 | + |
| 187 | + CollectionAssert.AreEqual ( |
| 188 | + [| 1; 2 |], |
| 189 | + sorted set, |
| 190 | + "frozenSet must keep the elements before the exception and add the handler's" |
| 191 | + ) |
| 192 | + |
| 193 | + [<TestMethod>] |
| 194 | + member _.``try finally runs the compensation`` () = |
| 195 | + let mutable compensated = false |
| 196 | + |
| 197 | + let set = frozenSet { |
| 198 | + try |
| 199 | + yield 1 |
| 200 | + finally |
| 201 | + compensated <- true |
| 202 | + } |
| 203 | + |
| 204 | + Assert.IsTrue (compensated, "frozenSet must run the compensation of try finally") |
| 205 | + CollectionAssert.AreEqual ([| 1 |], sorted set, "frozenSet must keep the elements of the body of try finally") |
| 206 | + |
| 207 | + [<TestMethod>] |
| 208 | + member _.``use disposes the resource after the body`` () = |
| 209 | + let mutable disposed = false |
| 210 | + |
| 211 | + let set = frozenSet { |
| 212 | + use _resource = { |
| 213 | + new IDisposable with |
| 214 | + member _.Dispose () = disposed <- true |
| 215 | + } |
| 216 | + |
| 217 | + yield 1 |
| 218 | + } |
| 219 | + |
| 220 | + Assert.IsTrue (disposed, "frozenSet must dispose the resource of use") |
| 221 | + CollectionAssert.AreEqual ([| 1 |], sorted set, "frozenSet must keep the elements of the body of use") |
| 222 | + |
| 223 | + [<TestMethod>] |
| 224 | + member _.``frozenSet compares with the default comparer`` () = |
| 225 | + let set = frozenSet { |
| 226 | + "apple" |
| 227 | + "APPLE" |
| 228 | + } |
| 229 | + |
| 230 | + Assert.AreSame ( |
| 231 | + EqualityComparer<string>.Default :> IEqualityComparer<string>, |
| 232 | + FrozenSet.comparer set, |
| 233 | + "frozenSet must compare with the default comparer" |
| 234 | + ) |
| 235 | + |
| 236 | + Assert.HasCount (2, set, "frozenSet must tell the case apart") |
| 237 | + |
| 238 | + [<TestMethod>] |
| 239 | + member _.``a builder with a comparer builds frozen sets that compare with it`` () = |
| 240 | + let ignoreCase : IEqualityComparer<string> = StringComparer.OrdinalIgnoreCase |
| 241 | + |
| 242 | + let set = FrozenSetCE ignoreCase { |
| 243 | + "apple" |
| 244 | + "APPLE" |
| 245 | + "Banana" |
| 246 | + } |
| 247 | + |
| 248 | + Assert.AreSame (ignoreCase, FrozenSet.comparer set, "FrozenSetCE must build frozen sets with its comparer") |
| 249 | + |
| 250 | + CollectionAssert.AreEqual ( |
| 251 | + [| "Banana"; "apple" |], |
| 252 | + sorted set, |
| 253 | + "FrozenSetCE must keep the first of the elements equal in its comparer" |
| 254 | + ) |
| 255 | + |
| 256 | + [<TestMethod>] |
| 257 | + member _.``a builder rejects a null comparer when it is created`` () = |
| 258 | + let ex = |
| 259 | + Assert.ThrowsExactly<ArgumentNullException>( |
| 260 | + Action (fun () -> |
| 261 | + FrozenSetCE (Unchecked.defaultof<IEqualityComparer<string>>) |
| 262 | + |> ignore |
| 263 | + ), |
| 264 | + "FrozenSetCE must reject a null comparer" |
| 265 | + ) |
| 266 | + |
| 267 | + Assert.AreEqual<string | null>("comparer", ex.ParamName, "FrozenSetCE must name the parameter comparer") |
0 commit comments