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