6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
import static java.util.concurrent.TimeUnit.DAYS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.HOURS; |
11 |
+ |
import static java.util.concurrent.TimeUnit.MICROSECONDS; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MINUTES; |
14 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
|
17 |
< |
import junit.framework.*; |
18 |
< |
import java.util.concurrent.*; |
19 |
< |
import java.io.*; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.TimeUnit; |
19 |
> |
|
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class TimeUnitTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
|
28 |
|
public static Test suite() { |
29 |
|
return new TestSuite(TimeUnitTest.class); |
30 |
|
} |
31 |
|
|
32 |
< |
// (loops to 88888 check increments at all time divisions.) |
33 |
< |
|
34 |
< |
/** |
35 |
< |
* convert correctly converts sample values across the units |
36 |
< |
*/ |
37 |
< |
public void testConvert() { |
38 |
< |
for (long t = 0; t < 88888; ++t) { |
39 |
< |
assertEquals(t*60*60*24, |
40 |
< |
TimeUnit.SECONDS.convert(t, |
41 |
< |
TimeUnit.DAYS)); |
42 |
< |
assertEquals(t*60*60, |
34 |
< |
TimeUnit.SECONDS.convert(t, |
35 |
< |
TimeUnit.HOURS)); |
36 |
< |
assertEquals(t*60, |
37 |
< |
TimeUnit.SECONDS.convert(t, |
38 |
< |
TimeUnit.MINUTES)); |
39 |
< |
assertEquals(t, |
40 |
< |
TimeUnit.SECONDS.convert(t, |
41 |
< |
TimeUnit.SECONDS)); |
42 |
< |
assertEquals(t, |
43 |
< |
TimeUnit.SECONDS.convert(1000L*t, |
44 |
< |
TimeUnit.MILLISECONDS)); |
45 |
< |
assertEquals(t, |
46 |
< |
TimeUnit.SECONDS.convert(1000000L*t, |
47 |
< |
TimeUnit.MICROSECONDS)); |
48 |
< |
assertEquals(t, |
49 |
< |
TimeUnit.SECONDS.convert(1000000000L*t, |
50 |
< |
TimeUnit.NANOSECONDS)); |
51 |
< |
|
52 |
< |
|
53 |
< |
assertEquals(1000L*t*60*60*24, |
54 |
< |
TimeUnit.MILLISECONDS.convert(t, |
55 |
< |
TimeUnit.DAYS)); |
56 |
< |
assertEquals(1000L*t*60*60, |
57 |
< |
TimeUnit.MILLISECONDS.convert(t, |
58 |
< |
TimeUnit.HOURS)); |
59 |
< |
assertEquals(1000L*t*60, |
60 |
< |
TimeUnit.MILLISECONDS.convert(t, |
61 |
< |
TimeUnit.MINUTES)); |
62 |
< |
assertEquals(1000L*t, |
63 |
< |
TimeUnit.MILLISECONDS.convert(t, |
64 |
< |
TimeUnit.SECONDS)); |
65 |
< |
assertEquals(t, |
66 |
< |
TimeUnit.MILLISECONDS.convert(t, |
67 |
< |
TimeUnit.MILLISECONDS)); |
68 |
< |
assertEquals(t, |
69 |
< |
TimeUnit.MILLISECONDS.convert(1000L*t, |
70 |
< |
TimeUnit.MICROSECONDS)); |
71 |
< |
assertEquals(t, |
72 |
< |
TimeUnit.MILLISECONDS.convert(1000000L*t, |
73 |
< |
TimeUnit.NANOSECONDS)); |
74 |
< |
|
75 |
< |
assertEquals(1000000L*t*60*60*24, |
76 |
< |
TimeUnit.MICROSECONDS.convert(t, |
77 |
< |
TimeUnit.DAYS)); |
78 |
< |
assertEquals(1000000L*t*60*60, |
79 |
< |
TimeUnit.MICROSECONDS.convert(t, |
80 |
< |
TimeUnit.HOURS)); |
81 |
< |
assertEquals(1000000L*t*60, |
82 |
< |
TimeUnit.MICROSECONDS.convert(t, |
83 |
< |
TimeUnit.MINUTES)); |
84 |
< |
assertEquals(1000000L*t, |
85 |
< |
TimeUnit.MICROSECONDS.convert(t, |
86 |
< |
TimeUnit.SECONDS)); |
87 |
< |
assertEquals(1000L*t, |
88 |
< |
TimeUnit.MICROSECONDS.convert(t, |
89 |
< |
TimeUnit.MILLISECONDS)); |
90 |
< |
assertEquals(t, |
91 |
< |
TimeUnit.MICROSECONDS.convert(t, |
92 |
< |
TimeUnit.MICROSECONDS)); |
93 |
< |
assertEquals(t, |
94 |
< |
TimeUnit.MICROSECONDS.convert(1000L*t, |
95 |
< |
TimeUnit.NANOSECONDS)); |
96 |
< |
|
97 |
< |
assertEquals(1000000000L*t*60*60*24, |
98 |
< |
TimeUnit.NANOSECONDS.convert(t, |
99 |
< |
TimeUnit.DAYS)); |
100 |
< |
assertEquals(1000000000L*t*60*60, |
101 |
< |
TimeUnit.NANOSECONDS.convert(t, |
102 |
< |
TimeUnit.HOURS)); |
103 |
< |
assertEquals(1000000000L*t*60, |
104 |
< |
TimeUnit.NANOSECONDS.convert(t, |
105 |
< |
TimeUnit.MINUTES)); |
106 |
< |
assertEquals(1000000000L*t, |
107 |
< |
TimeUnit.NANOSECONDS.convert(t, |
108 |
< |
TimeUnit.SECONDS)); |
109 |
< |
assertEquals(1000000L*t, |
110 |
< |
TimeUnit.NANOSECONDS.convert(t, |
111 |
< |
TimeUnit.MILLISECONDS)); |
112 |
< |
assertEquals(1000L*t, |
113 |
< |
TimeUnit.NANOSECONDS.convert(t, |
114 |
< |
TimeUnit.MICROSECONDS)); |
115 |
< |
assertEquals(t, |
116 |
< |
TimeUnit.NANOSECONDS.convert(t, |
117 |
< |
TimeUnit.NANOSECONDS)); |
32 |
> |
void testConversion(TimeUnit x, TimeUnit y, long n, long expected) { |
33 |
> |
assertEquals(expected, x.convert(n, y)); |
34 |
> |
switch (x) { |
35 |
> |
case NANOSECONDS: assertEquals(expected, y.toNanos(n)); break; |
36 |
> |
case MICROSECONDS: assertEquals(expected, y.toMicros(n)); break; |
37 |
> |
case MILLISECONDS: assertEquals(expected, y.toMillis(n)); break; |
38 |
> |
case SECONDS: assertEquals(expected, y.toSeconds(n)); break; |
39 |
> |
case MINUTES: assertEquals(expected, y.toMinutes(n)); break; |
40 |
> |
case HOURS: assertEquals(expected, y.toHours(n)); break; |
41 |
> |
case DAYS: assertEquals(expected, y.toDays(n)); break; |
42 |
> |
default: throw new AssertionError(); |
43 |
|
} |
44 |
+ |
|
45 |
+ |
if (n > 0) testConversion(x, y, -n, -expected); |
46 |
|
} |
47 |
|
|
48 |
< |
/** |
49 |
< |
* toNanos correctly converts sample values in different units to |
50 |
< |
* nanoseconds |
51 |
< |
*/ |
52 |
< |
public void testToNanos() { |
53 |
< |
for (long t = 0; t < 88888; ++t) { |
54 |
< |
assertEquals(t*1000000000L*60*60*24, |
55 |
< |
TimeUnit.DAYS.toNanos(t)); |
56 |
< |
assertEquals(t*1000000000L*60*60, |
57 |
< |
TimeUnit.HOURS.toNanos(t)); |
131 |
< |
assertEquals(t*1000000000L*60, |
132 |
< |
TimeUnit.MINUTES.toNanos(t)); |
133 |
< |
assertEquals(1000000000L*t, |
134 |
< |
TimeUnit.SECONDS.toNanos(t)); |
135 |
< |
assertEquals(1000000L*t, |
136 |
< |
TimeUnit.MILLISECONDS.toNanos(t)); |
137 |
< |
assertEquals(1000L*t, |
138 |
< |
TimeUnit.MICROSECONDS.toNanos(t)); |
139 |
< |
assertEquals(t, |
140 |
< |
TimeUnit.NANOSECONDS.toNanos(t)); |
48 |
> |
void testConversion(TimeUnit x, TimeUnit y) { |
49 |
> |
long ratio = x.toNanos(1)/y.toNanos(1); |
50 |
> |
assertTrue(ratio > 0); |
51 |
> |
long[] ns = { 0, 1, 2, Long.MAX_VALUE/ratio, Long.MIN_VALUE/ratio }; |
52 |
> |
for (long n : ns) { |
53 |
> |
testConversion(y, x, n, n * ratio); |
54 |
> |
long[] ks = { n * ratio, n * ratio + 1, n * ratio - 1 }; |
55 |
> |
for (long k : ks) { |
56 |
> |
testConversion(x, y, k, k / ratio); |
57 |
> |
} |
58 |
|
} |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
< |
* toMicros correctly converts sample values in different units to |
146 |
< |
* microseconds |
62 |
> |
* Conversion methods correctly convert sample values |
63 |
|
*/ |
64 |
< |
public void testToMicros() { |
65 |
< |
for (long t = 0; t < 88888; ++t) { |
66 |
< |
assertEquals(t*1000000L*60*60*24, |
67 |
< |
TimeUnit.DAYS.toMicros(t)); |
68 |
< |
assertEquals(t*1000000L*60*60, |
69 |
< |
TimeUnit.HOURS.toMicros(t)); |
70 |
< |
assertEquals(t*1000000L*60, |
71 |
< |
TimeUnit.MINUTES.toMicros(t)); |
72 |
< |
assertEquals(1000000L*t, |
73 |
< |
TimeUnit.SECONDS.toMicros(t)); |
74 |
< |
assertEquals(1000L*t, |
75 |
< |
TimeUnit.MILLISECONDS.toMicros(t)); |
160 |
< |
assertEquals(t, |
161 |
< |
TimeUnit.MICROSECONDS.toMicros(t)); |
162 |
< |
assertEquals(t, |
163 |
< |
TimeUnit.NANOSECONDS.toMicros(t*1000L)); |
64 |
> |
public void testConversions() { |
65 |
> |
// Sanity check |
66 |
> |
assertEquals(1, NANOSECONDS.toNanos(1)); |
67 |
> |
assertEquals(1000L * NANOSECONDS.toNanos(1), MICROSECONDS.toNanos(1)); |
68 |
> |
assertEquals(1000L * MICROSECONDS.toNanos(1), MILLISECONDS.toNanos(1)); |
69 |
> |
assertEquals(1000L * MILLISECONDS.toNanos(1), SECONDS.toNanos(1)); |
70 |
> |
assertEquals(60L * SECONDS.toNanos(1), MINUTES.toNanos(1)); |
71 |
> |
assertEquals(60L * MINUTES.toNanos(1), HOURS.toNanos(1)); |
72 |
> |
assertEquals(24L * HOURS.toNanos(1), DAYS.toNanos(1)); |
73 |
> |
|
74 |
> |
for (TimeUnit x : TimeUnit.values()) { |
75 |
> |
assertEquals(x.toNanos(1), NANOSECONDS.convert(1, x)); |
76 |
|
} |
77 |
+ |
|
78 |
+ |
for (TimeUnit x : TimeUnit.values()) |
79 |
+ |
for (TimeUnit y : TimeUnit.values()) |
80 |
+ |
if (x.toNanos(1) >= y.toNanos(1)) |
81 |
+ |
testConversion(x, y); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* toMillis correctly converts sample values in different units to |
86 |
< |
* milliseconds |
85 |
> |
* convert saturates positive too-large values to Long.MAX_VALUE |
86 |
> |
* and negative to LONG.MIN_VALUE |
87 |
|
*/ |
88 |
< |
public void testToMillis() { |
89 |
< |
for (long t = 0; t < 88888; ++t) { |
90 |
< |
assertEquals(t*1000L*60*60*24, |
91 |
< |
TimeUnit.DAYS.toMillis(t)); |
92 |
< |
assertEquals(t*1000L*60*60, |
93 |
< |
TimeUnit.HOURS.toMillis(t)); |
94 |
< |
assertEquals(t*1000L*60, |
95 |
< |
TimeUnit.MINUTES.toMillis(t)); |
96 |
< |
assertEquals(1000L*t, |
97 |
< |
TimeUnit.SECONDS.toMillis(t)); |
98 |
< |
assertEquals(t, |
99 |
< |
TimeUnit.MILLISECONDS.toMillis(t)); |
100 |
< |
assertEquals(t, |
101 |
< |
TimeUnit.MICROSECONDS.toMillis(t*1000L)); |
102 |
< |
assertEquals(t, |
103 |
< |
TimeUnit.NANOSECONDS.toMillis(t*1000000L)); |
104 |
< |
} |
88 |
> |
public void testConvertSaturate() { |
89 |
> |
assertEquals(Long.MAX_VALUE, |
90 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS)); |
91 |
> |
assertEquals(Long.MIN_VALUE, |
92 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS)); |
93 |
> |
assertEquals(Long.MAX_VALUE, |
94 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES)); |
95 |
> |
assertEquals(Long.MIN_VALUE, |
96 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES)); |
97 |
> |
assertEquals(Long.MAX_VALUE, |
98 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS)); |
99 |
> |
assertEquals(Long.MIN_VALUE, |
100 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS)); |
101 |
> |
assertEquals(Long.MAX_VALUE, |
102 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); |
103 |
> |
assertEquals(Long.MIN_VALUE, |
104 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); |
105 |
> |
|
106 |
> |
for (TimeUnit x : TimeUnit.values()) |
107 |
> |
for (TimeUnit y : TimeUnit.values()) { |
108 |
> |
long ratio = x.toNanos(1) / y.toNanos(1); |
109 |
> |
if (ratio >= 1) { |
110 |
> |
assertEquals(ratio, y.convert(1, x)); |
111 |
> |
assertEquals(1, x.convert(ratio, y)); |
112 |
> |
long max = Long.MAX_VALUE/ratio; |
113 |
> |
assertEquals(max * ratio, y.convert(max, x)); |
114 |
> |
assertEquals(-max * ratio, y.convert(-max, x)); |
115 |
> |
assertEquals(max, x.convert(max * ratio, y)); |
116 |
> |
assertEquals(-max, x.convert(-max * ratio, y)); |
117 |
> |
if (max < Long.MAX_VALUE) { |
118 |
> |
assertEquals(Long.MAX_VALUE, y.convert(max + 1, x)); |
119 |
> |
assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x)); |
120 |
> |
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x)); |
121 |
> |
} |
122 |
> |
assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x)); |
123 |
> |
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x)); |
124 |
> |
} |
125 |
> |
} |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
< |
* toSeconds correctly converts sample values in different units to |
130 |
< |
* seconds |
129 |
> |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
130 |
> |
* and negative to LONG.MIN_VALUE |
131 |
|
*/ |
132 |
< |
public void testToSeconds() { |
133 |
< |
for (long t = 0; t < 88888; ++t) { |
134 |
< |
assertEquals(t*60*60*24, |
135 |
< |
TimeUnit.DAYS.toSeconds(t)); |
136 |
< |
assertEquals(t*60*60, |
137 |
< |
TimeUnit.HOURS.toSeconds(t)); |
138 |
< |
assertEquals(t*60, |
139 |
< |
TimeUnit.MINUTES.toSeconds(t)); |
140 |
< |
assertEquals(t, |
141 |
< |
TimeUnit.SECONDS.toSeconds(t)); |
142 |
< |
assertEquals(t, |
143 |
< |
TimeUnit.MILLISECONDS.toSeconds(t*1000L)); |
144 |
< |
assertEquals(t, |
145 |
< |
TimeUnit.MICROSECONDS.toSeconds(t*1000000L)); |
146 |
< |
assertEquals(t, |
147 |
< |
TimeUnit.NANOSECONDS.toSeconds(t*1000000000L)); |
132 |
> |
public void testToNanosSaturate() { |
133 |
> |
assertEquals(Long.MAX_VALUE, |
134 |
> |
MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
135 |
> |
assertEquals(Long.MIN_VALUE, |
136 |
> |
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
137 |
> |
|
138 |
> |
for (TimeUnit x : TimeUnit.values()) { |
139 |
> |
long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1); |
140 |
> |
if (ratio >= 1) { |
141 |
> |
long max = Long.MAX_VALUE/ratio; |
142 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
143 |
> |
assertEquals(z * ratio, x.toNanos(z)); |
144 |
> |
if (max < Long.MAX_VALUE) { |
145 |
> |
assertEquals(Long.MAX_VALUE, x.toNanos(max + 1)); |
146 |
> |
assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1)); |
147 |
> |
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1)); |
148 |
> |
} |
149 |
> |
assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE)); |
150 |
> |
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE)); |
151 |
> |
if (max < Integer.MAX_VALUE) { |
152 |
> |
assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE)); |
153 |
> |
assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE)); |
154 |
> |
} |
155 |
> |
} |
156 |
|
} |
157 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
< |
* toMinutes correctly converts sample values in different units to |
161 |
< |
* minutes |
160 |
> |
* toMicros saturates positive too-large values to Long.MAX_VALUE |
161 |
> |
* and negative to LONG.MIN_VALUE |
162 |
|
*/ |
163 |
< |
public void testToMinutes() { |
164 |
< |
for (long t = 0; t < 88888; ++t) { |
165 |
< |
assertEquals(t*60*24, |
166 |
< |
TimeUnit.DAYS.toMinutes(t)); |
167 |
< |
assertEquals(t*60, |
168 |
< |
TimeUnit.HOURS.toMinutes(t)); |
169 |
< |
assertEquals(t, |
170 |
< |
TimeUnit.MINUTES.toMinutes(t)); |
171 |
< |
assertEquals(t, |
172 |
< |
TimeUnit.SECONDS.toMinutes(t*60)); |
173 |
< |
assertEquals(t, |
174 |
< |
TimeUnit.MILLISECONDS.toMinutes(t*1000L*60)); |
175 |
< |
assertEquals(t, |
176 |
< |
TimeUnit.MICROSECONDS.toMinutes(t*1000000L*60)); |
177 |
< |
assertEquals(t, |
178 |
< |
TimeUnit.NANOSECONDS.toMinutes(t*1000000000L*60)); |
163 |
> |
public void testToMicrosSaturate() { |
164 |
> |
for (TimeUnit x : TimeUnit.values()) { |
165 |
> |
long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1); |
166 |
> |
if (ratio >= 1) { |
167 |
> |
long max = Long.MAX_VALUE/ratio; |
168 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
169 |
> |
assertEquals(z * ratio, x.toMicros(z)); |
170 |
> |
if (max < Long.MAX_VALUE) { |
171 |
> |
assertEquals(Long.MAX_VALUE, x.toMicros(max + 1)); |
172 |
> |
assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1)); |
173 |
> |
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1)); |
174 |
> |
} |
175 |
> |
assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE)); |
176 |
> |
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE)); |
177 |
> |
if (max < Integer.MAX_VALUE) { |
178 |
> |
assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE)); |
179 |
> |
assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE)); |
180 |
> |
} |
181 |
> |
} |
182 |
|
} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
186 |
< |
* toHours correctly converts sample values in different units to |
187 |
< |
* hours |
186 |
> |
* toMillis saturates positive too-large values to Long.MAX_VALUE |
187 |
> |
* and negative to LONG.MIN_VALUE |
188 |
|
*/ |
189 |
< |
public void testToHours() { |
190 |
< |
for (long t = 0; t < 88888; ++t) { |
191 |
< |
assertEquals(t*24, |
192 |
< |
TimeUnit.DAYS.toHours(t)); |
193 |
< |
assertEquals(t, |
194 |
< |
TimeUnit.HOURS.toHours(t)); |
195 |
< |
assertEquals(t, |
196 |
< |
TimeUnit.MINUTES.toHours(t*60)); |
197 |
< |
assertEquals(t, |
198 |
< |
TimeUnit.SECONDS.toHours(t*60*60)); |
199 |
< |
assertEquals(t, |
200 |
< |
TimeUnit.MILLISECONDS.toHours(t*1000L*60*60)); |
201 |
< |
assertEquals(t, |
202 |
< |
TimeUnit.MICROSECONDS.toHours(t*1000000L*60*60)); |
203 |
< |
assertEquals(t, |
204 |
< |
TimeUnit.NANOSECONDS.toHours(t*1000000000L*60*60)); |
189 |
> |
public void testToMillisSaturate() { |
190 |
> |
for (TimeUnit x : TimeUnit.values()) { |
191 |
> |
long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1); |
192 |
> |
if (ratio >= 1) { |
193 |
> |
long max = Long.MAX_VALUE/ratio; |
194 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
195 |
> |
assertEquals(z * ratio, x.toMillis(z)); |
196 |
> |
if (max < Long.MAX_VALUE) { |
197 |
> |
assertEquals(Long.MAX_VALUE, x.toMillis(max + 1)); |
198 |
> |
assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1)); |
199 |
> |
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1)); |
200 |
> |
} |
201 |
> |
assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE)); |
202 |
> |
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE)); |
203 |
> |
if (max < Integer.MAX_VALUE) { |
204 |
> |
assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE)); |
205 |
> |
assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE)); |
206 |
> |
} |
207 |
> |
} |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* toDays correctly converts sample values in different units to |
213 |
< |
* days |
212 |
> |
* toSeconds saturates positive too-large values to Long.MAX_VALUE |
213 |
> |
* and negative to LONG.MIN_VALUE |
214 |
|
*/ |
215 |
< |
public void testToDays() { |
216 |
< |
for (long t = 0; t < 88888; ++t) { |
217 |
< |
assertEquals(t, |
218 |
< |
TimeUnit.DAYS.toDays(t)); |
219 |
< |
assertEquals(t, |
220 |
< |
TimeUnit.HOURS.toDays(t*24)); |
221 |
< |
assertEquals(t, |
222 |
< |
TimeUnit.MINUTES.toDays(t*60*24)); |
223 |
< |
assertEquals(t, |
224 |
< |
TimeUnit.SECONDS.toDays(t*60*60*24)); |
225 |
< |
assertEquals(t, |
226 |
< |
TimeUnit.MILLISECONDS.toDays(t*1000L*60*60*24)); |
227 |
< |
assertEquals(t, |
228 |
< |
TimeUnit.MICROSECONDS.toDays(t*1000000L*60*60*24)); |
229 |
< |
assertEquals(t, |
230 |
< |
TimeUnit.NANOSECONDS.toDays(t*1000000000L*60*60*24)); |
215 |
> |
public void testToSecondsSaturate() { |
216 |
> |
for (TimeUnit x : TimeUnit.values()) { |
217 |
> |
long ratio = x.toNanos(1) / SECONDS.toNanos(1); |
218 |
> |
if (ratio >= 1) { |
219 |
> |
long max = Long.MAX_VALUE/ratio; |
220 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
221 |
> |
assertEquals(z * ratio, x.toSeconds(z)); |
222 |
> |
if (max < Long.MAX_VALUE) { |
223 |
> |
assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1)); |
224 |
> |
assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1)); |
225 |
> |
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1)); |
226 |
> |
} |
227 |
> |
assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE)); |
228 |
> |
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE)); |
229 |
> |
if (max < Integer.MAX_VALUE) { |
230 |
> |
assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE)); |
231 |
> |
assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE)); |
232 |
> |
} |
233 |
> |
} |
234 |
|
} |
235 |
|
} |
236 |
|
|
282 |
– |
|
237 |
|
/** |
238 |
< |
* convert saturates positive too-large values to Long.MAX_VALUE |
238 |
> |
* toMinutes saturates positive too-large values to Long.MAX_VALUE |
239 |
|
* and negative to LONG.MIN_VALUE |
240 |
|
*/ |
241 |
< |
public void testConvertSaturate() { |
242 |
< |
assertEquals(Long.MAX_VALUE, |
243 |
< |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
244 |
< |
TimeUnit.SECONDS)); |
245 |
< |
assertEquals(Long.MIN_VALUE, |
246 |
< |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
247 |
< |
TimeUnit.SECONDS)); |
248 |
< |
assertEquals(Long.MAX_VALUE, |
249 |
< |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
250 |
< |
TimeUnit.MINUTES)); |
251 |
< |
assertEquals(Long.MIN_VALUE, |
252 |
< |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
253 |
< |
TimeUnit.MINUTES)); |
254 |
< |
assertEquals(Long.MAX_VALUE, |
301 |
< |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
302 |
< |
TimeUnit.HOURS)); |
303 |
< |
assertEquals(Long.MIN_VALUE, |
304 |
< |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
305 |
< |
TimeUnit.HOURS)); |
306 |
< |
assertEquals(Long.MAX_VALUE, |
307 |
< |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
308 |
< |
TimeUnit.DAYS)); |
309 |
< |
assertEquals(Long.MIN_VALUE, |
310 |
< |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
311 |
< |
TimeUnit.DAYS)); |
241 |
> |
public void testToMinutesSaturate() { |
242 |
> |
for (TimeUnit x : TimeUnit.values()) { |
243 |
> |
long ratio = x.toNanos(1) / MINUTES.toNanos(1); |
244 |
> |
if (ratio > 1) { |
245 |
> |
long max = Long.MAX_VALUE/ratio; |
246 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
247 |
> |
assertEquals(z * ratio, x.toMinutes(z)); |
248 |
> |
assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1)); |
249 |
> |
assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1)); |
250 |
> |
assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE)); |
251 |
> |
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE)); |
252 |
> |
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1)); |
253 |
> |
} |
254 |
> |
} |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
< |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
258 |
> |
* toHours saturates positive too-large values to Long.MAX_VALUE |
259 |
|
* and negative to LONG.MIN_VALUE |
260 |
|
*/ |
261 |
< |
public void testToNanosSaturate() { |
262 |
< |
assertEquals(Long.MAX_VALUE, |
263 |
< |
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
264 |
< |
assertEquals(Long.MIN_VALUE, |
265 |
< |
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
261 |
> |
public void testToHoursSaturate() { |
262 |
> |
for (TimeUnit x : TimeUnit.values()) { |
263 |
> |
long ratio = x.toNanos(1) / HOURS.toNanos(1); |
264 |
> |
if (ratio >= 1) { |
265 |
> |
long max = Long.MAX_VALUE/ratio; |
266 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
267 |
> |
assertEquals(z * ratio, x.toHours(z)); |
268 |
> |
if (max < Long.MAX_VALUE) { |
269 |
> |
assertEquals(Long.MAX_VALUE, x.toHours(max + 1)); |
270 |
> |
assertEquals(Long.MIN_VALUE, x.toHours(-max - 1)); |
271 |
> |
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1)); |
272 |
> |
} |
273 |
> |
assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE)); |
274 |
> |
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE)); |
275 |
> |
} |
276 |
> |
} |
277 |
|
} |
278 |
|
|
325 |
– |
|
279 |
|
/** |
280 |
< |
* toString returns string containing common name of unit |
280 |
> |
* toString returns name of unit |
281 |
|
*/ |
282 |
|
public void testToString() { |
283 |
< |
String s = TimeUnit.SECONDS.toString(); |
284 |
< |
assertTrue(s.indexOf("ECOND") >= 0); |
283 |
> |
assertEquals("NANOSECONDS", NANOSECONDS.toString()); |
284 |
> |
assertEquals("MICROSECONDS", MICROSECONDS.toString()); |
285 |
> |
assertEquals("MILLISECONDS", MILLISECONDS.toString()); |
286 |
> |
assertEquals("SECONDS", SECONDS.toString()); |
287 |
> |
assertEquals("MINUTES", MINUTES.toString()); |
288 |
> |
assertEquals("HOURS", HOURS.toString()); |
289 |
> |
assertEquals("DAYS", DAYS.toString()); |
290 |
|
} |
291 |
|
|
292 |
+ |
/** |
293 |
+ |
* name returns name of unit |
294 |
+ |
*/ |
295 |
+ |
public void testName() { |
296 |
+ |
for (TimeUnit x : TimeUnit.values()) |
297 |
+ |
assertEquals(x.toString(), x.name()); |
298 |
+ |
} |
299 |
|
|
300 |
|
/** |
301 |
|
* Timed wait without holding lock throws |
302 |
|
* IllegalMonitorStateException |
303 |
|
*/ |
304 |
< |
public void testTimedWait_IllegalMonitorException() throws Exception { |
305 |
< |
Thread t = new Thread(new CheckedRunnable() { |
304 |
> |
public void testTimedWait_IllegalMonitorException() { |
305 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
306 |
|
public void realRun() throws InterruptedException { |
307 |
|
Object o = new Object(); |
343 |
– |
TimeUnit tu = TimeUnit.MILLISECONDS; |
308 |
|
try { |
309 |
< |
tu.timedWait(o,LONG_DELAY_MS); |
309 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
310 |
|
threadShouldThrow(); |
311 |
< |
} catch (IllegalMonitorStateException success) {}}}); |
311 |
> |
} catch (IllegalMonitorStateException success) {} |
312 |
> |
}}); |
313 |
|
|
314 |
< |
t.start(); |
350 |
< |
Thread.sleep(SHORT_DELAY_MS); |
351 |
< |
t.interrupt(); |
352 |
< |
t.join(); |
314 |
> |
awaitTermination(t); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
|
* timedWait throws InterruptedException when interrupted |
319 |
|
*/ |
320 |
< |
public void testTimedWait() throws InterruptedException { |
321 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
320 |
> |
public void testTimedWait_Interruptible() { |
321 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
322 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
323 |
|
public void realRun() throws InterruptedException { |
324 |
|
Object o = new Object(); |
325 |
|
|
326 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
327 |
< |
synchronized (o) { |
328 |
< |
tu.timedWait(o,MEDIUM_DELAY_MS); |
329 |
< |
} |
326 |
> |
Thread.currentThread().interrupt(); |
327 |
> |
try { |
328 |
> |
synchronized (o) { |
329 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
330 |
> |
} |
331 |
> |
shouldThrow(); |
332 |
> |
} catch (InterruptedException success) {} |
333 |
> |
assertFalse(Thread.interrupted()); |
334 |
> |
|
335 |
> |
pleaseInterrupt.countDown(); |
336 |
> |
try { |
337 |
> |
synchronized (o) { |
338 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
339 |
> |
} |
340 |
> |
shouldThrow(); |
341 |
> |
} catch (InterruptedException success) {} |
342 |
> |
assertFalse(Thread.interrupted()); |
343 |
|
}}); |
344 |
< |
t.start(); |
345 |
< |
Thread.sleep(SHORT_DELAY_MS); |
344 |
> |
|
345 |
> |
await(pleaseInterrupt); |
346 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
347 |
|
t.interrupt(); |
348 |
< |
t.join(); |
348 |
> |
awaitTermination(t); |
349 |
|
} |
350 |
|
|
374 |
– |
|
351 |
|
/** |
352 |
|
* timedJoin throws InterruptedException when interrupted |
353 |
|
*/ |
354 |
< |
public void testTimedJoin() throws InterruptedException { |
355 |
< |
final Thread s = new Thread(new CheckedInterruptedRunnable() { |
354 |
> |
public void testTimedJoin_Interruptible() { |
355 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
356 |
> |
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
357 |
|
public void realRun() throws InterruptedException { |
358 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
358 |
> |
Thread.sleep(LONGER_DELAY_MS); |
359 |
|
}}); |
360 |
< |
final Thread t = new Thread(new CheckedInterruptedRunnable() { |
360 |
> |
final Thread t = newStartedThread(new CheckedRunnable() { |
361 |
|
public void realRun() throws InterruptedException { |
362 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
363 |
< |
tu.timedJoin(s, MEDIUM_DELAY_MS); |
362 |
> |
Thread.currentThread().interrupt(); |
363 |
> |
try { |
364 |
> |
MILLISECONDS.timedJoin(s, LONGER_DELAY_MS); |
365 |
> |
shouldThrow(); |
366 |
> |
} catch (InterruptedException success) {} |
367 |
> |
assertFalse(Thread.interrupted()); |
368 |
> |
|
369 |
> |
pleaseInterrupt.countDown(); |
370 |
> |
try { |
371 |
> |
MILLISECONDS.timedJoin(s, LONGER_DELAY_MS); |
372 |
> |
shouldThrow(); |
373 |
> |
} catch (InterruptedException success) {} |
374 |
> |
assertFalse(Thread.interrupted()); |
375 |
|
}}); |
376 |
< |
s.start(); |
377 |
< |
t.start(); |
378 |
< |
Thread.sleep(SHORT_DELAY_MS); |
376 |
> |
|
377 |
> |
await(pleaseInterrupt); |
378 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
379 |
|
t.interrupt(); |
380 |
< |
t.join(); |
380 |
> |
awaitTermination(t); |
381 |
|
s.interrupt(); |
382 |
< |
s.join(); |
382 |
> |
awaitTermination(s); |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
< |
* timedSleep throws InterruptedException when interrupted |
386 |
> |
* timeUnit.sleep throws InterruptedException when interrupted |
387 |
|
*/ |
388 |
< |
public void testTimedSleep() throws InterruptedException { |
389 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
388 |
> |
public void testTimedSleep_Interruptible() { |
389 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
390 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
391 |
|
public void realRun() throws InterruptedException { |
392 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
393 |
< |
tu.sleep(MEDIUM_DELAY_MS); |
392 |
> |
Thread.currentThread().interrupt(); |
393 |
> |
try { |
394 |
> |
MILLISECONDS.sleep(LONGER_DELAY_MS); |
395 |
> |
shouldThrow(); |
396 |
> |
} catch (InterruptedException success) {} |
397 |
> |
assertFalse(Thread.interrupted()); |
398 |
> |
|
399 |
> |
pleaseInterrupt.countDown(); |
400 |
> |
try { |
401 |
> |
MILLISECONDS.sleep(LONGER_DELAY_MS); |
402 |
> |
shouldThrow(); |
403 |
> |
} catch (InterruptedException success) {} |
404 |
> |
assertFalse(Thread.interrupted()); |
405 |
|
}}); |
406 |
|
|
407 |
< |
t.start(); |
408 |
< |
Thread.sleep(SHORT_DELAY_MS); |
407 |
> |
await(pleaseInterrupt); |
408 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
409 |
|
t.interrupt(); |
410 |
< |
t.join(); |
410 |
> |
awaitTermination(t); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
< |
* a deserialized serialized unit is the same instance |
414 |
> |
* timeUnit.sleep(x) for x <= 0 does not sleep at all. |
415 |
|
*/ |
416 |
< |
public void testSerialization() throws Exception { |
417 |
< |
TimeUnit q = TimeUnit.MILLISECONDS; |
416 |
> |
public void testTimedSleep_nonPositive() throws InterruptedException { |
417 |
> |
boolean interrupt = randomBoolean(); |
418 |
> |
if (interrupt) Thread.currentThread().interrupt(); |
419 |
> |
randomTimeUnit().sleep(0L); |
420 |
> |
randomTimeUnit().sleep(-1L); |
421 |
> |
randomTimeUnit().sleep(Long.MIN_VALUE); |
422 |
> |
if (interrupt) assertTrue(Thread.interrupted()); |
423 |
> |
} |
424 |
|
|
425 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
426 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
427 |
< |
out.writeObject(q); |
428 |
< |
out.close(); |
429 |
< |
|
430 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
425 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
426 |
< |
TimeUnit r = (TimeUnit)in.readObject(); |
427 |
< |
assertSame(q, r); |
425 |
> |
/** |
426 |
> |
* a deserialized/reserialized unit is the same instance |
427 |
> |
*/ |
428 |
> |
public void testSerialization() throws Exception { |
429 |
> |
for (TimeUnit x : TimeUnit.values()) |
430 |
> |
assertSame(x, serialClone(x)); |
431 |
|
} |
432 |
|
|
433 |
|
} |