public void CompareTo()
        {
            IInterval <int> interval = new Interval <int>(5, 15);

            Assert.IsTrue(interval.CompareTo(20) > 0);
            Assert.IsTrue(interval.CompareTo(10) == 0);
            Assert.IsTrue(interval.CompareTo(0) < 0);
        }
        public void CompareTo()
        {
            IInterval<int> interval = new Interval<int>(5, 15);

            Assert.IsTrue(interval.CompareTo(20) > 0);
            Assert.IsTrue(interval.CompareTo(10) == 0);
            Assert.IsTrue(interval.CompareTo(0) < 0);
        }
Пример #3
0
        public void IntervalShouldBeLessThanOtherIntervalWhenMinLessThanSecondMin()
        {
            var first  = new Interval('a', 'z');
            var second = new Interval('l', 'r');

            Assert.IsTrue(first.CompareTo(second) < 0);
        }
Пример #4
0
        public void IntervalShouldBeGreaterThanOtherIntervalWhenMinEqualAndFirstMaxGreaterThanSecondMax()
        {
            var first  = new Interval('m', 'x');
            var second = new Interval('m', 'p');

            Assert.IsTrue(first.CompareTo(second) > 0);
        }
Пример #5
0
        public void IntervalShouldEqualOtherIntervalWhenMinAndMaxEqual()
        {
            var first  = new Interval('m', 'x');
            var second = new Interval('m', 'x');

            Assert.IsTrue(first.CompareTo(second) == 0);
        }
Пример #6
0
        public void IntervalShouldBeGreaterThanOtherIntervalWhenMinGreaterThanSecondMin()
        {
            var first  = new Interval('k', 'z');
            var second = new Interval('f', 'h');

            Assert.IsTrue(first.CompareTo(second) > 0);
        }
Пример #7
0
        /// <summary>
        /// Recursively descends to the correct spot for interval insertion in the tree
        /// When a free spot is found for the node, it is attached and tree state is validated
        /// </summary>
        /// <param name="interval">interval to be added</param>
        /// <param name="currentNode">subtree accessed in recursion</param>
        private void InsertInterval(Interval <T> interval, IntervalNode <T> currentNode)
        {
            IntervalNode <T> addedNode = Sentinel;

            if (interval.CompareTo(currentNode.Interval) < 0)
            {
                if (currentNode.Left == Sentinel)
                {
                    addedNode        = new IntervalNode <T>(interval);
                    addedNode.Color  = NodeColor.RED;
                    currentNode.Left = addedNode;
                    addedNode.Parent = currentNode;
                }
                else
                {
                    this.InsertInterval(interval, currentNode.Left);
                    return;
                }
            }
            else if (interval.CompareTo(currentNode.Interval) > 0)
            {
                if (currentNode.Right == Sentinel)
                {
                    addedNode         = new IntervalNode <T>(interval);
                    addedNode.Color   = NodeColor.RED;
                    currentNode.Right = addedNode;
                    addedNode.Parent  = currentNode;
                }
                else
                {
                    this.InsertInterval(interval, currentNode.Right);
                    return;
                }
            }
            else
            {
                // NOTE: Does not allow for duplicates.
                return;
            }

            addedNode.Parent.RecalculateMaxEnd();

            this.RenewConstraintsAfterInsert(addedNode);

            this.Root.Color = NodeColor.BLACK;
        }
Пример #8
0
        [TestCase(0, 10, 1, 10, -1)]  // a comes before b
        public void Compare_TestCases_ProducesCorrectResults(int interval1Start, int interval1End, int interval2Start, int interval2End, int expected)
        {
            var a = new Interval <int>(interval1Start, interval1End);
            var b = new Interval <int>(interval2Start, interval2End);

            int result = a.CompareTo(b);

            Assert.That(result, Is.EqualTo(expected));
        }
Пример #9
0
        public void IntervalJoinShouldReturnOneIntervalWhenTwoIntervalsAreTheSame()
        {
            var first  = new Interval('1', '3');
            var second = new Interval('1', '3');

            var joins = Interval.Join(first, second);

            Assert.AreEqual(1, joins.Count);
            Assert.AreEqual(0, first.CompareTo(joins[0]));
        }
Пример #10
0
 public int CompareTo(SubtitleSegment other)
 {
     if (ReferenceEquals(this, other))
     {
         return(0);
     }
     if (ReferenceEquals(null, other))
     {
         return(1);
     }
     return(Interval.CompareTo(other.Interval));
 }
Пример #11
0
        protected void TimerThread()
        {
            Heap <FuzzyEvent>   events            = new Heap <FuzzyEvent>();
            List <FuzzyEvent>   next_todos        = new List <FuzzyEvent>();
            Interval <DateTime> next_schedule_int = null;
            int  wait_interval = -1;
            bool run           = true;
            bool timedout;

            while (run)
            {
                FuzzyEvent fe = _incoming_events.Dequeue(wait_interval, out timedout);
                if (!timedout)
                {
                    //We got a new event
                    if (fe != null)
                    {
                        events.Add(fe);
                    }
                    else
                    {
                        //Got a null event, that means stop:
                        break;
                    }
                }
                DateTime now = DateTime.UtcNow;

                /*
                 * Since we've already been awakened, let's check to see if we can run:
                 */
                if (next_schedule_int != null)
                {
                    if (next_schedule_int.CompareTo(now) <= 0)
                    {
                        //We are safe to go ahead and run:
                        Interlocked.Exchange(ref _last_run, now.Ticks);
                        foreach (FuzzyEvent feitem in next_todos)
                        {
                            try {
                                feitem.TryRun(now);
                            }
                            catch (Exception x) {
                                Console.WriteLine(x);
                                //Something bad happened
                            }
                        }
                        //Now reset and reschedule below:
                        next_todos.Clear();
                        next_schedule_int = null;
                    }
                    else
                    {
                        //It's not yet time to run.
                    }
                }
                //Time to schedule the next wait:
                Interval <DateTime> overlap;
                do
                {
                    if (events.Count > 0)
                    {
                        overlap = events.Peek();
                        if (next_schedule_int != null)
                        {
                            //We already have something scheduled:
                            var new_overlap = next_schedule_int.Intersection(overlap);
                            if (new_overlap == null)
                            {
                                if (overlap.CompareTo(next_schedule_int) <= 0)
                                {
                                    /*
                                     * If there is no overlap, but next_schedule_int is after,
                                     * overlap, we need to reorder things:
                                     */
                                    //Put the next_todos back:
                                    var new_next = events.Pop();
                                    foreach (FuzzyEvent fev in next_todos)
                                    {
                                        events.Add(fev);
                                    }
                                    next_todos.Clear();
                                    next_todos.Add(new_next);
                                    next_schedule_int = new_next;
                                    overlap           = new_next;
                                }
                                else
                                {
                                    //we'll deal with overlap later:
                                    overlap = null;
                                }
                            }
                            else
                            {
                                //There is an overlap
                                //We can combine the old and new event:
                                overlap           = new_overlap;
                                next_schedule_int = overlap;
                                next_todos.Add(events.Pop());
                            }
                        }
                        else
                        {
                            //There was nothing scheduled:
                            next_schedule_int = overlap;
                            next_todos.Add(events.Pop());
                        }
                    }
                    else
                    {
                        overlap = null;
                    }
                } while(overlap != null);

                if (next_schedule_int != null)
                {
                    //Wait as long as possible, we may be able to combine later:
                    TimeSpan to_wait = next_schedule_int.End - now;
                    wait_interval = (int)to_wait.TotalMilliseconds;
                    if (wait_interval < 0)
                    {
                        //Well, we should be able to go ahead and run, so do it:
                        wait_interval = 0;
                    }
                }
                else
                {
                    //Nothing to do, just wait for the next scheduled operation
                    wait_interval = -1;
                }
            }
        }
Пример #12
0
        public void TestCompareTo()
        {
            // Same
            //L       ○--------○
            //R       ○--------○
            Assert.AreEqual(0, openInterval.CompareTo(openInterval));
            //L       ○--------•
            //R       ○--------•
            Assert.AreEqual(0, leftOpenInterval.CompareTo(leftOpenInterval));
            //L       •--------○
            //R       •--------○
            Assert.AreEqual(0, rightOpenInterval.CompareTo(rightOpenInterval));
            //L       •--------•
            //R       •--------•
            Assert.AreEqual(0, closedInterval.CompareTo(closedInterval));

            // Below
            //L ○--○
            //R       ○--------○
            Assert.AreEqual(-1, openBelowInterval.CompareTo(openInterval));
            //L ○--○
            //R       •--------•
            Assert.AreEqual(-1, openBelowInterval.CompareTo(closedInterval));
            //L •--•
            //R       ○--------○
            Assert.AreEqual(-1, closedBelowInterval.CompareTo(openInterval));
            //L •--•
            //R       •--------•
            Assert.AreEqual(-1, closedBelowInterval.CompareTo(closedInterval));

            // Above
            //L                  ○--○
            //R       ○--------○
            Assert.AreEqual(1, openAboveInterval.CompareTo(openInterval));
            //L                  ○--○
            //R       •--------•
            Assert.AreEqual(1, openAboveInterval.CompareTo(closedInterval));
            //L                  •--•
            //R       ○--------○
            Assert.AreEqual(1, closedAboveInterval.CompareTo(openInterval));
            //L                  •--•
            //R       •--------•
            Assert.AreEqual(1, closedAboveInterval.CompareTo(closedInterval));

            // Different endpoint bound types
            //L       ○--------○
            //R       ○--------•
            Assert.AreEqual(-1, openInterval.CompareTo(leftOpenInterval));
            //L       ○--------○
            //R       •--------○
            Assert.AreEqual(1, openInterval.CompareTo(rightOpenInterval));
            //L       ○--------○
            //R       •--------•
            Assert.AreEqual(1, openInterval.CompareTo(closedInterval));
            //L       •--------•
            //R       ○--------•
            Assert.AreEqual(-1, closedInterval.CompareTo(leftOpenInterval));
            //L       •--------•
            //R       •--------○
            Assert.AreEqual(1, closedInterval.CompareTo(rightOpenInterval));
            //L       •--------•
            //R       ○--------○
            Assert.AreEqual(-1, closedInterval.CompareTo(openInterval));
        }
Пример #13
0
 public void CompareToReturnsExpectedResult(Interval<double> sut, Interval<double> other, int expected)
 {
     Assert.Equal(expected, sut.CompareTo(other));
     Assert.Equal(expected, sut.CompareTo((object)other));
     Assert.Equal(expected < 0, sut < other);
     Assert.Equal(expected <= 0, sut <= other);
     Assert.Equal(expected > 0, sut > other);
     Assert.Equal(expected >= 0, sut >= other);
 }