public void SerializeDeserialize_Roundtrips(CiccioList <int> c) { CiccioList <int> clone = BinaryFormatterHelpers.Clone(c); Assert.NotSame(c, clone); Assert.Equal(c, clone); }
public static void ParameterlessConstructorTest() { var col = new CiccioList <string>(); Assert.Equal(0, col.Count); Assert.Empty(col); }
public static void CopyToTest() { string[] anArray = new string[] { "one", "two", "three", "four" }; CiccioList <string> collection = new CiccioList <string>((IEnumerable <string>)anArray); string[] aCopy = new string[collection.Count]; collection.CopyTo(aCopy, 0); for (int i = 0; i < anArray.Length; ++i) { Assert.Equal(anArray[i], aCopy[i]); } // copy observable collection starting in middle, where array is larger than source. aCopy = new string[collection.Count + 2]; int offsetIndex = 1; collection.CopyTo(aCopy, offsetIndex); for (int i = 0; i < aCopy.Length; i++) { string value = aCopy[i]; if (i == 0) { Assert.True(null == value, "Should not have a value since we did not start copying there."); } else if (i == (aCopy.Length - 1)) { Assert.True(null == value, "Should not have a value since the collection is shorter than the copy array.."); } else { int indexInCollection = i - offsetIndex; Assert.Equal(collection[indexInCollection], aCopy[i]); } } }
public static void CopyToTest_Negative() { string[] anArray = new string[] { "one", "two", "three", "four" }; CiccioList <string> collection = new CiccioList <string>(anArray); int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue }; foreach (var index in iArrInvalidValues) { string[] aCopy = new string[collection.Count]; AssertExtensions.Throws <ArgumentOutOfRangeException>("destinationIndex", "dstIndex", () => collection.CopyTo(aCopy, index)); } int[] iArrLargeValues = new int[] { collection.Count, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 }; foreach (var index in iArrLargeValues) { string[] aCopy = new string[collection.Count]; AssertExtensions.Throws <ArgumentException>("destinationArray", null, () => collection.CopyTo(aCopy, index)); } AssertExtensions.Throws <ArgumentNullException>("destinationArray", "dest", () => collection.CopyTo(null, 1)); string[] copy = new string[collection.Count - 1]; AssertExtensions.Throws <ArgumentException>("destinationArray", "", () => collection.CopyTo(copy, 0)); copy = new string[0]; AssertExtensions.Throws <ArgumentException>("destinationArray", "", () => collection.CopyTo(copy, 0)); }
public static void InsertTest() { Guid[] anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() }; CiccioList <Guid> col0 = new CiccioList <Guid>((IEnumerable <Guid>)anArray); CiccioList <Guid> col1 = new CiccioList <Guid>((IEnumerable <Guid>)anArray); CiccioList <Guid> col3 = new CiccioList <Guid>((IEnumerable <Guid>)anArray); //inserting item at the beginning. Guid g0 = Guid.NewGuid(); col0.Insert(0, g0); Assert.Equal(g0, col0[0]); // inserting item in the middle Guid g1 = Guid.NewGuid(); col1.Insert(1, g1); Assert.Equal(g1, col1[1]); // inserting item at the end. Guid g3 = Guid.NewGuid(); col3.Insert(col3.Count, g3); Assert.Equal(g3, col3[col3.Count - 1]); string[] anArrayString = new string[] { "one", "two", "three", "four" }; CiccioList <string> collection = new CiccioList <string>((IEnumerable <string>)anArrayString); CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester(); helper.AddOrInsertItemTest(collection, "seven", 2); helper.AddOrInsertItemTest(collection, "zero", 0); helper.AddOrInsertItemTest(collection, "eight", collection.Count); }
/// <summary> /// Clears the given Collection. /// </summary> public void ClearTest(CiccioList <string> collection) { INotifyPropertyChanged collectionPropertyChanged = collection; collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged; _expectedPropertyChanged = new[] { new PropertyNameExpected(COUNT), new PropertyNameExpected(ITEMARRAY) }; collection.CollectionChanged += Collection_CollectionChanged; ExpectedCollectionChangedFired++; ExpectedAction = NotifyCollectionChangedAction.Reset; ExpectedNewItems = null; ExpectedNewStartingIndex = -1; ExpectedOldItems = null; ExpectedOldStartingIndex = -1; collection.Clear(); Assert.Equal(0, collection.Count); Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired); foreach (var item in _expectedPropertyChanged) { Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just cleared the collection"); } collection.CollectionChanged -= Collection_CollectionChanged; collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged; }
public static void IEnumerableConstructorTest_Empty() { var col = new CiccioList <string>(new string[] { }); Assert.Equal(0, col.Count); Assert.Empty(col); }
///// <summary> ///// Given a collection, will move an item from the oldIndex to the newIndex. ///// </summary> //public void MoveItemTest(CiccioList<string> collection, int oldIndex, int newIndex) //{ // INotifyPropertyChanged collectionPropertyChanged = collection; // collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged; // _expectedPropertyChanged = new[] { new PropertyNameExpected(ITEMARRAY) }; // collection.CollectionChanged += Collection_CollectionChanged; // string itemAtOldIndex = collection[oldIndex]; // ExpectedCollectionChangedFired++; // ExpectedAction = NotifyCollectionChangedAction.Move; // ExpectedNewItems = new string[] { itemAtOldIndex }; // ExpectedNewStartingIndex = newIndex; // ExpectedOldItems = new string[] { itemAtOldIndex }; // ExpectedOldStartingIndex = oldIndex; // int expectedCount = collection.Count; // collection.Move(oldIndex, newIndex); // Assert.Equal(expectedCount, collection.Count); // Assert.Equal(itemAtOldIndex, collection[newIndex]); // Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired); // foreach (var item in _expectedPropertyChanged) // Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just moved an item"); // collection.CollectionChanged -= Collection_CollectionChanged; // collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged; //} /// <summary> /// Will set that new item at the specified index in the given collection. /// </summary> public void ReplaceItemTest(CiccioList <string> collection, int index, string newItem) { INotifyPropertyChanged collectionPropertyChanged = collection; collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged; _expectedPropertyChanged = new[] { new PropertyNameExpected(ITEMARRAY) }; collection.CollectionChanged += Collection_CollectionChanged; string itemAtOldIndex = collection[index]; ExpectedCollectionChangedFired++; ExpectedAction = NotifyCollectionChangedAction.Replace; ExpectedNewItems = new string[] { newItem }; ExpectedNewStartingIndex = index; ExpectedOldItems = new string[] { itemAtOldIndex }; ExpectedOldStartingIndex = index; int expectedCount = collection.Count; collection[index] = newItem; Assert.Equal(expectedCount, collection.Count); Assert.Equal(newItem, collection[index]); Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired); foreach (var item in _expectedPropertyChanged) { Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just replaced an item"); } collection.CollectionChanged -= Collection_CollectionChanged; collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged; }
public static void IndexOfTest() { string[] anArray = new string[] { "one", "two", "three", "four" }; CiccioList <string> collection = new CiccioList <string>((IEnumerable <string>)anArray); for (int i = 0; i < anArray.Length; ++i) { Assert.Equal(i, collection.IndexOf(anArray[i])); } Assert.Equal(-1, collection.IndexOf("seven")); Assert.Equal(-1, collection.IndexOf(null)); // testing that the first occurrence is the index returned. CiccioList <int> intCol = new CiccioList <int>(); for (int i = 0; i < 4; ++i) { intCol.Add(i % 2); } Assert.Equal(0, intCol.IndexOf(0)); Assert.Equal(1, intCol.IndexOf(1)); IList colAsIList = (IList)intCol; var index = colAsIList.IndexOf("stringObj"); Assert.Equal(-1, index); }
/// <summary> /// Given a collection, index and item to remove, will try to remove that item /// from the index. If the item has duplicates, will verify that only the first /// instance was removed. /// </summary> public void RemoveItemTest(CiccioList <string> collection, int itemIndex, string itemToRemove, bool isSuccessfulRemove, bool hasDuplicates) { INotifyPropertyChanged collectionPropertyChanged = collection; collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged; _expectedPropertyChanged = new[] { new PropertyNameExpected(COUNT), new PropertyNameExpected(ITEMARRAY) }; collection.CollectionChanged += Collection_CollectionChanged; if (isSuccessfulRemove) { ExpectedCollectionChangedFired++; } ExpectedAction = NotifyCollectionChangedAction.Remove; ExpectedNewItems = null; ExpectedNewStartingIndex = -1; ExpectedOldItems = new string[] { itemToRemove }; ExpectedOldStartingIndex = itemIndex; int expectedCount = isSuccessfulRemove ? collection.Count - 1 : collection.Count; bool removedItem = collection.Remove(itemToRemove); Assert.Equal(expectedCount, collection.Count); Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired); if (isSuccessfulRemove) { foreach (var item in _expectedPropertyChanged) { Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since there were items removed."); } Assert.True(removedItem, "Should have been successful in removing the item."); } else { foreach (var item in _expectedPropertyChanged) { Assert.False(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since there were no items removed."); } Assert.False(removedItem, "Should not have been successful in removing the item."); } if (hasDuplicates) { return; } Assert.DoesNotContain(itemToRemove, collection); collection.CollectionChanged -= Collection_CollectionChanged; collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged; }
public static void AddTest() { string[] anArray = { "one", "two", "three" }; CiccioList <string> col = new CiccioList <string>(anArray); CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester(); helper.AddOrInsertItemTest(col, "four"); }
public static void ReplaceItemTest() { string[] anArray = new string[] { "one", "two", "three", "four" }; CiccioList <string> collection = new CiccioList <string>((IEnumerable <string>)anArray); CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester(); helper.ReplaceItemTest(collection, 1, "seven"); helper.ReplaceItemTest(collection, 3, "zero"); }
public static void ListConstructorTest() { List <string> collection = new List <string> { "one", "two", "three" }; var actual = new CiccioList <string>(collection); Assert.Equal(collection, actual); }
public void OnDeserialized_MonitorNotInitialized_ExpectSuccess() { var observableCollection = new CiccioList <int>(); MethodInfo onDeserializedMethodInfo = observableCollection.GetType().GetMethod("OnDeserialized", BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); Assert.NotNull(onDeserializedMethodInfo); onDeserializedMethodInfo.Invoke(observableCollection, new object[] { null }); }
public static void DebuggerAttributeTests() { CiccioList <int> col = new CiccioList <int>(new[] { 1, 2, 3, 4 }); DebuggerAttributes.ValidateDebuggerDisplayReferences(col); DebuggerAttributeInfo info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(col); PropertyInfo itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute <DebuggerBrowsableAttribute>().State == DebuggerBrowsableState.RootHidden); int[] items = itemProperty.GetValue(info.Instance) as int[]; Assert.Equal(col, items); }
public static void ItemTestSet() { var col = new CiccioList <Guid>(new[] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() }); for (int i = 0; i < col.Count; ++i) { Guid guid = Guid.NewGuid(); col[i] = guid; Assert.Equal(guid, col[i]); } }
/// <summary> /// Will perform an Add or Insert on the given Collection depending on whether the /// insertIndex is null or not. If it is null, will Add, otherwise, will Insert. /// </summary> public void AddOrInsertItemTest(CiccioList <string> collection, string itemToAdd, int?insertIndex = null) { INotifyPropertyChanged collectionPropertyChanged = collection; collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged; _expectedPropertyChanged = new[] { new PropertyNameExpected(COUNT), new PropertyNameExpected(ITEMARRAY) }; collection.CollectionChanged += Collection_CollectionChanged; ExpectedCollectionChangedFired++; ExpectedAction = NotifyCollectionChangedAction.Add; ExpectedNewItems = new string[] { itemToAdd }; if (insertIndex.HasValue) { ExpectedNewStartingIndex = insertIndex.Value; } else { ExpectedNewStartingIndex = collection.Count; } ExpectedOldItems = null; ExpectedOldStartingIndex = -1; int expectedCount = collection.Count + 1; if (insertIndex.HasValue) { collection.Insert(insertIndex.Value, itemToAdd); Assert.Equal(itemToAdd, collection[insertIndex.Value]); } else { collection.Add(itemToAdd); Assert.Equal(itemToAdd, collection[collection.Count - 1]); } Assert.Equal(expectedCount, collection.Count); Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired); foreach (var item in _expectedPropertyChanged) { Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just added an item"); } collection.CollectionChanged -= Collection_CollectionChanged; collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged; }
public static void GetEnumeratorTest() { Guid[] anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() }; CiccioList <Guid> col = new CiccioList <Guid>((IEnumerable <Guid>)anArray); int i = 0; IEnumerator <Guid> e; for (e = col.GetEnumerator(); e.MoveNext(); ++i) { Assert.Equal(anArray[i], e.Current); } Assert.Equal(col.Count, i); e.Dispose(); }
public static void ClearTest() { string[] anArray = { "one", "two", "three", "four" }; CiccioList <string> col = new CiccioList <string>(anArray); col.Clear(); Assert.Equal(0, col.Count); Assert.Empty(col); AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => col[1]); //tests that the collectionChanged events are fired. CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester(); col = new CiccioList <string>(anArray); helper.ClearTest(col); }
public static void RemoveAtTest_Negative() { Guid[] anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() }; CiccioList <Guid> collection = new CiccioList <Guid>(anArray); collection.CollectionChanged += (o, e) => { throw new ShouldNotBeInvokedException(); }; int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue }; foreach (var index in iArrInvalidValues) { AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => collection.RemoveAt(index)); Assert.Equal(anArray.Length, collection.Count); } int[] iArrLargeValues = new int[] { collection.Count, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 }; foreach (var index in iArrLargeValues) { AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => collection.RemoveAt(index)); Assert.Equal(anArray.Length, collection.Count); } }
public static void ContainsTest() { string[] anArray = new string[] { "one", "two", "three", "four" }; CiccioList <string> collection = new CiccioList <string>((IEnumerable <string>)anArray); string collectionString = ""; foreach (var item in collection) { collectionString += item + ", "; } for (int i = 0; i < collection.Count; ++i) { Assert.True(collection.Contains(anArray[i]), "ObservableCollection did not contain the item: " + anArray[i] + " Collection: " + collectionString); } string g = "six"; Assert.False(collection.Contains(g), "Collection contained an item that should not have been there. guid: " + g + " Collection: " + collectionString); Assert.False(collection.Contains(null), "Collection should not have contained null. Collection: " + collectionString); }
public void Reentrancy_MultipleListeners_Throws() { bool handler1Called = false; bool handler2Called = false; var collection = new CiccioList <int>(); collection.CollectionChanged += (sender, e) => { handler1Called = true; }; collection.CollectionChanged += (sender, e) => { handler2Called = true; // More than one listener; throws. Assert.Throws <InvalidOperationException>(() => collection.Add(2)); }; collection.Add(1); Assert.True(handler1Called); Assert.True(handler2Called); Assert.Equal(1, collection.Count); Assert.Equal(1, collection[0]); }
public static void RemoveAtTest() { Guid[] anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() }; CiccioList <Guid> col0 = new CiccioList <Guid>((IEnumerable <Guid>)anArray); CiccioList <Guid> col1 = new CiccioList <Guid>((IEnumerable <Guid>)anArray); CiccioList <Guid> col2 = new CiccioList <Guid>((IEnumerable <Guid>)anArray); col0.RemoveAt(0); string collectionString = ""; foreach (var item in col1) { collectionString += item + ", "; } Assert.False(col0.Contains(anArray[0]), "Collection0 should no longer contain the item: " + anArray[0] + " Collection: " + collectionString); col1.RemoveAt(1); collectionString = ""; foreach (var item in col1) { collectionString += item + ", "; } Assert.False(col1.Contains(anArray[1]), "Collection1 should no longer contain the item: " + anArray[1] + " Collection: " + collectionString); col2.RemoveAt(2); collectionString = ""; foreach (var item in col2) { collectionString += item + ", "; } Assert.False(col2.Contains(anArray[2]), "Collection2 should no longer contain the item: " + anArray[2] + " Collection: " + collectionString); string[] anArrayString = { "one", "two", "three", "four" }; CiccioList <string> col = new CiccioList <string>(anArrayString); CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester(); helper.RemoveItemAtTest(col, 1); }
public static void RemoveTest() { // trying to remove item in collection. string[] anArray = { "one", "two", "three", "four" }; CiccioList <string> col = new CiccioList <string>(anArray); CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester(); helper.RemoveItemTest(col, 2, "three", true, hasDuplicates: false); // trying to remove item not in collection. anArray = new string[] { "one", "two", "three", "four" }; col = new CiccioList <string>(anArray); helper = new CollectionAndPropertyChangedTester(); helper.RemoveItemTest(col, -1, "three2", false, hasDuplicates: false); // removing null anArray = new string[] { "one", "two", "three", "four" }; col = new CiccioList <string>(anArray); helper = new CollectionAndPropertyChangedTester(); helper.RemoveItemTest(col, -1, null, false, hasDuplicates: false); // trying to remove item in collection that has duplicates. anArray = new string[] { "one", "three", "two", "three", "four" }; col = new CiccioList <string>(anArray); helper = new CollectionAndPropertyChangedTester(); helper.RemoveItemTest(col, 1, "three", true, hasDuplicates: true); // want to ensure that there is one "three" left in collection and not both were removed. int occurrencesThree = 0; foreach (var item in col) { if (item.Equals("three")) { occurrencesThree++; } } Assert.Equal(1, occurrencesThree); }
public void Reentrancy_SingleListener_DoesNotThrow() { bool handlerCalled = false; var collection = new CiccioList <int>(); collection.CollectionChanged += (sender, e) => { if (!handlerCalled) { handlerCalled = true; // Single listener; does not throw. collection.Add(2); } }; collection.Add(1); Assert.True(handlerCalled); Assert.Equal(2, collection.Count); Assert.Equal(1, collection[0]); Assert.Equal(2, collection[1]); }
public static void ItemTestSet_Negative_InvalidIndex(int size, int index) { var col = new CiccioList <int>(new int[size]); AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => col[index]); }
public static void IsReadOnlyTest() { var col = new CiccioList <Guid>(new[] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() }); Assert.False(((ICollection <Guid>)col).IsReadOnly); }
public static void IEnumerableConstructorTest(IEnumerable <string> collection) { var actual = new CiccioList <string>(collection); Assert.Equal(collection, actual); }