public void Test_ConcurrentObservableSortedDictionary_Clear() { var initial = Enumerable.Range(0, 100).Select(x => new KeyValuePair <int, int>(x, x)); var collection = new ConcurrentObservableSortedDictionary <int, int>(); collection.AddRange(initial); Assert.AreEqual(100, collection.Count); // Record all the collection changed events List <(object, NotifyCollectionChangedEventArgs)> returnedList = new List <(object, NotifyCollectionChangedEventArgs)>(); collection.CollectionChanged += (s, e) => returnedList.Add((s, e)); collection.Clear(); // Check just one collection changed event was fired Assert.AreEqual(1, returnedList.Count); (var returnedObject, var returnedArgs) = returnedList[0]; Assert.AreEqual(0, collection.Count); Assert.AreEqual(returnedObject, collection); Assert.AreEqual(NotifyCollectionChangedAction.Remove, returnedArgs.Action); Assert.IsNull(returnedArgs.NewItems); Assert.IsNotNull(returnedArgs.OldItems); Assert.AreEqual(initial.Count(), returnedArgs.OldItems.Count); Assert.IsTrue(initial.Zip(returnedArgs.OldItems.OfType <KeyValuePair <int, int> >(), (a, b) => a.Key == b.Key && a.Value == b.Value).All(c => c)); }
public void Test_ConcurrentObservableSortedDictionary_RemoveRange_ByItems_IEnumerable() { var initial = Enumerable.Range(0, 100).Select(x => new KeyValuePair <int, int>(x, x)); var toRemove = Enumerable.Range(1, 40).Select(x => x * 2); var collection = new ConcurrentObservableSortedDictionary <int, int>(); collection.AddRange(initial); Assert.AreEqual(100, collection.Count); // Record all the collection changed events List <(object, NotifyCollectionChangedEventArgs)> returnedList = new List <(object, NotifyCollectionChangedEventArgs)>(); collection.CollectionChanged += (s, e) => returnedList.Add((s, e)); collection.RemoveRange(toRemove); // Check just one collection changed event was fired Assert.AreEqual(1, returnedList.Count); (var returnedObject, var returnedArgs) = returnedList[0]; Assert.IsNotNull(returnedObject); Assert.IsNotNull(returnedArgs); Assert.AreEqual(returnedObject, collection); Assert.AreEqual(NotifyCollectionChangedAction.Remove, returnedArgs.Action); Assert.AreEqual(-1, returnedArgs.OldStartingIndex); Assert.IsNull(returnedArgs.NewItems); Assert.IsNotNull(returnedArgs.OldItems); Assert.AreEqual(toRemove.Count(), returnedArgs.OldItems.Count); Assert.IsTrue(CollectionsAreEqual(toRemove, returnedArgs.OldItems)); }
private void Button_Click(object sender, RoutedEventArgs e) { Messages.Add("Testing BinaryFormatter..."); var list = new ConcurrentObservableCollection <string>(); var dictionary = new ConcurrentObservableDictionary <string, string>(); var sortedDictionary = new ConcurrentObservableSortedDictionary <string, string>(); InitializeList(list, 20); TestSerializingObject(list); InitializeDictionary(dictionary, 20); TestSerializingObject(dictionary); InitializeDictionary(sortedDictionary, 20); TestSerializingObject(sortedDictionary); Messages.Add("Done"); }
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 RealtimeOrderBookClient(string ProductString, RealtimeOrderBookSubscription realtimeOrderBookSubscription, CBAuthenticationContainer auth = null) { this.ProductString = ProductString; this.productOrderBookClient = new ProductOrderBookClient(auth); Sells = new ConcurrentObservableSortedDictionary <decimal, ObservableLinkedList <BidAskOrder> >(isMultithreaded: true, comparer: new DescendingComparer <decimal>()); Buys = new ConcurrentObservableSortedDictionary <decimal, ObservableLinkedList <BidAskOrder> >(isMultithreaded: true, comparer: new DescendingComparer <decimal>()); this.RealtimeOrderBookSubscription = realtimeOrderBookSubscription; this.RealtimeOrderBookSubscription.RealtimeOpen += OnOpen; this.RealtimeOrderBookSubscription.RealtimeDone += OnDone; this.RealtimeOrderBookSubscription.RealtimeMatch += OnMatch; this.RealtimeOrderBookSubscription.RealtimeChange += OnChange; // not really used for anything except for their sequence number this.RealtimeOrderBookSubscription.RealtimeReceived += OnReceived; this.RealtimeOrderBookSubscription.RealtimeLastMatch += OnLastMatch; this.RealtimeOrderBookSubscription.Heartbeat += OnHeartbeat; this.RealtimeOrderBookSubscription.ConnectionClosed += OnConnectionClosed; }
public void Test_ConcurrentObservableSortedDictionary_AddRange_Dictionary() { var toAdd = Enumerable.Range(0, 100).Select(x => new KeyValuePair <int, int>(x, x)).ToDictionary(x => x.Key, x => x.Value); var collection = new ConcurrentObservableSortedDictionary <int, int>(); // Record all the collection changed events List <(object, NotifyCollectionChangedEventArgs)> returnedList = new List <(object, NotifyCollectionChangedEventArgs)>(); collection.CollectionChanged += (s, e) => returnedList.Add((s, e)); collection.AddRange(toAdd); // Check just one collection changed event was fired Assert.AreEqual(1, returnedList.Count); (var returnedObject, var returnedArgs) = returnedList[0]; Assert.AreEqual(returnedObject, collection); Assert.AreEqual(returnedArgs.Action, NotifyCollectionChangedAction.Add); Assert.IsNotNull(returnedArgs.NewItems); Assert.IsNull(returnedArgs.OldItems); Assert.AreEqual(toAdd.Count(), returnedArgs.NewItems.Count); Assert.IsTrue(CollectionsAreEqual(toAdd, returnedArgs.NewItems)); }
public ConcurrentObservableSortedDictionaryTestViewModel() { TestCollection = new ConcurrentObservableSortedDictionary <string, string>(); NormalDictionary = new SortedDictionary <string, string>(); }
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(); * } */ }
public void AddTest() { int _testCollectionCount = 10; int _itemsPerCollection = 200_000; List <List <int> > _testCollections = new List <List <int> >(); List <int> _sortedCollection = new List <int>(); // Use a fixed seed for consistency in results Random random = new Random(1); // Create 10 test sets for (int collection = 0; collection < _testCollectionCount; ++collection) { List <int> testCollection = new List <int>(); for (int item = 0; item < _itemsPerCollection; ++item) { // Ensure we have some duplicates by picking a random number // less than half the number of items. testCollection.Add(random.Next(_itemsPerCollection / 2)); } _testCollections.Add(testCollection); } _sortedCollection = _testCollections .SelectMany(x => x) .Distinct() .OrderBy(x => x) .ToList(); ConcurrentObservableSortedDictionary <int, int> subject = new ConcurrentObservableSortedDictionary <int, int>(); using (var benchmark = new BenchmarkIt("Adding items to sorted dictionary")) { // Create test subject // Populate test subject _testCollections.AsParallel().ForAll(collection => { foreach (var item in collection) { subject[item] = item; } }); } bool keyMatchesValue = subject .All(kv => kv.Key == kv.Value); Assert.IsTrue(keyMatchesValue); bool isSorted = subject .Aggregate( new { Sorted = true, LastKey = (int?)null }, (a, b) => new { Sorted = a.Sorted && (!a.LastKey.HasValue || a.LastKey.Value < b.Key), LastKey = (int?)b.Key }) .Sorted; Assert.IsTrue(isSorted); // Compare test subject with expected result Assert.AreEqual(subject.Count, _sortedCollection.Count); bool itemsEqual = _sortedCollection .Zip(subject, (a, b) => a == b.Value) .All(b => b); Assert.IsTrue(itemsEqual); // Compare collectionView var view = subject.CollectionView; Assert.AreEqual(view.Count, _sortedCollection.Count); bool viewItemsEqual = _sortedCollection .Zip(view, (a, b) => a == b.Value) .All(b => b); Assert.IsTrue(viewItemsEqual); }