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> /// 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 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 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]); }