public void ConcurrentObservableSortedDictionarySerializationTest() { var serializer = new BinaryFormatter(); var stream = new MemoryStream(); var collection = new ConcurrentObservableSortedDictionary <string, int>(); for (int i = 0; i < 10; i++) { collection.Add("TestItem" + (i + 1).ToString(), i); } serializer.Serialize(stream, collection); stream.Position = 0; collection = serializer.Deserialize(stream) as ConcurrentObservableSortedDictionary <string, int>; for (int i = 0; i < 10; i++) { Assert.AreEqual(i, collection["TestItem" + (i + 1).ToString()]); } }
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 ConcurrentObservableSortedDictionary <string, string>(); var sortedDictionary = new SortedDictionary <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) { sortedDictionary.Add(item.Key, item.Value); testCollection.Add(item.Key, item.Value); } // Check items are equal count Assert.IsTrue(sortedDictionary.Count == testCollection.Count, "Added Items correct count"); // Check items are equal order var allEqualAfterAdd = sortedDictionary .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) { // Naturally sorted dictionary doesn't support insert at sortedDictionary.Add(item.Key, item.Value); // We have the function but it's there for other reasons testCollection.Insert(insertIndex, item); insertIndex--; } // Check items are equal count Assert.IsTrue(sortedDictionary.Count == testCollection.Count, "Items correct count after inserting"); // Check items are equal order var allEqualAfterInsert = sortedDictionary .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) { sortedDictionary.Remove(item.Key); testCollection.Remove(item.Key); } // Check items are equal count Assert.IsTrue(sortedDictionary.Count == testCollection.Count, "Items correct count after removing"); // Check items are equal order var allEqualAfterRemove = sortedDictionary .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 = sortedDictionary .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 var sortedList = new SortedList <string, string>(sortedDictionary); int removeAtIndex = sortedDictionary.Count - 30; while (removeAtIndex >= 0 && sortedDictionary.Count > 0) { sortedList.RemoveAt(removeAtIndex); testCollection.RemoveAt(removeAtIndex); removeAtIndex -= 30; } // Check items are equal count Assert.IsTrue(sortedList.Count == testCollection.Count, "Items correct count after removing at index"); // Check items are equal order var allEqualAfterRemoveAt = sortedList .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"); var list = sortedList .Select(x => x.Key) .ToList(); bool getItemCorrect = true; for (int i = 0; i < list.Count; ++i) { getItemCorrect &= testCollection.GetItem(i).Key == list[i]; } Assert.IsTrue(getItemCorrect, "Get item by index correct"); /* * * [TestMethod] * public void AddRangeTest() * { * Assert.Fail(); * } * * [TestMethod] * public void RemoveTest() * { * Assert.Fail(); * } * * [TestMethod] * public void RemoveRangeTest() * { * Assert.Fail(); * } * * [TestMethod] * public void ToStringTest() * { * Assert.Fail(); * } * * [TestMethod] * public void GetEnumeratorTest() * { * Assert.Fail(); * } * * [TestMethod] * public void ClearTest() * { * Assert.Fail(); * } * * [TestMethod] * public void ContainsTest() * { * Assert.Fail(); * } * * [TestMethod] * public void CopyToTest() * { * Assert.Fail(); * } */ }