View Javadoc

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 CartesianPointTest 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      double x;
24      double y;
25      double z;
26      String name;
27      String ok_prefix;
28      String error_prefix;
29      
30      public void setUp() throws Exception {
31          super.setUp();
32          x = 1.23;
33          y = 2.46;
34          z = 3.69;
35       
36          name = "CartesianPoint";
37          ok_prefix = name + " test (I): ";
38          error_prefix = name + " test (E): ";
39          if (debug) System.out.println(" ----- Testing component " + name + ". -------");
40      }
41      
42      public void testCartesianPoint() {
43          if (debug) System.out.println(ok_prefix + "Testing constructors.");
44          // Create a space point
45          CartesianPoint cart = new CartesianPoint(x,y,z);
46          if (debug) System.out.println(cart);
47          
48          // Create in cylindrical coordinates.
49          CylindricalPoint cyl = new CylindricalPoint( cart.rxy(), cart.phi(), cart.z() );
50          if (debug) System.out.println(cyl);
51          
52          // Create in spherical coordinates.
53          SphericalPoint sph = new SphericalPoint( cyl.rxyz(), cyl.phi(), cyl.theta() );
54          if (debug) System.out.println(sph);
55          
56          assertTrue( myequal(sph.x(),x) ||
57                  myequal(sph.y(),y) ||
58                  myequal(sph.z(),z) );
59          if (debug) System.out.println( ok_prefix + "Testing assignment.");
60          SpacePoint spt = new SpacePoint();
61          if (debug) System.out.println(spt);
62          spt = sph;
63          if (debug) System.out.println(spt);
64          assertTrue( myequal(spt.x(),sph.x()) ||
65                  myequal(spt.y(),sph.y()) ||
66                  myequal(spt.z(),sph.z()) );
67          //**********************************************************************
68          
69          if (debug) System.out.println(ok_prefix + "Test cos and sin returns.");
70          assertTrue( myequal( spt.cosPhi(), Math.cos( spt.phi() ) ));
71          assertTrue( myequal( spt.sinPhi(), Math.sin( spt.phi() ) ));
72          assertTrue( myequal( spt.cosTheta(), Math.cos( spt.theta() ) ));
73          assertTrue( myequal( spt.sinTheta(), Math.sin( spt.theta() ) ));
74          
75          //**********************************************************************
76          
77          if (debug) System.out.println(ok_prefix + "Test equality.");
78          CartesianPoint spt2 = new CartesianPoint(spt.x(),spt.y(),spt.z());
79          assertTrue( spt.equals(spt2));
80          assertTrue( !spt.notEquals(spt2) );
81          if (debug) System.out.println(" spt = spt2" );
82          CartesianPoint spt3 = new CartesianPoint(spt.x()+0.1,spt.y(),spt.z());
83          assertTrue( spt.notEquals(spt3) );
84          assertTrue( !spt.equals(spt3) );
85          if (debug) System.out.println(" spt != spt3" );
86          CartesianPoint spt4 = new CartesianPoint(spt.x(),spt.y()+0.2,spt.z());
87          assertTrue( spt.notEquals(spt4));
88          if (debug) System.out.println(" spt != spt4" );
89          CartesianPoint spt5 = new CartesianPoint(spt.x(),spt.y(),spt.z()+0.3);
90          assertTrue( spt.notEquals(spt5));
91          if (debug) System.out.println(" spt != spt5" );
92          //**********************************************************************
93          
94          if (debug) System.out.println(ok_prefix + "Test distance function.");
95          assertTrue( SpacePoint.distance(cart,cart) == 0.0 );
96          double dx = 1.1;
97          double dy = 2.2;
98          double dz = 3.3;
99          double dist0 = Math.sqrt(dx*dx+dy*dy+dz*dz);
100         CartesianPoint cart2 = new CartesianPoint(x+dx,y+dy,z+dz);
101         if (debug) System.out.println(cart2);
102         double dist = SpacePoint.distance(cart,cart2);
103         if (debug) System.out.println("Distance = " + dist0 + " " + dist);
104         assertTrue( Math.abs( dist - dist0 ) < 1.e-12 );
105         
106         //**********************************************************************
107         
108         if (debug) System.out.println( ok_prefix
109                 + "------------- All tests passed. ------------");
110     }
111 }
112 
113