public void ConcurrentAddTest()
        {
            var processorCount = Environment.ProcessorCount;
            var entryCount     = processorCount * 100000;
            var allNumbers     = Enumerable.Range(1, entryCount).ToArray();
            var groupsPerTask  = allNumbers.GroupBy(number => number % processorCount)
                                 .ToArray();
            var concurrentArray = new ConcurrentArrayBuilder <int, object>().WithCapacity(LinearDoublingPrimeStrategy.GetNextCapacity(entryCount))
                                  .Build();

            Parallel.ForEach(groupsPerTask, group =>
            {
                foreach (var number in group)
                {
                    var addResult = concurrentArray.TryAdd(new Entry <int, object>(number.GetHashCode(), number, null));
                    addResult.OperationResult.Should().Be(AddResult.AddSuccessful);
                }
            });

            concurrentArray.Count.Should().Be(allNumbers.Length);
            foreach (var number in allNumbers)
            {
                var entry = concurrentArray.Find(number.GetHashCode(), number);
                entry.Should().NotBeNull();
            }
        }
        public void FindWhenEmpty()
        {
            var concurrentArray = new ConcurrentArrayBuilder <int, object>().Build();

            var foundEntry = concurrentArray.Find(42, 42);

            foundEntry.Should().BeNull();
        }
        public void FindKeyNull()
        {
            var concurrentArray = new ConcurrentArrayBuilder <string, object>().Build();

            Action act = () => concurrentArray.Find(42, null);

            act.ShouldThrow <ArgumentNullException>()
            .And.ParamName.Should().Be("key");
        }
        public void SimpleAddAndRetrieve(Entry <string, string> entry)
        {
            var concurrentArray = new ConcurrentArrayBuilder <string, string>().Build();

            var tryAddResult = concurrentArray.TryAdd(entry);
            var foundEntry   = concurrentArray.Find(entry.HashCode, entry.Key);

            tryAddResult.OperationResult.Should().Be(AddResult.AddSuccessful);
            foundEntry.Should().BeSameAs(entry);
        }
        public void CustomKeyComparer()
        {
            var keyComparerMock = new KeyComparerMock();
            var concurrentArray = new ConcurrentArrayBuilder <int, object>().WithKeyComparer(keyComparerMock)
                                  .Build();

            concurrentArray.TryAdd(new Entry <int, object>(6, 6, null));

            concurrentArray.Find(6, 6);

            keyComparerMock.EqualsMustHaveBeenCalledAtLeastOnce();
        }
        public void AddAndRetrieveAll(Entry <int, string>[] entries)
        {
            var concurrentArray = new ConcurrentArrayBuilder <int, string>().Build();

            foreach (var entry in entries)
            {
                concurrentArray.TryAdd(entry);
            }

            foreach (var entry in entries)
            {
                var foundEntry = concurrentArray.Find(entry.HashCode, entry.Key);
                foundEntry.Should().BeSameAs(entry);
            }
        }
        public void FindWhenArrayIsFull(int targetKey, bool shouldBeFound)
        {
            var concurrentArray = new ConcurrentArrayBuilder <int, object>().WithCapacity(3)
                                  .Build()
                                  .FillArray();

            var foundEntry = concurrentArray.Find(targetKey.GetHashCode(), targetKey);

            if (shouldBeFound)
            {
                foundEntry.Key.Should().Be(targetKey);
            }
            else
            {
                foundEntry.Should().BeNull();
            }
        }