ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SystemTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SystemTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.7 by jsr166, Mon Nov 2 20:28:32 2009 UTC

# Line 1 | Line 1
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 <    
15 >
16      public static Test suite() {
17 <        return new TestSuite(SystemTest.class);
17 >        return new TestSuite(SystemTest.class);
18      }
19  
20 +    /**
21 +     * Worst case rounding for millisecs; set for 60 cycle millis clock.
22 +     * This value might need to be changed os JVMs with coarser
23 +     *  System.currentTimeMillis clocks.
24 +     */
25 +    static final long MILLIS_ROUND = 17;
26 +
27 +    /**
28 +     * Nanos between readings of millis is no longer than millis (plus
29 +     * possible rounding).
30 +     * This shows only that nano timing not (much) worse than milli.
31 +     */
32      public void testNanoTime1() {
33 <        // Nanos between readings of millis must be no longer than millis
34 <        long m1 = System.currentTimeMillis();
35 <        long n1 = System.nanoTime();
36 <
37 <        // Ensure some computation that is not optimized away.
38 <        long sum = 0;
39 <        for (long i = 1; i < 10000; ++i)
40 <            sum += i;
41 <        assertTrue(sum != 0);
42 <
43 <        long n2 = System.nanoTime();
44 <
45 <        for (long i = 1; i < 10000; ++i)
46 <            sum -= i;
47 <        assertTrue(sum == 0);
48 <
49 <        long m2 = System.currentTimeMillis();
37 <        long millis = m2 - m1;
38 <        long nanos = n2 - n1;
39 <
40 <        assertTrue(nanos >= 0);
41 <        assertTrue(nanos <= millis * 1000000);
33 >        try {
34 >            long m1 = System.currentTimeMillis();
35 >            Thread.sleep(1);
36 >            long n1 = System.nanoTime();
37 >            Thread.sleep(SHORT_DELAY_MS);
38 >            long n2 = System.nanoTime();
39 >            Thread.sleep(1);
40 >            long m2 = System.currentTimeMillis();
41 >            long millis = m2 - m1;
42 >            long nanos = n2 - n1;
43 >            assertTrue(nanos >= 0);
44 >            long nanosAsMillis = nanos / 1000000;
45 >            assertTrue(nanosAsMillis <= millis + MILLIS_ROUND);
46 >        }
47 >        catch(InterruptedException ie) {
48 >            unexpectedException();
49 >        }
50      }
51  
52 +    /**
53 +     * Millis between readings of nanos is less than nanos, adjusting
54 +     * for rounding.
55 +     * This shows only that nano timing not (much) worse than milli.
56 +     */
57      public void testNanoTime2() {
58 <        // Millis between readings of nanos must be no longer than nanos
59 <        long n1 = System.nanoTime();
60 <        long m1 = System.currentTimeMillis();
61 <
62 <        // Ensure some computation that is not optimized away.
63 <        long sum = 0;
64 <        for (long i = 1; i < 10000; ++i)
65 <            sum += i;
66 <        assertTrue(sum != 0);
67 <
68 <        long m2 = System.currentTimeMillis();
69 <
70 <        for (long i = 1; i < 10000; ++i)
71 <            sum -= i;
72 <        assertTrue(sum == 0);
73 <
74 <        long n2 = System.nanoTime();
75 <
63 <        long millis = m2 - m1;
64 <        long nanos = n2 - n1;
65 <
66 <        assertTrue(nanos >= 0);
67 <        assertTrue(millis * 1000000 <= nanos);
58 >        try {
59 >            long n1 = System.nanoTime();
60 >            Thread.sleep(1);
61 >            long m1 = System.currentTimeMillis();
62 >            Thread.sleep(SHORT_DELAY_MS);
63 >            long m2 = System.currentTimeMillis();
64 >            Thread.sleep(1);
65 >            long n2 = System.nanoTime();
66 >            long millis = m2 - m1;
67 >            long nanos = n2 - n1;
68 >
69 >            assertTrue(nanos >= 0);
70 >            long nanosAsMillis = nanos / 1000000;
71 >            assertTrue(millis <= nanosAsMillis + MILLIS_ROUND);
72 >        }
73 >        catch(InterruptedException ie) {
74 >            unexpectedException();
75 >        }
76      }
77  
78   }
71

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines