OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / api / java / util / concurrent / SystemTest.java
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.
7  */
8
9 package tests.api.java.util.concurrent; // android-added
10
11 import junit.framework.*;
12
13 public class SystemTest extends JSR166TestCase {
14     public static Test suite() {
15         return new TestSuite(SystemTest.class);
16     }
17
18     /**
19      * Worst case rounding for millisecs; set for 60 cycle millis clock.
20      * This value might need to be changed on JVMs with coarser
21      *  System.currentTimeMillis clocks.
22      */
23     static final long MILLIS_ROUND = 17;
24
25     /**
26      * Nanos between readings of millis is no longer than millis (plus
27      * possible rounding).
28      * This shows only that nano timing not (much) worse than milli.
29      */
30     public void testNanoTime1() throws InterruptedException {
31         long m1 = System.currentTimeMillis();
32         Thread.sleep(1);
33         long n1 = System.nanoTime();
34         Thread.sleep(SHORT_DELAY_MS);
35         long n2 = System.nanoTime();
36         Thread.sleep(1);
37         long m2 = System.currentTimeMillis();
38         long millis = m2 - m1;
39         long nanos = n2 - n1;
40         assertTrue(nanos >= 0);
41         long nanosAsMillis = nanos / 1000000;
42         assertTrue(nanosAsMillis <= millis + MILLIS_ROUND);
43     }
44
45     /**
46      * Millis between readings of nanos is less than nanos, adjusting
47      * for rounding.
48      * This shows only that nano timing not (much) worse than milli.
49      */
50     public void testNanoTime2() throws InterruptedException {
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         long nanosAsMillis = nanos / 1000000;
63         assertTrue(millis <= nanosAsMillis + MILLIS_ROUND);
64     }
65
66 }