public void MPCQuantityTest() { var coll = new ManyPredicateCollection(); var pred = Predicates.ElementTypePredicate <int>(); var quant = Quantifiers.Count(pred, 2); Assert.ThrowsException <InvalidPredicateException>(() => coll.AddQuant(quant)); coll.Add(1); Assert.ThrowsException <InvalidPredicateException>(() => coll.AddQuant(quant)); coll.Add(2); coll.AddQuant(quant); Assert.IsTrue(new int[] { 1, 2 }.SequenceEqual(coll.Get(pred))); coll.Add(1, 2); // Repeated elements, ok Assert.ThrowsException <InvalidElementException>(() => coll.Add(3)); Assert.IsTrue(new int[] { 1, 2 }.SequenceEqual(coll.Get(pred))); }
public void MPCPropertyTest1() { var coll = new ManyPredicateCollection(); var typePred = Predicates.ElementTypePredicate <CustomType1 <int> >(); var typeQuant = Quantifiers.All(typePred); // Two equivalent predicates. The second one is converted to the first one. var propPred1 = new Predicate((object elem) => !(elem is CustomType1 <int>) || ((CustomType1 <int>)elem).Value == 2); var propQuant1 = Quantifiers.All(propPred1); var propPred1Typed = new ForTypePredicate <CustomType1 <int> >((CustomType1 <int> elem) => elem.Value == 2) .ToPredicate(); var propQuant1Typed = Quantifiers.All(propPred1Typed); var propPred2 = new Predicate((object elem) => ((CustomType1 <int>)elem).Prop); var propQuant2 = Quantifiers.All(propPred2); coll.AddQuant(typeQuant); coll.AddQuant(propQuant1); coll.AddQuant(propQuant2); Assert.ThrowsException <InvalidElementException>(() => coll.Add(1)); Assert.ThrowsException <InvalidElementException>(() => coll.Add(new CustomType1 <int>() { Value = 2, Prop = false })); Assert.ThrowsException <InvalidElementException>(() => coll.Add(new CustomType1 <int>() { Value = 1, Prop = true })); coll.Add(new CustomType1 <int>() { Value = 2, Prop = true }); // Typed get and predicate Assert.IsTrue(new CustomType1 <int>[] { new CustomType1 <int>() { Value = 2, Prop = true } } .SequenceEqual(coll.Get(typePred))); }
public void MPCBasicTest() { var coll = new ManyPredicateCollection(); // The element predicate var pred = Predicates.ElementTypePredicate <int>(); // The quantifier var quant = Quantifiers.All(pred); coll.Add(1); coll.AddQuant(quant); coll.AddQuant(quant); // Ignored // Invalid element Assert.ThrowsException <InvalidElementException>(() => coll.Add("a")); Assert.IsTrue(new int[] { 1 }.SequenceEqual(coll.Get(pred))); }
public void MPCPositionTest() { var coll = new ManyPredicateCollection(); var pred = Predicates.ElementTypePredicate <int>(); var quant = coll.BindPositionQuantifier(pred, 2, 3); // No elements Assert.ThrowsException <InvalidPredicateException>(() => coll.AddQuant(quant)); coll.Add("a", "b"); coll.AddToPos(("c", 1)); coll.AddToPos(("d", 2)); // Wrong type element Assert.ThrowsException <InvalidPredicateException>(() => coll.AddQuant(quant)); coll.Remove("d"); coll.AddToPos((1, 2)); // Unfulfilled position Assert.ThrowsException <InvalidPredicateException>(() => coll.AddQuant(quant)); coll.AddToPos((2, 3)); // Satisfied coll.AddQuant(quant); // Replace position coll.AddToPos((4, 3)); Assert.IsTrue(new object[] { 1, 4 }.SequenceEqual(coll.GetByPos(2, 3))); Assert.ThrowsException <InvalidElementException>(() => coll.AddToPos(("e", 3))); // Remove constraint coll.RemoveQuant(quant); // Add again coll.AddToPos(("e", 3)); Assert.IsTrue(new object[] { 1, "e" }.SequenceEqual(coll.GetByPos(2, 3))); }