Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SmartList{T}"/> class.
        /// </summary>
        /// <param name="enumerable">The enumerable.</param>
        public SmartList(IEnumerable <T> enumerable)
        {
            #region Contract
            if (enumerable == null)
            {
                throw new ArgumentNullException(nameof(enumerable));
            }
            #endregion

            this.innerList = enumerable as IReadOnlyList <T>;
            if (this.innerList != null)
            {
                // We have converted the enumerable to a list.
                // We're done and can simply enumerate the list.
                this.count      = this.innerList.Count;
                this.enumerator = null;
            }
            else
            {
                // Maybe the enumerable is actually a collection, list, or set,
                // so we can determine the number of elements cheaply.
                this.count      = Enumerables.TryGetCount(enumerable);
                this.innerList  = this.count != null ? new List <T>((int)this.count) : new List <T>();
                this.enumerator = enumerable.GetEnumerator();
            }
        }
            public void ShouldReturnNull_WhenEnumerableIsNotACollection()
            {
                // Arrange
                var enumerable = new [] { "a", "b", "c" }.Select(s => s);

                // Act
                int?result = Enumerables.TryGetCount(enumerable);

                // Assert
                Assert.Null(result);
            }
            public void ShouldReturnTheCount_WhenEnumerableIsAnICollection()
            {
                // Arrange
                var collection = new ArrayList {
                    "a", "b", "c"
                };

                // Act
                int?result = Enumerables.TryGetCount(collection);

                // Assert
                Assert.Equal(3, result);
            }
Esempio n. 4
0
        /// <summary>
        /// Compares the number of elements in the two collections, if possible.
        /// </summary>
        /// <param name="x">The first collection.</param>
        /// <param name="y">The second collection.</param>
        /// <returns><see langword="true"/> when the collections have a mismatching
        /// number of elements; otherwise, <see langword="false"/>.</returns>
        private static bool HaveMismatchingCounts([NoEnumeration] IEnumerable <T> x, [NoEnumeration] IEnumerable <T> y)
        {
            #region Contract
            Debug.Assert(x != null);
            Debug.Assert(y != null);
            #endregion

            var xcount = Enumerables.TryGetCount(x);
            var ycount = Enumerables.TryGetCount(y);

            return(xcount != null &&
                   ycount != null &&
                   xcount != ycount);
        }
            public void ShouldThrowArgumentNullException_WhenEnumerableIsNull()
            {
                // Arrange
                IReadOnlyCollection <String> enumerable = null;

                // Act
                var exception = Record.Exception(() =>
                {
                    Enumerables.TryGetCount(enumerable);
                });

                // Assert
                Assert.IsType <ArgumentNullException>(exception);
            }