import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
import gs.edt.EDTDate;
import java.util.*;
import java.io.*;

public class Tester extends TestCase {
	public Tester(String ttl) {
		super(ttl);
	}
    public void testSunriseSunsetDate() {
		float[] rise_set = Astro.sunRiseSet( 2002, 8, 28, 45, 45 );
		assertEquals( 2.28f, rise_set[0], 0.01f );
		assertEquals( 15.76f, rise_set[1], 0.01f);
		rise_set = Astro.sunRiseSet( 2002, 8, 28, -45, -45 );
		assertEquals( 9.59f, rise_set[0], 0.01f );
		assertEquals( 20.45f, rise_set[1], 0.01f);
		rise_set = Astro.sunRiseSet( 2002, 02, 28, -149.88f, 61.22f);
		assertEquals( 17.04f, rise_set[0], 0.01f); 
		assertEquals( 27.36f, rise_set[1], 0.01f );
    }
	public void testAngle() {
		double a = YesClock.timeToAngle(6);
		assertEquals( 180.0, a, 0.1 );
		a = YesClock.timeToAngle( 12 );
		assertEquals( 90.0, a, 0.1 );
		a = YesClock.timeToAngle( 18 );
		assertTrue( "Should have been 0 or 360, but was "+a, (360.0==a || 0.0 == a) );
		a = YesClock.timeToAngle( 24 );
		assertEquals( 270.0, a, 0.1 );
	}
	
	public void testTimeToTheta() {
		Calendar date = Calendar.getInstance();
		date.set( date.MINUTE, 0 );
		date.set( date.SECOND, 0 );
		
		date.set( date.HOUR_OF_DAY, 12 );
		assertEquals( 0, YesClock.timeToTheta( date ), 0.01 );
		date.set( date.HOUR_OF_DAY, 18 );
		assertEquals( Math.PI/2.0, YesClock.timeToTheta(date), 0.01 );
		date.set( date.HOUR_OF_DAY, 6 );
		assertEquals( -Math.PI/2, YesClock.timeToTheta(date), 0.01 );
		date.set( date.HOUR_OF_DAY, 0 );
		assertEquals( -Math.PI, YesClock.timeToTheta(date), 0.01 );		
		date.set( date.HOUR_OF_DAY, 24 );
		assertEquals( -Math.PI, YesClock.timeToTheta(date), 0.01 );		
	}
	
	public void testPyxParse() {
		try{ //Fucking stupid Java exception handling
		myoPyx p = new myoPyx();
		Reader reader = new InputStreamReader( new FileInputStream("lib/cities.pyx" ) );
		p.parse( reader );
		
		p.addListener( null, new myoPyx.Listener() {
			int ttl=0,curr=0;
			public void startElement( String name, HashMap attributes ) {
				if (name.equals("Continents")) ttl++;
				curr++;
			}
			public void endElement( String name ) {
				curr--;
				if (name.equals("Cities")) {
					assertEquals( 0, curr );
					assertEquals( 8, ttl );
				} else
					assertTrue( "Curr got down below 0!", curr > 0);
			}
			public void text( String t ) {
			}
		});
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	
	public static void main(String args[]) { 
    	junit.textui.TestRunner.run(suite());
	}
	
    public static Test suite() {
		return new TestSuite( Tester.class );
    }
}
