blob: 412ce17b8a06b08b4168fa1edc0b64f574feb25a [file] [log] [blame]
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001/*
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/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9package jsr166;
10
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010011import junit.framework.Test;
12import junit.framework.TestSuite;
Calin Juravle8f0d92b2013-08-01 17:26:00 +010013
14public class SystemTest extends JSR166TestCase {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010015 // android-note: Removed because the CTS runner does a bad job of
16 // retrying tests that have suite() declarations.
17 //
18 // public static void main(String[] args) {
19 // main(suite(), args);
20 // }
21 // public static Test suite() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000022 // return new TestSuite(SystemTest.class);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010023 // }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010024
25 /**
26 * Worst case rounding for millisecs; set for 60 cycle millis clock.
27 * This value might need to be changed on JVMs with coarser
28 * System.currentTimeMillis clocks.
29 */
30 static final long MILLIS_ROUND = 17;
31
32 /**
33 * Nanos between readings of millis is no longer than millis (plus
34 * possible rounding).
35 * This shows only that nano timing not (much) worse than milli.
36 */
37 public void testNanoTime1() throws InterruptedException {
38 long m1 = System.currentTimeMillis();
39 Thread.sleep(1);
40 long n1 = System.nanoTime();
41 Thread.sleep(SHORT_DELAY_MS);
42 long n2 = System.nanoTime();
43 Thread.sleep(1);
44 long m2 = System.currentTimeMillis();
45 long millis = m2 - m1;
46 long nanos = n2 - n1;
47 assertTrue(nanos >= 0);
48 long nanosAsMillis = nanos / 1000000;
49 assertTrue(nanosAsMillis <= millis + MILLIS_ROUND);
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() throws InterruptedException {
58 long n1 = System.nanoTime();
59 Thread.sleep(1);
60 long m1 = System.currentTimeMillis();
61 Thread.sleep(SHORT_DELAY_MS);
62 long m2 = System.currentTimeMillis();
63 Thread.sleep(1);
64 long n2 = System.nanoTime();
65 long millis = m2 - m1;
66 long nanos = n2 - n1;
67
68 assertTrue(nanos >= 0);
69 long nanosAsMillis = nanos / 1000000;
70 assertTrue(millis <= nanosAsMillis + MILLIS_ROUND);
71 }
72
73}