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/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9package tests.api.java.util.concurrent;
10
11import junit.framework.*;
12
13public class SystemTest extends JSR166TestCase {
14    public static void main(String[] args) {
15        junit.textui.TestRunner.run(suite());
16    }
17
18    public static Test suite() {
19        return new TestSuite(SystemTest.class);
20    }
21
22    /**
23     * Worst case rounding for millisecs; set for 60 cycle millis clock.
24     * This value might need to be changed os JVMs with coarser
25     *  System.currentTimeMillis clocks.
26     */
27    static final long MILLIS_ROUND = 17;
28
29    /**
30     * Nanos between readings of millis is no longer than millis (plus
31     * possible rounding).
32     * This shows only that nano timing not (much) worse than milli.
33     */
34    public void testNanoTime1() {
35        try {
36            long m1 = System.currentTimeMillis();
37            Thread.sleep(1);
38            long n1 = System.nanoTime();
39            Thread.sleep(SHORT_DELAY_MS);
40            long n2 = System.nanoTime();
41            Thread.sleep(1);
42            long m2 = System.currentTimeMillis();
43            long millis = m2 - m1;
44            long nanos = n2 - n1;
45            assertTrue(nanos >= 0);
46            long nanosAsMillis = nanos / 1000000;
47            assertTrue(nanosAsMillis <= millis + MILLIS_ROUND);
48        }
49        catch(InterruptedException ie) {
50            unexpectedException();
51        }
52    }
53
54    /**
55     * Millis between readings of nanos is less than nanos, adjusting
56     * for rounding.
57     * This shows only that nano timing not (much) worse than milli.
58     */
59    public void testNanoTime2() {
60        try {
61            long n1 = System.nanoTime();
62            Thread.sleep(1);
63            long m1 = System.currentTimeMillis();
64            Thread.sleep(SHORT_DELAY_MS);
65            long m2 = System.currentTimeMillis();
66            Thread.sleep(1);
67            long n2 = System.nanoTime();
68            long millis = m2 - m1;
69            long nanos = n2 - n1;
70
71            assertTrue(nanos >= 0);
72            long nanosAsMillis = nanos / 1000000;
73            assertTrue(millis <= nanosAsMillis + MILLIS_ROUND);
74        }
75        catch(InterruptedException ie) {
76            unexpectedException();
77        }
78    }
79
80}
81
82