internal static IEnumerable<Dokument> GetVedlegg(int antall = 5) { var vedleggTxt0 = new Dokument("Vedlegg", ResourceUtility.ReadAllBytes(true, "vedlegg", "Vedlegg.txt"), "text/plain"); var vedleggDocx = new Dokument("Vedleggsgris", ResourceUtility.ReadAllBytes(true, "vedlegg", "VedleggsGris.docx"), "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); var vedleggPdf = new Dokument("Vedleggshjelm", ResourceUtility.ReadAllBytes(true, "vedlegg", "VedleggsHjelm.pdf"), "application/pdf"); var vedleggTxt1 = new Dokument("Vedlegg", ResourceUtility.ReadAllBytes(true, "vedlegg", "Vedlegg.txt"), "text/plain"); var vedleggTxt2 = new Dokument("Vedlegg", ResourceUtility.ReadAllBytes(true, "vedlegg", "Vedlegg.txt"), "text/plain"); var vedlegg = new[] {vedleggTxt0, vedleggDocx, vedleggPdf, vedleggTxt1, vedleggTxt2}; if (antall <= 5) { return vedlegg.Take(antall); } else { var vedleggbatch = new List<Dokument>(); for (var i = 0; i < antall; i++) { var element = vedlegg.ElementAt(i % vedlegg.Length); vedleggbatch.Add(new Dokument(element.Tittel, element.Bytes, element.MimeType, "NO", $"{i}-{element.Filnavn}")); } return vedleggbatch; } }
public void E_SlicingArrays() { var array = new[] { "peanut", "butter", "and", "jelly" }; Assert.AreEqual(new string[] { (string)FILL_ME_IN, (string)FILL_ME_IN }, array.Take(2).ToArray()); Assert.AreEqual(new string[] { (string)FILL_ME_IN, (string)FILL_ME_IN }, array.Skip(1).Take(2).ToArray()); }
public ActionAlternatesFactory(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; _actionAlternates = new Lazy<List<string>>(() => { var httpContext = _httpContextAccessor.Current(); if (httpContext == null) { return null; } var request = _httpContextAccessor.Current().Request; var actionSegments = new[] { request.RequestContext.RouteData.GetRequiredString("area").Replace("-", "__").Replace(".", "_"), request.RequestContext.RouteData.GetRequiredString("controller").Replace("-", "__").Replace(".", "_"), request.RequestContext.RouteData.GetRequiredString("action").Replace("-", "__").Replace(".", "_") }; return Enumerable.Range(1, actionSegments.Count()).Select(range => String.Join("__", actionSegments.Take(range))).ToList(); }); }
public void EndExclusive() { var spans = new[] { new ILSpan(0u, uint.MaxValue), new ILSpan(1, 9), new ILSpan(2, 8), new ILSpan(1, 3), new ILSpan(7, 9), }; Assert.Equal(new ILSpan(0u, uint.MaxValue), MethodContextReuseConstraints.CalculateReuseSpan(5, ILSpan.MaxValue, spans.Take(1))); Assert.Equal(new ILSpan(1, 9), MethodContextReuseConstraints.CalculateReuseSpan(5, ILSpan.MaxValue, spans.Take(2))); Assert.Equal(new ILSpan(2, 8), MethodContextReuseConstraints.CalculateReuseSpan(5, ILSpan.MaxValue, spans.Take(3))); Assert.Equal(new ILSpan(3, 8), MethodContextReuseConstraints.CalculateReuseSpan(5, ILSpan.MaxValue, spans.Take(4))); Assert.Equal(new ILSpan(3, 7), MethodContextReuseConstraints.CalculateReuseSpan(5, ILSpan.MaxValue, spans.Take(5))); }
public void Take_TakesItems() { var queryable = new[] { "1", "3", "6", "2", "8" }.AsQueryable(); var source = new Source<string>(queryable); var expected = queryable.Take(3).ToList(); var actual = source.Take(3).ToList(); CollectionAssert.AreEqual(expected, actual); }
public void ShareWorksForArray() { var result = new List<int>(); var enm = new[] { 1, 2, 3, 4, 5 }.Share(); enm.Take(3).ForEach(i => result.Add(i)); result.Add(-1); enm.ForEach(i => result.Add(i)); Assert.AreEqual(result, new[] { 1, 2, 3, -1, 4, 5 }); }
public void MaxBy() { var data = new[] { "A", "B", "C", "DZ" }; Assert.Throws<NullReferenceException> (() => ((IEnumerable<string>)null).MaxBy(x => x.Length)); Assert.Throws<NullReferenceException> (() => data.MaxBy<string, int>(null)); Assert.Throws<InvalidOperationException>(() => new string[0].MaxBy(x => x.Length)); Assert.Equal("DZ", data.MaxBy(x => x.Length)); Assert.Equal("A", data.Take(3).MaxBy(x => x.Length)); }
public void TestMultiplePartitionsShorterThanSequence() { const int count = 100; var parSizes = new[] { 10, 20, 30, 40 }; var sequence = Enumerable.Range(1, count); var result = sequence.Partition(parSizes); var index = 0; foreach (var resultSequence in result) { Assert.AreEqual(parSizes[index], resultSequence.Count()); Assert.IsTrue(resultSequence.SequenceEqual(sequence.Skip(parSizes.Take(index).Sum()) .Take(parSizes[index]))); ++index; } }
public void Test_Lazy_Pumping() { var log = new List<string>(); var results = new XmlPipeline() .Source(new[] { new DummyXmlSource("-1"), new DummyXmlSource("-2"), new DummyXmlSource("-3") }) .Pipe(new DummyXmlTransform()) .Destination(new DummyXmlDestination(name => log.Add(name))) .Pump(); Assert.That(log.ToArray(), Is.EqualTo(new string[0])); var expected = new[] { "dummy-source-1", "dummy-source-2", "dummy-source-3" }; var i = 1; foreach (var result in results) { Assert.That(log.ToArray(), Is.EqualTo(expected.Take(i++))); } }
private static IEnumerable<KeyValuePair<ExpressionType, Expression>> Invoke() { var p = Expression.Parameter(typeof(int)); var i = Expression.Parameter(typeof(Action)); var d = Expression.Parameter(typeof(Action)); var cs = new[] { i, i, i, d, i, d, d, i }; for (var j = 1; j < cs.Length; j++) { yield return new KeyValuePair<ExpressionType, Expression>(ExpressionType.Invoke, Expression.Block( new[] { p, i, d }, Expression.Assign(i, Expression.Lambda<Action>(Expression.PostIncrementAssign(p))), Expression.Assign(d, Expression.Lambda<Action>(Expression.PostDecrementAssign(p))), Expression.Block(cs.Take(j).Select(e => Expression.Invoke(e))), p ) ); } }
public void NestedDeepTranslationTest() { EnableDebugViewer(); List<Node> nodes = new[] { new Node(CurveFactory.CreateRectangle(30, 20, new Point())), new Node(CurveFactory.CreateRectangle(30, 20, new Point(100, 0))), new Node(CurveFactory.CreateRectangle(30, 20, new Point(200, 0))) } .ToList(); var graph = new GeometryGraph(); nodes.ForEach(graph.Nodes.Add); nodes.Add(CreateCluster(nodes.Take(2), 10)); Assert.AreEqual(nodes[3].BoundingBox.Width, 150, "Inner Cluster has incorrect width"); Assert.AreEqual(nodes[3].BoundingBox.Height, 40, "Inner Cluster has incorrect height"); nodes.Add(CreateCluster(new[] { nodes[3], nodes[2] }, 10)); graph.RootCluster = new Cluster(new Node[] { }, new[] { (Cluster)nodes[4] }); List<Edge> edges = new[] { new Edge(nodes[0], nodes[1]), new Edge(nodes[0], nodes[2]), new Edge(nodes[2], nodes[1]), new Edge(nodes[3], nodes[2]), new Edge(nodes[2], nodes[3]) } .ToList(); edges.ForEach(graph.Edges.Add); RouteEdges(graph, 10); var bounds = (from v in nodes select v.BoundingBox).Concat(from e in edges select e.BoundingBox).ToList(); var delta = new Point(10, 20); ((Cluster)nodes[4]).DeepTranslation(delta, true); ShowGraphInDebugViewer(graph); IEnumerable<GeometryObject> geometryObjects = (from v in nodes select (GeometryObject)v).Concat(from e in edges select (GeometryObject)e); foreach (var b in geometryObjects.Zip(bounds, (g, b) => new { G = g, Original = b, Target = g.BoundingBox })) { Assert.IsTrue(ApproximateComparer.Close(Rectangle.Translate(b.Original, delta), b.Target), "object was not translated: " + b.G); } }
public static void Main(string[] args) { var asm = Assembly.GetExecutingAssembly(); var stream = asm.GetManifestResourceStream("Example.vector.txt"); var text = new StreamReader(stream).ReadToEnd(); var template = Template.Create(text); var comps = new[] { "x", "y", "z", "w" }; var operators = new[] { "+", "-", "*", "/" }; for(int i = 0; i < 4; ++i) { var dim = i+1; var def = new VectorDef() { dim = dim, components = comps.Take(dim).ToArray(), operators = operators, baseType = "float", }; Console.WriteLine(template.Render(def)); } }
public Payload GetSharedMedia(string id, bool includeChildren, int startIndex, int requestCount) { _hierarchy = _hierarchy ?? CreateHierarchy(); var node = _hierarchy.GetNode(id); IEnumerable<AbstractSharedMediaInfo> nodes = new []{node.ToMedia()}; if (includeChildren) { nodes = _hierarchy.GetChildren(node).Select(n => n.ToMedia()) .Skip(startIndex); if (requestCount != 0) { nodes = nodes.Take(requestCount); } } var list = nodes.ToArray(); ApplySortIndexes(list); return new Payload(node.Id, node.ParentId, node.Title, 0, list, node.IsContainer); }
public void TestContainsNonCollection() { Func<bool> f = () => { var a = new[] { 1, 2, 3 }; return a.Skip(1).Contains(2) && !a.Take(1).Contains(2); }; this.Test(f, true); }
public void TestSingleOrDefaultNonCollectionTooMany() { Func<int> f = () => { var a = new[] { 1, 2 }; try { return a.Take(2).SingleOrDefault(); } catch { return -1; } }; this.Test(f, -1); }
public void TestSingleOrDefaultNonCollectionOnly1() { Func<int> f = () => { var a = new[] { 1 }; return a.Take(1).SingleOrDefault(); }; this.Test(f, 1); }
public void IfWeightsHaveWrongNumberOfLayers_Throw(int numLayers) { var weightsBase = new[] { new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}, new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}, new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1,}, new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1,}, }; ObjectStore.Add("weights", weightsBase.Take(numLayers).ToArray()); Action action = () => ExcelFunctions.MakeMultilayerPerceptron("x", 2, 3, new[] {4.0, 2.0}, "weights"); action.ShouldThrow<NNXException>() .WithMessage($"*Argument Weights was expected to have 3 layers; had: {numLayers}.*"); }
public void InitializePlayers(int count) { Settings.Players.Validate(this); var players = new[] { new Player(this, Settings.Players.Player1.Name, (CanonicalString)Settings.Players.Player1.ShipName, (CanonicalString)Settings.Players.Player1.Weapon2Name, (CanonicalString)Settings.Players.Player1.ExtraDeviceName, PlayerControls.FromSettings(Settings.Controls.Player1)), new Player(this, Settings.Players.Player2.Name, (CanonicalString)Settings.Players.Player2.ShipName, (CanonicalString)Settings.Players.Player2.Weapon2Name, (CanonicalString)Settings.Players.Player2.ExtraDeviceName, PlayerControls.FromSettings(Settings.Controls.Player2)) }; DataEngine.Teams.Clear(); DataEngine.Spectators.Clear(); foreach (var plr in players.Take(count)) DataEngine.Spectators.Add(plr); }
public void Should_return_QIFTransactions_containing_the_records() { var records = new[] { new QIFRecord(QIFRecordType.AccountHeader, ""), new QIFRecord(QIFRecordType.Content, "NPersonal Checking"), new QIFRecord(QIFRecordType.Content, "TBank"), new QIFRecord(QIFRecordType.Content, "$0.44"), new QIFRecord(QIFRecordType.TransactionEnd, ""), new QIFRecord(QIFRecordType.Content, "D09/23/2012"), new QIFRecord(QIFRecordType.Content, "N1701"), new QIFRecord(QIFRecordType.Content, "PCHECK"), new QIFRecord(QIFRecordType.Content, "MCHECK"), new QIFRecord(QIFRecordType.Content, "CC"), new QIFRecord(QIFRecordType.Content, "T-40"), new QIFRecord(QIFRecordType.TransactionEnd, ""), }; var transactions = records.CombineIntoTransactions().ToList(); transactions.Count.ShouldBeEqualTo(2); var transaction1 = transactions.First(); transaction1.Records.Count.ShouldBeEqualTo(5); transaction1.Records.ShouldContainAll(records.Take(5)); var transaction2 = transactions.Last(); transaction2.Records.Count.ShouldBeEqualTo(7); transaction2.Records.ShouldContainAll(records.Skip(5)); }
public void SlicingArrays() { var array = new[] { "peanut", "butter", "and", "jelly" }; Assert.Equal(new string[] { "peanut", "butter" }, array.Take(2).ToArray()); Assert.Equal(new string[] { "butter", "and" }, array.Skip(1).Take(2).ToArray()); // array.Take(2).ToArray is making a duplicate array w/o changing the original }
public void SplitByNumberOfGroups__NumberOfGroupsIsLessThenSourceCount_And_SourceCountIsMultipleOfNumberOfGroups__ReturnsGroupsOfSameSize() { var source = new[] { 1, 2, 3, 4, 5, 6 }; var result = source.SplitByNumberOfGroups(source.Length / 3).ToList(); // 6 / 3 = 2 groups Assert.That(result.Count, Is.EqualTo(2)); CollectionAssert.AreEqual(result[0], source.Take(3)); CollectionAssert.AreEqual(result[1], source.Skip(3).Take(3)); CollectionAssert.AreEqual(result.SelectMany(group => group.ToList()), source); }
internal HelpText AddToHelpText(HelpText helpText, Func<string, HelpText> func) { var strArray = new[] { line1, line2, line3, line4, line5 }; return strArray.Take(GetLastLineWithText(strArray)).Aggregate(helpText, (current, line) => func(line)); }
public void SlicingArrays() { var array = new[] { "peanut", "butter", "and", "jelly" }; Assert.Equal(new string[] { (string)"peanut", (string)"butter" }, array.Take(2).ToArray()); Assert.Equal(new string[] { (string)"butter", (string)"and" }, array.Skip(1).Take(2).ToArray()); }
public void SlicingArrays() { var array = new[] { "peanut", "butter", "and", "jelly" }; // Calling an array's Take(x) method will return the specified x number of elements from the start of the array. Assert.AreEqual (new string[] { "peanut", "butter" }, array.Take ((int)2).ToArray (), "George Washington Carver would be proud you've found another use of peanut butter."); // Calling an array's Skip(y) method will bypass the specified y number of elements from the start of the array and return the remaining elements. CollectionAssert.AreEqual (new string[] { "and" }, array.Skip ((int)2).Take (1).ToArray (), "Your array slicing skills need more practice to hone your C# Karma."); }
public void SlicingArrays() { var array = new[] { "peanut", "butter", "and", "jelly" }; //this is array.Take-ing the first(2) from the array Assert.Equal(new string[] { (string)"peanut", (string)"butter" }, array.Take(2).ToArray()); //this is array.Skip-ping the (1) element, then array.Take -ing (2) elements. Assert.Equal(new string[] { (string)"butter", (string)"and" }, array.Skip(1).Take(2).ToArray()); }
public void Split_GroupSizeIsMultipleOfSourceCount_ReturnsGroupsOfSameSize() { var source = new [] { 1, 2, 3, 4, 5, 6 }; var result = source.Split(source.Length / 2).ToList(); Assert.That(result.Count, Is.EqualTo(2)); CollectionAssert.AreEqual(result[0], source.Take(3)); CollectionAssert.AreEqual(result[1], source.Skip(3).Take(3)); CollectionAssert.AreEqual(result.SelectMany(group => group.ToList()), source); }
public void Test() { var ints = new[] { 1, 2, 3, 4, 5 }; var takes = ints.Take(3); Assert.IsNotNull(takes); }
public void Split_GroupSizeIsNotMultipleOfSourceCount_LastGroupHasSmallerSize() { var source = new[] { 1, 2, 3, 4, 5, 6, 7 }; var result = source.Split(source.Length / 2).ToList(); Assert.That(result.Count, Is.EqualTo(3)); CollectionAssert.AreEqual(result[0], source.Take(3)); CollectionAssert.AreEqual(result[1], source.Skip(3).Take(3)); CollectionAssert.AreEqual(result[2], source.Skip(6).Take(3)); CollectionAssert.AreEqual(result.SelectMany(group => group.ToList()), source); }
public void RepeatSequence1() { var i = 0; var xs = new[] { 1, 2 }.Do(_ => i++).Repeat(); var res = xs.Take(10).ToList(); Assert.AreEqual(10, res.Count); Assert.IsTrue(res.Buffer(2).Select(b => b.Sum()).All(x => x == 3)); Assert.AreEqual(10, i); }
public void SplitByNumberOfGroups__NumberOfGroupsIsLessThenSourceCount_And_SourceCountIsNotMultipleOfNumberOfGroups__LastGroupHasSmallerSize() { var source = new[] { 1, 2, 3, 4, 5, 6, 7 }; var result = source.SplitByNumberOfGroups(3).ToList(); // {1,2,3} {4,5,6} {7} Assert.That(result.Count, Is.EqualTo(3)); CollectionAssert.AreEqual(result[0], source.Take(3)); CollectionAssert.AreEqual(result[1], source.Skip(3).Take(3)); CollectionAssert.AreEqual(result[2], source.Skip(6).Take(3)); Assert.That(result[0].Count(), Is.EqualTo(result[1].Count())); Assert.That(result[2].Count(), Is.LessThan(result[0].Count())); CollectionAssert.AreEqual(result.SelectMany(group => group.ToList()), source); }