• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.fail;
36 import static org.threeten.bp.temporal.ChronoUnit.DAYS;
37 import static org.threeten.bp.temporal.ChronoUnit.HALF_DAYS;
38 import static org.threeten.bp.temporal.ChronoUnit.HOURS;
39 import static org.threeten.bp.temporal.ChronoUnit.MICROS;
40 import static org.threeten.bp.temporal.ChronoUnit.MILLIS;
41 import static org.threeten.bp.temporal.ChronoUnit.MINUTES;
42 import static org.threeten.bp.temporal.ChronoUnit.NANOS;
43 import static org.threeten.bp.temporal.ChronoUnit.SECONDS;
44 import static org.threeten.bp.temporal.ChronoUnit.WEEKS;
45 
46 import java.io.ByteArrayInputStream;
47 import java.io.ByteArrayOutputStream;
48 import java.io.ObjectInputStream;
49 import java.io.ObjectOutputStream;
50 import java.util.Locale;
51 
52 import org.testng.annotations.DataProvider;
53 import org.testng.annotations.Test;
54 import org.threeten.bp.format.DateTimeParseException;
55 import org.threeten.bp.temporal.TemporalUnit;
56 
57 /**
58  * Test Duration.
59  */
60 @Test
61 public class TestDuration extends AbstractTest {
62 
63     //-----------------------------------------------------------------------
64     @Test
test_immutable()65     public void test_immutable() {
66         assertImmutable(Duration.class);
67     }
68 
69     @Test
test_serialization()70     public void test_serialization() throws Exception {
71         assertSerializable(Duration.ofHours(5));
72         assertSerializable(Duration.ofHours(-5));
73         assertSerializableAndSame(Duration.ZERO);
74     }
75 
76     @Test
test_serialization_format()77     public void test_serialization_format() throws Exception {
78         assertEqualsSerialisedForm(Duration.ofSeconds(654321, 123456789));
79     }
80 
81     //-----------------------------------------------------------------------
82     // constants
83     //-----------------------------------------------------------------------
84     @Test
test_zero()85     public void test_zero() {
86         assertEquals(Duration.ZERO.getSeconds(), 0L);
87         assertEquals(Duration.ZERO.getNano(), 0);
88     }
89 
90     //-----------------------------------------------------------------------
91     // ofSeconds(long)
92     //-----------------------------------------------------------------------
93     @Test
factory_seconds_long()94     public void factory_seconds_long() {
95         for (long i = -2; i <= 2; i++) {
96             Duration t = Duration.ofSeconds(i);
97             assertEquals(t.getSeconds(), i);
98             assertEquals(t.getNano(), 0);
99         }
100     }
101 
102     //-----------------------------------------------------------------------
103     // ofSeconds(long,long)
104     //-----------------------------------------------------------------------
105     @Test
factory_seconds_long_long()106     public void factory_seconds_long_long() {
107         for (long i = -2; i <= 2; i++) {
108             for (int j = 0; j < 10; j++) {
109                 Duration t = Duration.ofSeconds(i, j);
110                 assertEquals(t.getSeconds(), i);
111                 assertEquals(t.getNano(), j);
112             }
113             for (int j = -10; j < 0; j++) {
114                 Duration t = Duration.ofSeconds(i, j);
115                 assertEquals(t.getSeconds(), i - 1);
116                 assertEquals(t.getNano(), j + 1000000000);
117             }
118             for (int j = 999999990; j < 1000000000; j++) {
119                 Duration t = Duration.ofSeconds(i, j);
120                 assertEquals(t.getSeconds(), i);
121                 assertEquals(t.getNano(), j);
122             }
123         }
124     }
125 
126     @Test
factory_seconds_long_long_nanosNegativeAdjusted()127     public void factory_seconds_long_long_nanosNegativeAdjusted() {
128         Duration test = Duration.ofSeconds(2L, -1);
129         assertEquals(test.getSeconds(), 1);
130         assertEquals(test.getNano(), 999999999);
131     }
132 
133     @Test(expectedExceptions=ArithmeticException.class)
factory_seconds_long_long_tooBig()134     public void factory_seconds_long_long_tooBig() {
135         Duration.ofSeconds(Long.MAX_VALUE, 1000000000);
136     }
137 
138     //-----------------------------------------------------------------------
139     // ofMillis(long)
140     //-----------------------------------------------------------------------
141     @DataProvider(name="MillisDurationNoNanos")
provider_factory_millis_long()142     Object[][] provider_factory_millis_long() {
143         return new Object[][] {
144             {0, 0, 0},
145             {1, 0, 1000000},
146             {2, 0, 2000000},
147             {999, 0, 999000000},
148             {1000, 1, 0},
149             {1001, 1, 1000000},
150             {-1, -1, 999000000},
151             {-2, -1, 998000000},
152             {-999, -1, 1000000},
153             {-1000, -1, 0},
154             {-1001, -2, 999000000},
155         };
156     }
157 
158     @Test(dataProvider="MillisDurationNoNanos")
factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond)159     public void factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond) {
160         Duration test = Duration.ofMillis(millis);
161         assertEquals(test.getSeconds(), expectedSeconds);
162         assertEquals(test.getNano(), expectedNanoOfSecond);
163     }
164 
165     //-----------------------------------------------------------------------
166     // ofNanos(long)
167     //-----------------------------------------------------------------------
168     @Test
factory_nanos_nanos()169     public void factory_nanos_nanos() {
170         Duration test = Duration.ofNanos(1);
171         assertEquals(test.getSeconds(), 0);
172         assertEquals(test.getNano(), 1);
173     }
174 
175     @Test
factory_nanos_nanosSecs()176     public void factory_nanos_nanosSecs() {
177         Duration test = Duration.ofNanos(1000000002);
178         assertEquals(test.getSeconds(), 1);
179         assertEquals(test.getNano(), 2);
180     }
181 
182     @Test
factory_nanos_negative()183     public void factory_nanos_negative() {
184         Duration test = Duration.ofNanos(-2000000001);
185         assertEquals(test.getSeconds(), -3);
186         assertEquals(test.getNano(), 999999999);
187     }
188 
189     @Test
factory_nanos_max()190     public void factory_nanos_max() {
191         Duration test = Duration.ofNanos(Long.MAX_VALUE);
192         assertEquals(test.getSeconds(), Long.MAX_VALUE / 1000000000);
193         assertEquals(test.getNano(), Long.MAX_VALUE % 1000000000);
194     }
195 
196     @Test
factory_nanos_min()197     public void factory_nanos_min() {
198         Duration test = Duration.ofNanos(Long.MIN_VALUE);
199         assertEquals(test.getSeconds(), Long.MIN_VALUE / 1000000000 - 1);
200         assertEquals(test.getNano(), Long.MIN_VALUE % 1000000000 + 1000000000);
201     }
202 
203     //-----------------------------------------------------------------------
204     // ofMinutes()
205     //-----------------------------------------------------------------------
206     @Test
factory_minutes()207     public void factory_minutes() {
208         Duration test = Duration.ofMinutes(2);
209         assertEquals(test.getSeconds(), 120);
210         assertEquals(test.getNano(), 0);
211     }
212 
213     @Test
factory_minutes_max()214     public void factory_minutes_max() {
215         Duration test = Duration.ofMinutes(Long.MAX_VALUE / 60);
216         assertEquals(test.getSeconds(), (Long.MAX_VALUE / 60) * 60);
217         assertEquals(test.getNano(), 0);
218     }
219 
220     @Test
factory_minutes_min()221     public void factory_minutes_min() {
222         Duration test = Duration.ofMinutes(Long.MIN_VALUE / 60);
223         assertEquals(test.getSeconds(), (Long.MIN_VALUE / 60) * 60);
224         assertEquals(test.getNano(), 0);
225     }
226 
227     @Test(expectedExceptions=ArithmeticException.class)
factory_minutes_tooBig()228     public void factory_minutes_tooBig() {
229         Duration.ofMinutes(Long.MAX_VALUE / 60 + 1);
230     }
231 
232     @Test(expectedExceptions=ArithmeticException.class)
factory_minutes_tooSmall()233     public void factory_minutes_tooSmall() {
234         Duration.ofMinutes(Long.MIN_VALUE / 60 - 1);
235     }
236 
237     //-----------------------------------------------------------------------
238     // ofHours()
239     //-----------------------------------------------------------------------
240     @Test
factory_hours()241     public void factory_hours() {
242         Duration test = Duration.ofHours(2);
243         assertEquals(test.getSeconds(), 2 * 3600);
244         assertEquals(test.getNano(), 0);
245     }
246 
247     @Test
factory_hours_max()248     public void factory_hours_max() {
249         Duration test = Duration.ofHours(Long.MAX_VALUE / 3600);
250         assertEquals(test.getSeconds(), (Long.MAX_VALUE / 3600) * 3600);
251         assertEquals(test.getNano(), 0);
252     }
253 
254     @Test
factory_hours_min()255     public void factory_hours_min() {
256         Duration test = Duration.ofHours(Long.MIN_VALUE / 3600);
257         assertEquals(test.getSeconds(), (Long.MIN_VALUE / 3600) * 3600);
258         assertEquals(test.getNano(), 0);
259     }
260 
261     @Test(expectedExceptions=ArithmeticException.class)
factory_hours_tooBig()262     public void factory_hours_tooBig() {
263         Duration.ofHours(Long.MAX_VALUE / 3600 + 1);
264     }
265 
266     @Test(expectedExceptions=ArithmeticException.class)
factory_hours_tooSmall()267     public void factory_hours_tooSmall() {
268         Duration.ofHours(Long.MIN_VALUE / 3600 - 1);
269     }
270 
271     //-----------------------------------------------------------------------
272     // ofDays()
273     //-----------------------------------------------------------------------
274     @Test
factory_days()275     public void factory_days() {
276         Duration test = Duration.ofDays(2);
277         assertEquals(test.getSeconds(), 2 * 86400);
278         assertEquals(test.getNano(), 0);
279     }
280 
281     @Test
factory_days_max()282     public void factory_days_max() {
283         Duration test = Duration.ofDays(Long.MAX_VALUE / 86400);
284         assertEquals(test.getSeconds(), (Long.MAX_VALUE / 86400) * 86400);
285         assertEquals(test.getNano(), 0);
286     }
287 
288     @Test
factory_days_min()289     public void factory_days_min() {
290         Duration test = Duration.ofDays(Long.MIN_VALUE / 86400);
291         assertEquals(test.getSeconds(), (Long.MIN_VALUE / 86400) * 86400);
292         assertEquals(test.getNano(), 0);
293     }
294 
295     @Test(expectedExceptions=ArithmeticException.class)
factory_days_tooBig()296     public void factory_days_tooBig() {
297         Duration.ofDays(Long.MAX_VALUE / 86400 + 1);
298     }
299 
300     @Test(expectedExceptions=ArithmeticException.class)
factory_days_tooSmall()301     public void factory_days_tooSmall() {
302         Duration.ofDays(Long.MIN_VALUE / 86400 - 1);
303     }
304 
305     //-----------------------------------------------------------------------
306     // of(long,TemporalUnit)
307     //-----------------------------------------------------------------------
308     @DataProvider(name="OfTemporalUnit")
provider_factory_of_longTemporalUnit()309     Object[][] provider_factory_of_longTemporalUnit() {
310         return new Object[][] {
311             {0, NANOS, 0, 0},
312             {0, MICROS, 0, 0},
313             {0, MILLIS, 0, 0},
314             {0, SECONDS, 0, 0},
315             {0, MINUTES, 0, 0},
316             {0, HOURS, 0, 0},
317             {0, HALF_DAYS, 0, 0},
318             {0, DAYS, 0, 0},
319             {1, NANOS, 0, 1},
320             {1, MICROS, 0, 1000},
321             {1, MILLIS, 0, 1000000},
322             {1, SECONDS, 1, 0},
323             {1, MINUTES, 60, 0},
324             {1, HOURS, 3600, 0},
325             {1, HALF_DAYS, 43200, 0},
326             {1, DAYS, 86400, 0},
327             {3, NANOS, 0, 3},
328             {3, MICROS, 0, 3000},
329             {3, MILLIS, 0, 3000000},
330             {3, SECONDS, 3, 0},
331             {3, MINUTES, 3 * 60, 0},
332             {3, HOURS, 3 * 3600, 0},
333             {3, HALF_DAYS, 3 * 43200, 0},
334             {3, DAYS, 3 * 86400, 0},
335             {-1, NANOS, -1, 999999999},
336             {-1, MICROS, -1, 999999000},
337             {-1, MILLIS, -1, 999000000},
338             {-1, SECONDS, -1, 0},
339             {-1, MINUTES, -60, 0},
340             {-1, HOURS, -3600, 0},
341             {-1, HALF_DAYS, -43200, 0},
342             {-1, DAYS, -86400, 0},
343             {-3, NANOS, -1, 999999997},
344             {-3, MICROS, -1, 999997000},
345             {-3, MILLIS, -1, 997000000},
346             {-3, SECONDS, -3, 0},
347             {-3, MINUTES, -3 * 60, 0},
348             {-3, HOURS, -3 * 3600, 0},
349             {-3, HALF_DAYS, -3 * 43200, 0},
350             {-3, DAYS, -3 * 86400, 0},
351             {Long.MAX_VALUE, NANOS, Long.MAX_VALUE / 1000000000, (int) (Long.MAX_VALUE % 1000000000)},
352             {Long.MIN_VALUE, NANOS, Long.MIN_VALUE / 1000000000 - 1, (int) (Long.MIN_VALUE % 1000000000 + 1000000000)},
353             {Long.MAX_VALUE, MICROS, Long.MAX_VALUE / 1000000, (int) ((Long.MAX_VALUE % 1000000) * 1000)},
354             {Long.MIN_VALUE, MICROS, Long.MIN_VALUE / 1000000 - 1, (int) ((Long.MIN_VALUE % 1000000 + 1000000) * 1000)},
355             {Long.MAX_VALUE, MILLIS, Long.MAX_VALUE / 1000, (int) ((Long.MAX_VALUE % 1000) * 1000000)},
356             {Long.MIN_VALUE, MILLIS, Long.MIN_VALUE / 1000 - 1, (int) ((Long.MIN_VALUE % 1000 + 1000) * 1000000)},
357             {Long.MAX_VALUE, SECONDS, Long.MAX_VALUE, 0},
358             {Long.MIN_VALUE, SECONDS, Long.MIN_VALUE, 0},
359             {Long.MAX_VALUE / 60, MINUTES, (Long.MAX_VALUE / 60) * 60, 0},
360             {Long.MIN_VALUE / 60, MINUTES, (Long.MIN_VALUE / 60) * 60, 0},
361             {Long.MAX_VALUE / 3600, HOURS, (Long.MAX_VALUE / 3600) * 3600, 0},
362             {Long.MIN_VALUE / 3600, HOURS, (Long.MIN_VALUE / 3600) * 3600, 0},
363             {Long.MAX_VALUE / 43200, HALF_DAYS, (Long.MAX_VALUE / 43200) * 43200, 0},
364             {Long.MIN_VALUE / 43200, HALF_DAYS, (Long.MIN_VALUE / 43200) * 43200, 0},
365         };
366     }
367 
368     @Test(dataProvider="OfTemporalUnit")
factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond)369     public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) {
370         Duration t = Duration.of(amount, unit);
371         assertEquals(t.getSeconds(), expectedSeconds);
372         assertEquals(t.getNano(), expectedNanoOfSecond);
373     }
374 
375     @DataProvider(name="OfTemporalUnitOutOfRange")
provider_factory_of_longTemporalUnit_outOfRange()376     Object[][] provider_factory_of_longTemporalUnit_outOfRange() {
377         return new Object[][] {
378             {Long.MAX_VALUE / 60 + 1, MINUTES},
379             {Long.MIN_VALUE / 60 - 1, MINUTES},
380             {Long.MAX_VALUE / 3600 + 1, HOURS},
381             {Long.MIN_VALUE / 3600 - 1, HOURS},
382             {Long.MAX_VALUE / 43200 + 1, HALF_DAYS},
383             {Long.MIN_VALUE / 43200 - 1, HALF_DAYS},
384         };
385     }
386 
387     @Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit)388     public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
389         Duration.of(amount, unit);
390     }
391 
392     @Test(expectedExceptions=DateTimeException.class)
factory_of_longTemporalUnit_estimatedUnit()393     public void factory_of_longTemporalUnit_estimatedUnit() {
394         Duration.of(2, WEEKS);
395     }
396 
397     @Test(expectedExceptions=NullPointerException.class)
factory_of_longTemporalUnit_null()398     public void factory_of_longTemporalUnit_null() {
399         Duration.of(1, (TemporalUnit) null);
400     }
401 
402     //-----------------------------------------------------------------------
403     // between()
404     //-----------------------------------------------------------------------
405     @DataProvider(name="DurationBetween")
provider_factory_between_Instant_Instant()406     Object[][] provider_factory_between_Instant_Instant() {
407         return new Object[][] {
408             {0, 0, 0, 0, 0, 0},
409             {3, 0, 7, 0, 4, 0},
410             {3, 20, 7, 50, 4, 30},
411             {3, 80, 7, 50, 3, 999999970},
412             {7, 0, 3, 0, -4, 0},
413         };
414     }
415 
416     @Test(dataProvider="DurationBetween")
factory_between_Instant_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond)417     public void factory_between_Instant_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) {
418         Instant start = Instant.ofEpochSecond(secs1, nanos1);
419         Instant end = Instant.ofEpochSecond(secs2, nanos2);
420         Duration t = Duration.between(start, end);
421         assertEquals(t.getSeconds(), expectedSeconds);
422         assertEquals(t.getNano(), expectedNanoOfSecond);
423     }
424 
425     @Test(expectedExceptions=NullPointerException.class)
factory_between_Instant_Instant_startNull()426     public void factory_between_Instant_Instant_startNull() {
427         Instant end = Instant.ofEpochSecond(1);
428         Duration.between(null, end);
429     }
430 
431     @Test(expectedExceptions=NullPointerException.class)
factory_between_Instant_Instant_endNull()432     public void factory_between_Instant_Instant_endNull() {
433         Instant start = Instant.ofEpochSecond(1);
434         Duration.between(start, null);
435     }
436 
437     //-----------------------------------------------------------------------
438     // parse(String)
439     //-----------------------------------------------------------------------
440     @DataProvider(name="Parse")
provider_factory_parse()441     Object[][] provider_factory_parse() {
442         return new Object[][] {
443             {"PT0S", 0, 0},
444 
445             {"PT1S", 1, 0},
446             {"PT12S", 12, 0},
447             {"PT123456789S", 123456789, 0},
448             {"PT" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0},
449 
450             {"PT+1S", 1, 0},
451             {"PT+12S", 12, 0},
452             {"PT-1S", -1, 0},
453             {"PT-12S", -12, 0},
454             {"PT-123456789S", -123456789, 0},
455             {"PT" + Long.MIN_VALUE + "S", Long.MIN_VALUE, 0},
456 
457             {"PT0.1S", 0, 100000000},
458             {"PT1.1S", 1, 100000000},
459             {"PT1.12S", 1, 120000000},
460             {"PT1.123S", 1, 123000000},
461             {"PT1.1234S", 1, 123400000},
462             {"PT1.12345S", 1, 123450000},
463             {"PT1.123456S", 1, 123456000},
464             {"PT1.1234567S", 1, 123456700},
465             {"PT1.12345678S", 1, 123456780},
466             {"PT1.123456789S", 1, 123456789},
467 
468             {"PT-0.1S", -1, 1000000000 - 100000000},
469             {"PT-1.1S", -2, 1000000000 - 100000000},
470             {"PT-1.12S", -2, 1000000000 - 120000000},
471             {"PT-1.123S", -2, 1000000000 - 123000000},
472             {"PT-1.1234S", -2, 1000000000 - 123400000},
473             {"PT-1.12345S", -2, 1000000000 - 123450000},
474             {"PT-1.123456S", -2, 1000000000 - 123456000},
475             {"PT-1.1234567S", -2, 1000000000 - 123456700},
476             {"PT-1.12345678S", -2, 1000000000 - 123456780},
477             {"PT-1.123456789S", -2, 1000000000 - 123456789},
478 
479             {"PT" + Long.MAX_VALUE + ".123456789S", Long.MAX_VALUE, 123456789},
480             {"PT" + Long.MIN_VALUE + ".000000000S", Long.MIN_VALUE, 0},
481 
482             {"PT12M", 12 * 60, 0},
483             {"PT12M0.35S", 12 * 60, 350000000},
484             {"PT12M1.35S", 12 * 60 + 1, 350000000},
485             {"PT12M-0.35S", 12 * 60 - 1, 1000000000 - 350000000},
486             {"PT12M-1.35S", 12 * 60 - 2, 1000000000 - 350000000},
487 
488             {"PT12H", 12 * 3600, 0},
489             {"PT12H0.35S", 12 * 3600, 350000000},
490             {"PT12H1.35S", 12 * 3600 + 1, 350000000},
491             {"PT12H-0.35S", 12 * 3600 - 1, 1000000000 - 350000000},
492             {"PT12H-1.35S", 12 * 3600 - 2, 1000000000 - 350000000},
493 
494             {"P12D", 12 * 24 * 3600, 0},
495             {"P12DT0.35S", 12 * 24 * 3600, 350000000},
496             {"P12DT1.35S", 12 * 24 * 3600 + 1, 350000000},
497             {"P12DT-0.35S", 12 * 24 * 3600 - 1, 1000000000 - 350000000},
498             {"P12DT-1.35S", 12 * 24 * 3600 - 2, 1000000000 - 350000000},
499         };
500     }
501 
502     @Test(dataProvider="Parse")
factory_parse(String text, long expectedSeconds, int expectedNanoOfSecond)503     public void factory_parse(String text, long expectedSeconds, int expectedNanoOfSecond) {
504         Duration t = Duration.parse(text);
505         assertEquals(t.getSeconds(), expectedSeconds);
506         assertEquals(t.getNano(), expectedNanoOfSecond);
507     }
508 
509     @Test(dataProvider="Parse")
factory_parse_ignoreCase(String text, long expectedSeconds, int expectedNanoOfSecond)510     public void factory_parse_ignoreCase(String text, long expectedSeconds, int expectedNanoOfSecond) {
511         Duration t = Duration.parse(text.toLowerCase(Locale.ENGLISH));
512         assertEquals(t.getSeconds(), expectedSeconds);
513         assertEquals(t.getNano(), expectedNanoOfSecond);
514     }
515 
516     @Test(dataProvider="Parse")
factory_parse_comma(String text, long expectedSeconds, int expectedNanoOfSecond)517     public void factory_parse_comma(String text, long expectedSeconds, int expectedNanoOfSecond) {
518         text = text.replace('.', ',');
519         Duration t = Duration.parse(text);
520         assertEquals(t.getSeconds(), expectedSeconds);
521         assertEquals(t.getNano(), expectedNanoOfSecond);
522     }
523 
524     @DataProvider(name="ParseFailures")
provider_factory_parseFailures()525     Object[][] provider_factory_parseFailures() {
526         return new Object[][] {
527             {""},
528             {"PTS"},
529             {"AT0S"},
530             {"PA0S"},
531             {"PT0A"},
532 
533             {"PT+S"},
534             {"PT-S"},
535             {"PT.S"},
536             {"PTAS"},
537 
538             {"PT-.S"},
539             {"PT+.S"},
540 
541             {"PT1ABC2S"},
542             {"PT1.1ABC2S"},
543 
544             {"PT123456789123456789123456789S"},
545             {"PT0.1234567891S"},
546             {"PT.1S"},
547 
548             {"PT2.-3"},
549             {"PT-2.-3"},
550             {"PT2.+3"},
551             {"PT-2.+3"},
552         };
553     }
554 
555     @Test(dataProvider="ParseFailures", expectedExceptions=DateTimeParseException.class)
factory_parseFailures(String text)556     public void factory_parseFailures(String text) {
557         Duration.parse(text);
558     }
559 
560     @Test(dataProvider="ParseFailures", expectedExceptions=DateTimeParseException.class)
factory_parseFailures_comma(String text)561     public void factory_parseFailures_comma(String text) {
562         text = text.replace('.', ',');
563         Duration.parse(text);
564     }
565 
566     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_tooBig()567     public void factory_parse_tooBig() {
568         Duration.parse("PT" + Long.MAX_VALUE + "1S");
569     }
570 
571     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_tooBig_decimal()572     public void factory_parse_tooBig_decimal() {
573         Duration.parse("PT" + Long.MAX_VALUE + "1.1S");
574     }
575 
576     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_tooSmall()577     public void factory_parse_tooSmall() {
578         Duration.parse("PT" + Long.MIN_VALUE + "1S");
579     }
580 
581     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_tooSmall_decimal()582     public void factory_parse_tooSmall_decimal() {
583         Duration.parse("PT" + Long.MIN_VALUE + ".1S");
584     }
585 
586     @Test(expectedExceptions=NullPointerException.class)
factory_parse_nullText()587     public void factory_parse_nullText() {
588         Duration.parse((String) null);
589     }
590 
591     @Test
test_deserialization()592     public void test_deserialization() throws Exception {
593         Duration orginal = Duration.ofSeconds(2);
594         ByteArrayOutputStream baos = new ByteArrayOutputStream();
595         ObjectOutputStream out = new ObjectOutputStream(baos);
596         out.writeObject(orginal);
597         out.close();
598         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
599         ObjectInputStream in = new ObjectInputStream(bais);
600         Duration ser = (Duration) in.readObject();
601         assertEquals(Duration.ofSeconds(2), ser);
602     }
603 
604     //-----------------------------------------------------------------------
605     // isZero(), isPositive(), isPositiveOrZero(), isNegative(), isNegativeOrZero()
606     //-----------------------------------------------------------------------
607     @Test
test_isZero()608     public void test_isZero() {
609         assertEquals(Duration.ofNanos(0).isZero(), true);
610         assertEquals(Duration.ofSeconds(0).isZero(), true);
611         assertEquals(Duration.ofNanos(1).isZero(), false);
612         assertEquals(Duration.ofSeconds(1).isZero(), false);
613         assertEquals(Duration.ofSeconds(1, 1).isZero(), false);
614         assertEquals(Duration.ofNanos(-1).isZero(), false);
615         assertEquals(Duration.ofSeconds(-1).isZero(), false);
616         assertEquals(Duration.ofSeconds(-1, -1).isZero(), false);
617     }
618 
619     @Test
test_isNegative()620     public void test_isNegative() {
621         assertEquals(Duration.ofNanos(0).isNegative(), false);
622         assertEquals(Duration.ofSeconds(0).isNegative(), false);
623         assertEquals(Duration.ofNanos(1).isNegative(), false);
624         assertEquals(Duration.ofSeconds(1).isNegative(), false);
625         assertEquals(Duration.ofSeconds(1, 1).isNegative(), false);
626         assertEquals(Duration.ofNanos(-1).isNegative(), true);
627         assertEquals(Duration.ofSeconds(-1).isNegative(), true);
628         assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
629     }
630 
631     //-----------------------------------------------------------------------
632     // plus()
633     //-----------------------------------------------------------------------
634     @DataProvider(name="Plus")
provider_plus()635     Object[][] provider_plus() {
636         return new Object[][] {
637             {Long.MIN_VALUE, 0, Long.MAX_VALUE, 0, -1, 0},
638 
639             {-4, 666666667, -4, 666666667, -7, 333333334},
640             {-4, 666666667, -3,         0, -7, 666666667},
641             {-4, 666666667, -2,         0, -6, 666666667},
642             {-4, 666666667, -1,         0, -5, 666666667},
643             {-4, 666666667, -1, 333333334, -4,         1},
644             {-4, 666666667, -1, 666666667, -4, 333333334},
645             {-4, 666666667, -1, 999999999, -4, 666666666},
646             {-4, 666666667,  0,         0, -4, 666666667},
647             {-4, 666666667,  0,         1, -4, 666666668},
648             {-4, 666666667,  0, 333333333, -3,         0},
649             {-4, 666666667,  0, 666666666, -3, 333333333},
650             {-4, 666666667,  1,         0, -3, 666666667},
651             {-4, 666666667,  2,         0, -2, 666666667},
652             {-4, 666666667,  3,         0, -1, 666666667},
653             {-4, 666666667,  3, 333333333,  0,         0},
654 
655             {-3, 0, -4, 666666667, -7, 666666667},
656             {-3, 0, -3,         0, -6,         0},
657             {-3, 0, -2,         0, -5,         0},
658             {-3, 0, -1,         0, -4,         0},
659             {-3, 0, -1, 333333334, -4, 333333334},
660             {-3, 0, -1, 666666667, -4, 666666667},
661             {-3, 0, -1, 999999999, -4, 999999999},
662             {-3, 0,  0,         0, -3,         0},
663             {-3, 0,  0,         1, -3,         1},
664             {-3, 0,  0, 333333333, -3, 333333333},
665             {-3, 0,  0, 666666666, -3, 666666666},
666             {-3, 0,  1,         0, -2,         0},
667             {-3, 0,  2,         0, -1,         0},
668             {-3, 0,  3,         0,  0,         0},
669             {-3, 0,  3, 333333333,  0, 333333333},
670 
671             {-2, 0, -4, 666666667, -6, 666666667},
672             {-2, 0, -3,         0, -5,         0},
673             {-2, 0, -2,         0, -4,         0},
674             {-2, 0, -1,         0, -3,         0},
675             {-2, 0, -1, 333333334, -3, 333333334},
676             {-2, 0, -1, 666666667, -3, 666666667},
677             {-2, 0, -1, 999999999, -3, 999999999},
678             {-2, 0,  0,         0, -2,         0},
679             {-2, 0,  0,         1, -2,         1},
680             {-2, 0,  0, 333333333, -2, 333333333},
681             {-2, 0,  0, 666666666, -2, 666666666},
682             {-2, 0,  1,         0, -1,         0},
683             {-2, 0,  2,         0,  0,         0},
684             {-2, 0,  3,         0,  1,         0},
685             {-2, 0,  3, 333333333,  1, 333333333},
686 
687             {-1, 0, -4, 666666667, -5, 666666667},
688             {-1, 0, -3,         0, -4,         0},
689             {-1, 0, -2,         0, -3,         0},
690             {-1, 0, -1,         0, -2,         0},
691             {-1, 0, -1, 333333334, -2, 333333334},
692             {-1, 0, -1, 666666667, -2, 666666667},
693             {-1, 0, -1, 999999999, -2, 999999999},
694             {-1, 0,  0,         0, -1,         0},
695             {-1, 0,  0,         1, -1,         1},
696             {-1, 0,  0, 333333333, -1, 333333333},
697             {-1, 0,  0, 666666666, -1, 666666666},
698             {-1, 0,  1,         0,  0,         0},
699             {-1, 0,  2,         0,  1,         0},
700             {-1, 0,  3,         0,  2,         0},
701             {-1, 0,  3, 333333333,  2, 333333333},
702 
703             {-1, 666666667, -4, 666666667, -4, 333333334},
704             {-1, 666666667, -3,         0, -4, 666666667},
705             {-1, 666666667, -2,         0, -3, 666666667},
706             {-1, 666666667, -1,         0, -2, 666666667},
707             {-1, 666666667, -1, 333333334, -1,         1},
708             {-1, 666666667, -1, 666666667, -1, 333333334},
709             {-1, 666666667, -1, 999999999, -1, 666666666},
710             {-1, 666666667,  0,         0, -1, 666666667},
711             {-1, 666666667,  0,         1, -1, 666666668},
712             {-1, 666666667,  0, 333333333,  0,         0},
713             {-1, 666666667,  0, 666666666,  0, 333333333},
714             {-1, 666666667,  1,         0,  0, 666666667},
715             {-1, 666666667,  2,         0,  1, 666666667},
716             {-1, 666666667,  3,         0,  2, 666666667},
717             {-1, 666666667,  3, 333333333,  3,         0},
718 
719             {0, 0, -4, 666666667, -4, 666666667},
720             {0, 0, -3,         0, -3,         0},
721             {0, 0, -2,         0, -2,         0},
722             {0, 0, -1,         0, -1,         0},
723             {0, 0, -1, 333333334, -1, 333333334},
724             {0, 0, -1, 666666667, -1, 666666667},
725             {0, 0, -1, 999999999, -1, 999999999},
726             {0, 0,  0,         0,  0,         0},
727             {0, 0,  0,         1,  0,         1},
728             {0, 0,  0, 333333333,  0, 333333333},
729             {0, 0,  0, 666666666,  0, 666666666},
730             {0, 0,  1,         0,  1,         0},
731             {0, 0,  2,         0,  2,         0},
732             {0, 0,  3,         0,  3,         0},
733             {0, 0,  3, 333333333,  3, 333333333},
734 
735             {0, 333333333, -4, 666666667, -3,         0},
736             {0, 333333333, -3,         0, -3, 333333333},
737             {0, 333333333, -2,         0, -2, 333333333},
738             {0, 333333333, -1,         0, -1, 333333333},
739             {0, 333333333, -1, 333333334, -1, 666666667},
740             {0, 333333333, -1, 666666667,  0,         0},
741             {0, 333333333, -1, 999999999,  0, 333333332},
742             {0, 333333333,  0,         0,  0, 333333333},
743             {0, 333333333,  0,         1,  0, 333333334},
744             {0, 333333333,  0, 333333333,  0, 666666666},
745             {0, 333333333,  0, 666666666,  0, 999999999},
746             {0, 333333333,  1,         0,  1, 333333333},
747             {0, 333333333,  2,         0,  2, 333333333},
748             {0, 333333333,  3,         0,  3, 333333333},
749             {0, 333333333,  3, 333333333,  3, 666666666},
750 
751             {1, 0, -4, 666666667, -3, 666666667},
752             {1, 0, -3,         0, -2,         0},
753             {1, 0, -2,         0, -1,         0},
754             {1, 0, -1,         0,  0,         0},
755             {1, 0, -1, 333333334,  0, 333333334},
756             {1, 0, -1, 666666667,  0, 666666667},
757             {1, 0, -1, 999999999,  0, 999999999},
758             {1, 0,  0,         0,  1,         0},
759             {1, 0,  0,         1,  1,         1},
760             {1, 0,  0, 333333333,  1, 333333333},
761             {1, 0,  0, 666666666,  1, 666666666},
762             {1, 0,  1,         0,  2,         0},
763             {1, 0,  2,         0,  3,         0},
764             {1, 0,  3,         0,  4,         0},
765             {1, 0,  3, 333333333,  4, 333333333},
766 
767             {2, 0, -4, 666666667, -2, 666666667},
768             {2, 0, -3,         0, -1,         0},
769             {2, 0, -2,         0,  0,         0},
770             {2, 0, -1,         0,  1,         0},
771             {2, 0, -1, 333333334,  1, 333333334},
772             {2, 0, -1, 666666667,  1, 666666667},
773             {2, 0, -1, 999999999,  1, 999999999},
774             {2, 0,  0,         0,  2,         0},
775             {2, 0,  0,         1,  2,         1},
776             {2, 0,  0, 333333333,  2, 333333333},
777             {2, 0,  0, 666666666,  2, 666666666},
778             {2, 0,  1,         0,  3,         0},
779             {2, 0,  2,         0,  4,         0},
780             {2, 0,  3,         0,  5,         0},
781             {2, 0,  3, 333333333,  5, 333333333},
782 
783             {3, 0, -4, 666666667, -1, 666666667},
784             {3, 0, -3,         0,  0,         0},
785             {3, 0, -2,         0,  1,         0},
786             {3, 0, -1,         0,  2,         0},
787             {3, 0, -1, 333333334,  2, 333333334},
788             {3, 0, -1, 666666667,  2, 666666667},
789             {3, 0, -1, 999999999,  2, 999999999},
790             {3, 0,  0,         0,  3,         0},
791             {3, 0,  0,         1,  3,         1},
792             {3, 0,  0, 333333333,  3, 333333333},
793             {3, 0,  0, 666666666,  3, 666666666},
794             {3, 0,  1,         0,  4,         0},
795             {3, 0,  2,         0,  5,         0},
796             {3, 0,  3,         0,  6,         0},
797             {3, 0,  3, 333333333,  6, 333333333},
798 
799             {3, 333333333, -4, 666666667,  0,         0},
800             {3, 333333333, -3,         0,  0, 333333333},
801             {3, 333333333, -2,         0,  1, 333333333},
802             {3, 333333333, -1,         0,  2, 333333333},
803             {3, 333333333, -1, 333333334,  2, 666666667},
804             {3, 333333333, -1, 666666667,  3,         0},
805             {3, 333333333, -1, 999999999,  3, 333333332},
806             {3, 333333333,  0,         0,  3, 333333333},
807             {3, 333333333,  0,         1,  3, 333333334},
808             {3, 333333333,  0, 333333333,  3, 666666666},
809             {3, 333333333,  0, 666666666,  3, 999999999},
810             {3, 333333333,  1,         0,  4, 333333333},
811             {3, 333333333,  2,         0,  5, 333333333},
812             {3, 333333333,  3,         0,  6, 333333333},
813             {3, 333333333,  3, 333333333,  6, 666666666},
814 
815             {Long.MAX_VALUE, 0, Long.MIN_VALUE, 0, -1, 0},
816        };
817     }
818 
819     @Test(dataProvider="Plus")
plus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond)820     public void plus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
821        Duration t = Duration.ofSeconds(seconds, nanos).plus(Duration.ofSeconds(otherSeconds, otherNanos));
822        assertEquals(t.getSeconds(), expectedSeconds);
823        assertEquals(t.getNano(), expectedNanoOfSecond);
824     }
825 
826     @Test(expectedExceptions=ArithmeticException.class)
plusOverflowTooBig()827     public void plusOverflowTooBig() {
828        Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
829        t.plus(Duration.ofSeconds(0, 1));
830     }
831 
832     @Test(expectedExceptions=ArithmeticException.class)
plusOverflowTooSmall()833     public void plusOverflowTooSmall() {
834        Duration t = Duration.ofSeconds(Long.MIN_VALUE);
835        t.plus(Duration.ofSeconds(-1, 999999999));
836     }
837 
838     //-----------------------------------------------------------------------
839     @Test
plus_longTemporalUnit_seconds()840     public void plus_longTemporalUnit_seconds() {
841         Duration t = Duration.ofSeconds(1);
842         t = t.plus(1, SECONDS);
843         assertEquals(2, t.getSeconds());
844         assertEquals(0, t.getNano());
845      }
846 
847     @Test
plus_longTemporalUnit_millis()848     public void plus_longTemporalUnit_millis() {
849         Duration t = Duration.ofSeconds(1);
850         t = t.plus(1, MILLIS);
851         assertEquals(1, t.getSeconds());
852         assertEquals(1000000, t.getNano());
853      }
854 
855     @Test
plus_longTemporalUnit_micros()856     public void plus_longTemporalUnit_micros() {
857         Duration t = Duration.ofSeconds(1);
858         t = t.plus(1, MICROS);
859         assertEquals(1, t.getSeconds());
860         assertEquals(1000, t.getNano());
861      }
862 
863     @Test
plus_longTemporalUnit_nanos()864     public void plus_longTemporalUnit_nanos() {
865         Duration t = Duration.ofSeconds(1);
866         t = t.plus(1, NANOS);
867         assertEquals(1, t.getSeconds());
868         assertEquals(1, t.getNano());
869      }
870 
871     @Test(expectedExceptions=NullPointerException.class)
plus_longTemporalUnit_null()872     public void plus_longTemporalUnit_null() {
873        Duration t = Duration.ofSeconds(1);
874        t.plus(1, (TemporalUnit) null);
875     }
876 
877     //-----------------------------------------------------------------------
878     @DataProvider(name="PlusSeconds")
provider_plusSeconds_long()879     Object[][] provider_plusSeconds_long() {
880         return new Object[][] {
881             {0, 0, 0, 0, 0},
882             {0, 0, 1, 1, 0},
883             {0, 0, -1, -1, 0},
884             {0, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0},
885             {0, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0},
886             {1, 0, 0, 1, 0},
887             {1, 0, 1, 2, 0},
888             {1, 0, -1, 0, 0},
889             {1, 0, Long.MAX_VALUE - 1, Long.MAX_VALUE, 0},
890             {1, 0, Long.MIN_VALUE, Long.MIN_VALUE + 1, 0},
891             {1, 1, 0, 1, 1},
892             {1, 1, 1, 2, 1},
893             {1, 1, -1, 0, 1},
894             {1, 1, Long.MAX_VALUE - 1, Long.MAX_VALUE, 1},
895             {1, 1, Long.MIN_VALUE, Long.MIN_VALUE + 1, 1},
896             {-1, 1, 0, -1, 1},
897             {-1, 1, 1, 0, 1},
898             {-1, 1, -1, -2, 1},
899             {-1, 1, Long.MAX_VALUE, Long.MAX_VALUE - 1, 1},
900             {-1, 1, Long.MIN_VALUE + 1, Long.MIN_VALUE, 1},
901         };
902     }
903 
904     @Test(dataProvider="PlusSeconds")
plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)905     public void plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
906         Duration t = Duration.ofSeconds(seconds, nanos);
907         t = t.plusSeconds(amount);
908         assertEquals(t.getSeconds(), expectedSeconds);
909         assertEquals(t.getNano(), expectedNanoOfSecond);
910     }
911 
912     @Test(expectedExceptions = {ArithmeticException.class})
plusSeconds_long_overflowTooBig()913     public void plusSeconds_long_overflowTooBig() {
914         Duration t = Duration.ofSeconds(1, 0);
915         t.plusSeconds(Long.MAX_VALUE);
916     }
917 
918     @Test(expectedExceptions = {ArithmeticException.class})
plusSeconds_long_overflowTooSmall()919     public void plusSeconds_long_overflowTooSmall() {
920         Duration t = Duration.ofSeconds(-1, 0);
921         t.plusSeconds(Long.MIN_VALUE);
922     }
923 
924     //-----------------------------------------------------------------------
925     @DataProvider(name="PlusMillis")
provider_plusMillis_long()926     Object[][] provider_plusMillis_long() {
927         return new Object[][] {
928             {0, 0, 0,       0, 0},
929             {0, 0, 1,       0, 1000000},
930             {0, 0, 999,     0, 999000000},
931             {0, 0, 1000,    1, 0},
932             {0, 0, 1001,    1, 1000000},
933             {0, 0, 1999,    1, 999000000},
934             {0, 0, 2000,    2, 0},
935             {0, 0, -1,      -1, 999000000},
936             {0, 0, -999,    -1, 1000000},
937             {0, 0, -1000,   -1, 0},
938             {0, 0, -1001,   -2, 999000000},
939             {0, 0, -1999,   -2, 1000000},
940 
941             {0, 1, 0,       0, 1},
942             {0, 1, 1,       0, 1000001},
943             {0, 1, 998,     0, 998000001},
944             {0, 1, 999,     0, 999000001},
945             {0, 1, 1000,    1, 1},
946             {0, 1, 1998,    1, 998000001},
947             {0, 1, 1999,    1, 999000001},
948             {0, 1, 2000,    2, 1},
949             {0, 1, -1,      -1, 999000001},
950             {0, 1, -2,      -1, 998000001},
951             {0, 1, -1000,   -1, 1},
952             {0, 1, -1001,   -2, 999000001},
953 
954             {0, 1000000, 0,       0, 1000000},
955             {0, 1000000, 1,       0, 2000000},
956             {0, 1000000, 998,     0, 999000000},
957             {0, 1000000, 999,     1, 0},
958             {0, 1000000, 1000,    1, 1000000},
959             {0, 1000000, 1998,    1, 999000000},
960             {0, 1000000, 1999,    2, 0},
961             {0, 1000000, 2000,    2, 1000000},
962             {0, 1000000, -1,      0, 0},
963             {0, 1000000, -2,      -1, 999000000},
964             {0, 1000000, -999,    -1, 2000000},
965             {0, 1000000, -1000,   -1, 1000000},
966             {0, 1000000, -1001,   -1, 0},
967             {0, 1000000, -1002,   -2, 999000000},
968 
969             {0, 999999999, 0,     0, 999999999},
970             {0, 999999999, 1,     1, 999999},
971             {0, 999999999, 999,   1, 998999999},
972             {0, 999999999, 1000,  1, 999999999},
973             {0, 999999999, 1001,  2, 999999},
974             {0, 999999999, -1,    0, 998999999},
975             {0, 999999999, -1000, -1, 999999999},
976             {0, 999999999, -1001, -1, 998999999},
977         };
978     }
979 
980     @Test(dataProvider="PlusMillis")
plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)981     public void plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
982         Duration t = Duration.ofSeconds(seconds, nanos);
983         t = t.plusMillis(amount);
984         assertEquals(t.getSeconds(), expectedSeconds);
985         assertEquals(t.getNano(), expectedNanoOfSecond);
986     }
987     @Test(dataProvider="PlusMillis")
plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)988     public void plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
989         Duration t = Duration.ofSeconds(seconds + 1, nanos);
990         t = t.plusMillis(amount);
991         assertEquals(t.getSeconds(), expectedSeconds + 1);
992         assertEquals(t.getNano(), expectedNanoOfSecond);
993     }
994     @Test(dataProvider="PlusMillis")
plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)995     public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
996         Duration t = Duration.ofSeconds(seconds - 1, nanos);
997         t = t.plusMillis(amount);
998         assertEquals(t.getSeconds(), expectedSeconds - 1);
999         assertEquals(t.getNano(), expectedNanoOfSecond);
1000     }
1001 
1002     @Test
plusMillis_long_max()1003     public void plusMillis_long_max() {
1004         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 998999999);
1005         t = t.plusMillis(1);
1006         assertEquals(t.getSeconds(), Long.MAX_VALUE);
1007         assertEquals(t.getNano(), 999999999);
1008     }
1009 
1010     @Test(expectedExceptions = {ArithmeticException.class})
plusMillis_long_overflowTooBig()1011     public void plusMillis_long_overflowTooBig() {
1012         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999000000);
1013         t.plusMillis(1);
1014     }
1015 
1016     @Test
plusMillis_long_min()1017     public void plusMillis_long_min() {
1018         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 1000000);
1019         t = t.plusMillis(-1);
1020         assertEquals(t.getSeconds(), Long.MIN_VALUE);
1021         assertEquals(t.getNano(), 0);
1022     }
1023 
1024     @Test(expectedExceptions = {ArithmeticException.class})
plusMillis_long_overflowTooSmall()1025     public void plusMillis_long_overflowTooSmall() {
1026         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
1027         t.plusMillis(-1);
1028     }
1029 
1030     //-----------------------------------------------------------------------
1031     @DataProvider(name="PlusNanos")
provider_plusNanos_long()1032     Object[][] provider_plusNanos_long() {
1033         return new Object[][] {
1034             {0, 0, 0,           0, 0},
1035             {0, 0, 1,           0, 1},
1036             {0, 0, 999999999,   0, 999999999},
1037             {0, 0, 1000000000,  1, 0},
1038             {0, 0, 1000000001,  1, 1},
1039             {0, 0, 1999999999,  1, 999999999},
1040             {0, 0, 2000000000,  2, 0},
1041             {0, 0, -1,          -1, 999999999},
1042             {0, 0, -999999999,  -1, 1},
1043             {0, 0, -1000000000, -1, 0},
1044             {0, 0, -1000000001, -2, 999999999},
1045             {0, 0, -1999999999, -2, 1},
1046 
1047             {1, 0, 0,           1, 0},
1048             {1, 0, 1,           1, 1},
1049             {1, 0, 999999999,   1, 999999999},
1050             {1, 0, 1000000000,  2, 0},
1051             {1, 0, 1000000001,  2, 1},
1052             {1, 0, 1999999999,  2, 999999999},
1053             {1, 0, 2000000000,  3, 0},
1054             {1, 0, -1,          0, 999999999},
1055             {1, 0, -999999999,  0, 1},
1056             {1, 0, -1000000000, 0, 0},
1057             {1, 0, -1000000001, -1, 999999999},
1058             {1, 0, -1999999999, -1, 1},
1059 
1060             {-1, 0, 0,           -1, 0},
1061             {-1, 0, 1,           -1, 1},
1062             {-1, 0, 999999999,   -1, 999999999},
1063             {-1, 0, 1000000000,  0, 0},
1064             {-1, 0, 1000000001,  0, 1},
1065             {-1, 0, 1999999999,  0, 999999999},
1066             {-1, 0, 2000000000,  1, 0},
1067             {-1, 0, -1,          -2, 999999999},
1068             {-1, 0, -999999999,  -2, 1},
1069             {-1, 0, -1000000000, -2, 0},
1070             {-1, 0, -1000000001, -3, 999999999},
1071             {-1, 0, -1999999999, -3, 1},
1072 
1073             {1, 1, 0,           1, 1},
1074             {1, 1, 1,           1, 2},
1075             {1, 1, 999999998,   1, 999999999},
1076             {1, 1, 999999999,   2, 0},
1077             {1, 1, 1000000000,  2, 1},
1078             {1, 1, 1999999998,  2, 999999999},
1079             {1, 1, 1999999999,  3, 0},
1080             {1, 1, 2000000000,  3, 1},
1081             {1, 1, -1,          1, 0},
1082             {1, 1, -2,          0, 999999999},
1083             {1, 1, -1000000000, 0, 1},
1084             {1, 1, -1000000001, 0, 0},
1085             {1, 1, -1000000002, -1, 999999999},
1086             {1, 1, -2000000000, -1, 1},
1087 
1088             {1, 999999999, 0,           1, 999999999},
1089             {1, 999999999, 1,           2, 0},
1090             {1, 999999999, 999999999,   2, 999999998},
1091             {1, 999999999, 1000000000,  2, 999999999},
1092             {1, 999999999, 1000000001,  3, 0},
1093             {1, 999999999, -1,          1, 999999998},
1094             {1, 999999999, -1000000000, 0, 999999999},
1095             {1, 999999999, -1000000001, 0, 999999998},
1096             {1, 999999999, -1999999999, 0, 0},
1097             {1, 999999999, -2000000000, -1, 999999999},
1098 
1099             {Long.MAX_VALUE, 0, 999999999, Long.MAX_VALUE, 999999999},
1100             {Long.MAX_VALUE - 1, 0, 1999999999, Long.MAX_VALUE, 999999999},
1101             {Long.MIN_VALUE, 1, -1, Long.MIN_VALUE, 0},
1102             {Long.MIN_VALUE + 1, 1, -1000000001, Long.MIN_VALUE, 0},
1103         };
1104     }
1105 
1106     @Test(dataProvider="PlusNanos")
plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1107     public void plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1108         Duration t = Duration.ofSeconds(seconds, nanos);
1109         t = t.plusNanos(amount);
1110         assertEquals(t.getSeconds(), expectedSeconds);
1111         assertEquals(t.getNano(), expectedNanoOfSecond);
1112     }
1113 
1114     @Test(expectedExceptions = {ArithmeticException.class})
plusNanos_long_overflowTooBig()1115     public void plusNanos_long_overflowTooBig() {
1116         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1117         t.plusNanos(1);
1118     }
1119 
1120     @Test(expectedExceptions = {ArithmeticException.class})
plusNanos_long_overflowTooSmall()1121     public void plusNanos_long_overflowTooSmall() {
1122         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
1123         t.plusNanos(-1);
1124     }
1125 
1126     //-----------------------------------------------------------------------
1127     @DataProvider(name="Minus")
provider_minus()1128     Object[][] provider_minus() {
1129         return new Object[][] {
1130             {Long.MIN_VALUE, 0, Long.MIN_VALUE + 1, 0, -1, 0},
1131 
1132             {-4, 666666667, -4, 666666667,  0,         0},
1133             {-4, 666666667, -3,         0, -1, 666666667},
1134             {-4, 666666667, -2,         0, -2, 666666667},
1135             {-4, 666666667, -1,         0, -3, 666666667},
1136             {-4, 666666667, -1, 333333334, -3, 333333333},
1137             {-4, 666666667, -1, 666666667, -3,         0},
1138             {-4, 666666667, -1, 999999999, -4, 666666668},
1139             {-4, 666666667,  0,         0, -4, 666666667},
1140             {-4, 666666667,  0,         1, -4, 666666666},
1141             {-4, 666666667,  0, 333333333, -4, 333333334},
1142             {-4, 666666667,  0, 666666666, -4,         1},
1143             {-4, 666666667,  1,         0, -5, 666666667},
1144             {-4, 666666667,  2,         0, -6, 666666667},
1145             {-4, 666666667,  3,         0, -7, 666666667},
1146             {-4, 666666667,  3, 333333333, -7, 333333334},
1147 
1148             {-3, 0, -4, 666666667,  0, 333333333},
1149             {-3, 0, -3,         0,  0,         0},
1150             {-3, 0, -2,         0, -1,         0},
1151             {-3, 0, -1,         0, -2,         0},
1152             {-3, 0, -1, 333333334, -3, 666666666},
1153             {-3, 0, -1, 666666667, -3, 333333333},
1154             {-3, 0, -1, 999999999, -3,         1},
1155             {-3, 0,  0,         0, -3,         0},
1156             {-3, 0,  0,         1, -4, 999999999},
1157             {-3, 0,  0, 333333333, -4, 666666667},
1158             {-3, 0,  0, 666666666, -4, 333333334},
1159             {-3, 0,  1,         0, -4,         0},
1160             {-3, 0,  2,         0, -5,         0},
1161             {-3, 0,  3,         0, -6,         0},
1162             {-3, 0,  3, 333333333, -7, 666666667},
1163 
1164             {-2, 0, -4, 666666667,  1, 333333333},
1165             {-2, 0, -3,         0,  1,         0},
1166             {-2, 0, -2,         0,  0,         0},
1167             {-2, 0, -1,         0, -1,         0},
1168             {-2, 0, -1, 333333334, -2, 666666666},
1169             {-2, 0, -1, 666666667, -2, 333333333},
1170             {-2, 0, -1, 999999999, -2,         1},
1171             {-2, 0,  0,         0, -2,         0},
1172             {-2, 0,  0,         1, -3, 999999999},
1173             {-2, 0,  0, 333333333, -3, 666666667},
1174             {-2, 0,  0, 666666666, -3, 333333334},
1175             {-2, 0,  1,         0, -3,         0},
1176             {-2, 0,  2,         0, -4,         0},
1177             {-2, 0,  3,         0, -5,         0},
1178             {-2, 0,  3, 333333333, -6, 666666667},
1179 
1180             {-1, 0, -4, 666666667,  2, 333333333},
1181             {-1, 0, -3,         0,  2,         0},
1182             {-1, 0, -2,         0,  1,         0},
1183             {-1, 0, -1,         0,  0,         0},
1184             {-1, 0, -1, 333333334, -1, 666666666},
1185             {-1, 0, -1, 666666667, -1, 333333333},
1186             {-1, 0, -1, 999999999, -1,         1},
1187             {-1, 0,  0,         0, -1,         0},
1188             {-1, 0,  0,         1, -2, 999999999},
1189             {-1, 0,  0, 333333333, -2, 666666667},
1190             {-1, 0,  0, 666666666, -2, 333333334},
1191             {-1, 0,  1,         0, -2,         0},
1192             {-1, 0,  2,         0, -3,         0},
1193             {-1, 0,  3,         0, -4,         0},
1194             {-1, 0,  3, 333333333, -5, 666666667},
1195 
1196             {-1, 666666667, -4, 666666667,  3,         0},
1197             {-1, 666666667, -3,         0,  2, 666666667},
1198             {-1, 666666667, -2,         0,  1, 666666667},
1199             {-1, 666666667, -1,         0,  0, 666666667},
1200             {-1, 666666667, -1, 333333334,  0, 333333333},
1201             {-1, 666666667, -1, 666666667,  0,         0},
1202             {-1, 666666667, -1, 999999999, -1, 666666668},
1203             {-1, 666666667,  0,         0, -1, 666666667},
1204             {-1, 666666667,  0,         1, -1, 666666666},
1205             {-1, 666666667,  0, 333333333, -1, 333333334},
1206             {-1, 666666667,  0, 666666666, -1,         1},
1207             {-1, 666666667,  1,         0, -2, 666666667},
1208             {-1, 666666667,  2,         0, -3, 666666667},
1209             {-1, 666666667,  3,         0, -4, 666666667},
1210             {-1, 666666667,  3, 333333333, -4, 333333334},
1211 
1212             {0, 0, -4, 666666667,  3, 333333333},
1213             {0, 0, -3,         0,  3,         0},
1214             {0, 0, -2,         0,  2,         0},
1215             {0, 0, -1,         0,  1,         0},
1216             {0, 0, -1, 333333334,  0, 666666666},
1217             {0, 0, -1, 666666667,  0, 333333333},
1218             {0, 0, -1, 999999999,  0,         1},
1219             {0, 0,  0,         0,  0,         0},
1220             {0, 0,  0,         1, -1, 999999999},
1221             {0, 0,  0, 333333333, -1, 666666667},
1222             {0, 0,  0, 666666666, -1, 333333334},
1223             {0, 0,  1,         0, -1,         0},
1224             {0, 0,  2,         0, -2,         0},
1225             {0, 0,  3,         0, -3,         0},
1226             {0, 0,  3, 333333333, -4, 666666667},
1227 
1228             {0, 333333333, -4, 666666667,  3, 666666666},
1229             {0, 333333333, -3,         0,  3, 333333333},
1230             {0, 333333333, -2,         0,  2, 333333333},
1231             {0, 333333333, -1,         0,  1, 333333333},
1232             {0, 333333333, -1, 333333334,  0, 999999999},
1233             {0, 333333333, -1, 666666667,  0, 666666666},
1234             {0, 333333333, -1, 999999999,  0, 333333334},
1235             {0, 333333333,  0,         0,  0, 333333333},
1236             {0, 333333333,  0,         1,  0, 333333332},
1237             {0, 333333333,  0, 333333333,  0,         0},
1238             {0, 333333333,  0, 666666666, -1, 666666667},
1239             {0, 333333333,  1,         0, -1, 333333333},
1240             {0, 333333333,  2,         0, -2, 333333333},
1241             {0, 333333333,  3,         0, -3, 333333333},
1242             {0, 333333333,  3, 333333333, -3,         0},
1243 
1244             {1, 0, -4, 666666667,  4, 333333333},
1245             {1, 0, -3,         0,  4,         0},
1246             {1, 0, -2,         0,  3,         0},
1247             {1, 0, -1,         0,  2,         0},
1248             {1, 0, -1, 333333334,  1, 666666666},
1249             {1, 0, -1, 666666667,  1, 333333333},
1250             {1, 0, -1, 999999999,  1,         1},
1251             {1, 0,  0,         0,  1,         0},
1252             {1, 0,  0,         1,  0, 999999999},
1253             {1, 0,  0, 333333333,  0, 666666667},
1254             {1, 0,  0, 666666666,  0, 333333334},
1255             {1, 0,  1,         0,  0,         0},
1256             {1, 0,  2,         0, -1,         0},
1257             {1, 0,  3,         0, -2,         0},
1258             {1, 0,  3, 333333333, -3, 666666667},
1259 
1260             {2, 0, -4, 666666667,  5, 333333333},
1261             {2, 0, -3,         0,  5,         0},
1262             {2, 0, -2,         0,  4,         0},
1263             {2, 0, -1,         0,  3,         0},
1264             {2, 0, -1, 333333334,  2, 666666666},
1265             {2, 0, -1, 666666667,  2, 333333333},
1266             {2, 0, -1, 999999999,  2,         1},
1267             {2, 0,  0,         0,  2,         0},
1268             {2, 0,  0,         1,  1, 999999999},
1269             {2, 0,  0, 333333333,  1, 666666667},
1270             {2, 0,  0, 666666666,  1, 333333334},
1271             {2, 0,  1,         0,  1,         0},
1272             {2, 0,  2,         0,  0,         0},
1273             {2, 0,  3,         0, -1,         0},
1274             {2, 0,  3, 333333333, -2, 666666667},
1275 
1276             {3, 0, -4, 666666667,  6, 333333333},
1277             {3, 0, -3,         0,  6,         0},
1278             {3, 0, -2,         0,  5,         0},
1279             {3, 0, -1,         0,  4,         0},
1280             {3, 0, -1, 333333334,  3, 666666666},
1281             {3, 0, -1, 666666667,  3, 333333333},
1282             {3, 0, -1, 999999999,  3,         1},
1283             {3, 0,  0,         0,  3,         0},
1284             {3, 0,  0,         1,  2, 999999999},
1285             {3, 0,  0, 333333333,  2, 666666667},
1286             {3, 0,  0, 666666666,  2, 333333334},
1287             {3, 0,  1,         0,  2,         0},
1288             {3, 0,  2,         0,  1,         0},
1289             {3, 0,  3,         0,  0,         0},
1290             {3, 0,  3, 333333333, -1, 666666667},
1291 
1292             {3, 333333333, -4, 666666667,  6, 666666666},
1293             {3, 333333333, -3,         0,  6, 333333333},
1294             {3, 333333333, -2,         0,  5, 333333333},
1295             {3, 333333333, -1,         0,  4, 333333333},
1296             {3, 333333333, -1, 333333334,  3, 999999999},
1297             {3, 333333333, -1, 666666667,  3, 666666666},
1298             {3, 333333333, -1, 999999999,  3, 333333334},
1299             {3, 333333333,  0,         0,  3, 333333333},
1300             {3, 333333333,  0,         1,  3, 333333332},
1301             {3, 333333333,  0, 333333333,  3,         0},
1302             {3, 333333333,  0, 666666666,  2, 666666667},
1303             {3, 333333333,  1,         0,  2, 333333333},
1304             {3, 333333333,  2,         0,  1, 333333333},
1305             {3, 333333333,  3,         0,  0, 333333333},
1306             {3, 333333333,  3, 333333333,  0,         0},
1307 
1308             {Long.MAX_VALUE, 0, Long.MAX_VALUE, 0, 0, 0},
1309        };
1310     }
1311 
1312     @Test(dataProvider="Minus")
minus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond)1313     public void minus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
1314        Duration t = Duration.ofSeconds(seconds, nanos).minus(Duration.ofSeconds(otherSeconds, otherNanos));
1315        assertEquals(t.getSeconds(), expectedSeconds);
1316        assertEquals(t.getNano(), expectedNanoOfSecond);
1317     }
1318 
1319     @Test(expectedExceptions=ArithmeticException.class)
minusOverflowTooSmall()1320     public void minusOverflowTooSmall() {
1321        Duration t = Duration.ofSeconds(Long.MIN_VALUE);
1322        t.minus(Duration.ofSeconds(0, 1));
1323     }
1324 
1325     @Test(expectedExceptions=ArithmeticException.class)
minusOverflowTooBig()1326     public void minusOverflowTooBig() {
1327        Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1328        t.minus(Duration.ofSeconds(-1, 999999999));
1329     }
1330 
1331     //-----------------------------------------------------------------------
1332     @Test
minus_longTemporalUnit_seconds()1333     public void minus_longTemporalUnit_seconds() {
1334         Duration t = Duration.ofSeconds(1);
1335         t = t.minus(1, SECONDS);
1336         assertEquals(0, t.getSeconds());
1337         assertEquals(0, t.getNano());
1338      }
1339 
1340     @Test
minus_longTemporalUnit_millis()1341     public void minus_longTemporalUnit_millis() {
1342         Duration t = Duration.ofSeconds(1);
1343         t = t.minus(1, MILLIS);
1344         assertEquals(0, t.getSeconds());
1345         assertEquals(999000000, t.getNano());
1346      }
1347 
1348     @Test
minus_longTemporalUnit_micros()1349     public void minus_longTemporalUnit_micros() {
1350         Duration t = Duration.ofSeconds(1);
1351         t = t.minus(1, MICROS);
1352         assertEquals(0, t.getSeconds());
1353         assertEquals(999999000, t.getNano());
1354      }
1355 
1356     @Test
minus_longTemporalUnit_nanos()1357     public void minus_longTemporalUnit_nanos() {
1358         Duration t = Duration.ofSeconds(1);
1359         t = t.minus(1, NANOS);
1360         assertEquals(0, t.getSeconds());
1361         assertEquals(999999999, t.getNano());
1362      }
1363 
1364     @Test(expectedExceptions=NullPointerException.class)
minus_longTemporalUnit_null()1365     public void minus_longTemporalUnit_null() {
1366        Duration t = Duration.ofSeconds(1);
1367        t.minus(1, (TemporalUnit) null);
1368     }
1369 
1370     //-----------------------------------------------------------------------
1371     @DataProvider(name="MinusSeconds")
provider_minusSeconds_long()1372     Object[][] provider_minusSeconds_long() {
1373         return new Object[][] {
1374             {0, 0, 0, 0, 0},
1375             {0, 0, 1, -1, 0},
1376             {0, 0, -1, 1, 0},
1377             {0, 0, Long.MAX_VALUE, -Long.MAX_VALUE, 0},
1378             {0, 0, Long.MIN_VALUE + 1, Long.MAX_VALUE, 0},
1379             {1, 0, 0, 1, 0},
1380             {1, 0, 1, 0, 0},
1381             {1, 0, -1, 2, 0},
1382             {1, 0, Long.MAX_VALUE - 1, -Long.MAX_VALUE + 2, 0},
1383             {1, 0, Long.MIN_VALUE + 2, Long.MAX_VALUE, 0},
1384             {1, 1, 0, 1, 1},
1385             {1, 1, 1, 0, 1},
1386             {1, 1, -1, 2, 1},
1387             {1, 1, Long.MAX_VALUE, -Long.MAX_VALUE + 1, 1},
1388             {1, 1, Long.MIN_VALUE + 2, Long.MAX_VALUE, 1},
1389             {-1, 1, 0, -1, 1},
1390             {-1, 1, 1, -2, 1},
1391             {-1, 1, -1, 0, 1},
1392             {-1, 1, Long.MAX_VALUE, Long.MIN_VALUE, 1},
1393             {-1, 1, Long.MIN_VALUE + 1, Long.MAX_VALUE - 1, 1},
1394         };
1395     }
1396 
1397     @Test(dataProvider="MinusSeconds")
minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1398     public void minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1399         Duration t = Duration.ofSeconds(seconds, nanos);
1400         t = t.minusSeconds(amount);
1401         assertEquals(t.getSeconds(), expectedSeconds);
1402         assertEquals(t.getNano(), expectedNanoOfSecond);
1403     }
1404 
1405     @Test(expectedExceptions = {ArithmeticException.class})
minusSeconds_long_overflowTooBig()1406     public void minusSeconds_long_overflowTooBig() {
1407         Duration t = Duration.ofSeconds(1, 0);
1408         t.minusSeconds(Long.MIN_VALUE + 1);
1409     }
1410 
1411     @Test(expectedExceptions = {ArithmeticException.class})
minusSeconds_long_overflowTooSmall()1412     public void minusSeconds_long_overflowTooSmall() {
1413         Duration t = Duration.ofSeconds(-2, 0);
1414         t.minusSeconds(Long.MAX_VALUE);
1415     }
1416 
1417     //-----------------------------------------------------------------------
1418     @DataProvider(name="MinusMillis")
provider_minusMillis_long()1419     Object[][] provider_minusMillis_long() {
1420         return new Object[][] {
1421             {0, 0, 0,       0, 0},
1422             {0, 0, 1,      -1, 999000000},
1423             {0, 0, 999,    -1, 1000000},
1424             {0, 0, 1000,   -1, 0},
1425             {0, 0, 1001,   -2, 999000000},
1426             {0, 0, 1999,   -2, 1000000},
1427             {0, 0, 2000,   -2, 0},
1428             {0, 0, -1,      0, 1000000},
1429             {0, 0, -999,    0, 999000000},
1430             {0, 0, -1000,   1, 0},
1431             {0, 0, -1001,   1, 1000000},
1432             {0, 0, -1999,   1, 999000000},
1433 
1434             {0, 1, 0,       0, 1},
1435             {0, 1, 1,      -1, 999000001},
1436             {0, 1, 998,    -1, 2000001},
1437             {0, 1, 999,    -1, 1000001},
1438             {0, 1, 1000,   -1, 1},
1439             {0, 1, 1998,   -2, 2000001},
1440             {0, 1, 1999,   -2, 1000001},
1441             {0, 1, 2000,   -2, 1},
1442             {0, 1, -1,      0, 1000001},
1443             {0, 1, -2,      0, 2000001},
1444             {0, 1, -1000,   1, 1},
1445             {0, 1, -1001,   1, 1000001},
1446 
1447             {0, 1000000, 0,       0, 1000000},
1448             {0, 1000000, 1,       0, 0},
1449             {0, 1000000, 998,    -1, 3000000},
1450             {0, 1000000, 999,    -1, 2000000},
1451             {0, 1000000, 1000,   -1, 1000000},
1452             {0, 1000000, 1998,   -2, 3000000},
1453             {0, 1000000, 1999,   -2, 2000000},
1454             {0, 1000000, 2000,   -2, 1000000},
1455             {0, 1000000, -1,      0, 2000000},
1456             {0, 1000000, -2,      0, 3000000},
1457             {0, 1000000, -999,    1, 0},
1458             {0, 1000000, -1000,   1, 1000000},
1459             {0, 1000000, -1001,   1, 2000000},
1460             {0, 1000000, -1002,   1, 3000000},
1461 
1462             {0, 999999999, 0,     0, 999999999},
1463             {0, 999999999, 1,     0, 998999999},
1464             {0, 999999999, 999,   0, 999999},
1465             {0, 999999999, 1000, -1, 999999999},
1466             {0, 999999999, 1001, -1, 998999999},
1467             {0, 999999999, -1,    1, 999999},
1468             {0, 999999999, -1000, 1, 999999999},
1469             {0, 999999999, -1001, 2, 999999},
1470         };
1471     }
1472 
1473     @Test(dataProvider="MinusMillis")
minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1474     public void minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1475         Duration t = Duration.ofSeconds(seconds, nanos);
1476         t = t.minusMillis(amount);
1477         assertEquals(t.getSeconds(), expectedSeconds);
1478         assertEquals(t.getNano(), expectedNanoOfSecond);
1479     }
1480     @Test(dataProvider="MinusMillis")
minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1481     public void minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1482         Duration t = Duration.ofSeconds(seconds + 1, nanos);
1483         t = t.minusMillis(amount);
1484         assertEquals(t.getSeconds(), expectedSeconds + 1);
1485         assertEquals(t.getNano(), expectedNanoOfSecond);
1486     }
1487     @Test(dataProvider="MinusMillis")
minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1488     public void minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1489         Duration t = Duration.ofSeconds(seconds - 1, nanos);
1490         t = t.minusMillis(amount);
1491         assertEquals(t.getSeconds(), expectedSeconds - 1);
1492         assertEquals(t.getNano(), expectedNanoOfSecond);
1493     }
1494 
1495     @Test
minusMillis_long_max()1496     public void minusMillis_long_max() {
1497         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 998999999);
1498         t = t.minusMillis(-1);
1499         assertEquals(t.getSeconds(), Long.MAX_VALUE);
1500         assertEquals(t.getNano(), 999999999);
1501     }
1502 
1503     @Test(expectedExceptions = {ArithmeticException.class})
minusMillis_long_overflowTooBig()1504     public void minusMillis_long_overflowTooBig() {
1505         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999000000);
1506         t.minusMillis(-1);
1507     }
1508 
1509     @Test
minusMillis_long_min()1510     public void minusMillis_long_min() {
1511         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 1000000);
1512         t = t.minusMillis(1);
1513         assertEquals(t.getSeconds(), Long.MIN_VALUE);
1514         assertEquals(t.getNano(), 0);
1515     }
1516 
1517     @Test(expectedExceptions = {ArithmeticException.class})
minusMillis_long_overflowTooSmall()1518     public void minusMillis_long_overflowTooSmall() {
1519         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
1520         t.minusMillis(1);
1521     }
1522 
1523     //-----------------------------------------------------------------------
1524     @DataProvider(name="MinusNanos")
provider_minusNanos_long()1525     Object[][] provider_minusNanos_long() {
1526         return new Object[][] {
1527             {0, 0, 0,           0, 0},
1528             {0, 0, 1,          -1, 999999999},
1529             {0, 0, 999999999,  -1, 1},
1530             {0, 0, 1000000000, -1, 0},
1531             {0, 0, 1000000001, -2, 999999999},
1532             {0, 0, 1999999999, -2, 1},
1533             {0, 0, 2000000000, -2, 0},
1534             {0, 0, -1,          0, 1},
1535             {0, 0, -999999999,  0, 999999999},
1536             {0, 0, -1000000000, 1, 0},
1537             {0, 0, -1000000001, 1, 1},
1538             {0, 0, -1999999999, 1, 999999999},
1539 
1540             {1, 0, 0,            1, 0},
1541             {1, 0, 1,            0, 999999999},
1542             {1, 0, 999999999,    0, 1},
1543             {1, 0, 1000000000,   0, 0},
1544             {1, 0, 1000000001,  -1, 999999999},
1545             {1, 0, 1999999999,  -1, 1},
1546             {1, 0, 2000000000,  -1, 0},
1547             {1, 0, -1,           1, 1},
1548             {1, 0, -999999999,   1, 999999999},
1549             {1, 0, -1000000000,  2, 0},
1550             {1, 0, -1000000001,  2, 1},
1551             {1, 0, -1999999999,  2, 999999999},
1552 
1553             {-1, 0, 0,           -1, 0},
1554             {-1, 0, 1,           -2, 999999999},
1555             {-1, 0, 999999999,   -2, 1},
1556             {-1, 0, 1000000000,  -2, 0},
1557             {-1, 0, 1000000001,  -3, 999999999},
1558             {-1, 0, 1999999999,  -3, 1},
1559             {-1, 0, 2000000000,  -3, 0},
1560             {-1, 0, -1,          -1, 1},
1561             {-1, 0, -999999999,  -1, 999999999},
1562             {-1, 0, -1000000000,  0, 0},
1563             {-1, 0, -1000000001,  0, 1},
1564             {-1, 0, -1999999999,  0, 999999999},
1565 
1566             {1, 1, 0,           1, 1},
1567             {1, 1, 1,           1, 0},
1568             {1, 1, 999999998,   0, 3},
1569             {1, 1, 999999999,   0, 2},
1570             {1, 1, 1000000000,  0, 1},
1571             {1, 1, 1999999998, -1, 3},
1572             {1, 1, 1999999999, -1, 2},
1573             {1, 1, 2000000000, -1, 1},
1574             {1, 1, -1,          1, 2},
1575             {1, 1, -2,          1, 3},
1576             {1, 1, -1000000000, 2, 1},
1577             {1, 1, -1000000001, 2, 2},
1578             {1, 1, -1000000002, 2, 3},
1579             {1, 1, -2000000000, 3, 1},
1580 
1581             {1, 999999999, 0,           1, 999999999},
1582             {1, 999999999, 1,           1, 999999998},
1583             {1, 999999999, 999999999,   1, 0},
1584             {1, 999999999, 1000000000,  0, 999999999},
1585             {1, 999999999, 1000000001,  0, 999999998},
1586             {1, 999999999, -1,          2, 0},
1587             {1, 999999999, -1000000000, 2, 999999999},
1588             {1, 999999999, -1000000001, 3, 0},
1589             {1, 999999999, -1999999999, 3, 999999998},
1590             {1, 999999999, -2000000000, 3, 999999999},
1591 
1592             {Long.MAX_VALUE, 0, -999999999, Long.MAX_VALUE, 999999999},
1593             {Long.MAX_VALUE - 1, 0, -1999999999, Long.MAX_VALUE, 999999999},
1594             {Long.MIN_VALUE, 1, 1, Long.MIN_VALUE, 0},
1595             {Long.MIN_VALUE + 1, 1, 1000000001, Long.MIN_VALUE, 0},
1596         };
1597     }
1598 
1599     @Test(dataProvider="MinusNanos")
minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1600     public void minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1601         Duration t = Duration.ofSeconds(seconds, nanos);
1602         t = t.minusNanos(amount);
1603         assertEquals(t.getSeconds(), expectedSeconds);
1604         assertEquals(t.getNano(), expectedNanoOfSecond);
1605     }
1606 
1607     @Test(expectedExceptions = {ArithmeticException.class})
minusNanos_long_overflowTooBig()1608     public void minusNanos_long_overflowTooBig() {
1609         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1610         t.minusNanos(-1);
1611     }
1612 
1613     @Test(expectedExceptions = {ArithmeticException.class})
minusNanos_long_overflowTooSmall()1614     public void minusNanos_long_overflowTooSmall() {
1615         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
1616         t.minusNanos(1);
1617     }
1618 
1619     //-----------------------------------------------------------------------
1620     // multipliedBy()
1621     //-----------------------------------------------------------------------
1622     @DataProvider(name="MultipliedBy")
provider_multipliedBy()1623     Object[][] provider_multipliedBy() {
1624        return new Object[][] {
1625           {-4, 666666667, -3,   9, 999999999},
1626           {-4, 666666667, -2,   6, 666666666},
1627           {-4, 666666667, -1,   3, 333333333},
1628           {-4, 666666667,  0,   0,         0},
1629           {-4, 666666667,  1,  -4, 666666667},
1630           {-4, 666666667,  2,  -7, 333333334},
1631           {-4, 666666667,  3, -10, 000000001},
1632 
1633           {-3, 0, -3,  9, 0},
1634           {-3, 0, -2,  6, 0},
1635           {-3, 0, -1,  3, 0},
1636           {-3, 0,  0,  0, 0},
1637           {-3, 0,  1, -3, 0},
1638           {-3, 0,  2, -6, 0},
1639           {-3, 0,  3, -9, 0},
1640 
1641           {-2, 0, -3,  6, 0},
1642           {-2, 0, -2,  4, 0},
1643           {-2, 0, -1,  2, 0},
1644           {-2, 0,  0,  0, 0},
1645           {-2, 0,  1, -2, 0},
1646           {-2, 0,  2, -4, 0},
1647           {-2, 0,  3, -6, 0},
1648 
1649           {-1, 0, -3,  3, 0},
1650           {-1, 0, -2,  2, 0},
1651           {-1, 0, -1,  1, 0},
1652           {-1, 0,  0,  0, 0},
1653           {-1, 0,  1, -1, 0},
1654           {-1, 0,  2, -2, 0},
1655           {-1, 0,  3, -3, 0},
1656 
1657           {-1, 500000000, -3,  1, 500000000},
1658           {-1, 500000000, -2,  1,         0},
1659           {-1, 500000000, -1,  0, 500000000},
1660           {-1, 500000000,  0,  0,         0},
1661           {-1, 500000000,  1, -1, 500000000},
1662           {-1, 500000000,  2, -1,         0},
1663           {-1, 500000000,  3, -2, 500000000},
1664 
1665           {0, 0, -3, 0, 0},
1666           {0, 0, -2, 0, 0},
1667           {0, 0, -1, 0, 0},
1668           {0, 0,  0, 0, 0},
1669           {0, 0,  1, 0, 0},
1670           {0, 0,  2, 0, 0},
1671           {0, 0,  3, 0, 0},
1672 
1673           {0, 500000000, -3, -2, 500000000},
1674           {0, 500000000, -2, -1,         0},
1675           {0, 500000000, -1, -1, 500000000},
1676           {0, 500000000,  0,  0,         0},
1677           {0, 500000000,  1,  0, 500000000},
1678           {0, 500000000,  2,  1,         0},
1679           {0, 500000000,  3,  1, 500000000},
1680 
1681           {1, 0, -3, -3, 0},
1682           {1, 0, -2, -2, 0},
1683           {1, 0, -1, -1, 0},
1684           {1, 0,  0,  0, 0},
1685           {1, 0,  1,  1, 0},
1686           {1, 0,  2,  2, 0},
1687           {1, 0,  3,  3, 0},
1688 
1689           {2, 0, -3, -6, 0},
1690           {2, 0, -2, -4, 0},
1691           {2, 0, -1, -2, 0},
1692           {2, 0,  0,  0, 0},
1693           {2, 0,  1,  2, 0},
1694           {2, 0,  2,  4, 0},
1695           {2, 0,  3,  6, 0},
1696 
1697           {3, 0, -3, -9, 0},
1698           {3, 0, -2, -6, 0},
1699           {3, 0, -1, -3, 0},
1700           {3, 0,  0,  0, 0},
1701           {3, 0,  1,  3, 0},
1702           {3, 0,  2,  6, 0},
1703           {3, 0,  3,  9, 0},
1704 
1705           {3, 333333333, -3, -10, 000000001},
1706           {3, 333333333, -2,  -7, 333333334},
1707           {3, 333333333, -1,  -4, 666666667},
1708           {3, 333333333,  0,   0,         0},
1709           {3, 333333333,  1,   3, 333333333},
1710           {3, 333333333,  2,   6, 666666666},
1711           {3, 333333333,  3,   9, 999999999},
1712        };
1713     }
1714 
1715     @Test(dataProvider="MultipliedBy")
multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos)1716     public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) {
1717         Duration t = Duration.ofSeconds(seconds, nanos);
1718         t = t.multipliedBy(multiplicand);
1719         assertEquals(t.getSeconds(), expectedSeconds);
1720         assertEquals(t.getNano(), expectedNanos);
1721     }
1722 
1723     @Test
multipliedBy_max()1724     public void multipliedBy_max() {
1725         Duration test = Duration.ofSeconds(1);
1726         assertEquals(test.multipliedBy(Long.MAX_VALUE), Duration.ofSeconds(Long.MAX_VALUE));
1727     }
1728 
1729     @Test
multipliedBy_min()1730     public void multipliedBy_min() {
1731         Duration test = Duration.ofSeconds(1);
1732         assertEquals(test.multipliedBy(Long.MIN_VALUE), Duration.ofSeconds(Long.MIN_VALUE));
1733     }
1734 
1735     @Test(expectedExceptions=ArithmeticException.class)
multipliedBy_tooBig()1736     public void multipliedBy_tooBig() {
1737         Duration test = Duration.ofSeconds(1, 1);
1738         test.multipliedBy(Long.MAX_VALUE);
1739     }
1740 
1741     @Test(expectedExceptions=ArithmeticException.class)
multipliedBy_tooBig_negative()1742     public void multipliedBy_tooBig_negative() {
1743         Duration test = Duration.ofSeconds(1, 1);
1744         test.multipliedBy(Long.MIN_VALUE);
1745     }
1746 
1747     //-----------------------------------------------------------------------
1748     // dividedBy()
1749     //-----------------------------------------------------------------------
1750     @DataProvider(name="DividedBy")
provider_dividedBy()1751     Object[][] provider_dividedBy() {
1752        return new Object[][] {
1753           {-4, 666666667, -3,  1, 111111111},
1754           {-4, 666666667, -2,  1, 666666666},
1755           {-4, 666666667, -1,  3, 333333333},
1756           {-4, 666666667,  1, -4, 666666667},
1757           {-4, 666666667,  2, -2, 333333334},
1758           {-4, 666666667,  3, -2, 888888889},
1759 
1760           {-3, 0, -3,  1, 0},
1761           {-3, 0, -2,  1, 500000000},
1762           {-3, 0, -1,  3, 0},
1763           {-3, 0,  1, -3, 0},
1764           {-3, 0,  2, -2, 500000000},
1765           {-3, 0,  3, -1, 0},
1766 
1767           {-2, 0, -3,  0, 666666666},
1768           {-2, 0, -2,  1,         0},
1769           {-2, 0, -1,  2,         0},
1770           {-2, 0,  1, -2,         0},
1771           {-2, 0,  2, -1,         0},
1772           {-2, 0,  3, -1, 333333334},
1773 
1774           {-1, 0, -3,  0, 333333333},
1775           {-1, 0, -2,  0, 500000000},
1776           {-1, 0, -1,  1,         0},
1777           {-1, 0,  1, -1,         0},
1778           {-1, 0,  2, -1, 500000000},
1779           {-1, 0,  3, -1, 666666667},
1780 
1781           {-1, 500000000, -3,  0, 166666666},
1782           {-1, 500000000, -2,  0, 250000000},
1783           {-1, 500000000, -1,  0, 500000000},
1784           {-1, 500000000,  1, -1, 500000000},
1785           {-1, 500000000,  2, -1, 750000000},
1786           {-1, 500000000,  3, -1, 833333334},
1787 
1788           {0, 0, -3, 0, 0},
1789           {0, 0, -2, 0, 0},
1790           {0, 0, -1, 0, 0},
1791           {0, 0,  1, 0, 0},
1792           {0, 0,  2, 0, 0},
1793           {0, 0,  3, 0, 0},
1794 
1795           {0, 500000000, -3, -1, 833333334},
1796           {0, 500000000, -2, -1, 750000000},
1797           {0, 500000000, -1, -1, 500000000},
1798           {0, 500000000,  1,  0, 500000000},
1799           {0, 500000000,  2,  0, 250000000},
1800           {0, 500000000,  3,  0, 166666666},
1801 
1802           {1, 0, -3, -1, 666666667},
1803           {1, 0, -2, -1, 500000000},
1804           {1, 0, -1, -1,         0},
1805           {1, 0,  1,  1,         0},
1806           {1, 0,  2,  0, 500000000},
1807           {1, 0,  3,  0, 333333333},
1808 
1809           {2, 0, -3, -1, 333333334},
1810           {2, 0, -2, -1,         0},
1811           {2, 0, -1, -2,         0},
1812           {2, 0,  1,  2,         0},
1813           {2, 0,  2,  1,         0},
1814           {2, 0,  3,  0, 666666666},
1815 
1816           {3, 0, -3, -1,         0},
1817           {3, 0, -2, -2, 500000000},
1818           {3, 0, -1, -3,         0},
1819           {3, 0,  1,  3,         0},
1820           {3, 0,  2,  1, 500000000},
1821           {3, 0,  3,  1,         0},
1822 
1823           {3, 333333333, -3, -2, 888888889},
1824           {3, 333333333, -2, -2, 333333334},
1825           {3, 333333333, -1, -4, 666666667},
1826           {3, 333333333,  1,  3, 333333333},
1827           {3, 333333333,  2,  1, 666666666},
1828           {3, 333333333,  3,  1, 111111111},
1829        };
1830     }
1831 
1832     @Test(dataProvider="DividedBy")
dividedBy(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos)1833     public void dividedBy(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos) {
1834         Duration t = Duration.ofSeconds(seconds, nanos);
1835         t = t.dividedBy(divisor);
1836         assertEquals(t.getSeconds(), expectedSeconds);
1837         assertEquals(t.getNano(), expectedNanos);
1838     }
1839 
1840     @Test(dataProvider="DividedBy", expectedExceptions=ArithmeticException.class)
dividedByZero(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos)1841     public void dividedByZero(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos) {
1842        Duration t = Duration.ofSeconds(seconds, nanos);
1843        t.dividedBy(0);
1844        fail(t + " divided by zero did not throw ArithmeticException");
1845     }
1846 
1847     @Test
dividedBy_max()1848     public void dividedBy_max() {
1849         Duration test = Duration.ofSeconds(Long.MAX_VALUE);
1850         assertEquals(test.dividedBy(Long.MAX_VALUE), Duration.ofSeconds(1));
1851     }
1852 
1853     //-----------------------------------------------------------------------
1854     // negated()
1855     //-----------------------------------------------------------------------
1856     @Test
test_negated()1857     public void test_negated() {
1858         assertEquals(Duration.ofSeconds(0).negated(), Duration.ofSeconds(0));
1859         assertEquals(Duration.ofSeconds(12).negated(), Duration.ofSeconds(-12));
1860         assertEquals(Duration.ofSeconds(-12).negated(), Duration.ofSeconds(12));
1861         assertEquals(Duration.ofSeconds(12, 20).negated(), Duration.ofSeconds(-12, -20));
1862         assertEquals(Duration.ofSeconds(12, -20).negated(), Duration.ofSeconds(-12, 20));
1863         assertEquals(Duration.ofSeconds(-12, -20).negated(), Duration.ofSeconds(12, 20));
1864         assertEquals(Duration.ofSeconds(-12, 20).negated(), Duration.ofSeconds(12, -20));
1865         assertEquals(Duration.ofSeconds(Long.MAX_VALUE).negated(), Duration.ofSeconds(-Long.MAX_VALUE));
1866     }
1867 
1868     @Test(expectedExceptions=ArithmeticException.class)
test_negated_overflow()1869     public void test_negated_overflow() {
1870         Duration.ofSeconds(Long.MIN_VALUE).negated();
1871     }
1872 
1873     //-----------------------------------------------------------------------
1874     // abs()
1875     //-----------------------------------------------------------------------
1876     @Test
test_abs()1877     public void test_abs() {
1878         assertEquals(Duration.ofSeconds(0).abs(), Duration.ofSeconds(0));
1879         assertEquals(Duration.ofSeconds(12).abs(), Duration.ofSeconds(12));
1880         assertEquals(Duration.ofSeconds(-12).abs(), Duration.ofSeconds(12));
1881         assertEquals(Duration.ofSeconds(12, 20).abs(), Duration.ofSeconds(12, 20));
1882         assertEquals(Duration.ofSeconds(12, -20).abs(), Duration.ofSeconds(12, -20));
1883         assertEquals(Duration.ofSeconds(-12, -20).abs(), Duration.ofSeconds(12, 20));
1884         assertEquals(Duration.ofSeconds(-12, 20).abs(), Duration.ofSeconds(12, -20));
1885         assertEquals(Duration.ofSeconds(Long.MAX_VALUE).abs(), Duration.ofSeconds(Long.MAX_VALUE));
1886     }
1887 
1888     @Test(expectedExceptions=ArithmeticException.class)
test_abs_overflow()1889     public void test_abs_overflow() {
1890         Duration.ofSeconds(Long.MIN_VALUE).abs();
1891     }
1892 
1893     //-----------------------------------------------------------------------
1894     // toNanos()
1895     //-----------------------------------------------------------------------
1896     @Test
test_toNanos()1897     public void test_toNanos() {
1898         Duration test = Duration.ofSeconds(321, 123456789);
1899         assertEquals(test.toNanos(), 321123456789L);
1900     }
1901 
1902     @Test
test_toNanos_max()1903     public void test_toNanos_max() {
1904         Duration test = Duration.ofSeconds(0, Long.MAX_VALUE);
1905         assertEquals(test.toNanos(), Long.MAX_VALUE);
1906     }
1907 
1908     @Test(expectedExceptions=ArithmeticException.class)
test_toNanos_tooBig()1909     public void test_toNanos_tooBig() {
1910         Duration test = Duration.ofSeconds(0, Long.MAX_VALUE).plusNanos(1);
1911         test.toNanos();
1912     }
1913 
1914     //-----------------------------------------------------------------------
1915     // toMillis()
1916     //-----------------------------------------------------------------------
1917     @Test
test_toMillis()1918     public void test_toMillis() {
1919         Duration test = Duration.ofSeconds(321, 123456789);
1920         assertEquals(test.toMillis(), 321000 + 123);
1921     }
1922 
1923     @Test
test_toMillis_max()1924     public void test_toMillis_max() {
1925         Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, (Long.MAX_VALUE % 1000) * 1000000);
1926         assertEquals(test.toMillis(), Long.MAX_VALUE);
1927     }
1928 
1929     @Test(expectedExceptions=ArithmeticException.class)
test_toMillis_tooBig()1930     public void test_toMillis_tooBig() {
1931         Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, ((Long.MAX_VALUE % 1000) + 1) * 1000000);
1932         test.toMillis();
1933     }
1934 
1935     //-----------------------------------------------------------------------
1936     // compareTo()
1937     //-----------------------------------------------------------------------
1938     @Test
test_comparisons()1939     public void test_comparisons() {
1940         doTest_comparisons_Duration(
1941             Duration.ofSeconds(-2L, 0),
1942             Duration.ofSeconds(-2L, 999999998),
1943             Duration.ofSeconds(-2L, 999999999),
1944             Duration.ofSeconds(-1L, 0),
1945             Duration.ofSeconds(-1L, 1),
1946             Duration.ofSeconds(-1L, 999999998),
1947             Duration.ofSeconds(-1L, 999999999),
1948             Duration.ofSeconds(0L, 0),
1949             Duration.ofSeconds(0L, 1),
1950             Duration.ofSeconds(0L, 2),
1951             Duration.ofSeconds(0L, 999999999),
1952             Duration.ofSeconds(1L, 0),
1953             Duration.ofSeconds(2L, 0)
1954         );
1955     }
1956 
doTest_comparisons_Duration(Duration... durations)1957     void doTest_comparisons_Duration(Duration... durations) {
1958         for (int i = 0; i < durations.length; i++) {
1959             Duration a = durations[i];
1960             for (int j = 0; j < durations.length; j++) {
1961                 Duration b = durations[j];
1962                 if (i < j) {
1963                     assertEquals(a.compareTo(b)< 0, true, a + " <=> " + b);
1964                     assertEquals(a.equals(b), false, a + " <=> " + b);
1965                 } else if (i > j) {
1966                     assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b);
1967                     assertEquals(a.equals(b), false, a + " <=> " + b);
1968                 } else {
1969                     assertEquals(a.compareTo(b), 0, a + " <=> " + b);
1970                     assertEquals(a.equals(b), true, a + " <=> " + b);
1971                 }
1972             }
1973         }
1974     }
1975 
1976     @Test(expectedExceptions=NullPointerException.class)
test_compareTo_ObjectNull()1977     public void test_compareTo_ObjectNull() {
1978         Duration a = Duration.ofSeconds(0L, 0);
1979         a.compareTo(null);
1980     }
1981 
1982     @Test(expectedExceptions=ClassCastException.class)
1983     @SuppressWarnings({ "unchecked", "rawtypes" })
compareToNonDuration()1984     public void compareToNonDuration() {
1985        Comparable c = Duration.ofSeconds(0L);
1986        c.compareTo(new Object());
1987     }
1988 
1989     //-----------------------------------------------------------------------
1990     // equals()
1991     //-----------------------------------------------------------------------
1992     @Test
test_equals()1993     public void test_equals() {
1994         Duration test5a = Duration.ofSeconds(5L, 20);
1995         Duration test5b = Duration.ofSeconds(5L, 20);
1996         Duration test5n = Duration.ofSeconds(5L, 30);
1997         Duration test6 = Duration.ofSeconds(6L, 20);
1998 
1999         assertEquals(test5a.equals(test5a), true);
2000         assertEquals(test5a.equals(test5b), true);
2001         assertEquals(test5a.equals(test5n), false);
2002         assertEquals(test5a.equals(test6), false);
2003 
2004         assertEquals(test5b.equals(test5a), true);
2005         assertEquals(test5b.equals(test5b), true);
2006         assertEquals(test5b.equals(test5n), false);
2007         assertEquals(test5b.equals(test6), false);
2008 
2009         assertEquals(test5n.equals(test5a), false);
2010         assertEquals(test5n.equals(test5b), false);
2011         assertEquals(test5n.equals(test5n), true);
2012         assertEquals(test5n.equals(test6), false);
2013 
2014         assertEquals(test6.equals(test5a), false);
2015         assertEquals(test6.equals(test5b), false);
2016         assertEquals(test6.equals(test5n), false);
2017         assertEquals(test6.equals(test6), true);
2018     }
2019 
2020     @Test
test_equals_null()2021     public void test_equals_null() {
2022         Duration test5 = Duration.ofSeconds(5L, 20);
2023         assertEquals(test5.equals(null), false);
2024     }
2025 
2026     @Test
test_equals_otherClass()2027     public void test_equals_otherClass() {
2028         Duration test5 = Duration.ofSeconds(5L, 20);
2029         assertEquals(test5.equals(""), false);
2030     }
2031 
2032     //-----------------------------------------------------------------------
2033     // hashCode()
2034     //-----------------------------------------------------------------------
2035     @Test
test_hashCode()2036     public void test_hashCode() {
2037         Duration test5a = Duration.ofSeconds(5L, 20);
2038         Duration test5b = Duration.ofSeconds(5L, 20);
2039         Duration test5n = Duration.ofSeconds(5L, 30);
2040         Duration test6 = Duration.ofSeconds(6L, 20);
2041 
2042         assertEquals(test5a.hashCode() == test5a.hashCode(), true);
2043         assertEquals(test5a.hashCode() == test5b.hashCode(), true);
2044         assertEquals(test5b.hashCode() == test5b.hashCode(), true);
2045 
2046         assertEquals(test5a.hashCode() == test5n.hashCode(), false);
2047         assertEquals(test5a.hashCode() == test6.hashCode(), false);
2048     }
2049 
2050     //-----------------------------------------------------------------------
2051     // toString()
2052     //-----------------------------------------------------------------------
2053     @DataProvider(name="ToString")
provider_toString()2054     Object[][] provider_toString() {
2055         return new Object[][] {
2056             {0, 0, "PT0S"},
2057             {0, 1, "PT0.000000001S"},
2058             {0, 10, "PT0.00000001S"},
2059             {0, 100, "PT0.0000001S"},
2060             {0, 1000, "PT0.000001S"},
2061             {0, 10000, "PT0.00001S"},
2062             {0, 100000, "PT0.0001S"},
2063             {0, 1000000, "PT0.001S"},
2064             {0, 10000000, "PT0.01S"},
2065             {0, 100000000, "PT0.1S"},
2066             {0, 120000000, "PT0.12S"},
2067             {0, 123000000, "PT0.123S"},
2068             {0, 123400000, "PT0.1234S"},
2069             {0, 123450000, "PT0.12345S"},
2070             {0, 123456000, "PT0.123456S"},
2071             {0, 123456700, "PT0.1234567S"},
2072             {0, 123456780, "PT0.12345678S"},
2073             {0, 123456789, "PT0.123456789S"},
2074             {1, 0, "PT1S"},
2075             {-1, 0, "PT-1S"},
2076             {-1, 1000, "PT-0.999999S"},
2077             {-1, 900000000, "PT-0.1S"},
2078 
2079             {60, 0, "PT1M"},
2080             {3600, 0, "PT1H"},
2081             {7261, 0, "PT2H1M1S"},
2082 //            {Long.MAX_VALUE, 0, "PT9223372036854775807S"},
2083 //            {Long.MIN_VALUE, 0, "PT-9223372036854775808S"},
2084         };
2085     }
2086 
2087     @Test(dataProvider="ToString")
test_toString(long seconds, int nanos, String expected)2088     public void test_toString(long seconds, int nanos, String expected) {
2089         Duration t = Duration.ofSeconds(seconds, nanos);
2090         assertEquals(t.toString(), expected);
2091     }
2092 
2093     //-------------------------------------------------------work in progress
2094     // toPartXxx()
2095     //-----------------------------------------------------------------------
2096     @DataProvider(name="ToPart")
provider_toPart()2097     Object[][] provider_toPart() {
2098         return new Object[][]{
2099                 {"PT0S", 0, 0, 0, 0, 0, 0},
2100                 {"P1DT1H1M1.123456789S", 1, 1, 1, 1, 123, 123456789},
2101                 {"-P1DT1H1M1.123456789S", -1, -1, -1, -2, 876, 876543211},
2102                 {"PT9999999.9S", 115, 17, 46, 39, 900, 900000000},
2103                 {"-PT9999999.9S", -115, -17, -46, -40, 100, 100000000},
2104                 {"-PT2S", 0, 0, 0, -2, 0, 0},
2105                 {"-PT1.999999999S", 0, 0, 0, -2, 0, 1}
2106 
2107         };
2108     }
2109 
2110     @Test(dataProvider = "ToPart")
test_toPart(String text, long daysPart, int hoursPart, int minutesPart, int secondsPart, int millisPart, int nanosPart)2111     public void test_toPart(String text, long daysPart, int hoursPart, int minutesPart, int secondsPart, int millisPart, int nanosPart) {
2112         Duration test = Duration.parse(text);
2113         assertEquals(test.toDaysPart(), daysPart);
2114         assertEquals(test.toHoursPart(), hoursPart);
2115         assertEquals(test.toMinutesPart(), minutesPart);
2116         assertEquals(test.toSecondsPart(), secondsPart);
2117         assertEquals(test.toMillisPart(), millisPart);
2118         assertEquals(test.toNanosPart(), nanosPart);
2119     }
2120 
2121 }
2122