public void IsNotReadonly()
        {
            // ReSharper disable once CollectionNeverUpdated.Local
            IDictionary <string, object> dictionary = new LockFreeArrayBasedDictionary <string, object>();

            dictionary.IsReadOnly.Should().BeFalse();
        }
        public void AddOrUpdateNonExistingEntry <TKey, TValue>(TKey key, TValue value)
        {
            var dictionary = new LockFreeArrayBasedDictionary <TKey, TValue>();

            var wasAdded = dictionary.AddOrUpdate(key, value);

            wasAdded.Should().BeTrue();
            dictionary.Should().Contain(key, value);
        }
        public void AddAndRetrieve()
        {
            var dictionary = new LockFreeArrayBasedDictionary <string, string>();

            var result = dictionary.TryAdd("Foo", "Bar");

            result.Should().BeTrue();
            dictionary["Foo"].Should().Be("Bar");
        }
        public void ContainsKey(Action <LockFreeArrayBasedDictionary <int, object> > configureTestTarget, int key, bool expected)
        {
            var testTarget = new LockFreeArrayBasedDictionary <int, object>();

            configureTestTarget(testTarget);

            var actual = testTarget.ContainsKey(key);

            actual.Should().Be(expected);
        }
        public void Values()
        {
            IDictionary <int, string> dictionary = new LockFreeArrayBasedDictionary <int, string>();

            dictionary.Add(1, "Foo");
            dictionary.Add(2, "Bar");
            dictionary.Add(3, "Baz");

            dictionary.Values.Should().BeEquivalentTo("Foo", "Bar", "Baz");
        }
        public void ConcurrentAddTest()
        {
            // Arrange
            var options = new LockFreeArrayBasedDictionary <int, object> .Options {
                BackgroundCopyTaskFactory = new FactoryCreatingAttachedChildTasks()
            };

#if CONCURRENT_LOGGING
            var logger = new ConcurrentLogger();
            options.GrowArrayProcessFactory = new LoggingDecoratorFactoryForGrowArrayProcesses <int, object>(new GrowArrayProcessFactory <int, object>(), logger);
#endif

            var dictionary = new LockFreeArrayBasedDictionary <int, object>(options);
#if CONCURRENT_LOGGING
            dictionary.Logger = logger;
#endif
            var processorCount = Environment.ProcessorCount;
            var entryCount     = processorCount * 100000;
            var allNumbers     = Enumerable.Range(1, entryCount).ToArray();
            var groupsPerTask  = allNumbers.GroupBy(number => number % processorCount)
                                 .ToArray();

            // Act
            try
            {
                Parallel.ForEach(groupsPerTask, group =>
                {
                    foreach (var number in group)
                    {
                        if (dictionary.TryAdd(number, new object()))
                        {
                            continue;
                        }

                        var errorMessage = $"Could not add entry {number}.";
#if CONCURRENT_LOGGING
                        logger.Log(errorMessage);
#endif
                        throw new AssertionFailedException(errorMessage);
                    }
                });


                // Assert
                dictionary.Count.Should().Be(allNumbers.Length);
                dictionary.Should().ContainKeys(allNumbers);
            }
            catch (Exception)
            {
#if CONCURRENT_LOGGING
                logger.WriteLogMessages(new StreamWriter("ConcurrentAddLog.txt"));
#endif
                throw;
            }
        }
