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