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, |
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, |
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, |
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, |
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, |
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, |
159 |
|
assertEquals(Long.MIN_VALUE, |
160 |
|
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
161 |
|
TimeUnit.SECONDS)); |
139 |
– |
|
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)); |
147 |
– |
|
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); |
195 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
196 |
|
try { |
197 |
|
tu.timedWait(o,LONG_DELAY_MS); |
198 |
< |
fail("should throw"); |
198 |
> |
threadShouldThrow(); |
199 |
|
} |
200 |
|
catch (InterruptedException ie) { |
201 |
< |
fail("should not throw IE here"); |
201 |
> |
threadUnexpectedException(); |
202 |
|
} |
203 |
|
catch(IllegalMonitorStateException success) { |
204 |
|
} |
211 |
|
t.interrupt(); |
212 |
|
t.join(); |
213 |
|
} catch(Exception e) { |
214 |
< |
fail("Unexpected exception"); |
214 |
> |
unexpectedException(); |
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* timedWait will throw InterruptedException. |
192 |
< |
* Thread t waits on timedWait while the main thread interrupts it. |
193 |
< |
* Note: This does not throw IllegalMonitorException since timeWait |
194 |
< |
* is synchronized on o |
219 |
> |
* timedWait throws InterruptedException when interrupted |
220 |
|
*/ |
221 |
|
public void testTimedWait() { |
222 |
|
Thread t = new Thread(new Runnable() { |
228 |
|
synchronized(o) { |
229 |
|
tu.timedWait(o,MEDIUM_DELAY_MS); |
230 |
|
} |
231 |
< |
fail("should throw"); |
231 |
> |
threadShouldThrow(); |
232 |
|
} |
233 |
|
catch(InterruptedException success) {} |
234 |
|
catch(IllegalMonitorStateException failure) { |
235 |
< |
fail("should not throw"); |
235 |
> |
threadUnexpectedException(); |
236 |
|
} |
237 |
|
} |
238 |
|
}); |
242 |
|
t.interrupt(); |
243 |
|
t.join(); |
244 |
|
} catch(Exception e) { |
245 |
< |
fail("Unexpected exception"); |
245 |
> |
unexpectedException(); |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
/** |
251 |
< |
* timedJoin will throw InterruptedException. |
227 |
< |
* Thread t waits on timedJoin while the main thread interrupts it. |
251 |
> |
* timedJoin throws InterruptedException when interrupted |
252 |
|
*/ |
253 |
|
public void testTimedJoin() { |
254 |
|
Thread t = new Thread(new Runnable() { |
257 |
|
try { |
258 |
|
Thread s = new Thread(new Runnable() { |
259 |
|
public void run() { |
260 |
< |
try{ |
260 |
> |
try { |
261 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
262 |
< |
}catch(InterruptedException success){} |
262 |
> |
} catch(InterruptedException success){} |
263 |
|
} |
264 |
|
}); |
265 |
|
s.start(); |
266 |
|
tu.timedJoin(s,MEDIUM_DELAY_MS); |
267 |
< |
fail("should throw"); |
267 |
> |
threadShouldThrow(); |
268 |
|
} |
269 |
|
catch(Exception e) {} |
270 |
|
} |
275 |
|
t.interrupt(); |
276 |
|
t.join(); |
277 |
|
} catch(Exception e) { |
278 |
< |
fail("Unexpected exception"); |
278 |
> |
unexpectedException(); |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
< |
* timedSleep will throw InterruptedException. |
260 |
< |
* Thread t waits on timedSleep while the main thread interrupts it. |
283 |
> |
* timedSleep throws InterruptedException when interrupted |
284 |
|
*/ |
285 |
|
public void testTimedSleep() { |
286 |
|
//created a new thread with anonymous runnable |
290 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
291 |
|
try { |
292 |
|
tu.sleep(MEDIUM_DELAY_MS); |
293 |
< |
fail("should throw"); |
293 |
> |
threadShouldThrow(); |
294 |
|
} |
295 |
|
catch(InterruptedException success) {} |
296 |
|
} |
301 |
|
t.interrupt(); |
302 |
|
t.join(); |
303 |
|
} catch(Exception e) { |
304 |
< |
fail("Unexpected exception"); |
304 |
> |
unexpectedException(); |
305 |
|
} |
306 |
|
} |
307 |
|
|
308 |
+ |
/** |
309 |
+ |
* a deserialized serialized unit is equal |
310 |
+ |
*/ |
311 |
|
public void testSerialization() { |
312 |
|
TimeUnit q = TimeUnit.MILLISECONDS; |
313 |
|
|
324 |
|
assertEquals(q.toString(), r.toString()); |
325 |
|
} catch(Exception e){ |
326 |
|
e.printStackTrace(); |
327 |
< |
fail("unexpected exception"); |
327 |
> |
unexpectedException(); |
328 |
|
} |
329 |
|
} |
330 |
|
|