Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
forrestguice committed Nov 26, 2024
1 parent bc542f0 commit f8854f8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.annotation.Nullable;

import com.forrestguice.suntimeswidget.UnlistedTest;
import com.forrestguice.suntimeswidget.calculator.core.SuntimesCalculator;

import net.time4j.Moment;
import net.time4j.PlainDate;
Expand All @@ -29,12 +30,12 @@ public class SunPositionTest
/* Test Fix: add the geodetic angle before comparing elevations; brings twilight elevations into agreement with expected values. */
public static final boolean APPLY_ALTITUDE_ANGLE = false;

public static final double TEST_TOLERANCE = 0.25; // degrees
public static final double TEST_TOLERANCE = 0.01; // degrees
public static final double TEST_LATITUDE = 33.45579;
public static final double TEST_LONGITUDE = -111.9485; // Phoenix, AZ
public static final int TEST_ALTITUDE = 360;
public static final long TEST_DATE = 1639791526; // Dec 17, 2021
public static final TimeZone TEST_TIMEZONE = TimeZone.getTimeZone("US/Arizona");
public static final TimeZone TEST_TIMEZONE = TimeZone.getTimeZone("UTC");
public static final StdSolarCalculator TEST_CALCULATOR = StdSolarCalculator.TIME4J;

@Before
Expand All @@ -45,6 +46,28 @@ public void setup() {
private Calendar date = Calendar.getInstance();
private SolarTime solarTime;

@Test
public void test_SunPosition()
{
Calendar date = Calendar.getInstance();
date.setTimeInMillis(1639791526); // Dec 17, 2021
PlainDate plainDate = calendarToPlainDate(date, TimeZone.getTimeZone("UTC"));

Moment moment0 = TemporalType.JAVA_UTIL_DATE.translate(date.getTime());
net.time4j.calendar.astro.SunPosition position0 = net.time4j.calendar.astro.SunPosition.at(moment0, solarTime);
double elevation0 = position0.getElevation();

double geodeticAngle = TEST_CALCULATOR.getGeodeticAngle(TEST_LATITUDE, TEST_ALTITUDE);
double zenith = 90 + geodeticAngle - elevation0;

Moment moment1 = solarTime.getCalculator().sunrise(plainDate, TEST_LATITUDE, TEST_LONGITUDE, zenith);
net.time4j.calendar.astro.SunPosition position1 = net.time4j.calendar.astro.SunPosition.at(moment1, solarTime);
double elevation1 = position1.getElevation() + geodeticAngle;

assertTrue("expects value near " + elevation0 + " (within " + TEST_TOLERANCE + "); " + elevation1,
Math.abs(elevation0 - elevation1) < TEST_TOLERANCE);
}

@Test
public void test_sunPositionAtSunrise() {
testElevationAtSunrise(date, solarTime, null, 0, TEST_TOLERANCE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.forrestguice.suntimeswidget.calculator;

import com.forrestguice.suntimeswidget.UnlistedTest;

import net.time4j.Moment;
import net.time4j.PlainDate;
import net.time4j.TemporalType;
import net.time4j.calendar.astro.SolarTime;
import net.time4j.calendar.astro.StdSolarCalculator;
import net.time4j.tz.ZonalOffset;

import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import static org.junit.Assert.assertTrue;

@Category(UnlistedTest.class)
public class SunPositionTest1
{
@Test
public void test_SunPosition()
{
// setup
double latitude = 33.45579;
double longitude = -111.9485; // Phoenix, AZ
int altitude = 360;

TimeZone tz = TimeZone.getTimeZone("UTC");
Calendar date0 = Calendar.getInstance(tz);
date0.setTimeInMillis(1639791526); // Dec 17, 2021
PlainDate plainDate = calendarToPlainDate(date0, tz);

SolarTime solarTime = SolarTime.ofLocation(latitude, longitude, altitude, StdSolarCalculator.TIME4J);
SolarTime.Calculator calculator = solarTime.getCalculator();

// find elevation at given date/time
Moment moment0 = TemporalType.JAVA_UTIL_DATE.translate(date0.getTime());
net.time4j.calendar.astro.SunPosition position0 = net.time4j.calendar.astro.SunPosition.at(moment0, solarTime);
double elevation0 = position0.getElevation();

// find date/time of given elevation
double geodeticAngle = StdSolarCalculator.TIME4J.getGeodeticAngle(latitude, altitude);
double zenith = 90 + geodeticAngle - elevation0;
Moment moment1 = calculator.sunset(plainDate, latitude, longitude, zenith);
Calendar date1 = momentToCalendar(moment1, tz);

double toleranceMs = 1 * 60 * 1000; // 1min
long difference = date1.getTimeInMillis() - date0.getTimeInMillis();
assertTrue("expected time near " + date0.getTimeInMillis() + " (within " + (toleranceMs / (1000d * 60d)) + " min); " + date1.getTimeInMillis() + " differs by " + (difference / (1000d * 60d)) + " min",
Math.abs(difference) < toleranceMs);
}

public static Calendar momentToCalendar(Moment moment, TimeZone timezone)
{
Calendar retValue = null;
if (moment != null)
{
retValue = new GregorianCalendar();
retValue.setTimeZone(timezone);
retValue.setTime(TemporalType.JAVA_UTIL_DATE.from(moment));
}
return retValue;
}

public static PlainDate calendarToPlainDate(Calendar input, TimeZone timezone)
{
Moment moment = TemporalType.JAVA_UTIL_DATE.translate(input.getTime());
ZonalOffset zonalOffset = ZonalOffset.ofTotalSeconds(timezone.getOffset(input.getTimeInMillis()) / 1000);
return moment.toZonalTimestamp(zonalOffset).toDate();
}
}

0 comments on commit f8854f8

Please sign in to comment.