|
| 1 | +module TaskSeq.Tests.Forall |
| 2 | + |
| 3 | +open Xunit |
| 4 | +open FsUnit.Xunit |
| 5 | + |
| 6 | +open FSharp.Control |
| 7 | + |
| 8 | +// |
| 9 | +// TaskSeq.forall |
| 10 | +// TaskSeq.forallAsyncc |
| 11 | +// |
| 12 | + |
| 13 | +module EmptySeq = |
| 14 | + [<Fact>] |
| 15 | + let ``Null source is invalid`` () = |
| 16 | + assertNullArg |
| 17 | + <| fun () -> TaskSeq.forall (fun _ -> false) null |
| 18 | + |
| 19 | + assertNullArg |
| 20 | + <| fun () -> TaskSeq.forallAsync (fun _ -> Task.fromResult false) null |
| 21 | + |
| 22 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 23 | + let ``TaskSeq-forall always returns true`` variant = |
| 24 | + Gen.getEmptyVariant variant |
| 25 | + |> TaskSeq.forall ((=) 12) |
| 26 | + |> Task.map (should be True) |
| 27 | + |
| 28 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 29 | + let ``TaskSeq-forallAsync always returns true`` variant = |
| 30 | + Gen.getEmptyVariant variant |
| 31 | + |> TaskSeq.forallAsync (fun x -> task { return x = 12 }) |
| 32 | + |> Task.map (should be True) |
| 33 | + |
| 34 | +module Immutable = |
| 35 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 36 | + let ``TaskSeq-forall sad path returns false`` variant = task { |
| 37 | + do! |
| 38 | + Gen.getSeqImmutable variant |
| 39 | + |> TaskSeq.forall ((=) 0) |
| 40 | + |> Task.map (should be False) |
| 41 | + |
| 42 | + do! |
| 43 | + Gen.getSeqImmutable variant |
| 44 | + |> TaskSeq.forall ((>) 9) // lt |
| 45 | + |> Task.map (should be False) |
| 46 | + } |
| 47 | + |
| 48 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 49 | + let ``TaskSeq-forallAsync sad path returns false`` variant = task { |
| 50 | + do! |
| 51 | + Gen.getSeqImmutable variant |
| 52 | + |> TaskSeq.forallAsync (fun x -> task { return x = 0 }) |
| 53 | + |> Task.map (should be False) |
| 54 | + |
| 55 | + do! |
| 56 | + Gen.getSeqImmutable variant |
| 57 | + |> TaskSeq.forallAsync (fun x -> task { return x < 9 }) |
| 58 | + |> Task.map (should be False) |
| 59 | + } |
| 60 | + |
| 61 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 62 | + let ``TaskSeq-forall happy path whole seq true`` variant = |
| 63 | + Gen.getSeqImmutable variant |
| 64 | + |> TaskSeq.forall (fun x -> x < 6 || x > 5) |
| 65 | + |> Task.map (should be True) |
| 66 | + |
| 67 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 68 | + let ``TaskSeq-forallAsync happy path whole seq true`` variant = |
| 69 | + Gen.getSeqImmutable variant |
| 70 | + |> TaskSeq.forallAsync (fun x -> task { return x <= 10 && x >= 0 }) |
| 71 | + |> Task.map (should be True) |
| 72 | + |
| 73 | +module SideEffects = |
| 74 | + [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] |
| 75 | + let ``TaskSeq-forall mutated state can change result`` variant = task { |
| 76 | + let ts = Gen.getSeqWithSideEffect variant |
| 77 | + let predicate x = x > 10 |
| 78 | + |
| 79 | + // first: false |
| 80 | + let! found = TaskSeq.forall predicate ts |
| 81 | + found |> should be False // fails on first item, not many side effects yet |
| 82 | + |
| 83 | + // ensure side effects executes |
| 84 | + do! consumeTaskSeq ts |
| 85 | + |
| 86 | + // find again: found now, because of side effects |
| 87 | + let! found = TaskSeq.forall predicate ts |
| 88 | + found |> should be True |
| 89 | + |
| 90 | + // find once more, still true, as numbers increase |
| 91 | + do! consumeTaskSeq ts // ensure side effects executes |
| 92 | + let! found = TaskSeq.forall predicate ts |
| 93 | + found |> should be True |
| 94 | + } |
| 95 | + |
| 96 | + [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] |
| 97 | + let ``TaskSeq-forallAsync mutated state can change result`` variant = task { |
| 98 | + let ts = Gen.getSeqWithSideEffect variant |
| 99 | + let predicate x = Task.fromResult (x > 10) |
| 100 | + |
| 101 | + // first: false |
| 102 | + let! found = TaskSeq.forallAsync predicate ts |
| 103 | + found |> should be False // fails on first item, not many side effects yet |
| 104 | + |
| 105 | + // ensure side effects executes |
| 106 | + do! consumeTaskSeq ts |
| 107 | + |
| 108 | + // find again: found now, because of side effects |
| 109 | + let! found = TaskSeq.forallAsync predicate ts |
| 110 | + found |> should be True |
| 111 | + |
| 112 | + // find once more, still true, as numbers increase |
| 113 | + do! consumeTaskSeq ts // ensure side effects executes |
| 114 | + let! found = TaskSeq.forallAsync predicate ts |
| 115 | + found |> should be True |
| 116 | + } |
| 117 | + |
| 118 | + [<Fact>] |
| 119 | + let ``TaskSeq-forall _specialcase_ prove we don't read past the first failing item`` () = task { |
| 120 | + let mutable i = 0 |
| 121 | + |
| 122 | + let ts = taskSeq { |
| 123 | + for _ in 0..9 do |
| 124 | + i <- i + 1 |
| 125 | + yield i |
| 126 | + } |
| 127 | + |
| 128 | + let! found = ts |> TaskSeq.forall ((>) 3) |
| 129 | + found |> should be False |
| 130 | + i |> should equal 3 // only partial evaluation! |
| 131 | + |
| 132 | + // find next item. We do get a new iterator, but mutable state is now starting at '3', so first item now returned is '4'. |
| 133 | + let! found = ts |> TaskSeq.forall ((<=) 4) |
| 134 | + found |> should be True |
| 135 | + i |> should equal 13 // we evaluated to the end |
| 136 | + } |
| 137 | + |
| 138 | + [<Fact>] |
| 139 | + let ``TaskSeq-forallAsync _specialcase_ prove we don't read past the first failing item`` () = task { |
| 140 | + let mutable i = 0 |
| 141 | + |
| 142 | + let ts = taskSeq { |
| 143 | + for _ in 0..9 do |
| 144 | + i <- i + 1 |
| 145 | + yield i |
| 146 | + } |
| 147 | + |
| 148 | + let! found = ts |> TaskSeq.forallAsync (fun x -> Task.fromResult (x < 3)) |
| 149 | + found |> should be False |
| 150 | + i |> should equal 3 // only partial evaluation! |
| 151 | + |
| 152 | + // find next item. We do get a new iterator, but mutable state is now starting at '3', so first item now returned is '4'. |
| 153 | + let! found = |
| 154 | + ts |
| 155 | + |> TaskSeq.forallAsync (fun x -> Task.fromResult (x >= 4)) |
| 156 | + |
| 157 | + found |> should be True |
| 158 | + i |> should equal 13 // we evaluated to the end |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + [<Fact>] |
| 163 | + let ``TaskSeq-forall _specialcase_ prove statement after first false result is not evaluated`` () = task { |
| 164 | + let mutable i = 0 |
| 165 | + |
| 166 | + let ts = taskSeq { |
| 167 | + for _ in 0..9 do |
| 168 | + yield i |
| 169 | + i <- i + 1 |
| 170 | + } |
| 171 | + |
| 172 | + let! found = ts |> TaskSeq.forall ((>) 0) |
| 173 | + found |> should be False |
| 174 | + i |> should equal 0 // notice that it should be one higher if the statement after 'yield' was evaluated |
| 175 | + |
| 176 | + // find some next item. We do get a new iterator, but mutable state is still starting at '0' |
| 177 | + let! found = ts |> TaskSeq.forall ((>) 4) |
| 178 | + found |> should be False |
| 179 | + i |> should equal 4 // only partial evaluation! |
| 180 | + } |
| 181 | + |
| 182 | + [<Fact>] |
| 183 | + let ``TaskSeq-forallAsync _specialcase_ prove statement after first false result is not evaluated`` () = task { |
| 184 | + let mutable i = 0 |
| 185 | + |
| 186 | + let ts = taskSeq { |
| 187 | + for _ in 0..9 do |
| 188 | + yield i |
| 189 | + i <- i + 1 |
| 190 | + } |
| 191 | + |
| 192 | + let! found = ts |> TaskSeq.forallAsync (fun x -> Task.fromResult (x < 0)) |
| 193 | + found |> should be False |
| 194 | + i |> should equal 0 // notice that it should be one higher if the statement after 'yield' was evaluated |
| 195 | + |
| 196 | + // find some next item. We do get a new iterator, but mutable state is still starting at '0' |
| 197 | + let! found = ts |> TaskSeq.forallAsync (fun x -> Task.fromResult (x < 4)) |
| 198 | + found |> should be False |
| 199 | + i |> should equal 4 // only partial evaluation! |
| 200 | + } |
0 commit comments