Ejemplo n.º 1
0
        public void SynchronisedHashSetTryRemoveRemoveEachUniqueItemOnlyOnceMultiThreaded()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin", "Gandalf", "Aragorn", "Gimli", "Legolas", "Boromir"
            };

            var itemsToRemove = new List <string> {
                "Gandalf", "Aragorn", "Gimli", "Legolas", "Boromir"
            };

            int successfulRemoveCount = 0;

            using (var set = new SynchronisedHashSet <string>(items))
            {
                var status = Parallel.For(0, 10000, i =>
                {
                    if (set.TryRemove(itemsToRemove[i % 5]))
                    {
                        Interlocked.Increment(ref successfulRemoveCount);
                    }
                });

                while (!status.IsCompleted)
                {
                    Thread.Sleep(5);
                }

                Assert.AreEqual(4, set.Count);
                Assert.AreEqual(5, successfulRemoveCount);
            }
        }
Ejemplo n.º 2
0
        public void SynchronisedHashSetTryRemoveRemoveEachUniqueItemOnlyOnce()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin", "Gandalf", "Aragorn", "Gimli", "Legolas", "Boromir"
            };

            var itemsToRemove = new List <string> {
                "Gandalf", "Aragorn", "Gimli", "Legolas", "Boromir"
            };

            int successfulRemoveCount = 0;

            using (var set = new SynchronisedHashSet <string>(items))
            {
                for (int i = 0; i < 10000; i++)
                {
                    if (set.TryRemove(itemsToRemove[i % 5]))
                    {
                        successfulRemoveCount++;
                    }
                }

                Assert.AreEqual(4, set.Count);
                Assert.AreEqual(5, successfulRemoveCount);
            }
        }
Ejemplo n.º 3
0
        public void SynchronsiedHashSetTryRemoveDoesNotRemoveIfUnderlyingSetDoesNotContainItem()
        {
            var collection = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(collection))
            {
                set.TryRemove("Gandalf");

                Assert.AreEqual(4, set.Count);
            }
        }
Ejemplo n.º 4
0
        public void SynchronisedHashSetTryRemoveReturnsFalseIfUnderlyingSetDoesNotContainItem()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(items))
            {
                bool result = set.TryRemove("Gandalf");

                Assert.IsFalse(result);
            }
        }
Ejemplo n.º 5
0
        public void SynchronisedHashSetTryRemoveRemovesIfUnderlyingSetContainsItem()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(items))
            {
                set.TryRemove("Frodo");

                Assert.IsFalse(set.Contains("Frodo"));
                Assert.AreEqual(3, set.Count);
            }
        }