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.4 by tim, Mon Dec 8 20:01:50 2003 UTC

# Line 7 | Line 7
7  
8   import junit.framework.*;
9  
10 < public class SystemTest extends TestCase {
10 > public class SystemTest extends JSR166TestCase {
11      public static void main(String[] args) {
12 <        junit.textui.TestRunner.run(suite());  
12 >        junit.textui.TestRunner.run(suite());  
13      }
14      
15      public static Test suite() {
16 <        return new TestSuite(SystemTest.class);
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 <        // Nanos between readings of millis must be no longer than millis
26 <        long m1 = System.currentTimeMillis();
27 <        long n1 = System.nanoTime();
28 <
29 <        // Ensure some computation that is not optimized away.
30 <        long sum = 0;
31 <        for (long i = 1; i < 10000; ++i)
32 <            sum += i;
33 <        assertTrue(sum != 0);
34 <
35 <        long n2 = System.nanoTime();
36 <
37 <        for (long i = 1; i < 10000; ++i)
38 <            sum -= i;
39 <        assertTrue(sum == 0);
40 <
41 <        long m2 = System.currentTimeMillis();
37 <        long millis = m2 - m1;
38 <        long nanos = n2 - n1;
39 <
40 <        assertTrue(nanos >= 0);
41 <        assertTrue(nanos <= millis * 1000000);
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 <        // Millis between readings of nanos must be no longer than nanos
50 <        long n1 = System.nanoTime();
51 <        long m1 = System.currentTimeMillis();
52 <
53 <        // Ensure some computation that is not optimized away.
54 <        long sum = 0;
55 <        for (long i = 1; i < 10000; ++i)
56 <            sum += i;
57 <        assertTrue(sum != 0);
58 <
59 <        long m2 = System.currentTimeMillis();
60 <
61 <        for (long i = 1; i < 10000; ++i)
62 <            sum -= i;
63 <        assertTrue(sum == 0);
64 <
65 <        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);
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines