ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
Revision: 1.5
Committed: Thu Sep 25 11:02:42 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.4: +18 -31 lines
Log Message:
improve tck javadocs; rename and add a few tests

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 dl 1.5 * convert correctly converts sample values across the four units
24 dl 1.4 */
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 dl 1.5 * toNanos correctly converts sample values in different units to
80     * nanoseconds
81 dl 1.4 */
82 dl 1.1 public void testToNanos() {
83     for (long t = 0; t < 10; ++t) {
84     assertEquals(1000000000 * t,
85     TimeUnit.SECONDS.toNanos(t));
86    
87     assertEquals(1000000 * t,
88     TimeUnit.MILLISECONDS.toNanos(t));
89     assertEquals(1000 * t,
90     TimeUnit.MICROSECONDS.toNanos(t));
91     assertEquals(t,
92 dl 1.3 TimeUnit.NANOSECONDS.toNanos(t));
93     }
94     }
95    
96 dl 1.4 /**
97 dl 1.5 * toMicros correctly converts sample values in different units to
98     * microseconds
99 dl 1.4 */
100 dl 1.3 public void testToMicros() {
101     for (long t = 0; t < 10; ++t) {
102     assertEquals(1000000 * t,
103     TimeUnit.SECONDS.toMicros(t));
104    
105     assertEquals(1000 * t,
106     TimeUnit.MILLISECONDS.toMicros(t));
107     assertEquals(t,
108     TimeUnit.MICROSECONDS.toMicros(t));
109     assertEquals(t,
110     TimeUnit.NANOSECONDS.toMicros(t * 1000));
111     }
112     }
113    
114 dl 1.4 /**
115 dl 1.5 * toMillis correctly converts sample values in different units to
116     * milliseconds
117 dl 1.4 */
118 dl 1.3 public void testToMillis() {
119     for (long t = 0; t < 10; ++t) {
120     assertEquals(1000 * t,
121     TimeUnit.SECONDS.toMillis(t));
122    
123     assertEquals(t,
124     TimeUnit.MILLISECONDS.toMillis(t));
125     assertEquals(t,
126     TimeUnit.MICROSECONDS.toMillis(t * 1000));
127     assertEquals(t,
128     TimeUnit.NANOSECONDS.toMillis(t * 1000000));
129     }
130     }
131    
132 dl 1.4 /**
133 dl 1.5 * toSeconds correctly converts sample values in different units to
134     * seconds
135 dl 1.4 */
136 dl 1.3 public void testToSeconds() {
137     for (long t = 0; t < 10; ++t) {
138     assertEquals(t,
139     TimeUnit.SECONDS.toSeconds(t));
140    
141     assertEquals(t,
142     TimeUnit.MILLISECONDS.toSeconds(t * 1000));
143     assertEquals(t,
144     TimeUnit.MICROSECONDS.toSeconds(t * 1000000));
145     assertEquals(t,
146     TimeUnit.NANOSECONDS.toSeconds(t * 1000000000));
147 dl 1.1 }
148     }
149    
150    
151 dl 1.4 /**
152 dl 1.5 * convert saturates positive too-large values to Long.MAX_VALUE
153     * and negative to LONG.MIN_VALUE
154 dl 1.4 */
155 dl 1.1 public void testConvertSaturate() {
156     assertEquals(Long.MAX_VALUE,
157     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
158     TimeUnit.SECONDS));
159     assertEquals(Long.MIN_VALUE,
160     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
161     TimeUnit.SECONDS));
162     }
163    
164 dl 1.4 /**
165 dl 1.5 * toNanos saturates positive too-large values to Long.MAX_VALUE
166     * and negative to LONG.MIN_VALUE
167 dl 1.4 */
168 dl 1.1 public void testToNanosSaturate() {
169     assertEquals(Long.MAX_VALUE,
170     TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
171     assertEquals(Long.MIN_VALUE,
172     TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
173     }
174    
175    
176 dl 1.4 /**
177 dl 1.5 * toString returns string containing commn name of unit
178 dl 1.4 */
179 dl 1.1 public void testToString() {
180     String s = TimeUnit.SECONDS.toString();
181     assertTrue(s.indexOf("econd") >= 0);
182     }
183    
184    
185     /**
186 dl 1.3 * Timed wait without holding lock throws
187     * IllegalMonitorStateException
188 dl 1.1 */
189 dl 1.3 public void testTimedWait_IllegalMonitorException() {
190 dl 1.1 //created a new thread with anonymous runnable
191    
192     Thread t = new Thread(new Runnable() {
193     public void run() {
194     Object o = new Object();
195     TimeUnit tu = TimeUnit.MILLISECONDS;
196     try {
197 dl 1.3 tu.timedWait(o,LONG_DELAY_MS);
198 dl 1.4 threadShouldThrow();
199 dl 1.1 }
200     catch (InterruptedException ie) {
201 dl 1.4 threadUnexpectedException();
202 dl 1.1 }
203     catch(IllegalMonitorStateException success) {
204     }
205    
206     }
207     });
208     t.start();
209     try {
210 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
211 dl 1.1 t.interrupt();
212     t.join();
213     } catch(Exception e) {
214 dl 1.4 unexpectedException();
215 dl 1.1 }
216     }
217    
218     /**
219 dl 1.5 * timedWait throws InterruptedException when interrupted
220 dl 1.4 */
221 dl 1.1 public void testTimedWait() {
222     Thread t = new Thread(new Runnable() {
223     public void run() {
224     Object o = new Object();
225    
226     TimeUnit tu = TimeUnit.MILLISECONDS;
227     try {
228     synchronized(o) {
229 dl 1.3 tu.timedWait(o,MEDIUM_DELAY_MS);
230 dl 1.1 }
231 dl 1.4 threadShouldThrow();
232 dl 1.1 }
233     catch(InterruptedException success) {}
234     catch(IllegalMonitorStateException failure) {
235 dl 1.4 threadUnexpectedException();
236 dl 1.1 }
237     }
238     });
239     t.start();
240     try {
241 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
242 dl 1.1 t.interrupt();
243     t.join();
244     } catch(Exception e) {
245 dl 1.4 unexpectedException();
246 dl 1.1 }
247     }
248    
249    
250     /**
251 dl 1.5 * timedJoin throws InterruptedException when interrupted
252 dl 1.4 */
253 dl 1.1 public void testTimedJoin() {
254     Thread t = new Thread(new Runnable() {
255     public void run() {
256     TimeUnit tu = TimeUnit.MILLISECONDS;
257     try {
258     Thread s = new Thread(new Runnable() {
259     public void run() {
260 dl 1.4 try {
261 dl 1.3 Thread.sleep(MEDIUM_DELAY_MS);
262 dl 1.4 } catch(InterruptedException success){}
263 dl 1.1 }
264     });
265     s.start();
266 dl 1.3 tu.timedJoin(s,MEDIUM_DELAY_MS);
267 dl 1.4 threadShouldThrow();
268 dl 1.1 }
269     catch(Exception e) {}
270     }
271     });
272     t.start();
273     try {
274 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
275 dl 1.1 t.interrupt();
276     t.join();
277     } catch(Exception e) {
278 dl 1.4 unexpectedException();
279 dl 1.1 }
280     }
281    
282     /**
283 dl 1.5 * timedSleep throws InterruptedException when interrupted
284 dl 1.4 */
285 dl 1.1 public void testTimedSleep() {
286     //created a new thread with anonymous runnable
287    
288     Thread t = new Thread(new Runnable() {
289     public void run() {
290     TimeUnit tu = TimeUnit.MILLISECONDS;
291     try {
292 dl 1.3 tu.sleep(MEDIUM_DELAY_MS);
293 dl 1.4 threadShouldThrow();
294 dl 1.1 }
295     catch(InterruptedException success) {}
296     }
297     });
298     t.start();
299     try {
300 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
301 dl 1.1 t.interrupt();
302     t.join();
303     } catch(Exception e) {
304 dl 1.4 unexpectedException();
305 dl 1.1 }
306     }
307 dl 1.2
308 dl 1.4 /**
309 dl 1.5 * a deserialized serialized unit is equal
310 dl 1.4 */
311 dl 1.2 public void testSerialization() {
312     TimeUnit q = TimeUnit.MILLISECONDS;
313    
314     try {
315     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
316     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
317     out.writeObject(q);
318     out.close();
319    
320     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
321     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
322     TimeUnit r = (TimeUnit)in.readObject();
323    
324     assertEquals(q.toString(), r.toString());
325     } catch(Exception e){
326     e.printStackTrace();
327 dl 1.4 unexpectedException();
328 dl 1.2 }
329     }
330    
331 dl 1.1 }