public void CopyToTest()
        {
            var listQuery = this.GetListQuery(ImmutableTrieList.Create(1, 2));
            var list      = (IEnumerable <int>)listQuery;

            var array = new int[2];

            listQuery.CopyTo(array);
            Assert.Equal(list, array);

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

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

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

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

            array = new int[2];
            ((ICollection)listQuery).CopyTo(array, 0);
            Assert.Equal(list, array);
        }
Exemple #2
0
        protected override List <T> SortTestHelper <T>(ImmutableTrieList <T> list, int index, int count, IComparer <T> comparer)
        {
            var builder = list.ToBuilder();

            builder.Sort(index, count, comparer);
            return(builder.ToImmutable().ToList());
        }
Exemple #3
0
        public void GetEnumeratorExplicit()
        {
            ICollection <int> builder = ImmutableTrieList.Create <int>().ToBuilder();
            var enumerator            = builder.GetEnumerator();

            Assert.NotNull(enumerator);
        }
Exemple #4
0
        protected override List <T> SortTestHelper <T>(ImmutableTrieList <T> list, Comparison <T> comparison)
        {
            var builder = list.ToBuilder();

            builder.Sort(comparison);
            return(builder.ToImmutable().ToList());
        }
        public void RemoveRangeEnumerableTest()
        {
            var list = ImmutableTrieList.Create(1, 2, 3);

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

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

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

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

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

            Assert.Same(list, list.RemoveRange(new[] { 5 }));
            Assert.Same(ImmutableTrieList.Create <int>(), ImmutableTrieList.Create <int>().RemoveRange(new[] { 1 }));

            var listWithDuplicates = ImmutableTrieList.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", () => ((IImmutableList <int>)ImmutableTrieList.Create(1, 2, 3)).RemoveRange(null));
            Assert.Equal(new[] { 1, 3 }, ((IImmutableList <int>)ImmutableTrieList.Create(1, 2, 3)).RemoveRange(new[] { 2 }));
        }
Exemple #6
0
        public void Reverse()
        {
            var mutable = ImmutableTrieList.CreateRange(Enumerable.Range(1, 3)).ToBuilder();

            mutable.Reverse();
            Assert.Equal(Enumerable.Range(1, 3).Reverse(), mutable);
        }
        public void RemoveRange_EnumerableEqualityComparer_AcceptsNullEQ()
        {
            var list       = ImmutableTrieList.Create(1, 2, 3);
            var removed2eq = list.RemoveRange(new[] { 2 }, null);

            Assert.Equal(2, removed2eq.Count);
            Assert.Equal(new[] { 1, 3 }, removed2eq);
        }
        private void TrueForAllTestHelper <T>(ImmutableTrieList <T> list, Predicate <T> test)
        {
            var bclList  = list.ToList();
            var expected = bclList.TrueForAll(test);
            var actual   = this.GetListQuery(list).TrueForAll(test);

            Assert.Equal(expected, actual);
        }
        protected override void RemoveAllTestHelper <T>(ImmutableTrieList <T> list, Predicate <T> test)
        {
            var expected = list.ToList();

            expected.RemoveAll(test);
            var actual = list.RemoveAll(test);

            Assert.Equal <T>(expected, actual.ToList());
        }
Exemple #10
0
 public void IndexOf()
 {
     IndexOfTests.IndexOfTest(
         seq => ImmutableTrieList.CreateRange(seq).ToBuilder(),
         (b, v) => b.IndexOf(v),
         (b, v, i) => b.IndexOf(v, i),
         (b, v, i, c) => b.IndexOf(v, i, c),
         (b, v, i, c, eq) => b.IndexOf(v, i, c, eq));
 }
        protected override void ReverseTestHelper <T>(ImmutableTrieList <T> list, int index, int count)
        {
            var expected = list.ToList();

            expected.Reverse(index, count);
            var actual = list.Reverse(index, count);

            Assert.Equal <T>(expected, actual.ToList());
        }
Exemple #12
0
        protected override void ReverseTestHelper <T>(ImmutableTrieList <T> list, int index, int count)
        {
            var expected = list.ToList();

            expected.Reverse(index, count);
            var builder = list.ToBuilder();

            builder.Reverse(index, count);
            Assert.Equal <T>(expected, builder.ToList());
        }
