public void TestImmutableSortedTreeListCreateRangeValidation()
 {
     Assert.Throws <ArgumentNullException>("items", () => ImmutableSortedTreeList.CreateRange <int>(null !));
     Assert.Throws <ArgumentNullException>("items", () => ImmutableSortedTreeList.CreateRange(Comparer <int> .Default, null !));
     Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <int>) !.ToImmutableSortedTreeList());
     Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <int>) !.ToImmutableSortedTreeList(Comparer <int> .Default));
 }
        public void TestImmutableSortedTreeListCreateRange()
        {
            var values = new[] { Generator.GetInt32(), Generator.GetInt32(), Generator.GetInt32() };
            var list   = ImmutableSortedTreeList.CreateRange(values);

            Assert.Equal(values.OrderBy(x => x), list);
        }
Ejemplo n.º 3
0
        public void TestLastIndexOfInvalidOperations()
        {
            var single = ImmutableSortedTreeList.CreateRange(Enumerable.Range(1, 1)).ToBuilder();

            Assert.Throws <ArgumentException>(() => single.LastIndexOf(0, 1, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => single.LastIndexOf(0, 0, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => single.LastIndexOf(0, 0, 2));
        }
Ejemplo n.º 4
0
        public void TestTrimExcess()
        {
            ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>();
            List <int> reference = new List <int>();

            for (int i = 0; i < 4 * 8 * 8; i++)
            {
                int value = Generator.GetInt32(list.Count + 1);
                list.Add(i);
                reference.Add(i);
            }

            reference.Sort();

            list.Validate(ValidationRules.None);

            // In the first call to TrimExcess, items will move
            list.TrimExcess();
            list.Validate(ValidationRules.RequirePacked);
            Assert.Equal(reference, list);

            // In the second call, the list is already packed so nothing will move
            list.TrimExcess();
            list.Validate(ValidationRules.RequirePacked);
            Assert.Equal(reference, list);

            ImmutableSortedTreeList <int> .Builder empty = ImmutableSortedTreeList.CreateBuilder <int>();
            empty.Validate(ValidationRules.RequirePacked);
            empty.TrimExcess();
            empty.Validate(ValidationRules.RequirePacked);

            var single = ImmutableSortedTreeList.CreateRange(Enumerable.Range(0, 1)).ToBuilder();

            single.Validate(ValidationRules.RequirePacked);
            single.TrimExcess();
            single.Validate(ValidationRules.RequirePacked);

            // Construct a poorly-packed list with several levels
            ImmutableSortedTreeList <int> .Builder binary = ImmutableSortedTreeList.CreateBuilder <int>();
            for (int i = 5000; i >= 0; i--)
            {
                binary.Add(i);
            }

            binary.TrimExcess();
            binary.Validate(ValidationRules.RequirePacked);

            // Construct a poorly-packed list with several levels
            ImmutableSortedTreeList <int> .Builder ternary = ImmutableSortedTreeList.CreateRange <int>(comparer: null, Enumerable.Range(0, 5000)).ToBuilder();
            for (int i = 5000; i >= 0; i--)
            {
                ternary.Add(i);
            }

            ternary.TrimExcess();
            ternary.Validate(ValidationRules.RequirePacked);
        }
Ejemplo n.º 5
0
        public void TestRemoveValue()
        {
            var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 10)).ToBuilder();

            Assert.False(list.Remove(-1));
            Assert.Equal(10, list.Count);
            Assert.True(list.Remove(3));
            Assert.Equal(9, list.Count);
        }
Ejemplo n.º 6
0
        public void TestIListTInterface()
        {
            IList <int> list = ImmutableSortedTreeList.CreateRange <int>(Enumerable.Range(0, 10)).ToBuilder();

            Assert.False(list.IsReadOnly);
            Assert.Equal(0, list[0]);

            Assert.Throws <NotSupportedException>(() => list.Insert(0, 0));
        }
Ejemplo n.º 7
0
        public void TestRemoveAll()
        {
            var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 10)).ToBuilder();

            Assert.Throws <ArgumentNullException>(() => list.RemoveAll(null !));

            Assert.Equal(5, list.RemoveAll(i => (i % 2) == 0));
            Assert.Equal(new[] { 1, 3, 5, 7, 9 }, list);
            Assert.Equal(0, list.RemoveAll(i => i < 0));
            Assert.Equal(5, list.Count);
        }
