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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
|
11 |
< |
public class SystemTest extends TestCase { |
11 |
> |
public class SystemTest extends JSR166TestCase { |
12 |
|
public static void main(String[] args) { |
13 |
< |
junit.textui.TestRunner.run(suite()); |
13 |
> |
junit.textui.TestRunner.run(suite()); |
14 |
|
} |
15 |
|
|
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(SystemTest.class); |
17 |
> |
return new TestSuite(SystemTest.class); |
18 |
|
} |
19 |
|
|
20 |
+ |
/** |
21 |
+ |
* Nanos between readings of millis is no longer than millis (plus |
22 |
+ |
* one milli to allow for rounding). |
23 |
+ |
* This shows only that nano timing not (much) worse than milli. |
24 |
+ |
*/ |
25 |
|
public void testNanoTime1() { |
26 |
< |
// Nanos between readings of millis must be no longer than millis |
27 |
< |
long m1 = System.currentTimeMillis(); |
28 |
< |
long n1 = System.nanoTime(); |
29 |
< |
|
30 |
< |
// Ensure some computation that is not optimized away. |
31 |
< |
long sum = 0; |
32 |
< |
for (long i = 1; i < 10000; ++i) |
33 |
< |
sum += i; |
34 |
< |
assertTrue(sum != 0); |
35 |
< |
|
36 |
< |
long n2 = System.nanoTime(); |
37 |
< |
|
38 |
< |
for (long i = 1; i < 10000; ++i) |
39 |
< |
sum -= i; |
40 |
< |
assertTrue(sum == 0); |
41 |
< |
|
42 |
< |
long m2 = System.currentTimeMillis(); |
37 |
< |
long millis = m2 - m1; |
38 |
< |
long nanos = n2 - n1; |
39 |
< |
|
40 |
< |
assertTrue(nanos >= 0); |
41 |
< |
assertTrue(nanos <= millis * 1000000); |
26 |
> |
try { |
27 |
> |
long m1 = System.currentTimeMillis(); |
28 |
> |
Thread.sleep(1); |
29 |
> |
long n1 = System.nanoTime(); |
30 |
> |
Thread.sleep(SHORT_DELAY_MS); |
31 |
> |
long n2 = System.nanoTime(); |
32 |
> |
Thread.sleep(1); |
33 |
> |
long m2 = System.currentTimeMillis(); |
34 |
> |
long millis = m2 - m1; |
35 |
> |
long nanos = n2 - n1; |
36 |
> |
|
37 |
> |
assertTrue(nanos >= 0); |
38 |
> |
assertTrue(nanos < (millis+1) * 1000000); |
39 |
> |
} |
40 |
> |
catch(InterruptedException ie) { |
41 |
> |
unexpectedException(); |
42 |
> |
} |
43 |
|
} |
44 |
|
|
45 |
+ |
/** |
46 |
+ |
* Millis between readings of nanos is no longer than nanos |
47 |
+ |
* This shows only that nano timing not (much) worse than milli. |
48 |
+ |
*/ |
49 |
|
public void testNanoTime2() { |
50 |
< |
// Millis between readings of nanos must be no longer than nanos |
51 |
< |
long n1 = System.nanoTime(); |
52 |
< |
long m1 = System.currentTimeMillis(); |
53 |
< |
|
54 |
< |
// Ensure some computation that is not optimized away. |
55 |
< |
long sum = 0; |
56 |
< |
for (long i = 1; i < 10000; ++i) |
57 |
< |
sum += i; |
58 |
< |
assertTrue(sum != 0); |
59 |
< |
|
60 |
< |
long m2 = System.currentTimeMillis(); |
61 |
< |
|
62 |
< |
for (long i = 1; i < 10000; ++i) |
63 |
< |
sum -= i; |
64 |
< |
assertTrue(sum == 0); |
65 |
< |
|
66 |
< |
long n2 = System.nanoTime(); |
62 |
< |
|
63 |
< |
long millis = m2 - m1; |
64 |
< |
long nanos = n2 - n1; |
65 |
< |
|
66 |
< |
assertTrue(nanos >= 0); |
67 |
< |
assertTrue(millis * 1000000 <= nanos); |
50 |
> |
try { |
51 |
> |
long n1 = System.nanoTime(); |
52 |
> |
Thread.sleep(1); |
53 |
> |
long m1 = System.currentTimeMillis(); |
54 |
> |
Thread.sleep(SHORT_DELAY_MS); |
55 |
> |
long m2 = System.currentTimeMillis(); |
56 |
> |
Thread.sleep(1); |
57 |
> |
long n2 = System.nanoTime(); |
58 |
> |
long millis = m2 - m1; |
59 |
> |
long nanos = n2 - n1; |
60 |
> |
|
61 |
> |
assertTrue(nanos >= 0); |
62 |
> |
assertTrue(millis * 1000000 <= nanos); |
63 |
> |
} |
64 |
> |
catch(InterruptedException ie) { |
65 |
> |
unexpectedException(); |
66 |
> |
} |
67 |
|
} |
68 |
|
|
69 |
|
} |