package lc.mecha.range;

import lc.mecha.log.MechaLogger;
import lc.mecha.log.MechaLoggerFactory;
import lc.mecha.math.IntInterval;
import lc.mecha.math.IntIntervals;
import org.junit.Test;

import static org.junit.Assert.*;

public class IntIntervalsTest {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(IntIntervalsTest.class);

    @Test
    public void testRanges() {
        IntIntervals ranges = new IntIntervals();

        ranges.add(30, 40);
        ranges.add(10, 20);

        // TODO: Check that the entries are sorted properly

        logger.info("Ranges: {}", ranges);
        ranges.compact();
        logger.info("Ranges: {}", ranges);

        ranges.add(5, 42);
        logger.info("Ranges: {}", ranges);
        ranges.compact();
        logger.info("Ranges: {}", ranges);

        ranges.remove(15, 20);
        logger.info("Remove 15-20: Ranges: {}", ranges);
    }

    @Test
    public void testFlip() {
        IntIntervals ranges = new IntIntervals();
        ranges.add(10, 20);

        ranges.flip(15, 20);
        logger.info("Ranges: {}", ranges);

        ranges.flip(15, 20);
        logger.info("Ranges: {}", ranges);

        ranges.compact();
        logger.info("Ranges: {}", ranges);
    }

    @Test
    public void rangeAfterSingleBugTest() {
        // This was a bug that emerged on 2020-12-24
        IntIntervals r = new IntIntervals();
        r.add(new IntInterval(100));
        r.add(new IntInterval(100, 250));

        assertEquals(2, r.getIntervals().size());
    }

    @Test
    public void testSingle() {
        IntIntervals ranges = new IntIntervals();
        ranges.add(10, 20);
        assertFalse(ranges.isSingle());

        ranges.clear();
        ranges.add(5, 5);
        assertTrue(ranges.isSingle());
    }

    @Test
    public void testAsString() {
        IntIntervals r = new IntIntervals();
        r.add(new IntInterval(100));
        r.add(new IntInterval(100, 250));

        String str = r.asString();
        logger.info("{}", str);
        assertEquals("100,[100..250]", str);
    }

    @Test
    public void testFromString() {
        IntIntervals r = new IntIntervals("[100..250],1,2,3");
        assertEquals(4, r.getIntervals().size());
    }

}
