jdk/test/java/util/concurrent/TimeUnit/Basic.java
author ohair
Wed, 01 Apr 2009 09:08:54 -0700
changeset 2402 825d0b77ef92
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6824583: regtest TimeUnit/Basic.java fails intermittently on Windows - again Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import static java.util.concurrent.TimeUnit.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        for (TimeUnit u : TimeUnit.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            System.out.println(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            check(u instanceof TimeUnit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            equal(42L, u.convert(42, u));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            for (TimeUnit v : TimeUnit.values())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                if (u.convert(42, v) >= 42)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                    equal(42L, v.convert(u.convert(42, v), u));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            check(readObject(serializedForm(u)) == u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        equal(  24L, HOURS.convert       (1, DAYS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        equal(  60L, MINUTES.convert     (1, HOURS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        equal(  60L, SECONDS.convert     (1, MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        equal(1000L, MILLISECONDS.convert(1, SECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        equal(1000L, MICROSECONDS.convert(1, MILLISECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        equal(1000L, NANOSECONDS.convert (1, MICROSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        equal(  24L, DAYS.toHours(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        equal(  60L, HOURS.toMinutes(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        equal(  60L, MINUTES.toSeconds(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        equal(1000L, SECONDS.toMillis(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        equal(1000L, MILLISECONDS.toMicros(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        equal(1000L, MICROSECONDS.toNanos(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        long t0 = System.nanoTime();
2402
825d0b77ef92 6824583: regtest TimeUnit/Basic.java fails intermittently on Windows - again
ohair
parents: 2
diff changeset
    63
        MILLISECONDS.sleep(3); /* See windows bug 6313903, might not sleep */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        long elapsedMillis = (System.nanoTime() - t0)/(1000L * 1000L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.printf("elapsed=%d%n", elapsedMillis);
2402
825d0b77ef92 6824583: regtest TimeUnit/Basic.java fails intermittently on Windows - again
ohair
parents: 2
diff changeset
    66
        check(elapsedMillis >= 0);
825d0b77ef92 6824583: regtest TimeUnit/Basic.java fails intermittently on Windows - again
ohair
parents: 2
diff changeset
    67
        /* Might not sleep on windows: check(elapsedMillis >= 3); */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        check(elapsedMillis < 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // Tests for serialized form compatibility with previous release
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        byte[] serializedForm = /* Generated using tiger */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
             'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
             'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
             0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
             'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
             'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
             'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        check(Arrays.equals(serializedForm(SECONDS), serializedForm));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static byte[] serializedForm(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        new ObjectOutputStream(baos).writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static Object readObject(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        InputStream is = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return new ObjectInputStream(is).readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
}