@@ -3046,3 +3046,98 @@ let ``AsyncSeq.tryExactlyOne returns None for sequence with more than one elemen
30463046 let result = AsyncSeq.tryExactlyOne source |> Async.RunSynchronously
30473047 Assert.AreEqual( None, result)
30483048 } |> Async.RunSynchronously
3049+
3050+ // ===== head =====
3051+
3052+ [<Test>]
3053+ let ``AsyncSeq.head returns first element`` () =
3054+ let source = asyncSeq { yield 42 ; yield 99 }
3055+ let result = AsyncSeq.head source |> Async.RunSynchronously
3056+ Assert.AreEqual( 42 , result)
3057+
3058+ [<Test>]
3059+ let ``AsyncSeq.head raises on empty sequence`` () =
3060+ Assert.Throws< InvalidOperationException>( fun () ->
3061+ AsyncSeq.head AsyncSeq.empty< int> |> Async.RunSynchronously |> ignore) |> ignore
3062+
3063+ // ===== iteri =====
3064+
3065+ [<Test>]
3066+ let ``AsyncSeq.iteri calls action with correct indices`` () =
3067+ let indices = System.Collections.Generic.List< int>()
3068+ let values = System.Collections.Generic.List< int>()
3069+ asyncSeq { yield 10 ; yield 20 ; yield 30 }
3070+ |> AsyncSeq.iteri ( fun i v -> indices.Add( i); values.Add( v))
3071+ |> Async.RunSynchronously
3072+ Assert.AreEqual([| 0 ; 1 ; 2 |], indices |> Seq.toArray)
3073+ Assert.AreEqual([| 10 ; 20 ; 30 |], values |> Seq.toArray)
3074+
3075+ [<Test>]
3076+ let ``AsyncSeq.iteri on empty sequence does nothing`` () =
3077+ let mutable count = 0
3078+ AsyncSeq.empty< int>
3079+ |> AsyncSeq.iteri ( fun _ _ -> count <- count + 1 )
3080+ |> Async.RunSynchronously
3081+ Assert.AreEqual( 0 , count)
3082+
3083+ // ===== find / tryFindAsync =====
3084+
3085+ [<Test>]
3086+ let ``AsyncSeq.find returns matching element`` () =
3087+ for i in 0 .. 10 do
3088+ let ls = [ 1 .. i + 1 ]
3089+ let result = AsyncSeq.ofSeq ls |> AsyncSeq.find ( fun x -> x = i + 1 ) |> Async.RunSynchronously
3090+ Assert.AreEqual( i + 1 , result)
3091+
3092+ [<Test>]
3093+ let ``AsyncSeq.find raises KeyNotFoundException when no match`` () =
3094+ Assert.Throws< System.Collections.Generic.KeyNotFoundException>( fun () ->
3095+ AsyncSeq.ofSeq [ 1 ; 2 ; 3 ] |> AsyncSeq.find ( fun x -> x = 99 ) |> Async.RunSynchronously |> ignore)
3096+ |> ignore
3097+
3098+ [<Test>]
3099+ let ``AsyncSeq.tryFindAsync returns Some when found`` () =
3100+ for i in 0 .. 10 do
3101+ let ls = [ 1 .. i + 1 ]
3102+ let result =
3103+ AsyncSeq.ofSeq ls
3104+ |> AsyncSeq.tryFindAsync ( fun x -> async { return x = i + 1 })
3105+ |> Async.RunSynchronously
3106+ Assert.AreEqual( Some ( i + 1 ), result)
3107+
3108+ [<Test>]
3109+ let ``AsyncSeq.tryFindAsync returns None when not found`` () =
3110+ let result =
3111+ AsyncSeq.ofSeq [ 1 ; 2 ; 3 ]
3112+ |> AsyncSeq.tryFindAsync ( fun x -> async { return x = 99 })
3113+ |> Async.RunSynchronously
3114+ Assert.AreEqual( None, result)
3115+
3116+ // ===== tail =====
3117+
3118+ [<Test>]
3119+ let ``AsyncSeq.tail skips first element`` () =
3120+ let result =
3121+ asyncSeq { yield 1 ; yield 2 ; yield 3 }
3122+ |> AsyncSeq.tail
3123+ |> AsyncSeq.toListAsync
3124+ |> Async.RunSynchronously
3125+ Assert.AreEqual([ 2 ; 3 ], result)
3126+
3127+ [<Test>]
3128+ let ``AsyncSeq.tail on singleton returns empty`` () =
3129+ let result =
3130+ asyncSeq { yield 42 }
3131+ |> AsyncSeq.tail
3132+ |> AsyncSeq.toListAsync
3133+ |> Async.RunSynchronously
3134+ Assert.AreEqual([], result)
3135+
3136+ [<Test>]
3137+ let ``AsyncSeq.tail on empty returns empty`` () =
3138+ let result =
3139+ AsyncSeq.empty< int>
3140+ |> AsyncSeq.tail
3141+ |> AsyncSeq.toListAsync
3142+ |> Async.RunSynchronously
3143+ Assert.AreEqual([], result)
0 commit comments