コード例 #1
0
ファイル: LockFreeStackTests.cs プロジェクト: Miltt/Console
        public void MultiThreadPushTest()
        {
            const int threadCount = 10;
            const int itemCount   = 5;

            var stack = new LockFreeStack <int>();

            var threads = new Thread[threadCount];

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() =>
                {
                    for (int j = 0; j < itemCount; j++)
                    {
                        stack.Push(j);
                    }
                });
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Start();
            }
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            var list          = stack.ToList();
            var expectedCount = threadCount * itemCount;

            Assert.IsTrue(list.Count == expectedCount, "Incorrect number of items added");
        }
コード例 #2
0
        public void New_TryPop_TryPush_x50_TryPop_x50_TryPop_Count()
        {
            int value;
            var q = new LockFreeStack <int>();

            Assert.AreEqual(0, q.Count);

            q.TryPop(out value);
            Assert.AreEqual(0, q.Count);

            for (int i = 0; i < 50; ++i)
            {
                q.TryPush(100 - i);
            }
            Assert.AreEqual(50, q.Count);

            for (int i = 49; i >= 0; --i)
            {
                q.TryPop(out value);
                Assert.AreEqual(100 - i, value);
            }
            Assert.AreEqual(0, q.Count);

            q.TryPop(out value);
            Assert.AreEqual(0, q.Count);
        }
コード例 #3
0
        /// <summary>
        ///Clear 的测试
        ///</summary>
        public void ClearTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target = new LockFreeStack <ValueT>(); // TODO: 初始化为适当的值

            target.Clear();
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
コード例 #4
0
        public void TestPushPop()
        {
            StackBase <string> stack = new LockFreeStack <string>();

            base.TestPushPop(stack);
            Assert.IsTrue(stack.IsEmpty, @"Stack must be empty!");
        }
コード例 #5
0
ファイル: LockFreeStackTests.cs プロジェクト: Miltt/Console
        public void MultiThreadPopTest()
        {
            const int threadCount = 10;
            const int itemCount   = 100;

            var collection = new ConcurrentBag <int>();
            var stack      = new LockFreeStack <int>(Enumerable.Range(0, itemCount));

            var threads = new Thread[threadCount];

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() =>
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if (stack.TryPop(out var item))
                        {
                            collection.Add(item);
                        }
                    }
                });
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Start();
            }
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            Assert.IsTrue(collection.Count == itemCount, "Incorrect number of items");
        }
コード例 #6
0
        /// <summary>
        ///Push 的测试
        ///</summary>
        public void PushTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target = new LockFreeStack <ValueT>(); // TODO: 初始化为适当的值
            ValueT itemT = default(ValueT);                               // TODO: 初始化为适当的值

            target.Push(itemT);
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
コード例 #7
0
        public void New_TryPush_Count()
        {
            var q = new LockFreeStack <int>();

            Assert.AreEqual(0, q.Count);

            q.TryPush(42);
            Assert.AreEqual(1, q.Count);
        }
コード例 #8
0
ファイル: LockFreeStackTests.cs プロジェクト: maximmass/DReAM
        public void New_TryPop_Count()
        {
            int value;
            var q = new LockFreeStack<int>();
            Assert.AreEqual(0, q.Count);

            q.TryPop(out value);
            Assert.AreEqual(0, q.Count);
        }
コード例 #9
0
        /// <summary>
        ///CopyTo 的测试
        ///</summary>
        public void CopyToTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target = new LockFreeStack <ValueT>(); // TODO: 初始化为适当的值

            ValueT[] arrayT      = null;                                  // TODO: 初始化为适当的值
            int      iArrayIndex = 0;                                     // TODO: 初始化为适当的值

            target.CopyTo(arrayT, iArrayIndex);
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
コード例 #10
0
        /// <summary>
        ///GetEnumerator 的测试
        ///</summary>
        public void GetEnumeratorTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target   = new LockFreeStack <ValueT>(); // TODO: 初始化为适当的值
            IEnumerator <ValueT>   expected = null;                         // TODO: 初始化为适当的值
            IEnumerator <ValueT>   actual;

            actual = target.GetEnumerator();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
コード例 #11
0
        /// <summary>
        ///Peek 的测试
        ///</summary>
        public void PeekTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target = new LockFreeStack <ValueT>(); // TODO: 初始化为适当的值
            ValueT expected = default(ValueT);                            // TODO: 初始化为适当的值
            ValueT actual;

            actual = target.Peek();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
