Skip to content

Commit

Permalink
fix a bug in DistinctQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
Inok committed Dec 6, 2023
1 parent f5bdaf7 commit 414a269
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Promote.NuGet.Commands/Core/DistinctQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public DistinctQueue()
_enqueuedItems = new HashSet<T>();
}

public DistinctQueue(IEnumerable<T> items)
public DistinctQueue(IEnumerable<T> items) : this()
{
if (items == null) throw new ArgumentNullException(nameof(items));

_queue = new Queue<T>(items);
_enqueuedItems = new HashSet<T>();
foreach (var item in items)
{
Enqueue(item);
}
}

public bool Enqueue(T item)
Expand Down
33 changes: 33 additions & 0 deletions tests/Promote.NuGet.Commands.Tests/Core/DistinctQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,37 @@ public void Create_queue_with_items()
queue.TryDequeue(out result).Should().BeFalse();
result.Should().BeNull();
}

[Test]
public void Ctor_keeps_only_unique_initial_items()
{
var queue = new DistinctQueue<string>(new[] { "str_1", "str_2", "str_1" });

queue.TryDequeue(out var result).Should().BeTrue();
result.Should().Be("str_1");

queue.TryDequeue(out result).Should().BeTrue();
result.Should().Be("str_2");

queue.TryDequeue(out result).Should().BeFalse();
result.Should().BeNull();
}

[Test]
public void Cannot_enqueue_items_added_by_ctor()
{
var queue = new DistinctQueue<string>(new[] { "str_1", "str_2" });

queue.TryDequeue(out var result).Should().BeTrue();
result.Should().Be("str_1");

queue.Enqueue("str_1").Should().BeFalse();
queue.Enqueue("str_2").Should().BeFalse();

queue.TryDequeue(out result).Should().BeTrue();
result.Should().Be("str_2");

queue.TryDequeue(out result).Should().BeFalse();
result.Should().BeNull();
}
}

0 comments on commit 414a269

Please sign in to comment.