Ejemplo n.º 8
0
        public void TestCopyToValidation()
        {
            var list = ImmutableSortedTreeList.CreateRange <int>(Enumerable.Range(0, 10)).ToBuilder();

            Assert.Throws <ArgumentNullException>("array", () => list.CopyTo(0, null !, 0, list.Count));
            Assert.Throws <ArgumentOutOfRangeException>("index", () => list.CopyTo(-1, new int[list.Count], 0, list.Count));
            Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => list.CopyTo(0, new int[list.Count], -1, list.Count));
            Assert.Throws <ArgumentOutOfRangeException>("count", () => list.CopyTo(0, new int[list.Count], 0, -1));
            Assert.Throws <ArgumentOutOfRangeException>(null, () => list.CopyTo(1, new int[list.Count], 0, list.Count));
            Assert.Throws <ArgumentOutOfRangeException>(string.Empty, () => list.CopyTo(0, new int[list.Count], 1, list.Count));
        }
Ejemplo n.º 9
0
        public void TestIndexer()
        {
            var list = ImmutableSortedTreeList.CreateRange <int>(null, Enumerable.Range(0, 10)).ToBuilder();

            Assert.Throws <ArgumentOutOfRangeException>(() => list[-1]);
            Assert.Throws <ArgumentOutOfRangeException>(() => list[list.Count]);
            Assert.Throws <NotSupportedException>(() => ((IList <int>)list)[-1]         = 0);
            Assert.Throws <NotSupportedException>(() => ((IList <int>)list)[list.Count] = 0);

            for (int i = 0; i < list.Count; i++)
            {
                Assert.Equal(i, list[i]);
            }
        }
Ejemplo n.º 10
0
        public void TestExplicitComparer()
        {
            var objComparer        = new ComparisonComparer <object>((x, y) => 0);
            var intComparer        = new ComparisonComparer <int>((x, y) => 0);
            var comparableComparer = new ComparisonComparer <IComparable>((x, y) => 0);

            Assert.Same(objComparer, ImmutableSortedTreeList.CreateBuilder <object>(comparer: objComparer).Comparer);
            Assert.Same(intComparer, ImmutableSortedTreeList.CreateBuilder <int>(comparer: intComparer).Comparer);
            Assert.Same(comparableComparer, ImmutableSortedTreeList.CreateBuilder <IComparable>(comparer: comparableComparer).Comparer);

            Assert.Same(objComparer, ImmutableSortedTreeList.CreateRange <object>(comparer: objComparer, Enumerable.Empty <object>()).ToBuilder().Comparer);
            Assert.Same(intComparer, ImmutableSortedTreeList.CreateRange <int>(comparer: intComparer, Enumerable.Empty <int>()).ToBuilder().Comparer);
            Assert.Same(comparableComparer, ImmutableSortedTreeList.CreateRange <IComparable>(comparer: comparableComparer, Enumerable.Empty <IComparable>()).ToBuilder().Comparer);
        }
Ejemplo n.º 11
0
        public void TestFind()
        {
            var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(1, 10)).ToBuilder();

            Assert.Throws <ArgumentNullException>(() => list.Find(null !));

            Assert.Equal(0, list.Find(value => value < 0));
            foreach (var i in list)
            {
                Assert.Equal(i, list.Find(value => value == i));
            }

            Assert.Equal(2, list.Find(value => value > 1));
        }
Ejemplo n.º 12
0
        public void TestFindAll()
        {
            var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 10)).ToBuilder();

            Assert.Throws <ArgumentNullException>(() => list.FindAll(null !));

            ImmutableSortedTreeList <int> found = list.FindAll(i => (i % 2) == 0);

            Assert.Equal(10, list.Count);
            Assert.Equal(5, found.Count);
            Assert.Equal(new[] { 0, 2, 4, 6, 8 }, found);

            Assert.Empty(list.FindAll(i => i < 0));
        }
