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