Exemple #7
0
        public LockFreeArrayBasedDictionary <int, object> LockFreeArrayBasedDictionary()
        {
            var lockFreeDictionary = new LockFreeArrayBasedDictionary <int, object>();

            foreach (var key in _keys)
            {
                lockFreeDictionary.TryAdd(key, new object());
            }

            return(lockFreeDictionary);
        }
        public void CountMustReflectNumberOfAddedItems(KeyValuePair <string, object>[] itemsToAdd)
        {
            IDictionary <string, object> dictionary = new LockFreeArrayBasedDictionary <string, object>();

            foreach (var keyValuePair in itemsToAdd)
            {
                dictionary.Add(keyValuePair);
            }

            dictionary.Count.Should().Be(itemsToAdd.Length);
        }
        public void IndexerSetKeyNull()
        {
            // ReSharper disable once CollectionNeverQueried.Local
            var dictionary = new LockFreeArrayBasedDictionary <string, object>();

            // ReSharper disable once AssignNullToNotNullAttribute
            Action act = () => dictionary[null] = new object();

            act.ShouldThrow <ArgumentNullException>()
            .And.ParamName.Should().Be("key");
        }
        public void AddViaInterface(KeyValuePair <string, object>[] itemsToAdd)
        {
            IDictionary <string, object> dictionary = new LockFreeArrayBasedDictionary <string, object>();

            foreach (var keyValuePair in itemsToAdd)
            {
                dictionary.Add(keyValuePair.Key, keyValuePair.Value);
            }

            dictionary.Count.Should().Be(itemsToAdd.Length);
        }
        public void RemoveExistingKvp <TKey, TValue>(TKey key, TValue value)
        {
            ICollection <KeyValuePair <TKey, TValue> > dictionary = new LockFreeArrayBasedDictionary <TKey, TValue>();

            dictionary.Add(new KeyValuePair <TKey, TValue>(key, value));

            var wasRemoved = dictionary.Remove(new KeyValuePair <TKey, TValue>(key, value));

            wasRemoved.Should().BeTrue();
            dictionary.Should().NotContain(new KeyValuePair <TKey, TValue>(key, value));
            dictionary.Count.Should().Be(0);
        }
        public void AddOrUpdateExistingEntry <TKey, TValue>(TKey key, TValue value1, TValue value2)
        {
            var dictionary = new LockFreeArrayBasedDictionary <TKey, TValue>();

            dictionary.TryAdd(key, value1);

            var wasAdded = dictionary.AddOrUpdate(key, value2);

            wasAdded.Should().BeFalse();
            dictionary.Should().Contain(key, value2);
            dictionary.Should().NotContain(key, value1);
        }
        public void IncreaseCapacity(int numberOfItems)
        {
            var keys       = Enumerable.Range(1, numberOfItems).ToArray();
            var dictionary = new LockFreeArrayBasedDictionary <int, object>();

            foreach (var key in keys)
            {
                dictionary.TryAdd(key, new object());
            }

            dictionary.Should().ContainKeys(keys);
        }
        public void RemoveExistingKey <TKey, TValue>(TKey key, TValue value)
        {
            var dictionary = new LockFreeArrayBasedDictionary <TKey, TValue>();

            dictionary.TryAdd(key, value);

            var wasRemoved = dictionary.Remove(key);

            wasRemoved.Should().BeTrue();
            dictionary.Should().NotContainKey(key);
            dictionary.Count.Should().Be(0);
        }
        public void ClearViaInterface(KeyValuePair <string, object>[] itemsToAdd)
        {
            IDictionary <string, object> dictionary = new LockFreeArrayBasedDictionary <string, object>();

            foreach (var keyValuePair in itemsToAdd)
            {
                dictionary.Add(keyValuePair);
            }

            dictionary.Clear();

            dictionary.Count.Should().Be(0);
        }
