ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
Revision: 1.7
Committed: Mon Dec 29 19:05:40 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
spellcheck

File Contents

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