[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.14, Sun May 14 03:09:25 2017 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   */   */
8    
9  import junit.framework.*;  import junit.framework.Test;
10    import junit.framework.TestSuite;
11    
12  public class SystemTest extends JSR166TestCase {  public class SystemTest extends JSR166TestCase {
13      public static void main(String[] args) {      public static void main(String[] args) {
14          junit.textui.TestRunner.run(suite());          main(suite(), args);
15      }      }
16    
17      public static Test suite() {      public static Test suite() {
# Line 18  Line 19 
19      }      }
20    
21      /**      /**
22       * Nanos between readings of millis is no longer than millis (plus       * Worst case rounding for millisecs; set for 60 cycle millis clock.
23       * one milli to allow for rounding).       * This value might need to be changed on JVMs with coarser
24       * This shows only that nano timing not (much) worse than milli.       * System.currentTimeMillis clocks.
25       */       */
26      public void testNanoTime1() {      static final long MILLIS_ROUND = 17;
         try {  
             long m1 = System.currentTimeMillis();  
             Thread.sleep(1);  
             long n1 = System.nanoTime();  
             Thread.sleep(SHORT_DELAY_MS);  
             long n2 = System.nanoTime();  
             Thread.sleep(1);  
             long m2 = System.currentTimeMillis();  
             long millis = m2 - m1;  
             long nanos = n2 - n1;  
   
             assertTrue(nanos >= 0);  
             assertTrue(nanos < (millis+1) * 1000000);  
         }  
         catch(InterruptedException ie) {  
             unexpectedException();  
         }  
     }  
27    
28      /**      /**
29       * Millis between readings of nanos is no longer than nanos       * Nanos between readings of millis is no longer than millis (plus
30         * possible rounding), and vice versa.
31       * This shows only that nano timing not (much) worse than milli.       * This shows only that nano timing not (much) worse than milli.
32       */       */
33      public void testNanoTime2() {      public void testNanoTime() throws InterruptedException {
34          try {          long m0 = System.currentTimeMillis();
35              long n1 = System.nanoTime();          long n0 = System.nanoTime();
36              Thread.sleep(1);              Thread.sleep(1);
37              long m1 = System.currentTimeMillis();              long m1 = System.currentTimeMillis();
38              Thread.sleep(SHORT_DELAY_MS);          long n1 = System.nanoTime();
39            Thread.sleep(50);       // avoid possibly scaled SHORT_DELAY_MS
40              long m2 = System.currentTimeMillis();              long m2 = System.currentTimeMillis();
             Thread.sleep(1);  
41              long n2 = System.nanoTime();              long n2 = System.nanoTime();
42              long millis = m2 - m1;          Thread.sleep(1);
43              long nanos = n2 - n1;          long m3 = System.currentTimeMillis();
44            long n3 = System.nanoTime();
45              assertTrue(nanos >= 0);          assertTrue((n2 - n1) / 1_000_000 <= m3 - m0 + MILLIS_ROUND);
46              assertTrue(millis * 1000000 <= nanos);          assertTrue(m2 - m1 <= (n3 - n0) / 1_000_000 + MILLIS_ROUND);
         }  
         catch(InterruptedException ie) {  
             unexpectedException();  
47          }          }
48      }      }
   
 }  
   

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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8