test/jdk/java/util/concurrent/TimeUnit/Basic.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 34347 jdk/test/java/util/concurrent/TimeUnit/Basic.java@4a17f9e90a0f
child 58138 1e4270f875ee
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2402
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2402
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2402
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2402
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 5057341 6363898
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Basic tests for TimeUnit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    30
import static java.util.concurrent.TimeUnit.DAYS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    31
import static java.util.concurrent.TimeUnit.HOURS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    32
import static java.util.concurrent.TimeUnit.MICROSECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    33
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    34
import static java.util.concurrent.TimeUnit.MINUTES;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    35
import static java.util.concurrent.TimeUnit.NANOSECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    36
import static java.util.concurrent.TimeUnit.SECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    37
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    38
import java.io.ByteArrayInputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    39
import java.io.ByteArrayOutputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    40
import java.io.IOException;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    41
import java.io.InputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    42
import java.io.ObjectOutputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    43
import java.io.ObjectInputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    44
import java.util.Arrays;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    45
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (TimeUnit u : TimeUnit.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            System.out.println(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            check(u instanceof TimeUnit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            equal(42L, u.convert(42, u));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            for (TimeUnit v : TimeUnit.values())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                if (u.convert(42, v) >= 42)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    equal(42L, v.convert(u.convert(42, v), u));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            check(readObject(serializedForm(u)) == u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        equal(  24L, HOURS.convert       (1, DAYS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        equal(  60L, MINUTES.convert     (1, HOURS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        equal(  60L, SECONDS.convert     (1, MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        equal(1000L, MILLISECONDS.convert(1, SECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        equal(1000L, MICROSECONDS.convert(1, MILLISECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        equal(1000L, NANOSECONDS.convert (1, MICROSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        equal(  24L, DAYS.toHours(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        equal(  60L, HOURS.toMinutes(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        equal(  60L, MINUTES.toSeconds(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        equal(1000L, SECONDS.toMillis(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        equal(1000L, MILLISECONDS.toMicros(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        equal(1000L, MICROSECONDS.toNanos(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        long t0 = System.nanoTime();
2402
825d0b77ef92 6824583: regtest TimeUnit/Basic.java fails intermittently on Windows - again
ohair
parents: 2
diff changeset
    75
        MILLISECONDS.sleep(3); /* See windows bug 6313903, might not sleep */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        long elapsedMillis = (System.nanoTime() - t0)/(1000L * 1000L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.printf("elapsed=%d%n", elapsedMillis);
2402
825d0b77ef92 6824583: regtest TimeUnit/Basic.java fails intermittently on Windows - again
ohair
parents: 2
diff changeset
    78
        check(elapsedMillis >= 0);
825d0b77ef92 6824583: regtest TimeUnit/Basic.java fails intermittently on Windows - again
ohair
parents: 2
diff changeset
    79
        /* Might not sleep on windows: check(elapsedMillis >= 3); */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        check(elapsedMillis < 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // Tests for serialized form compatibility with previous release
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        byte[] serializedForm = /* Generated using tiger */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
             'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        check(Arrays.equals(serializedForm(SECONDS), serializedForm));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static byte[] serializedForm(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        new ObjectOutputStream(baos).writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static Object readObject(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        InputStream is = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return new ObjectInputStream(is).readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}