summaryrefslogtreecommitdiffstats
path: root/jsr166-tests/src/test/java/jsr166/TimeUnitTest.java
blob: 2c9529b43fbda547fa9512cf6b764b0288691204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 * Other contributors include Andrew Wright, Jeffrey Hayes,
 * Pat Fisher, Mike Judd.
 */

package jsr166;

import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import junit.framework.Test;
import junit.framework.TestSuite;

public class TimeUnitTest extends JSR166TestCase {
    // android-note: Removed because the CTS runner does a bad job of
    // retrying tests that have suite() declarations.
    //
    // public static void main(String[] args) {
    //     main(suite(), args);
    // }
    // public static Test suite() {
    //     return new TestSuite(...);
    // }

    // (loops to 88888 check increments at all time divisions.)

    /**
     * convert correctly converts sample values across the units
     */
    public void testConvert() {
        for (long t = 0; t < 88888; ++t) {
            assertEquals(t*60*60*24,
                         SECONDS.convert(t, DAYS));
            assertEquals(t*60*60,
                         SECONDS.convert(t, HOURS));
            assertEquals(t*60,
                         SECONDS.convert(t, MINUTES));
            assertEquals(t,
                         SECONDS.convert(t, SECONDS));
            assertEquals(t,
                         SECONDS.convert(1000L*t, MILLISECONDS));
            assertEquals(t,
                         SECONDS.convert(1000000L*t, MICROSECONDS));
            assertEquals(t,
                         SECONDS.convert(1000000000L*t, NANOSECONDS));

            assertEquals(1000L*t*60*60*24,
                         MILLISECONDS.convert(t, DAYS));
            assertEquals(1000L*t*60*60,
                         MILLISECONDS.convert(t, HOURS));
            assertEquals(1000L*t*60,
                         MILLISECONDS.convert(t, MINUTES));
            assertEquals(1000L*t,
                         MILLISECONDS.convert(t, SECONDS));
            assertEquals(t,
                         MILLISECONDS.convert(t, MILLISECONDS));
            assertEquals(t,
                         MILLISECONDS.convert(1000L*t, MICROSECONDS));
            assertEquals(t,
                         MILLISECONDS.convert(1000000L*t, NANOSECONDS));

            assertEquals(1000000L*t*60*60*24,
                         MICROSECONDS.convert(t, DAYS));
            assertEquals(1000000L*t*60*60,
                         MICROSECONDS.convert(t, HOURS));
            assertEquals(1000000L*t*60,
                         MICROSECONDS.convert(t, MINUTES));
            assertEquals(1000000L*t,
                         MICROSECONDS.convert(t, SECONDS));
            assertEquals(1000L*t,
                         MICROSECONDS.convert(t, MILLISECONDS));
            assertEquals(t,
                         MICROSECONDS.convert(t, MICROSECONDS));
            assertEquals(t,
                         MICROSECONDS.convert(1000L*t, NANOSECONDS));

            assertEquals(1000000000L*t*60*60*24,
                         NANOSECONDS.convert(t, DAYS));
            assertEquals(1000000000L*t*60*60,
                         NANOSECONDS.convert(t, HOURS));
            assertEquals(1000000000L*t*60,
                         NANOSECONDS.convert(t, MINUTES));
            assertEquals(1000000000L*t,
                         NANOSECONDS.convert(t, SECONDS));
            assertEquals(1000000L*t,
                         NANOSECONDS.convert(t, MILLISECONDS));
            assertEquals(1000L*t,
                         NANOSECONDS.convert(t, MICROSECONDS));
            assertEquals(t,
                         NANOSECONDS.convert(t, NANOSECONDS));
        }
    }

    /**
     * toNanos correctly converts sample values in different units to
     * nanoseconds
     */
    public void testToNanos() {
        for (long t = 0; t < 88888; ++t) {
            assertEquals(t*1000000000L*60*60*24,
                         DAYS.toNanos(t));
            assertEquals(t*1000000000L*60*60,
                         HOURS.toNanos(t));
            assertEquals(t*1000000000L*60,
                         MINUTES.toNanos(t));
            assertEquals(1000000000L*t,
                         SECONDS.toNanos(t));
            assertEquals(1000000L*t,
                         MILLISECONDS.toNanos(t));
            assertEquals(1000L*t,
                         MICROSECONDS.toNanos(t));
            assertEquals(t,
                         NANOSECONDS.toNanos(t));
        }
    }

