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. |
21 |
* This shows only that nano timing not (much) worse than milli. |
22 |
*/ |
23 |
public void testNanoTime1() { |
24 |
try { |
25 |
long m1 = System.currentTimeMillis(); |
26 |
Thread.sleep(1); |
27 |
long n1 = System.nanoTime(); |
28 |
Thread.sleep(SHORT_DELAY_MS); |
29 |
long n2 = System.nanoTime(); |
30 |
Thread.sleep(1); |
31 |
long m2 = System.currentTimeMillis(); |
32 |
long millis = m2 - m1; |
33 |
long nanos = n2 - n1; |
34 |
|
35 |
assertTrue(nanos >= 0); |
36 |
assertTrue(nanos <= millis * 1000000); |
37 |
} |
38 |
catch(InterruptedException ie) { |
39 |
fail("unexpected exception"); |
40 |
} |
41 |
} |
42 |
|
43 |
/** |
44 |
* Millis between readings of nanos is no longer than nanos |
45 |
* This shows only that nano timing not (much) worse than milli. |
46 |
*/ |
47 |
public void testNanoTime2() { |
48 |
try { |
49 |
long n1 = System.nanoTime(); |
50 |
Thread.sleep(1); |
51 |
long m1 = System.currentTimeMillis(); |
52 |
Thread.sleep(SHORT_DELAY_MS); |
53 |
long m2 = System.currentTimeMillis(); |
54 |
Thread.sleep(1); |
55 |
long n2 = System.nanoTime(); |
56 |
long millis = m2 - m1; |
57 |
long nanos = n2 - n1; |
58 |
|
59 |
assertTrue(nanos >= 0); |
60 |
assertTrue(millis * 1000000 <= nanos); |
61 |
} |
62 |
catch(InterruptedException ie) { |
63 |
fail("unexpected exception"); |
64 |
} |
65 |
} |
66 |
|
67 |
} |
68 |
|