1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
< |
import junit.framework.*; |
9 |
> |
import junit.framework.Test; |
10 |
> |
import junit.framework.TestSuite; |
11 |
|
|
12 |
< |
public class SystemTest extends TestCase { |
12 |
> |
public class SystemTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run(suite()); |
14 |
> |
main(suite(), args); |
15 |
|
} |
16 |
< |
|
16 |
> |
|
17 |
|
public static Test suite() { |
18 |
< |
return new TestSuite(SystemTest.class); |
18 |
> |
return new TestSuite(SystemTest.class); |
19 |
|
} |
20 |
|
|
21 |
< |
public void testNanoTime1() { |
22 |
< |
// Nanos between readings of millis must be no longer than millis |
21 |
> |
/** |
22 |
> |
* Worst case rounding for millisecs; set for 60 cycle millis clock. |
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 |
> |
|
28 |
> |
/** |
29 |
> |
* Nanos between readings of millis is no longer than millis (plus |
30 |
> |
* possible rounding), and vice versa. |
31 |
> |
* This shows only that nano timing not (much) worse than milli. |
32 |
> |
*/ |
33 |
> |
public void testNanoTime() throws InterruptedException { |
34 |
> |
long m0 = System.currentTimeMillis(); |
35 |
> |
long n0 = System.nanoTime(); |
36 |
> |
Thread.sleep(1); |
37 |
|
long m1 = System.currentTimeMillis(); |
38 |
|
long n1 = System.nanoTime(); |
39 |
< |
|
24 |
< |
// Ensure some computation that is not optimized away. |
25 |
< |
long sum = 0; |
26 |
< |
for (long i = 1; i < 10000; ++i) |
27 |
< |
sum += i; |
28 |
< |
assertTrue(sum != 0); |
29 |
< |
|
30 |
< |
long n2 = System.nanoTime(); |
31 |
< |
|
32 |
< |
for (long i = 1; i < 10000; ++i) |
33 |
< |
sum -= i; |
34 |
< |
assertTrue(sum == 0); |
35 |
< |
|
36 |
< |
long m2 = System.currentTimeMillis(); |
37 |
< |
long millis = m2 - m1; |
38 |
< |
long nanos = n2 - n1; |
39 |
< |
|
40 |
< |
assertTrue(nanos >= 0); |
41 |
< |
assertTrue(nanos <= millis * 1000000); |
42 |
< |
} |
43 |
< |
|
44 |
< |
public void testNanoTime2() { |
45 |
< |
// Millis between readings of nanos must be no longer than nanos |
46 |
< |
long n1 = System.nanoTime(); |
47 |
< |
long m1 = System.currentTimeMillis(); |
48 |
< |
|
49 |
< |
// Ensure some computation that is not optimized away. |
50 |
< |
long sum = 0; |
51 |
< |
for (long i = 1; i < 10000; ++i) |
52 |
< |
sum += i; |
53 |
< |
assertTrue(sum != 0); |
54 |
< |
|
39 |
> |
Thread.sleep(50); // avoid possibly scaled SHORT_DELAY_MS |
40 |
|
long m2 = System.currentTimeMillis(); |
56 |
– |
|
57 |
– |
for (long i = 1; i < 10000; ++i) |
58 |
– |
sum -= i; |
59 |
– |
assertTrue(sum == 0); |
60 |
– |
|
41 |
|
long n2 = System.nanoTime(); |
42 |
< |
|
43 |
< |
long millis = m2 - m1; |
44 |
< |
long nanos = n2 - n1; |
45 |
< |
|
46 |
< |
assertTrue(nanos >= 0); |
67 |
< |
assertTrue(millis * 1000000 <= nanos); |
42 |
> |
Thread.sleep(1); |
43 |
> |
long m3 = System.currentTimeMillis(); |
44 |
> |
long n3 = System.nanoTime(); |
45 |
> |
assertTrue((n2 - n1) / 1_000_000 <= m3 - m0 + MILLIS_ROUND); |
46 |
> |
assertTrue(m2 - m1 <= (n3 - n0) / 1_000_000 + MILLIS_ROUND); |
47 |
|
} |
69 |
– |
|
48 |
|
} |
71 |
– |
|