コード例 #1
0
ファイル: Aggregate.cs プロジェクト: zlinst/UniTask
        public async Task Sum(int start, int count)
        {
            {
                var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAsync();

                var ys = Enumerable.Range(start, count).Sum();
                xs.Should().Be(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAsync(x => x * 2);

                var ys = Enumerable.Range(start, count).Sum(x => x * 2);
                xs.Should().Be(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAwaitAsync(x => UniTask.Run(() => x));

                var ys = Enumerable.Range(start, count).Sum(x => x);
                xs.Should().Be(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(start, count).SumAwaitWithCancellationAsync((x, _) => UniTask.Run(() => x));

                var ys = Enumerable.Range(start, count).Sum(x => x);
                xs.Should().Be(ys);
            }
        }
コード例 #2
0
ファイル: Aggregate.cs プロジェクト: zlinst/UniTask
        public async Task Count(int start, int count)
        {
            {
                var xs = await UniTaskAsyncEnumerable.Range(start, count).CountAsync();

                var ys = Enumerable.Range(start, count).Count();
                xs.Should().Be(ys);
            }

            {
                var xs = await UniTaskAsyncEnumerable.Range(start, count).CountAsync(x => x % 2 == 0);

                var ys = Enumerable.Range(start, count).Count(x => x % 2 == 0);
                xs.Should().Be(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(start, count).LongCountAsync();

                var ys = Enumerable.Range(start, count).LongCount();
                xs.Should().Be(ys);
            }

            {
                var xs = await UniTaskAsyncEnumerable.Range(start, count).LongCountAsync(x => x % 2 == 0);

                var ys = Enumerable.Range(start, count).LongCount(x => x % 2 == 0);
                xs.Should().Be(ys);
            }
        }
コード例 #3
0
        public async Task BufferSkip(int rangeCount, int bufferCount, int skipCount)
        {
            var xs = await UniTaskAsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount, skipCount).Select(x => string.Join(",", x)).ToArrayAsync();

            var ys = await AsyncEnumerable.Range(0, rangeCount).Buffer(bufferCount, skipCount).Select(x => string.Join(",", x)).ToArrayAsync();

            xs.Should().Equal(ys);
        }
コード例 #4
0
ファイル: Paging.cs プロジェクト: zhumingchuang/UniTask
        public async Task Skip(int collection, int skipCount)
        {
            var xs = await UniTaskAsyncEnumerable.Range(1, collection).Skip(skipCount).ToArrayAsync();

            var ys = Enumerable.Range(1, collection).Skip(skipCount).ToArray();

            xs.Should().Equal(ys);
        }
コード例 #5
0
ファイル: Paging.cs プロジェクト: zhumingchuang/UniTask
        public async Task TakeLast(int collection, int takeCount)
        {
            var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeLast(takeCount).ToArrayAsync();

            var ys = Enumerable.Range(1, collection).TakeLast(takeCount).ToArray();

            xs.Should().Equal(ys);
        }
コード例 #6
0
        private async UniTaskVoid Start()
        {
            await UniTaskAsyncEnumerable
            .Range(0, 5)
            .ForEachAsync(x => Debug.Log(x));

            Debug.Log("Done");
        }
コード例 #7
0
        public async Task RangeTest(int start, int count)
        {
            var xs = await UniTaskAsyncEnumerable.Range(start, count).ToArrayAsync();

            var ys = Enumerable.Range(start, count).ToArray();

            xs.Should().Equal(ys);
        }
コード例 #8
0
        public async Task ToObservable()
        {
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, 10).ToObservable().ToArray();

                xs.Should().Equal(Enumerable.Range(1, 10));
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, 0).ToObservable().ToArray();

                xs.Should().Equal(Enumerable.Range(1, 0));
            }
        }
コード例 #9
0
 static async UniTask WhereSelect()
 {
     await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10)
                    .SelectAwait(async x =>
     {
         await UniTask.Yield();
         return(x);
     })
                    .Where(x => x % 2 == 0))
     {
         Console.WriteLine(item);
     }
 }
コード例 #10
0
    async UniTaskVoid Update2()
    {
        UnityEngine.Debug.Log("async linq!");

        await UniTaskAsyncEnumerable.Range(1, 10)
        .Where(x => x % 2 == 0)
        .Select(x => x * x)
        .ForEachAsync(x =>
        {
            UnityEngine.Debug.Log(x);
        });

        UnityEngine.Debug.Log("done");
    }
コード例 #11
0
        public static IEnumerable <IUniTaskAsyncEnumerable <int> > Throws(int count = 3)
        {
            yield return(ThrowImmediate());

            yield return(ThrowAfter());

            yield return(ThrowInMoveNext());

            yield return(UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowImmediate()));

            yield return(UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowAfter()));

            yield return(UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowInMoveNext()));
        }
コード例 #12
0
        public async Task CombineLatestLong()
        {
            var a = UniTaskAsyncEnumerable.Range(1, 100000);
            var b = new AsyncReactiveProperty <int>(0);

            var list     = new List <(int, int)>();
            var complete = a.CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));

            b.Value = 1;

            list[0].Should().Be((100000, 1));

            b.Dispose();

            await complete;
        }
