Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Written by Doug Lea with assistance from members of JCP JSR-166 |
| 3 | * Expert Group and released to the public domain, as explained at |
| 4 | * http://creativecommons.org/publicdomain/zero/1.0/ |
| 5 | * Other contributors include Andrew Wright, Jeffrey Hayes, |
| 6 | * Pat Fisher, Mike Judd. |
| 7 | */ |
| 8 | |
| 9 | package jsr166; |
| 10 | |
| 11 | import junit.framework.*; |
| 12 | import java.util.concurrent.CountDownLatch; |
| 13 | import java.util.concurrent.TimeUnit; |
| 14 | |
| 15 | public class TimeUnitTest extends JSR166TestCase { |
| 16 | |
| 17 | // (loops to 88888 check increments at all time divisions.) |
| 18 | |
| 19 | /** |
| 20 | * convert correctly converts sample values across the units |
| 21 | */ |
| 22 | public void testConvert() { |
| 23 | for (long t = 0; t < 88888; ++t) { |
| 24 | assertEquals(t*60*60*24, |
| 25 | TimeUnit.SECONDS.convert(t, |
| 26 | TimeUnit.DAYS)); |
| 27 | assertEquals(t*60*60, |
| 28 | TimeUnit.SECONDS.convert(t, |
| 29 | TimeUnit.HOURS)); |
| 30 | assertEquals(t*60, |
| 31 | TimeUnit.SECONDS.convert(t, |
| 32 | TimeUnit.MINUTES)); |
| 33 | assertEquals(t, |
| 34 | TimeUnit.SECONDS.convert(t, |
| 35 | TimeUnit.SECONDS)); |
| 36 | assertEquals(t, |
| 37 | TimeUnit.SECONDS.convert(1000L*t, |
| 38 | TimeUnit.MILLISECONDS)); |
| 39 | assertEquals(t, |
| 40 | TimeUnit.SECONDS.convert(1000000L*t, |
| 41 | TimeUnit.MICROSECONDS)); |
| 42 | assertEquals(t, |
| 43 | TimeUnit.SECONDS.convert(1000000000L*t, |
| 44 | TimeUnit.NANOSECONDS)); |
| 45 | |
| 46 | assertEquals(1000L*t*60*60*24, |
| 47 | TimeUnit.MILLISECONDS.convert(t, |
| 48 | TimeUnit.DAYS)); |
| 49 | assertEquals(1000L*t*60*60, |
| 50 | TimeUnit.MILLISECONDS.convert(t, |
| 51 | TimeUnit.HOURS)); |
| 52 | assertEquals(1000L*t*60, |
| 53 | TimeUnit.MILLISECONDS.convert(t, |
| 54 | TimeUnit.MINUTES)); |
| 55 | assertEquals(1000L*t, |
| 56 | TimeUnit.MILLISECONDS.convert(t, |
| 57 | TimeUnit.SECONDS)); |
| 58 | assertEquals(t, |
| 59 | TimeUnit.MILLISECONDS.convert(t, |
| 60 | TimeUnit.MILLISECONDS)); |
| 61 | assertEquals(t, |
| 62 | TimeUnit.MILLISECONDS.convert(1000L*t, |
| 63 | TimeUnit.MICROSECONDS)); |
| 64 | assertEquals(t, |
| 65 | TimeUnit.MILLISECONDS.convert(1000000L*t, |
| 66 | TimeUnit.NANOSECONDS)); |
| 67 | |
| 68 | assertEquals(1000000L*t*60*60*24, |
| 69 | TimeUnit.MICROSECONDS.convert(t, |
| 70 | TimeUnit.DAYS)); |
| 71 | assertEquals(1000000L*t*60*60, |
| 72 | TimeUnit.MICROSECONDS.convert(t, |
| 73 | TimeUnit.HOURS)); |
| 74 | assertEquals(1000000L*t*60, |
| 75 | TimeUnit.MICROSECONDS.convert(t, |
| 76 | TimeUnit.MINUTES)); |
| 77 | assertEquals(1000000L*t, |
| 78 | TimeUnit.MICROSECONDS.convert(t, |
| 79 | TimeUnit.SECONDS)); |
| 80 | assertEquals(1000L*t, |
| 81 | TimeUnit.MICROSECONDS.convert(t, |
| 82 | TimeUnit.MILLISECONDS)); |
| 83 | assertEquals(t, |
| 84 | TimeUnit.MICROSECONDS.convert(t, |
| 85 | TimeUnit.MICROSECONDS)); |
| 86 | assertEquals(t, |
| 87 | TimeUnit.MICROSECONDS.convert(1000L*t, |
| 88 | TimeUnit.NANOSECONDS)); |
| 89 | |
| 90 | assertEquals(1000000000L*t*60*60*24, |
| 91 | TimeUnit.NANOSECONDS.convert(t, |
| 92 | TimeUnit.DAYS)); |
| 93 | assertEquals(1000000000L*t*60*60, |
| 94 | TimeUnit.NANOSECONDS.convert(t, |
| 95 | TimeUnit.HOURS)); |
| 96 | assertEquals(1000000000L*t*60, |
| 97 | TimeUnit.NANOSECONDS.convert(t, |
| 98 | TimeUnit.MINUTES)); |
| 99 | assertEquals(1000000000L*t, |
| 100 | TimeUnit.NANOSECONDS.convert(t, |
| 101 | TimeUnit.SECONDS)); |
| 102 | assertEquals(1000000L*t, |
| 103 | TimeUnit.NANOSECONDS.convert(t, |
| 104 | TimeUnit.MILLISECONDS)); |
| 105 | assertEquals(1000L*t, |
| 106 | TimeUnit.NANOSECONDS.convert(t, |
| 107 | TimeUnit.MICROSECONDS)); |
| 108 | assertEquals(t, |
| 109 | TimeUnit.NANOSECONDS.convert(t, |
| 110 | TimeUnit.NANOSECONDS)); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * toNanos correctly converts sample values in different units to |
| 116 | * nanoseconds |
| 117 | */ |
| 118 | public void testToNanos() { |
| 119 | for (long t = 0; t < 88888; ++t) { |
| 120 | assertEquals(t*1000000000L*60*60*24, |
| 121 | TimeUnit.DAYS.toNanos(t)); |
| 122 | assertEquals(t*1000000000L*60*60, |
| 123 | TimeUnit.HOURS.toNanos(t)); |
| 124 | assertEquals(t*1000000000L*60, |
| 125 | TimeUnit.MINUTES.toNanos(t)); |
| 126 | assertEquals(1000000000L*t, |
| 127 | TimeUnit.SECONDS.toNanos(t)); |
| 128 | assertEquals(1000000L*t, |
| 129 | TimeUnit.MILLISECONDS.toNanos(t)); |
| 130 | assertEquals(1000L*t, |
| 131 | TimeUnit.MICROSECONDS.toNanos(t)); |
| 132 | assertEquals(t, |
| 133 | TimeUnit.NANOSECONDS.toNanos(t)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * toMicros correctly converts sample values in different units to |
| 139 | * microseconds |
| 140 | */ |
| 141 | public void testToMicros() { |
| 142 | for (long t = 0; t < 88888; ++t) { |
| 143 | assertEquals(t*1000000L*60*60*24, |
| 144 | TimeUnit.DAYS.toMicros(t)); |
| 145 | assertEquals(t*1000000L*60*60, |
| 146 | TimeUnit.HOURS.toMicros(t)); |
| 147 | assertEquals(t*1000000L*60, |
| 148 | TimeUnit.MINUTES.toMicros(t)); |
| 149 | assertEquals(1000000L*t, |
| 150 | TimeUnit.SECONDS.toMicros(t)); |
| 151 | assertEquals(1000L*t, |
| 152 | TimeUnit.MILLISECONDS.toMicros(t)); |
| 153 | assertEquals(t, |
| 154 | TimeUnit.MICROSECONDS.toMicros(t)); |
| 155 | assertEquals(t, |
| 156 | TimeUnit.NANOSECONDS.toMicros(t*1000L)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * toMillis correctly converts sample values in different units to |
| 162 | * milliseconds |
| 163 | */ |
| 164 | public void testToMillis() { |
| 165 | for (long t = 0; t < 88888; ++t) { |
| 166 | assertEquals(t*1000L*60*60*24, |
| 167 | TimeUnit.DAYS.toMillis(t)); |
| 168 | assertEquals(t*1000L*60*60, |
| 169 | TimeUnit.HOURS.toMillis(t)); |
| 170 | assertEquals(t*1000L*60, |
| 171 | TimeUnit.MINUTES.toMillis(t)); |
| 172 | assertEquals(1000L*t, |
| 173 | TimeUnit.SECONDS.toMillis(t)); |
| 174 | assertEquals(t, |
| 175 | TimeUnit.MILLISECONDS.toMillis(t)); |
| 176 | assertEquals(t, |
| 177 | TimeUnit.MICROSECONDS.toMillis(t*1000L)); |
| 178 | assertEquals(t, |
| 179 | TimeUnit.NANOSECONDS.toMillis(t*1000000L)); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * toSeconds correctly converts sample values in different units to |
| 185 | * seconds |
| 186 | */ |
| 187 | public void testToSeconds() { |
| 188 | for (long t = 0; t < 88888; ++t) { |
| 189 | assertEquals(t*60*60*24, |
| 190 | TimeUnit.DAYS.toSeconds(t)); |
| 191 | assertEquals(t*60*60, |
| 192 | TimeUnit.HOURS.toSeconds(t)); |
| 193 | assertEquals(t*60, |
| 194 | TimeUnit.MINUTES.toSeconds(t)); |
| 195 | assertEquals(t, |
| 196 | TimeUnit.SECONDS.toSeconds(t)); |
| 197 | assertEquals(t, |
| 198 | TimeUnit.MILLISECONDS.toSeconds(t*1000L)); |
| 199 | assertEquals(t, |
| 200 | TimeUnit.MICROSECONDS.toSeconds(t*1000000L)); |
| 201 | assertEquals(t, |
| 202 | TimeUnit.NANOSECONDS.toSeconds(t*1000000000L)); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * toMinutes correctly converts sample values in different units to |
| 208 | * minutes |
| 209 | */ |
| 210 | public void testToMinutes() { |
| 211 | for (long t = 0; t < 88888; ++t) { |
| 212 | assertEquals(t*60*24, |
| 213 | TimeUnit.DAYS.toMinutes(t)); |
| 214 | assertEquals(t*60, |
| 215 | TimeUnit.HOURS.toMinutes(t)); |
| 216 | assertEquals(t, |
| 217 | TimeUnit.MINUTES.toMinutes(t)); |
| 218 | assertEquals(t, |
| 219 | TimeUnit.SECONDS.toMinutes(t*60)); |
| 220 | assertEquals(t, |
| 221 | TimeUnit.MILLISECONDS.toMinutes(t*1000L*60)); |
| 222 | assertEquals(t, |
| 223 | TimeUnit.MICROSECONDS.toMinutes(t*1000000L*60)); |
| 224 | assertEquals(t, |
| 225 | TimeUnit.NANOSECONDS.toMinutes(t*1000000000L*60)); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * toHours correctly converts sample values in different units to |
| 231 | * hours |
| 232 | */ |
| 233 | public void testToHours() { |
| 234 | for (long t = 0; t < 88888; ++t) { |
| 235 | assertEquals(t*24, |
| 236 | TimeUnit.DAYS.toHours(t)); |
| 237 | assertEquals(t, |
| 238 | TimeUnit.HOURS.toHours(t)); |
| 239 | assertEquals(t, |
| 240 | TimeUnit.MINUTES.toHours(t*60)); |
| 241 | assertEquals(t, |
| 242 | TimeUnit.SECONDS.toHours(t*60*60)); |
| 243 | assertEquals(t, |
| 244 | TimeUnit.MILLISECONDS.toHours(t*1000L*60*60)); |
| 245 | assertEquals(t, |
| 246 | TimeUnit.MICROSECONDS.toHours(t*1000000L*60*60)); |
| 247 | assertEquals(t, |
| 248 | TimeUnit.NANOSECONDS.toHours(t*1000000000L*60*60)); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * toDays correctly converts sample values in different units to |
| 254 | * days |
| 255 | */ |
| 256 | public void testToDays() { |
| 257 | for (long t = 0; t < 88888; ++t) { |
| 258 | assertEquals(t, |
| 259 | TimeUnit.DAYS.toDays(t)); |
| 260 | assertEquals(t, |
| 261 | TimeUnit.HOURS.toDays(t*24)); |
| 262 | assertEquals(t, |
| 263 | TimeUnit.MINUTES.toDays(t*60*24)); |
| 264 | assertEquals(t, |
| 265 | TimeUnit.SECONDS.toDays(t*60*60*24)); |
| 266 | assertEquals(t, |
| 267 | TimeUnit.MILLISECONDS.toDays(t*1000L*60*60*24)); |
| 268 | assertEquals(t, |
| 269 | TimeUnit.MICROSECONDS.toDays(t*1000000L*60*60*24)); |
| 270 | assertEquals(t, |
| 271 | TimeUnit.NANOSECONDS.toDays(t*1000000000L*60*60*24)); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * convert saturates positive too-large values to Long.MAX_VALUE |
| 277 | * and negative to LONG.MIN_VALUE |
| 278 | */ |
| 279 | public void testConvertSaturate() { |
| 280 | assertEquals(Long.MAX_VALUE, |
| 281 | TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
| 282 | TimeUnit.SECONDS)); |
| 283 | assertEquals(Long.MIN_VALUE, |
| 284 | TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
| 285 | TimeUnit.SECONDS)); |
| 286 | assertEquals(Long.MAX_VALUE, |
| 287 | TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
| 288 | TimeUnit.MINUTES)); |
| 289 | assertEquals(Long.MIN_VALUE, |
| 290 | TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
| 291 | TimeUnit.MINUTES)); |
| 292 | assertEquals(Long.MAX_VALUE, |
| 293 | TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
| 294 | TimeUnit.HOURS)); |
| 295 | assertEquals(Long.MIN_VALUE, |
| 296 | TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
| 297 | TimeUnit.HOURS)); |
| 298 | assertEquals(Long.MAX_VALUE, |
| 299 | TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
| 300 | TimeUnit.DAYS)); |
| 301 | assertEquals(Long.MIN_VALUE, |
| 302 | TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
| 303 | TimeUnit.DAYS)); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * toNanos saturates positive too-large values to Long.MAX_VALUE |
| 308 | * and negative to LONG.MIN_VALUE |
| 309 | */ |
| 310 | public void testToNanosSaturate() { |
| 311 | assertEquals(Long.MAX_VALUE, |
| 312 | TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
| 313 | assertEquals(Long.MIN_VALUE, |
| 314 | TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * toString returns name of unit |
| 319 | */ |
| 320 | public void testToString() { |
| 321 | assertEquals("SECONDS", TimeUnit.SECONDS.toString()); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * name returns name of unit |
| 326 | */ |
| 327 | public void testName() { |
| 328 | assertEquals("SECONDS", TimeUnit.SECONDS.name()); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Timed wait without holding lock throws |
| 333 | * IllegalMonitorStateException |
| 334 | */ |
| 335 | public void testTimedWait_IllegalMonitorException() { |
| 336 | Thread t = newStartedThread(new CheckedRunnable() { |
| 337 | public void realRun() throws InterruptedException { |
| 338 | Object o = new Object(); |
| 339 | TimeUnit tu = TimeUnit.MILLISECONDS; |
| 340 | |
| 341 | try { |
| 342 | tu.timedWait(o, LONG_DELAY_MS); |
| 343 | threadShouldThrow(); |
| 344 | } catch (IllegalMonitorStateException success) {} |
| 345 | }}); |
| 346 | |
| 347 | awaitTermination(t); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * timedWait throws InterruptedException when interrupted |
| 352 | */ |
| 353 | public void testTimedWait_Interruptible() { |
| 354 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 355 | Thread t = newStartedThread(new CheckedRunnable() { |
| 356 | public void realRun() throws InterruptedException { |
| 357 | Object o = new Object(); |
| 358 | TimeUnit tu = TimeUnit.MILLISECONDS; |
| 359 | |
| 360 | Thread.currentThread().interrupt(); |
| 361 | try { |
| 362 | synchronized (o) { |
| 363 | tu.timedWait(o, LONG_DELAY_MS); |
| 364 | } |
| 365 | shouldThrow(); |
| 366 | } catch (InterruptedException success) {} |
| 367 | assertFalse(Thread.interrupted()); |
| 368 | |
| 369 | pleaseInterrupt.countDown(); |
| 370 | try { |
| 371 | synchronized (o) { |
| 372 | tu.timedWait(o, LONG_DELAY_MS); |
| 373 | } |
| 374 | shouldThrow(); |
| 375 | } catch (InterruptedException success) {} |
| 376 | assertFalse(Thread.interrupted()); |
| 377 | }}); |
| 378 | |
| 379 | await(pleaseInterrupt); |
| 380 | assertThreadStaysAlive(t); |
| 381 | t.interrupt(); |
| 382 | awaitTermination(t); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * timedJoin throws InterruptedException when interrupted |
| 387 | */ |
| 388 | public void testTimedJoin_Interruptible() { |
| 389 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 390 | final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
| 391 | public void realRun() throws InterruptedException { |
| 392 | Thread.sleep(LONG_DELAY_MS); |
| 393 | }}); |
| 394 | final Thread t = newStartedThread(new CheckedRunnable() { |
| 395 | public void realRun() throws InterruptedException { |
| 396 | TimeUnit tu = TimeUnit.MILLISECONDS; |
| 397 | Thread.currentThread().interrupt(); |
| 398 | try { |
| 399 | tu.timedJoin(s, LONG_DELAY_MS); |
| 400 | shouldThrow(); |
| 401 | } catch (InterruptedException success) {} |
| 402 | assertFalse(Thread.interrupted()); |
| 403 | |
| 404 | pleaseInterrupt.countDown(); |
| 405 | try { |
| 406 | tu.timedJoin(s, LONG_DELAY_MS); |
| 407 | shouldThrow(); |
| 408 | } catch (InterruptedException success) {} |
| 409 | assertFalse(Thread.interrupted()); |
| 410 | }}); |
| 411 | |
| 412 | await(pleaseInterrupt); |
| 413 | assertThreadStaysAlive(t); |
| 414 | t.interrupt(); |
| 415 | awaitTermination(t); |
| 416 | s.interrupt(); |
| 417 | awaitTermination(s); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * timedSleep throws InterruptedException when interrupted |
| 422 | */ |
| 423 | public void testTimedSleep_Interruptible() { |
| 424 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 425 | Thread t = newStartedThread(new CheckedRunnable() { |
| 426 | public void realRun() throws InterruptedException { |
| 427 | TimeUnit tu = TimeUnit.MILLISECONDS; |
| 428 | Thread.currentThread().interrupt(); |
| 429 | try { |
| 430 | tu.sleep(LONG_DELAY_MS); |
| 431 | shouldThrow(); |
| 432 | } catch (InterruptedException success) {} |
| 433 | assertFalse(Thread.interrupted()); |
| 434 | |
| 435 | pleaseInterrupt.countDown(); |
| 436 | try { |
| 437 | tu.sleep(LONG_DELAY_MS); |
| 438 | shouldThrow(); |
| 439 | } catch (InterruptedException success) {} |
| 440 | assertFalse(Thread.interrupted()); |
| 441 | }}); |
| 442 | |
| 443 | await(pleaseInterrupt); |
| 444 | assertThreadStaysAlive(t); |
| 445 | t.interrupt(); |
| 446 | awaitTermination(t); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * a deserialized serialized unit is the same instance |
| 451 | */ |
| 452 | public void testSerialization() throws Exception { |
| 453 | TimeUnit x = TimeUnit.MILLISECONDS; |
| 454 | assertSame(x, serialClone(x)); |
| 455 | } |
| 456 | |
| 457 | } |