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