Beispiel #1
0
        public void ReadOnlyCollectionBuilder_CopyTo_ArgumentChecking()
        {
            var rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1, 2, 3 });

            Assert.Throws <ArgumentNullException>(() => rocb.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => rocb.CopyTo(new int[3], -1));
            Assert.Throws <ArgumentException>(() => rocb.CopyTo(new int[3], 3)); // NB: Consistent with List<T> behavior
        }
Beispiel #2
0
        public void ReadOnlyCollectionBuilder_ICollection_CopyTo_ArgumentChecking()
        {
            ICollection rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1, 2, 3 });

            Assert.Throws <ArgumentNullException>(() => rocb.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => rocb.CopyTo(new int[3], -1));
            Assert.Throws <ArgumentException>(() => rocb.CopyTo(new int[3], 3)); // NB: Consistent with List<T> behavior
            Assert.Throws <ArgumentException>(() => rocb.CopyTo(new int[3, 3], 0));

            // CONSIDER: Throw ArgumentException instead to be consistent with List<T>, see https://github.com/dotnet/corefx/issues/14059
            Assert.Throws <ArrayTypeMismatchException>(() => rocb.CopyTo(new string[3], 0));
        }
Beispiel #3
0
        public void ReadOnlyCollectionBuilder_ICollection_CopyTo1()
        {
            ICollection rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1, 2, 3 });

            var array = new int[3];

            rocb.CopyTo(array, 0);

            Assert.True(new[] { 1, 2, 3 }.SequenceEqual(array));
        }
