Exemple #1
0
        public void TryFindFloorReportsProgress_FixedSample2()
        {
            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>(5, "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.TryFindFloor(4, out long index, out long key, reporter));
            Assert.AreEqual(4, index);
            Assert.AreEqual(sample[4].Key, key);
            long complexity = 4;

            CollectionAssert.AreEquivalent(new ProgressReport[] {
                new ProgressReport(1, complexity),
                new ProgressReport(2, complexity),
                new ProgressReport(3, complexity),
                new ProgressReport(4, complexity),
            }, gotReports);
        }
Exemple #2
0
        public void TryFindFloorCanBeCancelled()
        {
            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.TryFindFloor(2, out var index, out var key, reporter, cancellation.Token));
            Assert.AreEqual(-1, index);          //Search was cancelled, index not found
            Assert.AreEqual(default(long), key); //Search was cancelled, key 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);
        }