    /**
     * toMicros correctly converts sample values in different units to
     * microseconds
     */
    public void testToMicros() {
        for (long t = 0; t < 88888; ++t) {
            assertEquals(t*1000000L*60*60*24,
                         DAYS.toMicros(t));
            assertEquals(t*1000000L*60*60,
                         HOURS.toMicros(t));
            assertEquals(t*1000000L*60,
                         MINUTES.toMicros(t));
            assertEquals(1000000L*t,
                         SECONDS.toMicros(t));
            assertEquals(1000L*t,
                         MILLISECONDS.toMicros(t));
            assertEquals(t,
                         MICROSECONDS.toMicros(t));
            assertEquals(t,
                         NANOSECONDS.toMicros(t*1000L));
        }
    }

    /**
     * toMillis correctly converts sample values in different units to
     * milliseconds
     */
    public void testToMillis() {
        for (long t = 0; t < 88888; ++t) {
            assertEquals(t*1000L*60*60*24,
                         DAYS.toMillis(t));
            assertEquals(t*1000L*60*60,
                         HOURS.toMillis(t));
            assertEquals(t*1000L*60,
                         MINUTES.toMillis(t));
            assertEquals(1000L*t,
                         SECONDS.toMillis(t));
            assertEquals(t,
                         MILLISECONDS.toMillis(t));
            assertEquals(t,
                         MICROSECONDS.toMillis(t*1000L));
            assertEquals(t,
                         NANOSECONDS.toMillis(t*1000000L));
        }
    }

    /**
     * toSeconds correctly converts sample values in different units to
     * seconds
     */
    public void testToSeconds() {
        for (long t = 0; t < 88888; ++t) {
            assertEquals(t*60*60*24,
                         DAYS.toSeconds(t));
            assertEquals(t*60*60,
                         HOURS.toSeconds(t));
            assertEquals(t*60,
                         MINUTES.toSeconds(t));
            assertEquals(t,
                         SECONDS.toSeconds(t));
            assertEquals(t,
                         MILLISECONDS.toSeconds(t*1000L));
            assertEquals(t,
                         MICROSECONDS.toSeconds(t*1000000L));
            assertEquals(t,
                         NANOSECONDS.toSeconds(t*1000000000L));
        }
    }

    /**
     * toMinutes correctly converts sample values in different units to
     * minutes
     */
    public void testToMinutes() {
        for (long t = 0; t < 88888; ++t) {
            assertEquals(t*60*24,
                         DAYS.toMinutes(t));
            assertEquals(t*60,
                         HOURS.toMinutes(t));
            assertEquals(t,
                         MINUTES.toMinutes(t));
            assertEquals(t,
                         SECONDS.toMinutes(t*60));
            assertEquals(t,
                         MILLISECONDS.toMinutes(t*1000L*60));
            assertEquals(t,
                         MICROSECONDS.toMinutes(t*1000000L*60));
            assertEquals(t,
                         NANOSECONDS.toMinutes(t*1000000000L*60));
        }
    }

    /**
     * toHours correctly converts sample values in different units to
     * hours
     */
    public void testToHours() {
        for (long t = 0; t < 88888; ++t) {
            assertEquals(t*24,
                         DAYS.toHours(t));
            assertEquals(t,
                         HOURS.toHours(t));
            assertEquals(t,
                         MINUTES.toHours(t*60));
            assertEquals(t,
                         SECONDS.toHours(t*60*60));
            assertEquals(t,
                         MILLISECONDS.toHours(t*1000L*60*60));
            assertEquals(t,
                         MICROSECONDS.toHours(t*1000000L*60*60));
            assertEquals(t,
                         NANOSECONDS.toHours(t*1000000000L*60*60));
        }
    }

    /**
     * toDays correctly converts sample values in different units to
     * days
     */
    public void testToDays() {
        for (long t = 0; t < 88888; ++t) {
            assertEquals(t,
                         DAYS.toDays(t));
            assertEquals(t,
                         HOURS.toDays(t*24));
            assertEquals(t,
                         MINUTES.toDays(t*60*24));
            assertEquals(t,
                         SECONDS.toDays(t*60*60*24));
            assertEquals(t,
                         MILLISECONDS.toDays(t*1000L*60*60*24));
            assertEquals(t,
                         MICROSECONDS.toDays(t*1000000L*60*60*24));
            assertEquals(t,
                         NANOSECONDS.toDays(t*1000000000L*60*60*24));
        }
    }

    /**
     * convert saturates positive too-large values to Long.MAX_VALUE
     * and negative to LONG.MIN_VALUE
     */
    public void testConvertSaturate() {
        assertEquals(Long.MAX_VALUE,
                     NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
        assertEquals(Long.MIN_VALUE,
                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS));
        assertEquals(Long.MAX_VALUE,
                     NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES));
        assertEquals(Long.MIN_VALUE,
                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES));
        assertEquals(Long.MAX_VALUE,
                     NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS));
        assertEquals(Long.MIN_VALUE,
                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS));
        assertEquals(Long.MAX_VALUE,
                     NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
        assertEquals(Long.MIN_VALUE,
                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS));
    }

    /**
     * toNanos saturates positive too-large values to Long.MAX_VALUE
     * and negative to LONG.MIN_VALUE
     */
    public void testToNanosSaturate() {
        assertEquals(Long.MAX_VALUE,
                     MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
        assertEquals(Long.MIN_VALUE,
                     MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
    }

    /**
     * toString returns name of unit
     */
    public void testToString() {
        assertEquals("SECONDS", SECONDS.toString());
    }

    /**
     * name returns name of unit
     */
    public void testName() {
        assertEquals("SECONDS", SECONDS.name());
    }

    /**
     * Timed wait without holding lock throws
     * IllegalMonitorStateException
     */
    public void testTimedWait_IllegalMonitorException() {
        Thread t = newStartedThread(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                Object o = new Object();
                TimeUnit tu = MILLISECONDS;

                try {
                    tu.timedWait(o, LONG_DELAY_MS);
                    threadShouldThrow();
                } catch (IllegalMonitorStateException success) {}
            }});

        awaitTermination(t);
    }

    /**
     * timedWait throws InterruptedException when interrupted
     */
    public void testTimedWait_Interruptible() {
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
        Thread t = newStartedThread(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                Object o = new Object();
                TimeUnit tu = MILLISECONDS;

                Thread.currentThread().interrupt();
                try {
                    synchronized (o) {
                        tu.timedWait(o, LONG_DELAY_MS);
                    }
                    shouldThrow();
                } catch (InterruptedException success) {}
                assertFalse(Thread.interrupted());

                pleaseInterrupt.countDown();
                try {
                    synchronized (o) {
                        tu.timedWait(o, LONG_DELAY_MS);
                    }
                    shouldThrow();
                } catch (InterruptedException success) {}
                assertFalse(Thread.interrupted());
            }});

        await(pleaseInterrupt);
        assertThreadStaysAlive(t);
        t.interrupt();
        awaitTermination(t);
    }

    /**
     * timedJoin throws InterruptedException when interrupted
     */
    public void testTimedJoin_Interruptible() {
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
        final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
            public void realRun() throws InterruptedException {
                Thread.sleep(LONG_DELAY_MS);
            }});
        final Thread t = newStartedThread(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                TimeUnit tu = MILLISECONDS;
                Thread.currentThread().interrupt();
                try {
                    tu.timedJoin(s, LONG_DELAY_MS);
                    shouldThrow();
                } catch (InterruptedException success) {}
                assertFalse(Thread.interrupted());

                pleaseInterrupt.countDown();
                try {
                    tu.timedJoin(s, LONG_DELAY_MS);
                    shouldThrow();
                } catch (InterruptedException success) {}
                assertFalse(Thread.interrupted());
            }});

        await(pleaseInterrupt);
        assertThreadStaysAlive(t);
        t.interrupt();
        awaitTermination(t);
        s.interrupt();
        awaitTermination(s);
    }

    /**
     * timedSleep throws InterruptedException when interrupted
     */
    public void testTimedSleep_Interruptible() {
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
        Thread t = newStartedThread(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                TimeUnit tu = MILLISECONDS;
                Thread.currentThread().interrupt();
                try {
                    tu.sleep(LONG_DELAY_MS);
                    shouldThrow();
                } catch (InterruptedException success) {}
                assertFalse(Thread.interrupted());

                pleaseInterrupt.countDown();
                try {
                    tu.sleep(LONG_DELAY_MS);
                    shouldThrow();
                } catch (InterruptedException success) {}
                assertFalse(Thread.interrupted());
            }});

        await(pleaseInterrupt);
        assertThreadStaysAlive(t);
        t.interrupt();
        awaitTermination(t);
    }

    /**
     * a deserialized serialized unit is the same instance
     */
    public void testSerialization() throws Exception {
        TimeUnit x = MILLISECONDS;
        assertSame(x, serialClone(x));
    }

}