Beispiel #4
0
        public void ReadOnlyCollectionBuilder_CopyTo2()
        {
            var rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1, 2, 3 });

            var array = new int[5] {
                1, 2, 3, 4, 5
            };

            rocb.CopyTo(array, 1);

            Assert.True(new[] { 1, 1, 2, 3, 5 }.SequenceEqual(array));
        }
        public void TestReadOnlyCollectionBuilder()
        {
            int cnt = 0;

            // Empty
            ReadOnlyCollectionBuilder <int> a = new ReadOnlyCollectionBuilder <int>();

            AreEqual(0, a.Count);
            AreEqual(0, a.Capacity);
            AreEqual(a.ToReadOnlyCollection().Count, 0);
            AreEqual(a.ToReadOnlyCollection().Count, 0);

            // Simple case
            a.Add(5);
            AreEqual(1, a.Count);
            AreEqual(4, a.Capacity);
            AreEqual(a.ToReadOnlyCollection()[0], 5);
            AreEqual(a.ToReadOnlyCollection().Count, 0);  // Will reset

            a = new ReadOnlyCollectionBuilder <int>(0);
            AreEqual(0, a.Count);
            AssertError <ArgumentException>(() => a = new ReadOnlyCollectionBuilder <int>(-1));

            a = new ReadOnlyCollectionBuilder <int>(5);
            for (int i = 1; i <= 10; i++)
            {
                a.Add(i);
            }

            AreEqual(10, a.Capacity);
            System.Collections.ObjectModel.ReadOnlyCollection <int> readonlyCollection = a.ToReadOnlyCollection();
            AreEqual(0, a.Capacity);
            AreEqual(readonlyCollection.Count, 10);

            ReadOnlyCollectionBuilder <int> b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);

            b.Add(11);
            AreEqual(b.Count, 11);

            AssertError <ArgumentException>(() => a = new ReadOnlyCollectionBuilder <int>(null));

            // Capacity tests
            b.Capacity = 11;
            AssertError <ArgumentException>(() => b.Capacity = 10);
            b.Capacity = 50;
            AreEqual(b.Count, 11);
            AreEqual(b.Capacity, 50);

            // IndexOf cases
            AreEqual(b.IndexOf(5), 4);
            AreEqual(b[4], 5);
            a = new ReadOnlyCollectionBuilder <int>();
            AreEqual(a.IndexOf(5), -1);

            // Insert cases
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            AssertError <ArgumentException>(() => b.Insert(11, 11));
            b.Insert(2, 24);
            AreEqual(b.Count, 11);
            AreEqual(b[1], 2);
            AreEqual(b[2], 24);
            AreEqual(b[3], 3);
            b.Insert(11, 1234);
            AssertError <ArgumentException>(() => b.Insert(-1, 55));
            AreEqual(b[11], 1234);
            AreEqual(b.ToReadOnlyCollection().Count, 12);

            // Remove
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            AreEqual(b.Remove(2), true);
            AreEqual(b[0], 1);
            AreEqual(b[1], 3);
            AreEqual(b[2], 4);
            AreEqual(b.Remove(2), false);

            // RemoveAt
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            b.RemoveAt(2);
            AreEqual(b[1], 2);
            AreEqual(b[2], 4);
            AreEqual(b[3], 5);
            AssertError <ArgumentException>(() => b.RemoveAt(-5));
            AssertError <ArgumentException>(() => b.RemoveAt(9));

            // Clear
            b.Clear();
            AreEqual(b.Count, 0);
            AreEqual(b.ToReadOnlyCollection().Count, 0);
            b = new ReadOnlyCollectionBuilder <int>();
            b.Clear();
            AreEqual(b.Count, 0);

            // Contains
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            AreEqual(b.Contains(5), true);
            AreEqual(b.Contains(-3), false);

            ReadOnlyCollectionBuilder <object> c = new ReadOnlyCollectionBuilder <object>();

            c.Add("HI");
            AreEqual(c.Contains("HI"), true);
            AreEqual(c.Contains(null), false);
            c.Add(null);
            AreEqual(c.Contains(null), true);

            // CopyTo
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            int[] ary = new int[10];
            b.CopyTo(ary, 0);

            AreEqual(ary[0], 1);
            AreEqual(ary[9], 10);

            // Reverse
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            b.Reverse();
            // 1..10
            cnt = 10;
            for (int i = 0; i < 10; i++)
            {
                AreEqual(b[i], cnt--);
            }

            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            AssertError <ArgumentException>(() => b.Reverse(-1, 5));
            AssertError <ArgumentException>(() => b.Reverse(5, -1));
            b.Reverse(3, 3);
            // 1,2,3,4,5,6,7,8,9.10
            // 1,2,3,6,5,4,7,8,9,10
            AreEqual(b[1], 2);
            AreEqual(b[2], 3);
            AreEqual(b[3], 6);
            AreEqual(b[4], 5);
            AreEqual(b[5], 4);
            AreEqual(b[6], 7);

            // ToArray
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            int[] intAry = b.ToArray();
            AreEqual(intAry[0], 1);
            AreEqual(intAry[9], 10);

            b      = new ReadOnlyCollectionBuilder <int>();
            intAry = b.ToArray();
            AreEqual(intAry.Length, 0);

            // IEnumerable cases
            b   = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            cnt = 0;
            foreach (int i in b)
            {
                cnt++;
            }
            AreEqual(cnt, 10);

            b   = new ReadOnlyCollectionBuilder <int>();
            cnt = 0;
            foreach (int i in b)
            {
                cnt++;
            }
            AreEqual(cnt, 0);

            // Error case
            AssertError <InvalidOperationException>(() => ChangeWhileEnumeratingAdd());
            AssertError <InvalidOperationException>(() => ChangeWhileEnumeratingRemove());

            // IList members
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            System.Collections.IList lst = b;

            // IsReadOnly
            AreEqual(lst.IsReadOnly, false);

            // Add
            AreEqual(lst.Add(11), 10);
            AreEqual(lst.Count, 11);
            AssertError <ArgumentException>(() => lst.Add("MOM"));
            AssertError <ArgumentException>(() => lst.Add(null));

            c = new ReadOnlyCollectionBuilder <object>();

            c.Add("HI");
            c.Add(null);
            lst = c;
            lst.Add(null);
            AreEqual(lst.Count, 3);

            // Contains
            lst = b;
            AreEqual(lst.Contains(5), true);
            AreEqual(lst.Contains(null), false);

            lst = c;
            AreEqual(lst.Contains("HI"), true);
            AreEqual(lst.Contains("hi"), false);
            AreEqual(lst.Contains(null), true);

            // IndexOf
            lst = b;
            AreEqual(lst.IndexOf(null), -1);
            AreEqual(lst.IndexOf(1234), -1);
            AreEqual(lst.IndexOf(5), 4);

            // Insert
            b   = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            lst = b;
            AssertError <ArgumentException>(() => lst.Insert(11, 11));
            lst.Insert(2, 24);
            AreEqual(lst.Count, 11);
            AreEqual(lst[1], 2);
            AreEqual(lst[2], 24);
            AreEqual(lst[3], 3);
            lst.Insert(11, 1234);
            AssertError <ArgumentException>(() => lst.Insert(-1, 55));
            AreEqual(lst[11], 1234);

            AssertError <ArgumentException>(() => lst.Insert(3, "MOM"));

            // IsFixedSize
            AreEqual(lst.IsFixedSize, false);

            // Remove
            b   = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            lst = b;
            lst.Remove(2);
            AreEqual(lst[0], 1);
            AreEqual(lst[1], 3);
            AreEqual(lst[2], 4);
            lst.Remove(2);

            // Indexing
            lst[3] = 234;
            AreEqual(lst[3], 234);
            AssertError <ArgumentException>(() => lst[3] = null);
            AssertError <ArgumentException>(() => lst[3] = "HI");

            // ICollection<T>

            // IsReadOnly
            System.Collections.Generic.ICollection <int> col = b;
            AreEqual(col.IsReadOnly, false);

            // ICollection
            b = new ReadOnlyCollectionBuilder <int>(readonlyCollection);
            System.Collections.ICollection col2 = b;
            AreEqual(col2.IsSynchronized, false);
            Assert(col2.SyncRoot != null);
            intAry = new int[10];
            col2.CopyTo(intAry, 0);
            AreEqual(intAry[0], 1);
            AreEqual(intAry[9], 10);

            string[] str = new string[50];
            AssertError <ArrayTypeMismatchException>(() => col2.CopyTo(str, 0));
        }