ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TimeUnitTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 18:20:08 2003 UTC

# Line 10 | Line 10 | import junit.framework.*;
10   import java.util.concurrent.*;
11   import java.io.*;
12  
13 < public class TimeUnitTest extends TestCase {
14 <      static public boolean DEBUG = false;
15 <
16 <
13 > public class TimeUnitTest extends JSR166TestCase {
14      public static void main(String[] args) {
15          junit.textui.TestRunner.run(suite());  
16      }
# Line 22 | Line 19 | public class TimeUnitTest extends TestCa
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,
# Line 75 | Line 75 | public class TimeUnitTest extends TestCa
75          }
76      }
77  
78 +    /**
79 +     *
80 +     */
81      public void testToNanos() {
82          for (long t = 0; t < 10; ++t) {
83              assertEquals(1000000000 * t,
# Line 85 | Line 88 | public class TimeUnitTest extends TestCa
88              assertEquals(1000 * t,
89                           TimeUnit.MICROSECONDS.toNanos(t));
90              assertEquals(t,
91 <                         TimeUnit.SECONDS.NANOSECONDS.toNanos(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,
# Line 100 | Line 157 | public class TimeUnitTest extends TestCa
157  
158      }
159  
160 +    /**
161 +     *
162 +     */
163      public void testToNanosSaturate() {
164              assertEquals(Long.MAX_VALUE,
165                           TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
# Line 109 | Line 169 | public class TimeUnitTest extends TestCa
169      }
170  
171  
172 +    /**
173 +     *
174 +     */
175      public void testToString() {
176          String s = TimeUnit.SECONDS.toString();
177          assertTrue(s.indexOf("econd") >= 0);
178      }
179  
117    // Exception tests
180      
181      /**
182 <     *  This test specifically to catch the unreported exception
182 >     *  Timed wait without holding lock throws
183 >     *  IllegalMonitorStateException
184       */
185 <    public void testTimedWaitForUnreportedIllegalMonitorException() {
185 >    /**
186 >     *
187 >     */
188 >    public void testTimedWait_IllegalMonitorException() {
189          //created a new thread with anonymous runnable
190  
191          Thread t = new Thread(new Runnable() {
# Line 127 | Line 193 | public class TimeUnitTest extends TestCa
193                      Object o = new Object();
194                      TimeUnit tu = TimeUnit.MILLISECONDS;
195                      try {
196 <                        tu.timedWait(o,40000);
197 <                        fail("should throw");
196 >                        tu.timedWait(o,LONG_DELAY_MS);
197 >                        threadShouldThrow();
198                      }
199                      catch (InterruptedException ie) {
200 <                        fail("should not throw IE here");
200 >                        threadUnexpectedException();
201                      }
202                      catch(IllegalMonitorStateException success) {
203                      }
# Line 140 | Line 206 | public class TimeUnitTest extends TestCa
206              });
207          t.start();
208          try {
209 <            Thread.sleep(100);
209 >            Thread.sleep(SHORT_DELAY_MS);
210              t.interrupt();
211              t.join();
212          } catch(Exception e) {
213 <            fail("Unexpected exception");
213 >            unexpectedException();
214          }
215      }
216      
217      /**
218 <     *  Test to verify that timedWait will throw InterruptedException.
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() {
# Line 162 | Line 231 | public class TimeUnitTest extends TestCa
231                      TimeUnit tu = TimeUnit.MILLISECONDS;
232                      try {
233                          synchronized(o) {
234 <                            tu.timedWait(o,1000);
234 >                            tu.timedWait(o,MEDIUM_DELAY_MS);
235                          }
236 <                        fail("should throw");
236 >                        threadShouldThrow();
237                      }
238                      catch(InterruptedException success) {}
239                      catch(IllegalMonitorStateException failure) {
240 <                        fail("should not throw");
240 >                        threadUnexpectedException();
241                      }
242                  }
243              });
244          t.start();
245          try {
246 <            Thread.sleep(100);
246 >            Thread.sleep(SHORT_DELAY_MS);
247              t.interrupt();
248              t.join();
249          } catch(Exception e) {
250 <            fail("Unexpected exception");
250 >            unexpectedException();
251          }
252      }
253      
254      
255      /**
256 <     *  Test to verify that timedJoin will throw InterruptedException.
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() {
# Line 194 | Line 266 | public class TimeUnitTest extends TestCa
266                      try {
267                          Thread s = new Thread(new Runnable() {
268                                  public void run() {
269 <                                    try{
270 <                                        Thread.sleep(1000);
271 <                                    }catch(InterruptedException success){}
269 >                                    try {
270 >                                        Thread.sleep(MEDIUM_DELAY_MS);
271 >                                    } catch(InterruptedException success){}
272                                  }
273                              });
274                          s.start();
275 <                        tu.timedJoin(s,1000);
276 <                        fail("should throw");
275 >                        tu.timedJoin(s,MEDIUM_DELAY_MS);
276 >                        threadShouldThrow();
277                      }
278                      catch(Exception e) {}
279                  }
280              });
281          t.start();
282          try {
283 <            Thread.sleep(100);
283 >            Thread.sleep(SHORT_DELAY_MS);
284              t.interrupt();
285              t.join();
286          } catch(Exception e) {
287 <            fail("Unexpected exception");
287 >            unexpectedException();
288          }
289      }
290      
291      /**
292 <     *  Test to verify that timedSleep will throw InterruptedException.
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  
# Line 227 | Line 302 | public class TimeUnitTest extends TestCa
302                  public void run() {
303                      TimeUnit tu = TimeUnit.MILLISECONDS;
304                      try {
305 <                        tu.sleep(1000);
306 <                        fail("should throw");
305 >                        tu.sleep(MEDIUM_DELAY_MS);
306 >                        threadShouldThrow();
307                      }
308                      catch(InterruptedException success) {}
309                  }
310              });
311          t.start();
312          try {
313 <            Thread.sleep(100);
313 >            Thread.sleep(SHORT_DELAY_MS);
314              t.interrupt();
315              t.join();
316          } catch(Exception e) {
317 <            fail("Unexpected exception");
317 >            unexpectedException();
318          }
319      }
320  
321 +    /**
322 +     *
323 +     */
324      public void testSerialization() {
325          TimeUnit q = TimeUnit.MILLISECONDS;
326  
# Line 259 | Line 337 | public class TimeUnitTest extends TestCa
337              assertEquals(q.toString(), r.toString());
338          } catch(Exception e){
339              e.printStackTrace();
340 <            fail("unexpected exception");
340 >            unexpectedException();
341          }
342      }
343  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines