/// <summary> Returns the first occurence in specified list that matches the specified element according to the specified comparer. </summary> /// <typeparam name="T"> The type of the elements in the list. </typeparam> /// <typeparam name="U"> The type of the element to match elements in the list. </typeparam> /// <param name="sortedList"> The list to search in for a match. Must be transitive over the specified comparer with specified element. </param> /// <param name="element"> The element to search the match of. </param> /// <param name="comparer"> The comparer determining whether a </param> /// <returns> an occurence in the sorted list matching the specified element, or default(T) if no match was found. </returns> public static T FirstOrDefault <T, U>(this SortedReadOnlyList <T> sortedList, U element, Func <T, U, int> comparer) { //TODO: First suggests the first is yielded, but actually any is yielded... Contract.Requires(sortedList != null); Contract.Requires(comparer != null); //transitivity is checked in FirstOrDefault return(FirstOrDefault(i => sortedList[i], sortedList.Count, element, comparer)); }
internal static void SetEnabledNotations(IEnumerable<NotationConstructor> notationConstructors) { Contract.Requires(notationConstructors != null); Contract.LazilyAssertMinimumCount(ref notationConstructors, 1); if (!notationConstructors.Select(OrderRequirementAttribute.ToComparableToken).IsSorted()) { //whenever a special array of ConstructorNotations is specified, I don't want to have to manually sort it. This just does that for me. //however, the most frequently specified enumerable, AllLinearNotations, should simply be already sorted notationConstructors = notationConstructors.OrderBy(OrderRequirementAttribute.ToComparableToken); } List<INotation<TestDomain>> allNotations = new List<INotation<TestDomain>>(); foreach (NotationConstructor notationConstructor in notationConstructors) { allNotations.AddRange(notationConstructor(allNotations)); } allNotations.Sort(Notation<TestDomain>.ComparerByForm);//perhaps it is important that this happens after adding all elements. Artificial notations that are to be replaced, can they be replaced with normal notations that exist //before that artificial notation was created notation-precedence wise, or added-wise? Contract.Assert(allNotations.Select(notation => notation.Form).AreUnique());//checks that no artificial notation forms are returned that could have been replaced by normal notation forms EnabledNotations = new SortedReadOnlyList<ICompositeNotationForm<TestDomain>>(allNotations.Map(notation => (ICompositeNotationForm<TestDomain>)notation.Form)); }
/// <summary> Calls the code to be tested, namely creating the AST. </summary> /// <param name="testExpressions"> The test expressions to be parsed into an AST. </param> /// <param name="cachingMethod"> The caching method to use in parsing. </param> /// <returns> null if no AST was found for the test case, and the (first) AST otherwise. </returns> internal static ASTBaseNode GetAST(IEnumerable<IExpression<TestDomain>> testExpressions, SortedReadOnlyList<ICompositeNotationForm<TestDomain>> enabledNotations) { var builder = new TestASTBuilder(enabledNotations); var trees = builder.GetAbstractSyntaxTrees(testExpressions.ToReadOnlyList()).ToList(); Contract.Assert(trees.All(tree => tree == null || !IsImpossible(tree)), "An AST succeeded with the Impossible domain"); return trees.FirstOrDefault(ast => ast != null); }
public IList<IExpression<TestDomain>> ToExpressions(out SortedReadOnlyList<ICompositeNotationForm<TestDomain>> notations) { notations = this.notations; return ToExpressions().ToList(); }
private Testcase(SortedReadOnlyList<ICompositeNotationForm<TestDomain>> notations, params object[] expressions) { Contract.Requires(expressions != null); Contract.Requires(expressions.Length >= 1); Contract.RequiresForAll(expressions, expression => expression is Tuple<object, IPosition>); Contract.Requires(notations != null); this.leaves = expressions; this.notations = notations; }