|
| 1 | +module TaskSeq.Tests.Reduce |
| 2 | + |
| 3 | +open Xunit |
| 4 | +open FsUnit.Xunit |
| 5 | + |
| 6 | +open FSharp.Control |
| 7 | + |
| 8 | +// |
| 9 | +// TaskSeq.reduce |
| 10 | +// TaskSeq.reduceAsync |
| 11 | +// |
| 12 | + |
| 13 | +module EmptySeq = |
| 14 | + [<Fact>] |
| 15 | + let ``Null source is invalid`` () = |
| 16 | + assertNullArg |
| 17 | + <| fun () -> TaskSeq.reduce (fun a _ -> a) null |
| 18 | + |
| 19 | + assertNullArg |
| 20 | + <| fun () -> TaskSeq.reduceAsync (fun a _ -> Task.fromResult a) null |
| 21 | + |
| 22 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 23 | + let ``TaskSeq-reduce raises on empty`` variant = |
| 24 | + fun () -> |
| 25 | + Gen.getEmptyVariant variant |
| 26 | + |> TaskSeq.reduce (fun a b -> a + b) |
| 27 | + |> Task.ignore |
| 28 | + |
| 29 | + |> should throwAsyncExact typeof<System.ArgumentException> |
| 30 | + |
| 31 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 32 | + let ``TaskSeq-reduceAsync raises on empty`` variant = |
| 33 | + fun () -> |
| 34 | + Gen.getEmptyVariant variant |
| 35 | + |> TaskSeq.reduceAsync (fun a b -> task { return a + b }) |
| 36 | + |> Task.ignore |
| 37 | + |
| 38 | + |> should throwAsyncExact typeof<System.ArgumentException> |
| 39 | + |
| 40 | +module Immutable = |
| 41 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 42 | + let ``TaskSeq-reduce folds from first element`` variant = task { |
| 43 | + // items are 1..10; sum = 55 |
| 44 | + let! sum = |
| 45 | + Gen.getSeqImmutable variant |
| 46 | + |> TaskSeq.reduce (fun acc item -> acc + item) |
| 47 | + |
| 48 | + sum |> should equal 55 |
| 49 | + } |
| 50 | + |
| 51 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 52 | + let ``TaskSeq-reduceAsync folds from first element`` variant = task { |
| 53 | + let! sum = |
| 54 | + Gen.getSeqImmutable variant |
| 55 | + |> TaskSeq.reduceAsync (fun acc item -> task { return acc + item }) |
| 56 | + |
| 57 | + sum |> should equal 55 |
| 58 | + } |
| 59 | + |
| 60 | + [<Fact>] |
| 61 | + let ``TaskSeq-reduce returns single element without calling folder`` () = task { |
| 62 | + let mutable called = false |
| 63 | + |
| 64 | + let! result = |
| 65 | + TaskSeq.singleton 42 |
| 66 | + |> TaskSeq.reduce (fun _ _ -> |
| 67 | + called <- true |
| 68 | + failwith "should not be called") |
| 69 | + |
| 70 | + result |> should equal 42 |
| 71 | + called |> should equal false |
| 72 | + } |
| 73 | + |
| 74 | + [<Fact>] |
| 75 | + let ``TaskSeq-reduceAsync returns single element without calling folder`` () = task { |
| 76 | + let mutable called = false |
| 77 | + |
| 78 | + let! result = |
| 79 | + TaskSeq.singleton 42 |
| 80 | + |> TaskSeq.reduceAsync (fun _ _ -> task { |
| 81 | + called <- true |
| 82 | + return failwith "should not be called" |
| 83 | + }) |
| 84 | + |
| 85 | + result |> should equal 42 |
| 86 | + called |> should equal false |
| 87 | + } |
| 88 | + |
| 89 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 90 | + let ``TaskSeq-reduce uses first element as initial accumulator`` variant = task { |
| 91 | + // reduce must use element[0] as initial state; for 1..10 summing gives 55 |
| 92 | + // if it used 0 as initial, sum would also be 55 — but we verify the folder is called n-1 times |
| 93 | + let mutable callCount = 0 |
| 94 | + |
| 95 | + let! sum = |
| 96 | + Gen.getSeqImmutable variant |
| 97 | + |> TaskSeq.reduce (fun acc item -> |
| 98 | + callCount <- callCount + 1 |
| 99 | + acc + item) |
| 100 | + |
| 101 | + sum |> should equal 55 |
| 102 | + callCount |> should equal 9 // 10 elements => 9 reduce calls |
| 103 | + } |
| 104 | + |
| 105 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 106 | + let ``TaskSeq-reduce can concatenate strings`` variant = task { |
| 107 | + // items 1..10 as chars: ABCDEFGHIJ |
| 108 | + let! letters = |
| 109 | + Gen.getSeqImmutable variant |
| 110 | + |> TaskSeq.map (fun i -> string (char (i + 64))) |
| 111 | + |> TaskSeq.reduce (fun acc item -> acc + item) |
| 112 | + |
| 113 | + letters |> should equal "ABCDEFGHIJ" |
| 114 | + } |
| 115 | + |
| 116 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 117 | + let ``TaskSeq-reduceAsync can concatenate strings`` variant = task { |
| 118 | + let! letters = |
| 119 | + Gen.getSeqImmutable variant |
| 120 | + |> TaskSeq.map (fun i -> string (char (i + 64))) |
| 121 | + |> TaskSeq.reduceAsync (fun acc item -> task { return acc + item }) |
| 122 | + |
| 123 | + letters |> should equal "ABCDEFGHIJ" |
| 124 | + } |
| 125 | + |
| 126 | +module SideEffects = |
| 127 | + [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] |
| 128 | + let ``TaskSeq-reduce folds correctly with side-effecting sequences`` variant = task { |
| 129 | + let ts = Gen.getSeqWithSideEffect variant |
| 130 | + |
| 131 | + let! sum = ts |> TaskSeq.reduce (fun acc item -> acc + item) |
| 132 | + |
| 133 | + sum |> should equal 55 |
| 134 | + |
| 135 | + // second enumeration produces next 10 elements: 11..20, sum = 155 |
| 136 | + let! sum2 = ts |> TaskSeq.reduce (fun acc item -> acc + item) |
| 137 | + |
| 138 | + sum2 |> should equal 155 |
| 139 | + } |
| 140 | + |
| 141 | + [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] |
| 142 | + let ``TaskSeq-reduceAsync folds correctly with side-effecting sequences`` variant = task { |
| 143 | + let ts = Gen.getSeqWithSideEffect variant |
| 144 | + |
| 145 | + let! sum = |
| 146 | + ts |
| 147 | + |> TaskSeq.reduceAsync (fun acc item -> task { return acc + item }) |
| 148 | + |
| 149 | + sum |> should equal 55 |
| 150 | + |
| 151 | + let! sum2 = |
| 152 | + ts |
| 153 | + |> TaskSeq.reduceAsync (fun acc item -> task { return acc + item }) |
| 154 | + |
| 155 | + sum2 |> should equal 155 |
| 156 | + } |
0 commit comments