コード例 #1
0
        public void GetEnumeratorExplicit()
        {
            ICollection <int> builder = ImmutableSegmentedList.Create <int>().ToBuilder();
            var enumerator            = builder.GetEnumerator();

            Assert.NotNull(enumerator);
        }
コード例 #2
0
        public void RemoveRangeEnumerableTest()
        {
            var list = ImmutableSegmentedList.Create(1, 2, 3);

            Assert.Throws <ArgumentNullException>("items", () => list.RemoveRange(null !));

            ImmutableSegmentedList <int> removed2 = list.RemoveRange(new[] { 2 });

            Assert.Equal(2, removed2.Count);
            Assert.Equal(new[] { 1, 3 }, removed2);

            ImmutableSegmentedList <int> removed13 = list.RemoveRange(new[] { 1, 3, 5 });

            Assert.Equal(1, removed13.Count);
            Assert.Equal(new[] { 2 }, removed13);
            Assert.Equal(new[] { 2 }, System.Collections.Immutable.ImmutableList.RemoveRange((System.Collections.Immutable.IImmutableList <int>)list, new[] { 1, 3, 5 }));

            Assert.True(IsSame(list, list.RemoveRange(new[] { 5 })));
            Assert.True(IsSame(ImmutableSegmentedList.Create <int>(), ImmutableSegmentedList.Create <int>().RemoveRange(new[] { 1 })));

            var listWithDuplicates = ImmutableSegmentedList.Create(1, 2, 2, 3);

            Assert.Equal(new[] { 1, 2, 3 }, listWithDuplicates.RemoveRange(new[] { 2 }));
            Assert.Equal(new[] { 1, 3 }, listWithDuplicates.RemoveRange(new[] { 2, 2 }));

            Assert.Throws <ArgumentNullException>("items", () => System.Collections.Immutable.ImmutableList.RemoveRange((System.Collections.Immutable.IImmutableList <int>)ImmutableSegmentedList.Create(1, 2, 3), null !));
            Assert.Equal(new[] { 1, 3 }, System.Collections.Immutable.ImmutableList.RemoveRange((System.Collections.Immutable.IImmutableList <int>)ImmutableSegmentedList.Create(1, 2, 3), new[] { 2 }));
        }
コード例 #3
0
        public void InsertBalanceTest()
        {
            var list = ImmutableSegmentedList.Create(1);

            list = list.Insert(0, 2);
            list = list.Insert(1, 3);
        }
コード例 #4
0
        public static void TestDebuggerAttributes_Null()
        {
            Type proxyType = DebuggerAttributes.GetProxyType(ImmutableSegmentedList.Create <double>());
            TargetInvocationException tie = Assert.Throws <TargetInvocationException>(() => Activator.CreateInstance(proxyType, (object?)null));

            Assert.IsType <ArgumentNullException>(tie.InnerException);
        }
コード例 #5
0
        public void CopyToTest()
        {
            var list       = ImmutableSegmentedList.Create(1, 2);
            var enumerable = (IEnumerable <int>)list;

            var array = new int[2];

            CopyToImpl(list, array);
            Assert.Equal(enumerable, array);

            array = new int[2];
            CopyToImpl(list, array, 0);
            Assert.Equal(enumerable, array);

            array = new int[2];
            CopyToImpl(list, 0, array, 0, list.Count);
            Assert.Equal(enumerable, array);

            array = new int[1]; // shorter than source length
            CopyToImpl(list, 0, array, 0, array.Length);
            Assert.Equal(enumerable.Take(array.Length), array);

            array = new int[3];
            CopyToImpl(list, 1, array, 2, 1);
            Assert.Equal(new[] { 0, 0, 2 }, array);

            array = new int[2];
            ((ICollection)GetListQuery(list)).CopyTo(array, 0);
            Assert.Equal(enumerable, array);
        }
