2c8e207a5d6c612a5926485f29e660b407ba9f27
[jalview.git] / test / jalview / util / MapListTest.java
1 package jalview.util;
2
3 import java.util.Arrays;
4
5 import org.junit.Assert;
6 import org.junit.Test;
7
8 public class MapListTest
9 {
10
11   @Test
12   public void testSomething()
13   {
14     MapList ml = new MapList(new int[]
15     { 1, 5, 10, 15, 25, 20 }, new int[]
16     { 51, 1 }, 1, 3);
17     MapList ml1 = new MapList(new int[]
18     { 1, 3, 17, 4 }, new int[]
19     { 51, 1 }, 1, 3);
20     MapList ml2 = new MapList(new int[]
21     { 1, 60 }, new int[]
22     { 1, 20 }, 3, 1);
23     // test internal consistency
24     int to[] = new int[51];
25     testMap(ml, 1, 60);
26     MapList mldna = new MapList(new int[]
27     { 2, 2, 6, 8, 12, 16 }, new int[]
28     { 1, 3 }, 3, 1);
29     int[] frm = mldna.locateInFrom(1, 1);
30     testLocateFrom(mldna, 1, 1, new int[]
31     { 2, 2, 6, 7 });
32     testMap(mldna, 1, 3);
33     /*
34      * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
35      * toofrom=ml.shiftFrom(too[0]);
36      * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
37      * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
38      */
39   }
40
41   private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
42   {
43     int[] frm = mldna.locateInFrom(i, j);
44     Assert.assertEquals("Failed test locate from " + i + " to " + j,
45             Arrays.toString(frm), Arrays.toString(ks));
46   }
47
48   /**
49    * test routine. not incremental.
50    * 
51    * @param ml
52    * @param fromS
53    * @param fromE
54    */
55   private void testMap(MapList ml, int fromS, int fromE)
56   {
57     // todo convert to JUnit style tests
58     for (int from = 1; from <= 25; from++)
59     {
60       int[] too = ml.shiftFrom(from);
61       System.out.print("ShiftFrom(" + from + ")==");
62       if (too == null)
63       {
64         System.out.print("NaN\n");
65       }
66       else
67       {
68         System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
69         System.out.print("\t+--+\t");
70         int[] toofrom = ml.shiftTo(too[0]);
71         if (toofrom != null)
72         {
73           if (toofrom[0] != from)
74           {
75             System.err.println("Mapping not reflexive:" + from + " "
76                     + too[0] + "->" + toofrom[0]);
77           }
78           System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
79                   + " % " + toofrom[1] + " (" + toofrom[2] + ")");
80         }
81         else
82         {
83           System.out.println("ShiftTo(" + too[0] + ")=="
84                   + "NaN! - not Bijective Mapping!");
85         }
86       }
87     }
88     int mmap[][] = ml.makeFromMap();
89     System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
90             + mmap[0][2] + " " + mmap[0][3] + " ");
91     for (int i = 1; i <= mmap[1].length; i++)
92     {
93       if (mmap[1][i - 1] == -1)
94       {
95         System.out.print(i + "=XXX");
96   
97       }
98       else
99       {
100         System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
101       }
102       if (i % 20 == 0)
103       {
104         System.out.print("\n");
105       }
106       else
107       {
108         System.out.print(",");
109       }
110     }
111     // test range function
112     System.out.print("\nTest locateInFrom\n");
113     {
114       int f = mmap[0][2], t = mmap[0][3];
115       while (f <= t)
116       {
117         System.out.println("Range " + f + " to " + t);
118         int rng[] = ml.locateInFrom(f, t);
119         if (rng != null)
120         {
121           for (int i = 0; i < rng.length; i++)
122           {
123             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
124           }
125         }
126         else
127         {
128           System.out.println("No range!");
129         }
130         System.out.print("\nReversed\n");
131         rng = ml.locateInFrom(t, f);
132         if (rng != null)
133         {
134           for (int i = 0; i < rng.length; i++)
135           {
136             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
137           }
138         }
139         else
140         {
141           System.out.println("No range!");
142         }
143         System.out.print("\n");
144         f++;
145         t--;
146       }
147     }
148     System.out.print("\n");
149     mmap = ml.makeToMap();
150     System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
151             + mmap[0][2] + " " + mmap[0][3] + " ");
152     for (int i = 1; i <= mmap[1].length; i++)
153     {
154       if (mmap[1][i - 1] == -1)
155       {
156         System.out.print(i + "=XXX");
157   
158       }
159       else
160       {
161         System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
162       }
163       if (i % 20 == 0)
164       {
165         System.out.print("\n");
166       }
167       else
168       {
169         System.out.print(",");
170       }
171     }
172     System.out.print("\n");
173     // test range function
174     System.out.print("\nTest locateInTo\n");
175     {
176       int f = mmap[0][2], t = mmap[0][3];
177       while (f <= t)
178       {
179         System.out.println("Range " + f + " to " + t);
180         int rng[] = ml.locateInTo(f, t);
181         if (rng != null)
182         {
183           for (int i = 0; i < rng.length; i++)
184           {
185             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
186           }
187         }
188         else
189         {
190           System.out.println("No range!");
191         }
192         System.out.print("\nReversed\n");
193         rng = ml.locateInTo(t, f);
194         if (rng != null)
195         {
196           for (int i = 0; i < rng.length; i++)
197           {
198             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
199           }
200         }
201         else
202         {
203           System.out.println("No range!");
204         }
205         f++;
206         t--;
207         System.out.print("\n");
208       }
209     }
210   
211   }
212
213 }