Exemple #16
0
        public LockFreeArrayBasedDictionary <int, object> LockFreeDictionaryArrayBasedDictionary()
        {
            var dictionary = new LockFreeArrayBasedDictionary <int, object>();

            Parallel.ForEach(_perThreadKeyGroups, kvp =>
            {
                foreach (var number in kvp.Value)
                {
                    dictionary.TryAdd(number, new object());
                }
            });

            return(dictionary);
        }
        public void RemoveNonExistingKey(string key, KeyValuePair <string, object>[] existingEntries)
        {
            var dictionary = new LockFreeArrayBasedDictionary <string, object>();

            foreach (var existingEntry in existingEntries)
            {
                dictionary.TryAdd(existingEntry.Key, existingEntry.Value);
            }

            var wasRemoved = dictionary.Remove(key);

            wasRemoved.Should().BeFalse();
            dictionary.Should().NotContainKey(key);
        }
        public void IndexerGetKeyNull()
        {
            // ReSharper disable once CollectionNeverUpdated.Local
            var dictionary = new LockFreeArrayBasedDictionary <string, object>();

            // ReSharper disable once UnusedVariable
            Action act = () =>
            {
                var value = dictionary[null];
            };

            act.ShouldThrow <ArgumentNullException>()
            .And.ParamName.Should().Be("key");
        }
        public void TryRemoveNonExisting(string key, KeyValuePair <string, object>[] existingEntries)
        {
            var dictionary = new LockFreeArrayBasedDictionary <string, object>();

            foreach (var existingEntry in existingEntries)
            {
                dictionary.TryAdd(existingEntry.Key, existingEntry.Value);
            }

            object removedValue;
            var    wasRemoved = dictionary.TryRemove(key, out removedValue);

            wasRemoved.Should().BeFalse();
            removedValue.Should().Be(default(object));
        }
        public void RemoveNonExistingKvp(string key, KeyValuePair <string, object>[] existingEntries)
        {
            ICollection <KeyValuePair <string, object> > dictionary = new LockFreeArrayBasedDictionary <string, object>();

            foreach (var existingEntry in existingEntries)
            {
                dictionary.Add(existingEntry);
            }

            var entry      = new KeyValuePair <string, object>(key, new object());
            var wasRemoved = dictionary.Remove(entry);

            wasRemoved.Should().BeFalse();
            dictionary.Should().NotContain(entry);
        }
        public void SuccessfulTryGetValue <TKey, TValue>(TKey searchedKey, TValue expectedValue, IEnumerable <KeyValuePair <TKey, TValue> > existingEntries)
        {
            var dictionary = new LockFreeArrayBasedDictionary <TKey, TValue>();

            foreach (var existingEntry in existingEntries)
            {
                dictionary.TryAdd(existingEntry.Key, existingEntry.Value);
            }

            TValue foundValue;
            var    wasFound = dictionary.TryGetValue(searchedKey, out foundValue);

            wasFound.Should().BeTrue();
            foundValue.Should().Be(expectedValue);
        }
        public void UnsuccessfulTryGetValue <TKey>(TKey searchedKey, IEnumerable <KeyValuePair <TKey, object> > existingEntries)
        {
            var dictionary = new LockFreeArrayBasedDictionary <TKey, object>();

            foreach (var existingEntry in existingEntries)
            {
                dictionary.TryAdd(existingEntry.Key, existingEntry.Value);
            }

            object foundValue;
            var    wasFound = dictionary.TryGetValue(searchedKey, out foundValue);

            wasFound.Should().BeFalse();
            foundValue.Should().Be(default(object));
        }
        public void TryAddOnPreviouslyRemovedEntry()
        {
            var dictionary   = new LockFreeArrayBasedDictionary <string, object>();
            var initialValue = new object();

            dictionary.TryAdd("Foo", initialValue);
            dictionary.Remove("Foo");

            var newValue = new object();
            var result   = dictionary.TryAdd("Foo", newValue);

            result.Should().BeTrue();
            var actualValue = dictionary["Foo"];

            actualValue.Should().BeSameAs(newValue);
            actualValue.Should().NotBeSameAs(initialValue);
        }
        public void CopyTo()
        {
            IDictionary <string, string> dictionary = new LockFreeArrayBasedDictionary <string, string>();

            dictionary.Add("Foo", "Bar");
            dictionary.Add("Baz", "Qux");
            dictionary.Add("Quux", "Quuz");
            var array = new KeyValuePair <string, string> [3];

            dictionary.CopyTo(array, 0);

            var expected = new[]
            {
                new KeyValuePair <string, string>("Foo", "Bar"),
                new KeyValuePair <string, string>("Baz", "Qux"),
                new KeyValuePair <string, string>("Quux", "Quuz")
            };

            array.Should().BeEquivalentTo(expected);
        }
        public void Contains(LockFreeArrayBasedDictionary <int, string> dictionary, KeyValuePair <int, string> targetPair, bool expected)
        {
            var result = dictionary.Contains(targetPair);

            result.Should().Be(expected);
        }