コード例 #6
0
        public void RemoveRange_EnumerableEqualityComparer_AcceptsNullEQ()
        {
            var list       = ImmutableSegmentedList.Create(1, 2, 3);
            var removed2eq = list.RemoveRange(new[] { 2 }, null);

            Assert.Equal(2, removed2eq.Count);
            Assert.Equal(new[] { 1, 3 }, removed2eq);
        }
コード例 #7
0
        public void ReverseTest2()
        {
            var emptyList = ImmutableSegmentedList.Create <int>();

            Assert.True(IsSame(emptyList, emptyList.Reverse()));

            var populatedList = ImmutableSegmentedList.Create(3, 2, 1);

            Assert.Equal(Enumerable.Range(1, 3), populatedList.Reverse());
        }
コード例 #8
0
        /// <summary>
        /// Asserts that the <see cref="ImmutableSegmentedList{T}"/> or <see cref="ImmutableSegmentedList{T}.Builder"/>'s
        /// implementation of <see cref="IList"/> behave the same way <see cref="List{T}"/> does.
        /// </summary>
        /// <typeparam name="T">The type of the element for one collection to test with.</typeparam>
        /// <param name="operation">
        /// The <see cref="IList"/> operation to perform.
        /// The function is provided with the <see cref="IList"/> implementation to test
        /// and the item to use as the argument to the operation.
        /// The function should return some equatable value by which to compare the effects
        /// of the operation across <see cref="IList"/> implementations.
        /// </param>
        /// <param name="item">The item to add to the collection.</param>
        /// <param name="other">The item to pass to the <paramref name="operation"/> function as the second parameter.</param>
        protected void AssertIListBaseline <T>(Func <IList, object?, object> operation, T item, object?other)
        {
            IList bclList = new List <T> {
                item
            };
            IList testedList = (IList)this.GetListQuery(ImmutableSegmentedList.Create(item));

            object expected = operation(bclList, other);
            object actual   = operation(testedList, other);

            Assert.Equal(expected, actual);
        }
コード例 #9
0
        public void IListOfTIsReadOnly()
        {
            IList <int> list = ImmutableSegmentedList.Create <int>();

            Assert.True(list.IsReadOnly);
            Assert.Throws <NotSupportedException>(() => list.Add(1));
            Assert.Throws <NotSupportedException>(() => list.Clear());
            Assert.Throws <NotSupportedException>(() => list.Insert(0, 1));
            Assert.Throws <NotSupportedException>(() => list.Remove(1));
            Assert.Throws <NotSupportedException>(() => list.RemoveAt(0));
            Assert.Throws <NotSupportedException>(() => list[0] = 1);
        }
コード例 #10
0
        public void Remove_NullEqualityComparer()
        {
            var collection = ImmutableSegmentedList.Create(1, 2, 3);
            var modified   = collection.Remove(2, null);

            Assert.Equal(new[] { 1, 3 }, modified);

            // Try again through the explicit interface implementation.
            System.Collections.Immutable.IImmutableList <int> collectionIface = collection;
            var modified2 = collectionIface.Remove(2, null);

            Assert.Equal(new[] { 1, 3 }, modified2);
        }
コード例 #11
0
        public void RemoveRangeArrayTest()
        {
            Assert.True(ImmutableSegmentedList <int> .Empty.RemoveRange(0, 0).IsEmpty);

            var list = ImmutableSegmentedList.Create(1, 2, 3);

            Assert.Throws <ArgumentOutOfRangeException>("index", () => list.RemoveRange(-1, 0));
            Assert.Throws <ArgumentOutOfRangeException>("count", () => list.RemoveRange(0, -1));
            Assert.Throws <ArgumentException>(() => list.RemoveRange(4, 0));
            Assert.Throws <ArgumentException>(() => list.RemoveRange(0, 4));
            Assert.Throws <ArgumentException>(() => list.RemoveRange(2, 2));
            Assert.Equal(list, list.RemoveRange(3, 0));
        }
