|
| 1 | +module TaskSeq.Tests.Distinct |
| 2 | + |
| 3 | +open Xunit |
| 4 | +open FsUnit.Xunit |
| 5 | + |
| 6 | +open FSharp.Control |
| 7 | + |
| 8 | +// |
| 9 | +// TaskSeq.distinct |
| 10 | +// TaskSeq.distinctBy |
| 11 | +// TaskSeq.distinctByAsync |
| 12 | +// |
| 13 | + |
| 14 | + |
| 15 | +module EmptySeq = |
| 16 | + [<Fact>] |
| 17 | + let ``TaskSeq-distinct with null source raises`` () = assertNullArg <| fun () -> TaskSeq.distinct null |
| 18 | + |
| 19 | + [<Fact>] |
| 20 | + let ``TaskSeq-distinctBy with null source raises`` () = assertNullArg <| fun () -> TaskSeq.distinctBy id null |
| 21 | + |
| 22 | + [<Fact>] |
| 23 | + let ``TaskSeq-distinctByAsync with null source raises`` () = |
| 24 | + assertNullArg |
| 25 | + <| fun () -> TaskSeq.distinctByAsync (fun x -> Task.fromResult x) null |
| 26 | + |
| 27 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 28 | + let ``TaskSeq-distinct on empty returns empty`` variant = |
| 29 | + Gen.getEmptyVariant variant |
| 30 | + |> TaskSeq.distinct |
| 31 | + |> verifyEmpty |
| 32 | + |
| 33 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 34 | + let ``TaskSeq-distinctBy on empty returns empty`` variant = |
| 35 | + Gen.getEmptyVariant variant |
| 36 | + |> TaskSeq.distinctBy id |
| 37 | + |> verifyEmpty |
| 38 | + |
| 39 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 40 | + let ``TaskSeq-distinctByAsync on empty returns empty`` variant = |
| 41 | + Gen.getEmptyVariant variant |
| 42 | + |> TaskSeq.distinctByAsync (fun x -> Task.fromResult x) |
| 43 | + |> verifyEmpty |
| 44 | + |
| 45 | + |
| 46 | +module Functionality = |
| 47 | + [<Fact>] |
| 48 | + let ``TaskSeq-distinct removes duplicate ints`` () = task { |
| 49 | + let! result = |
| 50 | + taskSeq { yield! [ 1; 2; 2; 3; 1; 4; 3; 5 ] } |
| 51 | + |> TaskSeq.distinct |
| 52 | + |> TaskSeq.toListAsync |
| 53 | + |
| 54 | + result |> should equal [ 1; 2; 3; 4; 5 ] |
| 55 | + } |
| 56 | + |
| 57 | + [<Fact>] |
| 58 | + let ``TaskSeq-distinct removes duplicate strings`` () = task { |
| 59 | + let! result = |
| 60 | + taskSeq { yield! [ "a"; "b"; "b"; "a"; "c" ] } |
| 61 | + |> TaskSeq.distinct |
| 62 | + |> TaskSeq.toListAsync |
| 63 | + |
| 64 | + result |> should equal [ "a"; "b"; "c" ] |
| 65 | + } |
| 66 | + |
| 67 | + [<Fact>] |
| 68 | + let ``TaskSeq-distinct with all identical elements returns singleton`` () = task { |
| 69 | + let! result = |
| 70 | + taskSeq { yield! [ 7; 7; 7; 7; 7 ] } |
| 71 | + |> TaskSeq.distinct |
| 72 | + |> TaskSeq.toListAsync |
| 73 | + |
| 74 | + result |> should equal [ 7 ] |
| 75 | + } |
| 76 | + |
| 77 | + [<Fact>] |
| 78 | + let ``TaskSeq-distinct with all distinct elements returns all`` () = task { |
| 79 | + let! result = |
| 80 | + taskSeq { yield! [ 1..5 ] } |
| 81 | + |> TaskSeq.distinct |
| 82 | + |> TaskSeq.toListAsync |
| 83 | + |
| 84 | + result |> should equal [ 1; 2; 3; 4; 5 ] |
| 85 | + } |
| 86 | + |
| 87 | + [<Fact>] |
| 88 | + let ``TaskSeq-distinct on singleton returns singleton`` () = task { |
| 89 | + let! result = |
| 90 | + taskSeq { yield 42 } |
| 91 | + |> TaskSeq.distinct |
| 92 | + |> TaskSeq.toListAsync |
| 93 | + |
| 94 | + result |> should equal [ 42 ] |
| 95 | + } |
| 96 | + |
| 97 | + [<Fact>] |
| 98 | + let ``TaskSeq-distinct keeps first occurrence, not last`` () = task { |
| 99 | + // sequence [3;1;2;1;3] - first occurrences are at indices 0,1,2 for values 3,1,2 |
| 100 | + let! result = |
| 101 | + taskSeq { yield! [ 3; 1; 2; 1; 3 ] } |
| 102 | + |> TaskSeq.distinct |
| 103 | + |> TaskSeq.toListAsync |
| 104 | + |
| 105 | + result |> should equal [ 3; 1; 2 ] |
| 106 | + } |
| 107 | + |
| 108 | + [<Fact>] |
| 109 | + let ``TaskSeq-distinct is different from distinctUntilChanged`` () = task { |
| 110 | + // [1;2;1] - distinct gives [1;2], distinctUntilChanged gives [1;2;1] |
| 111 | + let! distinct = |
| 112 | + taskSeq { yield! [ 1; 2; 1 ] } |
| 113 | + |> TaskSeq.distinct |
| 114 | + |> TaskSeq.toListAsync |
| 115 | + |
| 116 | + let! distinctUntilChanged = |
| 117 | + taskSeq { yield! [ 1; 2; 1 ] } |
| 118 | + |> TaskSeq.distinctUntilChanged |
| 119 | + |> TaskSeq.toListAsync |
| 120 | + |
| 121 | + distinct |> should equal [ 1; 2 ] |
| 122 | + distinctUntilChanged |> should equal [ 1; 2; 1 ] |
| 123 | + } |
| 124 | + |
| 125 | + [<Fact>] |
| 126 | + let ``TaskSeq-distinctBy removes elements with duplicate projected keys`` () = task { |
| 127 | + let! result = |
| 128 | + taskSeq { yield! [ 1; 2; 3; 4; 5; 6 ] } |
| 129 | + |> TaskSeq.distinctBy (fun x -> x % 3) |
| 130 | + |> TaskSeq.toListAsync |
| 131 | + |
| 132 | + // keys: 1%3=1, 2%3=2, 3%3=0, 4%3=1(dup), 5%3=2(dup), 6%3=0(dup) |
| 133 | + result |> should equal [ 1; 2; 3 ] |
| 134 | + } |
| 135 | + |
| 136 | + [<Fact>] |
| 137 | + let ``TaskSeq-distinctBy with string length as key`` () = task { |
| 138 | + let! result = |
| 139 | + taskSeq { yield! [ "a"; "bb"; "c"; "dd"; "eee" ] } |
| 140 | + |> TaskSeq.distinctBy String.length |
| 141 | + |> TaskSeq.toListAsync |
| 142 | + |
| 143 | + // lengths: 1, 2, 1(dup), 2(dup), 3 |
| 144 | + result |> should equal [ "a"; "bb"; "eee" ] |
| 145 | + } |
| 146 | + |
| 147 | + [<Fact>] |
| 148 | + let ``TaskSeq-distinctBy with identity projection equals distinct`` () = task { |
| 149 | + let input = [ 1; 2; 2; 3; 1; 4 ] |
| 150 | + |
| 151 | + let! byId = |
| 152 | + taskSeq { yield! input } |
| 153 | + |> TaskSeq.distinctBy id |
| 154 | + |> TaskSeq.toListAsync |
| 155 | + |
| 156 | + let! plain = |
| 157 | + taskSeq { yield! input } |
| 158 | + |> TaskSeq.distinct |
| 159 | + |> TaskSeq.toListAsync |
| 160 | + |
| 161 | + byId |> should equal plain |
| 162 | + } |
| 163 | + |
| 164 | + [<Fact>] |
| 165 | + let ``TaskSeq-distinctBy keeps first element with a given key`` () = task { |
| 166 | + let! result = |
| 167 | + taskSeq { yield! [ (1, "a"); (2, "b"); (1, "c") ] } |
| 168 | + |> TaskSeq.distinctBy fst |
| 169 | + |> TaskSeq.toListAsync |
| 170 | + |
| 171 | + result |> should equal [ (1, "a"); (2, "b") ] |
| 172 | + } |
| 173 | + |
| 174 | + [<Fact>] |
| 175 | + let ``TaskSeq-distinctByAsync removes elements with duplicate projected keys`` () = task { |
| 176 | + let! result = |
| 177 | + taskSeq { yield! [ 1; 2; 3; 4; 5; 6 ] } |
| 178 | + |> TaskSeq.distinctByAsync (fun x -> task { return x % 3 }) |
| 179 | + |> TaskSeq.toListAsync |
| 180 | + |
| 181 | + result |> should equal [ 1; 2; 3 ] |
| 182 | + } |
| 183 | + |
| 184 | + [<Fact>] |
| 185 | + let ``TaskSeq-distinctByAsync behaves identically to distinctBy`` () = task { |
| 186 | + let input = [ 1; 2; 2; 3; 1; 4 ] |
| 187 | + let projection x = x % 2 |
| 188 | + |
| 189 | + let! bySync = |
| 190 | + taskSeq { yield! input } |
| 191 | + |> TaskSeq.distinctBy projection |
| 192 | + |> TaskSeq.toListAsync |
| 193 | + |
| 194 | + let! byAsync = |
| 195 | + taskSeq { yield! input } |
| 196 | + |> TaskSeq.distinctByAsync (fun x -> task { return projection x }) |
| 197 | + |> TaskSeq.toListAsync |
| 198 | + |
| 199 | + bySync |> should equal byAsync |
| 200 | + } |
| 201 | + |
| 202 | + [<Fact>] |
| 203 | + let ``TaskSeq-distinct with chars`` () = task { |
| 204 | + let! result = |
| 205 | + taskSeq { yield! [ 'A'; 'A'; 'B'; 'Z'; 'C'; 'C'; 'Z'; 'C'; 'D'; 'D'; 'D'; 'Z' ] } |
| 206 | + |> TaskSeq.distinct |
| 207 | + |> TaskSeq.toListAsync |
| 208 | + |
| 209 | + result |> should equal [ 'A'; 'B'; 'Z'; 'C'; 'D' ] |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | +module SideEffects = |
| 214 | + [<Fact>] |
| 215 | + let ``TaskSeq-distinct evaluates elements lazily`` () = task { |
| 216 | + let mutable sideEffects = 0 |
| 217 | + |
| 218 | + let ts = taskSeq { |
| 219 | + for i in 1..5 do |
| 220 | + sideEffects <- sideEffects + 1 |
| 221 | + yield i |
| 222 | + } |
| 223 | + |
| 224 | + let distinct = ts |> TaskSeq.distinct |
| 225 | + |
| 226 | + // no evaluation yet |
| 227 | + sideEffects |> should equal 0 |
| 228 | + |
| 229 | + let! _ = distinct |> TaskSeq.toListAsync |
| 230 | + |
| 231 | + // only evaluated when consumed |
| 232 | + sideEffects |> should equal 5 |
| 233 | + } |
| 234 | + |
| 235 | + [<Fact>] |
| 236 | + let ``TaskSeq-distinctBy evaluates projection lazily`` () = task { |
| 237 | + let mutable projections = 0 |
| 238 | + |
| 239 | + let! result = |
| 240 | + taskSeq { yield! [ 1; 2; 3; 1; 2 ] } |
| 241 | + |> TaskSeq.distinctBy (fun x -> |
| 242 | + projections <- projections + 1 |
| 243 | + x) |
| 244 | + |> TaskSeq.toListAsync |
| 245 | + |
| 246 | + result |> should equal [ 1; 2; 3 ] |
| 247 | + // projection called once per element (5 elements) |
| 248 | + projections |> should equal 5 |
| 249 | + } |
0 commit comments