public void Next_NoItems_ThrowException()
        {
            var cycleProvider = new CycleProvider <int>();
            var actual        = Assert.ThrowsException <InvalidOperationException>(() => cycleProvider.Next());

            Assert.AreEqual("Cycle provider has no item", actual.Message);
        }
Exemplo n.º 2
0
        public void Next_Added3Items_AppropriateItemsAreReturned()
        {
            ICycleProvider <string> iCycleProvider = new CycleProvider <string>();
            const string            testedItem1    = "Tested Item 1";
            const string            testedItem2    = "Tested Item 2";
            const string            testedItem3    = "Tested Item 3";

            iCycleProvider.Add(testedItem1);
            iCycleProvider.Add(testedItem2);
            iCycleProvider.Add(testedItem3);

            object actual = iCycleProvider.CurrentItem;

            Assert.AreEqual(testedItem1, actual);

            actual = iCycleProvider.Next();

            Assert.AreEqual(testedItem2, actual);
            Assert.AreEqual(testedItem2, iCycleProvider.CurrentItem);

            actual = iCycleProvider.Next();

            Assert.AreEqual(testedItem3, actual);
            Assert.AreEqual(testedItem3, iCycleProvider.CurrentItem);

            actual = iCycleProvider.Next();

            Assert.AreEqual(testedItem1, actual);
            Assert.AreEqual(testedItem1, iCycleProvider.CurrentItem);
        }
Exemplo n.º 3
0
        private static void Main(string[] args)
        {
            ICycleProvider <int> provider = new CycleProvider <int>()
                                            .Add(1).Add(2).Add(3).Add(4).Add(5).Add(6).Add(7).Add(8)
                                            .Add(9).Add(10).Add(15).Add(4).Add(14).Add(13);

            var t1 = new Task(() =>
            {
                int i = 0;
                while (++i < 10)
                {
                    lock (provider)
                    {
                        Console.ForegroundColor = (ConsoleColor)provider.Next();
                        Console.WriteLine($"Task 1 i={i} | {provider.CurrentItem} {Console.ForegroundColor}");
                    }
                    1.SleepSec();
                }
            });

            var t2 = new Task(() =>
            {
                int i = 0;
                while (++i < 10)
                {
                    lock (provider)
                    {
                        Console.ForegroundColor = (ConsoleColor)provider.Next();
                        Console.WriteLine($"Task 2 i={i} | {provider.CurrentItem} {Console.ForegroundColor}");
                    }
                    3.SleepSec();
                }
            });

            var t3 = new Task(() =>
            {
                int i = 0;
                while (++i < 10)
                {
                    lock (provider)
                    {
                        Console.ForegroundColor = (ConsoleColor)provider.Next();
                        Console.WriteLine($"Task 3 i={i} | {provider.CurrentItem} {Console.ForegroundColor}");
                    }
                    2.SleepSec();
                }
            });

            t1.Start();
            t2.Start();
            t3.Start();

            Task.WaitAll(t1, t2, t3);
            //Console.ReadKey(true);
            //ParallelForechDemo();
        }
        public void Next_FirstUse_ReturnsFirstItem()
        {
            var cycleProvider = new CycleProvider <string>();

            cycleProvider.Add("First Item");

            object actual = cycleProvider.Next();

            Assert.AreEqual("First Item", actual);
        }
Exemplo n.º 5
0
        public void Add_OneItem_CurrentItemIsAddedItem()
        {
            ICycleProvider <string> iCycleProvider = new CycleProvider <string>();
            const string            testedItem     = "Tested Item";

            iCycleProvider.Add(testedItem);

            var actual = iCycleProvider.CurrentItem;

            Assert.AreEqual(testedItem, actual);
        }
        public void Next_ThreeItems_ReturnsLastItem()
        {
            var cycleProvider = new CycleProvider <string>();

            cycleProvider.Add("First Item");
            cycleProvider.Add("Second Item");
            cycleProvider.Add("Third Item");
            cycleProvider.Next();
            cycleProvider.Next();

            object actual = cycleProvider.Next();

            Assert.AreEqual("Third Item", actual);
        }
        public void Next_AfterLastElement_ReturnsFirst()
        {
            var cycleProvider = new CycleProvider <string>();

            cycleProvider.Add("First Item");
            cycleProvider.Add("Second Item");
            cycleProvider.Add("Third Item");

            cycleProvider.Next();
            cycleProvider.Next();
            cycleProvider.Next();
            object actual = cycleProvider.Next();

            Assert.AreEqual("First Item", actual);
        }
        public void Next_LastItem_EventOnLastItemFired()
        {
            const string lastItem         = "Last Item";
            var          actualWasInvoked = false;
            var          cycleProvider    = new CycleProvider <string>();

            cycleProvider.Add("First Item");
            cycleProvider.Add(lastItem);
            cycleProvider.OnLastItem += (s, a) =>
            {
                actualWasInvoked = true;
                Assert.AreEqual(cycleProvider, s);
                Assert.AreEqual(lastItem, a.LastItem);
                Assert.AreEqual(2, a.TotalItems);
            };
            cycleProvider.Next();

            object actual = cycleProvider.Next();

            Assert.AreEqual(true, actualWasInvoked, "Event handler was never invoked");
            Assert.AreEqual(lastItem, actual);
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var @lock = new CycleProvider <ConsoleColor>();

            @lock.Add(ConsoleColor.Red);
            @lock.Add(ConsoleColor.Green);
            @lock.Add(ConsoleColor.Blue);
            @lock.Add(ConsoleColor.Yellow);
            @lock.Add(ConsoleColor.Cyan);
            @lock.Add(ConsoleColor.White);
            //@lock.OnLastItem += (s, a) => { lock (@lock) { Console.Clear(); } };
            @lock.OnLastItem += (s, a) => Console.Clear();

            var t1 = new Task(() =>
            {
                string log;
                int i = 0;
                while (++i < 20)
                {
                    log = 1000.Sleep();
                    lock (@lock)
                    {
                        Console.ForegroundColor = @lock.Next();
                        Console.WriteLine($"▓▓▓ t1 [{i}] => {log} ▓▓▓");
                        Console.Title = log;
                    }
                }
            });
            var t2 = new Task(() =>
            {
                string log;
                int i = 0;
                while (++i < 20)
                {
                    log = 1500.Sleep();
                    lock (@lock)
                    {
                        Console.ForegroundColor = @lock.Next();
                        Console.WriteLine($"███ t2 [{i}] => {log} ███");
                        Console.Title = log;
                    }
                }
            });
            var t3 = new Task(() =>
            {
                string log;
                int i = 0;
                while (++i < 20)
                {
                    log = 2000.Sleep();
                    lock (@lock)
                    {
                        Console.ForegroundColor = @lock.Next();
                        Console.WriteLine($"▒▒▒ t3 [{i}] => {log} ▒▒▒");
                        Console.Title = log;
                    }
                }
            });

            t1.Start();
            t2.Start();
            t3.Start();

            Task.WaitAll(t1, t2, t3);
        }