コード例 #12
0
        public void DebuggerAttributesValid()
        {
            DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableSegmentedList.Create <int>());
            ImmutableSegmentedList <double> list = ImmutableSegmentedList.Create <double>(1, 2, 3);
            DebuggerAttributeInfo           info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(list);

            object?rootNode = DebuggerAttributes.GetFieldValue(ImmutableSegmentedList.Create <string>("1", "2", "3"), "_root") !;

            DebuggerAttributes.ValidateDebuggerDisplayReferences(rootNode);
            PropertyInfo itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute <DebuggerBrowsableAttribute>() !.State == DebuggerBrowsableState.RootHidden);

            double[]? items = itemProperty.GetValue(info.Instance) as double[];
            Assert.Equal(list, items);
        }
コード例 #13
0
        public void SetItem()
        {
            var emptyList = ImmutableSegmentedList.Create <int>();

            Assert.Throws <ArgumentOutOfRangeException>("index", () => emptyList[-1]);
            Assert.Throws <ArgumentOutOfRangeException>("index", () => emptyList[0]);
            Assert.Throws <ArgumentOutOfRangeException>("index", () => emptyList[1]);

            var listOfOne = emptyList.Add(5);

            Assert.Throws <ArgumentOutOfRangeException>("index", () => listOfOne[-1]);
            Assert.Equal(5, listOfOne[0]);
            Assert.Throws <ArgumentOutOfRangeException>("index", () => listOfOne[1]);
        }
コード例 #14
0
        public void Create()
        {
            var comparer = StringComparer.OrdinalIgnoreCase;

            ImmutableSegmentedList <string> list = ImmutableSegmentedList.Create <string>();

            Assert.Equal(0, list.Count);

            list = ImmutableSegmentedList.Create("a");
            Assert.Equal(1, list.Count);

            list = ImmutableSegmentedList.Create("a", "b");
            Assert.Equal(2, list.Count);

            list = ImmutableSegmentedList.CreateRange((IEnumerable <string>) new[] { "a", "b" });
            Assert.Equal(2, list.Count);
        }
コード例 #15
0
        public void InsertRangeImmutableTest()
        {
            var list         = ImmutableSegmentedList <int> .Empty;
            var nonEmptyList = ImmutableSegmentedList.Create(1);

            Assert.Throws <ArgumentOutOfRangeException>("index", () => list.InsertRange(1, nonEmptyList));
            Assert.Throws <ArgumentOutOfRangeException>("index", () => list.InsertRange(-1, nonEmptyList));

            list = list.InsertRange(0, ImmutableSegmentedList.Create(1, 104, 105));
            list = list.InsertRange(1, ImmutableSegmentedList.Create(2, 3));
            list = list.InsertRange(2, ImmutableSegmentedList <int> .Empty);
            list = list.InsertRange(3, ImmutableSegmentedList <int> .Empty.InsertRange(0, Enumerable.Range(4, 100)));
            Assert.Equal(Enumerable.Range(1, 105), list);

            Assert.Throws <ArgumentOutOfRangeException>("index", () => list.InsertRange(106, nonEmptyList));
            Assert.Throws <ArgumentOutOfRangeException>("index", () => list.InsertRange(-1, nonEmptyList));
        }
コード例 #16
0
        public void AddRangeOptimizationsTest()
        {
            // All these optimizations are tested based on filling an empty list.
            var emptyList = ImmutableSegmentedList.Create <string>();

            // Adding an empty list to an empty list should yield the original list.
            Assert.True(IsSame(emptyList, emptyList.AddRange(new string[0])));

            // Adding a non-empty immutable list to an empty one should return the added list.
            var nonEmptyListDefaultComparer = ImmutableSegmentedList.Create("5");

            Assert.True(IsSame(nonEmptyListDefaultComparer, emptyList.AddRange(nonEmptyListDefaultComparer)));

            // Adding a Builder instance to an empty list should be seen through.
            var builderOfNonEmptyListDefaultComparer = nonEmptyListDefaultComparer.ToBuilder();

            Assert.True(IsSame(nonEmptyListDefaultComparer, emptyList.AddRange(builderOfNonEmptyListDefaultComparer)));
        }
