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