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); } }
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); } }
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); }
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); } }
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); } }