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

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