コード例 #17
0
        public void IListMembers()
        {
            IList list = ImmutableSegmentedList.Create <int>().ToBuilder();

            Assert.False(list.IsReadOnly);
            Assert.False(list.IsFixedSize);

            Assert.Equal(0, list.Add(5));
            Assert.Equal(1, list.Add(8));
            Assert.True(list.Contains(5));
            Assert.False(list.Contains(7));
            list.Insert(1, 6);
            Assert.Equal(6, list[1]);
            list.Remove(5);
            list[0] = 9;
            Assert.Equal(new[] { 9, 8 }, list.Cast <int>().ToArray());
            list.Clear();
            Assert.Equal(0, list.Count);
        }
コード例 #18
0
        public void EnumeratorTest()
        {
            var list       = ImmutableSegmentedList.Create("a");
            var enumerator = list.GetEnumerator();

            Assert.Null(enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal("a", enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Null(enumerator.Current);

            enumerator.Reset();
            Assert.Null(enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal("a", enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Null(enumerator.Current);

            enumerator.Dispose();
            enumerator.Reset();
        }
コード例 #19
0
        public void ReplaceWithEqualityComparerTest()
        {
            var list = ImmutableSegmentedList.Create(new Person {
                Name = "Andrew", Age = 20
            });
            var newAge = new Person {
                Name = "Andrew", Age = 21
            };
            var updatedList = list.Replace(newAge, newAge, new NameOnlyEqualityComparer());

            Assert.Equal(newAge.Age, updatedList[0].Age);

            // Try again with a null equality comparer, which should use the default EQ.
            updatedList = list.Replace(list[0], newAge);
            Assert.False(IsSame(list, updatedList));

            // Finally, try one last time using the interface implementation.
            System.Collections.Immutable.IImmutableList <Person> iface = list;
            var updatedIface = System.Collections.Immutable.ImmutableList.Replace(iface, list[0], newAge);

            Assert.NotSame(iface, updatedIface);
        }
コード例 #20
0
        public void EnumeratorRecyclingMisuse()
        {
            var collection     = ImmutableSegmentedList.Create(1);
            var enumerator     = collection.GetEnumerator();
            var enumeratorCopy = enumerator;

            Assert.True(enumerator.MoveNext());
            enumerator.Dispose();
            Assert.False(enumerator.MoveNext());
            enumerator.Reset();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumeratorCopy.MoveNext());
            enumeratorCopy.Reset();
            Assert.Equal(0, enumeratorCopy.Current);
            enumerator.Dispose(); // double-disposal should not throw
            enumeratorCopy.Dispose();

            // We expect that acquiring a new enumerator will use the same underlying Stack<T> object,
            // but that it will not throw exceptions for the new enumerator.
            enumerator = collection.GetEnumerator();
            Assert.True(enumerator.MoveNext());
            Assert.Equal(collection[0], enumerator.Current);
            enumerator.Dispose();
        }
コード例 #21
0
        public void IsReadOnly()
        {
            ICollection <int> builder = ImmutableSegmentedList.Create <int>().ToBuilder();

            Assert.False(builder.IsReadOnly);
        }
コード例 #22
0
 public void GetHashCodeVariesByInstance()
 {
     Assert.NotEqual(ImmutableSegmentedList.Create <int>().GetHashCode(), ImmutableSegmentedList.Create(5).GetHashCode());
 }
コード例 #23
0
        public void ToImmutableListOfSameType()
        {
            var list = ImmutableSegmentedList.Create("a");

            Assert.True(IsSame(list, list.ToImmutableSegmentedList()));
        }
コード例 #24
0
        public void IsSynchronized()
        {
            ICollection collection = ImmutableSegmentedList.Create <int>();

            Assert.True(collection.IsSynchronized);
        }