Exemple #13
0
 public void LastIndexOf()
 {
     IndexOfTests.LastIndexOfTest(
         seq => ImmutableTrieList.CreateRange(seq).ToBuilder(),
         (b, v) => b.LastIndexOf(v),
         (b, v, eq) => b.LastIndexOf(v, b.Count > 0 ? b.Count - 1 : 0, b.Count, eq),
         (b, v, i) => b.LastIndexOf(v, i),
         (b, v, i, c) => b.LastIndexOf(v, i, c),
         (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq));
 }
        public void AddRange_IOrderedCollection()
        {
            var list = ImmutableTrieList <int> .Empty;

            ImmutableTrieList <int> .Builder builder = ImmutableTrieList.CreateBuilder <int>();
            builder.Add(1);

            list = list.AddRange(builder);
            Assert.Equal(new int[] { 1 }, list);
        }
        public void ReverseTest2()
        {
            var emptyList = ImmutableTrieList.Create <int>();

            Assert.Same(emptyList, emptyList.Reverse());

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

            Assert.Equal(Enumerable.Range(1, 3), populatedList.Reverse());
        }
        public void AddSetGetPop()
        {
            const int testSize = 1089;

            List <(ImmutableTrieList <int> list, List <int> expected)> result = new List <(ImmutableTrieList <int>, List <int>)>();

            ImmutableTrieList <int> list = ImmutableTrieList <int> .Empty;

            result.Add((list, list.ToList()));

            // Add
            for (int i = 0; i < testSize; i++)
            {
                var expected = list.ToList();
                expected.Add(i);

                list = list.Add(i);
                result.Add((list, expected));

                Assert.Equal(i, list[i]);
            }

            // Set
            const int offset = 14; // setter offset

            for (int i = 0; i < testSize; i++)
            {
                var expected = list.ToList();
                expected[i] = i + offset;

                list = list.SetItem(i, i + offset);
                result.Add((list, expected));

                Assert.Equal(i + offset, list[i]);
            }

            // Pop
            for (int i = 0; i < testSize; i++)
            {
                int        oldCount = list.Count;
                List <int> expected = list.ToList();
                expected.RemoveAt(oldCount - 1);

                list = list.Pop();
                result.Add((list, expected));

                Assert.Equal(oldCount - 1, list.Count);
            }

            for (int tupleIndex = 0; tupleIndex < result.Count; tupleIndex++)
            {
                var tuple = result[tupleIndex];
                Assert.Equal(tuple.list, tuple.expected);
            }
        }
Exemple #17
0
        public void Clear()
        {
            var mutable = ImmutableTrieList.CreateRange(Enumerable.Range(1, 3)).ToBuilder();

            mutable.Clear();
            Assert.Equal(0, mutable.Count);

            // Do it again for good measure. :)
            mutable.Clear();
            Assert.Equal(0, mutable.Count);
        }
