Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns>false if empty</returns>
        public bool TryGetAndRemoveOldest(out Height height, out Block block)
        {
            height = Height.Unknown;
            block  = default(Block);
            if (_blocks.Count == 0)
            {
                return(false);
            }

            height = _blocks.Keys.Min();
            block  = _blocks[height];
            _blocks.Remove(height);

            return(true);
        }
Exemple #2
0
        public void ConcurrentObservableDictionaryTest()
        {
            ConcurrentObservableDictionary <int, string> dict = new ConcurrentObservableDictionary <int, string>();
            var times = 0;

            dict.CollectionChanged += delegate
            {
                times++;
            };

            dict.Add(1, "foo");
            dict.Add(2, "moo");
            dict.Add(3, "boo");

            dict.AddOrReplace(1, "boo");
            dict.Remove(dict.First(x => x.Value == "moo"));

            Assert.True(dict.Values.All(x => x == "boo"));
            Assert.Equal(5, times);
        }
Exemple #3
0
        public void ConcurrentObservableDictionaryTest()
        {
            ConcurrentObservableDictionary <int, string> dict = new ConcurrentObservableDictionary <int, string>();

            _times = 0;
            dict.CollectionChanged += Dict_CollectionChanged;

            try
            {
                dict.Add(1, "foo");
                dict.Add(2, "moo");
                dict.Add(3, "boo");

                dict.AddOrReplace(1, "boo");
                dict.Remove(dict.First(x => x.Value == "moo"));

                Assert.DoesNotContain(dict.Values, x => x != "boo");
                Assert.Equal(5, _times);
            }
            finally
            {
                dict.CollectionChanged -= Dict_CollectionChanged;
            }
        }
Exemple #4
0
 protected async Task Remove(List <KeyValuePair <string, string> > itemsToRemove, ConcurrentObservableDictionary <string, string> destCollection, bool onGuiThread)
 {
     await Remove(itemsToRemove, destCollection, (item) => destCollection.Remove(item), onGuiThread);
 }
        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");
        }