Repository: dotnet/runtime
File: src/libraries/System.Linq/tests/AppendPrependTests.cs (C#)

Code before:
            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 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 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 int[int.MaxValue];
            var result = source.Append(1).Prepend(2);
            Assert.Throws<OverflowException>(() => result.Count());
        }
    }
}


Review comment (bug):
@copilot these array allocations are going to immediately fail because they are simply too large.

However, because the optimization works on `ICollection<TSource>`, we can create a private "mock" class whose `Count` property implementation simply returns `int.MaxValue` the rest of the implementation can just be stubs. This gives also the benefit that the unit test no longer needs to create very large allocations.

Code after:
            Assert.Equal(42, new int[] { 84 }.Prepend(42).ElementAt(0));
            Assert.Equal(84, new int[] { 42 }.Append(84).ElementAt(1));
            Assert.Equal(84, new int[] { 84 }.Prepend(42).ElementAt(1));

            Assert.Equal(42, NumberRangeGuaranteedNotCollectionType(42, 1).Append(84).First());
            Assert.Equal(42, NumberRangeGuaranteedNotCollectionType(84, 1).Prepend(42).First());
            Assert.Equal(84, NumberRangeGuaranteedNotCollectionType(42, 1).Append(84).Last());
            Assert.Equal(84, NumberRangeGuaranteedNotCollectionType(84, 1).Prepend(42).Last());
            Assert.Equal(42, NumberRangeGuaranteedNotCollectionType(42, 1).Append(84).ElementAt(0));
            Assert.Equal(42, NumberRangeGuaranteedNotCollectionType(84, 1).Prepend(42).ElementAt(0));
            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]