ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
Revision: 1.6
Committed: Sat Dec 27 19:26:44 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.5: +5 -4 lines
Log Message:
Headers reference Creative Commons

File Contents

# Content
1 /*
2 * 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 */
8
9
10 import junit.framework.*;
11 import java.util.concurrent.*;
12 import java.io.*;
13
14 public class TimeUnitTest extends JSR166TestCase {
15 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 /**
24 * convert correctly converts sample values across the four units
25 */
26 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 /**
80 * toNanos correctly converts sample values in different units to
81 * nanoseconds
82 */
83 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 TimeUnit.NANOSECONDS.toNanos(t));
94 }
95 }
96
97 /**
98 * toMicros correctly converts sample values in different units to
99 * microseconds
100 */
101 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 /**
116 * toMillis correctly converts sample values in different units to
117 * milliseconds
118 */
119 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 /**
134 * toSeconds correctly converts sample values in different units to
135 * seconds
136 */
137 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 }
149 }
150
151
152 /**
153 * convert saturates positive too-large values to Long.MAX_VALUE
154 * and negative to LONG.MIN_VALUE
155 */
156 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 /**
166 * toNanos saturates positive too-large values to Long.MAX_VALUE
167 * and negative to LONG.MIN_VALUE
168 */
169 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 /**
178 * toString returns string containing commn name of unit
179 */
180 public void testToString() {
181 String s = TimeUnit.SECONDS.toString();
182 assertTrue(s.indexOf("econd") >= 0);
183 }
184
185
186 /**
187 * Timed wait without holding lock throws
188 * IllegalMonitorStateException
189 */
190 public void testTimedWait_IllegalMonitorException() {
191 //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 tu.timedWait(o,LONG_DELAY_MS);
199 threadShouldThrow();
200 }
201 catch (InterruptedException ie) {
202 threadUnexpectedException();
203 }
204 catch(IllegalMonitorStateException success) {
205 }
206
207 }
208 });
209 t.start();
210 try {
211 Thread.sleep(SHORT_DELAY_MS);
212 t.interrupt();
213 t.join();
214 } catch(Exception e) {
215 unexpectedException();
216 }
217 }
218
219 /**
220 * timedWait throws InterruptedException when interrupted
221 */
222 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 tu.timedWait(o,MEDIUM_DELAY_MS);
231 }
232 threadShouldThrow();
233 }
234 catch(InterruptedException success) {}
235 catch(IllegalMonitorStateException failure) {
236 threadUnexpectedException();
237 }
238 }
239 });
240 t.start();
241 try {
242 Thread.sleep(SHORT_DELAY_MS);
243 t.interrupt();
244 t.join();
245 } catch(Exception e) {
246 unexpectedException();
247 }
248 }
249
250
251 /**
252 * timedJoin throws InterruptedException when interrupted
253 */
254 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 try {
262 Thread.sleep(MEDIUM_DELAY_MS);
263 } catch(InterruptedException success){}
264 }
265 });
266 s.start();
267 tu.timedJoin(s,MEDIUM_DELAY_MS);
268 threadShouldThrow();
269 }
270 catch(Exception e) {}
271 }
272 });
273 t.start();
274 try {
275 Thread.sleep(SHORT_DELAY_MS);
276 t.interrupt();
277 t.join();
278 } catch(Exception e) {
279 unexpectedException();
280 }
281 }
282
283 /**
284 * timedSleep throws InterruptedException when interrupted
285 */
286 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 tu.sleep(MEDIUM_DELAY_MS);
294 threadShouldThrow();
295 }
296 catch(InterruptedException success) {}
297 }
298 });
299 t.start();
300 try {
301 Thread.sleep(SHORT_DELAY_MS);
302 t.interrupt();
303 t.join();
304 } catch(Exception e) {
305 unexpectedException();
306 }
307 }
308
309 /**
310 * a deserialized serialized unit is equal
311 */
312 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 unexpectedException();
329 }
330 }
331
332 }