public void TestIndexOfInViews() { var initial = Enumerable.Range(0, 100).Select(x => new KeyValuePair <int, string>(x, x.ToString())).ToList(); var other = Enumerable.Range(100, 100).Select(x => new KeyValuePair <int, string>(x, x.ToString())).ToList(); var testCollection = new ConcurrentObservableDictionary <int, string>(); testCollection.AddRange(initial); var collectionView = testCollection.CollectionView; var keysView = testCollection.Keys; var valuesView = testCollection.Values; // Test the IList implementation because it had a bug IList collectionList = (IList)testCollection.CollectionView; IList keysList = (IList)testCollection.Keys; IList valuesList = (IList)testCollection.Values; foreach (var item in initial) { Assert.IsTrue(testCollection.Contains(item)); Assert.IsTrue(collectionView.Contains(item)); Assert.IsTrue(keysView.Contains(item.Key)); Assert.IsTrue(valuesView.Contains(item.Value)); Assert.IsTrue(collectionList.Contains(item)); Assert.IsTrue(keysList.Contains(item.Key)); Assert.IsTrue(valuesList.Contains(item.Value)); } foreach (var item in other) { Assert.IsFalse(testCollection.Contains(item)); Assert.IsFalse(collectionView.Contains(item)); Assert.IsFalse(keysView.Contains(item.Key)); Assert.IsFalse(valuesView.Contains(item.Value)); Assert.IsFalse(collectionList.Contains(item)); Assert.IsFalse(keysList.Contains(item.Key)); Assert.IsFalse(valuesList.Contains(item.Value)); } for (int i = 0; i < initial.Count; ++i) { Assert.AreEqual(initial[i], collectionView[i]); Assert.AreEqual(initial[i].Key, keysView[i]); Assert.AreEqual(initial[i].Value, valuesView[i]); Assert.AreEqual(initial[i], collectionList[i]); Assert.AreEqual(initial[i].Key, keysList[i]); Assert.AreEqual(initial[i].Value, valuesList[i]); Assert.AreEqual(i, collectionView.IndexOf(initial[i])); Assert.AreEqual(i, keysView.IndexOf(initial[i].Key)); Assert.AreEqual(i, valuesView.IndexOf(initial[i].Value)); Assert.AreEqual(i, collectionList.IndexOf(initial[i])); Assert.AreEqual(i, keysList.IndexOf(initial[i].Key)); Assert.AreEqual(i, valuesList.IndexOf(initial[i].Value)); } }
public void TestManyOperations() { // Create some random, but unique items // Use a fixed seed for consistency in results Random random = new Random(1); HashSet <int> baseItemsSet = new HashSet <int>(); while (baseItemsSet.Count < 1_100_000) { baseItemsSet.Add(random.Next()); } // Create 2 collections, 1 to test, and 1 to compare against var testCollection = new ConcurrentObservableDictionary <string, string>(); var list = new List <KeyValuePair <string, string> >(); // Create 1,000,000 items to add and insert var itemsToAdd = baseItemsSet .Take(1_000_000) .Select(x => Swordfish.NET.Collections.KeyValuePair.Create($"Key {x}", $"Value {x}")) .ToList(); // Create 100,000 items to insert var itemsToInsert = baseItemsSet .Skip(1_000_000) .Take(100_000) .Select(x => Swordfish.NET.Collections.KeyValuePair.Create($"Insert Key {x}", $"Insert Value {x}")) .ToList(); // Create items to remove var itemsToRemove = itemsToInsert .Take(1000) .ToList(); foreach (var item in itemsToAdd) { testCollection.Add(item.Key, item.Value); list.Add(item); } // Check items are equal count Assert.IsTrue(list.Count == testCollection.Count, "Added Items correct count"); // Check items are equal order var allEqualAfterAdd = list .Zip(testCollection, (a, b) => (a.Key == b.Key) && (a.Value == b.Value)) .All(a => a); Assert.IsTrue(allEqualAfterAdd, "Added items correct order"); // Test inserting items int insertIndex = itemsToInsert.Count + 100; foreach (var item in itemsToInsert) { // We have the function but it's there for other reasons testCollection.Insert(insertIndex, item); list.Insert(insertIndex, item); insertIndex--; } // Check items are equal count Assert.IsTrue(list.Count == testCollection.Count, "Items correct count after inserting"); // Check items are equal order var allEqualAfterInsert = list .Zip(testCollection, (a, b) => (a.Key == b.Key) && (a.Value == b.Value)) .All(a => a); Assert.IsTrue(allEqualAfterAdd, "Items correct order after insert"); // Test removing items foreach (var item in itemsToRemove) { testCollection.Remove(item.Key); list.Remove(item); } // Check items are equal count Assert.IsTrue(list.Count == testCollection.Count, "Items correct count after removing"); // Check items are equal order var allEqualAfterRemove = list .Zip(testCollection, (a, b) => (a.Key == b.Key) && (a.Value == b.Value)) .All(a => a); Assert.IsTrue(allEqualAfterRemove, "Items correct order after removing"); // Test contains var containsAll = list .All(kv => testCollection.Contains(kv)); Assert.IsTrue(containsAll, "Contains all the items is true"); var containsNone = itemsToRemove .Any(kv => testCollection.ContainsKey(kv.Key)); Assert.IsFalse(containsNone, "Contains any of the removed items is false"); // Test removing at int removeAtIndex = list.Count - 30; while (removeAtIndex >= 0 && list.Count > 0) { list.RemoveAt(removeAtIndex); testCollection.RemoveAt(removeAtIndex); removeAtIndex -= 30; } // Check items are equal count Assert.IsTrue(list.Count == testCollection.Count, "Items correct count after removing at index"); // Check items are equal order var allEqualAfterRemoveAt = list .Zip(testCollection, (a, b) => (a.Key == b.Key) && (a.Value == b.Value)) .All(a => a); Assert.IsTrue(allEqualAfterRemoveAt, "Items correct order after removing at index"); }