/// <summary> /// Helper function to create an SortedSet fulfilling the given specific parameters. The function will /// create an SortedSet using the Comparer constructor and then add values /// to it until it is full. It will begin by adding the desired number of matching, /// followed by random (deterministic) elements until the desired count is reached. /// </summary> protected IEnumerable <T> CreateSortedSet(IEnumerable <T> enumerableToMatchTo, int count, int numberOfMatchingElements) { JCG.SortedSet <T> set = new JCG.SortedSet <T>(GetIComparer()); int seed = 528; JCG.List <T> match = null; // Add Matching elements if (enumerableToMatchTo != null) { match = enumerableToMatchTo.ToList(); for (int i = 0; i < numberOfMatchingElements; i++) { set.Add(match[i]); } } // Add elements to reach the desired count while (set.Count < count) { T toAdd = CreateT(seed++); while (set.Contains(toAdd) || (match != null && match.Contains(toAdd, GetIEqualityComparer()))) // Don't want any unexpectedly duplicate values { toAdd = CreateT(seed++); } set.Add(toAdd); } // Validate that the Enumerable fits the guidelines as expected Debug.Assert(set.Count == count); if (match != null) { int actualMatchingCount = 0; foreach (T lookingFor in match) { actualMatchingCount += set.Contains(lookingFor) ? 1 : 0; } Assert.Equal(numberOfMatchingElements, actualMatchingCount); } return(set); }
/// <summary> /// Tests that all documents up to maxDoc which are *not* in the /// expected result set, have an explanation which indicates that /// the document does not match /// </summary> public static void CheckNoMatchExplanations(Query q, string defaultFieldName, IndexSearcher searcher, int[] results) { string d = q.ToString(defaultFieldName); JCG.SortedSet <int?> ignore = new JCG.SortedSet <int?>(); for (int i = 0; i < results.Length; i++) { ignore.Add(Convert.ToInt32(results[i], CultureInfo.InvariantCulture)); } int maxDoc = searcher.IndexReader.MaxDoc; for (int doc = 0; doc < maxDoc; doc++) { if (ignore.Contains(Convert.ToInt32(doc, CultureInfo.InvariantCulture))) { continue; } Explanation exp = searcher.Explain(q, doc); Assert.IsNotNull(exp, "Explanation of [[" + d + "]] for #" + doc + " is null"); Assert.IsFalse(exp.IsMatch, "Explanation of [[" + d + "]] for #" + doc + " doesn't indicate non-match: " + exp.ToString()); } }