[cvs] / jsr166 / src / test / tck / SystemTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/SystemTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5, Sat Dec 27 19:26:44 2003 UTC revision 1.11, Tue Mar 15 19:47:07 2011 UTC
# Line 1  Line 1 
1  /*  /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain   * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.   * Pat Fisher, Mike Judd.
7   */   */
# Line 18  Line 18 
18      }      }
19    
20      /**      /**
21         * Worst case rounding for millisecs; set for 60 cycle millis clock.
22         * This value might need to be changed on 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       * Nanos between readings of millis is no longer than millis (plus
29       * one milli to allow for rounding).       * possible rounding).
30       * This shows only that nano timing not (much) worse than milli.       * This shows only that nano timing not (much) worse than milli.
31       */       */
32      public void testNanoTime1() {      public void testNanoTime1() throws InterruptedException {
         try {  
33              long m1 = System.currentTimeMillis();              long m1 = System.currentTimeMillis();
34              Thread.sleep(1);              Thread.sleep(1);
35              long n1 = System.nanoTime();              long n1 = System.nanoTime();
# Line 33  Line 39 
39              long m2 = System.currentTimeMillis();              long m2 = System.currentTimeMillis();
40              long millis = m2 - m1;              long millis = m2 - m1;
41              long nanos = n2 - n1;              long nanos = n2 - n1;
   
42              assertTrue(nanos >= 0);              assertTrue(nanos >= 0);
43              assertTrue(nanos < (millis+1) * 1000000);          long nanosAsMillis = nanos / 1000000;
44          }          assertTrue(nanosAsMillis <= millis + MILLIS_ROUND);
         catch(InterruptedException ie) {  
             unexpectedException();  
         }  
45      }      }
46    
47      /**      /**
48       * Millis between readings of nanos is no longer than nanos       * Millis between readings of nanos is less than nanos, adjusting
49         * for rounding.
50       * This shows only that nano timing not (much) worse than milli.       * This shows only that nano timing not (much) worse than milli.
51       */       */
52      public void testNanoTime2() {      public void testNanoTime2() throws InterruptedException {
         try {  
53              long n1 = System.nanoTime();              long n1 = System.nanoTime();
54              Thread.sleep(1);              Thread.sleep(1);
55              long m1 = System.currentTimeMillis();              long m1 = System.currentTimeMillis();
# Line 59  Line 61 
61              long nanos = n2 - n1;              long nanos = n2 - n1;
62    
63              assertTrue(nanos >= 0);              assertTrue(nanos >= 0);
64              assertTrue(millis * 1000000 <= nanos);          long nanosAsMillis = nanos / 1000000;
65          }          assertTrue(millis <= nanosAsMillis + MILLIS_ROUND);
         catch(InterruptedException ie) {  
             unexpectedException();  
         }  
66      }      }
67    
68  }  }
   

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.11

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8