/// <summary>Returns at most the 4 closest addresses in order.</summary> protected List<Connection> GetClosest(List<Connection> cons) { List<Connection> closest = new List<Connection>(cons); // Since MeasuredLatency could change the duration of our sorting we make // a copy as necessary Dictionary<Connection, double> latencies = new Dictionary<Connection, double>(); Comparison<Connection> comparer = delegate(Connection x, Connection y) { double lat_x, lat_y; if(!latencies.TryGetValue(x, out lat_x)) { lat_x = _ncservice.GetMeasuredLatency(x.Address); latencies[x] = lat_x; } if(!latencies.TryGetValue(y, out lat_y)) { lat_y = _ncservice.GetMeasuredLatency(y.Address); latencies[y] = lat_y; } // Remember that smaller is better but -1 is bad... // If either is smaller than 0 invert the comparison.. if(lat_x < 0 || lat_y < 0) { return lat_y.CompareTo(lat_x); } else { return lat_x.CompareTo(lat_y); } }; closest.Sort(comparer); if(closest.Count > 4) { closest.RemoveRange(4, closest.Count - 4); } return closest; }
protected List<Connection> GetOldest(List<Connection> cons) { List<Connection> oldest = new List<Connection>(cons); Comparison<Connection> comparer = delegate(Connection x, Connection y) { return x.CreationTime.CompareTo(y.CreationTime); }; oldest.Sort(comparer); if(oldest.Count > 4) { oldest.RemoveRange(0, oldest.Count - 4); } return oldest; }
private static List<LogLine> Listen(ILogFile logFile) { var data = new List<LogLine>(); var listener = new Mock<ILogFileListener>(); listener.Setup(x => x.OnLogFileModified(It.IsAny<ILogFile>(), It.IsAny<LogFileSection>())) .Callback((ILogFile file, LogFileSection section) => { if (section.IsReset) { data.Clear(); } else if (section.InvalidateSection) { data.RemoveRange((int) section.Index, section.Count); } else { data.AddRange(file.GetSection(section)); } }); logFile.AddListener(listener.Object, TimeSpan.Zero, 1); return data; }
public void NextUnusedInUnchartedThreeParaText() { const int kmaxAnnotations = 20; StTxtPara para1 = MakeParagraph(); StTxtPara para2 = MakeParagraph(); StTxtPara para3 = MakeParagraph(); List<int> wfics1 = MakeNWfics(para1, null, 3); List<int> wfics2 = MakeNWfics(para2, null, 2); List<int> wfics3 = MakeNWfics(para3, null, 4); // Make another annotation on the text that is NOT a wfic. MakeAnnotation(para2, CmBaseAnnotation.CreateFromDBObject(Cache, wfics2[wfics2.Count - 1]), CmAnnotationDefn.TextSegment(Cache)); // And make an indirect annotation on some of them that is NOT a CCA. MakeIndirectAnnotation(wfics2.ToArray(), CmAnnotationDefn.Punctuation(Cache)); List<int> expected = new List<int>(9); expected.AddRange(wfics1); expected.AddRange(wfics2); expected.AddRange(wfics3); int[] result = m_ccl.NextUnchartedInput(kmaxAnnotations); Assert.AreEqual(expected.ToArray(), result); // OK, two things in this test :-) int[] result2 = m_ccl.NextUnchartedInput(7); expected.RemoveRange(7, 2); Assert.AreEqual(expected.ToArray(), result2, "length limit failed"); }
/* F E V A L U A T E */ /*---------------------------------------------------------------------------- %%Function: FEvaluate %%Qualified: AzLog.AzLogFilter.FEvaluate %%Contact: rlittle Evaluate the item against the filter, return true if it matches ----------------------------------------------------------------------------*/ public bool FEvaluate(ILogFilterItem ilf) { // evaluate the list using a stack... List<bool> plfStack = new List<bool>(); for (int i = 0; i < m_plazlfo.Count; i++) { AzLogFilterOperation azlfo = m_plazlfo[i]; switch (azlfo.Op) { case AzLogFilterOperation.OperationType.Value: plfStack.Add(azlfo.Condition.FEvaluate(ilf)); break; case AzLogFilterOperation.OperationType.And: { bool f1 = plfStack[plfStack.Count - 1]; bool f2 = plfStack[plfStack.Count - 2]; plfStack.RemoveRange(plfStack.Count - 2, 2); plfStack.Add(f1 && f2); break; } case AzLogFilterOperation.OperationType.Or: { bool f1 = plfStack[plfStack.Count - 1]; bool f2 = plfStack[plfStack.Count - 2]; plfStack.RemoveRange(plfStack.Count - 2, 2); plfStack.Add(f1 || f2); break; } } } if (plfStack.Count > 1) throw new Exception("expression did not reduce"); return plfStack[0]; }
public void NextUnusedInUnchartedThreeParaText() { var para1 = MakeParagraphSpecificContent("Two segments. Here."); var para2 = MakeParagraphSpecificContent("Two words."); var para3 = MakeParagraphSpecificContent("We want four words."); var seg1 = para1.SegmentsOS[0]; var seg2 = para1.SegmentsOS[1]; var seg3 = para2.SegmentsOS[0]; var seg4 = para3.SegmentsOS[0]; var expected = new List<AnalysisOccurrence>(9) { new AnalysisOccurrence(seg1, 0), // Two new AnalysisOccurrence(seg1, 1), // segments new AnalysisOccurrence(seg2, 0), // Here new AnalysisOccurrence(seg3, 0), // Two new AnalysisOccurrence(seg3, 1), // words new AnalysisOccurrence(seg4, 0), // We new AnalysisOccurrence(seg4, 1), // want new AnalysisOccurrence(seg4, 2), // four new AnalysisOccurrence(seg4, 3) // words }; // SUT var result = m_ccl.NextUnchartedInput(kmaxWords); Assert.AreEqual(expected.ToArray(), result); // OK, two things in this test :-) expected.RemoveRange(7, 2); // SUT2 var result2 = m_ccl.NextUnchartedInput(7); Assert.AreEqual(expected.ToArray(), result2, "length limit failed"); }
public void TestInstanceTracking() { TestViewModel rootViewModel = new TestViewModel (); bridge.ShowView ("TestView.html", rootViewModel); int numRootViewModels = 0; int numChildViewModels = 0; CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (1, numRootViewModels, "Should have been aware of exactly 1 root view model"); Assert.AreEqual (0, numChildViewModels, "Should not have been aware of ANY child view models yet"); Assert.AreEqual (1, webBrowser.GetNumScriptFunctionInvocations ("createInstance")); Assert.AreEqual (0, webBrowser.GetNumScriptFunctionInvocations ("releaseInstance")); rootViewModel.ChildViewModel = new TestChildViewModel (); CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (1, numRootViewModels, "Should have been aware of exactly 1 root view model"); Assert.AreEqual (1, numChildViewModels, "Should have been aware of exactly 1 child view model"); Assert.AreEqual (2, webBrowser.GetNumScriptFunctionInvocations ("createInstance")); Assert.AreEqual (0, webBrowser.GetNumScriptFunctionInvocations ("releaseInstance")); rootViewModel.ChildViewModel = new TestChildViewModel (); CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (1, numRootViewModels, "Should have been aware of exactly 1 root view model"); Assert.AreEqual (1, numChildViewModels, "Should have been aware of exactly 1 child view model"); rootViewModel.ChildViewModel = null; CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (1, numRootViewModels, "Should have been aware of exactly 1 root view model"); Assert.AreEqual (0, numChildViewModels, "Should no longer have been aware of any child view models"); List<TestChildViewModel> childCollection = new List<TestChildViewModel> (); for (int i = 0; i < 100; i++) childCollection.Add (new TestChildViewModel ()); rootViewModel.ChildViewModels = childCollection; rootViewModel.ChildViewModel = new TestChildViewModel (); foreach (ViewModelInstance instance in SpinnakerConfiguration.CurrentConfig.ViewModelManager.CurrentInstances) Assert.AreEqual (1, instance.RefCount, "Initial ref count of child should be 1"); CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (1, numRootViewModels, "Should have been aware of exactly 1 root view model"); Assert.AreEqual (101, numChildViewModels, "Should have been aware of all child view models"); childCollection.RemoveRange (0, 50); rootViewModel.ChildViewModels = childCollection; CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (1, numRootViewModels, "Should have been aware of exactly 1 root view model"); Assert.AreEqual (51, numChildViewModels, "Should have let go of 50 child view models"); foreach (ViewModelInstance instance in SpinnakerConfiguration.CurrentConfig.ViewModelManager.CurrentInstances) Assert.AreEqual (1, instance.RefCount, "Ref count of child should still be 1"); childCollection.Add (rootViewModel.ChildViewModel); rootViewModel.ChildViewModels = childCollection; CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (51, numChildViewModels, "Should have realized a collection member and a root member were the same view model"); rootViewModel.ChildViewModel = null; CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (51, numChildViewModels, "Should have realized a collection member and a root member were the same view model"); childCollection.RemoveAt (childCollection.Count - 1); rootViewModel.ChildViewModels = childCollection; CountInstances (out numRootViewModels, out numChildViewModels); Assert.AreEqual (1, numRootViewModels, "Should have been aware of exactly 1 root view model"); Assert.AreEqual (50, numChildViewModels, "Should have released child via ref counting"); foreach (ViewModelInstance instance in SpinnakerConfiguration.CurrentConfig.ViewModelManager.CurrentInstances) Assert.AreEqual (1, instance.RefCount, "Ref count of child should be 1"); }
public void RemoveRange_negative_index() { IList<int> list = new List<int>(); list.RemoveRange(-1); Assert.AreEqual(0, list.Count); }
public void RemoveRange_outside() { IList<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.RemoveRange(4, 1); Assert.AreEqual(4, list.Count); }
public void RemoveRange_middle() { IList<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.RemoveRange(1, 2); Assert.AreEqual(2, list.Count); Assert.AreEqual(1, list[0]); Assert.AreEqual(4, list[1]); }
public void RemoveRange_last() { IList<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.RemoveRange(3, 1); Assert.AreEqual(3, list.Count); Assert.AreEqual(3, list.Last()); }
public void RemoveRange_first() { IList<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.RemoveRange(0, 1); Assert.AreEqual(3, list.Count); Assert.AreEqual(2, list[0]); }
public void RemoveRange_all_2() { IList<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.RemoveRange(0, 4); Assert.AreEqual(0, list.Count); }
public void RemoveRange_empty_list_1() { IList<int> list = new List<int>(); list.RemoveRange(0, 1); Assert.AreEqual(0, list.Count); }
public void CreateAndGCAnchors() { List<TextAnchor> anchors = new List<TextAnchor>(); List<int> expectedOffsets = new List<int>(); document.Text = new string(' ', 1000); for (int t = 0; t < 250; t++) { int c = rnd.Next(50); if (rnd.Next(2) == 0) { for (int i = 0; i < c; i++) { int offset = rnd.Next(1000); anchors.Add(document.CreateAnchor(offset)); expectedOffsets.Add(offset); } } else if (c <= anchors.Count) { anchors.RemoveRange(0, c); expectedOffsets.RemoveRange(0, c); GC.Collect(); } for (int j = 0; j < anchors.Count; j++) { Assert.AreEqual(expectedOffsets[j], anchors[j].Offset); } } GC.KeepAlive(anchors); }
public void CreateAndMoveAnchors() { List<TextAnchor> anchors = new List<TextAnchor>(); List<int> expectedOffsets = new List<int>(); document.Text = new string(' ', 1000); for (int t = 0; t < 250; t++) { //Console.Write("t = " + t + " "); int c = rnd.Next(50); switch (rnd.Next(5)) { case 0: //Console.WriteLine("Add c=" + c + " anchors"); for (int i = 0; i < c; i++) { int offset = rnd.Next(document.TextLength); TextAnchor anchor = document.CreateAnchor(offset); if (rnd.Next(2) == 0) anchor.MovementType = AnchorMovementType.BeforeInsertion; else anchor.MovementType = AnchorMovementType.AfterInsertion; anchor.SurviveDeletion = rnd.Next(2) == 0; anchors.Add(anchor); expectedOffsets.Add(offset); } break; case 1: if (c <= anchors.Count) { //Console.WriteLine("Remove c=" + c + " anchors"); anchors.RemoveRange(0, c); expectedOffsets.RemoveRange(0, c); GC.Collect(); } break; case 2: int insertOffset = rnd.Next(document.TextLength); int insertLength = rnd.Next(1000); //Console.WriteLine("insertOffset=" + insertOffset + " insertLength="+insertLength); document.Insert(insertOffset, new string(' ', insertLength)); for (int i = 0; i < anchors.Count; i++) { if (anchors[i].MovementType == AnchorMovementType.BeforeInsertion) { if (expectedOffsets[i] > insertOffset) expectedOffsets[i] += insertLength; } else { if (expectedOffsets[i] >= insertOffset) expectedOffsets[i] += insertLength; } } break; case 3: int removalOffset = rnd.Next(document.TextLength); int removalLength = rnd.Next(document.TextLength - removalOffset); //Console.WriteLine("RemovalOffset=" + removalOffset + " RemovalLength="+removalLength); document.Remove(removalOffset, removalLength); for (int i = anchors.Count - 1; i >= 0; i--) { if (expectedOffsets[i] > removalOffset && expectedOffsets[i] < removalOffset + removalLength) { if (anchors[i].SurviveDeletion) { expectedOffsets[i] = removalOffset; } else { Assert.IsTrue(anchors[i].IsDeleted); anchors.RemoveAt(i); expectedOffsets.RemoveAt(i); } } else if (expectedOffsets[i] > removalOffset) { expectedOffsets[i] -= removalLength; } } break; case 4: int replaceOffset = rnd.Next(document.TextLength); int replaceRemovalLength = rnd.Next(document.TextLength - replaceOffset); int replaceInsertLength = rnd.Next(1000); //Console.WriteLine("ReplaceOffset=" + replaceOffset + " RemovalLength="+replaceRemovalLength + " InsertLength=" + replaceInsertLength); document.Replace(replaceOffset, replaceRemovalLength, new string(' ', replaceInsertLength)); for (int i = anchors.Count - 1; i >= 0; i--) { if (expectedOffsets[i] > replaceOffset && expectedOffsets[i] < replaceOffset + replaceRemovalLength) { if (anchors[i].SurviveDeletion) { if (anchors[i].MovementType == AnchorMovementType.AfterInsertion) expectedOffsets[i] = replaceOffset + replaceInsertLength; else expectedOffsets[i] = replaceOffset; } else { Assert.IsTrue(anchors[i].IsDeleted); anchors.RemoveAt(i); expectedOffsets.RemoveAt(i); } } else if (expectedOffsets[i] > replaceOffset) { expectedOffsets[i] += replaceInsertLength - replaceRemovalLength; } else if (expectedOffsets[i] == replaceOffset && replaceRemovalLength == 0 && anchors[i].MovementType == AnchorMovementType.AfterInsertion) { expectedOffsets[i] += replaceInsertLength - replaceRemovalLength; } } break; } Assert.AreEqual(anchors.Count, expectedOffsets.Count); for (int j = 0; j < anchors.Count; j++) { Assert.AreEqual(expectedOffsets[j], anchors[j].Offset); } } GC.KeepAlive(anchors); }
public void checkingOrderOfCollection(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null, string pendingMode = "") { string cassandraDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed); string cassandraKeyDataTypeName = ""; string openBracket = CassandraCollectionType == "list" ? "[" : "{"; string closeBracket = CassandraCollectionType == "list" ? "]" : "}"; string mapSyntax = ""; string randomKeyValue = string.Empty; if (TypeOfKeyForMap != null) { cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap); mapSyntax = cassandraKeyDataTypeName + ","; if (TypeOfKeyForMap == typeof (DateTimeOffset)) randomKeyValue = Randomm.RandomVal(typeof (DateTimeOffset)) .GetType() .GetMethod("ToString", new[] {typeof (string)}) .Invoke(Randomm.RandomVal(typeof (DateTimeOffset)), new object[1] {"yyyy-MM-dd H:mm:sszz00"}) + "' : '"; else randomKeyValue = Randomm.RandomVal(TypeOfDataToBeInputed) + "' : '"; } string tableName = "table" + Guid.NewGuid().ToString("N"); try { Session.WaitForSchemaAgreement( QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}( tweet_id uuid PRIMARY KEY, some_collection {1}<{2}{3}> );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName))); } catch (AlreadyExistsException) { } Guid tweet_id = Guid.NewGuid(); var longQ = new StringBuilder(); longQ.AppendLine("BEGIN BATCH "); int CollectionElementsNo = 100; var orderedAsInputed = new List<Int32>(CollectionElementsNo); string inputSide = "some_collection + {1}"; if (CassandraCollectionType == "list" && pendingMode == "prepending") inputSide = "{1} + some_collection"; for (int i = 0; i < CollectionElementsNo; i++) { int data = i*(i%2); longQ.AppendFormat(@"UPDATE {0} SET some_collection = " + inputSide + " WHERE tweet_id = {2};" , tableName, openBracket + randomKeyValue + data + closeBracket, tweet_id); orderedAsInputed.Add(data); } longQ.AppendLine("APPLY BATCH;"); QueryTools.ExecuteSyncNonQuery(Session, longQ.ToString(), "Inserting..."); if (CassandraCollectionType == "set") { orderedAsInputed.Sort(); orderedAsInputed.RemoveRange(0, orderedAsInputed.LastIndexOf(0)); } else if (CassandraCollectionType == "list" && pendingMode == "prepending") orderedAsInputed.Reverse(); var rs = Session.Execute(string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); { int ind = 0; foreach (Row row in rs.GetRows()) foreach (object value in row[1] as IEnumerable) { Assert.True(orderedAsInputed[ind] == (int) value); ind++; } } QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName), Session.Cluster.Configuration.QueryOptions.GetConsistencyLevel()); QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName)); }