コード例 #12
0
        public void New_TryPop_Count()
        {
            int value;
            var q = new LockFreeStack <int>();

            Assert.AreEqual(0, q.Count);

            q.TryPop(out value);
            Assert.AreEqual(0, q.Count);
        }
コード例 #13
0
        /// <summary>
        ///ToArrayAndClear 的测试
        ///</summary>
        public void ToArrayAndClearTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target = new LockFreeStack <ValueT>(); // TODO: 初始化为适当的值

            ValueT[] expected = null;                                     // TODO: 初始化为适当的值
            ValueT[] actual;
            actual = target.ToArrayAndClear();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
コード例 #14
0
ファイル: LockFreeStackTests.cs プロジェクト: Miltt/Console
        public void ClearTest()
        {
            var stack = new LockFreeStack <int>();

            stack.Push(2);
            stack.Clear();

            const int expectedCount = 0;

            Assert.IsTrue(stack.Count == expectedCount, "Incorrect number of items");
        }
コード例 #15
0
        /// <summary>
        ///Contains 的测试
        ///</summary>
        public void ContainsTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target = new LockFreeStack <ValueT>(); // TODO: 初始化为适当的值
            ValueT value    = default(ValueT);                            // TODO: 初始化为适当的值
            bool   expected = false;                                      // TODO: 初始化为适当的值
            bool   actual;

            actual = target.Contains(value);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
コード例 #16
0
ファイル: LockFreeStackTests.cs プロジェクト: Miltt/Console
        public void PushTest()
        {
            var stack = new LockFreeStack <int>();

            stack.Push(2);
            stack.Push(4);

            const int expectedCount = 2;

            Assert.IsTrue(stack.Count == expectedCount, "Incorrect number of items added");
        }
コード例 #17
0
ファイル: LockFreeStackTests.cs プロジェクト: Miltt/Console
        public void PeekTest()
        {
            var stack = new LockFreeStack <int>();

            stack.Push(2);
            stack.TryPeek(out var item);

            const int expectedItem = 2;

            Assert.IsTrue(expectedItem == item, "Incorrect item");
        }
コード例 #18
0
ファイル: LockFreeStackTests.cs プロジェクト: Miltt/Console
        public void PopTest()
        {
            var stack = new LockFreeStack <int>();

            stack.Push(2);
            stack.Push(4);

            stack.TryPop(out var item);

            const int expectedItem = 4;

            Assert.IsTrue(item == expectedItem, "Incorrect item");
        }
コード例 #19
0
ファイル: SocketRecycling.cs プロジェクト: vf1/serversockets
        public SocketRecycling(int maxSocket)
        {
            if (maxSocket > 0)
            {
                isEnabled = true;

                array = new LockFreeItem<Socket>[maxSocket];

                empty = new LockFreeStack<Socket>(array, 0, maxSocket);
                full4 = new LockFreeStack<Socket>(array, -1, -1);
                full6 = new LockFreeStack<Socket>(array, -1, -1);
            }
        }
コード例 #20
0
        //
        //编写测试时,还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///TryPop 的测试
        ///</summary>
        public void TryPopTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target = new LockFreeStack <ValueT>(); // TODO: 初始化为适当的值
            ValueT itemT         = default(ValueT);                       // TODO: 初始化为适当的值
            ValueT itemTExpected = default(ValueT);                       // TODO: 初始化为适当的值
            bool   expected      = false;                                 // TODO: 初始化为适当的值
            bool   actual;

            actual = target.TryPop(out itemT);
            Assert.AreEqual(itemTExpected, itemT);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
コード例 #21
0
ファイル: LockFreeStackTests.cs プロジェクト: Miltt/Console
        public void InitTest()
        {
            var initList = new List <int> {
                1, 2, 3
            };
            var stack = new LockFreeStack <int>(initList);
            var list  = stack.ToList();

            var expectedList = new List <int> {
                3, 2, 1
            };

            Assert.IsTrue(list.SequenceEqual(expectedList), "Two sequences are not equal by comparing the element");
        }
コード例 #22
0
        public void StackNullableInt_Test()
        {
            var stack = new LockFreeStack <int?>();

            Assert.True(stack.Pop() == null);

            stack.Push(1);
            stack.Push(2);
            stack.Push(3);

            Assert.True(stack.Pop() == 3);
            Assert.True(stack.Pop() == 2);
            Assert.True(stack.Pop() == 1);

            Assert.True(stack.Pop() == null);
        }
