package fi.frs.weathermeter;

import android.test.suitebuilder.annotation.SmallTest;

import junit.framework.TestCase;

import java.util.TimeZone;

/**
 * Created by hamalaiv on 02-Oct-16.
 */
public class HelpersTest extends TestCase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    @SmallTest
    public void testTemperatureConversions(){
        double delta = 0.001;

        assertEquals(Helpers.kelvToC(0), -273.15, delta);
        assertEquals(Helpers.kelvToF(0), -459.67, delta);

        assertEquals(Helpers.kelvToC(273.15), 0, delta);
        assertEquals(Helpers.kelvToF(273.15), 32, delta);

        assertEquals(Helpers.kelvToC(300), 26.85, delta);
        assertEquals(Helpers.kelvToF(300), 80.33, delta);

        assertEquals(Helpers.kelvToC(-1), -274.15, delta);
        assertEquals(Helpers.kelvToF(-1), -461.47, delta);
    }

    @SmallTest
    public void testRoundFloor(){
        assertEquals(Helpers.round(17,20),20);
        assertEquals(Helpers.floor(17,20),0);

        assertEquals(Helpers.round(1,1),2);
        assertEquals(Helpers.floor(1,1),1);

        assertEquals(Helpers.round(-21,20),-20);
        assertEquals(Helpers.floor(-21,20),-40);

        assertEquals(Helpers.round(-15,5),-15);
        assertEquals(Helpers.floor(-15,5),-20);
    }

    @SmallTest
    public void testIsNearNoon(){
        TimeZone timeZone = TimeZone.getTimeZone("GMT");

        int t1 = 1476036000; // Sun, 09 Oct 2016 18:00:00 GMT
        int t2 = 1476100555; // Mon, 10 Oct 2016 11:55:55 GMT
        int t3 = 1476100800; // Mon, 10 Oct 2016 12:00:00 GMT
        int t4 = 1476111355; // Mon, 10 Oct 2016 14:55:55 GMT
        int t5 = 1476111600; // Mon, 10 Oct 2016 15:00:00 GMT

        assertEquals(false,Helpers.isNearNoon(t1,timeZone));
        assertEquals(false,Helpers.isNearNoon(t2,timeZone));
        assertEquals(true,Helpers.isNearNoon(t3,timeZone));
        assertEquals(true,Helpers.isNearNoon(t4,timeZone));
        assertEquals(false,Helpers.isNearNoon(t5,timeZone));
    }

    @SmallTest
    public void testIsNearNoonUS(){
        TimeZone usTimeZone = TimeZone.getTimeZone("America/Los_Angeles"); // -7 from GMT

        int t1 = 1476043200; // Sun, 09 Oct 2016 20:00:00 GMT = Sun, 09 Oct 2016 13:00:00 PST
        int t2 = 1476100800; // Mon, 10 Oct 2016 12:00:00 GMT = Mon, 10 Oct 2016 05:00:00 PST

        assertEquals(true,Helpers.isNearNoon(t1,usTimeZone));
        assertEquals(false,Helpers.isNearNoon(t2,usTimeZone));
    }

    @SmallTest
    public void testIsDay(){
        TimeZone timeZone = TimeZone.getTimeZone("GMT");

        int t1 = 1476036000; // Sun, 09 Oct 2016 18:00:00 GMT
        int t2 = 1476057355; // Sun, 09 Oct 2016 23:55:55 GMT
        int t3 = 1476057600; // Mon, 10 Oct 2016 00:00:00 GMT
        int t4 = 1476079199; // Mon, 10 Oct 2016 05:55:55 GMT
        int t5 = 1476079200; // Mon, 10 Oct 2016 06:00:00 GMT
        int t6 = 1476079201; // Mon, 10 Oct 2016 06:00:01 GMT

        assertEquals(true,Helpers.isDay(t1,timeZone));
        assertEquals(true,Helpers.isDay(t2,timeZone));
        assertEquals(false,Helpers.isDay(t3,timeZone));
        assertEquals(false,Helpers.isDay(t4,timeZone));
        assertEquals(true,Helpers.isDay(t5,timeZone));
        assertEquals(true,Helpers.isDay(t6,timeZone));
    }

    @SmallTest
    public void testIsDayUS(){
        TimeZone usTimeZone = TimeZone.getTimeZone("America/Los_Angeles"); // -7 from GMT

        int t1 = 1476043200; // Sun, 09 Oct 2016 20:00:00 GMT = Sun, 09 Oct 2016 13:00:00 PST
        int t2 = 1476100800; // Mon, 10 Oct 2016 12:00:00 GMT = Mon, 10 Oct 2016 05:00:00 PST

        assertEquals(true,Helpers.isDay(t1,usTimeZone));
        assertEquals(false,Helpers.isDay(t2,usTimeZone));
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }
}