Ejemplo n.º 13
0
        public void TestExists()
        {
            var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 10)).ToBuilder();

            Assert.Throws <ArgumentNullException>(() => list.Exists(null !));

            Assert.False(list.Exists(value => value < 0));
            foreach (var i in list)
            {
                Assert.True(list.Exists(value => value == i));
            }

            Assert.False(list.Exists(value => value > 10));
        }
Ejemplo n.º 14
0
        public void TestForEach()
        {
            var list      = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 100)).ToBuilder();
            var reference = new List <int>(Enumerable.Range(0, 100));

            Assert.Throws <ArgumentNullException>("action", () => list.ForEach(null !));
            Assert.Throws <ArgumentNullException>(() => reference.ForEach(null !));

            var listOutput      = new List <int>();
            var referenceOutput = new List <int>();

            list.ForEach(listOutput.Add);
            reference.ForEach(referenceOutput.Add);
            Assert.Equal(referenceOutput, listOutput);
        }
Ejemplo n.º 15
0
        public void TestFindLast()
        {
            var list      = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(1, 10)).ToBuilder();
            var reference = new List <int>(Enumerable.Range(1, 10));

            Assert.Throws <ArgumentNullException>(() => list.FindLast(null !));

            Assert.Equal(0, list.FindLast(i => i < 0));
            Assert.Equal(0, reference.FindLast(i => i < 0));

            Assert.Equal(10, list.FindLast(value => (value % 2) == 0));
            Assert.Equal(10, reference.FindLast(value => (value % 2) == 0));

            Assert.Equal(4, list.FindLast(value => value < 5));
            Assert.Equal(4, reference.FindLast(value => value < 5));
        }
Ejemplo n.º 16
0
        public void TestDefaultComparer()
        {
            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeList.CreateBuilder <object>().Comparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeList.CreateBuilder <int>().Comparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeList.CreateBuilder <IComparable>().Comparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeList.CreateRange <object>(Enumerable.Empty <object>()).ToBuilder().Comparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeList.CreateRange <int>(Enumerable.Empty <int>()).ToBuilder().Comparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeList.CreateRange <IComparable>(Enumerable.Empty <IComparable>()).ToBuilder().Comparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeList.CreateBuilder <object>(comparer: null).Comparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeList.CreateBuilder <int>(comparer: null).Comparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeList.CreateBuilder <IComparable>(comparer: null).Comparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeList.CreateRange <object>(comparer: null, Enumerable.Empty <object>()).ToBuilder().Comparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeList.CreateRange <int>(comparer: null, Enumerable.Empty <int>()).ToBuilder().Comparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeList.CreateRange <IComparable>(comparer: null, Enumerable.Empty <IComparable>()).ToBuilder().Comparer);
        }
Ejemplo n.º 17
0
        public void TestGetRange()
        {
            var list      = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 100)).ToBuilder();
            var reference = new List <int>(Enumerable.Range(0, 100));

            ImmutableSortedTreeList <int> subList = list.GetRange(10, 80);
            List <int> subReference = reference.GetRange(10, 80);

            Assert.Equal(subReference, subList);

            // Verify that changes to the original list do not affect previous calls to GetRange
            int[] values = subList.ToArray();
            list.Add(list[list.Count / 2]);
            reference.Insert((reference.Count / 2) + 1, reference[reference.Count / 2]);
            Assert.Equal(reference, list);
            Assert.Equal(values, subList);
            Assert.Equal(values, subReference);
        }
Ejemplo n.º 18
0
        public void TestCopyTo()
        {
            var list      = ImmutableSortedTreeList.CreateRange(comparer: null, items: Enumerable.Range(0, 100)).ToBuilder();
            var reference = new List <int>(Enumerable.Range(0, 100));

            int[] listArray      = new int[list.Count * 2];
            int[] referenceArray = new int[reference.Count * 2];

            list.CopyTo(listArray);
            reference.CopyTo(referenceArray);
            Assert.Equal(referenceArray, listArray);

            list.CopyTo(listArray, 0);
            Assert.Equal(referenceArray, listArray);

            list.CopyTo(listArray, list.Count / 2);
            reference.CopyTo(referenceArray, reference.Count / 2);
            Assert.Equal(referenceArray, listArray);
        }