1 package org.lcsim.spacegeom; 2 // Test the spacepoint class. 3 import junit.framework.TestCase; 4 5 import org.lcsim.spacegeom.CartesianTwoPoint; 6 import org.lcsim.spacegeom.CylindricalTwoPoint; 7 import org.lcsim.spacegeom.TwoSpacePoint; 8 9 public class TwoSpacePointTest extends TestCase 10 { 11 boolean debug = false; 12 private static boolean myequal(double x1, double x2) 13 { 14 return (Math.abs(x2-x1) < 1.e-12); 15 } 16 17 //********************************************************************** 18 19 public void testTwoSpacePoint() 20 { 21 22 String name = "TwoSpacePoint"; 23 String ok_prefix = name + " test (I): "; 24 String error_prefix = name + " test (E): "; 25 26 if (debug) System.out.println( ok_prefix 27 + "------- Testing component " + name + ". -------" ); 28 29 double x = 1.23; 30 double y = 2.46; 31 32 //********************************************************************** 33 34 if (debug) System.out.println( ok_prefix + "Testing constructors." ); 35 // Create a space point 36 CartesianTwoPoint cart = new CartesianTwoPoint(x,y); 37 if (debug) System.out.println( cart ); 38 39 // Create in cylindrical coordinates. 40 CylindricalTwoPoint cyl = new CylindricalTwoPoint( cart.rxy(), cart.phi() ); 41 if (debug) System.out.println( cyl ); 42 43 if ( ! myequal(cyl.x(),x) || ! myequal(cyl.y(),y) ) 44 { 45 if (debug) System.out.println( error_prefix + "Mismatch." ); 46 System.exit(1); 47 } 48 49 //********************************************************************** 50 51 if (debug) System.out.println( ok_prefix + "Testing assignment." ); 52 TwoSpacePoint spt = new TwoSpacePoint(); 53 if (debug) System.out.println( spt ); 54 spt = cyl; 55 if (debug) System.out.println( spt ); 56 if ( ! myequal(spt.x(),cyl.x()) || ! myequal(spt.y(),cyl.y()) ) 57 { 58 if (debug) System.out.println( error_prefix + "Mismatch." ); 59 System.exit(1); 60 } 61 62 //********************************************************************** 63 64 if (debug) System.out.println( ok_prefix + "Test cos and sin returns." ); 65 assertTrue( myequal( spt.cosPhi(), Math.cos( spt.phi() ) ) ); 66 assertTrue( myequal( spt.sinPhi(), Math.sin( spt.phi() ) ) ); 67 68 //********************************************************************** 69 70 if (debug) System.out.println( ok_prefix + "Test equality." ); 71 CartesianTwoPoint spt2 = new CartesianTwoPoint(spt.x(),spt.y()); 72 assertTrue( spt.equals(spt2) ); 73 assertTrue( ! (spt.notEquals(spt2)) ); 74 CartesianTwoPoint spt3 = new CartesianTwoPoint(spt.x()+0.1,spt.y()); 75 assertTrue( ! (spt.equals(spt3)) ); 76 assertTrue( spt.notEquals(spt3) ); 77 CartesianTwoPoint spt4 = new CartesianTwoPoint(spt.x(),spt.y()+0.2); 78 assertTrue( spt.notEquals(spt4) ); 79 80 //********************************************************************** 81 82 if (debug) System.out.println( ok_prefix + "Test distance function." ); 83 assertTrue( TwoSpacePoint.distance(cart,cart) == 0.0 ); 84 double dx = 1.1; 85 double dy = 2.2; 86 double dist0 = Math.sqrt(dx*dx+dy*dy); 87 CartesianTwoPoint cart2 = new CartesianTwoPoint(x+dx,y+dy); 88 if (debug) System.out.println( cart ); 89 if (debug) System.out.println( cart2 ); 90 double dist = TwoSpacePoint.distance(cart,cart2); 91 if (debug) System.out.println( "Distance = " + dist0 + " " + dist ); 92 assertTrue( Math.abs( dist - dist0 ) < 1.e-12 ); 93 94 //********************************************************************** 95 96 if (debug) System.out.println( ok_prefix 97 + "------------- All tests passed. ------------" ); 98 99 } 100 }