コード例 #1
0
        public async Task N2()
        {
            var o = new ZeroEvenOdd(2);
            var s = "";

            var tasks = new[]
            {
                Task.Factory.StartNew(() =>
                {
                    o.Even((i) => s += i);
                }),
                Task.Factory.StartNew(() =>
                {
                    o.Zero((i) => s += i);
                }),
                Task.Factory.StartNew(() =>
                {
                    o.Odd((i) => s += i);
                })
            };

            var delay  = Task.Delay(timeout);
            var result = await Task.WhenAny(Task.WhenAll(tasks), delay);

            if (delay == result)
            {
                Assert.Fail("Timeout");
            }

            Assert.AreEqual("0102", s);
        }
コード例 #2
0
        public void DisplayZeroEvenOdd()
        {
            ZeroEvenOdd a = new ZeroEvenOdd(10);

            Task.Run(() => a.Zero(p => Console.WriteLine(p.ToString())));
            Task.Run(() => a.Even(p => Console.WriteLine(p.ToString())));
            Task.Run(() => a.Odd(p => Console.WriteLine(p.ToString())));
        }
コード例 #3
0
        public void RunProblem()
        {
            var oddTemp = new ZeroEvenOdd(2);

            Action a1 = () => oddTemp.Zero((i) => Console.WriteLine($"{i}"));
            Action a2 = () => oddTemp.Even((i) => Console.WriteLine($"{i}"));
            Action a3 = () => oddTemp.Odd((i) => Console.WriteLine($"{i}"));

            Task.Run(a1);
            Task.Run(a2);
            Task.Run(a3);

            Console.ReadKey();
        }
コード例 #4
0
        public void Check_ZeroEvenOdd_BaseCase(int n, string result)
        {
            string wholeString = string.Empty;
            var    zeroEvenOdd = new ZeroEvenOdd(n);
            var    zeroAction  = new Action <int>((x) => wholeString = string.Concat(wholeString, x.ToString()));
            var    evenAction  = new Action <int>((x) => wholeString = string.Concat(wholeString, x.ToString()));
            var    oddAction   = new Action <int>((x) => wholeString = string.Concat(wholeString, x.ToString()));

            Parallel.Invoke
            (
                () => zeroEvenOdd.Zero(zeroAction),
                () => zeroEvenOdd.Even(evenAction),
                () => zeroEvenOdd.Odd(oddAction)
            );

            Assert.AreEqual(result, wholeString);
        }
コード例 #5
0
        public async Task N555()
        {
            var c = 555;
            var o = new ZeroEvenOdd(c);
            var s = "";

            var tasks = new[]
            {
                Task.Factory.StartNew(() =>
                {
                    o.Even((i) =>
                    {
                        s += i;
                    });
                }),
                Task.Factory.StartNew(() =>
                {
                    o.Zero((i) =>
                    {
                        s += i;
                    });
                }),
                Task.Factory.StartNew(() =>
                {
                    o.Odd((i) =>
                    {
                        s += i;
                    });
                })
            };

            var delay  = Task.Delay(timeout);
            var result = await Task.WhenAny(Task.WhenAll(tasks), delay);

            var e = string.Join("", Enumerable.Range(1, c).Select(i => $"0{i}"));

            if (delay == result)
            {
                Assert.Fail("Timeout");
            }

            Assert.AreEqual(e, s);
        }