コード例 #23
0
        public void StackDatetime_Test()
        {
            var stack        = new LockFreeStack <DateTime>();
            var defaultValue = default(DateTime);

            Assert.True(stack.Pop() == defaultValue);

            stack.Push(new DateTime(2000, 1, 1));
            stack.Push(new DateTime(2001, 1, 1));
            stack.Push(new DateTime(2002, 1, 1));

            Assert.True(stack.Pop() == new DateTime(2002, 1, 1));
            Assert.True(stack.Pop() == new DateTime(2001, 1, 1));
            Assert.True(stack.Pop() == new DateTime(2000, 1, 1));

            Assert.True(stack.Pop() == defaultValue);
        }
コード例 #24
0
ファイル: SmartBufferPool.cs プロジェクト: vf1/serversockets
        public SmartBufferPool(int maxMemoryUsageMb, int initialSizeMb, int extraBufferSizeMb)
        {
            InitialMemoryUsage = initialSizeMb * Mb;
            ExtraMemoryUsage = extraBufferSizeMb * Mb;
            MaxBuffersCount = (maxMemoryUsageMb * Mb - InitialMemoryUsage) / ExtraMemoryUsage;
            MaxMemoryUsage = InitialMemoryUsage + ExtraMemoryUsage * MaxBuffersCount;

            array = new LockFreeItem<long>[MaxMemoryUsage / MinSize];

            empty = new LockFreeStack<long>(array, 0, array.Length);

            int count = 0;
            while (MaxSize >> count >= MinSize)
                count++;

            ready = new LockFreeStack<long>[count];
            for (int i = 0; i < ready.Length; i++)
                ready[i] = new LockFreeStack<long>(array, -1, -1);

            buffers = new byte[MaxBuffersCount][];
            buffers[0] = NewBuffer(InitialMemoryUsage);
        }
コード例 #25
0
ファイル: Program.cs プロジェクト: vf1/serversockets
        static void Main(string[] args)
        {
            Console.Write(@"Test Lock-free ");
            if (testObject == TestObject.Pool)
            {
                Console.WriteLine("Pool");
                testPool = true;
            }
            else if (testObject == TestObject.FastPool)
            {
                Console.WriteLine("Fast Pool");
                testPool = true;
            }
            else if (testObject == TestObject.StackQueue)
            {
                Console.WriteLine("Stack & Queue (Not Pool)");
                testPool = false;
            }
            else
                throw new NotImplementedException();

            Console.WriteLine(@"ThreadPool Test Starting...");

            Console.WriteLine(@"  All threads: {0}", threads);
            Console.WriteLine(@"  Buffers per thread: {0}", actions);
            Console.WriteLine(@"  Buffer pool size: {0}", poolSize);

            Console.WriteLine();

            if (testPool)
            {
                if (testObject == TestObject.Pool)
                    pool = new LockFreePool<Item>(poolSize);
                if (testObject == TestObject.FastPool)
                    pool = new LockFreeFastPool<Item>(poolSize);
            }
            else
            {
                array = new LockFreeItem<Item>[poolSize + 1];
                for (int i = 1; i < array.Length; i++)
                    array[i].Value = new Item();

                queue = new LockFreeQueue<Item>(array, 0, poolSize + 1);
                stack = new LockFreeStack<Item>(array, -1, -1);
            }

            items = new bool[65536];

            run = true;
            for (int i = 0; i < threads; i++)
            {
                var thread = new Thread(TestBufferPool);
                thread.Start();
            }

            Console.WriteLine(@"Started. Press any key to stop...");
            Console.ReadKey(true);

            run = false;

            while (count > 0)
                Thread.Sleep(25);
        }
