ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:41 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +59 -20 lines
Log Message:
New base class JSR166TestCase

File Contents

# User Rev Content
1 dl 1.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 dl 1.2 import java.io.*;
12 dl 1.1
13 dl 1.3 public class TimeUnitTest extends JSR166TestCase {
14 dl 1.1 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     public void testConvert() {
23     for (long t = 0; t < 10; ++t) {
24     assertEquals(t,
25     TimeUnit.SECONDS.convert(t,
26     TimeUnit.SECONDS));
27     assertEquals(t,
28     TimeUnit.SECONDS.convert(1000 * t,
29     TimeUnit.MILLISECONDS));
30     assertEquals(t,
31     TimeUnit.SECONDS.convert(1000000 * t,
32     TimeUnit.MICROSECONDS));
33     assertEquals(t,
34     TimeUnit.SECONDS.convert(1000000000 * t,
35     TimeUnit.NANOSECONDS));
36     assertEquals(1000 * t,
37     TimeUnit.MILLISECONDS.convert(t,
38     TimeUnit.SECONDS));
39     assertEquals(t,
40     TimeUnit.MILLISECONDS.convert(t,
41     TimeUnit.MILLISECONDS));
42     assertEquals(t,
43     TimeUnit.MILLISECONDS.convert(1000 * t,
44     TimeUnit.MICROSECONDS));
45     assertEquals(t,
46     TimeUnit.MILLISECONDS.convert(1000000 * t,
47     TimeUnit.NANOSECONDS));
48     assertEquals(1000000 * t,
49     TimeUnit.MICROSECONDS.convert(t,
50     TimeUnit.SECONDS));
51     assertEquals(1000 * t,
52     TimeUnit.MICROSECONDS.convert(t,
53     TimeUnit.MILLISECONDS));
54     assertEquals(t,
55     TimeUnit.MICROSECONDS.convert(t,
56     TimeUnit.MICROSECONDS));
57     assertEquals(t,
58     TimeUnit.MICROSECONDS.convert(1000 * t,
59     TimeUnit.NANOSECONDS));
60     assertEquals(1000000000 * t,
61     TimeUnit.NANOSECONDS.convert(t,
62     TimeUnit.SECONDS));
63     assertEquals(1000000 * t,
64     TimeUnit.NANOSECONDS.convert(t,
65     TimeUnit.MILLISECONDS));
66     assertEquals(1000 * t,
67     TimeUnit.NANOSECONDS.convert(t,
68     TimeUnit.MICROSECONDS));
69     assertEquals(t,
70     TimeUnit.NANOSECONDS.convert(t,
71     TimeUnit.NANOSECONDS));
72     }
73     }
74    
75     public void testToNanos() {
76     for (long t = 0; t < 10; ++t) {
77     assertEquals(1000000000 * t,
78     TimeUnit.SECONDS.toNanos(t));
79    
80     assertEquals(1000000 * t,
81     TimeUnit.MILLISECONDS.toNanos(t));
82     assertEquals(1000 * t,
83     TimeUnit.MICROSECONDS.toNanos(t));
84     assertEquals(t,
85 dl 1.3 TimeUnit.NANOSECONDS.toNanos(t));
86     }
87     }
88    
89     public void testToMicros() {
90     for (long t = 0; t < 10; ++t) {
91     assertEquals(1000000 * t,
92     TimeUnit.SECONDS.toMicros(t));
93    
94     assertEquals(1000 * t,
95     TimeUnit.MILLISECONDS.toMicros(t));
96     assertEquals(t,
97     TimeUnit.MICROSECONDS.toMicros(t));
98     assertEquals(t,
99     TimeUnit.NANOSECONDS.toMicros(t * 1000));
100     }
101     }
102    
103     public void testToMillis() {
104     for (long t = 0; t < 10; ++t) {
105     assertEquals(1000 * t,
106     TimeUnit.SECONDS.toMillis(t));
107    
108     assertEquals(t,
109     TimeUnit.MILLISECONDS.toMillis(t));
110     assertEquals(t,
111     TimeUnit.MICROSECONDS.toMillis(t * 1000));
112     assertEquals(t,
113     TimeUnit.NANOSECONDS.toMillis(t * 1000000));
114     }
115     }
116    
117     public void testToSeconds() {
118     for (long t = 0; t < 10; ++t) {
119     assertEquals(t,
120     TimeUnit.SECONDS.toSeconds(t));
121    
122     assertEquals(t,
123     TimeUnit.MILLISECONDS.toSeconds(t * 1000));
124     assertEquals(t,
125     TimeUnit.MICROSECONDS.toSeconds(t * 1000000));
126     assertEquals(t,
127     TimeUnit.NANOSECONDS.toSeconds(t * 1000000000));
128 dl 1.1 }
129     }
130    
131    
132     public void testConvertSaturate() {
133     assertEquals(Long.MAX_VALUE,
134     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
135     TimeUnit.SECONDS));
136     assertEquals(Long.MIN_VALUE,
137     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
138     TimeUnit.SECONDS));
139    
140     }
141    
142     public void testToNanosSaturate() {
143     assertEquals(Long.MAX_VALUE,
144     TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
145     assertEquals(Long.MIN_VALUE,
146     TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
147    
148     }
149    
150    
151     public void testToString() {
152     String s = TimeUnit.SECONDS.toString();
153     assertTrue(s.indexOf("econd") >= 0);
154     }
155    
156    
157     /**
158 dl 1.3 * Timed wait without holding lock throws
159     * IllegalMonitorStateException
160 dl 1.1 */
161 dl 1.3 public void testTimedWait_IllegalMonitorException() {
162 dl 1.1 //created a new thread with anonymous runnable
163    
164     Thread t = new Thread(new Runnable() {
165     public void run() {
166     Object o = new Object();
167     TimeUnit tu = TimeUnit.MILLISECONDS;
168     try {
169 dl 1.3 tu.timedWait(o,LONG_DELAY_MS);
170 dl 1.1 fail("should throw");
171     }
172     catch (InterruptedException ie) {
173     fail("should not throw IE here");
174     }
175     catch(IllegalMonitorStateException success) {
176     }
177    
178     }
179     });
180     t.start();
181     try {
182 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
183 dl 1.1 t.interrupt();
184     t.join();
185     } catch(Exception e) {
186     fail("Unexpected exception");
187     }
188     }
189    
190     /**
191 dl 1.3 * timedWait will throw InterruptedException.
192 dl 1.1 * 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
195     */
196     public void testTimedWait() {
197     Thread t = new Thread(new Runnable() {
198     public void run() {
199     Object o = new Object();
200    
201     TimeUnit tu = TimeUnit.MILLISECONDS;
202     try {
203     synchronized(o) {
204 dl 1.3 tu.timedWait(o,MEDIUM_DELAY_MS);
205 dl 1.1 }
206     fail("should throw");
207     }
208     catch(InterruptedException success) {}
209     catch(IllegalMonitorStateException failure) {
210     fail("should not throw");
211     }
212     }
213     });
214     t.start();
215     try {
216 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
217 dl 1.1 t.interrupt();
218     t.join();
219     } catch(Exception e) {
220     fail("Unexpected exception");
221     }
222     }
223    
224    
225     /**
226 dl 1.3 * timedJoin will throw InterruptedException.
227 dl 1.1 * Thread t waits on timedJoin while the main thread interrupts it.
228     */
229     public void testTimedJoin() {
230     Thread t = new Thread(new Runnable() {
231     public void run() {
232     TimeUnit tu = TimeUnit.MILLISECONDS;
233     try {
234     Thread s = new Thread(new Runnable() {
235     public void run() {
236     try{
237 dl 1.3 Thread.sleep(MEDIUM_DELAY_MS);
238 dl 1.1 }catch(InterruptedException success){}
239     }
240     });
241     s.start();
242 dl 1.3 tu.timedJoin(s,MEDIUM_DELAY_MS);
243 dl 1.1 fail("should throw");
244     }
245     catch(Exception e) {}
246     }
247     });
248     t.start();
249     try {
250 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
251 dl 1.1 t.interrupt();
252     t.join();
253     } catch(Exception e) {
254     fail("Unexpected exception");
255     }
256     }
257    
258     /**
259 dl 1.3 * timedSleep will throw InterruptedException.
260 dl 1.1 * Thread t waits on timedSleep while the main thread interrupts it.
261     */
262     public void testTimedSleep() {
263     //created a new thread with anonymous runnable
264    
265     Thread t = new Thread(new Runnable() {
266     public void run() {
267     TimeUnit tu = TimeUnit.MILLISECONDS;
268     try {
269 dl 1.3 tu.sleep(MEDIUM_DELAY_MS);
270 dl 1.1 fail("should throw");
271     }
272     catch(InterruptedException success) {}
273     }
274     });
275     t.start();
276     try {
277 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
278 dl 1.1 t.interrupt();
279     t.join();
280     } catch(Exception e) {
281     fail("Unexpected exception");
282     }
283     }
284 dl 1.2
285     public void testSerialization() {
286     TimeUnit q = TimeUnit.MILLISECONDS;
287    
288     try {
289     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
290     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
291     out.writeObject(q);
292     out.close();
293    
294     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
295     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
296     TimeUnit r = (TimeUnit)in.readObject();
297    
298     assertEquals(q.toString(), r.toString());
299     } catch(Exception e){
300     e.printStackTrace();
301     fail("unexpected exception");
302     }
303     }
304    
305 dl 1.1 }