Beispiel #1
0
        public void CountOfConcatIteratorShouldThrowExceptionOnIntegerOverflow()
        {
            var supposedlyLargeCollection = new DelegateBasedCollection <int> {
                CountWorker = () => int.MaxValue
            };
            var tinyCollection = new DelegateBasedCollection <int> {
                CountWorker = () => 1
            };

            Action <Action> assertThrows = (testCode) =>
            {
                Assert.Throws <OverflowException>(testCode);
            };

            // We need to use checked arithmetic summing up the collections' counts.
            assertThrows(() => supposedlyLargeCollection.Concat(tinyCollection).Count());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(supposedlyLargeCollection).Count());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(tinyCollection).Concat(supposedlyLargeCollection).Count());

            // This applies to ToArray() and ToList() as well, which try to preallocate the exact size
            // needed if all inputs are ICollections.
            assertThrows(() => supposedlyLargeCollection.Concat(tinyCollection).ToArray());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(supposedlyLargeCollection).ToArray());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(tinyCollection).Concat(supposedlyLargeCollection).ToArray());

            assertThrows(() => supposedlyLargeCollection.Concat(tinyCollection).ToList());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(supposedlyLargeCollection).ToList());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(tinyCollection).Concat(supposedlyLargeCollection).ToList());
        }
Beispiel #2
0
        public void CountOfConcatIteratorShouldThrowExceptionOnIntegerOverflow()
        {
            var supposedlyLargeCollection = new DelegateBasedCollection <int> {
                CountWorker = () => int.MaxValue
            };
            var tinyCollection = new DelegateBasedCollection <int> {
                CountWorker = () => 1
            };

            // We need to use checked arithmetic summing up the collections' counts.
            Assert.Throws <OverflowException>(() => supposedlyLargeCollection.Concat(tinyCollection).Count());
            Assert.Throws <OverflowException>(() => tinyCollection.Concat(tinyCollection).Concat(supposedlyLargeCollection).Count());
            Assert.Throws <OverflowException>(() => tinyCollection.Concat(tinyCollection).Concat(tinyCollection).Concat(supposedlyLargeCollection).Count());
        }
Beispiel #3
0
        public void CountOfConcatIteratorShouldThrowExceptionOnIntegerOverflow()
        {
            var supposedlyLargeCollection = new DelegateBasedCollection <int> {
                CountWorker = () => int.MaxValue
            };
            var tinyCollection = new DelegateBasedCollection <int> {
                CountWorker = () => 1
            };

            Action <Action> assertThrows = (testCode) =>
            {
                // The full .NET Framework uses unsigned arithmetic summing up collection counts.
                // See https://github.com/dotnet/corefx/pull/11492.
                if (PlatformDetection.IsFullFramework)
                {
                    testCode();
                }
                else
                {
                    Assert.Throws <OverflowException>(testCode);
                }
            };

            // We need to use checked arithmetic summing up the collections' counts.
            assertThrows(() => supposedlyLargeCollection.Concat(tinyCollection).Count());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(supposedlyLargeCollection).Count());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(tinyCollection).Concat(supposedlyLargeCollection).Count());

            // This applies to ToArray() and ToList() as well, which try to preallocate the exact size
            // needed if all inputs are ICollections.
            assertThrows(() => supposedlyLargeCollection.Concat(tinyCollection).ToArray());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(supposedlyLargeCollection).ToArray());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(tinyCollection).Concat(supposedlyLargeCollection).ToArray());

            assertThrows(() => supposedlyLargeCollection.Concat(tinyCollection).ToList());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(supposedlyLargeCollection).ToList());
            assertThrows(() => tinyCollection.Concat(tinyCollection).Concat(tinyCollection).Concat(supposedlyLargeCollection).ToList());
        }