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