@@ -2113,6 +2113,135 @@ let ``Seq.ofAsyncSeq with exception should propagate``() =
21132113
21142114#endif
21152115
2116+ [<Test>]
2117+ let ``AsyncSeq.fold with empty sequence should return seed`` () =
2118+ let result = AsyncSeq.empty
2119+ |> AsyncSeq.fold (+) 10
2120+ |> Async.RunSynchronously
2121+ Assert.AreEqual( 10 , result)
2122+
2123+ [<Test>]
2124+ let ``AsyncSeq.ofSeq should work with large sequence`` () =
2125+ let largeSeq = seq { 1 .. 1000 }
2126+ let asyncSeq = AsyncSeq.ofSeq largeSeq
2127+ let result = asyncSeq |> AsyncSeq.toListAsync |> Async.RunSynchronously
2128+ Assert.AreEqual( 1000 , result.Length)
2129+ Assert.AreEqual( 1 , result.[ 0 ])
2130+ Assert.AreEqual( 1000 , result.[ 999 ])
2131+
2132+ [<Test>]
2133+ let ``AsyncSeq.mapAsync should preserve order with async transformations`` () =
2134+ let data = [ 1 ; 2 ; 3 ; 4 ; 5 ] |> AsyncSeq.ofSeq
2135+ let asyncTransform x = async {
2136+ do ! Async.Sleep( 50 - x * 10 ) // Shorter sleep for larger numbers
2137+ return x * 2
2138+ }
2139+
2140+ let result = data
2141+ |> AsyncSeq.mapAsync asyncTransform
2142+ |> AsyncSeq.toListAsync
2143+ |> Async.RunSynchronously
2144+ Assert.AreEqual([ 2 ; 4 ; 6 ; 8 ; 10 ], result)
2145+
2146+ [<Test>]
2147+ let ``AsyncSeq.mapAsync should propagate exceptions`` () =
2148+ let data = [ 1 ; 2 ; 3 ] |> AsyncSeq.ofSeq
2149+ let asyncTransform x = async {
2150+ if x = 2 then failwith " test error"
2151+ return x * 2
2152+ }
2153+
2154+ try
2155+ data
2156+ |> AsyncSeq.mapAsync asyncTransform
2157+ |> AsyncSeq.toListAsync
2158+ |> Async.RunSynchronously
2159+ |> ignore
2160+ Assert.Fail( " Expected exception to be thrown" )
2161+ with
2162+ | ex when ex.Message = " test error" -> () // Expected
2163+ | ex -> Assert.Fail( $" Unexpected exception: {ex.Message}" )
2164+
2165+ [<Test>]
2166+ let ``AsyncSeq.chooseAsync should filter and transform`` () =
2167+ let data = [ 1 ; 2 ; 3 ; 4 ; 5 ] |> AsyncSeq.ofSeq
2168+ let asyncChoose x = async {
2169+ if x % 2 = 0 then return Some ( x * 10 )
2170+ else return None
2171+ }
2172+
2173+ let result = data
2174+ |> AsyncSeq.chooseAsync asyncChoose
2175+ |> AsyncSeq.toListAsync
2176+ |> Async.RunSynchronously
2177+ Assert.AreEqual([ 20 ; 40 ], result)
2178+
2179+ [<Test>]
2180+ let ``AsyncSeq.filterAsync should work with async predicates`` () =
2181+ let data = [ 1 ; 2 ; 3 ; 4 ; 5 ] |> AsyncSeq.ofSeq
2182+ let asyncPredicate x = async {
2183+ do ! Async.Sleep( 1 )
2184+ return x % 2 = 1
2185+ }
2186+
2187+ let result = data
2188+ |> AsyncSeq.filterAsync asyncPredicate
2189+ |> AsyncSeq.toListAsync
2190+ |> Async.RunSynchronously
2191+ Assert.AreEqual([ 1 ; 3 ; 5 ], result)
2192+
2193+ [<Test>]
2194+ let ``AsyncSeq.scan should work with accumulator`` () =
2195+ let data = [ 1 ; 2 ; 3 ; 4 ] |> AsyncSeq.ofSeq
2196+ let result = data
2197+ |> AsyncSeq.scan (+) 0
2198+ |> AsyncSeq.toListAsync
2199+ |> Async.RunSynchronously
2200+ Assert.AreEqual([ 0 ; 1 ; 3 ; 6 ; 10 ], result)
2201+
2202+ [<Test>]
2203+ let ``AsyncSeq.scanAsync should work with async accumulator`` () =
2204+ let data = [ 1 ; 2 ; 3 ] |> AsyncSeq.ofSeq
2205+ let asyncFolder acc x = async {
2206+ do ! Async.Sleep( 1 )
2207+ return acc + x
2208+ }
2209+ let result = data
2210+ |> AsyncSeq.scanAsync asyncFolder 0
2211+ |> AsyncSeq.toListAsync
2212+ |> Async.RunSynchronously
2213+ Assert.AreEqual([ 0 ; 1 ; 3 ; 6 ], result)
2214+
2215+ [<Test>]
2216+ let ``AsyncSeq.threadStateAsync should maintain state correctly`` () =
2217+ let data = [ 1 ; 2 ; 3 ; 4 ] |> AsyncSeq.ofSeq
2218+ let statefulFolder state x = async {
2219+ let newState = state + 1
2220+ let output = x * newState
2221+ return ( output, newState)
2222+ }
2223+
2224+ let result = data
2225+ |> AsyncSeq.threadStateAsync statefulFolder 0
2226+ |> AsyncSeq.toListAsync
2227+ |> Async.RunSynchronously
2228+ Assert.AreEqual([ 1 ; 4 ; 9 ; 16 ], result)
2229+
2230+ [<Test>]
2231+ let ``AsyncSeq.lastOrDefault should return default for empty sequence`` () =
2232+ let result = AsyncSeq.empty
2233+ |> AsyncSeq.lastOrDefault 999
2234+ |> Async.RunSynchronously
2235+ Assert.AreEqual( 999 , result)
2236+
2237+ [<Test>]
2238+ let ``AsyncSeq.lastOrDefault should return last element`` () =
2239+ let data = [ 1 ; 2 ; 3 ; 4 ; 5 ] |> AsyncSeq.ofSeq
2240+ let result = data
2241+ |> AsyncSeq.lastOrDefault 999
2242+ |> Async.RunSynchronously
2243+ Assert.AreEqual( 5 , result)
2244+
21162245// ----------------------------------------------------------------------------
21172246// Additional Coverage Tests targeting uncovered edge cases and branches
21182247
@@ -2224,3 +2353,4 @@ let ``AsyncSeq.chooseAsync with async transformation should work`` () =
22242353 Assert.AreEqual([ 6 ; 12 ], result)
22252354 } |> Async.RunSynchronously
22262355
2356+
0 commit comments