Exemple #18
0
        protected override void RemoveAllTestHelper <T>(ImmutableTrieList <T> list, Predicate <T> test)
        {
            var builder = list.ToBuilder();
            var bcl     = list.ToList();

            int expected = bcl.RemoveAll(test);
            var actual   = builder.RemoveAll(test);

            Assert.Equal(expected, actual);
            Assert.Equal <T>(bcl, builder.ToList());
        }
        public void GetRangeExceptionTest()
        {
            const int count = 100;
            var       list  = ImmutableTrieList.CreateRange(Enumerable.Range(0, count));

            Assert.Throws <ArgumentOutOfRangeException>(() => list.GetRange(-1, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.GetRange(0, -1));
            Assert.Throws <ArgumentException>(() => list.GetRange(count + 1, 0));
            Assert.Throws <ArgumentException>(() => list.GetRange(count, 1));
            Assert.Throws <ArgumentException>(() => list.GetRange(0, count + 1));
        }
        public void IListOfTIsReadOnly()
        {
            IList <int> list = ImmutableTrieList.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);
        }
        /// <summary>
        /// Asserts that the <see cref="ImmutableList{T}"/> or <see cref="ImmutableList{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(ImmutableTrieList.Create(item));

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

            Assert.Equal(expected, actual);
        }
        public void RemoveTest()
        {
            ImmutableTrieList <int> list = ImmutableTrieList <int> .Empty;

            for (int i = 1; i <= 10; i++)
            {
                list = list.Add(i * 10);
            }

            list = list.Remove(30);
            Assert.Equal(9, list.Count);
            Assert.False(list.Contains(30));

            list = list.Remove(100);
            Assert.Equal(8, list.Count);
            Assert.False(list.Contains(100));

            list = list.Remove(10);
            Assert.Equal(7, list.Count);
            Assert.False(list.Contains(10));

            var removeList = new int[] { 20, 70 };

            list = list.RemoveAll(item => removeList.Contains(item));
            Assert.Equal(5, list.Count);
            Assert.False(list.Contains(20));
            Assert.False(list.Contains(70));

            IImmutableList <int> list2 = ImmutableTrieList <int> .Empty;

            for (int i = 1; i <= 10; i++)
            {
                list2 = list2.Add(i * 10);
            }

            list2 = list2.Remove(30);
            Assert.Equal(9, list2.Count);
            Assert.False(list2.Contains(30));

            list2 = list2.Remove(100);
            Assert.Equal(8, list2.Count);
            Assert.False(list2.Contains(100));

            list2 = list2.Remove(10);
            Assert.Equal(7, list2.Count);
            Assert.False(list2.Contains(10));

            list2 = list2.RemoveAll(item => removeList.Contains(item));
            Assert.Equal(5, list2.Count);
            Assert.False(list2.Contains(20));
            Assert.False(list2.Contains(70));
        }
        public void Setup()
        {
            var range = Enumerable.Range(0, N);

            immutableList = ImmutableList <int> .Empty;
            immutableList = immutableList.AddRange(range);

            trieList = ImmutableTrieList <int> .Empty;
            trieList = trieList.AddRange(range);

            list = new List <int>();
            list.AddRange(range);
        }
        public void RemoveRangeArrayTest()
        {
            Assert.True(ImmutableTrieList <int> .Empty.RemoveRange(0, 0).IsEmpty);

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

            Assert.Throws <ArgumentOutOfRangeException>("index", () => list.RemoveRange(-1, 0));
            Assert.Throws <ArgumentOutOfRangeException>("count", () => list.RemoveRange(0, -1));
            Assert.Throws <ArgumentOutOfRangeException>("index", () => list.RemoveRange(4, 0));
            Assert.Throws <ArgumentOutOfRangeException>("count", () => list.RemoveRange(0, 4));
            Assert.Throws <ArgumentOutOfRangeException>("count", () => list.RemoveRange(2, 2));
            Assert.Equal(list, list.RemoveRange(3, 0));
        }
        public void PopTest()
        {
            int count = 1025; // 2^10 + 1
            var list  = ImmutableTrieList.CreateRange(Enumerable.Range(0, count));

            Assert.Equal(Enumerable.Range(0, count), list);

            for (int i = count - 1; i >= 0; i--)
            {
                list = list.Pop();
                Assert.Equal(Enumerable.Range(0, i), list);
            }
        }
        public void Remove_NullEqualityComparer()
        {
            var collection = ImmutableTrieList.Create(1, 2, 3);
            var modified   = collection.Remove(2, null);

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

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

            Assert.Equal(new[] { 1, 3 }, modified2);
        }
Exemple #27
0
        public void RemoveAllBugTest()
        {
            var builder = ImmutableTrieList.CreateBuilder <int>();
            var elemsToRemove = new[] { 0, 1, 2, 3, 4, 5 }.ToImmutableHashSet();

            // NOTE: this uses Add instead of AddRange because AddRange doesn't exhibit the same issue due to a different order of tree building.  Don't change it without testing with the bug repro from issue #20609
            foreach (var elem in new[] { 0, 1, 2, 3, 4, 5, 6 })
            {
                builder.Add(elem);
            }
            builder.RemoveAll(elemsToRemove.Contains);
            Assert.Equal(new[] { 6 }, builder);
        }
        public void Indexer()
        {
            var list = ImmutableTrieList.CreateRange(Enumerable.Range(1, 3));

            Assert.Equal(1, list[0]);
            Assert.Equal(2, list[1]);
            Assert.Equal(3, list[2]);

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

            Assert.Equal(3, ((IList)list)[2]);
            Assert.Equal(3, ((IList <int>)list)[2]);
        }
        public void SetItem()
        {
            var emptyList = ImmutableTrieList.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]);
        }
 public void IndexOf()
 {
     IndexOfTests.IndexOfTest(
         seq => ImmutableTrieList.CreateRange(seq),
         (b, v) => b.IndexOf(v),
         (b, v, i) => b.IndexOf(v, i),
         (b, v, i, c) => b.IndexOf(v, i, c),
         (b, v, i, c, eq) => b.IndexOf(v, i, c, eq));
     IndexOfTests.IndexOfTest(
         seq => (IImmutableList <int>)ImmutableTrieList.CreateRange(seq),
         (b, v) => b.IndexOf(v),
         (b, v, i) => b.IndexOf(v, i),
         (b, v, i, c) => b.IndexOf(v, i, c),
         (b, v, i, c, eq) => b.IndexOf(v, i, c, eq));
 }