View Javadoc

1   /*
2    * RandomGenerator_Test.java
3    *
4    * Created on July 24, 2007, 11:21 AM
5    *
6    * $Id: RandomGenerator_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 java.util.ArrayList;
12  import java.util.Iterator;
13  import java.util.List;
14  import junit.framework.TestCase;
15  
16  /**
17   *
18   * @author Norman Graf
19   */
20  public class RandomGenerator_Test extends TestCase
21  {
22      private boolean debug;
23      /** Creates a new instance of RandomGenerator_Test */
24      public void testRandomGenerator()
25      {
26          String component = "RandomGenerator";
27          String ok_prefix = component + " (I): ";
28          String error_prefix = component + " test (E): ";
29          
30          if(debug) System.out.println( ok_prefix
31                  + "---------- Testing component " + component
32                  + ". ----------" );
33          
34          //********************************************************************
35          
36          // Verify that each generated value is different and is in range.
37          if(debug) System.out.println( ok_prefix + "Test default sequence." );
38          double min = 2.0;
39          double max = 5.0;
40          long seed = 246813579L;
41          RandomGeneratorTest rgen = new RandomGeneratorTest(min,max);
42          if(debug) System.out.println( rgen );
43          int ntest = 20;
44          int itest;
45          List values = new ArrayList();
46          Iterator ival;
47          for ( itest=0; itest<ntest; ++itest )
48          {
49              double newval;
50              if ( itest == 4 || itest == 6 || itest == 7 ) newval = rgen.gauss();
51              else newval = rgen.flat();
52              if(debug) System.out.println( newval );
53              for ( ival=values.iterator(); ival.hasNext();)
54              {
55                  Assert.assertTrue( newval != ((Double) ival.next()).doubleValue() );
56              }
57              if ( itest != 4 && itest != 6 && itest != 7 )
58              {
59                  Assert.assertTrue( newval >= min );
60                  Assert.assertTrue( newval <= max );
61              }
62              values.add(new Double(newval));
63          }
64          
65          //********************************************************************
66          
67          // Verify that two generators starting with the same seed give the
68          // same values.
69          if(debug) System.out.println( ok_prefix + "Test sequence with seed." );
70          rgen.setSeed(seed);
71          RandomGeneratorTest rgen2 = new RandomGeneratorTest(min,max,seed);
72          for ( itest=0; itest<ntest; ++itest )
73          {
74              RandomGeneratorTest rgen3 = new RandomGeneratorTest(rgen);
75              
76              double newval;
77              double newval2;
78              double newval3;
79              if ( itest == 4 || itest == 6 || itest == 7 )
80              {
81                  newval = rgen.gauss();
82                  newval2 = rgen2.gauss();
83                  newval3 = rgen3.gauss();
84              }
85              else
86              {
87                  newval = rgen.flat();
88                  newval2 = rgen2.flat();
89                  newval3 = rgen3.flat();
90              }
91              if(debug) System.out.println( newval + " " + newval2 + " " + newval3 );
92              Assert.assertTrue( newval == newval2 );
93          }
94          
95          //********************************************************************
96          
97          if(debug) System.out.println( ok_prefix + "Test copy constructor." );
98          // Note that rgen is not at the beginning of its sequence.
99          // Copying the state should copy the seed and invoke the
100         // appropriate number of calls so the next value will be
101         // the same for both objects.
102         RandomGeneratorTest rgen3 = new RandomGeneratorTest(rgen);
103         for ( itest=0; itest<ntest; ++itest )
104         {
105             double newval;
106             double newval2;
107             if ( itest == 4 || itest == 6 || itest == 7 )
108             {
109                 newval = rgen.gauss();
110                 newval2 = rgen3.gauss();
111             }
112             else
113             {
114                 newval = rgen.flat();
115                 newval2 = rgen3.flat();
116             }
117             if(debug) System.out.println( newval + " " + newval2 );
118             Assert.assertTrue( newval == newval2 );
119         }
120         
121         //********************************************************************
122         
123         if(debug) System.out.println( ok_prefix + "Test copying state." );
124         rgen.setSeed(seed);
125         RandomGeneratorTest rgen4 = new RandomGeneratorTest(min,max);
126         rgen4.setState(rgen);
127         for ( itest=0; itest<ntest; ++itest )
128         {
129             double newval;
130             double newval2;
131             if ( itest == 4 || itest == 6 || itest == 7 )
132             {
133                 newval = rgen.gauss();
134                 newval2 = rgen4.gauss();
135             }
136             else
137             {
138                 newval = rgen.flat();
139                 newval2 = rgen4.flat();
140             }
141             if(debug) System.out.println( newval + ' ' + newval2 );
142             Assert.assertTrue( newval == newval2 );
143         }
144         
145         //********************************************************************
146         
147         if(debug) System.out.println( ok_prefix
148                 + "------------- All tests passed. -------------" );
149         //********************************************************************
150     }
151     
152 }