Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ public override int GetCount(bool onlyIfCheap)
if (_source is Iterator<TSource> iterator)
{
int count = iterator.GetCount(onlyIfCheap);
return count == -1 ? -1 : count + 1;
return count == -1 ? -1 : checked(count + 1);
}

return !onlyIfCheap || _source is ICollection<TSource> ? _source.Count() + 1 : -1;
return !onlyIfCheap || _source is ICollection<TSource> ? checked(_source.Count() + 1) : -1;
}

public override TSource? TryGetFirst(out bool found)
Expand Down Expand Up @@ -277,10 +277,10 @@ public override int GetCount(bool onlyIfCheap)
if (_source is Iterator<TSource> iterator)
{
int count = iterator.GetCount(onlyIfCheap);
return count == -1 ? -1 : count + _appendCount + _prependCount;
return count == -1 ? -1 : checked(count + _appendCount + _prependCount);
}

return !onlyIfCheap || _source is ICollection<TSource> ? _source.Count() + _appendCount + _prependCount : -1;
return !onlyIfCheap || _source is ICollection<TSource> ? checked(_source.Count() + _appendCount + _prependCount) : -1;
}

public override bool Contains(TSource value)
Expand Down
74 changes: 74 additions & 0 deletions src/libraries/System.Linq/tests/AppendPrependTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using Xunit;

namespace System.Linq.Tests
{
public class AppendPrependTests : EnumerableTests
{
// Mock collection for testing overflow without allocating memory
private sealed class MockCollection<T> : ICollection<T>
{
private readonly int _count;

public MockCollection(int count) => _count = count;

public int Count => _count;
public bool IsReadOnly => true;

public void Add(T item) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Contains(T item) => false;
public void CopyTo(T[] array, int arrayIndex) { }
public bool Remove(T item) => throw new NotSupportedException();
public IEnumerator<T> GetEnumerator() => Enumerable.Empty<T>().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

[Fact]
public void SameResultsRepeatCallsIntQueryAppend()
{
Expand Down Expand Up @@ -263,5 +283,59 @@ public void AppendPrepend_First_Last_ElementAt()
Assert.Equal(84, NumberRangeGuaranteedNotCollectionType(42, 1).Append(84).ElementAt(1));
Assert.Equal(84, NumberRangeGuaranteedNotCollectionType(84, 1).Prepend(42).ElementAt(1));
}

[Fact]
public void AppendOverflowCount()
{
// AppendPrepend1Iterator overflow when source has GetCount optimization
var source = Enumerable.Repeat(0, int.MaxValue);
var appended = source.Append(1);
Assert.Throws<OverflowException>(() => appended.Count());
}

[Fact]
public void PrependOverflowCount()
{
// AppendPrepend1Iterator overflow when source has GetCount optimization
var source = Enumerable.Repeat(0, int.MaxValue);
var prepended = source.Prepend(1);
Assert.Throws<OverflowException>(() => prepended.Count());
}

[Fact]
public void AppendPrependNOverflowCount()
{
// AppendPrependN overflow when source has GetCount optimization
var source = Enumerable.Repeat(0, int.MaxValue);
var result = source.Append(1).Prepend(2);
Assert.Throws<OverflowException>(() => result.Count());
}

[Fact]
public void AppendOverflowCountWithICollection()
{
// AppendPrepend1Iterator overflow when source is ICollection
var source = new MockCollection<int>(int.MaxValue);
var appended = source.Append(1);
Assert.Throws<OverflowException>(() => appended.Count());
}

[Fact]
public void PrependOverflowCountWithICollection()
{
// AppendPrepend1Iterator overflow when source is ICollection
var source = new MockCollection<int>(int.MaxValue);
var prepended = source.Prepend(1);
Assert.Throws<OverflowException>(() => prepended.Count());
}

[Fact]
public void AppendPrependNOverflowCountWithICollection()
{
// AppendPrependN overflow when source is ICollection
var source = new MockCollection<int>(int.MaxValue);
var result = source.Append(1).Prepend(2);
Assert.Throws<OverflowException>(() => result.Count());
}
}
}
Loading