public void SelectorImplicitConversion() { Expression <Func <TestPerson, IComparable> > selector1 = person => person.Name !.Length; var conversion1 = new ActiveOrderingKeySelector <TestPerson>(selector1); Expression <Func <TestPerson, IComparable> > selector2 = person => person.Name !; var conversion2 = new ActiveOrderingKeySelector <TestPerson>(selector2); var people = TestPerson.CreatePeopleCollection(); using var query = people.ActiveOrderBy(conversion1, conversion2); void checkMergedNames(string against) => Assert.AreEqual(against, string.Join(string.Empty, query.Select(person => person.Name))); checkMergedNames("BenErinJohnBryanCliffCraigEmilyJamesSteveGeorgeHunterBridgetCharlesNanette"); }
public void SourceManipulation() { var people = new ObservableDictionary <string, TestPerson>(TestPerson.CreatePeopleCollection().ToDictionary(p => p.Name !)); using var aggregate = people.ActiveMin((key, value) => value.Name !.Length); Assert.IsNull(aggregate.OperationFault); Assert.AreEqual(3, aggregate.Value); people.Add("John2", people["John"]); Assert.AreEqual(3, aggregate.Value); people["John"].Name = "J"; Assert.AreEqual(1, aggregate.Value); people["John"].Name = "John"; Assert.AreEqual(3, aggregate.Value); }
public void SourceManipulation() { var people = TestPerson.CreatePeopleCollection(); using var aggregate = people.ActiveMax(p => p.Name !.Length); Assert.IsNull(aggregate.OperationFault); Assert.AreEqual(7, aggregate.Value); people.Add(people[0]); Assert.AreEqual(7, aggregate.Value); people[0].Name = "Johnathon"; Assert.AreEqual(9, aggregate.Value); people[0].Name = "John"; Assert.AreEqual(7, aggregate.Value); }
public void SourceManipulation() { var people = TestPerson.CreatePeopleCollection(); using (var aggregate = people.ActiveSum(p => p.Name.Length)) { Assert.IsNull(aggregate.OperationFault); Assert.AreEqual(74, aggregate.Value); people.Add(people[0]); Assert.AreEqual(78, aggregate.Value); people[0].Name = "Johnny"; Assert.AreEqual(82, aggregate.Value); } }
public void SourceManipulationUnindexed() { var argumentOutOfRangeThrown = false; var people = TestPerson.CreatePeopleCollection(); try { using var groupsExpr = people.ActiveGroupBy(person => person.Name !.Length, IndexingStrategy.NoneOrInherit); } catch (ArgumentOutOfRangeException) { argumentOutOfRangeThrown = true; } Assert.IsTrue(argumentOutOfRangeThrown); }
public void DifferentSynchronizationContexts() { var invalidThrown = false; var left = TestPerson.CreatePeopleCollection(new AsyncSynchronizationContext()); var right = TestPerson.CreatePeopleCollection(new AsyncSynchronizationContext()); try { using var query = left.ActiveConcat(right); } catch (InvalidOperationException) { invalidThrown = true; } Assert.IsTrue(invalidThrown); }
public void SourceManipulation() { foreach (var indexingStrategy in new IndexingStrategy[] { IndexingStrategy.HashTable, IndexingStrategy.SelfBalancingBinarySearchTree }) { var people = TestPerson.CreatePeopleCollection(); using (var query = people.ToActiveDictionary(p => new KeyValuePair <string, string>(p.Name.Substring(0, 3), p.Name.Substring(3)), indexingStrategy)) { Assert.IsNull(query.OperationFault); Assert.AreEqual(string.Empty, query["Ben"]); people[6].Name = "Benjamin"; Assert.IsNull(query.OperationFault); Assert.AreEqual("jamin", query["Ben"]); people[6].Name = "Ben"; var benny = new TestPerson("!!!TROUBLE"); people.Add(benny); Assert.IsNull(query.OperationFault); benny.Name = "Benny"; Assert.IsNotNull(query.OperationFault); var benjamin = new TestPerson("@@@TROUBLE"); people.Add(benjamin); benjamin.Name = "Benjamin"; Assert.IsNotNull(query.OperationFault); benny.Name = "!!!TROUBLE"; Assert.IsNotNull(query.OperationFault); Assert.AreEqual("TROUBLE", query["!!!"]); benjamin.Name = "@@@TROUBLE"; Assert.IsNull(query.OperationFault); Assert.AreEqual("TROUBLE", query["@@@"]); people.Add(benjamin); Assert.IsNotNull(query.OperationFault); Assert.AreEqual("TROUBLE", query["@@@"]); benjamin.Name = "###TROUBLE"; Assert.IsNotNull(query.OperationFault); Assert.AreEqual("TROUBLE", query["###"]); people.Add(benjamin); Assert.IsNotNull(query.OperationFault); people.Remove(benjamin); Assert.IsNotNull(query.OperationFault); people.Remove(benjamin); Assert.IsNull(query.OperationFault); people.Remove(benjamin); Assert.IsNull(query.OperationFault); people.Remove(benny); Assert.IsNull(query.OperationFault); } } }
public void ElementResultChanges() { var people = TestPerson.CreatePeopleCollection(); var counts = new BlockingCollection <int>(); using (var query = people.ActiveWhere(p => p.Name.Length == 4)) { counts.Add(query.Count); people[0].Name = "Johnny"; counts.Add(query.Count); people[1].Name = "Emilia"; counts.Add(query.Count); people[12].Name = "Jack"; counts.Add(query.Count); } Assert.IsTrue(new int[] { 2, 1, 1, 2 }.SequenceEqual(counts)); }
public void EnumerableSourceManipulationUnindexed() { var people = TestPerson.CreatePeopleCollection(); using var expr = ((IEnumerable)people).ActiveSelect(person => (person as TestPerson) !.Name !.Length, IndexingStrategy.NoneOrInherit); void checkValues(params int[] values) => Assert.IsTrue(values.SequenceEqual(expr)); checkValues(4, 5, 7, 4, 5, 6, 3, 5, 7, 7, 6, 5, 5, 5); people.Add(people.First()); checkValues(4, 5, 7, 4, 5, 6, 3, 5, 7, 7, 6, 5, 5, 5, 4); people[0].Name = "Johnny"; checkValues(6, 5, 7, 4, 5, 6, 3, 5, 7, 7, 6, 5, 5, 5, 6); people.RemoveAt(people.Count - 1); checkValues(6, 5, 7, 4, 5, 6, 3, 5, 7, 7, 6, 5, 5, 5); people.Move(0, 1); checkValues(5, 6, 7, 4, 5, 6, 3, 5, 7, 7, 6, 5, 5, 5); }
public void ElementsRemoved() { var people = TestPerson.CreatePeopleCollection(); var counts = new BlockingCollection <int>(); using (var query = people.ActiveWhere(p => p.Name.Length == 5)) { counts.Add(query.Count); people.RemoveAt(1); counts.Add(query.Count); people.RemoveAt(0); counts.Add(query.Count); people.RemoveRange(9, 2); counts.Add(query.Count); people.RemoveRange(8, 2); counts.Add(query.Count); } Assert.IsTrue(new int[] { 6, 5, 5, 3, 2 }.SequenceEqual(counts)); }
public void WrappedObservableCollectionManipulation() { var people = TestPerson.CreatePeopleCollection(); var wrappedPeople = new TestSimpleObservableCollection <TestPerson>(people.SynchronizationContext, people); using var query = wrappedPeople.ToActiveEnumerable(); Assert.IsTrue(query.SequenceEqual(people)); people.Add(new TestPerson("Jenny")); Assert.IsTrue(query.SequenceEqual(people)); people.InsertRange(3, new TestPerson[] { new TestPerson("Renata"), new TestPerson("Tony") }); Assert.IsTrue(query.SequenceEqual(people)); people[4] = new TestPerson("Anthony"); Assert.IsTrue(query.SequenceEqual(people)); people.RemoveRange(3, 2); Assert.IsTrue(query.SequenceEqual(people)); people.RemoveAt(people.Count - 1); Assert.IsTrue(query.SequenceEqual(people)); people.Clear(); Assert.IsTrue(query.SequenceEqual(people)); }
public void SourceManipulationSorted() { var people = TestPerson.CreatePeopleCollection(); using var groupsExpr = people.ActiveGroupBy(person => person.Name !.Length, IndexingStrategy.SelfBalancingBinarySearchTree); using var orderedGroupMembersExpr = groupsExpr.ActiveSelect(group => Tuple.Create(group.Key, group.ActiveOrderBy(person => person.Name !))); using var orderedGroupsExpr = orderedGroupMembersExpr.ActiveOrderBy(group => group !.Item1); void checkMergedNames(string against) => Assert.AreEqual(against, string.Join(";", orderedGroupsExpr.Select(group => $"{group!.Item1}:{string.Join(",", group.Item2.Select(person => person.Name))}"))); checkMergedNames("3:Ben;4:Erin,John;5:Bryan,Cliff,Craig,Emily,James,Steve;6:George,Hunter;7:Bridget,Charles,Nanette"); people[0].Name = "Adam"; checkMergedNames("3:Ben;4:Adam,Erin;5:Bryan,Cliff,Craig,Emily,James,Steve;6:George,Hunter;7:Bridget,Charles,Nanette"); people[0].Name = "J"; checkMergedNames("1:J;3:Ben;4:Erin;5:Bryan,Cliff,Craig,Emily,James,Steve;6:George,Hunter;7:Bridget,Charles,Nanette"); people[0].Name = "John"; checkMergedNames("3:Ben;4:Erin,John;5:Bryan,Cliff,Craig,Emily,James,Steve;6:George,Hunter;7:Bridget,Charles,Nanette"); people.Add(new TestPerson("Daniel")); checkMergedNames("3:Ben;4:Erin,John;5:Bryan,Cliff,Craig,Emily,James,Steve;6:Daniel,George,Hunter;7:Bridget,Charles,Nanette"); people.RemoveAt(people.Count - 1); checkMergedNames("3:Ben;4:Erin,John;5:Bryan,Cliff,Craig,Emily,James,Steve;6:George,Hunter;7:Bridget,Charles,Nanette"); }
public void ElementsAdded() { var people = TestPerson.CreatePeopleCollection(); var counts = new BlockingCollection <int>(); using (var query = people.ActiveWhere(p => p.Name.Length == 4)) { counts.Add(query.Count); people.Add(new TestPerson("Jack")); counts.Add(query.Count); people.Add(new TestPerson("Chuck")); counts.Add(query.Count); people.AddRange(new TestPerson[] { new TestPerson("Jill"), new TestPerson("Nick") }); counts.Add(query.Count); people.AddRange(new TestPerson[] { new TestPerson("Clint"), new TestPerson("Harry") }); counts.Add(query.Count); people.AddRange(new TestPerson[] { new TestPerson("Dana"), new TestPerson("Ray") }); counts.Add(query.Count); people[7] = new TestPerson("Tony"); counts.Add(query.Count); } Assert.IsTrue(new int[] { 2, 3, 3, 5, 5, 6, 7 }.SequenceEqual(counts)); }