コード例 #1
0
ファイル: AsyncMonadTests.cs プロジェクト: nordril/functional
        public static async Task WhileMAsync2CancelTest()
        {
            var cts = new CancellationTokenSource();

            cts.Cancel();
            var xs  = Task.FromResult(new Finite <int>(0).ToAsyncMonad());
            var ret = (await xs.WhileMAsync(c => c > 0, c => Task.FromResult(new Finite <int>(c + 2).ToAsyncMonad()), cts.Token)).DownF();

            Assert.Empty(ret.Value);

            cts = new CancellationTokenSource();
            cts.CancelAfter(750);
            var body = new Finite <int>(2, 4);

            xs  = Task.FromResult(new Finite <int>(15, 2).ToAsyncMonad());
            ret = (await xs.WhileMAsync(c => c > 0, c => Task.FromResult(body.Mutate(d => d + c).ToAsyncMonad()), cts.Token)).DownF();

            Assert.Equal(new int[] { 17 }, ret.Value);

            cts = new CancellationTokenSource();
            cts.CancelAfter(1250);
            body = new Finite <int>(2, 1);
            xs   = Task.FromResult(new Finite <int>(15, 3).ToAsyncMonad());
            ret  = (await xs.WhileMAsync(c => c > 0, c => Task.FromResult(body.Mutate(d => d + c).ToAsyncMonad()), cts.Token)).DownF();

            Assert.Equal(new int[] { 17, 0 }, ret.Value);
        }
コード例 #2
0
ファイル: AsyncMonadTests.cs プロジェクト: nordril/functional
        public static async Task WhileMAsync2Test()
        {
            var xs  = Task.FromResult(new Finite <int>(0).ToAsyncMonad());
            var ret = (await xs.WhileMAsync(c => c > 0, c => Task.FromResult(new Finite <int>(c + 2).ToAsyncMonad()))).DownF();

            Assert.Empty(ret.Value);

            var body = new Finite <int>(2, 4);

            xs  = Task.FromResult(new Finite <int>(15, 2).ToAsyncMonad());
            ret = (await xs.WhileMAsync(c => c > 0, c => Task.FromResult(body.Mutate(d => d + c).ToAsyncMonad()))).DownF();

            Assert.Equal(new int[] { 17, 32 }, ret.Value);

            body = new Finite <int>(2, 1);
            xs   = Task.FromResult(new Finite <int>(15, 3).ToAsyncMonad());
            ret  = (await xs.WhileMAsync(c => c > 0, c => Task.FromResult(body.Mutate(d => d + c).ToAsyncMonad()))).DownF();

            Assert.Equal(new int[] { 17, 0, 0 }, ret.Value);
        }