/* Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using C5; using NUnit.Framework; using SCG = System.Collections.Generic; using System.Reflection; namespace C5UnitTests.Templates.Events { public abstract class CollectionValueTester : GenericCollectionTester where TCollection : ICollectionValue { protected TCollection collection; protected CollectionEventList seen; protected EventTypeEnum listenTo; protected void listen() { seen.Listen(collection, listenTo); } public override void SetUp(TCollection list, EventTypeEnum testSpec) { this.collection = list; listenTo = testSpec; seen = new CollectionEventList(EqualityComparer.Default); } public SCG.IEnumerable SpecsBasic { get { CircularQueue specs = new CircularQueue(); //foreach (EventTypeEnum listenTo in Enum.GetValues(typeof(EventTypeEnum))) // if ((listenTo & ~EventTypeEnum.Basic) == 0) // specs.Enqueue(listenTo); //specs.Enqueue(EventTypeEnum.Added | EventTypeEnum.Removed); for (int spec = 0; spec <= (int)EventTypeEnum.Basic; spec++) specs.Enqueue((EventTypeEnum)spec); return specs; } } public SCG.IEnumerable SpecsAll { get { CircularQueue specs = new CircularQueue(); //foreach (EventTypeEnum listenTo in Enum.GetValues(typeof(EventTypeEnum))) // specs.Enqueue(listenTo); //specs.Enqueue(EventTypeEnum.Added | EventTypeEnum.Removed); for (int spec = 0; spec <= (int)EventTypeEnum.All; spec++) specs.Enqueue((EventTypeEnum)spec); return specs; } } } public abstract class CollectionValueTester : CollectionValueTester where U : ICollectionValue { } public class ExtensibleTester : CollectionValueTester where U : IExtensible { public override SCG.IEnumerable GetSpecs() { return SpecsBasic; } [Test] public virtual void Listenable() { Assert.AreEqual(EventTypeEnum.Basic, collection.ListenableEvents); Assert.AreEqual(EventTypeEnum.None, collection.ActiveEvents); listen(); Assert.AreEqual(listenTo, collection.ActiveEvents); } [Test] public void Add() { listen(); seen.Check(new CollectionEvent[0]); collection.Add(23); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(23, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); } [Test] public void AddAll() { for (int i = 0; i < 10; i++) { collection.Add(10 * i + 5); } listen(); collection.AddAll(new int[] { 45, 200, 56, 67 }); seen.Check(collection.AllowsDuplicates ? collection.DuplicatesByCounting ? new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(45, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(200, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(55, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(65, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)} : new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(45, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(200, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(67, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)} : new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(200, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.AddAll(new int[] { }); seen.Check(new CollectionEvent[] { }); } } public class CollectionTester : ExtensibleTester where U : ICollection { [Test] public void Update() { collection.Add(4); collection.Add(54); collection.Add(56); collection.Add(8); listen(); collection.Update(53); seen.Check( collection.AllowsDuplicates ? collection.DuplicatesByCounting ? new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(54, 2), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(53, 2), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) } : new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(54, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(53, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) } : new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(54, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(53, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.Update(67); seen.Check(new CollectionEvent[] { }); } [Test] public void FindOrAdd() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); int val = 53; collection.FindOrAdd(ref val); seen.Check(new CollectionEvent[] { }); val = 67; collection.FindOrAdd(ref val); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(67, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); } [Test] public void UpdateOrAdd() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); int val = 53; collection.UpdateOrAdd(val); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(53, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); val = 67; collection.UpdateOrAdd(val); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(67, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.UpdateOrAdd(51, out val); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(53, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(51, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); val = 67; collection.UpdateOrAdd(81, out val); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(81, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); } [Test] public void RemoveItem() { collection.Add(4); collection.Add(56); collection.Add(18); listen(); collection.Remove(53); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.Remove(11); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(18, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); } [Test] public void RemoveAll() { for (int i = 0; i < 10; i++) { collection.Add(10 * i + 5); } listen(); collection.RemoveAll(new int[] { 32, 187, 45 }); //TODO: the order depends on internals of the HashSet seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(35, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(45, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.RemoveAll(new int[] { 200, 300 }); seen.Check(new CollectionEvent[] { }); } [Test] public void RetainAll() { for (int i = 0; i < 10; i++) { collection.Add(10 * i + 5); } listen(); collection.RetainAll(new int[] { 32, 187, 45, 62, 75, 82, 95, 2 }); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(15, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(25, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(55, 1), collection), //new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(75, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.RetainAll(new int[] { 32, 187, 45, 62, 75, 82, 95, 2 }); seen.Check(new CollectionEvent[] { }); } [Test] public void RemoveAllCopies() { for (int i = 0; i < 10; i++) { collection.Add(3 * i + 5); } listen(); collection.RemoveAllCopies(14); seen.Check( collection.AllowsDuplicates ? collection.DuplicatesByCounting ? new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(11, 3), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)} : new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(11, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(14, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(17, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)} : new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(11, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.RemoveAllCopies(14); seen.Check(new CollectionEvent[] { }); } [Test] public virtual void Clear() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); collection.Clear(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Cleared, new ClearedEventArgs(true, collection.AllowsDuplicates ? 3 : 2), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.Clear(); seen.Check(new CollectionEvent[] { }); } } public class IndexedTester : CollectionTester where U : IIndexed { [Test] public void RemoveAt() { collection.Add(4); collection.Add(16); collection.Add(28); listen(); collection.RemoveAt(1); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.RemovedAt, new ItemAtEventArgs(16,1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(16, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); } [Test] public void RemoveInterval() { collection.Add(4); collection.Add(56); collection.Add(18); listen(); collection.RemoveInterval(1, 2); seen.Check(new CollectionEvent[] { collection is IList ? new CollectionEvent(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,2,1), collection): new CollectionEvent(EventTypeEnum.Cleared, new ClearedEventArgs(false,2), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.RemoveInterval(1, 0); seen.Check(new CollectionEvent[] { }); } } public class SortedIndexedTester : IndexedTester where U : IIndexedSorted { [Test] public void DeleteMinMax() { collection.Add(34); collection.Add(56); collection.Add(34); collection.Add(12); listen(); collection.DeleteMax(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.DeleteMin(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(12, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); } [Test] public void AddSorted() { listen(); collection.AddSorted(collection.AllowsDuplicates ? new int[] { 31, 62, 63, 93 } : new int[] { 31, 62, 93 }); seen.Check(collection.AllowsDuplicates ? collection.DuplicatesByCounting ? new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(31, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(62, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(62, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(93, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)} : new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(31, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(62, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(63, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(93, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)} : new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(31, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(62, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(93, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.AddSorted(new int[] { }); seen.Check(new CollectionEvent[] { }); } [Test] public void RemoveRange() { for (int i = 0; i < 20; i++) collection.Add(i * 10 + 5); listen(); collection.RemoveRangeFrom(173); //TODO: fix order to remove in: seen.Check( new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(195, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(185, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(175, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.RemoveRangeFromTo(83, 113); seen.Check( new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(105, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(95, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(85, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.RemoveRangeTo(33); seen.Check( new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(5, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(15, 1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(25, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.RemoveRangeFrom(173); seen.Check(new CollectionEvent[] { }); collection.RemoveRangeFromTo(83, 113); seen.Check(new CollectionEvent[] { }); collection.RemoveRangeTo(33); seen.Check(new CollectionEvent[] { }); } } public class ListTester : IndexedTester where U : IList { public override SCG.IEnumerable GetSpecs() { return SpecsAll; } [Test] public override void Listenable() { Assert.AreEqual(EventTypeEnum.All, collection.ListenableEvents); Assert.AreEqual(EventTypeEnum.None, collection.ActiveEvents); listen(); Assert.AreEqual(listenTo, collection.ActiveEvents); } [Test] public void SetThis() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); collection[1] = 45; seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.RemovedAt, new ItemAtEventArgs(56,1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(45, 1), collection), new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(45,1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); } [Test] public void Insert() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); collection.Insert(1, 45); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(45,1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(45, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); } [Test] public void InsertAll() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); collection.InsertAll(1, new int[] { 666, 777, 888 }); //seen.Print(Console.Error); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(666,1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(666, 1), collection), new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(777,2), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(777, 1), collection), new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(888,3), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(888, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.InsertAll(1, new int[] { }); seen.Check(new CollectionEvent[] { }); } [Test] public void InsertFirstLast() { collection.Add(4); collection.Add(56); collection.Add(18); listen(); collection.InsertFirst(45); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(45,0), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(45, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.InsertLast(88); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(88,4), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(88, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); } [Test] public void Remove() { collection.FIFO = false; collection.Add(4); collection.Add(56); collection.Add(18); listen(); collection.Remove(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(18, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.FIFO = true; collection.Remove(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(4, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); } [Test] public void RemoveFirst() { collection.Add(4); collection.Add(56); collection.Add(18); listen(); collection.RemoveFirst(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.RemovedAt, new ItemAtEventArgs(4,0), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(4, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); } [Test] public void RemoveLast() { collection.Add(4); collection.Add(56); collection.Add(18); listen(); collection.RemoveLast(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.RemovedAt, new ItemAtEventArgs(18,2), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(18, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); } [Test] public void Reverse() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); collection.Reverse(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.View(1, 0).Reverse(); seen.Check(new CollectionEvent[] { }); } [Test] public void Sort() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); collection.Sort(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.View(1, 0).Sort(); seen.Check(new CollectionEvent[] { }); } [Test] public void Shuffle() { collection.Add(4); collection.Add(56); collection.Add(8); listen(); collection.Shuffle(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.View(1, 0).Shuffle(); seen.Check(new CollectionEvent[] { }); } [Test] public override void Clear() { collection.Add(4); collection.Add(56); collection.Add(18); listen(); collection.View(1, 1).Clear(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,1,1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.Clear(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,2,0), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.Clear(); seen.Check(new CollectionEvent[] { }); } [Test] public void ListDispose() { collection.Add(4); collection.Add(56); collection.Add(18); listen(); collection.View(1, 1).Dispose(); seen.Check(new CollectionEvent[] { }); collection.Dispose(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,3,0), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); collection.Dispose(); seen.Check(new CollectionEvent[] { }); } /* * / //[TearDown] //public void Dispose() { list = null; seen = null; } /* [Test] [ExpectedException(typeof(UnlistenableEventException))] public void ViewChanged() { IList w = collection.View(0, 0); w.CollectionChanged += new CollectionChangedHandler(w_CollectionChanged); } [Test] [ExpectedException(typeof(UnlistenableEventException))] public void ViewCleared() { IList w = collection.View(0, 0); w.CollectionCleared += new CollectionClearedHandler(w_CollectionCleared); } [Test] [ExpectedException(typeof(UnlistenableEventException))] public void ViewAdded() { IList w = collection.View(0, 0); w.ItemsAdded += new ItemsAddedHandler(w_ItemAdded); } [Test] [ExpectedException(typeof(UnlistenableEventException))] public void ViewInserted() { IList w = collection.View(0, 0); w.ItemInserted += new ItemInsertedHandler(w_ItemInserted); } [Test] [ExpectedException(typeof(UnlistenableEventException))] public void ViewRemoved() { IList w = collection.View(0, 0); w.ItemsRemoved += new ItemsRemovedHandler(w_ItemRemoved); } [Test] [ExpectedException(typeof(UnlistenableEventException))] public void ViewRemovedAt() { IList w = collection.View(0, 0); w.ItemRemovedAt += new ItemRemovedAtHandler(w_ItemRemovedAt); } void w_CollectionChanged(object sender) { throw new NotImplementedException(); } void w_CollectionCleared(object sender, ClearedEventArgs eventArgs) { throw new NotImplementedException(); } void w_ItemAdded(object sender, ItemCountEventArgs eventArgs) { throw new NotImplementedException(); } void w_ItemInserted(object sender, ItemAtEventArgs eventArgs) { throw new NotImplementedException(); } void w_ItemRemoved(object sender, ItemCountEventArgs eventArgs) { throw new NotImplementedException(); } void w_ItemRemovedAt(object sender, ItemAtEventArgs eventArgs) { throw new NotImplementedException(); }*/ } public class StackTester : CollectionValueTester where U : IStack { public override SCG.IEnumerable GetSpecs() { return SpecsBasic; } [Test] public void PushPop() { listen(); seen.Check(new CollectionEvent[0]); collection.Push(23); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(23,0), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(23, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.Push(-12); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(-12,1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(-12, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.Pop(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.RemovedAt, new ItemAtEventArgs(-12,1), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(-12, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.Pop(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.RemovedAt, new ItemAtEventArgs(23,0), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(23, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); } } public class QueueTester : CollectionValueTester where U : IQueue { public override SCG.IEnumerable GetSpecs() { return SpecsBasic; } [Test] public void EnqueueDequeue() { listen(); collection.Enqueue(67); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(67,0), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(67, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.Enqueue(2); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Inserted, new ItemAtEventArgs(2,1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(2, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.Dequeue(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.RemovedAt, new ItemAtEventArgs(67,0), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(67, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.Dequeue(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.RemovedAt, new ItemAtEventArgs(2,0), collection), new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(2, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection)}); } } public class PriorityQueueTester : ExtensibleTester where U : IPriorityQueue { public override System.Collections.Generic.IEnumerable GetSpecs() { return SpecsBasic; } [Test] public void Direct() { listen(); collection.Add(34); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(34, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Add(56); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.AddAll(new int[] { }); seen.Check(new CollectionEvent[] { }); collection.Add(34); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(34, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Add(12); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(12, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.DeleteMax(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.DeleteMin(); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(12, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.AddAll(new int[] { 4, 5, 6, 2 }); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(4, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(5, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(6, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(2, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection) }); } [Test] public void WithHandles() { listen(); IPriorityQueueHandle handle = null, handle2; collection.Add(34); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(34, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Add(56); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Add(ref handle, 34); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(34, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Add(12); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(12, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.DeleteMax(out handle2); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(56, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.DeleteMin(out handle2); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(12, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Replace(handle, 117); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(34, 1), collection), new CollectionEvent(EventTypeEnum.Added, new ItemCountEventArgs(117, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); collection.Delete(handle); seen.Check(new CollectionEvent[] { new CollectionEvent(EventTypeEnum.Removed, new ItemCountEventArgs(117, 1), collection), new CollectionEvent(EventTypeEnum.Changed, new EventArgs(), collection), }); } } public class DictionaryTester : CollectionValueTester> where U : IDictionary { public override SCG.IEnumerable GetSpecs() { return SpecsBasic; } [Test] public virtual void Listenable() { Assert.AreEqual(EventTypeEnum.Basic, collection.ListenableEvents); Assert.AreEqual(EventTypeEnum.None, collection.ActiveEvents); listen(); Assert.AreEqual(listenTo, collection.ActiveEvents); } [Test] public void AddAndREmove() { listen(); seen.Check(new CollectionEvent>[0]); collection.Add(23, 45); seen.Check(new CollectionEvent>[] { new CollectionEvent>(EventTypeEnum.Added, new ItemCountEventArgs>(new KeyValuePair(23,45), 1), collection), new CollectionEvent>(EventTypeEnum.Changed, new EventArgs(), collection)}); collection.Remove(25); seen.Check(new CollectionEvent>[] { new CollectionEvent>(EventTypeEnum.Removed, new ItemCountEventArgs>(new KeyValuePair(23,45), 1), collection), new CollectionEvent>(EventTypeEnum.Changed, new EventArgs(), collection)}); } } }