ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:56 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8    
9     import junit.framework.*;
10     import java.util.concurrent.*;
11    
12     public class TimeUnitTest extends TestCase {
13     static public boolean DEBUG = false;
14    
15    
16     public static void main(String[] args) {
17     junit.textui.TestRunner.run(suite());
18     }
19    
20     public static Test suite() {
21     return new TestSuite(TimeUnitTest.class);
22     }
23    
24     public void testConvert() {
25     for (long t = 0; t < 10; ++t) {
26     assertEquals(t,
27     TimeUnit.SECONDS.convert(t,
28     TimeUnit.SECONDS));
29     assertEquals(t,
30     TimeUnit.SECONDS.convert(1000 * t,
31     TimeUnit.MILLISECONDS));
32     assertEquals(t,
33     TimeUnit.SECONDS.convert(1000000 * t,
34     TimeUnit.MICROSECONDS));
35     assertEquals(t,
36     TimeUnit.SECONDS.convert(1000000000 * t,
37     TimeUnit.NANOSECONDS));
38     assertEquals(1000 * t,
39     TimeUnit.MILLISECONDS.convert(t,
40     TimeUnit.SECONDS));
41     assertEquals(t,
42     TimeUnit.MILLISECONDS.convert(t,
43     TimeUnit.MILLISECONDS));
44     assertEquals(t,
45     TimeUnit.MILLISECONDS.convert(1000 * t,
46     TimeUnit.MICROSECONDS));
47     assertEquals(t,
48     TimeUnit.MILLISECONDS.convert(1000000 * t,
49     TimeUnit.NANOSECONDS));
50     assertEquals(1000000 * t,
51     TimeUnit.MICROSECONDS.convert(t,
52     TimeUnit.SECONDS));
53     assertEquals(1000 * t,
54     TimeUnit.MICROSECONDS.convert(t,
55     TimeUnit.MILLISECONDS));
56     assertEquals(t,
57     TimeUnit.MICROSECONDS.convert(t,
58     TimeUnit.MICROSECONDS));
59     assertEquals(t,
60     TimeUnit.MICROSECONDS.convert(1000 * t,
61     TimeUnit.NANOSECONDS));
62     assertEquals(1000000000 * t,
63     TimeUnit.NANOSECONDS.convert(t,
64     TimeUnit.SECONDS));
65     assertEquals(1000000 * t,
66     TimeUnit.NANOSECONDS.convert(t,
67     TimeUnit.MILLISECONDS));
68     assertEquals(1000 * t,
69     TimeUnit.NANOSECONDS.convert(t,
70     TimeUnit.MICROSECONDS));
71     assertEquals(t,
72     TimeUnit.NANOSECONDS.convert(t,
73     TimeUnit.NANOSECONDS));
74     }
75     }
76    
77     public void testToNanos() {
78     for (long t = 0; t < 10; ++t) {
79     assertEquals(1000000000 * t,
80     TimeUnit.SECONDS.toNanos(t));
81    
82     assertEquals(1000000 * t,
83     TimeUnit.MILLISECONDS.toNanos(t));
84     assertEquals(1000 * t,
85     TimeUnit.MICROSECONDS.toNanos(t));
86     assertEquals(t,
87     TimeUnit.SECONDS.NANOSECONDS.toNanos(t));
88     }
89     }
90    
91    
92     public void testConvertSaturate() {
93     assertEquals(Long.MAX_VALUE,
94     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
95     TimeUnit.SECONDS));
96     assertEquals(Long.MIN_VALUE,
97     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
98     TimeUnit.SECONDS));
99    
100     }
101    
102     public void testToNanosSaturate() {
103     assertEquals(Long.MAX_VALUE,
104     TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
105     assertEquals(Long.MIN_VALUE,
106     TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
107    
108     }
109    
110    
111     public void testToString() {
112     String s = TimeUnit.SECONDS.toString();
113     assertTrue(s.indexOf("econd") >= 0);
114     }
115    
116     // Exception tests
117    
118     /**
119     * This test specifically to catch the unreported exception
120     */
121     public void testTimedWaitForUnreportedIllegalMonitorException() {
122     //created a new thread with anonymous runnable
123    
124     Thread t = new Thread(new Runnable() {
125     public void run() {
126     Object o = new Object();
127     TimeUnit tu = TimeUnit.MILLISECONDS;
128     try {
129     tu.timedWait(o,40000);
130     fail("should throw");
131     }
132     catch (InterruptedException ie) {
133     fail("should not throw IE here");
134     }
135     catch(IllegalMonitorStateException success) {
136     }
137    
138     }
139     });
140     t.start();
141     try {
142     Thread.sleep(100);
143     t.interrupt();
144     t.join();
145     } catch(Exception e) {
146     fail("Unexpected exception");
147     }
148     }
149    
150     /**
151     * Test to verify that timedWait will throw InterruptedException.
152     * Thread t waits on timedWait while the main thread interrupts it.
153     * Note: This does not throw IllegalMonitorException since timeWait
154     * is synchronized on o
155     */
156     public void testTimedWait() {
157     Thread t = new Thread(new Runnable() {
158     public void run() {
159     Object o = new Object();
160    
161     TimeUnit tu = TimeUnit.MILLISECONDS;
162     try {
163     synchronized(o) {
164     tu.timedWait(o,1000);
165     }
166     fail("should throw");
167     }
168     catch(InterruptedException success) {}
169     catch(IllegalMonitorStateException failure) {
170     fail("should not throw");
171     }
172     }
173     });
174     t.start();
175     try {
176     Thread.sleep(100);
177     t.interrupt();
178     t.join();
179     } catch(Exception e) {
180     fail("Unexpected exception");
181     }
182     }
183    
184    
185     /**
186     * Test to verify that timedJoin will throw InterruptedException.
187     * Thread t waits on timedJoin while the main thread interrupts it.
188     */
189     public void testTimedJoin() {
190     Thread t = new Thread(new Runnable() {
191     public void run() {
192     TimeUnit tu = TimeUnit.MILLISECONDS;
193     try {
194     Thread s = new Thread(new Runnable() {
195     public void run() {
196     try{
197     Thread.sleep(1000);
198     }catch(InterruptedException success){}
199     }
200     });
201     s.start();
202     tu.timedJoin(s,1000);
203     fail("should throw");
204     }
205     catch(Exception e) {}
206     }
207     });
208     t.start();
209     try {
210     Thread.sleep(100);
211     t.interrupt();
212     t.join();
213     } catch(Exception e) {
214     fail("Unexpected exception");
215     }
216     }
217    
218     /**
219     * Test to verify that timedSleep will throw InterruptedException.
220     * Thread t waits on timedSleep while the main thread interrupts it.
221     */
222     public void testTimedSleep() {
223     //created a new thread with anonymous runnable
224    
225     Thread t = new Thread(new Runnable() {
226     public void run() {
227     TimeUnit tu = TimeUnit.MILLISECONDS;
228     try {
229     tu.sleep(1000);
230     fail("should throw");
231     }
232     catch(InterruptedException success) {}
233     }
234     });
235     t.start();
236     try {
237     Thread.sleep(100);
238     t.interrupt();
239     t.join();
240     } catch(Exception e) {
241     fail("Unexpected exception");
242     }
243     }
244     }