Esempio n. 1
0
        public void TestIntervalSet()
        {
            // Test various "suffix" extensions
            var set = new IntervalSet <int>();

            set.Add(new Interval <int>(5, 7));
            Assert.AreEqual("[5, 7]", set.ToString());
            set.Add(new Interval <int>(6, 12));
            Assert.AreEqual("[5, 12]", set.ToString());
            set.Add(new Interval <int>(12, 14));
            Assert.AreEqual("[5, 14]", set.ToString());
            set.Add(new Interval <int>(15, 20));
            Assert.AreEqual("[5, 14] [15, 20]", set.ToString());
            set.Add(new Interval <int>(30, 40));
            Assert.AreEqual("[5, 14] [15, 20] [30, 40]", set.ToString());
            set.Add(new Interval <int>(6, 23));
            Assert.AreEqual("[5, 23] [30, 40]", set.ToString());

            // Test various "prefix" extensions
            set = new IntervalSet <int>();
            set.Add(new Interval <int>(30, 35));
            Assert.AreEqual("[30, 35]", set.ToString());
            set.Add(new Interval <int>(27, 32));
            Assert.AreEqual("[27, 35]", set.ToString());
            set.Add(new Interval <int>(24, 27));
            Assert.AreEqual("[24, 35]", set.ToString());
            set.Add(new Interval <int>(19, 23));
            Assert.AreEqual("[19, 23] [24, 35]", set.ToString());
            set.Add(new Interval <int>(2, 7));
            Assert.AreEqual("[2, 7] [19, 23] [24, 35]", set.ToString());
            set.Add(new Interval <int>(9, 25));
            Assert.AreEqual("[2, 7] [9, 35]", set.ToString());

            // Test extensions in both directions
            set = new IntervalSet <int>();
            set.Add(new Interval <int>(30, 35));
            Assert.AreEqual("[30, 35]", set.ToString());
            set.Add(new Interval <int>(27, 38));
            Assert.AreEqual("[27, 38]", set.ToString());

            // Test merge multiple subintervals
            set = new IntervalSet <int>();
            set.Add(new Interval <int>(30, 35));
            Assert.AreEqual("[30, 35]", set.ToString());
            set.Add(new Interval <int>(10, 15));
            Assert.AreEqual("[10, 15] [30, 35]", set.ToString());
            set.Add(new Interval <int>(20, 25));
            Assert.AreEqual("[10, 15] [20, 25] [30, 35]", set.ToString());
            set.Add(new Interval <int>(13, 32));
            Assert.AreEqual("[10, 35]", set.ToString());
        }
Esempio n. 2
0
        public void TestIntervalSetRandom()
        {
            var random = new Random(0);

            for (int i = 0; i < 1000; i++)
            {
                var dummySet = new IntervalSetDummy();
                var fastSet  = new IntervalSet <int>();

                for (int j = 0; j < 50; j++)
                {
                    int start    = random.Next(0, 9000);
                    int end      = start + random.Next(1, 300);
                    var interval = new Interval <int>(start, end);

                    dummySet.Add(interval);
                    fastSet.Add(interval);

                    int chkStart     = random.Next(0, 9000);
                    var chk          = new Interval <int>(chkStart, chkStart + 1);
                    var dummyOverlap = dummySet.FindOverlappingInterval(chk);
                    var fastOverlap  = fastSet.FindOverlappingInterval(chk);
                    Assert.AreEqual(dummyOverlap, fastOverlap);
                }

                Assert.AreEqual(fastSet.ToString(), dummySet.ToString());
            }
        }