package com.greef.util;

import junit.framework.TestCase;

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

    private Intervals i;

    protected void setUp() {
        i = new Intervals();
        i.add(new Interval(new Integer(5), new Integer(9)));
        i.add(new Interval(new Integer(13), new Integer(17)));
        i.add(new Interval(new Integer(21), new Integer(25)));
    }

    public void testContainsValue() {
        assertTrue(i.contains(new Integer(5)));
        assertTrue(i.contains(new Integer(7)));
        assertTrue(i.contains(new Integer(25)));
        assertFalse(i.contains(new Integer(11)));
    }

    public void testContainsInterval() {
        assertTrue(i.contains(new Interval(new Integer(5), new Integer(9))));
        assertTrue(i.contains(new Interval(new Integer(14), new Integer(16))));
        assertFalse(i.contains(new Interval(new Integer(18), new Integer(21))));
    }

    public void testEquals() {
        Intervals aux = new Intervals();
        aux.add(new Interval(new Integer(5), new Integer(9)));
        aux.add(new Interval(new Integer(13), new Integer(17)));
        aux.add(new Interval(new Integer(21), new Integer(25)));
        assertTrue(i.equals(aux));
        aux.add(new Interval(new Integer(1), new Integer(3)));
        assertFalse(i.equals(aux));
    }

    public void testAdd1() {
        assertEquals(3, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
    }

    public void testAdd2() {
        i.add(new Interval(new Integer(1), new Integer(27)));
        assertEquals(1, i.getIntervals().size());
        assertEquals(new Interval(new Integer(1), new Integer(27)), i.getIntervals().get(0));
    }

    public void testAdd3() {
        i.add(new Interval(new Integer(1), new Integer(3)));
        assertEquals(4, i.getIntervals().size());
        assertEquals(new Interval(new Integer(1), new Integer(3)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(5), new Integer(9)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(2));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(3));
    }

    public void testAdd4() {
        i.add(new Interval(new Integer(29), new Integer(31)));
        assertEquals(4, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
        assertEquals(new Interval(new Integer(29), new Integer(31)), i.getIntervals().get(3));
    }

    public void testAdd5() {
        i.add(new Interval(new Integer(2), new Integer(7)));
        assertEquals(3, i.getIntervals().size());
        assertEquals(new Interval(new Integer(2), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
    }

    public void testAdd6() {
        i.add(new Interval(new Integer(7), new Integer(14)));
        assertEquals(2, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(17)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(1));
    }

    public void testAdd7() {
        i.add(new Interval(new Integer(8), new Integer(23)));
        assertEquals(1, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(25)), i.getIntervals().get(0));
    }

    public void testAdd8() {
        i.add(new Interval(new Integer(22), new Integer(24)));
        assertEquals(3, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
    }

    public void testAdd9() {
        i.add(new Interval(new Integer(13), new Integer(17)));
        assertEquals(3, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
    }

    public void testRemove() {
        Intervals aux = new Intervals();
        aux.remove(new Interval(new Integer(2), new Integer(31)));
        assertEquals(0, aux.getIntervals().size());
    }

    public void testRemove1() {
        i.remove(new Interval(new Integer(1), new Integer(27)));
        assertEquals(0, i.getIntervals().size());
    }

    public void testRemove2() {
        i.remove(new Interval(new Integer(1), new Integer(3)));
        assertEquals(3, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
    }

    public void testRemove3() {
        i.remove(new Interval(new Integer(29), new Integer(31)));
        assertEquals(3, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
    }

    public void testRemove4() {
        i.remove(new Interval(new Integer(1), new Integer(7)));
        assertEquals(3, i.getIntervals().size());
        assertEquals(new Interval(new Integer(7), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
    }

    public void testRemove5() {
        i.remove(new Interval(new Integer(7), new Integer(14)));
        assertEquals(3, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(7)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(14), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(2));
    }

    public void testRemove6() {
        i.remove(new Interval(new Integer(8), new Integer(23)));
        assertEquals(2, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(8)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(23), new Integer(25)), i.getIntervals().get(1));
    }

    public void testRemove7() {
        i.remove(new Interval(new Integer(22), new Integer(24)));
        assertEquals(4, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(9)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(21), new Integer(22)), i.getIntervals().get(2));
        assertEquals(new Interval(new Integer(24), new Integer(25)), i.getIntervals().get(3));
    }

    public void testRemove8() {
        i.remove(new Interval(new Integer(5), new Integer(9)));
        assertEquals(2, i.getIntervals().size());
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(1));
    }

    public void testRemove9() {
        i.remove(new Interval(new Integer(6), new Integer(7)));
        assertEquals(4, i.getIntervals().size());
        assertEquals(new Interval(new Integer(5), new Integer(6)), i.getIntervals().get(0));
        assertEquals(new Interval(new Integer(7), new Integer(9)), i.getIntervals().get(1));
        assertEquals(new Interval(new Integer(13), new Integer(17)), i.getIntervals().get(2));
        assertEquals(new Interval(new Integer(21), new Integer(25)), i.getIntervals().get(3));
    }

    public void testIntersect() {
        Intervals x = i.intersect(new Interval(new Integer(7), new Integer(15)));
        assertEquals(2, x.getIntervals().size());
        assertEquals(new Integer(7), ((Interval)x.getIntervals().get(0)).getFrom());
        assertEquals(new Integer(9), ((Interval)x.getIntervals().get(0)).getTo());
        assertEquals(new Integer(13), ((Interval)x.getIntervals().get(1)).getFrom());
        assertEquals(new Integer(15), ((Interval)x.getIntervals().get(1)).getTo());

        x = i.intersect(new Interval(new Integer(14), new Integer(16)));
        assertEquals(1, x.getIntervals().size());
        assertEquals(new Integer(14), ((Interval)x.getIntervals().get(0)).getFrom());
        assertEquals(new Integer(16), ((Interval)x.getIntervals().get(0)).getTo());

        x = i.intersect(new Interval(new Integer(10), new Integer(12)));
        assertEquals(0, x.getIntervals().size());
    }


    public void testClear() {
        assertFalse(i.isEmpty());
        i.clear();
        assertTrue(i.isEmpty());
    }
}
