View Javadoc

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