ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +52 -13 lines
Log Message:
Documentation scaffolding

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 dl 1.2 import java.io.*;
12 dl 1.1
13 dl 1.3 public class TimeUnitTest extends JSR166TestCase {
14 dl 1.1 public static void main(String[] args) {
15     junit.textui.TestRunner.run(suite());
16     }
17    
18     public static Test suite() {
19     return new TestSuite(TimeUnitTest.class);
20     }
21    
22 dl 1.4 /**
23     *
24     */
25 dl 1.1 public void testConvert() {
26     for (long t = 0; t < 10; ++t) {
27     assertEquals(t,
28     TimeUnit.SECONDS.convert(t,
29     TimeUnit.SECONDS));
30     assertEquals(t,
31     TimeUnit.SECONDS.convert(1000 * t,
32     TimeUnit.MILLISECONDS));
33     assertEquals(t,
34     TimeUnit.SECONDS.convert(1000000 * t,
35     TimeUnit.MICROSECONDS));
36     assertEquals(t,
37     TimeUnit.SECONDS.convert(1000000000 * t,
38     TimeUnit.NANOSECONDS));
39     assertEquals(1000 * t,
40     TimeUnit.MILLISECONDS.convert(t,
41     TimeUnit.SECONDS));
42     assertEquals(t,
43     TimeUnit.MILLISECONDS.convert(t,
44     TimeUnit.MILLISECONDS));
45     assertEquals(t,
46     TimeUnit.MILLISECONDS.convert(1000 * t,
47     TimeUnit.MICROSECONDS));
48     assertEquals(t,
49     TimeUnit.MILLISECONDS.convert(1000000 * t,
50     TimeUnit.NANOSECONDS));
51     assertEquals(1000000 * t,
52     TimeUnit.MICROSECONDS.convert(t,
53     TimeUnit.SECONDS));
54     assertEquals(1000 * t,
55     TimeUnit.MICROSECONDS.convert(t,
56     TimeUnit.MILLISECONDS));
57     assertEquals(t,
58     TimeUnit.MICROSECONDS.convert(t,
59     TimeUnit.MICROSECONDS));
60     assertEquals(t,
61     TimeUnit.MICROSECONDS.convert(1000 * t,
62     TimeUnit.NANOSECONDS));
63     assertEquals(1000000000 * t,
64     TimeUnit.NANOSECONDS.convert(t,
65     TimeUnit.SECONDS));
66     assertEquals(1000000 * t,
67     TimeUnit.NANOSECONDS.convert(t,
68     TimeUnit.MILLISECONDS));
69     assertEquals(1000 * t,
70     TimeUnit.NANOSECONDS.convert(t,
71     TimeUnit.MICROSECONDS));
72     assertEquals(t,
73     TimeUnit.NANOSECONDS.convert(t,
74     TimeUnit.NANOSECONDS));
75     }
76     }
77    
78 dl 1.4 /**
79     *
80     */
81 dl 1.1 public void testToNanos() {
82     for (long t = 0; t < 10; ++t) {
83     assertEquals(1000000000 * t,
84     TimeUnit.SECONDS.toNanos(t));
85    
86     assertEquals(1000000 * t,
87     TimeUnit.MILLISECONDS.toNanos(t));
88     assertEquals(1000 * t,
89     TimeUnit.MICROSECONDS.toNanos(t));
90     assertEquals(t,
91 dl 1.3 TimeUnit.NANOSECONDS.toNanos(t));
92     }
93     }
94    
95 dl 1.4 /**
96     *
97     */
98 dl 1.3 public void testToMicros() {
99     for (long t = 0; t < 10; ++t) {
100     assertEquals(1000000 * t,
101     TimeUnit.SECONDS.toMicros(t));
102    
103     assertEquals(1000 * t,
104     TimeUnit.MILLISECONDS.toMicros(t));
105     assertEquals(t,
106     TimeUnit.MICROSECONDS.toMicros(t));
107     assertEquals(t,
108     TimeUnit.NANOSECONDS.toMicros(t * 1000));
109     }
110     }
111    
112 dl 1.4 /**
113     *
114     */
115 dl 1.3 public void testToMillis() {
116     for (long t = 0; t < 10; ++t) {
117     assertEquals(1000 * t,
118     TimeUnit.SECONDS.toMillis(t));
119    
120     assertEquals(t,
121     TimeUnit.MILLISECONDS.toMillis(t));
122     assertEquals(t,
123     TimeUnit.MICROSECONDS.toMillis(t * 1000));
124     assertEquals(t,
125     TimeUnit.NANOSECONDS.toMillis(t * 1000000));
126     }
127     }
128    
129 dl 1.4 /**
130     *
131     */
132 dl 1.3 public void testToSeconds() {
133     for (long t = 0; t < 10; ++t) {
134     assertEquals(t,
135     TimeUnit.SECONDS.toSeconds(t));
136    
137     assertEquals(t,
138     TimeUnit.MILLISECONDS.toSeconds(t * 1000));
139     assertEquals(t,
140     TimeUnit.MICROSECONDS.toSeconds(t * 1000000));
141     assertEquals(t,
142     TimeUnit.NANOSECONDS.toSeconds(t * 1000000000));
143 dl 1.1 }
144     }
145    
146    
147 dl 1.4 /**
148     *
149     */
150 dl 1.1 public void testConvertSaturate() {
151     assertEquals(Long.MAX_VALUE,
152     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
153     TimeUnit.SECONDS));
154     assertEquals(Long.MIN_VALUE,
155     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
156     TimeUnit.SECONDS));
157    
158     }
159    
160 dl 1.4 /**
161     *
162     */
163 dl 1.1 public void testToNanosSaturate() {
164     assertEquals(Long.MAX_VALUE,
165     TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
166     assertEquals(Long.MIN_VALUE,
167     TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
168    
169     }
170    
171    
172 dl 1.4 /**
173     *
174     */
175 dl 1.1 public void testToString() {
176     String s = TimeUnit.SECONDS.toString();
177     assertTrue(s.indexOf("econd") >= 0);
178     }
179    
180    
181     /**
182 dl 1.3 * Timed wait without holding lock throws
183     * IllegalMonitorStateException
184 dl 1.1 */
185 dl 1.4 /**
186     *
187     */
188 dl 1.3 public void testTimedWait_IllegalMonitorException() {
189 dl 1.1 //created a new thread with anonymous runnable
190    
191     Thread t = new Thread(new Runnable() {
192     public void run() {
193     Object o = new Object();
194     TimeUnit tu = TimeUnit.MILLISECONDS;
195     try {
196 dl 1.3 tu.timedWait(o,LONG_DELAY_MS);
197 dl 1.4 threadShouldThrow();
198 dl 1.1 }
199     catch (InterruptedException ie) {
200 dl 1.4 threadUnexpectedException();
201 dl 1.1 }
202     catch(IllegalMonitorStateException success) {
203     }
204    
205     }
206     });
207     t.start();
208     try {
209 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
210 dl 1.1 t.interrupt();
211     t.join();
212     } catch(Exception e) {
213 dl 1.4 unexpectedException();
214 dl 1.1 }
215     }
216    
217     /**
218 dl 1.3 * timedWait will throw InterruptedException.
219 dl 1.1 * Thread t waits on timedWait while the main thread interrupts it.
220     * Note: This does not throw IllegalMonitorException since timeWait
221     * is synchronized on o
222     */
223 dl 1.4 /**
224     *
225     */
226 dl 1.1 public void testTimedWait() {
227     Thread t = new Thread(new Runnable() {
228     public void run() {
229     Object o = new Object();
230    
231     TimeUnit tu = TimeUnit.MILLISECONDS;
232     try {
233     synchronized(o) {
234 dl 1.3 tu.timedWait(o,MEDIUM_DELAY_MS);
235 dl 1.1 }
236 dl 1.4 threadShouldThrow();
237 dl 1.1 }
238     catch(InterruptedException success) {}
239     catch(IllegalMonitorStateException failure) {
240 dl 1.4 threadUnexpectedException();
241 dl 1.1 }
242     }
243     });
244     t.start();
245     try {
246 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
247 dl 1.1 t.interrupt();
248     t.join();
249     } catch(Exception e) {
250 dl 1.4 unexpectedException();
251 dl 1.1 }
252     }
253    
254    
255     /**
256 dl 1.3 * timedJoin will throw InterruptedException.
257 dl 1.1 * Thread t waits on timedJoin while the main thread interrupts it.
258     */
259 dl 1.4 /**
260     *
261     */
262 dl 1.1 public void testTimedJoin() {
263     Thread t = new Thread(new Runnable() {
264     public void run() {
265     TimeUnit tu = TimeUnit.MILLISECONDS;
266     try {
267     Thread s = new Thread(new Runnable() {
268     public void run() {
269 dl 1.4 try {
270 dl 1.3 Thread.sleep(MEDIUM_DELAY_MS);
271 dl 1.4 } catch(InterruptedException success){}
272 dl 1.1 }
273     });
274     s.start();
275 dl 1.3 tu.timedJoin(s,MEDIUM_DELAY_MS);
276 dl 1.4 threadShouldThrow();
277 dl 1.1 }
278     catch(Exception e) {}
279     }
280     });
281     t.start();
282     try {
283 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
284 dl 1.1 t.interrupt();
285     t.join();
286     } catch(Exception e) {
287 dl 1.4 unexpectedException();
288 dl 1.1 }
289     }
290    
291     /**
292 dl 1.3 * timedSleep will throw InterruptedException.
293 dl 1.1 * Thread t waits on timedSleep while the main thread interrupts it.
294     */
295 dl 1.4 /**
296     *
297     */
298 dl 1.1 public void testTimedSleep() {
299     //created a new thread with anonymous runnable
300    
301     Thread t = new Thread(new Runnable() {
302     public void run() {
303     TimeUnit tu = TimeUnit.MILLISECONDS;
304     try {
305 dl 1.3 tu.sleep(MEDIUM_DELAY_MS);
306 dl 1.4 threadShouldThrow();
307 dl 1.1 }
308     catch(InterruptedException success) {}
309     }
310     });
311     t.start();
312     try {
313 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
314 dl 1.1 t.interrupt();
315     t.join();
316     } catch(Exception e) {
317 dl 1.4 unexpectedException();
318 dl 1.1 }
319     }
320 dl 1.2
321 dl 1.4 /**
322     *
323     */
324 dl 1.2 public void testSerialization() {
325     TimeUnit q = TimeUnit.MILLISECONDS;
326    
327     try {
328     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
329     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
330     out.writeObject(q);
331     out.close();
332    
333     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
334     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
335     TimeUnit r = (TimeUnit)in.readObject();
336    
337     assertEquals(q.toString(), r.toString());
338     } catch(Exception e){
339     e.printStackTrace();
340 dl 1.4 unexpectedException();
341 dl 1.2 }
342     }
343    
344 dl 1.1 }