コード例 #26
0
        static void Main(string[] args)
        {
            Console.Write(@"Test Lock-free ");
            if (testObject == TestObject.Pool)
            {
                Console.WriteLine("Pool");
                testPool = true;
            }
            else if (testObject == TestObject.FastPool)
            {
                Console.WriteLine("Fast Pool");
                testPool = true;
            }
            else if (testObject == TestObject.StackQueue)
            {
                Console.WriteLine("Stack & Queue (Not Pool)");
                testPool = false;
            }
            else
            {
                throw new NotImplementedException();
            }

            Console.WriteLine(@"ThreadPool Test Starting...");

            Console.WriteLine(@"  All threads: {0}", threads);
            Console.WriteLine(@"  Buffers per thread: {0}", actions);
            Console.WriteLine(@"  Buffer pool size: {0}", poolSize);

            Console.WriteLine();

            if (testPool)
            {
                if (testObject == TestObject.Pool)
                {
                    pool = new LockFreePool <Item>(poolSize);
                }
                if (testObject == TestObject.FastPool)
                {
                    pool = new LockFreeFastPool <Item>(poolSize);
                }
            }
            else
            {
                array = new LockFreeItem <Item> [poolSize + 1];
                for (int i = 1; i < array.Length; i++)
                {
                    array[i].Value = new Item();
                }

                queue = new LockFreeQueue <Item>(array, 0, poolSize + 1);
                stack = new LockFreeStack <Item>(array, -1, -1);
            }

            items = new bool[65536];

            run = true;
            for (int i = 0; i < threads; i++)
            {
                var thread = new Thread(TestBufferPool);
                thread.Start();
            }

            Console.WriteLine(@"Started. Press any key to stop...");
            Console.ReadKey(true);

            run = false;

            while (count > 0)
            {
                Thread.Sleep(25);
            }
        }
コード例 #27
0
ファイル: Program.cs プロジェクト: antonmisa/ConcurrentStacks
        public void LockFreeStackPushPopW4Threads()
        {
            StackBase <string> stack = new LockFreeStack <string>();

            base.TestPushPop(stack, 4);
        }
コード例 #28
0
ファイル: Program.cs プロジェクト: antonmisa/ConcurrentStacks
        public void LockFreeStackPushW2Threads()
        {
            StackBase <string> stack = new LockFreeStack <string>();

            base.TestPush(stack, 2);
        }
コード例 #29
0
ファイル: LockFreeStackTests.cs プロジェクト: maximmass/DReAM
 public void New_Count()
 {
     var q = new LockFreeStack<int>();
     Assert.AreEqual(0, q.Count);
 }
コード例 #30
0
        public void New_Count()
        {
            var q = new LockFreeStack <int>();

            Assert.AreEqual(0, q.Count);
        }
コード例 #31
0
ファイル: LockFreeStackTests.cs プロジェクト: maximmass/DReAM
        public void New_TryPush_Count()
        {
            var q = new LockFreeStack<int>();
            Assert.AreEqual(0, q.Count);

            q.TryPush(42);
            Assert.AreEqual(1, q.Count);
        }
コード例 #32
0
ファイル: LockFreeStackTests.cs プロジェクト: maximmass/DReAM
        public void New_TryPush_x50_TryPop_x50_TryPop_Count()
        {
            int value;
            var q = new LockFreeStack<int>();
            Assert.AreEqual(0, q.Count);

            for(int i = 0; i < 50; ++i) {
                q.TryPush(100 - i);
            }
            Assert.AreEqual(50, q.Count);

            for(int i = 49; i >= 0; --i) {
                q.TryPop(out value);
                Assert.AreEqual(100 - i, value);
            }
            Assert.AreEqual(0, q.Count);

            q.TryPop(out value);
            Assert.AreEqual(0, q.Count);
        }
コード例 #33
0
 /// <summary> Creates <see cref="ObjectTypePool{T}"/>. </summary>
 /// <param name="factoryMethod">Factory method.</param>
 public ObjectTypePool(Func <T> factoryMethod)
 {
     _objectStack   = new LockFreeStack <T>();
     _factoryMethod = factoryMethod;
 }
コード例 #34
0
        public void TestPush()
        {
            StackBase <string> stack = new LockFreeStack <string>();

            base.TestPush(stack);
        }
コード例 #35
0
        /// <summary>
        ///LockFreeStack`1 构造函数 的测试
        ///</summary>
        public void LockFreeStackConstructorTestHelper <ValueT>()
        {
            LockFreeStack <ValueT> target = new LockFreeStack <ValueT>();

            Assert.Inconclusive("TODO: 实现用来验证目标的代码");
        }
コード例 #36
0
 public static void Main()
 {
     //AsyncCoordinatorDemo.Go();
     LockFreeStack.Go();
 }
コード例 #37
0
 /// <summary> Creates <see cref="ObjectListPool{T}"/>. </summary>
 public ObjectListPool()
 {
     _objectStack = new LockFreeStack <List <T> >();
 }