public void Should_shrink_after_removing_a_lot_of_elements() { var itemsCount = 150 * 1000; dic = new StripedDictionary <Guid, string, FastGuidComparer>(itemsCount); var capacityBefore = dic.Capacity; var segmentsCountBefore = dic.SegmentsCount; Console.Out.WriteLine($"Capacity before = {capacityBefore}"); Console.Out.WriteLine($"Segments before = {segmentsCountBefore}"); for (int i = 0; i < itemsCount; i++) { dic.Add(Guid.NewGuid(), string.Empty); } var capacityAfterFilling = dic.Capacity; var segmentsCountAfterFilling = dic.SegmentsCount; Console.Out.WriteLine($"Capacity after filling = {capacityAfterFilling}"); Console.Out.WriteLine($"Segments after filling = {segmentsCountAfterFilling}"); foreach (var pair in dic) { dic.Remove(pair.Key); } Console.Out.WriteLine($"Capacity after cleaning = {dic.Capacity}"); Console.Out.WriteLine($"Segments after cleaning = {dic.SegmentsCount}"); dic.Capacity.Should().BeLessThan(capacityAfterFilling); dic.SegmentsCount.Should().BeLessThan(segmentsCountAfterFilling); dic.HasArraysInLOH.Should().BeFalse(); }
private void LaunchWriter(TimeSpan time, CancellationToken cancellationToken) { var task = Task.Run(() => { var random = new Random(Guid.NewGuid().GetHashCode()); var watch = Stopwatch.StartNew(); try { while (watch.Elapsed < time && !cancellationToken.IsCancellationRequested) { var index = random.Next(keys.Length / 2, keys.Length); if (random.NextDouble() <= 0.5) { dic.Set(keys[index], keys[index].ToString()); } else { dic.Remove(keys[index]); } } } catch (Exception error) { throw new Exception("Error in writer!", error); } }); tasks.Add(task); }
public void Remove_should_return_true_when_removing_existing_key() { var keys = Enumerable.Range(0, 100 * 000).Select(_ => Guid.NewGuid()).ToArray(); foreach (var key in keys) { dic.Add(key, key.ToString()); } foreach (var key in keys) { dic.Remove(key).Should().BeTrue(); } }