View Javadoc

1   package org.lcsim.geometry.util;
2   
3   import java.util.Random;
4   
5   import junit.framework.Test;
6   import junit.framework.TestCase;
7   import junit.framework.TestSuite;
8   
9   import org.lcsim.geometry.util.IDDescriptor.IDException;
10  
11  /**
12   *
13   * @author tonyj
14   */
15  public class IDEncoderTest extends TestCase
16  {
17     
18     public IDEncoderTest(String testName)
19     {
20        super(testName);
21     }
22     
23     public static Test suite()
24     {
25        return new TestSuite(IDEncoderTest.class);
26     }
27     public void testEncoder() throws IDException
28     {
29        int[] ids = { 1, 2, 3, 400, 500 };
30        long[] lds = { 1, 2, 3, 400, 500 };
31        long id = lds[0] | lds[1]<<7 | lds[2]<<10 | lds[3]<<32 | lds[4]<<43;
32  
33        String description = "layer:7,system:3,barrel:3,theta:32:11,phi:11";
34        IDDescriptor desc = new IDDescriptor(description);
35        IDEncoder ring = new IDEncoder(desc);
36  
37        long rc = ring.setValues(ids);
38        assertEquals(rc,id);
39  
40        ring = new IDEncoder(desc);
41        for (int i=0; i<ids.length; i++)
42        {
43           ring.setValues(ids);
44        }
45        assertEquals(id,ring.getID());
46     }
47     public void testSignedEncoder() throws IDException
48     {
49        int[] ids = { 1, 2, 3, -400, 500 };
50        long[] lds = { 1, 2, 3, -400, 500 };
51        long id = lds[0] | lds[1]<<7 | lds[2]<<10 | (lds[3] & ((1<<11) - 1))<<32 | lds[4]<<43;
52  
53        String description = "layer:7,system:-3,barrel:3,theta:32:-11,phi:11";
54        IDDescriptor desc = new IDDescriptor(description);
55        IDEncoder ring = new IDEncoder(desc);
56  
57        long rc = ring.setValues(ids);
58        assertEquals(rc,id);
59  
60        ring = new IDEncoder(desc);
61        for (int i=0; i<ids.length; i++)
62        {
63           ring.setValues(ids);
64        }
65        assertEquals(id,ring.getID());
66     }
67     public void testKiller() throws IDException
68     {
69        StringBuffer desc = new StringBuffer();
70        Random r = new Random();
71        for (int i=0; i<100; i++)
72        {
73           desc.setLength(0);
74           for (int j=0,pos=0;;j++)
75           {
76              int l = r.nextInt(31) + 1;
77              if (pos + l > 64) l = 64-pos;
78              pos += l;
79              desc.append('v').append(j).append(':').append(l);
80              if (pos == 64) break;
81              desc.append(',');
82           }
83           IDDescriptor id = new IDDescriptor(desc.toString());
84           IDEncoder e = new IDEncoder(id);
85           IDDecoder d = new IDDecoder(id);
86           int[] buffer = new int[id.fieldCount()];
87           for (int k=0; k<100; k++)
88           {
89              long data = r.nextLong();
90              d.setID(data);
91              long result = e.setValues(d.getValues(buffer));
92              assertEquals(data,result);
93           }
94        }
95     }
96  }