package com.greef.util;

import junit.framework.TestCase;

/**
 * @author Adrian BER
 */
public class IntervalTestCase extends TestCase {

    private Interval interval;

    protected void setUp() throws Exception {
        interval = new Interval(new Integer(3), new Integer(9));
    }

    public void testContainsValue() {
        assertTrue(interval.contains(new Integer(3)));
        assertTrue(interval.contains(new Integer(6)));
        assertTrue(interval.contains(new Integer(9)));
        assertFalse(interval.contains(new Integer(2)));
        assertFalse(interval.contains(new Integer(15)));
    }

    public void testContainsInterval() {
        assertTrue(interval.contains(new Interval(new Integer(3), new Integer(9))));
        assertTrue(interval.contains(new Interval(new Integer(5), new Integer(7))));
        assertFalse(interval.contains(new Interval(new Integer(2), new Integer(10))));
        assertFalse(interval.contains(new Interval(new Integer(2), new Integer(6))));
        assertFalse(interval.contains(new Interval(new Integer(8), new Integer(11))));
    }

    public void testEquals() {
        assertTrue(interval.equals(new Interval(new Integer(3), new Integer(9))));
        assertFalse(interval.equals(new Interval(new Integer(5), new Integer(7))));
        assertFalse(interval.equals(new Interval(new Integer(3), new Integer(11))));
    }

    public void testIntersect() {
        Interval i;

        i = interval.intersect(new Interval(new Integer(5), new Integer(7)));
        assertEquals(new Integer(5), i.getFrom());
        assertEquals(new Integer(7), i.getTo());

        i = interval.intersect(new Interval(new Integer(1), new Integer(11)));
        assertEquals(new Integer(3), i.getFrom());
        assertEquals(new Integer(9), i.getTo());

        i = interval.intersect(new Interval(new Integer(2), new Integer(6)));
        assertEquals(new Integer(3), i.getFrom());
        assertEquals(new Integer(6), i.getTo());

        i = interval.intersect(new Interval(new Integer(7), new Integer(10)));
        assertEquals(new Integer(7), i.getFrom());
        assertEquals(new Integer(9), i.getTo());

        i = interval.intersect(new Interval(new Integer(15), new Integer(21)));
        assertNull(i);

        i = interval.intersect(new Interval(new Integer(1), new Integer(2)));
        assertNull(i);

        i = interval.intersect(new Interval(null, new Integer(7)));
        assertEquals(new Integer(3), i.getFrom());
        assertEquals(new Integer(7), i.getTo());

        i = interval.intersect(new Interval(new Integer(5), null));
        assertEquals(new Integer(5), i.getFrom());
        assertEquals(new Integer(9), i.getTo());

        i = new Interval(null, new Integer(7));
        i = i.intersect(interval);
        assertEquals(new Integer(3), i.getFrom());
        assertEquals(new Integer(7), i.getTo());

    }
}
