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