]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/core/src/com/ibm/icu/dev/test/util/VersionInfoTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / core / src / com / ibm / icu / dev / test / util / VersionInfoTest.java
1 /*
2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7
8
9 package com.ibm.icu.dev.test.util;
10
11
12 import com.ibm.icu.dev.test.TestFmwk;
13 import com.ibm.icu.util.VersionInfo;
14
15
16 /**
17 * Testing class for VersionInfo
18 * @author Syn Wee Quek
19 * @since release 2.1 March 01 2002
20 */
21 public final class VersionInfoTest extends TestFmwk 
22
23     // constructor ---------------------------------------------------
24   
25     /**
26     * Constructor
27     */
28     public VersionInfoTest()
29     {
30     }
31
32     // public methods -----------------------------------------------
33     
34     public static void main(String arg[]) 
35     {
36         VersionInfoTest test = new VersionInfoTest();
37         try {
38             test.run(arg);
39         } catch (Exception e) {
40             test.errln("Error testing VersionInfo");
41         }
42     }
43     
44     /**
45      * Test that the instantiation works
46      */
47     public void TestInstance()
48     {
49         for (int i = 0; i < INSTANCE_INVALID_STRING_.length; i ++) {
50             try {
51                 VersionInfo.getInstance(INSTANCE_INVALID_STRING_[i]);
52                 errln("\"" + INSTANCE_INVALID_STRING_[i] + 
53                       "\" should produce an exception");
54             } catch (RuntimeException e) {
55                 logln("PASS: \"" + INSTANCE_INVALID_STRING_[i] + 
56                       "\" failed as expected");
57             }
58         }
59         for (int i = 0; i < INSTANCE_VALID_STRING_.length; i ++) {
60             try {
61                 VersionInfo.getInstance(INSTANCE_VALID_STRING_[i]);
62             } catch (RuntimeException e) {
63                 errln("\"" + INSTANCE_VALID_STRING_[i] + 
64                       "\" should produce an valid version");
65             }
66         }
67         for (int i = 0; i < INSTANCE_INVALID_INT_.length; i ++) {
68             try {
69                 getInstance(INSTANCE_INVALID_INT_[i]);
70                 errln("invalid ints should produce an exception");
71             } catch (RuntimeException e) {
72                 logln("PASS: \"" + INSTANCE_INVALID_INT_[i] + 
73                       "\" failed as expected");
74             }
75         }
76         for (int i = 0; i < INSTANCE_VALID_INT_.length; i ++) {
77             try {
78                 getInstance(INSTANCE_VALID_INT_[i]);
79             } catch (RuntimeException e) {
80                 errln("valid ints should not produce an exception");
81             }
82         }
83     }
84     
85     /**
86      * Test that the comparison works
87      */
88     public void TestCompare()
89     {
90         for (int i = 0; i < COMPARE_NOT_EQUAL_STRING_.length; i += 2) {
91             VersionInfo v1 = 
92                         VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i]);
93             VersionInfo v2 = 
94                     VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i + 1]);
95             if (v1.compareTo(v2) == 0) {
96                 errln(COMPARE_NOT_EQUAL_STRING_[i] + " should not equal " +
97                       COMPARE_NOT_EQUAL_STRING_[i + 1]);
98             }
99         }
100         for (int i = 0; i < COMPARE_NOT_EQUAL_INT_.length; i += 2) {
101             VersionInfo v1 = getInstance(COMPARE_NOT_EQUAL_INT_[i]);
102             VersionInfo v2 = getInstance(COMPARE_NOT_EQUAL_INT_[i + 1]);
103             if (v1.compareTo(v2) == 0) {
104                 errln(COMPARE_NOT_EQUAL_INT_[i] + " should not equal " +
105                       COMPARE_NOT_EQUAL_INT_[i + 1]);
106             }
107         }
108         for (int i = 0; i < COMPARE_EQUAL_STRING_.length - 1; i ++) {
109             VersionInfo v1 = 
110                         VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i]);
111             VersionInfo v2 = 
112                     VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i + 1]);
113             if (v1.compareTo(v2) != 0) {
114                 errln(COMPARE_EQUAL_STRING_[i] + " should equal " +
115                       COMPARE_EQUAL_STRING_[i + 1]);
116             }
117         }
118         for (int i = 0; i < COMPARE_EQUAL_INT_.length - 1; i ++) {
119             VersionInfo v1 = getInstance(COMPARE_EQUAL_INT_[i]);
120             VersionInfo v2 = getInstance(COMPARE_EQUAL_INT_[i + 1]);
121             if (v1.compareTo(v2) != 0) {
122                 errln(COMPARE_EQUAL_INT_[i] + " should equal " +
123                       COMPARE_EQUAL_INT_[i + 1]);
124             }
125         }
126         for (int i = 0; i < COMPARE_LESS_.length - 1; i ++) {
127             VersionInfo v1 = VersionInfo.getInstance(COMPARE_LESS_[i]);
128             VersionInfo v2 = VersionInfo.getInstance(COMPARE_LESS_[i + 1]);
129             if (v1.compareTo(v2) >= 0) {
130                 errln(COMPARE_LESS_[i] + " should be less than " + 
131                       COMPARE_LESS_[i + 1]);
132             }
133             if (v2.compareTo(v1) <= 0) {
134                 errln(COMPARE_LESS_[i + 1] + " should be greater than " + 
135                       COMPARE_LESS_[i]);
136             }
137         }
138     }
139     
140     /**
141      * Test that the getter function works
142      */
143     public void TestGetter()
144     {
145         for (int i = 0; i < GET_STRING_.length; i ++) {
146             VersionInfo v = VersionInfo.getInstance(GET_STRING_[i]);
147             if (v.getMajor() != GET_RESULT_[i << 2] ||
148                 v.getMinor() != GET_RESULT_[(i << 2) + 1] ||
149                 v.getMilli() != GET_RESULT_[(i << 2) + 2] ||
150                 v.getMicro() != GET_RESULT_[(i << 2) + 3]) {
151                 errln(GET_STRING_[i] + " should return major=" + 
152                       GET_RESULT_[i << 2] + " minor=" + 
153                       GET_RESULT_[(i << 2) + 1] + " milli=" + 
154                       GET_RESULT_[(i << 2) + 2] + " micro=" +
155                       GET_RESULT_[(i << 2) + 3]);  
156             }
157             v = getInstance(GET_INT_[i]);
158             if (v.getMajor() != GET_RESULT_[i << 2] ||
159                 v.getMinor() != GET_RESULT_[(i << 2) + 1] ||
160                 v.getMilli() != GET_RESULT_[(i << 2) + 2] ||
161                 v.getMicro() != GET_RESULT_[(i << 2) + 3]) {
162                 errln(GET_STRING_[i] + " should return major=" + 
163                       GET_RESULT_[i << 2] + " minor=" + 
164                       GET_RESULT_[(i << 2) + 1] + " milli=" + 
165                       GET_RESULT_[(i << 2) + 2] + " micro=" +
166                       GET_RESULT_[(i << 2) + 3]);  
167             }
168         }
169     }
170     
171     /**
172      * Test toString()
173      */
174     public void TesttoString() 
175     {
176         for (int i = 0; i < TOSTRING_STRING_.length; i ++) {
177             VersionInfo v = VersionInfo.getInstance(TOSTRING_STRING_[i]);
178             if (!v.toString().equals(TOSTRING_RESULT_[i])) {
179                 errln("toString() for " + TOSTRING_STRING_[i] + 
180                       " should produce " + TOSTRING_RESULT_[i]);
181             }
182             v = getInstance(TOSTRING_INT_[i]);
183             if (!v.toString().equals(TOSTRING_RESULT_[i])) {
184                 errln("toString() for " + TOSTRING_INT_[i] + 
185                       " should produce " + TOSTRING_RESULT_[i]);
186             }
187         }
188     }
189     
190     /**
191      * Test Comparable interface
192      */
193     public void TestComparable() {
194         for (int i = 0; i < COMPARE_NOT_EQUAL_STRING_.length; i += 2) {
195             VersionInfo v1 = VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i]);
196             VersionInfo v2 = VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i + 1]);
197             if (v1.compareTo(v2) == 0) {
198                 errln(COMPARE_NOT_EQUAL_STRING_[i] + " should not equal " +
199                       COMPARE_NOT_EQUAL_STRING_[i + 1]);
200             }
201         }
202         for (int i = 0; i < COMPARE_EQUAL_STRING_.length - 1; i ++) {
203             VersionInfo v1 = VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i]);
204             VersionInfo v2 = VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i + 1]);
205             if (v1.compareTo(v2) != 0) {
206                 errln(COMPARE_EQUAL_STRING_[i] + " should equal " +
207                       COMPARE_EQUAL_STRING_[i + 1]);
208             }
209         }
210     }
211     // private methods --------------------------------------------------
212     
213     /**
214      * int array versioninfo creation
215      */
216     private static VersionInfo getInstance(int data[])
217     {
218         switch (data.length) {
219             case 1:
220                 return VersionInfo.getInstance(data[0]);
221             case 2:
222                 return VersionInfo.getInstance(data[0], data[1]);
223             case 3:
224                 return VersionInfo.getInstance(data[0], data[1], data[2]);
225             default:
226                 return VersionInfo.getInstance(data[0], data[1], data[2], 
227                                                data[3]);
228         }
229     }
230     
231     // private data members --------------------------------------------
232     
233     /**
234      * Test instance data
235      */
236     private static final String INSTANCE_INVALID_STRING_[] = {
237         "a",
238         "-1",
239         "-1.0",
240         "-1.0.0",
241         "-1.0.0.0",
242         "0.-1",
243         "0.0.-1",
244         "0.0.0.-1",
245         "256",
246         "256.0",
247         "256.0.0",
248         "256.0.0.0",
249         "0.256",
250         "0.0.256",
251         "0.0.0.256",
252         "1.2.3.4.5"
253     };
254     private static final String INSTANCE_VALID_STRING_[] = {
255         "255",
256         "255.255",
257         "255.255.255",
258         "255.255.255.255"
259     };
260     private static final int INSTANCE_INVALID_INT_[][] = {
261         {-1},
262         {-1, 0},
263         {-1, 0, 0},
264         {-1, 0, 0, 0},
265         {0, -1},
266         {0, 0, -1},
267         {0, 0, 0, -1},
268         {256},
269         {256, 0},
270         {256, 0, 0},
271         {256, 0, 0, 0},
272         {0, 256},
273         {0, 0, 256},
274         {0, 0, 0, 256},
275     };
276     private static final int INSTANCE_VALID_INT_[][] = {
277         {255},
278         {255, 255},
279         {255, 255, 255},
280         {255, 255, 255, 255}
281     };
282     
283     /**
284      * Test compare data
285      */
286     private static final String COMPARE_NOT_EQUAL_STRING_[] = {
287         "2.0.0.0", "3.0.0.0"
288     };
289     private static final int COMPARE_NOT_EQUAL_INT_[][] = {
290         {2, 0, 0, 0}, {3, 0, 0, 0}
291     };
292     private static final String COMPARE_EQUAL_STRING_[] = {
293         "2.0.0.0", "2.0.0", "2.0", "2"
294     };
295     private static final int COMPARE_EQUAL_INT_[][] = {
296         {2}, {2, 0}, {2, 0, 0}, {2, 0, 0, 0}    
297     };
298     private static final String COMPARE_LESS_[] = {
299         "0", "0.0.0.1", "0.0.1", "0.1", "1", "2", "2.1", "2.1.1", "2.1.1.1"
300     };
301     
302     /**
303      * Test Getter data
304      */
305     private static final String GET_STRING_[] = {
306         "0",
307         "1.1",
308         "2.1.255",
309         "3.1.255.100"
310     };
311     private static final int GET_INT_[][] = {
312         {0},
313         {1, 1},
314         {2, 1, 255},
315         {3, 1, 255, 100}
316     };
317     private static final int GET_RESULT_[] = {
318         0, 0, 0, 0,
319         1, 1, 0, 0,
320         2, 1, 255, 0,
321         3, 1, 255, 100
322     };
323     
324     /**
325      * Test toString data
326      */
327     private static final String TOSTRING_STRING_[] = {
328         "0",
329         "1.1",
330         "2.1.255",
331         "3.1.255.100"    
332     };
333     private static final int TOSTRING_INT_[][] = {
334         {0},
335         {1, 1},
336         {2, 1, 255},
337         {3, 1, 255, 100}
338     };
339     private static final String TOSTRING_RESULT_[] = {
340         "0.0.0.0",
341         "1.1.0.0",
342         "2.1.255.0",
343         "3.1.255.100"    
344     };
345
346     /*
347      * Test case for multi-threading problem reported by ticket#7880
348      */
349     public void TestMultiThread() {
350         final int numThreads = 20;
351         GetInstanceWorker[] workers = new GetInstanceWorker[numThreads];
352         VersionInfo[][] results = new VersionInfo[numThreads][255];
353
354         // Create workers
355         for (int i = 0; i < workers.length; i++) {
356             workers[i] = new GetInstanceWorker(i, results[i]);
357         }
358
359         // Start workers
360         for (int i = 0; i < workers.length; i++) {
361             workers[i].start();
362         }
363
364         // Wait for the completion
365         for (int i = 0; i < workers.length; i++) {
366             try {
367                 workers[i].join();
368             } catch (InterruptedException e) {
369                 errln("A problem in thread execution. " + e.getMessage());
370             }
371         }
372
373         // Check if singleton for each
374         for (int i = 1; i < results.length; i++) {
375             for (int j = 0; j < results[0].length; j++) {
376                 if (results[0][j] != results[i][j]) {
377                     errln("Different instance at index " + j + " Thread#" + i);
378                 }
379             }
380         }
381     }
382
383     private class GetInstanceWorker extends Thread {
384         private VersionInfo[] results;
385
386         GetInstanceWorker(int serialNumber, VersionInfo[] results) {
387             super("GetInstnaceWorker#" + serialNumber);
388             this.results = results;
389         }
390
391         public void run() {
392             for (int i = 0; i < results.length; i++) {
393                 results[i] = VersionInfo.getInstance(i);
394             }
395         }
396     }
397 }