package com.gmail.walles.johan.batterylogger;

import com.androidplot.xy.XYSeries;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

public class DrainLinesCreatorTest extends TestCase {
    private static final Date BEFORE = new Date(System.currentTimeMillis() - 86400 * 1000);
    private static final Date NOW = new Date();

    public void testMedianLine() {
        try {
            DrainLinesCreator.median(Collections.<Double>emptyList());
            fail("Computing median of one number should fail");
        } catch (IllegalArgumentException e) {
            // Expected exception intentionally ignored
        }

        assertEquals(5.0, DrainLinesCreator.median(Arrays.asList(5.0)));
        assertEquals(5.5, DrainLinesCreator.median(Arrays.asList(5.0, 6.0)));
        assertEquals(6.0, DrainLinesCreator.median(Arrays.asList(5.0, 6.0, 7.0)));
        assertEquals(6.5, DrainLinesCreator.median(Arrays.asList(5.0, 6.0, 7.0, 7.5)));
    }

    public void testWithZeroEvents() {
        DrainLinesCreator testMe = new DrainLinesCreator(Collections.<HistoryEvent>emptyList());
        assertEquals(0, testMe.getDrainLines().size());
    }

    public void testWithOneEvent() {
        DrainLinesCreator testMe = new DrainLinesCreator(Arrays.asList(
                HistoryEvent.createBatteryLevelEvent(NOW, 50)
        ));
        assertEquals(0, testMe.getDrainLines().size());
    }

    public void testDischargeLineWithZeroEvents() {
        DrainLinesCreator testMe = new DrainLinesCreator(Arrays.asList(
                HistoryEvent.createStopChargingEvent(NOW)
        ));
        assertEquals(0, testMe.getDrainLines().size());
    }

    public void testDischargeLineWithOneEvent() {
        DrainLinesCreator testMe = new DrainLinesCreator(Arrays.asList(
                HistoryEvent.createStopChargingEvent(NOW),
                HistoryEvent.createBatteryLevelEvent(BEFORE, 50)
        ));
        assertEquals(0, testMe.getDrainLines().size());
    }

    public void testDischargeLineWithTwoEvents() {
        Date dates[] = SystemState.between(BEFORE, NOW, 3);
        DrainLinesCreator testMe = new DrainLinesCreator(Arrays.asList(
                HistoryEvent.createStopChargingEvent(dates[0]),
                HistoryEvent.createBatteryLevelEvent(dates[1], 50),
                HistoryEvent.createBatteryLevelEvent(dates[2], 40)
        ));
        assertEquals(1, testMe.getDrainLines().size());

        XYSeries drainLine = testMe.getDrainLines().get(0);
        assertEquals(2, drainLine.size());

        Number y = drainLine.getY(0);
        assertTrue(y.doubleValue() > 0.0);
        assertEquals(y, drainLine.getY(1));
        assertEquals(drainLine.getX(0).doubleValue(), History.toDouble(dates[0]));
        assertEquals(drainLine.getX(1).doubleValue(), History.toDouble(dates[2]));
    }

    public void testCharging() {
        DrainLinesCreator testMe = new DrainLinesCreator(Arrays.asList(
                HistoryEvent.createStartChargingEvent(BEFORE),
                HistoryEvent.createBatteryLevelEvent(NOW, 50)
        ));

        assertEquals(1, testMe.getDrainLines().size());

        XYSeries drainLine = testMe.getDrainLines().get(0);
        assertEquals(2, drainLine.size());

        Number y = drainLine.getY(0);
        assertEquals(0.0, y.doubleValue());
        assertEquals(y, drainLine.getY(1));
        assertEquals(drainLine.getX(0).doubleValue(), History.toDouble(BEFORE));
        assertEquals(drainLine.getX(1).doubleValue(), History.toDouble(NOW));
    }
}
@@ -188,16 +188,68 @@
 il.Date;
+%0Aimport java.util.LinkedList;%0Aimport java.util.List;
 %0A%0Apublic
@@ -2719,24 +2719,82 @@
 e() %3E 0.0);%0A
+        assertFalse(Double.isInfinite(y.doubleValue()));%0A%0A
         asse
@@ -2990,32 +2990,935 @@
 es%5B2%5D));%0A    %7D%0A%0A
+    public void testZeroPercentDischarge() %7B%0A        Date dates%5B%5D = SystemState.between(BEFORE, NOW, 6);%0A        Date bootTimestamp = dates%5B0%5D;%0A        SystemState a = new SystemState(dates%5B1%5D, 50, true, bootTimestamp);%0A        SystemState b = new SystemState(dates%5B2%5D, 50, false, bootTimestamp);%0A        SystemState c = new SystemState(dates%5B3%5D, 50, false, bootTimestamp);%0A        SystemState d = new SystemState(dates%5B4%5D, 50, false, bootTimestamp);%0A        SystemState e = new SystemState(dates%5B5%5D, 50, false, bootTimestamp);%0A%0A        List%3CHistoryEvent%3E events = new LinkedList%3CHistoryEvent%3E();%0A        events.addAll(b.getEventsSince(a));%0A        events.addAll(c.getEventsSince(b));%0A        events.addAll(d.getEventsSince(c));%0A        events.addAll(e.getEventsSince(c));%0A%0A        DrainLinesCreator testMe = new DrainLinesCreator(events);%0A        assertEquals(0, testMe.getDrainLines().size());%0A    %7D%0A%0A
     public void 

Add test for drainless discharge