public class RecTest {

static int fails = 0;


public static void main(String[] args) {
	Rectangle t,q;
	double w;

	t = new Rectangle(8.5);
	String s = "Square Test ";
	c(s+ "height", t.getHeight(),8.5);
	c(s+ "width", t.getWidth(),8.5);
	c(s+ "area", t.getArea(),72.25);
	c(s+ "diagonal", t.getDiagonal(),12.020815280171307);

	t = new Rectangle();
	s = "no-arg test ";
	c(s+ "height", t.getHeight(),1);
	c(s+ "width", t.getWidth(),1);
	c(s+ "area", t.getArea(),1);
	double m = Math.sqrt(2.0);
	c(s+ "diagonal", t.getDiagonal(),m);

	t = new Rectangle(3,4);
	s = "basic 3X4 Test ";
	c(s+ "height", t.getHeight(),3);
	c(s+ "width", t.getWidth(),4);
	c(s+ "area", t.getArea(),12);
	c(s+ "diagonal", t.getDiagonal(),5);

	t = new Rectangle(3,4);
	s = "setHeight Test ";
	w = 17.5;
	t.setHeight(w);
	pp("After setting height to 17.5" ,t);
	c(s+ "height", t.getHeight(),w);
	c(s+ "width", t.getWidth(),4);
	c(s+ "area", t.getArea(),70);
	c(s+ "diagonal", t.getDiagonal(),17.95132307101624);

	s = "setWidth Test ";
	w = 22.5;
	t.setWidth(w);
	pp("After setting height to 22.5" ,t);

	c(s+ "height", t.getHeight(),17.5);
	c(s+ "width", t.getWidth(),22.5);
	c(s+ "area", t.getArea(),393.75);
	c(s+ "diagonal", t.getDiagonal(),28.50438562747845);
		
	q = new Rectangle(3,4);
	t = new Rectangle(q);
	s = "Rectangle constructor Test ";
	c(s+ "height", t.getHeight(),3);
	c(s+ "width", t.getWidth(),4);
	c(s+ "area", t.getArea(),12);
	c(s+ "diagonal", t.getDiagonal(),5);
	
	q = new Rectangle(3,4);
	t = q.copy();
	s = "Rectangle .copy Test ";
	c(s+ "height", t.getHeight(),3);
	c(s+ "width", t.getWidth(),4);
	c(s+ "area", t.getArea(),12);
	c(s+ "diagonal", t.getDiagonal(),5);

	t = new Rectangle(3,4);
	pp("b4 rotate",t);
	t.rotate();
	pp("after rotate",t);
	s = "Rotate Test ";
	c(s+ "height", t.getHeight(),4);
	c(s+ "width", t.getWidth(),3);
	c(s+ "area", t.getArea(),12);
	c(s+ "diagonal", t.getDiagonal(),5);
	
	s = "doubleIt Test ";
	t.doubleIt();
	m = Math.sqrt(2.0);
	c(s+ "height", t.getHeight(),4*m);
	c(s+ "width", t.getWidth(),3*m);
	c(s+ "area", t.getArea(),12*2);
	c(s+ "diagonal", t.getDiagonal(),5*m);

	t = new Rectangle(3,4);
	m = 7.0;
	t.scale(m);
	s = "scale Test(7) ";
	c(s+ "height", t.getHeight(),3*m);
	c(s+ "width", t.getWidth(),4*m);
	c(s+ "area", t.getArea(),12*m*m);
	c(s+ "diagonal", t.getDiagonal(),5*m);

	t = new Rectangle(3,4);
	q = new Rectangle(4.01,3.01);
	s = "Equals test, nudged";
	eqt(s,t,q, true);

	q = new Rectangle(3.01,3.99);
	s = "Equals test, rotated and nudged";

	q = new Rectangle(3.01,3.99);
	eqt(s,t,q,true);

	s = "Equals test, should fail";
	q = new Rectangle(3.51,3.99);
	eqt(s,t,q,false);

	p("All done, " + fails + " failures.");

}
public static void eqt(String s, Rectangle a, Rectangle b, boolean expected) {
	if (a.equals(b) == expected) {
		p("OK: " + s);
	} else {
		f("Fail on: " + s);
	}
}
public static void c(String s, double x, double y) {
	if (almostEquals(x,y)) {
		p("OK: " + s);
	} else {
		f("Fail on: " + s);
	}
}
public static boolean almostEquals(double x, double y) {
	return Math.abs(x-y) < 0.000001*Math.abs(x);
}
public static void p(String g) {
	System.out.println(g);
}
public static void pp(String s, Rectangle r) {
	String a = s;
	a += " height: " + r.getHeight();
	a += " Width: " + r.getWidth();
	a += " area: " + r.getArea();
	a += " diagonal: " + r.getDiagonal();
	p(a);

}
public static void f(String g){
	fails++;
	p(g);

}
}
