// Note that the node being enqueued does not need to be physically placed
// there at this point, as such an assignment would be redundant.
- int currentSize = _size++;
+ int currentSize = _size;
_version++;
if (_nodes.Length == currentSize)
Grow(currentSize + 1);
}
+ _size = currentSize + 1;
+
if (_comparer == null)
{
MoveUpDefaultComparer((element, priority), currentSize);
var collection = items as ICollection<(TElement Element, TPriority Priority)>;
if (collection is not null && (count = collection.Count) > _nodes.Length - _size)
{
- Grow(_size + count);
+ Grow(checked(_size + count));
}
if (_size == 0)
ArgumentNullException.ThrowIfNull(elements);
int count;
- if (elements is ICollection<(TElement Element, TPriority Priority)> collection &&
+ if (elements is ICollection<TElement> collection &&
(count = collection.Count) > _nodes.Length - _size)
{
- Grow(_size + count);
+ Grow(checked(_size + count));
}
if (_size == 0)
AssertExtensions.CollectionEqual(itemsToEnqueue, queue.UnorderedItems, EqualityComparer<(TElement, TPriority)>.Default);
}
+ [Fact]
+ public void PriorityQueue_EnqueueRange_CollectionWithLargeCount_ThrowsOverflowException()
+ {
+ PriorityQueue<TElement, TPriority> queue = CreatePriorityQueue(1, 1, out List<(TElement element, TPriority priority)> generatedItems);
+
+ CollectionWithLargeCount<(TElement, TPriority)> pairCollection = new();
+ Assert.Throws<OverflowException>(() => queue.EnqueueRange(pairCollection));
+
+ (_, TPriority priority) = generatedItems[0];
+ CollectionWithLargeCount<TElement> elementCollection = new();
+ Assert.Throws<OverflowException>(() => queue.EnqueueRange(elementCollection, priority));
+ }
+
+ private class CollectionWithLargeCount<T> : ICollection<T>
+ {
+ public int Count => int.MaxValue;
+
+ public bool IsReadOnly => throw new NotImplementedException();
+ public void Add(T item) => throw new NotImplementedException();
+ public void Clear() => throw new NotImplementedException();
+ public bool Contains(T item) => throw new NotImplementedException();
+ public void CopyTo(T[] array, int arrayIndex) => throw new NotImplementedException();
+ public IEnumerator<T> GetEnumerator() => throw new NotImplementedException();
+ public bool Remove(T item) => throw new NotImplementedException();
+ IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
+ }
+
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void PriorityQueue_EnqueueDequeue(int count)