public void TestAtLeastPercent() { var ints = Linq.Range(1, 10).ToShuffledList(0); Assert.True(ints.AtLeastPercent(0.5, x => x >= 5)); Assert.False(ints.AtLeastPercent(0.5, x => x > 5)); }
public void TestDistinct() { var ints = Linq.Range(0, 10).ToList(); Assert.True(ints.Distinct((a, b) => a == b).Count() == 10); var ints2 = new[] { 1, 1, 2, 2, 3, 2, 2 }; Assert.True(ints2.Distinct((a, b) => a == b).Count() == 3); }
public void TestRange() { CollectionAssert.AreEqual(Linq.Range(5), new[] { 0, 1, 2, 3, 4 }); CollectionAssert.AreEqual(Linq.Range(2, 5), new[] { 2, 3, 4 }); CollectionAssert.AreEqual(Linq.Range(5, 2), new[] { 5, 4, 3 }); CollectionAssert.AreEqual(Linq.Range(2, 5, 2), new[] { 2, 4 }); CollectionAssert.AreEqual(Linq.Range(0, 5, 2), new[] { 0, 2, 4 }); CollectionAssert.AreEqual(Linq.Range(5, 0, -2), new[] { 5, 3, 1 }); CollectionAssert.AreEqual(Linq.Range(0), new int[] { }); CollectionAssert.AreEqual(Linq.Range(1, 1), new int[] { }); CollectionAssert.AreEqual(Linq.Range(5, 2, 1), new int[] { }); CollectionAssert.AreEqual(Linq.Range(2, 5, -1), new int[] { }); }
private HtmlTable(IHtmlTableElement e) { var headings = e.QuerySelectorAll("tr") .Where(x => x.Children.All(y => y is IHtmlTableHeaderCellElement)) .Select(x => (IHtmlTableRowElement)x) .ToList(); if (headings.Any()) { var rowChildren = headings.Select(x => (Row: x, Children: RowChildren(x).ToList())).ToList(); var num = rowChildren.First().Children.Count; foreach (var(row, children) in rowChildren.Skip(1)) { if (children.Count != num) { throw new HtmlElementException(row, $"Expected all of the rows to have the same amount of cells ({num}). But this one has {RowChildren(row).Count()}"); } } ColumnTitles = Linq.Range(num) .Select(i => rowChildren.Select(x => x.Children[i])) .Select(x => x.Select(y => y.TextContent.Trim()).Join(" ").Trim()) .ToList(); _rows = e.QuerySelectorAll("tr").Skip(headings.Count).Select(row => (IHtmlTableRowElement)row).ToList(); } else { ColumnTitles = Array.Empty <string>(); _rows = e.QuerySelectorAll("tr").Select(row => (IHtmlTableRowElement)row).ToList(); } _columnTitleToIndex = ColumnTitles.Enumerate() .Distinct((i1, i2) => i1.Index == i2.Index) .ToDictionary(tup => tup.Elem, tup => tup.Index); }
private static IEnumerable <IHtmlTableCellElement> RowChildren(IHtmlTableRowElement re) { return(re.Children <IHtmlTableCellElement>() .Value.SelectMany(child => Linq.Range(child.ColumnSpan), (child, _) => child)); }