1 /* 2 * TRFMath_Test.java 3 * 4 * Created on July 24, 2007, 11:34 AM 5 * 6 * $Id: TRFMath_Test.java,v 1.1.1.1 2010/04/08 20:38:00 jeremy Exp $ 7 */ 8 9 package org.lcsim.recon.tracking.trfutil; 10 11 import junit.framework.TestCase; 12 13 /** 14 * 15 * @author Norman Graf 16 */ 17 public class TRFMath_Test extends TestCase 18 { 19 private boolean debug; 20 /** Creates a new instance of TRFMath_Test */ 21 public void testTRFMath() 22 { 23 String ok_prefix = "TRFMath (I): "; 24 String error_prefix = "TRFMath test (E): "; 25 26 if(debug) System.out.println("-------- Testing component TRFMath. --------" ); 27 28 //******************************************************************** 29 30 if(debug) System.out.println("Test mathematical constants." ); 31 if(debug) System.out.println(" pi: " + Math.PI ); 32 if(debug) System.out.println(" 2*pi: " + TRFMath.TWOPI ); 33 if(debug) System.out.println(" pi/2: " + TRFMath.PI2 ); 34 Assert.assertTrue( TRFMath.TWOPI == 2.0*Math.PI ); 35 Assert.assertTrue( TRFMath.PI2 == 0.5*Math.PI ); 36 37 //******************************************************************** 38 39 if(debug) System.out.println("Test physical constants." ); 40 if(debug) System.out.println("c (cm/sec): " + TRFMath.CLIGHT ); 41 if(debug) System.out.println("pT/(q*B*Rc): " + TRFMath.BFAC ); 42 Assert.assertTrue( TRFMath.CLIGHT > 0.0 ); 43 Assert.assertTrue( TRFMath.BFAC > 0.0 ); 44 Assert.assertTrue( Math.abs( (TRFMath.CLIGHT - 3.e10) / TRFMath.CLIGHT ) < 0.01 ); 45 Assert.assertTrue( Math.abs( (TRFMath.BFAC - 0.003) / TRFMath.BFAC ) < 0.01 ); 46 47 //******************************************************************** 48 49 if(debug) System.out.println("Test fmod1 and fmod2." ); 50 double val[] = { -2.7, -1.7, -0.7, 0.3, 1.3, 2.3, 200.3 }; 51 double val2[] = { -0.7, 0.3, -0.7, 0.3, -0.7, 0.3, 0.3 }; 52 double range1 = 1.0; 53 double range2 = 2.0; 54 double maxdiff = 1.e-10; 55 for ( int i=0; i<7; ++i ) 56 { 57 double x1 = TRFMath.fmod1( val[i], range1 ); 58 double x2 = TRFMath.fmod2( val[i], range2 ); 59 if(debug) System.out.println(x1 + ' ' + x2 ); 60 Assert.assertTrue( Math.abs(x1-0.3) < maxdiff ); 61 Assert.assertTrue( Math.abs(x2-val2[i]) < maxdiff ); 62 double x1m = TRFMath.fmod1( val[i], -range1 ); 63 double x2m = TRFMath.fmod2( val[i], -range2 ); 64 if(debug) System.out.println(x1m + ' ' + x2m ); 65 Assert.assertTrue( Math.abs(x1m-x1) < maxdiff ); 66 Assert.assertTrue( Math.abs(x2m-x2) < maxdiff ); 67 if(debug) System.out.println("----------------" ); 68 } 69 70 //******************************************************************** 71 72 //******************************************************************** 73 74 if(debug) System.out.println("Test asinrat." ); 75 { 76 Assert.assertTrue( TRFMath.asinrat(0.0) == 1.0 ); 77 double[] vals = {1.e-15, 0.00004, 0.003, 1.0, -.137 }; 78 double close = 1.e-12; 79 for ( int i = 0; i<vals.length; ++i) 80 { 81 double x = vals[i]; 82 double asx1 = Math.asin(x); 83 double asx2 = TRFMath.asinrat(x)*x; 84 double dif = Math.abs(asx2-asx1); 85 if(debug) System.out.println(x + " " + asx1 + " " + asx2 + " " + dif ); 86 Assert.assertTrue( dif < close ); 87 } 88 89 //******************************************************************** 90 91 if(debug) System.out.println("------------- All tests passed. -------------" ); 92 } 93 } 94 95 }