public void ReadOnlyCollectionBuilder_Remove() { var rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1, 2, 2, 4 }); Assert.True(new[] { 1, 2, 2, 4 }.SequenceEqual(rocb)); Assert.False(rocb.Remove(0)); Assert.True(new[] { 1, 2, 2, 4 }.SequenceEqual(rocb)); Assert.True(rocb.Remove(2)); Assert.True(new[] { 1, 2, 4 }.SequenceEqual(rocb)); Assert.True(rocb.Remove(1)); Assert.True(new[] { 2, 4 }.SequenceEqual(rocb)); Assert.True(rocb.Remove(4)); Assert.True(new[] { 2 }.SequenceEqual(rocb)); Assert.True(rocb.Remove(2)); Assert.Equal(0, rocb.Count); }
public void ReadOnlyCollectionBuilder_IList_Remove() { IList rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1, 2, 2, 4 }); Assert.True(new[] { 1, 2, 2, 4 }.SequenceEqual(rocb.Cast <int>())); rocb.Remove(0); rocb.Remove("bar"); rocb.Remove(null); Assert.True(new[] { 1, 2, 2, 4 }.SequenceEqual(rocb.Cast <int>())); rocb.Remove(2); Assert.True(new[] { 1, 2, 4 }.SequenceEqual(rocb.Cast <int>())); rocb.Remove(1); Assert.True(new[] { 2, 4 }.SequenceEqual(rocb.Cast <int>())); rocb.Remove(4); Assert.True(new[] { 2 }.SequenceEqual(rocb.Cast <int>())); rocb.Remove(2); Assert.Equal(0, rocb.Count); }
void ChangeWhileEnumeratingRemove() { ReadOnlyCollectionBuilder <int> b = new ReadOnlyCollectionBuilder <int>(); b.Add(5); b.Add(6); foreach (int i in b) { b.Remove(5); } }
private static void AssertEmpty <T>(ReadOnlyCollectionBuilder <T> rocb) { Assert.Equal(0, rocb.Count); Assert.False(rocb.Contains(default(T))); Assert.False(rocb.Remove(default(T))); Assert.InRange(rocb.IndexOf(default(T)), int.MinValue, -1); IEnumerator <T> e = rocb.GetEnumerator(); Assert.False(e.MoveNext()); }
public void ReadOnlyCollectionBuilder_IndexOf() { var rocb = new ReadOnlyCollectionBuilder <int>(new[] { 1, 2, 2, 3 }); Assert.Equal(4, rocb.Count); Assert.Equal(0, rocb.IndexOf(1)); Assert.Equal(1, rocb.IndexOf(2)); Assert.Equal(3, rocb.IndexOf(3)); Assert.InRange(rocb.IndexOf(0), int.MinValue, -1); Assert.InRange(rocb.IndexOf(4), int.MinValue, -1); rocb.Capacity = 5; Assert.Equal(4, rocb.Count); Assert.InRange(rocb.IndexOf(0), int.MinValue, -1); // No default values leak in through underlying array Assert.True(rocb.Remove(3)); Assert.Equal(3, rocb.Count); Assert.InRange(rocb.IndexOf(0), int.MinValue, -1); // No default values leak in through underlying array Assert.InRange(rocb.IndexOf(3), int.MinValue, -1); }
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)); }