コード例 #13
0
ファイル: Paging.cs プロジェクト: zhumingchuang/UniTask
        public async Task TakeWhile(int collection, int skipCount)
        {
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArrayAsync();

                var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();

                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArrayAsync();

                var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();

                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwait(x => UniTask.Run(() => x < skipCount)).ToArrayAsync();

                var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();

                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwait((x, i) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();

                var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();

                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwaitWithCancellation((x, _) => UniTask.Run(() => x < skipCount)).ToArrayAsync();

                var ys = Enumerable.Range(1, collection).TakeWhile(x => x < skipCount).ToArray();

                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, collection).TakeWhileAwaitWithCancellation((x, i, _) => UniTask.Run(() => x < (skipCount - i))).ToArrayAsync();

                var ys = Enumerable.Range(1, collection).TakeWhile((x, i) => x < (skipCount - i)).ToArray();

                xs.Should().Equal(ys);
            }
        }
コード例 #14
0
        public async Task SelectManyException()
        {
            // error + exists
            // exists + error
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.SelectMany(x => UniTaskAsyncEnumerable.Range(0, 1)).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectMany(x => item).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }

            // await

            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectManyAwait(x => UniTask.Run(() => item)).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }

            // with c

            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = UniTaskAsyncEnumerable.Range(0, 1).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => item)).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
        }
コード例 #15
0
        [InlineData(4, 9)] // rightlong
        public async Task Zip(int leftCount, int rightCount)
        {
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).Zip(UniTaskAsyncEnumerable.Range(99, rightCount)).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).ZipAwait(UniTaskAsyncEnumerable.Range(99, rightCount), (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).ZipAwaitWithCancellation(UniTaskAsyncEnumerable.Range(99, rightCount), (x, y, _) => UniTask.Run(() => (x, y))).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).Zip(Enumerable.Range(99, rightCount)).ToArray();
                xs.Should().Equal(ys);
            }
        }
コード例 #16
0
        public async Task ZipException()
        {
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.Zip(UniTaskAsyncEnumerable.Range(1, 10)).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = UniTaskAsyncEnumerable.Range(1, 10).Zip(item).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }

            // a

            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.ZipAwait(UniTaskAsyncEnumerable.Range(1, 10), (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = UniTaskAsyncEnumerable.Range(1, 10).ZipAwait(item, (x, y) => UniTask.Run(() => (x, y))).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }

            // c

            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.ZipAwaitWithCancellation(UniTaskAsyncEnumerable.Range(1, 10), (x, y, c) => UniTask.Run(() => (x, y))).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = UniTaskAsyncEnumerable.Range(1, 10).ZipAwaitWithCancellation(item, (x, y, c) => UniTask.Run(() => (x, y))).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
        }
コード例 #17
0
        public async Task Select(int count)
        {
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, count).Select(x => x * x).ToArrayAsync();

                var ys = Enumerable.Range(1, count).Select(x => x * x).ToArray();
                xs.Should().Equal(ys);

                var zs = await UniTaskAsyncEnumerable.Range(1, count).SelectAwait((x) => UniTask.Run(() => x * x)).ToArrayAsync();

                zs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, count).Select((x, i) => x * x * i).ToArrayAsync();

                var ys = Enumerable.Range(1, count).Select((x, i) => x * x * i).ToArray();
                xs.Should().Equal(ys);

                var zs = await UniTaskAsyncEnumerable.Range(1, count).SelectAwait((x, i) => UniTask.Run(() => x * x * i)).ToArrayAsync();

                zs.Should().Equal(ys);
            }
        }
コード例 #18
0
        public async Task SelectException()
        {
            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.Select(x => UniTaskAsyncEnumerable.Range(0, 1)).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }

            // await

            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.SelectAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }

            // cancel

            foreach (var item in UniTaskTestException.Throws())
            {
                var xs = item.SelectAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(0, 1))).ToArrayAsync();
                await Assert.ThrowsAsync <UniTaskTestException>(async() => await xs);
            }
        }
コード例 #19
0
        [InlineData(9, 9)] // exists + exists
        public async Task SelectMany(int leftCount, int rightCount)
        {
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany(x => UniTaskAsyncEnumerable.Range(99, rightCount * x)).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany((i, x) => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x)).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany(x => UniTaskAsyncEnumerable.Range(99, rightCount * x), (x, y) => x * y).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (x, y) => x * y).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectMany((i, x) => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x), (x, y) => x * y).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (x, y) => x * y).ToArray();
                xs.Should().Equal(ys);
            }

            // await

            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x))).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait((i, x) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x))).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait(x => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x)), (x, y) => UniTask.Run(() => x * y)).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (x, y) => x * y).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwait((i, x) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x)), (x, y) => UniTask.Run(() => x * y)).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (x, y) => x * y).ToArray();
                xs.Should().Equal(ys);
            }

            // with cancel

            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x))).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x)).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((i, x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x))).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x)).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99, rightCount * x)), (x, y, _) => UniTask.Run(() => x * y)).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany(x => Enumerable.Range(99, rightCount * x), (x, y) => x * y).ToArray();
                xs.Should().Equal(ys);
            }
            {
                var xs = await UniTaskAsyncEnumerable.Range(1, leftCount).SelectManyAwaitWithCancellation((i, x, _) => UniTask.Run(() => UniTaskAsyncEnumerable.Range(99 * i, rightCount * x)), (x, y, _) => UniTask.Run(() => x * y)).ToArrayAsync();

                var ys = Enumerable.Range(1, leftCount).SelectMany((i, x) => Enumerable.Range(99 * i, rightCount * x), (x, y) => x * y).ToArray();
                xs.Should().Equal(ys);
            }
        }
コード例 #20
0
        public async Task PariwiseImmediate()
        {
            var xs = await UniTaskAsyncEnumerable.Range(1, 5).Pairwise().ToArrayAsync();

            xs.Should().Equal((1, 2), (2, 3), (3, 4), (4, 5));
        }