示例#1
0
        public void SynchronsiedHashSetTryAddAddsEachUniqueItemOnlyOnceMultiThreaded()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

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

            int successfulAddCount = 0;

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

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

                Assert.AreEqual(9, set.Count);
                Assert.AreEqual(5, successfulAddCount);
            }
        }
示例#2
0
        public void SynchronisedHashSetTryAddAddsEachUniqueItemsOnlyOnce()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

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

            int successfulAddCount = 0;

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

                Assert.AreEqual(9, set.Count);
                Assert.AreEqual(5, successfulAddCount);
            }
        }
示例#3
0
 public void SynchronisedHashSetRemoveRemovesIfSetContainsItem()
 {
     for (int i = 0; i < _validWords.Length; i++)
     {
         Assert.IsTrue(_hashSet.Remove(_validWords[i]) &&
                       _hashSet.TryAdd(_validWords[i]));
     }
 }
        public void SynchronisedHashSetTryAddUniqueWordToSet()
        {
            var set = new SynchronisedHashSet <string>();

            for (int i = 0; i < _words.Length; i++)
            {
                set.TryAdd(_words[i]);
            }

            Assert.AreEqual(Number, set.Count);
        }
示例#5
0
        public void SynchronsiedHashSetTryAddReturnsFalseIfUnderlyingSetDoesContainItem()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

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

                Assert.IsFalse(result);
            }
        }
示例#6
0
        public void SynchronisedHashSetTryAddDoesNotAddIfUnderlyingSetContainsItem()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

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

                Assert.IsTrue(set.Contains("Sam"));
                Assert.AreEqual(4, set.Count);
            }
        }