author | ohair |
Wed, 01 Apr 2009 09:08:54 -0700 | |
changeset 2402 | 825d0b77ef92 |
parent 2 | 90ce3da70b43 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* @test |
|
25 |
* @bug 5057341 6363898 |
|
26 |
* @summary Basic tests for TimeUnit |
|
27 |
* @author Martin Buchholz |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.util.*; |
|
32 |
import java.util.concurrent.*; |
|
33 |
import static java.util.concurrent.TimeUnit.*; |
|
34 |
||
35 |
public class Basic { |
|
36 |
private static void realMain(String[] args) throws Throwable { |
|
37 |
||
38 |
for (TimeUnit u : TimeUnit.values()) { |
|
39 |
System.out.println(u); |
|
40 |
check(u instanceof TimeUnit); |
|
41 |
equal(42L, u.convert(42, u)); |
|
42 |
for (TimeUnit v : TimeUnit.values()) |
|
43 |
if (u.convert(42, v) >= 42) |
|
44 |
equal(42L, v.convert(u.convert(42, v), u)); |
|
45 |
check(readObject(serializedForm(u)) == u); |
|
46 |
} |
|
47 |
||
48 |
equal( 24L, HOURS.convert (1, DAYS)); |
|
49 |
equal( 60L, MINUTES.convert (1, HOURS)); |
|
50 |
equal( 60L, SECONDS.convert (1, MINUTES)); |
|
51 |
equal(1000L, MILLISECONDS.convert(1, SECONDS)); |
|
52 |
equal(1000L, MICROSECONDS.convert(1, MILLISECONDS)); |
|
53 |
equal(1000L, NANOSECONDS.convert (1, MICROSECONDS)); |
|
54 |
||
55 |
equal( 24L, DAYS.toHours(1)); |
|
56 |
equal( 60L, HOURS.toMinutes(1)); |
|
57 |
equal( 60L, MINUTES.toSeconds(1)); |
|
58 |
equal(1000L, SECONDS.toMillis(1)); |
|
59 |
equal(1000L, MILLISECONDS.toMicros(1)); |
|
60 |
equal(1000L, MICROSECONDS.toNanos(1)); |
|
61 |
||
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 | 64 |
long elapsedMillis = (System.nanoTime() - t0)/(1000L * 1000L); |
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 | 68 |
check(elapsedMillis < 1000); |
69 |
||
70 |
//---------------------------------------------------------------- |
|
71 |
// Tests for serialized form compatibility with previous release |
|
72 |
//---------------------------------------------------------------- |
|
73 |
byte[] serializedForm = /* Generated using tiger */ |
|
74 |
{-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.', |
|
75 |
'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e', |
|
76 |
'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0, |
|
77 |
0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14, |
|
78 |
'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u', |
|
79 |
'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', |
|
80 |
'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', }; |
|
81 |
check(Arrays.equals(serializedForm(SECONDS), serializedForm)); |
|
82 |
} |
|
83 |
||
84 |
//--------------------- Infrastructure --------------------------- |
|
85 |
static volatile int passed = 0, failed = 0; |
|
86 |
static void pass() {passed++;} |
|
87 |
static void fail() {failed++; Thread.dumpStack();} |
|
88 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
89 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
90 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
91 |
static void equal(Object x, Object y) { |
|
92 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
93 |
else fail(x + " not equal to " + y);} |
|
94 |
public static void main(String[] args) throws Throwable { |
|
95 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
96 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
97 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
98 |
static byte[] serializedForm(Object obj) throws IOException { |
|
99 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
100 |
new ObjectOutputStream(baos).writeObject(obj); |
|
101 |
return baos.toByteArray(); |
|
102 |
} |
|
103 |
static Object readObject(byte[] bytes) |
|
104 |
throws IOException, ClassNotFoundException { |
|
105 |
InputStream is = new ByteArrayInputStream(bytes); |
|
106 |
return new ObjectInputStream(is).readObject(); |
|
107 |
} |
|
108 |
} |