public void MultipleSortingAndFiltering() { var expectedTotal = 20; var rnd = new Random(214748364); var titles1 = Enumerable.Range(1, expectedTotal).Select(x => ((char)('a' + x)).ToString()).ToList(); var dates1 = Enumerable.Range(1, expectedTotal).Select(x => Now + TimeSpan.FromMinutes(x)).ToList(); var idstack1 = new Stack<int>(Enumerable.Range(1, expectedTotal).OrderBy(rnd.Next)); var datestack1 = new Stack<DateTimeOffset>(dates1); var titlestack1 = new Stack<string>(titles1.OrderBy(_ => rnd.Next())); var titles2 = Enumerable.Range(1, expectedTotal).Select(x => ((char)('c' + x)).ToString()).ToList(); var dates2 = Enumerable.Range(1, expectedTotal).Select(x => Now + TimeSpan.FromMinutes(x)).ToList(); var idstack2 = new Stack<int>(Enumerable.Range(1, expectedTotal).OrderBy(rnd.Next)); var datestack2 = new Stack<DateTimeOffset>(new List<DateTimeOffset>() { dates2[2], dates2[0], dates2[1], dates2[3], dates2[5], dates2[9], dates2[15], dates2[6], dates2[7], dates2[8], dates2[13], dates2[10], dates2[16], dates2[11], dates2[12], dates2[14], dates2[17], dates2[18], dates2[19], dates2[4], }); var titlestack2 = new Stack<string>(titles2.OrderBy(_ => rnd.Next())); var list1 = Observable.Defer(() => Enumerable.Range(1, expectedTotal) .OrderBy(rnd.Next) .Select(x => new Thing(idstack1.Pop(), titlestack1.Pop(), datestack1.Pop())) .ToObservable()) .Replay() .RefCount(); var list2 = Observable.Defer(() => Enumerable.Range(1, expectedTotal) .OrderBy(rnd.Next) .Select(x => new Thing(idstack2.Pop(), titlestack2.Pop(), datestack2.Pop())) .ToObservable()) .Replay() .RefCount(); var col = new TrackingCollection<Thing>( list1.Concat(list2), OrderedComparer<Thing>.OrderByDescending(x => x.CreatedAt).Compare, (item, idx, list) => idx < 5 ) { ProcessingDelay = TimeSpan.Zero }; var count = 0; var evt = new ManualResetEvent(false); col.Subscribe(t => { if (++count == expectedTotal * 2) evt.Set(); }, () => { }); evt.WaitOne(); evt.Reset(); // it's initially sorted by date, so id list should not match CollectionAssert.AreNotEqual(list1.Select(x => x.Number).ToEnumerable(), list2.Select(x => x.Number).ToEnumerable()); var sortlist = list1.ToEnumerable().ToArray(); Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderByDescending(x => x.CreatedAt).Compare)); CollectionAssert.AreEqual(sortlist.Take(5), col); col.SetComparer(OrderedComparer<Thing>.OrderBy(x => x.Number).Compare); sortlist = list1.ToEnumerable().ToArray(); Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderBy(x => x.Number).Compare)); CollectionAssert.AreEqual(sortlist.Take(5), col); col.SetComparer(OrderedComparer<Thing>.OrderBy(x => x.CreatedAt).Compare); sortlist = list1.ToEnumerable().ToArray(); Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderBy(x => x.CreatedAt).Compare)); CollectionAssert.AreEqual(sortlist.Take(5), col); col.SetComparer(OrderedComparer<Thing>.OrderByDescending(x => x.Title).Compare); sortlist = list1.ToEnumerable().ToArray(); Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderByDescending(x => x.Title).Compare)); CollectionAssert.AreEqual(sortlist.Take(5), col); col.SetComparer(OrderedComparer<Thing>.OrderBy(x => x.Title).Compare); sortlist = list1.ToEnumerable().ToArray(); Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderBy(x => x.Title).Compare)); CollectionAssert.AreEqual(sortlist.Take(5), col); col.Dispose(); }
public void ChangingComparers() { var source = new Subject<Thing>(); var col = new TrackingCollection<Thing>(source, OrderedComparer<Thing>.OrderBy(x => x.CreatedAt).Compare) { ProcessingDelay = TimeSpan.Zero }; var count = 0; var evt = new ManualResetEvent(false); var list1 = new List<Thing> { GetThing(1, 1, 9), GetThing(2, 2, 8), GetThing(3, 3, 7), GetThing(4, 4, 6), GetThing(5, 5, 5), GetThing(6, 6, 4), GetThing(7, 7, 3), GetThing(8, 8, 2), GetThing(9, 9, 1), }; col.Subscribe(t => { if (++count == list1.Count) evt.Set(); }, () => { }); foreach (var l in list1) Add(source, l); evt.WaitOne(); evt.Reset(); CollectionAssert.AreEqual(col, list1); col.SetComparer(null); CollectionAssert.AreEqual(col, list1.Reverse<Thing>().ToArray()); col.Dispose(); }
public void DisposingThrows() { var col = new TrackingCollection<Thing>(Observable.Empty<Thing>()); col.Dispose(); Assert.Throws<ObjectDisposedException>(() => col.SetFilter(null)); Assert.Throws<ObjectDisposedException>(() => col.SetComparer(null)); Assert.Throws<ObjectDisposedException>(() => col.Subscribe()); Assert.Throws<ObjectDisposedException>(() => col.AddItem(GetThing(1))); Assert.Throws<ObjectDisposedException>(() => col.RemoveItem(GetThing(1))); }
public void ChangingSortUpdatesCollection() { var source = new Subject<Thing>(); var col = new TrackingCollection<Thing>( source, OrderedComparer<Thing>.OrderBy(x => x.UpdatedAt).Compare, (item, position, list) => item.UpdatedAt < Now + TimeSpan.FromMinutes(10)) { ProcessingDelay = TimeSpan.Zero }; var count = 0; var evt = new ManualResetEvent(false); var list1 = new List<Thing> { GetThing(1, 1), GetThing(2, 2), GetThing(3, 3), GetThing(4, 4), GetThing(5, 5), GetThing(6, 6), GetThing(7, 7), GetThing(8, 8), GetThing(9, 9), }; col.Subscribe(t => { if (++count == list1.Count) evt.Set(); }, () => { }); foreach (var l in list1) Add(source, l); evt.WaitOne(); evt.Reset(); CollectionAssert.AreEqual(col, list1); col.SetComparer(OrderedComparer<Thing>.OrderByDescending(x => x.UpdatedAt).Compare); CollectionAssert.AreEqual(col, list1.Reverse<Thing>().ToArray()); col.Dispose(); }