public void TryFindValueReportsProgressBestCase() { KeyValuePair <long, string>[] sample = new KeyValuePair <long, string>[] { new KeyValuePair <long, string>(-3, "0"), new KeyValuePair <long, string>(-1, "C"), new KeyValuePair <long, string>(0, "B"), new KeyValuePair <long, string>(1, "A"), new KeyValuePair <long, string>(3, "Value"), new KeyValuePair <long, string>(4, "A"), new KeyValuePair <long, string>(400, "E"), new KeyValuePair <long, string>(401, "F"), new KeyValuePair <long, string>(405, "G"), }; MockBinarySearchable searchable = new MockBinarySearchable(sample); List <ProgressReport> gotReports = new List <ProgressReport>(); ProgressReporter reporter = new ProgressReporter((x) => { gotReports.Add(x); }); Assert.IsTrue(searchable.TryFindValue(3, out var value, reporter)); Assert.AreEqual(sample[4].Value, value); long complexity = 4; CollectionAssert.AreEquivalent(new ProgressReport[] { new ProgressReport(1, complexity), new ProgressReport(complexity, complexity),//Jumps to full progress when found early }, gotReports); }
public void TryFindValueCanBeCancelled() { KeyValuePair <long, string>[] sample = new KeyValuePair <long, string>[] { new KeyValuePair <long, string>(-3, "0"), new KeyValuePair <long, string>(-1, "C"), new KeyValuePair <long, string>(0, "B"), new KeyValuePair <long, string>(1, "A"), new KeyValuePair <long, string>(3, "Value"), new KeyValuePair <long, string>(4, "A"), new KeyValuePair <long, string>(400, "E"), new KeyValuePair <long, string>(401, "F"), new KeyValuePair <long, string>(405, "G"), }; MockBinarySearchable searchable = new MockBinarySearchable(sample); CancellationTokenSource cancellation = new CancellationTokenSource(); List <ProgressReport> gotReports = new List <ProgressReport>(); ProgressReporter reporter = new ProgressReporter((x) => { gotReports.Add(x); if (gotReports.Count == 2) { cancellation.Cancel(); } if (gotReports.Count > 2) { Assert.Fail("Expected all progress to stop after the cancellation request."); } }); Assert.IsFalse(searchable.TryFindValue(-3, out var value, reporter, cancellation.Token)); Assert.IsNull(value);//Search was cancelled, value not found long complexity = 4; CollectionAssert.AreEquivalent(new ProgressReport[] { new ProgressReport(1, complexity), new ProgressReport(2, complexity), //Expect cancellation here, so no more progress reports }, gotReports); }