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

# Content
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 import java.io.*;
12
13 public class TimeUnitTest extends JSR166TestCase {
14 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 /**
23 *
24 */
25 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 /**
79 *
80 */
81 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 TimeUnit.NANOSECONDS.toNanos(t));
92 }
93 }
94
95 /**
96 *
97 */
98 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 /**
113 *
114 */
115 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 /**
130 *
131 */
132 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 }
144 }
145
146
147 /**
148 *
149 */
150 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 /**
161 *
162 */
163 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 /**
173 *
174 */
175 public void testToString() {
176 String s = TimeUnit.SECONDS.toString();
177 assertTrue(s.indexOf("econd") >= 0);
178 }
179
180
181 /**
182 * Timed wait without holding lock throws
183 * IllegalMonitorStateException
184 */
185 /**
186 *
187 */
188 public void testTimedWait_IllegalMonitorException() {
189 //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 tu.timedWait(o,LONG_DELAY_MS);
197 threadShouldThrow();
198 }
199 catch (InterruptedException ie) {
200 threadUnexpectedException();
201 }
202 catch(IllegalMonitorStateException success) {
203 }
204
205 }
206 });
207 t.start();
208 try {
209 Thread.sleep(SHORT_DELAY_MS);
210 t.interrupt();
211 t.join();
212 } catch(Exception e) {
213 unexpectedException();
214 }
215 }
216
217 /**
218 * timedWait will throw InterruptedException.
219 * 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 /**
224 *
225 */
226 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 tu.timedWait(o,MEDIUM_DELAY_MS);
235 }
236 threadShouldThrow();
237 }
238 catch(InterruptedException success) {}
239 catch(IllegalMonitorStateException failure) {
240 threadUnexpectedException();
241 }
242 }
243 });
244 t.start();
245 try {
246 Thread.sleep(SHORT_DELAY_MS);
247 t.interrupt();
248 t.join();
249 } catch(Exception e) {
250 unexpectedException();
251 }
252 }
253
254
255 /**
256 * timedJoin will throw InterruptedException.
257 * Thread t waits on timedJoin while the main thread interrupts it.
258 */
259 /**
260 *
261 */
262 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 try {
270 Thread.sleep(MEDIUM_DELAY_MS);
271 } catch(InterruptedException success){}
272 }
273 });
274 s.start();
275 tu.timedJoin(s,MEDIUM_DELAY_MS);
276 threadShouldThrow();
277 }
278 catch(Exception e) {}
279 }
280 });
281 t.start();
282 try {
283 Thread.sleep(SHORT_DELAY_MS);
284 t.interrupt();
285 t.join();
286 } catch(Exception e) {
287 unexpectedException();
288 }
289 }
290
291 /**
292 * timedSleep will throw InterruptedException.
293 * Thread t waits on timedSleep while the main thread interrupts it.
294 */
295 /**
296 *
297 */
298 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 tu.sleep(MEDIUM_DELAY_MS);
306 threadShouldThrow();
307 }
308 catch(InterruptedException success) {}
309 }
310 });
311 t.start();
312 try {
313 Thread.sleep(SHORT_DELAY_MS);
314 t.interrupt();
315 t.join();
316 } catch(Exception e) {
317 unexpectedException();
318 }
319 }
320
321 /**
322 *
323 */
324 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 unexpectedException();
341 }
342 }
343
344 }