Esempio n. 1
0
        public void TryFindIndexReportsProgressBestCase()
        {
            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.TryFindIndex(3, out long index, reporter));
            Assert.AreEqual(4, index);
            long complexity = 4;

            CollectionAssert.AreEquivalent(new ProgressReport[] {
                new ProgressReport(1, complexity),
                new ProgressReport(complexity, complexity),//Jumps to full progress when found early
            }, gotReports);
        }
Esempio n. 2
0
        public void TryFindIndexCanBeCancelled()
        {
            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.TryFindIndex(-3, out long index, reporter, cancellation.Token));
            Assert.AreEqual(-1, index);//Search was cancelled, index 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);
        }