コード例 #1
0
        public void Test(int n)
        {
            string r = string.Join("", Enumerable.Range(0, n * 2).Select(i => i % 2 == 0 ? "foo" : "bar"));

            var fooBar = new FooBar(n);

            TextWriter tw = new StringWriter();

            CancellationTokenSource cts     = new CancellationTokenSource(5000);
            ParallelOptions         options = new ParallelOptions {
                CancellationToken = cts.Token
            };

            Parallel.ForEach(Enumerable.Range(0, 2), options, i =>
            {
                if (i % 2 == 0)
                {
                    fooBar.Foo(() => tw.Write("foo"));
                }
                else
                {
                    fooBar.Bar(() => tw.Write("bar"));
                }
            });

            string result = tw.ToString();

            Assert.True(0 == r.CompareTo(result));
        }
コード例 #2
0
        public void TestMethodResolution()
        {
            // 25.1.7 Overloading in generic classes
            // Methods, constructors, indexers, and operators within a generic class declaration can be overloaded.
            // While signatures as declared must be unique, it is possible that substitution of type arguments
            // results in identical signatures.
            // In such a situation, overload resolution will pick the most specific one (14.4.2.2).
            // 14.4.2.2 Better function member
            // If one of MP and MQ is non-generic, but the other is generic, then the non-generic is better.
            var b = new FooBar <int>();

            Assert.AreEqual("Foo(int)", b.Foo(0));
            Assert.AreEqual("Bar(int)", b.Bar(0));
            Assert.AreEqual("Bar2(T)", b.Bar2(0));

            // 14.4.2.2 Better function member
            // Otherwise, if one of MP and MQ is applicable in its non-expanded form (or has no params array) and the
            // other is applicable only in its expanded form (and has a params array),
            // then the non-expanded method is better.
            Assert.AreEqual("Foobar(int)", b.Foobar(0));

            // 14.4.2.2 Better function member
            // Otherwise, if the numbers of parameters K in MP and L in MQ are different,
            // then the method with more parameters is better.
            // This can only occur if both methods have params arrays
            // and are only applicable in their expanded forms.
            Assert.AreEqual("Foobar2(int, params)", b.Foobar2(0));
        }
コード例 #3
0
        public void Display()
        {
            FooBar foo = new FooBar(4);

            Task.Run(() =>
            {
                foo.Bar(() => Console.Write("bar"));
            });
            Task.Run(() =>
            {
                foo.Foo(() => Console.WriteLine("foo"));
            });
        }
コード例 #4
0
        public void Check_FooBar_BaseCase(int n, string result)
        {
            string wholeString = string.Empty;
            var    fooBar      = new FooBar(n);
            var    fooAction   = new Action(() => wholeString = string.Concat(wholeString, "foo"));
            var    barAction   = new Action(() => wholeString = string.Concat(wholeString, "bar"));

            Parallel.Invoke
            (
                () => fooBar.Foo(fooAction),
                () => fooBar.Bar(barAction)
            );

            Assert.AreEqual(result, wholeString);
        }