Example #1
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);
            }
        }
Example #2
0
 public void SynchronisedHashSetIsNotReadOnly()
 {
     using (var set = new SynchronisedHashSet <string>())
     {
         Assert.IsFalse(set.IsReadOnly);
     }
 }
Example #3
0
 public void SynchronisedHashSetInitialisesEmptyByDefault()
 {
     using (var set = new SynchronisedHashSet <string>())
     {
         Assert.AreEqual(0, set.Count);
     }
 }
Example #4
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);
            }
        }
Example #5
0
 public void SynchronisedHashSetInitialises()
 {
     using (var set = new SynchronisedHashSet <string>())
     {
         Assert.IsNotNull(set);
     }
 }
Example #6
0
        public void SynchronisedHashSetTryRemoveRemovesIfSetContainsItem()
        {
            _hashSet = new SynchronisedHashSet <string>(_validWords);

            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);
        }
Example #8
0
        public void SynchronisedHashSetAddsElementsFromCollectionToSet()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(items))
            {
                Assert.AreEqual(items.Count, set.Count);
            }
        }
Example #9
0
        public void SynchronisedHashSetInitialisesWithCollection()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(items))
            {
                Assert.IsNotNull(set);
            }
        }
Example #10
0
        public void SynchronisedHashSetClearRemovesAllItemsFromSet()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(items))
            {
                set.Clear();

                Assert.IsEmpty(set);
            }
        }
Example #11
0
        public void SynchronisedHashSetDoesNotRemoveItemIfUnderlyingSetDoesNotContainItem()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

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

                Assert.AreEqual(4, set.Count);
            }
        }
Example #12
0
        public void SynchronisedHashSetRemoveReturnsTrueIfUnderlyingSetContainedItem()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

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

                Assert.IsTrue(result);
            }
        }
Example #13
0
        public void SynchronisedHashSetContainsReturnsTrueIfItemIsPresentInUnderlyingSet()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

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

                Assert.IsTrue(result);
            }
        }
Example #14
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);
            }
        }
Example #15
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);
            }
        }
Example #16
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);
            }
        }
Example #17
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);
            }
        }
Example #18
0
        public void SynchronisedHashSetSupportsNonGenericIterationForEmptySet()
        {
            using (var set = new SynchronisedHashSet <string>())
            {
                var i = (set as System.Collections.IEnumerable).GetEnumerator();

                Assert.DoesNotThrow(() =>
                {
                    while (i.MoveNext())
                    {
                        //iterate to end of hash-set
                    }
                });
            }
        }
Example #19
0
        public void Setup()
        {
            _validWords = new string[Number];

            using (var reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "words.txt")))
            {
                int    i    = 0;
                string word = null;

                while ((word = reader.ReadLine()) != null &&
                       i < Number)
                {
                    _validWords[i] = (word);
                    i++;
                }
            }
            _hashSet = new SynchronisedHashSet <string>(_validWords);
        }
Example #20
0
        public void SynchronisedHashSetCopyToArrayCopiesItemsToArray()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(items))
            {
                string[] array = new string[4];

                set.CopyTo(array, 0);

                array.ToList().ForEach(i =>
                {
                    Assert.IsNotNull(i);
                });
            }
        }
Example #21
0
        public void SynchronsiedHashSetSupportsNonGenericIteration()
        {
            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(items))
            {
                var i = (set as System.Collections.IEnumerable).GetEnumerator();

                Assert.DoesNotThrow(() =>
                {
                    while (i.MoveNext())
                    {
                        //iterate to end of hash-set
                    }
                });
            }
        }
Example #22
0
        public void SynchronisedHashSetNonGenericIteratesOverAllItems()
        {
            int count = 0;

            var items = new List <string> {
                "Frodo", "Sam", "Merry", "Pippin"
            };

            using (var set = new SynchronisedHashSet <string>(items))
            {
                var i = (set as System.Collections.IEnumerable).GetEnumerator();

                while (i.MoveNext())
                {
                    count++;
                }
                Assert.AreEqual(4, count);
            }
        }