• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * This file is available under and governed by the GNU General Public
26  * License version 2 only, as published by the Free Software Foundation.
27  * However, the following notice accompanied the original version of this
28  * file:
29  *
30  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
31  *
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions are met:
36  *
37  *  * Redistributions of source code must retain the above copyright notice,
38  *    this list of conditions and the following disclaimer.
39  *
40  *  * Redistributions in binary form must reproduce the above copyright notice,
41  *    this list of conditions and the following disclaimer in the documentation
42  *    and/or other materials provided with the distribution.
43  *
44  *  * Neither the name of JSR-310 nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 package tck.java.time;
61 
62 import static java.time.temporal.ChronoUnit.DAYS;
63 import static java.time.temporal.ChronoUnit.HALF_DAYS;
64 import static java.time.temporal.ChronoUnit.HOURS;
65 import static java.time.temporal.ChronoUnit.MICROS;
66 import static java.time.temporal.ChronoUnit.MILLIS;
67 import static java.time.temporal.ChronoUnit.MINUTES;
68 import static java.time.temporal.ChronoUnit.MONTHS;
69 import static java.time.temporal.ChronoUnit.NANOS;
70 import static java.time.temporal.ChronoUnit.SECONDS;
71 import static java.time.temporal.ChronoUnit.WEEKS;
72 import static java.time.temporal.ChronoUnit.YEARS;
73 import static org.testng.Assert.assertEquals;
74 import static org.testng.Assert.assertTrue;
75 import static org.testng.Assert.fail;
76 
77 import java.io.ByteArrayOutputStream;
78 import java.io.DataOutputStream;
79 import java.time.DateTimeException;
80 import java.time.Duration;
81 import java.time.Instant;
82 import java.time.LocalDate;
83 import java.time.LocalDateTime;
84 import java.time.LocalTime;
85 import java.time.Period;
86 import java.time.ZoneOffset;
87 import java.time.ZonedDateTime;
88 import java.time.format.DateTimeParseException;
89 import java.time.temporal.ChronoUnit;
90 import java.time.temporal.Temporal;
91 import java.time.temporal.TemporalAmount;
92 import java.time.temporal.TemporalUnit;
93 import java.util.ArrayList;
94 import java.util.Collections;
95 import java.util.List;
96 import java.util.Locale;
97 
98 import org.testng.annotations.DataProvider;
99 import org.testng.annotations.Test;
100 
101 /**
102  * Test Duration.
103  */
104 @Test
105 public class TCKDuration extends AbstractTCKTest {
106 
107     private static final long CYCLE_SECS = 146097L * 86400L;
108 
109     //-----------------------------------------------------------------------
110     // constants
111     //-----------------------------------------------------------------------
112     @Test
test_zero()113     public void test_zero() {
114         assertEquals(Duration.ZERO.getSeconds(), 0L);
115         assertEquals(Duration.ZERO.getNano(), 0);
116     }
117 
118     //-----------------------------------------------------------------------
119     // ofSeconds(long)
120     //-----------------------------------------------------------------------
121     @Test
factory_seconds_long()122     public void factory_seconds_long() {
123         for (long i = -2; i <= 2; i++) {
124             Duration t = Duration.ofSeconds(i);
125             assertEquals(t.getSeconds(), i);
126             assertEquals(t.getNano(), 0);
127         }
128     }
129 
130     //-----------------------------------------------------------------------
131     // ofSeconds(long,long)
132     //-----------------------------------------------------------------------
133     @Test
factory_seconds_long_long()134     public void factory_seconds_long_long() {
135         for (long i = -2; i <= 2; i++) {
136             for (int j = 0; j < 10; j++) {
137                 Duration t = Duration.ofSeconds(i, j);
138                 assertEquals(t.getSeconds(), i);
139                 assertEquals(t.getNano(), j);
140             }
141             for (int j = -10; j < 0; j++) {
142                 Duration t = Duration.ofSeconds(i, j);
143                 assertEquals(t.getSeconds(), i - 1);
144                 assertEquals(t.getNano(), j + 1000000000);
145             }
146             for (int j = 999999990; j < 1000000000; j++) {
147                 Duration t = Duration.ofSeconds(i, j);
148                 assertEquals(t.getSeconds(), i);
149                 assertEquals(t.getNano(), j);
150             }
151         }
152     }
153 
154     @Test
factory_seconds_long_long_nanosNegativeAdjusted()155     public void factory_seconds_long_long_nanosNegativeAdjusted() {
156         Duration test = Duration.ofSeconds(2L, -1);
157         assertEquals(test.getSeconds(), 1);
158         assertEquals(test.getNano(), 999999999);
159     }
160 
161     @Test(expectedExceptions=ArithmeticException.class)
factory_seconds_long_long_tooBig()162     public void factory_seconds_long_long_tooBig() {
163         Duration.ofSeconds(Long.MAX_VALUE, 1000000000);
164     }
165 
166     //-----------------------------------------------------------------------
167     // ofMillis(long)
168     //-----------------------------------------------------------------------
169     @DataProvider(name="MillisDurationNoNanos")
provider_factory_millis_long()170     Object[][] provider_factory_millis_long() {
171         return new Object[][] {
172             {0, 0, 0},
173             {1, 0, 1000000},
174             {2, 0, 2000000},
175             {999, 0, 999000000},
176             {1000, 1, 0},
177             {1001, 1, 1000000},
178             {-1, -1, 999000000},
179             {-2, -1, 998000000},
180             {-999, -1, 1000000},
181             {-1000, -1, 0},
182             {-1001, -2, 999000000},
183         };
184     }
185 
186     @Test(dataProvider="MillisDurationNoNanos")
factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond)187     public void factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond) {
188         Duration test = Duration.ofMillis(millis);
189         assertEquals(test.getSeconds(), expectedSeconds);
190         assertEquals(test.getNano(), expectedNanoOfSecond);
191     }
192 
193     //-----------------------------------------------------------------------
194     // ofNanos(long)
195     //-----------------------------------------------------------------------
196     @Test
factory_nanos_nanos()197     public void factory_nanos_nanos() {
198         Duration test = Duration.ofNanos(1);
199         assertEquals(test.getSeconds(), 0);
200         assertEquals(test.getNano(), 1);
201     }
202 
203     @Test
factory_nanos_nanosSecs()204     public void factory_nanos_nanosSecs() {
205         Duration test = Duration.ofNanos(1000000002);
206         assertEquals(test.getSeconds(), 1);
207         assertEquals(test.getNano(), 2);
208     }
209 
210     @Test
factory_nanos_negative()211     public void factory_nanos_negative() {
212         Duration test = Duration.ofNanos(-2000000001);
213         assertEquals(test.getSeconds(), -3);
214         assertEquals(test.getNano(), 999999999);
215     }
216 
217     @Test
factory_nanos_max()218     public void factory_nanos_max() {
219         Duration test = Duration.ofNanos(Long.MAX_VALUE);
220         assertEquals(test.getSeconds(), Long.MAX_VALUE / 1000000000);
221         assertEquals(test.getNano(), Long.MAX_VALUE % 1000000000);
222     }
223 
224     @Test
factory_nanos_min()225     public void factory_nanos_min() {
226         Duration test = Duration.ofNanos(Long.MIN_VALUE);
227         assertEquals(test.getSeconds(), Long.MIN_VALUE / 1000000000 - 1);
228         assertEquals(test.getNano(), Long.MIN_VALUE % 1000000000 + 1000000000);
229     }
230 
231     //-----------------------------------------------------------------------
232     // ofMinutes()
233     //-----------------------------------------------------------------------
234     @Test
factory_minutes()235     public void factory_minutes() {
236         Duration test = Duration.ofMinutes(2);
237         assertEquals(test.getSeconds(), 120);
238         assertEquals(test.getNano(), 0);
239     }
240 
241     @Test
factory_minutes_max()242     public void factory_minutes_max() {
243         Duration test = Duration.ofMinutes(Long.MAX_VALUE / 60);
244         assertEquals(test.getSeconds(), (Long.MAX_VALUE / 60) * 60);
245         assertEquals(test.getNano(), 0);
246     }
247 
248     @Test
factory_minutes_min()249     public void factory_minutes_min() {
250         Duration test = Duration.ofMinutes(Long.MIN_VALUE / 60);
251         assertEquals(test.getSeconds(), (Long.MIN_VALUE / 60) * 60);
252         assertEquals(test.getNano(), 0);
253     }
254 
255     @Test(expectedExceptions=ArithmeticException.class)
factory_minutes_tooBig()256     public void factory_minutes_tooBig() {
257         Duration.ofMinutes(Long.MAX_VALUE / 60 + 1);
258     }
259 
260     @Test(expectedExceptions=ArithmeticException.class)
factory_minutes_tooSmall()261     public void factory_minutes_tooSmall() {
262         Duration.ofMinutes(Long.MIN_VALUE / 60 - 1);
263     }
264 
265     //-----------------------------------------------------------------------
266     // ofHours()
267     //-----------------------------------------------------------------------
268     @Test
factory_hours()269     public void factory_hours() {
270         Duration test = Duration.ofHours(2);
271         assertEquals(test.getSeconds(), 2 * 3600);
272         assertEquals(test.getNano(), 0);
273     }
274 
275     @Test
factory_hours_max()276     public void factory_hours_max() {
277         Duration test = Duration.ofHours(Long.MAX_VALUE / 3600);
278         assertEquals(test.getSeconds(), (Long.MAX_VALUE / 3600) * 3600);
279         assertEquals(test.getNano(), 0);
280     }
281 
282     @Test
factory_hours_min()283     public void factory_hours_min() {
284         Duration test = Duration.ofHours(Long.MIN_VALUE / 3600);
285         assertEquals(test.getSeconds(), (Long.MIN_VALUE / 3600) * 3600);
286         assertEquals(test.getNano(), 0);
287     }
288 
289     @Test(expectedExceptions=ArithmeticException.class)
factory_hours_tooBig()290     public void factory_hours_tooBig() {
291         Duration.ofHours(Long.MAX_VALUE / 3600 + 1);
292     }
293 
294     @Test(expectedExceptions=ArithmeticException.class)
factory_hours_tooSmall()295     public void factory_hours_tooSmall() {
296         Duration.ofHours(Long.MIN_VALUE / 3600 - 1);
297     }
298 
299     //-----------------------------------------------------------------------
300     // ofDays()
301     //-----------------------------------------------------------------------
302     @Test
factory_days()303     public void factory_days() {
304         Duration test = Duration.ofDays(2);
305         assertEquals(test.getSeconds(), 2 * 86400);
306         assertEquals(test.getNano(), 0);
307     }
308 
309     @Test
factory_days_max()310     public void factory_days_max() {
311         Duration test = Duration.ofDays(Long.MAX_VALUE / 86400);
312         assertEquals(test.getSeconds(), (Long.MAX_VALUE / 86400) * 86400);
313         assertEquals(test.getNano(), 0);
314     }
315 
316     @Test
factory_days_min()317     public void factory_days_min() {
318         Duration test = Duration.ofDays(Long.MIN_VALUE / 86400);
319         assertEquals(test.getSeconds(), (Long.MIN_VALUE / 86400) * 86400);
320         assertEquals(test.getNano(), 0);
321     }
322 
323     @Test(expectedExceptions=ArithmeticException.class)
factory_days_tooBig()324     public void factory_days_tooBig() {
325         Duration.ofDays(Long.MAX_VALUE / 86400 + 1);
326     }
327 
328     @Test(expectedExceptions=ArithmeticException.class)
factory_days_tooSmall()329     public void factory_days_tooSmall() {
330         Duration.ofDays(Long.MIN_VALUE / 86400 - 1);
331     }
332 
333     //-----------------------------------------------------------------------
334     // of(long,TemporalUnit)
335     //-----------------------------------------------------------------------
336     @DataProvider(name="OfTemporalUnit")
provider_factory_of_longTemporalUnit()337     Object[][] provider_factory_of_longTemporalUnit() {
338         return new Object[][] {
339             {0, NANOS, 0, 0},
340             {0, MICROS, 0, 0},
341             {0, MILLIS, 0, 0},
342             {0, SECONDS, 0, 0},
343             {0, MINUTES, 0, 0},
344             {0, HOURS, 0, 0},
345             {0, HALF_DAYS, 0, 0},
346             {0, DAYS, 0, 0},
347             {1, NANOS, 0, 1},
348             {1, MICROS, 0, 1000},
349             {1, MILLIS, 0, 1000000},
350             {1, SECONDS, 1, 0},
351             {1, MINUTES, 60, 0},
352             {1, HOURS, 3600, 0},
353             {1, HALF_DAYS, 43200, 0},
354             {1, DAYS, 86400, 0},
355             {3, NANOS, 0, 3},
356             {3, MICROS, 0, 3000},
357             {3, MILLIS, 0, 3000000},
358             {3, SECONDS, 3, 0},
359             {3, MINUTES, 3 * 60, 0},
360             {3, HOURS, 3 * 3600, 0},
361             {3, HALF_DAYS, 3 * 43200, 0},
362             {3, DAYS, 3 * 86400, 0},
363             {-1, NANOS, -1, 999999999},
364             {-1, MICROS, -1, 999999000},
365             {-1, MILLIS, -1, 999000000},
366             {-1, SECONDS, -1, 0},
367             {-1, MINUTES, -60, 0},
368             {-1, HOURS, -3600, 0},
369             {-1, HALF_DAYS, -43200, 0},
370             {-1, DAYS, -86400, 0},
371             {-3, NANOS, -1, 999999997},
372             {-3, MICROS, -1, 999997000},
373             {-3, MILLIS, -1, 997000000},
374             {-3, SECONDS, -3, 0},
375             {-3, MINUTES, -3 * 60, 0},
376             {-3, HOURS, -3 * 3600, 0},
377             {-3, HALF_DAYS, -3 * 43200, 0},
378             {-3, DAYS, -3 * 86400, 0},
379             {Long.MAX_VALUE, NANOS, Long.MAX_VALUE / 1000000000, (int) (Long.MAX_VALUE % 1000000000)},
380             {Long.MIN_VALUE, NANOS, Long.MIN_VALUE / 1000000000 - 1, (int) (Long.MIN_VALUE % 1000000000 + 1000000000)},
381             {Long.MAX_VALUE, MICROS, Long.MAX_VALUE / 1000000, (int) ((Long.MAX_VALUE % 1000000) * 1000)},
382             {Long.MIN_VALUE, MICROS, Long.MIN_VALUE / 1000000 - 1, (int) ((Long.MIN_VALUE % 1000000 + 1000000) * 1000)},
383             {Long.MAX_VALUE, MILLIS, Long.MAX_VALUE / 1000, (int) ((Long.MAX_VALUE % 1000) * 1000000)},
384             {Long.MIN_VALUE, MILLIS, Long.MIN_VALUE / 1000 - 1, (int) ((Long.MIN_VALUE % 1000 + 1000) * 1000000)},
385             {Long.MAX_VALUE, SECONDS, Long.MAX_VALUE, 0},
386             {Long.MIN_VALUE, SECONDS, Long.MIN_VALUE, 0},
387             {Long.MAX_VALUE / 60, MINUTES, (Long.MAX_VALUE / 60) * 60, 0},
388             {Long.MIN_VALUE / 60, MINUTES, (Long.MIN_VALUE / 60) * 60, 0},
389             {Long.MAX_VALUE / 3600, HOURS, (Long.MAX_VALUE / 3600) * 3600, 0},
390             {Long.MIN_VALUE / 3600, HOURS, (Long.MIN_VALUE / 3600) * 3600, 0},
391             {Long.MAX_VALUE / 43200, HALF_DAYS, (Long.MAX_VALUE / 43200) * 43200, 0},
392             {Long.MIN_VALUE / 43200, HALF_DAYS, (Long.MIN_VALUE / 43200) * 43200, 0},
393         };
394     }
395 
396     @Test(dataProvider="OfTemporalUnit")
factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond)397     public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) {
398         Duration t = Duration.of(amount, unit);
399         assertEquals(t.getSeconds(), expectedSeconds);
400         assertEquals(t.getNano(), expectedNanoOfSecond);
401     }
402 
403     @DataProvider(name="OfTemporalUnitOutOfRange")
provider_factory_of_longTemporalUnit_outOfRange()404     Object[][] provider_factory_of_longTemporalUnit_outOfRange() {
405         return new Object[][] {
406             {Long.MAX_VALUE / 60 + 1, MINUTES},
407             {Long.MIN_VALUE / 60 - 1, MINUTES},
408             {Long.MAX_VALUE / 3600 + 1, HOURS},
409             {Long.MIN_VALUE / 3600 - 1, HOURS},
410             {Long.MAX_VALUE / 43200 + 1, HALF_DAYS},
411             {Long.MIN_VALUE / 43200 - 1, HALF_DAYS},
412         };
413     }
414 
415     @Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit)416     public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
417         Duration.of(amount, unit);
418     }
419 
420     @Test(expectedExceptions=DateTimeException.class)
factory_of_longTemporalUnit_estimatedUnit()421     public void factory_of_longTemporalUnit_estimatedUnit() {
422         Duration.of(2, WEEKS);
423     }
424 
425     @Test(expectedExceptions=NullPointerException.class)
factory_of_longTemporalUnit_null()426     public void factory_of_longTemporalUnit_null() {
427         Duration.of(1, (TemporalUnit) null);
428     }
429 
430     //-----------------------------------------------------------------------
431     // from(TemporalAmount)
432     //-----------------------------------------------------------------------
433     @Test
factory_from_TemporalAmount_Duration()434     public void factory_from_TemporalAmount_Duration() {
435         TemporalAmount amount = Duration.ofHours(3);
436         assertEquals(Duration.from(amount), Duration.ofHours(3));
437     }
438 
439     @Test
factory_from_TemporalAmount_DaysNanos()440     public void factory_from_TemporalAmount_DaysNanos() {
441         TemporalAmount amount = new TemporalAmount() {
442             @Override
443             public long get(TemporalUnit unit) {
444                 if (unit == DAYS) {
445                     return 23;
446                 } else {
447                     return 45;
448                 }
449             }
450             @Override
451             public List<TemporalUnit> getUnits() {
452                 List<TemporalUnit> list = new ArrayList<>();
453                 list.add(DAYS);
454                 list.add(NANOS);
455                 return list;
456             }
457             @Override
458             public Temporal addTo(Temporal temporal) {
459                 throw new UnsupportedOperationException();
460             }
461             @Override
462             public Temporal subtractFrom(Temporal temporal) {
463                 throw new UnsupportedOperationException();
464             }
465         };
466         Duration t = Duration.from(amount);
467         assertEquals(t.getSeconds(), 23 * 86400);
468         assertEquals(t.getNano(), 45);
469     }
470 
471     @Test(expectedExceptions = ArithmeticException.class)
factory_from_TemporalAmount_Minutes_tooBig()472     public void factory_from_TemporalAmount_Minutes_tooBig() {
473         TemporalAmount amount = new TemporalAmount() {
474             @Override
475             public long get(TemporalUnit unit) {
476                 return (Long.MAX_VALUE / 60) + 2;
477             }
478             @Override
479             public List<TemporalUnit> getUnits() {
480                 return Collections.<TemporalUnit>singletonList(MINUTES);
481             }
482             @Override
483             public Temporal addTo(Temporal temporal) {
484                 throw new UnsupportedOperationException();
485             }
486             @Override
487             public Temporal subtractFrom(Temporal temporal) {
488                 throw new UnsupportedOperationException();
489             }
490         };
491         Duration.from(amount);
492     }
493 
494     @Test(expectedExceptions = DateTimeException.class)
factory_from_TemporalAmount_Period()495     public void factory_from_TemporalAmount_Period() {
496         Duration.from(Period.ZERO);
497     }
498 
499     @Test(expectedExceptions = NullPointerException.class)
factory_from_TemporalAmount_null()500     public void factory_from_TemporalAmount_null() {
501         Duration.from(null);
502     }
503 
504     //-----------------------------------------------------------------------
505     // parse(String)
506     //-----------------------------------------------------------------------
507     @DataProvider(name="parseSuccess")
data_parseSuccess()508     Object[][] data_parseSuccess() {
509         return new Object[][] {
510                 {"PT0S", 0, 0},
511                 {"PT1S", 1, 0},
512                 {"PT12S", 12, 0},
513                 {"PT123456789S", 123456789, 0},
514                 {"PT" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0},
515 
516                 {"PT+1S", 1, 0},
517                 {"PT+12S", 12, 0},
518                 {"PT+123456789S", 123456789, 0},
519                 {"PT+" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0},
520 
521                 {"PT-1S", -1, 0},
522                 {"PT-12S", -12, 0},
523                 {"PT-123456789S", -123456789, 0},
524                 {"PT" + Long.MIN_VALUE + "S", Long.MIN_VALUE, 0},
525 
526 
527                 {"PT0.1S", 0, 100000000},
528                 {"PT1.1S", 1, 100000000},
529                 {"PT1.12S", 1, 120000000},
530                 {"PT1.123S", 1, 123000000},
531                 {"PT1.1234S", 1, 123400000},
532                 {"PT1.12345S", 1, 123450000},
533                 {"PT1.123456S", 1, 123456000},
534                 {"PT1.1234567S", 1, 123456700},
535                 {"PT1.12345678S", 1, 123456780},
536                 {"PT1.123456789S", 1, 123456789},
537 
538                 // Android-removed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
539                 // {"PT-0.1S", -1, 1000000000 - 100000000},
540                 {"PT-1.1S", -2, 1000000000 - 100000000},
541                 {"PT-1.12S", -2, 1000000000 - 120000000},
542                 {"PT-1.123S", -2, 1000000000 - 123000000},
543                 {"PT-1.1234S", -2, 1000000000 - 123400000},
544                 {"PT-1.12345S", -2, 1000000000 - 123450000},
545                 {"PT-1.123456S", -2, 1000000000 - 123456000},
546                 {"PT-1.1234567S", -2, 1000000000 - 123456700},
547                 {"PT-1.12345678S", -2, 1000000000 - 123456780},
548                 {"PT-1.123456789S", -2, 1000000000 - 123456789},
549 
550                 {"PT" + Long.MAX_VALUE + ".123456789S", Long.MAX_VALUE, 123456789},
551                 {"PT" + Long.MIN_VALUE + ".000000000S", Long.MIN_VALUE, 0},
552 
553                 // Android-removed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
554                 /*
555                 {"PT12M", 12 * 60, 0},
556                 {"PT12M0.35S", 12 * 60, 350000000},
557                 {"PT12M1.35S", 12 * 60 + 1, 350000000},
558                 {"PT12M-0.35S", 12 * 60 - 1, 1000000000 - 350000000},
559                 {"PT12M-1.35S", 12 * 60 - 2, 1000000000 - 350000000},
560 
561                 {"PT12H", 12 * 3600, 0},
562                 {"PT12H0.35S", 12 * 3600, 350000000},
563                 {"PT12H1.35S", 12 * 3600 + 1, 350000000},
564                 {"PT12H-0.35S", 12 * 3600 - 1, 1000000000 - 350000000},
565                 {"PT12H-1.35S", 12 * 3600 - 2, 1000000000 - 350000000},
566 
567                 {"P12D", 12 * 24 * 3600, 0},
568                 {"P12DT0.35S", 12 * 24 * 3600, 350000000},
569                 {"P12DT1.35S", 12 * 24 * 3600 + 1, 350000000},
570                 {"P12DT-0.35S", 12 * 24 * 3600 - 1, 1000000000 - 350000000},
571                 {"P12DT-1.35S", 12 * 24 * 3600 - 2, 1000000000 - 350000000},
572                  */
573 
574                 {"PT01S", 1, 0},
575                 {"PT001S", 1, 0},
576                 {"PT000S", 0, 0},
577                 {"PT+01S", 1, 0},
578                 {"PT-01S", -1, 0},
579 
580                 {"PT1.S", 1, 0},
581                 {"PT+1.S", 1, 0},
582                 {"PT-1.S", -1, 0},
583 
584                 {"P0D", 0, 0},
585                 {"P0DT0H", 0, 0},
586                 {"P0DT0M", 0, 0},
587                 {"P0DT0S", 0, 0},
588                 {"P0DT0H0S", 0, 0},
589                 {"P0DT0M0S", 0, 0},
590                 {"P0DT0H0M0S", 0, 0},
591 
592                 {"P1D", 86400, 0},
593                 {"P1DT0H", 86400, 0},
594                 {"P1DT0M", 86400, 0},
595                 {"P1DT0S", 86400, 0},
596                 {"P1DT0H0S", 86400, 0},
597                 {"P1DT0M0S", 86400, 0},
598                 {"P1DT0H0M0S", 86400, 0},
599 
600                 {"P3D", 86400 * 3, 0},
601                 {"P3DT2H", 86400 * 3 + 3600 * 2, 0},
602                 {"P3DT2M", 86400 * 3 + 60 * 2, 0},
603                 {"P3DT2S", 86400 * 3 + 2, 0},
604                 {"P3DT2H1S", 86400 * 3 + 3600 * 2 + 1, 0},
605                 {"P3DT2M1S", 86400 * 3 + 60 * 2 + 1, 0},
606                 {"P3DT2H1M1S", 86400 * 3 + 3600 * 2 + 60 + 1, 0},
607 
608                 {"P-3D", -86400 * 3, 0},
609                 {"P-3DT2H", -86400 * 3 + 3600 * 2, 0},
610                 {"P-3DT2M", -86400 * 3 + 60 * 2, 0},
611                 {"P-3DT2S", -86400 * 3 + 2, 0},
612                 {"P-3DT2H1S", -86400 * 3 + 3600 * 2 + 1, 0},
613                 {"P-3DT2M1S", -86400 * 3 + 60 * 2 + 1, 0},
614                 {"P-3DT2H1M1S", -86400 * 3 + 3600 * 2 + 60 + 1, 0},
615 
616                 {"P-3DT-2H", -86400 * 3 - 3600 * 2, 0},
617                 {"P-3DT-2M", -86400 * 3 - 60 * 2, 0},
618                 {"P-3DT-2S", -86400 * 3 - 2, 0},
619                 {"P-3DT-2H1S", -86400 * 3 - 3600 * 2 + 1, 0},
620                 {"P-3DT-2M1S", -86400 * 3 - 60 * 2 + 1, 0},
621                 {"P-3DT-2H1M1S", -86400 * 3 - 3600 * 2 + 60 + 1, 0},
622 
623                 {"PT0H", 0, 0},
624                 {"PT0H0M", 0, 0},
625                 {"PT0H0S", 0, 0},
626                 {"PT0H0M0S", 0, 0},
627 
628                 {"PT1H", 3600, 0},
629                 {"PT3H", 3600 * 3, 0},
630                 {"PT-1H", -3600, 0},
631                 {"PT-3H", -3600 * 3, 0},
632 
633                 {"PT2H5M", 3600 * 2 + 60 * 5, 0},
634                 {"PT2H5S", 3600 * 2 + 5, 0},
635                 {"PT2H5M8S", 3600 * 2 + 60 * 5 + 8, 0},
636                 {"PT-2H5M", -3600 * 2 + 60 * 5, 0},
637                 {"PT-2H5S", -3600 * 2 + 5, 0},
638                 {"PT-2H5M8S", -3600 * 2 + 60 * 5 + 8, 0},
639                 {"PT-2H-5M", -3600 * 2 - 60 * 5, 0},
640                 {"PT-2H-5S", -3600 * 2 - 5, 0},
641                 {"PT-2H-5M8S", -3600 * 2 - 60 * 5 + 8, 0},
642                 {"PT-2H-5M-8S", -3600 * 2 - 60 * 5 - 8, 0},
643 
644                 {"PT0M", 0, 0},
645                 {"PT1M", 60, 0},
646                 {"PT3M", 60 * 3, 0},
647                 {"PT-1M", -60, 0},
648                 {"PT-3M", -60 * 3, 0},
649                 {"P0DT3M", 60 * 3, 0},
650                 {"P0DT-3M", -60 * 3, 0},
651         };
652     }
653 
654     @Test(dataProvider="parseSuccess")
factory_parse(String text, long expectedSeconds, int expectedNanoOfSecond)655     public void factory_parse(String text, long expectedSeconds, int expectedNanoOfSecond) {
656         Duration test = Duration.parse(text);
657         assertEquals(test.getSeconds(), expectedSeconds);
658         assertEquals(test.getNano(), expectedNanoOfSecond);
659     }
660 
661     @Test(dataProvider="parseSuccess")
factory_parse_plus(String text, long expectedSeconds, int expectedNanoOfSecond)662     public void factory_parse_plus(String text, long expectedSeconds, int expectedNanoOfSecond) {
663         Duration test = Duration.parse("+" + text);
664         assertEquals(test.getSeconds(), expectedSeconds);
665         assertEquals(test.getNano(), expectedNanoOfSecond);
666     }
667 
668     @Test(dataProvider="parseSuccess")
factory_parse_minus(String text, long expectedSeconds, int expectedNanoOfSecond)669     public void factory_parse_minus(String text, long expectedSeconds, int expectedNanoOfSecond) {
670         Duration test;
671         try {
672             test = Duration.parse("-" + text);
673         } catch (DateTimeParseException ex) {
674             assertEquals(expectedSeconds == Long.MIN_VALUE, true);
675             return;
676         }
677         // not inside try/catch or it breaks test
678         assertEquals(test, Duration.ofSeconds(expectedSeconds, expectedNanoOfSecond).negated());
679     }
680 
681     @Test(dataProvider="parseSuccess")
factory_parse_comma(String text, long expectedSeconds, int expectedNanoOfSecond)682     public void factory_parse_comma(String text, long expectedSeconds, int expectedNanoOfSecond) {
683         text = text.replace('.', ',');
684         Duration test = Duration.parse(text);
685         assertEquals(test.getSeconds(), expectedSeconds);
686         assertEquals(test.getNano(), expectedNanoOfSecond);
687     }
688 
689     @Test(dataProvider="parseSuccess")
factory_parse_lowerCase(String text, long expectedSeconds, int expectedNanoOfSecond)690     public void factory_parse_lowerCase(String text, long expectedSeconds, int expectedNanoOfSecond) {
691         Duration test = Duration.parse(text.toLowerCase(Locale.ENGLISH));
692         assertEquals(test.getSeconds(), expectedSeconds);
693         assertEquals(test.getNano(), expectedNanoOfSecond);
694     }
695 
696     @DataProvider(name="parseFailure")
data_parseFailure()697     Object[][] data_parseFailure() {
698         return new Object[][] {
699                 {""},
700                 {"ABCDEF"},
701                 {" PT0S"},
702                 {"PT0S "},
703 
704                 {"PTS"},
705                 {"AT0S"},
706                 {"PA0S"},
707                 {"PT0A"},
708 
709                 {"P0Y"},
710                 {"P1Y"},
711                 {"P-2Y"},
712                 {"P0M"},
713                 {"P1M"},
714                 {"P-2M"},
715                 {"P3Y2D"},
716                 {"P3M2D"},
717                 {"P3W"},
718                 {"P-3W"},
719                 {"P2YT30S"},
720                 {"P2MT30S"},
721 
722                 {"P1DT"},
723 
724                 {"PT+S"},
725                 {"PT-S"},
726                 {"PT.S"},
727                 {"PTAS"},
728 
729                 {"PT-.S"},
730                 {"PT+.S"},
731 
732                 {"PT1ABC2S"},
733                 {"PT1.1ABC2S"},
734 
735                 {"PT123456789123456789123456789S"},
736                 {"PT0.1234567891S"},
737 
738                 {"PT2.-3"},
739                 {"PT-2.-3"},
740                 {"PT2.+3"},
741                 {"PT-2.+3"},
742         };
743     }
744 
745     @Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
factory_parseFailures(String text)746     public void factory_parseFailures(String text) {
747         Duration.parse(text);
748     }
749 
750     @Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
factory_parseFailures_comma(String text)751     public void factory_parseFailures_comma(String text) {
752         text = text.replace('.', ',');
753         Duration.parse(text);
754     }
755 
756     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_tooBig()757     public void factory_parse_tooBig() {
758         Duration.parse("PT" + Long.MAX_VALUE + "1S");
759     }
760 
761     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_tooBig_decimal()762     public void factory_parse_tooBig_decimal() {
763         Duration.parse("PT" + Long.MAX_VALUE + "1.1S");
764     }
765 
766     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_tooSmall()767     public void factory_parse_tooSmall() {
768         Duration.parse("PT" + Long.MIN_VALUE + "1S");
769     }
770 
771     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_tooSmall_decimal()772     public void factory_parse_tooSmall_decimal() {
773         Duration.parse("PT" + Long.MIN_VALUE + ".1S");
774     }
775 
776     @Test(expectedExceptions=NullPointerException.class)
factory_parse_nullText()777     public void factory_parse_nullText() {
778         Duration.parse(null);
779     }
780 
781     //-----------------------------------------------------------------------
782     // between()
783     //-----------------------------------------------------------------------
784     @DataProvider(name="durationBetweenInstant")
data_durationBetweenInstant()785     Object[][] data_durationBetweenInstant() {
786         return new Object[][] {
787                 {0, 0, 0, 0, 0, 0},
788                 {3, 0, 7, 0, 4, 0},
789                 {7, 0, 3, 0, -4, 0},
790 
791                 {3, 20, 7, 50, 4, 30},
792                 {3, 80, 7, 50, 3, 999999970},
793                 {3, 80, 7, 79, 3, 999999999},
794                 {3, 80, 7, 80, 4, 0},
795                 {3, 80, 7, 81, 4, 1},
796         };
797     }
798 
799     @Test(dataProvider="durationBetweenInstant")
factory_between_TemporalTemporal_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond)800     public void factory_between_TemporalTemporal_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) {
801         Instant start = Instant.ofEpochSecond(secs1, nanos1);
802         Instant end = Instant.ofEpochSecond(secs2, nanos2);
803         Duration t = Duration.between(start, end);
804         assertEquals(t.getSeconds(), expectedSeconds);
805         assertEquals(t.getNano(), expectedNanoOfSecond);
806     }
807 
808     @Test(dataProvider="durationBetweenInstant")
factory_between_TemporalTemporal_Instant_negated(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond)809     public void factory_between_TemporalTemporal_Instant_negated(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) {
810         Instant start = Instant.ofEpochSecond(secs1, nanos1);
811         Instant end = Instant.ofEpochSecond(secs2, nanos2);
812         assertEquals(Duration.between(end, start), Duration.between(start, end).negated());
813     }
814 
815     @DataProvider(name="durationBetweenLocalTime")
data_durationBetweenLocalTime()816     Object[][] data_durationBetweenLocalTime() {
817         return new Object[][] {
818                 {LocalTime.of(11, 0, 30), LocalTime.of(11, 0, 45), 15L, 0},
819                 {LocalTime.of(11, 0, 30), LocalTime.of(11, 0, 25), -5L, 0},
820         };
821     }
822 
823     @Test(dataProvider="durationBetweenLocalTime")
factory_between_TemporalTemporal_LT(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond)824     public void factory_between_TemporalTemporal_LT(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond) {
825         Duration t = Duration.between(start, end);
826         assertEquals(t.getSeconds(), expectedSeconds);
827         assertEquals(t.getNano(), expectedNanoOfSecond);
828     }
829 
830     @Test(dataProvider="durationBetweenLocalTime")
factory_between_TemporalTemporal_LT_negated(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond)831     public void factory_between_TemporalTemporal_LT_negated(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond) {
832         assertEquals(Duration.between(end, start), Duration.between(start, end).negated());
833     }
834 
835     @DataProvider(name="durationBetweenLocalDateTime")
data_durationBetweenLocalDateTime()836     Object[][] data_durationBetweenLocalDateTime() {
837         return new Object[][] {
838                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 30, 65_000_000), -2L, 500_000_000},
839                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), -1L, 500_000_000},
840                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 32, 65_000_000), 0L, 500_000_000},
841                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 33, 65_000_000), 1L, 500_000_000},
842                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 34, 65_000_000), 2L, 500_000_000},
843 
844                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 30, 565_000_000), -1L, 500_000_000},
845                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), 0L, 500_000_000},
846                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 32, 565_000_000), 1L, 500_000_000},
847                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 33, 565_000_000), 2L, 500_000_000},
848                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 34, 565_000_000), 3L, 500_000_000},
849 
850                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 30, 65_000_000), -1L, 0},
851                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), 0L, 0},
852                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 32, 65_000_000), 1L, 0},
853                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 33, 65_000_000), 2L, 0},
854                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 34, 65_000_000), 3L, 0},
855 
856                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2813, 3, 24, 0, 44, 30, 565_000_000), 2 * CYCLE_SECS - 1L, 500_000_000},
857                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2813, 3, 24, 0, 44, 31, 565_000_000), 2 * CYCLE_SECS + 0L, 500_000_000},
858                 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2813, 3, 24, 0, 44, 32, 565_000_000), 2 * CYCLE_SECS + 1L, 500_000_000},
859         };
860     }
861 
862     @Test(dataProvider="durationBetweenLocalDateTime")
factory_between_TemporalTemporal_LDT(LocalDateTime start, LocalDateTime end, long expectedSeconds, int expectedNanoOfSecond)863     public void factory_between_TemporalTemporal_LDT(LocalDateTime start, LocalDateTime end, long expectedSeconds, int expectedNanoOfSecond) {
864         Duration t = Duration.between(start, end);
865         assertEquals(t.getSeconds(), expectedSeconds);
866         assertEquals(t.getNano(), expectedNanoOfSecond);
867     }
868 
869     @Test(dataProvider="durationBetweenLocalDateTime")
factory_between_TemporalTemporal_LDT_negated(LocalDateTime start, LocalDateTime end, long expectedSeconds, int expectedNanoOfSecond)870     public void factory_between_TemporalTemporal_LDT_negated(LocalDateTime start, LocalDateTime end, long expectedSeconds, int expectedNanoOfSecond) {
871         assertEquals(Duration.between(end, start), Duration.between(start, end).negated());
872     }
873 
874     @Test
factory_between_TemporalTemporal_mixedTypes()875     public void factory_between_TemporalTemporal_mixedTypes() {
876         Instant start = Instant.ofEpochSecond(1);
877         ZonedDateTime end = Instant.ofEpochSecond(4).atZone(ZoneOffset.UTC);
878         assertEquals(Duration.between(start, end), Duration.ofSeconds(3));
879     }
880 
881     @Test(expectedExceptions=DateTimeException.class)
factory_between_TemporalTemporal_invalidMixedTypes()882     public void factory_between_TemporalTemporal_invalidMixedTypes() {
883         Instant start = Instant.ofEpochSecond(1);
884         LocalDate end = LocalDate.of(2010, 6, 20);
885         Duration.between(start, end);
886     }
887 
888     @Test(expectedExceptions=NullPointerException.class)
factory_between__TemporalTemporal_startNull()889     public void factory_between__TemporalTemporal_startNull() {
890         Instant end = Instant.ofEpochSecond(1);
891         Duration.between(null, end);
892     }
893 
894     @Test(expectedExceptions=NullPointerException.class)
factory_between__TemporalTemporal_endNull()895     public void factory_between__TemporalTemporal_endNull() {
896         Instant start = Instant.ofEpochSecond(1);
897         Duration.between(start, null);
898     }
899 
900     //-----------------------------------------------------------------------
901     // isZero(), isPositive(), isPositiveOrZero(), isNegative(), isNegativeOrZero()
902     //-----------------------------------------------------------------------
903     @Test
test_isZero()904     public void test_isZero() {
905         assertEquals(Duration.ofNanos(0).isZero(), true);
906         assertEquals(Duration.ofSeconds(0).isZero(), true);
907         assertEquals(Duration.ofNanos(1).isZero(), false);
908         assertEquals(Duration.ofSeconds(1).isZero(), false);
909         assertEquals(Duration.ofSeconds(1, 1).isZero(), false);
910         assertEquals(Duration.ofNanos(-1).isZero(), false);
911         assertEquals(Duration.ofSeconds(-1).isZero(), false);
912         assertEquals(Duration.ofSeconds(-1, -1).isZero(), false);
913     }
914 
915     @Test
test_isNegative()916     public void test_isNegative() {
917         assertEquals(Duration.ofNanos(0).isNegative(), false);
918         assertEquals(Duration.ofSeconds(0).isNegative(), false);
919         assertEquals(Duration.ofNanos(1).isNegative(), false);
920         assertEquals(Duration.ofSeconds(1).isNegative(), false);
921         assertEquals(Duration.ofSeconds(1, 1).isNegative(), false);
922         assertEquals(Duration.ofNanos(-1).isNegative(), true);
923         assertEquals(Duration.ofSeconds(-1).isNegative(), true);
924         assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
925     }
926 
927     //-----------------------------------------------------------------------
928     // plus()
929     //-----------------------------------------------------------------------
930     @DataProvider(name="Plus")
provider_plus()931     Object[][] provider_plus() {
932         return new Object[][] {
933             {Long.MIN_VALUE, 0, Long.MAX_VALUE, 0, -1, 0},
934 
935             {-4, 666666667, -4, 666666667, -7, 333333334},
936             {-4, 666666667, -3,         0, -7, 666666667},
937             {-4, 666666667, -2,         0, -6, 666666667},
938             {-4, 666666667, -1,         0, -5, 666666667},
939             {-4, 666666667, -1, 333333334, -4,         1},
940             {-4, 666666667, -1, 666666667, -4, 333333334},
941             {-4, 666666667, -1, 999999999, -4, 666666666},
942             {-4, 666666667,  0,         0, -4, 666666667},
943             {-4, 666666667,  0,         1, -4, 666666668},
944             {-4, 666666667,  0, 333333333, -3,         0},
945             {-4, 666666667,  0, 666666666, -3, 333333333},
946             {-4, 666666667,  1,         0, -3, 666666667},
947             {-4, 666666667,  2,         0, -2, 666666667},
948             {-4, 666666667,  3,         0, -1, 666666667},
949             {-4, 666666667,  3, 333333333,  0,         0},
950 
951             {-3, 0, -4, 666666667, -7, 666666667},
952             {-3, 0, -3,         0, -6,         0},
953             {-3, 0, -2,         0, -5,         0},
954             {-3, 0, -1,         0, -4,         0},
955             {-3, 0, -1, 333333334, -4, 333333334},
956             {-3, 0, -1, 666666667, -4, 666666667},
957             {-3, 0, -1, 999999999, -4, 999999999},
958             {-3, 0,  0,         0, -3,         0},
959             {-3, 0,  0,         1, -3,         1},
960             {-3, 0,  0, 333333333, -3, 333333333},
961             {-3, 0,  0, 666666666, -3, 666666666},
962             {-3, 0,  1,         0, -2,         0},
963             {-3, 0,  2,         0, -1,         0},
964             {-3, 0,  3,         0,  0,         0},
965             {-3, 0,  3, 333333333,  0, 333333333},
966 
967             {-2, 0, -4, 666666667, -6, 666666667},
968             {-2, 0, -3,         0, -5,         0},
969             {-2, 0, -2,         0, -4,         0},
970             {-2, 0, -1,         0, -3,         0},
971             {-2, 0, -1, 333333334, -3, 333333334},
972             {-2, 0, -1, 666666667, -3, 666666667},
973             {-2, 0, -1, 999999999, -3, 999999999},
974             {-2, 0,  0,         0, -2,         0},
975             {-2, 0,  0,         1, -2,         1},
976             {-2, 0,  0, 333333333, -2, 333333333},
977             {-2, 0,  0, 666666666, -2, 666666666},
978             {-2, 0,  1,         0, -1,         0},
979             {-2, 0,  2,         0,  0,         0},
980             {-2, 0,  3,         0,  1,         0},
981             {-2, 0,  3, 333333333,  1, 333333333},
982 
983             {-1, 0, -4, 666666667, -5, 666666667},
984             {-1, 0, -3,         0, -4,         0},
985             {-1, 0, -2,         0, -3,         0},
986             {-1, 0, -1,         0, -2,         0},
987             {-1, 0, -1, 333333334, -2, 333333334},
988             {-1, 0, -1, 666666667, -2, 666666667},
989             {-1, 0, -1, 999999999, -2, 999999999},
990             {-1, 0,  0,         0, -1,         0},
991             {-1, 0,  0,         1, -1,         1},
992             {-1, 0,  0, 333333333, -1, 333333333},
993             {-1, 0,  0, 666666666, -1, 666666666},
994             {-1, 0,  1,         0,  0,         0},
995             {-1, 0,  2,         0,  1,         0},
996             {-1, 0,  3,         0,  2,         0},
997             {-1, 0,  3, 333333333,  2, 333333333},
998 
999             {-1, 666666667, -4, 666666667, -4, 333333334},
1000             {-1, 666666667, -3,         0, -4, 666666667},
1001             {-1, 666666667, -2,         0, -3, 666666667},
1002             {-1, 666666667, -1,         0, -2, 666666667},
1003             {-1, 666666667, -1, 333333334, -1,         1},
1004             {-1, 666666667, -1, 666666667, -1, 333333334},
1005             {-1, 666666667, -1, 999999999, -1, 666666666},
1006             {-1, 666666667,  0,         0, -1, 666666667},
1007             {-1, 666666667,  0,         1, -1, 666666668},
1008             {-1, 666666667,  0, 333333333,  0,         0},
1009             {-1, 666666667,  0, 666666666,  0, 333333333},
1010             {-1, 666666667,  1,         0,  0, 666666667},
1011             {-1, 666666667,  2,         0,  1, 666666667},
1012             {-1, 666666667,  3,         0,  2, 666666667},
1013             {-1, 666666667,  3, 333333333,  3,         0},
1014 
1015             {0, 0, -4, 666666667, -4, 666666667},
1016             {0, 0, -3,         0, -3,         0},
1017             {0, 0, -2,         0, -2,         0},
1018             {0, 0, -1,         0, -1,         0},
1019             {0, 0, -1, 333333334, -1, 333333334},
1020             {0, 0, -1, 666666667, -1, 666666667},
1021             {0, 0, -1, 999999999, -1, 999999999},
1022             {0, 0,  0,         0,  0,         0},
1023             {0, 0,  0,         1,  0,         1},
1024             {0, 0,  0, 333333333,  0, 333333333},
1025             {0, 0,  0, 666666666,  0, 666666666},
1026             {0, 0,  1,         0,  1,         0},
1027             {0, 0,  2,         0,  2,         0},
1028             {0, 0,  3,         0,  3,         0},
1029             {0, 0,  3, 333333333,  3, 333333333},
1030 
1031             {0, 333333333, -4, 666666667, -3,         0},
1032             {0, 333333333, -3,         0, -3, 333333333},
1033             {0, 333333333, -2,         0, -2, 333333333},
1034             {0, 333333333, -1,         0, -1, 333333333},
1035             {0, 333333333, -1, 333333334, -1, 666666667},
1036             {0, 333333333, -1, 666666667,  0,         0},
1037             {0, 333333333, -1, 999999999,  0, 333333332},
1038             {0, 333333333,  0,         0,  0, 333333333},
1039             {0, 333333333,  0,         1,  0, 333333334},
1040             {0, 333333333,  0, 333333333,  0, 666666666},
1041             {0, 333333333,  0, 666666666,  0, 999999999},
1042             {0, 333333333,  1,         0,  1, 333333333},
1043             {0, 333333333,  2,         0,  2, 333333333},
1044             {0, 333333333,  3,         0,  3, 333333333},
1045             {0, 333333333,  3, 333333333,  3, 666666666},
1046 
1047             {1, 0, -4, 666666667, -3, 666666667},
1048             {1, 0, -3,         0, -2,         0},
1049             {1, 0, -2,         0, -1,         0},
1050             {1, 0, -1,         0,  0,         0},
1051             {1, 0, -1, 333333334,  0, 333333334},
1052             {1, 0, -1, 666666667,  0, 666666667},
1053             {1, 0, -1, 999999999,  0, 999999999},
1054             {1, 0,  0,         0,  1,         0},
1055             {1, 0,  0,         1,  1,         1},
1056             {1, 0,  0, 333333333,  1, 333333333},
1057             {1, 0,  0, 666666666,  1, 666666666},
1058             {1, 0,  1,         0,  2,         0},
1059             {1, 0,  2,         0,  3,         0},
1060             {1, 0,  3,         0,  4,         0},
1061             {1, 0,  3, 333333333,  4, 333333333},
1062 
1063             {2, 0, -4, 666666667, -2, 666666667},
1064             {2, 0, -3,         0, -1,         0},
1065             {2, 0, -2,         0,  0,         0},
1066             {2, 0, -1,         0,  1,         0},
1067             {2, 0, -1, 333333334,  1, 333333334},
1068             {2, 0, -1, 666666667,  1, 666666667},
1069             {2, 0, -1, 999999999,  1, 999999999},
1070             {2, 0,  0,         0,  2,         0},
1071             {2, 0,  0,         1,  2,         1},
1072             {2, 0,  0, 333333333,  2, 333333333},
1073             {2, 0,  0, 666666666,  2, 666666666},
1074             {2, 0,  1,         0,  3,         0},
1075             {2, 0,  2,         0,  4,         0},
1076             {2, 0,  3,         0,  5,         0},
1077             {2, 0,  3, 333333333,  5, 333333333},
1078 
1079             {3, 0, -4, 666666667, -1, 666666667},
1080             {3, 0, -3,         0,  0,         0},
1081             {3, 0, -2,         0,  1,         0},
1082             {3, 0, -1,         0,  2,         0},
1083             {3, 0, -1, 333333334,  2, 333333334},
1084             {3, 0, -1, 666666667,  2, 666666667},
1085             {3, 0, -1, 999999999,  2, 999999999},
1086             {3, 0,  0,         0,  3,         0},
1087             {3, 0,  0,         1,  3,         1},
1088             {3, 0,  0, 333333333,  3, 333333333},
1089             {3, 0,  0, 666666666,  3, 666666666},
1090             {3, 0,  1,         0,  4,         0},
1091             {3, 0,  2,         0,  5,         0},
1092             {3, 0,  3,         0,  6,         0},
1093             {3, 0,  3, 333333333,  6, 333333333},
1094 
1095             {3, 333333333, -4, 666666667,  0,         0},
1096             {3, 333333333, -3,         0,  0, 333333333},
1097             {3, 333333333, -2,         0,  1, 333333333},
1098             {3, 333333333, -1,         0,  2, 333333333},
1099             {3, 333333333, -1, 333333334,  2, 666666667},
1100             {3, 333333333, -1, 666666667,  3,         0},
1101             {3, 333333333, -1, 999999999,  3, 333333332},
1102             {3, 333333333,  0,         0,  3, 333333333},
1103             {3, 333333333,  0,         1,  3, 333333334},
1104             {3, 333333333,  0, 333333333,  3, 666666666},
1105             {3, 333333333,  0, 666666666,  3, 999999999},
1106             {3, 333333333,  1,         0,  4, 333333333},
1107             {3, 333333333,  2,         0,  5, 333333333},
1108             {3, 333333333,  3,         0,  6, 333333333},
1109             {3, 333333333,  3, 333333333,  6, 666666666},
1110 
1111             {Long.MAX_VALUE, 0, Long.MIN_VALUE, 0, -1, 0},
1112        };
1113     }
1114 
1115     @Test(dataProvider="Plus")
plus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond)1116     public void plus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
1117        Duration t = Duration.ofSeconds(seconds, nanos).plus(Duration.ofSeconds(otherSeconds, otherNanos));
1118        assertEquals(t.getSeconds(), expectedSeconds);
1119        assertEquals(t.getNano(), expectedNanoOfSecond);
1120     }
1121 
1122     @Test(expectedExceptions=ArithmeticException.class)
plusOverflowTooBig()1123     public void plusOverflowTooBig() {
1124        Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1125        t.plus(Duration.ofSeconds(0, 1));
1126     }
1127 
1128     @Test(expectedExceptions=ArithmeticException.class)
plusOverflowTooSmall()1129     public void plusOverflowTooSmall() {
1130        Duration t = Duration.ofSeconds(Long.MIN_VALUE);
1131        t.plus(Duration.ofSeconds(-1, 999999999));
1132     }
1133 
1134     //-----------------------------------------------------------------------
1135     @Test
plus_longTemporalUnit_seconds()1136     public void plus_longTemporalUnit_seconds() {
1137         Duration t = Duration.ofSeconds(1);
1138         t = t.plus(1, SECONDS);
1139         assertEquals(2, t.getSeconds());
1140         assertEquals(0, t.getNano());
1141      }
1142 
1143     @Test
plus_longTemporalUnit_millis()1144     public void plus_longTemporalUnit_millis() {
1145         Duration t = Duration.ofSeconds(1);
1146         t = t.plus(1, MILLIS);
1147         assertEquals(1, t.getSeconds());
1148         assertEquals(1000000, t.getNano());
1149      }
1150 
1151     @Test
plus_longTemporalUnit_micros()1152     public void plus_longTemporalUnit_micros() {
1153         Duration t = Duration.ofSeconds(1);
1154         t = t.plus(1, MICROS);
1155         assertEquals(1, t.getSeconds());
1156         assertEquals(1000, t.getNano());
1157      }
1158 
1159     @Test
plus_longTemporalUnit_nanos()1160     public void plus_longTemporalUnit_nanos() {
1161         Duration t = Duration.ofSeconds(1);
1162         t = t.plus(1, NANOS);
1163         assertEquals(1, t.getSeconds());
1164         assertEquals(1, t.getNano());
1165      }
1166 
1167     @Test(expectedExceptions=NullPointerException.class)
plus_longTemporalUnit_null()1168     public void plus_longTemporalUnit_null() {
1169        Duration t = Duration.ofSeconds(1);
1170        t.plus(1, (TemporalUnit) null);
1171     }
1172 
1173     //-----------------------------------------------------------------------
1174     @DataProvider(name="PlusDays")
provider_plusDays_long()1175     Object[][] provider_plusDays_long() {
1176         return new Object[][] {
1177             {0, 0, 0},
1178             {0, 1, 1},
1179             {0, -1, -1},
1180             {Long.MAX_VALUE/3600/24, 0, Long.MAX_VALUE/3600/24},
1181             {Long.MIN_VALUE/3600/24, 0, Long.MIN_VALUE/3600/24},
1182             {1, 0, 1},
1183             {1, 1, 2},
1184             {1, -1, 0},
1185             {1, Long.MIN_VALUE/3600/24, Long.MIN_VALUE/3600/24 + 1},
1186             {1, 0, 1},
1187             {1, 1, 2},
1188             {1, -1, 0},
1189             {-1, 0, -1},
1190             {-1, 1, 0},
1191             {-1, -1, -2},
1192             {-1, Long.MAX_VALUE/3600/24, Long.MAX_VALUE/3600/24 - 1},
1193         };
1194     }
1195 
1196     @Test(dataProvider="PlusDays")
plusDays_long(long days, long amount, long expectedDays)1197     public void plusDays_long(long days, long amount, long expectedDays) {
1198         Duration t = Duration.ofDays(days);
1199         t = t.plusDays(amount);
1200         assertEquals(t.toDays(), expectedDays);
1201     }
1202 
1203     @Test(expectedExceptions = {ArithmeticException.class})
plusDays_long_overflowTooBig()1204     public void plusDays_long_overflowTooBig() {
1205         Duration t = Duration.ofDays(1);
1206         t.plusDays(Long.MAX_VALUE/3600/24);
1207     }
1208 
1209     @Test(expectedExceptions = {ArithmeticException.class})
plusDays_long_overflowTooSmall()1210     public void plusDays_long_overflowTooSmall() {
1211         Duration t = Duration.ofDays(-1);
1212         t.plusDays(Long.MIN_VALUE/3600/24);
1213     }
1214 
1215     //-----------------------------------------------------------------------
1216     @DataProvider(name="PlusHours")
provider_plusHours_long()1217     Object[][] provider_plusHours_long() {
1218         return new Object[][] {
1219             {0, 0, 0},
1220             {0, 1, 1},
1221             {0, -1, -1},
1222             {Long.MAX_VALUE/3600, 0, Long.MAX_VALUE/3600},
1223             {Long.MIN_VALUE/3600, 0, Long.MIN_VALUE/3600},
1224             {1, 0, 1},
1225             {1, 1, 2},
1226             {1, -1, 0},
1227             {1, Long.MIN_VALUE/3600, Long.MIN_VALUE/3600 + 1},
1228             {1, 0, 1},
1229             {1, 1, 2},
1230             {1, -1, 0},
1231             {-1, 0, -1},
1232             {-1, 1, 0},
1233             {-1, -1, -2},
1234             {-1, Long.MAX_VALUE/3600, Long.MAX_VALUE/3600 - 1},
1235         };
1236     }
1237 
1238     @Test(dataProvider="PlusHours")
plusHours_long(long hours, long amount, long expectedHours)1239     public void plusHours_long(long hours, long amount, long expectedHours) {
1240         Duration t = Duration.ofHours(hours);
1241         t = t.plusHours(amount);
1242         assertEquals(t.toHours(), expectedHours);
1243     }
1244 
1245     @Test(expectedExceptions = {ArithmeticException.class})
plusHours_long_overflowTooBig()1246     public void plusHours_long_overflowTooBig() {
1247         Duration t = Duration.ofHours(1);
1248         t.plusHours(Long.MAX_VALUE/3600);
1249     }
1250 
1251     @Test(expectedExceptions = {ArithmeticException.class})
plusHours_long_overflowTooSmall()1252     public void plusHours_long_overflowTooSmall() {
1253         Duration t = Duration.ofHours(-1);
1254         t.plusHours(Long.MIN_VALUE/3600);
1255     }
1256 
1257     //-----------------------------------------------------------------------
1258     @DataProvider(name="PlusMinutes")
provider_plusMinutes_long()1259     Object[][] provider_plusMinutes_long() {
1260         return new Object[][] {
1261             {0, 0, 0},
1262             {0, 1, 1},
1263             {0, -1, -1},
1264             {Long.MAX_VALUE/60, 0, Long.MAX_VALUE/60},
1265             {Long.MIN_VALUE/60, 0, Long.MIN_VALUE/60},
1266             {1, 0, 1},
1267             {1, 1, 2},
1268             {1, -1, 0},
1269             {1, Long.MIN_VALUE/60, Long.MIN_VALUE/60 + 1},
1270             {1, 0, 1},
1271             {1, 1, 2},
1272             {1, -1, 0},
1273             {-1, 0, -1},
1274             {-1, 1, 0},
1275             {-1, -1, -2},
1276             {-1, Long.MAX_VALUE/60, Long.MAX_VALUE/60 - 1},
1277         };
1278     }
1279 
1280     @Test(dataProvider="PlusMinutes")
plusMinutes_long(long minutes, long amount, long expectedMinutes)1281     public void plusMinutes_long(long minutes, long amount, long expectedMinutes) {
1282         Duration t = Duration.ofMinutes(minutes);
1283         t = t.plusMinutes(amount);
1284         assertEquals(t.toMinutes(), expectedMinutes);
1285     }
1286 
1287     @Test(expectedExceptions = {ArithmeticException.class})
plusMinutes_long_overflowTooBig()1288     public void plusMinutes_long_overflowTooBig() {
1289         Duration t = Duration.ofMinutes(1);
1290         t.plusMinutes(Long.MAX_VALUE/60);
1291     }
1292 
1293     @Test(expectedExceptions = {ArithmeticException.class})
plusMinutes_long_overflowTooSmall()1294     public void plusMinutes_long_overflowTooSmall() {
1295         Duration t = Duration.ofMinutes(-1);
1296         t.plusMinutes(Long.MIN_VALUE/60);
1297     }
1298 
1299     //-----------------------------------------------------------------------
1300     @DataProvider(name="PlusSeconds")
provider_plusSeconds_long()1301     Object[][] provider_plusSeconds_long() {
1302         return new Object[][] {
1303             {0, 0, 0, 0, 0},
1304             {0, 0, 1, 1, 0},
1305             {0, 0, -1, -1, 0},
1306             {0, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0},
1307             {0, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0},
1308             {1, 0, 0, 1, 0},
1309             {1, 0, 1, 2, 0},
1310             {1, 0, -1, 0, 0},
1311             {1, 0, Long.MAX_VALUE - 1, Long.MAX_VALUE, 0},
1312             {1, 0, Long.MIN_VALUE, Long.MIN_VALUE + 1, 0},
1313             {1, 1, 0, 1, 1},
1314             {1, 1, 1, 2, 1},
1315             {1, 1, -1, 0, 1},
1316             {1, 1, Long.MAX_VALUE - 1, Long.MAX_VALUE, 1},
1317             {1, 1, Long.MIN_VALUE, Long.MIN_VALUE + 1, 1},
1318             {-1, 1, 0, -1, 1},
1319             {-1, 1, 1, 0, 1},
1320             {-1, 1, -1, -2, 1},
1321             {-1, 1, Long.MAX_VALUE, Long.MAX_VALUE - 1, 1},
1322             {-1, 1, Long.MIN_VALUE + 1, Long.MIN_VALUE, 1},
1323         };
1324     }
1325 
1326     @Test(dataProvider="PlusSeconds")
plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1327     public void plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1328         Duration t = Duration.ofSeconds(seconds, nanos);
1329         t = t.plusSeconds(amount);
1330         assertEquals(t.getSeconds(), expectedSeconds);
1331         assertEquals(t.getNano(), expectedNanoOfSecond);
1332     }
1333 
1334     @Test(expectedExceptions = {ArithmeticException.class})
plusSeconds_long_overflowTooBig()1335     public void plusSeconds_long_overflowTooBig() {
1336         Duration t = Duration.ofSeconds(1, 0);
1337         t.plusSeconds(Long.MAX_VALUE);
1338     }
1339 
1340     @Test(expectedExceptions = {ArithmeticException.class})
plusSeconds_long_overflowTooSmall()1341     public void plusSeconds_long_overflowTooSmall() {
1342         Duration t = Duration.ofSeconds(-1, 0);
1343         t.plusSeconds(Long.MIN_VALUE);
1344     }
1345 
1346     //-----------------------------------------------------------------------
1347     @DataProvider(name="PlusMillis")
provider_plusMillis_long()1348     Object[][] provider_plusMillis_long() {
1349         return new Object[][] {
1350             {0, 0, 0,       0, 0},
1351             {0, 0, 1,       0, 1000000},
1352             {0, 0, 999,     0, 999000000},
1353             {0, 0, 1000,    1, 0},
1354             {0, 0, 1001,    1, 1000000},
1355             {0, 0, 1999,    1, 999000000},
1356             {0, 0, 2000,    2, 0},
1357             {0, 0, -1,      -1, 999000000},
1358             {0, 0, -999,    -1, 1000000},
1359             {0, 0, -1000,   -1, 0},
1360             {0, 0, -1001,   -2, 999000000},
1361             {0, 0, -1999,   -2, 1000000},
1362 
1363             {0, 1, 0,       0, 1},
1364             {0, 1, 1,       0, 1000001},
1365             {0, 1, 998,     0, 998000001},
1366             {0, 1, 999,     0, 999000001},
1367             {0, 1, 1000,    1, 1},
1368             {0, 1, 1998,    1, 998000001},
1369             {0, 1, 1999,    1, 999000001},
1370             {0, 1, 2000,    2, 1},
1371             {0, 1, -1,      -1, 999000001},
1372             {0, 1, -2,      -1, 998000001},
1373             {0, 1, -1000,   -1, 1},
1374             {0, 1, -1001,   -2, 999000001},
1375 
1376             {0, 1000000, 0,       0, 1000000},
1377             {0, 1000000, 1,       0, 2000000},
1378             {0, 1000000, 998,     0, 999000000},
1379             {0, 1000000, 999,     1, 0},
1380             {0, 1000000, 1000,    1, 1000000},
1381             {0, 1000000, 1998,    1, 999000000},
1382             {0, 1000000, 1999,    2, 0},
1383             {0, 1000000, 2000,    2, 1000000},
1384             {0, 1000000, -1,      0, 0},
1385             {0, 1000000, -2,      -1, 999000000},
1386             {0, 1000000, -999,    -1, 2000000},
1387             {0, 1000000, -1000,   -1, 1000000},
1388             {0, 1000000, -1001,   -1, 0},
1389             {0, 1000000, -1002,   -2, 999000000},
1390 
1391             {0, 999999999, 0,     0, 999999999},
1392             {0, 999999999, 1,     1, 999999},
1393             {0, 999999999, 999,   1, 998999999},
1394             {0, 999999999, 1000,  1, 999999999},
1395             {0, 999999999, 1001,  2, 999999},
1396             {0, 999999999, -1,    0, 998999999},
1397             {0, 999999999, -1000, -1, 999999999},
1398             {0, 999999999, -1001, -1, 998999999},
1399         };
1400     }
1401 
1402     @Test(dataProvider="PlusMillis")
plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1403     public void plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1404         Duration t = Duration.ofSeconds(seconds, nanos);
1405         t = t.plusMillis(amount);
1406         assertEquals(t.getSeconds(), expectedSeconds);
1407         assertEquals(t.getNano(), expectedNanoOfSecond);
1408     }
1409     @Test(dataProvider="PlusMillis")
plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1410     public void plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1411         Duration t = Duration.ofSeconds(seconds + 1, nanos);
1412         t = t.plusMillis(amount);
1413         assertEquals(t.getSeconds(), expectedSeconds + 1);
1414         assertEquals(t.getNano(), expectedNanoOfSecond);
1415     }
1416     @Test(dataProvider="PlusMillis")
plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1417     public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1418         Duration t = Duration.ofSeconds(seconds - 1, nanos);
1419         t = t.plusMillis(amount);
1420         assertEquals(t.getSeconds(), expectedSeconds - 1);
1421         assertEquals(t.getNano(), expectedNanoOfSecond);
1422     }
1423 
1424     @Test
plusMillis_long_max()1425     public void plusMillis_long_max() {
1426         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 998999999);
1427         t = t.plusMillis(1);
1428         assertEquals(t.getSeconds(), Long.MAX_VALUE);
1429         assertEquals(t.getNano(), 999999999);
1430     }
1431 
1432     @Test(expectedExceptions = {ArithmeticException.class})
plusMillis_long_overflowTooBig()1433     public void plusMillis_long_overflowTooBig() {
1434         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999000000);
1435         t.plusMillis(1);
1436     }
1437 
1438     @Test
plusMillis_long_min()1439     public void plusMillis_long_min() {
1440         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 1000000);
1441         t = t.plusMillis(-1);
1442         assertEquals(t.getSeconds(), Long.MIN_VALUE);
1443         assertEquals(t.getNano(), 0);
1444     }
1445 
1446     @Test(expectedExceptions = {ArithmeticException.class})
plusMillis_long_overflowTooSmall()1447     public void plusMillis_long_overflowTooSmall() {
1448         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
1449         t.plusMillis(-1);
1450     }
1451 
1452     //-----------------------------------------------------------------------
1453     @DataProvider(name="PlusNanos")
provider_plusNanos_long()1454     Object[][] provider_plusNanos_long() {
1455         return new Object[][] {
1456             {0, 0, 0,           0, 0},
1457             {0, 0, 1,           0, 1},
1458             {0, 0, 999999999,   0, 999999999},
1459             {0, 0, 1000000000,  1, 0},
1460             {0, 0, 1000000001,  1, 1},
1461             {0, 0, 1999999999,  1, 999999999},
1462             {0, 0, 2000000000,  2, 0},
1463             {0, 0, -1,          -1, 999999999},
1464             {0, 0, -999999999,  -1, 1},
1465             {0, 0, -1000000000, -1, 0},
1466             {0, 0, -1000000001, -2, 999999999},
1467             {0, 0, -1999999999, -2, 1},
1468 
1469             {1, 0, 0,           1, 0},
1470             {1, 0, 1,           1, 1},
1471             {1, 0, 999999999,   1, 999999999},
1472             {1, 0, 1000000000,  2, 0},
1473             {1, 0, 1000000001,  2, 1},
1474             {1, 0, 1999999999,  2, 999999999},
1475             {1, 0, 2000000000,  3, 0},
1476             {1, 0, -1,          0, 999999999},
1477             {1, 0, -999999999,  0, 1},
1478             {1, 0, -1000000000, 0, 0},
1479             {1, 0, -1000000001, -1, 999999999},
1480             {1, 0, -1999999999, -1, 1},
1481 
1482             {-1, 0, 0,           -1, 0},
1483             {-1, 0, 1,           -1, 1},
1484             {-1, 0, 999999999,   -1, 999999999},
1485             {-1, 0, 1000000000,  0, 0},
1486             {-1, 0, 1000000001,  0, 1},
1487             {-1, 0, 1999999999,  0, 999999999},
1488             {-1, 0, 2000000000,  1, 0},
1489             {-1, 0, -1,          -2, 999999999},
1490             {-1, 0, -999999999,  -2, 1},
1491             {-1, 0, -1000000000, -2, 0},
1492             {-1, 0, -1000000001, -3, 999999999},
1493             {-1, 0, -1999999999, -3, 1},
1494 
1495             {1, 1, 0,           1, 1},
1496             {1, 1, 1,           1, 2},
1497             {1, 1, 999999998,   1, 999999999},
1498             {1, 1, 999999999,   2, 0},
1499             {1, 1, 1000000000,  2, 1},
1500             {1, 1, 1999999998,  2, 999999999},
1501             {1, 1, 1999999999,  3, 0},
1502             {1, 1, 2000000000,  3, 1},
1503             {1, 1, -1,          1, 0},
1504             {1, 1, -2,          0, 999999999},
1505             {1, 1, -1000000000, 0, 1},
1506             {1, 1, -1000000001, 0, 0},
1507             {1, 1, -1000000002, -1, 999999999},
1508             {1, 1, -2000000000, -1, 1},
1509 
1510             {1, 999999999, 0,           1, 999999999},
1511             {1, 999999999, 1,           2, 0},
1512             {1, 999999999, 999999999,   2, 999999998},
1513             {1, 999999999, 1000000000,  2, 999999999},
1514             {1, 999999999, 1000000001,  3, 0},
1515             {1, 999999999, -1,          1, 999999998},
1516             {1, 999999999, -1000000000, 0, 999999999},
1517             {1, 999999999, -1000000001, 0, 999999998},
1518             {1, 999999999, -1999999999, 0, 0},
1519             {1, 999999999, -2000000000, -1, 999999999},
1520 
1521             {Long.MAX_VALUE, 0, 999999999, Long.MAX_VALUE, 999999999},
1522             {Long.MAX_VALUE - 1, 0, 1999999999, Long.MAX_VALUE, 999999999},
1523             {Long.MIN_VALUE, 1, -1, Long.MIN_VALUE, 0},
1524             {Long.MIN_VALUE + 1, 1, -1000000001, Long.MIN_VALUE, 0},
1525         };
1526     }
1527 
1528     @Test(dataProvider="PlusNanos")
plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1529     public void plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1530         Duration t = Duration.ofSeconds(seconds, nanos);
1531         t = t.plusNanos(amount);
1532         assertEquals(t.getSeconds(), expectedSeconds);
1533         assertEquals(t.getNano(), expectedNanoOfSecond);
1534     }
1535 
1536     @Test(expectedExceptions = {ArithmeticException.class})
plusNanos_long_overflowTooBig()1537     public void plusNanos_long_overflowTooBig() {
1538         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1539         t.plusNanos(1);
1540     }
1541 
1542     @Test(expectedExceptions = {ArithmeticException.class})
plusNanos_long_overflowTooSmall()1543     public void plusNanos_long_overflowTooSmall() {
1544         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
1545         t.plusNanos(-1);
1546     }
1547 
1548     //-----------------------------------------------------------------------
1549     @DataProvider(name="Minus")
provider_minus()1550     Object[][] provider_minus() {
1551         return new Object[][] {
1552             {Long.MIN_VALUE, 0, Long.MIN_VALUE + 1, 0, -1, 0},
1553 
1554             {-4, 666666667, -4, 666666667,  0,         0},
1555             {-4, 666666667, -3,         0, -1, 666666667},
1556             {-4, 666666667, -2,         0, -2, 666666667},
1557             {-4, 666666667, -1,         0, -3, 666666667},
1558             {-4, 666666667, -1, 333333334, -3, 333333333},
1559             {-4, 666666667, -1, 666666667, -3,         0},
1560             {-4, 666666667, -1, 999999999, -4, 666666668},
1561             {-4, 666666667,  0,         0, -4, 666666667},
1562             {-4, 666666667,  0,         1, -4, 666666666},
1563             {-4, 666666667,  0, 333333333, -4, 333333334},
1564             {-4, 666666667,  0, 666666666, -4,         1},
1565             {-4, 666666667,  1,         0, -5, 666666667},
1566             {-4, 666666667,  2,         0, -6, 666666667},
1567             {-4, 666666667,  3,         0, -7, 666666667},
1568             {-4, 666666667,  3, 333333333, -7, 333333334},
1569 
1570             {-3, 0, -4, 666666667,  0, 333333333},
1571             {-3, 0, -3,         0,  0,         0},
1572             {-3, 0, -2,         0, -1,         0},
1573             {-3, 0, -1,         0, -2,         0},
1574             {-3, 0, -1, 333333334, -3, 666666666},
1575             {-3, 0, -1, 666666667, -3, 333333333},
1576             {-3, 0, -1, 999999999, -3,         1},
1577             {-3, 0,  0,         0, -3,         0},
1578             {-3, 0,  0,         1, -4, 999999999},
1579             {-3, 0,  0, 333333333, -4, 666666667},
1580             {-3, 0,  0, 666666666, -4, 333333334},
1581             {-3, 0,  1,         0, -4,         0},
1582             {-3, 0,  2,         0, -5,         0},
1583             {-3, 0,  3,         0, -6,         0},
1584             {-3, 0,  3, 333333333, -7, 666666667},
1585 
1586             {-2, 0, -4, 666666667,  1, 333333333},
1587             {-2, 0, -3,         0,  1,         0},
1588             {-2, 0, -2,         0,  0,         0},
1589             {-2, 0, -1,         0, -1,         0},
1590             {-2, 0, -1, 333333334, -2, 666666666},
1591             {-2, 0, -1, 666666667, -2, 333333333},
1592             {-2, 0, -1, 999999999, -2,         1},
1593             {-2, 0,  0,         0, -2,         0},
1594             {-2, 0,  0,         1, -3, 999999999},
1595             {-2, 0,  0, 333333333, -3, 666666667},
1596             {-2, 0,  0, 666666666, -3, 333333334},
1597             {-2, 0,  1,         0, -3,         0},
1598             {-2, 0,  2,         0, -4,         0},
1599             {-2, 0,  3,         0, -5,         0},
1600             {-2, 0,  3, 333333333, -6, 666666667},
1601 
1602             {-1, 0, -4, 666666667,  2, 333333333},
1603             {-1, 0, -3,         0,  2,         0},
1604             {-1, 0, -2,         0,  1,         0},
1605             {-1, 0, -1,         0,  0,         0},
1606             {-1, 0, -1, 333333334, -1, 666666666},
1607             {-1, 0, -1, 666666667, -1, 333333333},
1608             {-1, 0, -1, 999999999, -1,         1},
1609             {-1, 0,  0,         0, -1,         0},
1610             {-1, 0,  0,         1, -2, 999999999},
1611             {-1, 0,  0, 333333333, -2, 666666667},
1612             {-1, 0,  0, 666666666, -2, 333333334},
1613             {-1, 0,  1,         0, -2,         0},
1614             {-1, 0,  2,         0, -3,         0},
1615             {-1, 0,  3,         0, -4,         0},
1616             {-1, 0,  3, 333333333, -5, 666666667},
1617 
1618             {-1, 666666667, -4, 666666667,  3,         0},
1619             {-1, 666666667, -3,         0,  2, 666666667},
1620             {-1, 666666667, -2,         0,  1, 666666667},
1621             {-1, 666666667, -1,         0,  0, 666666667},
1622             {-1, 666666667, -1, 333333334,  0, 333333333},
1623             {-1, 666666667, -1, 666666667,  0,         0},
1624             {-1, 666666667, -1, 999999999, -1, 666666668},
1625             {-1, 666666667,  0,         0, -1, 666666667},
1626             {-1, 666666667,  0,         1, -1, 666666666},
1627             {-1, 666666667,  0, 333333333, -1, 333333334},
1628             {-1, 666666667,  0, 666666666, -1,         1},
1629             {-1, 666666667,  1,         0, -2, 666666667},
1630             {-1, 666666667,  2,         0, -3, 666666667},
1631             {-1, 666666667,  3,         0, -4, 666666667},
1632             {-1, 666666667,  3, 333333333, -4, 333333334},
1633 
1634             {0, 0, -4, 666666667,  3, 333333333},
1635             {0, 0, -3,         0,  3,         0},
1636             {0, 0, -2,         0,  2,         0},
1637             {0, 0, -1,         0,  1,         0},
1638             {0, 0, -1, 333333334,  0, 666666666},
1639             {0, 0, -1, 666666667,  0, 333333333},
1640             {0, 0, -1, 999999999,  0,         1},
1641             {0, 0,  0,         0,  0,         0},
1642             {0, 0,  0,         1, -1, 999999999},
1643             {0, 0,  0, 333333333, -1, 666666667},
1644             {0, 0,  0, 666666666, -1, 333333334},
1645             {0, 0,  1,         0, -1,         0},
1646             {0, 0,  2,         0, -2,         0},
1647             {0, 0,  3,         0, -3,         0},
1648             {0, 0,  3, 333333333, -4, 666666667},
1649 
1650             {0, 333333333, -4, 666666667,  3, 666666666},
1651             {0, 333333333, -3,         0,  3, 333333333},
1652             {0, 333333333, -2,         0,  2, 333333333},
1653             {0, 333333333, -1,         0,  1, 333333333},
1654             {0, 333333333, -1, 333333334,  0, 999999999},
1655             {0, 333333333, -1, 666666667,  0, 666666666},
1656             {0, 333333333, -1, 999999999,  0, 333333334},
1657             {0, 333333333,  0,         0,  0, 333333333},
1658             {0, 333333333,  0,         1,  0, 333333332},
1659             {0, 333333333,  0, 333333333,  0,         0},
1660             {0, 333333333,  0, 666666666, -1, 666666667},
1661             {0, 333333333,  1,         0, -1, 333333333},
1662             {0, 333333333,  2,         0, -2, 333333333},
1663             {0, 333333333,  3,         0, -3, 333333333},
1664             {0, 333333333,  3, 333333333, -3,         0},
1665 
1666             {1, 0, -4, 666666667,  4, 333333333},
1667             {1, 0, -3,         0,  4,         0},
1668             {1, 0, -2,         0,  3,         0},
1669             {1, 0, -1,         0,  2,         0},
1670             {1, 0, -1, 333333334,  1, 666666666},
1671             {1, 0, -1, 666666667,  1, 333333333},
1672             {1, 0, -1, 999999999,  1,         1},
1673             {1, 0,  0,         0,  1,         0},
1674             {1, 0,  0,         1,  0, 999999999},
1675             {1, 0,  0, 333333333,  0, 666666667},
1676             {1, 0,  0, 666666666,  0, 333333334},
1677             {1, 0,  1,         0,  0,         0},
1678             {1, 0,  2,         0, -1,         0},
1679             {1, 0,  3,         0, -2,         0},
1680             {1, 0,  3, 333333333, -3, 666666667},
1681 
1682             {2, 0, -4, 666666667,  5, 333333333},
1683             {2, 0, -3,         0,  5,         0},
1684             {2, 0, -2,         0,  4,         0},
1685             {2, 0, -1,         0,  3,         0},
1686             {2, 0, -1, 333333334,  2, 666666666},
1687             {2, 0, -1, 666666667,  2, 333333333},
1688             {2, 0, -1, 999999999,  2,         1},
1689             {2, 0,  0,         0,  2,         0},
1690             {2, 0,  0,         1,  1, 999999999},
1691             {2, 0,  0, 333333333,  1, 666666667},
1692             {2, 0,  0, 666666666,  1, 333333334},
1693             {2, 0,  1,         0,  1,         0},
1694             {2, 0,  2,         0,  0,         0},
1695             {2, 0,  3,         0, -1,         0},
1696             {2, 0,  3, 333333333, -2, 666666667},
1697 
1698             {3, 0, -4, 666666667,  6, 333333333},
1699             {3, 0, -3,         0,  6,         0},
1700             {3, 0, -2,         0,  5,         0},
1701             {3, 0, -1,         0,  4,         0},
1702             {3, 0, -1, 333333334,  3, 666666666},
1703             {3, 0, -1, 666666667,  3, 333333333},
1704             {3, 0, -1, 999999999,  3,         1},
1705             {3, 0,  0,         0,  3,         0},
1706             {3, 0,  0,         1,  2, 999999999},
1707             {3, 0,  0, 333333333,  2, 666666667},
1708             {3, 0,  0, 666666666,  2, 333333334},
1709             {3, 0,  1,         0,  2,         0},
1710             {3, 0,  2,         0,  1,         0},
1711             {3, 0,  3,         0,  0,         0},
1712             {3, 0,  3, 333333333, -1, 666666667},
1713 
1714             {3, 333333333, -4, 666666667,  6, 666666666},
1715             {3, 333333333, -3,         0,  6, 333333333},
1716             {3, 333333333, -2,         0,  5, 333333333},
1717             {3, 333333333, -1,         0,  4, 333333333},
1718             {3, 333333333, -1, 333333334,  3, 999999999},
1719             {3, 333333333, -1, 666666667,  3, 666666666},
1720             {3, 333333333, -1, 999999999,  3, 333333334},
1721             {3, 333333333,  0,         0,  3, 333333333},
1722             {3, 333333333,  0,         1,  3, 333333332},
1723             {3, 333333333,  0, 333333333,  3,         0},
1724             {3, 333333333,  0, 666666666,  2, 666666667},
1725             {3, 333333333,  1,         0,  2, 333333333},
1726             {3, 333333333,  2,         0,  1, 333333333},
1727             {3, 333333333,  3,         0,  0, 333333333},
1728             {3, 333333333,  3, 333333333,  0,         0},
1729 
1730             {Long.MAX_VALUE, 0, Long.MAX_VALUE, 0, 0, 0},
1731        };
1732     }
1733 
1734     @Test(dataProvider="Minus")
minus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond)1735     public void minus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
1736        Duration t = Duration.ofSeconds(seconds, nanos).minus(Duration.ofSeconds(otherSeconds, otherNanos));
1737        assertEquals(t.getSeconds(), expectedSeconds);
1738        assertEquals(t.getNano(), expectedNanoOfSecond);
1739     }
1740 
1741     @Test(expectedExceptions=ArithmeticException.class)
minusOverflowTooSmall()1742     public void minusOverflowTooSmall() {
1743        Duration t = Duration.ofSeconds(Long.MIN_VALUE);
1744        t.minus(Duration.ofSeconds(0, 1));
1745     }
1746 
1747     @Test(expectedExceptions=ArithmeticException.class)
minusOverflowTooBig()1748     public void minusOverflowTooBig() {
1749        Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1750        t.minus(Duration.ofSeconds(-1, 999999999));
1751     }
1752 
1753     //-----------------------------------------------------------------------
1754     @Test
minus_longTemporalUnit_seconds()1755     public void minus_longTemporalUnit_seconds() {
1756         Duration t = Duration.ofSeconds(1);
1757         t = t.minus(1, SECONDS);
1758         assertEquals(0, t.getSeconds());
1759         assertEquals(0, t.getNano());
1760      }
1761 
1762     @Test
minus_longTemporalUnit_millis()1763     public void minus_longTemporalUnit_millis() {
1764         Duration t = Duration.ofSeconds(1);
1765         t = t.minus(1, MILLIS);
1766         assertEquals(0, t.getSeconds());
1767         assertEquals(999000000, t.getNano());
1768      }
1769 
1770     @Test
minus_longTemporalUnit_micros()1771     public void minus_longTemporalUnit_micros() {
1772         Duration t = Duration.ofSeconds(1);
1773         t = t.minus(1, MICROS);
1774         assertEquals(0, t.getSeconds());
1775         assertEquals(999999000, t.getNano());
1776      }
1777 
1778     @Test
minus_longTemporalUnit_nanos()1779     public void minus_longTemporalUnit_nanos() {
1780         Duration t = Duration.ofSeconds(1);
1781         t = t.minus(1, NANOS);
1782         assertEquals(0, t.getSeconds());
1783         assertEquals(999999999, t.getNano());
1784      }
1785 
1786     @Test(expectedExceptions=NullPointerException.class)
minus_longTemporalUnit_null()1787     public void minus_longTemporalUnit_null() {
1788        Duration t = Duration.ofSeconds(1);
1789        t.minus(1, (TemporalUnit) null);
1790     }
1791 
1792     //-----------------------------------------------------------------------
1793     @DataProvider(name="MinusDays")
provider_minusDays_long()1794     Object[][] provider_minusDays_long() {
1795         return new Object[][] {
1796             {0, 0, 0},
1797             {0, 1, -1},
1798             {0, -1, 1},
1799             {Long.MAX_VALUE/3600/24, 0, Long.MAX_VALUE/3600/24},
1800             {Long.MIN_VALUE/3600/24, 0, Long.MIN_VALUE/3600/24},
1801             {1, 0, 1},
1802             {1, 1, 0},
1803             {1, -1, 2},
1804             {Long.MAX_VALUE/3600/24, 1, Long.MAX_VALUE/3600/24 - 1},
1805             {Long.MIN_VALUE/3600/24, -1, Long.MIN_VALUE/3600/24 + 1},
1806             {1, 0, 1},
1807             {1, 1, 0},
1808             {1, -1, 2},
1809             {-1, 0, -1},
1810             {-1, 1, -2},
1811             {-1, -1, 0},
1812         };
1813     }
1814 
1815     @Test(dataProvider="MinusDays")
minusDays_long(long days, long amount, long expectedDays)1816     public void minusDays_long(long days, long amount, long expectedDays) {
1817         Duration t = Duration.ofDays(days);
1818         t = t.minusDays(amount);
1819         assertEquals(t.toDays(), expectedDays);
1820     }
1821 
1822     @Test(expectedExceptions = {ArithmeticException.class})
minusDays_long_overflowTooBig()1823     public void minusDays_long_overflowTooBig() {
1824         Duration t = Duration.ofDays(Long.MAX_VALUE/3600/24);
1825         t.minusDays(-1);
1826     }
1827 
1828     @Test(expectedExceptions = {ArithmeticException.class})
minusDays_long_overflowTooSmall()1829     public void minusDays_long_overflowTooSmall() {
1830         Duration t = Duration.ofDays(Long.MIN_VALUE/3600/24);
1831         t.minusDays(1);
1832     }
1833 
1834     //-----------------------------------------------------------------------
1835     @DataProvider(name="MinusHours")
provider_minusHours_long()1836     Object[][] provider_minusHours_long() {
1837         return new Object[][] {
1838             {0, 0, 0},
1839             {0, 1, -1},
1840             {0, -1, 1},
1841             {Long.MAX_VALUE/3600, 0, Long.MAX_VALUE/3600},
1842             {Long.MIN_VALUE/3600, 0, Long.MIN_VALUE/3600},
1843             {1, 0, 1},
1844             {1, 1, 0},
1845             {1, -1, 2},
1846             {Long.MAX_VALUE/3600, 1, Long.MAX_VALUE/3600 - 1},
1847             {Long.MIN_VALUE/3600, -1, Long.MIN_VALUE/3600 + 1},
1848             {1, 0, 1},
1849             {1, 1, 0},
1850             {1, -1, 2},
1851             {-1, 0, -1},
1852             {-1, 1, -2},
1853             {-1, -1, 0},
1854         };
1855     }
1856 
1857     @Test(dataProvider="MinusHours")
minusHours_long(long hours, long amount, long expectedHours)1858     public void minusHours_long(long hours, long amount, long expectedHours) {
1859         Duration t = Duration.ofHours(hours);
1860         t = t.minusHours(amount);
1861         assertEquals(t.toHours(), expectedHours);
1862     }
1863 
1864     @Test(expectedExceptions = {ArithmeticException.class})
minusHours_long_overflowTooBig()1865     public void minusHours_long_overflowTooBig() {
1866         Duration t = Duration.ofHours(Long.MAX_VALUE/3600);
1867         t.minusHours(-1);
1868     }
1869 
1870     @Test(expectedExceptions = {ArithmeticException.class})
minusHours_long_overflowTooSmall()1871     public void minusHours_long_overflowTooSmall() {
1872         Duration t = Duration.ofHours(Long.MIN_VALUE/3600);
1873         t.minusHours(1);
1874     }
1875 
1876     //-----------------------------------------------------------------------
1877     @DataProvider(name="MinusMinutes")
provider_minusminutes_long()1878     Object[][] provider_minusminutes_long() {
1879         return new Object[][] {
1880             {0, 0, 0},
1881             {0, 1, -1},
1882             {0, -1, 1},
1883             {Long.MAX_VALUE/60, 0, Long.MAX_VALUE/60},
1884             {Long.MIN_VALUE/60, 0, Long.MIN_VALUE/60},
1885             {1, 0, 1},
1886             {1, 1, 0},
1887             {1, -1, 2},
1888             {Long.MAX_VALUE/60, 1, Long.MAX_VALUE/60 - 1},
1889             {Long.MIN_VALUE/60, -1, Long.MIN_VALUE/60 + 1},
1890             {1, 0, 1},
1891             {1, 1, 0},
1892             {1, -1, 2},
1893             {-1, 0, -1},
1894             {-1, 1, -2},
1895             {-1, -1, 0},
1896         };
1897     }
1898 
1899     @Test(dataProvider="MinusMinutes")
minusMinutes_long(long minutes, long amount, long expectedMinutes)1900     public void minusMinutes_long(long minutes, long amount, long expectedMinutes) {
1901         Duration t = Duration.ofMinutes(minutes);
1902         t = t.minusMinutes(amount);
1903         assertEquals(t.toMinutes(), expectedMinutes);
1904     }
1905 
1906     @Test(expectedExceptions = {ArithmeticException.class})
minusMinutes_long_overflowTooBig()1907     public void minusMinutes_long_overflowTooBig() {
1908         Duration t = Duration.ofMinutes(Long.MAX_VALUE/60);
1909         t.minusMinutes(-1);
1910     }
1911 
1912     @Test(expectedExceptions = {ArithmeticException.class})
minusMinutes_long_overflowTooSmall()1913     public void minusMinutes_long_overflowTooSmall() {
1914         Duration t = Duration.ofMinutes(Long.MIN_VALUE/60);
1915         t.minusMinutes(1);
1916     }
1917 
1918     //-----------------------------------------------------------------------
1919     @DataProvider(name="MinusSeconds")
provider_minusSeconds_long()1920     Object[][] provider_minusSeconds_long() {
1921         return new Object[][] {
1922             {0, 0, 0, 0, 0},
1923             {0, 0, 1, -1, 0},
1924             {0, 0, -1, 1, 0},
1925             {0, 0, Long.MAX_VALUE, -Long.MAX_VALUE, 0},
1926             {0, 0, Long.MIN_VALUE + 1, Long.MAX_VALUE, 0},
1927             {1, 0, 0, 1, 0},
1928             {1, 0, 1, 0, 0},
1929             {1, 0, -1, 2, 0},
1930             {1, 0, Long.MAX_VALUE - 1, -Long.MAX_VALUE + 2, 0},
1931             {1, 0, Long.MIN_VALUE + 2, Long.MAX_VALUE, 0},
1932             {1, 1, 0, 1, 1},
1933             {1, 1, 1, 0, 1},
1934             {1, 1, -1, 2, 1},
1935             {1, 1, Long.MAX_VALUE, -Long.MAX_VALUE + 1, 1},
1936             {1, 1, Long.MIN_VALUE + 2, Long.MAX_VALUE, 1},
1937             {-1, 1, 0, -1, 1},
1938             {-1, 1, 1, -2, 1},
1939             {-1, 1, -1, 0, 1},
1940             {-1, 1, Long.MAX_VALUE, Long.MIN_VALUE, 1},
1941             {-1, 1, Long.MIN_VALUE + 1, Long.MAX_VALUE - 1, 1},
1942         };
1943     }
1944 
1945     @Test(dataProvider="MinusSeconds")
minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1946     public void minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1947         Duration t = Duration.ofSeconds(seconds, nanos);
1948         t = t.minusSeconds(amount);
1949         assertEquals(t.getSeconds(), expectedSeconds);
1950         assertEquals(t.getNano(), expectedNanoOfSecond);
1951     }
1952 
1953     @Test(expectedExceptions = {ArithmeticException.class})
minusSeconds_long_overflowTooBig()1954     public void minusSeconds_long_overflowTooBig() {
1955         Duration t = Duration.ofSeconds(1, 0);
1956         t.minusSeconds(Long.MIN_VALUE + 1);
1957     }
1958 
1959     @Test(expectedExceptions = {ArithmeticException.class})
minusSeconds_long_overflowTooSmall()1960     public void minusSeconds_long_overflowTooSmall() {
1961         Duration t = Duration.ofSeconds(-2, 0);
1962         t.minusSeconds(Long.MAX_VALUE);
1963     }
1964 
1965     //-----------------------------------------------------------------------
1966     @DataProvider(name="MinusMillis")
provider_minusMillis_long()1967     Object[][] provider_minusMillis_long() {
1968         return new Object[][] {
1969             {0, 0, 0,       0, 0},
1970             {0, 0, 1,      -1, 999000000},
1971             {0, 0, 999,    -1, 1000000},
1972             {0, 0, 1000,   -1, 0},
1973             {0, 0, 1001,   -2, 999000000},
1974             {0, 0, 1999,   -2, 1000000},
1975             {0, 0, 2000,   -2, 0},
1976             {0, 0, -1,      0, 1000000},
1977             {0, 0, -999,    0, 999000000},
1978             {0, 0, -1000,   1, 0},
1979             {0, 0, -1001,   1, 1000000},
1980             {0, 0, -1999,   1, 999000000},
1981 
1982             {0, 1, 0,       0, 1},
1983             {0, 1, 1,      -1, 999000001},
1984             {0, 1, 998,    -1, 2000001},
1985             {0, 1, 999,    -1, 1000001},
1986             {0, 1, 1000,   -1, 1},
1987             {0, 1, 1998,   -2, 2000001},
1988             {0, 1, 1999,   -2, 1000001},
1989             {0, 1, 2000,   -2, 1},
1990             {0, 1, -1,      0, 1000001},
1991             {0, 1, -2,      0, 2000001},
1992             {0, 1, -1000,   1, 1},
1993             {0, 1, -1001,   1, 1000001},
1994 
1995             {0, 1000000, 0,       0, 1000000},
1996             {0, 1000000, 1,       0, 0},
1997             {0, 1000000, 998,    -1, 3000000},
1998             {0, 1000000, 999,    -1, 2000000},
1999             {0, 1000000, 1000,   -1, 1000000},
2000             {0, 1000000, 1998,   -2, 3000000},
2001             {0, 1000000, 1999,   -2, 2000000},
2002             {0, 1000000, 2000,   -2, 1000000},
2003             {0, 1000000, -1,      0, 2000000},
2004             {0, 1000000, -2,      0, 3000000},
2005             {0, 1000000, -999,    1, 0},
2006             {0, 1000000, -1000,   1, 1000000},
2007             {0, 1000000, -1001,   1, 2000000},
2008             {0, 1000000, -1002,   1, 3000000},
2009 
2010             {0, 999999999, 0,     0, 999999999},
2011             {0, 999999999, 1,     0, 998999999},
2012             {0, 999999999, 999,   0, 999999},
2013             {0, 999999999, 1000, -1, 999999999},
2014             {0, 999999999, 1001, -1, 998999999},
2015             {0, 999999999, -1,    1, 999999},
2016             {0, 999999999, -1000, 1, 999999999},
2017             {0, 999999999, -1001, 2, 999999},
2018         };
2019     }
2020 
2021     @Test(dataProvider="MinusMillis")
minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)2022     public void minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
2023         Duration t = Duration.ofSeconds(seconds, nanos);
2024         t = t.minusMillis(amount);
2025         assertEquals(t.getSeconds(), expectedSeconds);
2026         assertEquals(t.getNano(), expectedNanoOfSecond);
2027     }
2028     @Test(dataProvider="MinusMillis")
minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)2029     public void minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
2030         Duration t = Duration.ofSeconds(seconds + 1, nanos);
2031         t = t.minusMillis(amount);
2032         assertEquals(t.getSeconds(), expectedSeconds + 1);
2033         assertEquals(t.getNano(), expectedNanoOfSecond);
2034     }
2035     @Test(dataProvider="MinusMillis")
minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)2036     public void minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
2037         Duration t = Duration.ofSeconds(seconds - 1, nanos);
2038         t = t.minusMillis(amount);
2039         assertEquals(t.getSeconds(), expectedSeconds - 1);
2040         assertEquals(t.getNano(), expectedNanoOfSecond);
2041     }
2042 
2043     @Test
minusMillis_long_max()2044     public void minusMillis_long_max() {
2045         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 998999999);
2046         t = t.minusMillis(-1);
2047         assertEquals(t.getSeconds(), Long.MAX_VALUE);
2048         assertEquals(t.getNano(), 999999999);
2049     }
2050 
2051     @Test(expectedExceptions = {ArithmeticException.class})
minusMillis_long_overflowTooBig()2052     public void minusMillis_long_overflowTooBig() {
2053         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999000000);
2054         t.minusMillis(-1);
2055     }
2056 
2057     @Test
minusMillis_long_min()2058     public void minusMillis_long_min() {
2059         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 1000000);
2060         t = t.minusMillis(1);
2061         assertEquals(t.getSeconds(), Long.MIN_VALUE);
2062         assertEquals(t.getNano(), 0);
2063     }
2064 
2065     @Test(expectedExceptions = {ArithmeticException.class})
minusMillis_long_overflowTooSmall()2066     public void minusMillis_long_overflowTooSmall() {
2067         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
2068         t.minusMillis(1);
2069     }
2070 
2071     //-----------------------------------------------------------------------
2072     @DataProvider(name="MinusNanos")
provider_minusNanos_long()2073     Object[][] provider_minusNanos_long() {
2074         return new Object[][] {
2075             {0, 0, 0,           0, 0},
2076             {0, 0, 1,          -1, 999999999},
2077             {0, 0, 999999999,  -1, 1},
2078             {0, 0, 1000000000, -1, 0},
2079             {0, 0, 1000000001, -2, 999999999},
2080             {0, 0, 1999999999, -2, 1},
2081             {0, 0, 2000000000, -2, 0},
2082             {0, 0, -1,          0, 1},
2083             {0, 0, -999999999,  0, 999999999},
2084             {0, 0, -1000000000, 1, 0},
2085             {0, 0, -1000000001, 1, 1},
2086             {0, 0, -1999999999, 1, 999999999},
2087 
2088             {1, 0, 0,            1, 0},
2089             {1, 0, 1,            0, 999999999},
2090             {1, 0, 999999999,    0, 1},
2091             {1, 0, 1000000000,   0, 0},
2092             {1, 0, 1000000001,  -1, 999999999},
2093             {1, 0, 1999999999,  -1, 1},
2094             {1, 0, 2000000000,  -1, 0},
2095             {1, 0, -1,           1, 1},
2096             {1, 0, -999999999,   1, 999999999},
2097             {1, 0, -1000000000,  2, 0},
2098             {1, 0, -1000000001,  2, 1},
2099             {1, 0, -1999999999,  2, 999999999},
2100 
2101             {-1, 0, 0,           -1, 0},
2102             {-1, 0, 1,           -2, 999999999},
2103             {-1, 0, 999999999,   -2, 1},
2104             {-1, 0, 1000000000,  -2, 0},
2105             {-1, 0, 1000000001,  -3, 999999999},
2106             {-1, 0, 1999999999,  -3, 1},
2107             {-1, 0, 2000000000,  -3, 0},
2108             {-1, 0, -1,          -1, 1},
2109             {-1, 0, -999999999,  -1, 999999999},
2110             {-1, 0, -1000000000,  0, 0},
2111             {-1, 0, -1000000001,  0, 1},
2112             {-1, 0, -1999999999,  0, 999999999},
2113 
2114             {1, 1, 0,           1, 1},
2115             {1, 1, 1,           1, 0},
2116             {1, 1, 999999998,   0, 3},
2117             {1, 1, 999999999,   0, 2},
2118             {1, 1, 1000000000,  0, 1},
2119             {1, 1, 1999999998, -1, 3},
2120             {1, 1, 1999999999, -1, 2},
2121             {1, 1, 2000000000, -1, 1},
2122             {1, 1, -1,          1, 2},
2123             {1, 1, -2,          1, 3},
2124             {1, 1, -1000000000, 2, 1},
2125             {1, 1, -1000000001, 2, 2},
2126             {1, 1, -1000000002, 2, 3},
2127             {1, 1, -2000000000, 3, 1},
2128 
2129             {1, 999999999, 0,           1, 999999999},
2130             {1, 999999999, 1,           1, 999999998},
2131             {1, 999999999, 999999999,   1, 0},
2132             {1, 999999999, 1000000000,  0, 999999999},
2133             {1, 999999999, 1000000001,  0, 999999998},
2134             {1, 999999999, -1,          2, 0},
2135             {1, 999999999, -1000000000, 2, 999999999},
2136             {1, 999999999, -1000000001, 3, 0},
2137             {1, 999999999, -1999999999, 3, 999999998},
2138             {1, 999999999, -2000000000, 3, 999999999},
2139 
2140             {Long.MAX_VALUE, 0, -999999999, Long.MAX_VALUE, 999999999},
2141             {Long.MAX_VALUE - 1, 0, -1999999999, Long.MAX_VALUE, 999999999},
2142             {Long.MIN_VALUE, 1, 1, Long.MIN_VALUE, 0},
2143             {Long.MIN_VALUE + 1, 1, 1000000001, Long.MIN_VALUE, 0},
2144         };
2145     }
2146 
2147     @Test(dataProvider="MinusNanos")
minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)2148     public void minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
2149         Duration t = Duration.ofSeconds(seconds, nanos);
2150         t = t.minusNanos(amount);
2151         assertEquals(t.getSeconds(), expectedSeconds);
2152         assertEquals(t.getNano(), expectedNanoOfSecond);
2153     }
2154 
2155     @Test(expectedExceptions = {ArithmeticException.class})
minusNanos_long_overflowTooBig()2156     public void minusNanos_long_overflowTooBig() {
2157         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
2158         t.minusNanos(-1);
2159     }
2160 
2161     @Test(expectedExceptions = {ArithmeticException.class})
minusNanos_long_overflowTooSmall()2162     public void minusNanos_long_overflowTooSmall() {
2163         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
2164         t.minusNanos(1);
2165     }
2166 
2167     //-----------------------------------------------------------------------
2168     // multipliedBy()
2169     //-----------------------------------------------------------------------
2170     @DataProvider(name="MultipliedBy")
provider_multipliedBy()2171     Object[][] provider_multipliedBy() {
2172        return new Object[][] {
2173           {-4, 666666667, -3,   9, 999999999},
2174           {-4, 666666667, -2,   6, 666666666},
2175           {-4, 666666667, -1,   3, 333333333},
2176           {-4, 666666667,  0,   0,         0},
2177           {-4, 666666667,  1,  -4, 666666667},
2178           {-4, 666666667,  2,  -7, 333333334},
2179           {-4, 666666667,  3, -10, 000000001},
2180 
2181           {-3, 0, -3,  9, 0},
2182           {-3, 0, -2,  6, 0},
2183           {-3, 0, -1,  3, 0},
2184           {-3, 0,  0,  0, 0},
2185           {-3, 0,  1, -3, 0},
2186           {-3, 0,  2, -6, 0},
2187           {-3, 0,  3, -9, 0},
2188 
2189           {-2, 0, -3,  6, 0},
2190           {-2, 0, -2,  4, 0},
2191           {-2, 0, -1,  2, 0},
2192           {-2, 0,  0,  0, 0},
2193           {-2, 0,  1, -2, 0},
2194           {-2, 0,  2, -4, 0},
2195           {-2, 0,  3, -6, 0},
2196 
2197           {-1, 0, -3,  3, 0},
2198           {-1, 0, -2,  2, 0},
2199           {-1, 0, -1,  1, 0},
2200           {-1, 0,  0,  0, 0},
2201           {-1, 0,  1, -1, 0},
2202           {-1, 0,  2, -2, 0},
2203           {-1, 0,  3, -3, 0},
2204 
2205           {-1, 500000000, -3,  1, 500000000},
2206           {-1, 500000000, -2,  1,         0},
2207           {-1, 500000000, -1,  0, 500000000},
2208           {-1, 500000000,  0,  0,         0},
2209           {-1, 500000000,  1, -1, 500000000},
2210           {-1, 500000000,  2, -1,         0},
2211           {-1, 500000000,  3, -2, 500000000},
2212 
2213           {0, 0, -3, 0, 0},
2214           {0, 0, -2, 0, 0},
2215           {0, 0, -1, 0, 0},
2216           {0, 0,  0, 0, 0},
2217           {0, 0,  1, 0, 0},
2218           {0, 0,  2, 0, 0},
2219           {0, 0,  3, 0, 0},
2220 
2221           {0, 500000000, -3, -2, 500000000},
2222           {0, 500000000, -2, -1,         0},
2223           {0, 500000000, -1, -1, 500000000},
2224           {0, 500000000,  0,  0,         0},
2225           {0, 500000000,  1,  0, 500000000},
2226           {0, 500000000,  2,  1,         0},
2227           {0, 500000000,  3,  1, 500000000},
2228 
2229           {1, 0, -3, -3, 0},
2230           {1, 0, -2, -2, 0},
2231           {1, 0, -1, -1, 0},
2232           {1, 0,  0,  0, 0},
2233           {1, 0,  1,  1, 0},
2234           {1, 0,  2,  2, 0},
2235           {1, 0,  3,  3, 0},
2236 
2237           {2, 0, -3, -6, 0},
2238           {2, 0, -2, -4, 0},
2239           {2, 0, -1, -2, 0},
2240           {2, 0,  0,  0, 0},
2241           {2, 0,  1,  2, 0},
2242           {2, 0,  2,  4, 0},
2243           {2, 0,  3,  6, 0},
2244 
2245           {3, 0, -3, -9, 0},
2246           {3, 0, -2, -6, 0},
2247           {3, 0, -1, -3, 0},
2248           {3, 0,  0,  0, 0},
2249           {3, 0,  1,  3, 0},
2250           {3, 0,  2,  6, 0},
2251           {3, 0,  3,  9, 0},
2252 
2253           {3, 333333333, -3, -10, 000000001},
2254           {3, 333333333, -2,  -7, 333333334},
2255           {3, 333333333, -1,  -4, 666666667},
2256           {3, 333333333,  0,   0,         0},
2257           {3, 333333333,  1,   3, 333333333},
2258           {3, 333333333,  2,   6, 666666666},
2259           {3, 333333333,  3,   9, 999999999},
2260        };
2261     }
2262 
2263     @Test(dataProvider="MultipliedBy")
multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos)2264     public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) {
2265         Duration t = Duration.ofSeconds(seconds, nanos);
2266         t = t.multipliedBy(multiplicand);
2267         assertEquals(t.getSeconds(), expectedSeconds);
2268         assertEquals(t.getNano(), expectedNanos);
2269     }
2270 
2271     @Test
multipliedBy_max()2272     public void multipliedBy_max() {
2273         Duration test = Duration.ofSeconds(1);
2274         assertEquals(test.multipliedBy(Long.MAX_VALUE), Duration.ofSeconds(Long.MAX_VALUE));
2275     }
2276 
2277     @Test
multipliedBy_min()2278     public void multipliedBy_min() {
2279         Duration test = Duration.ofSeconds(1);
2280         assertEquals(test.multipliedBy(Long.MIN_VALUE), Duration.ofSeconds(Long.MIN_VALUE));
2281     }
2282 
2283     @Test(expectedExceptions=ArithmeticException.class)
multipliedBy_tooBig()2284     public void multipliedBy_tooBig() {
2285         Duration test = Duration.ofSeconds(1, 1);
2286         test.multipliedBy(Long.MAX_VALUE);
2287     }
2288 
2289     @Test(expectedExceptions=ArithmeticException.class)
multipliedBy_tooBig_negative()2290     public void multipliedBy_tooBig_negative() {
2291         Duration test = Duration.ofSeconds(1, 1);
2292         test.multipliedBy(Long.MIN_VALUE);
2293     }
2294 
2295     //-----------------------------------------------------------------------
2296     //  truncated(TemporalUnit)
2297     //-----------------------------------------------------------------------
2298     TemporalUnit NINETY_MINS = new TemporalUnit() {
2299         @Override
2300         public Duration getDuration() {
2301             return Duration.ofMinutes(90);
2302         }
2303         @Override
2304         public boolean isDurationEstimated() {
2305             return false;
2306         }
2307         @Override
2308         public boolean isDateBased() {
2309             return false;
2310         }
2311         @Override
2312         public boolean isTimeBased() {
2313             return true;
2314         }
2315         @Override
2316         public boolean isSupportedBy(Temporal temporal) {
2317             return false;
2318         }
2319         @Override
2320         public <R extends Temporal> R addTo(R temporal, long amount) {
2321             throw new UnsupportedOperationException();
2322         }
2323         @Override
2324         public long between(Temporal temporal1, Temporal temporal2) {
2325             throw new UnsupportedOperationException();
2326         }
2327         @Override
2328         public String toString() {
2329             return "NinetyMins";
2330         }
2331     };
2332 
2333     TemporalUnit NINETY_FIVE_MINS = new TemporalUnit() {
2334         @Override
2335         public Duration getDuration() {
2336             return Duration.ofMinutes(95);
2337         }
2338         @Override
2339         public boolean isDurationEstimated() {
2340             return false;
2341         }
2342         @Override
2343         public boolean isDateBased() {
2344             return false;
2345         }
2346         @Override
2347         public boolean isTimeBased() {
2348             return false;
2349         }
2350         @Override
2351         public boolean isSupportedBy(Temporal temporal) {
2352             return false;
2353         }
2354         @Override
2355         public <R extends Temporal> R addTo(R temporal, long amount) {
2356             throw new UnsupportedOperationException();
2357         }
2358         @Override
2359         public long between(Temporal temporal1, Temporal temporal2) {
2360             throw new UnsupportedOperationException();
2361         }
2362         @Override
2363         public String toString() {
2364             return "NinetyFiveMins";
2365         }
2366     };
2367 
2368     @DataProvider(name="truncatedToValid")
data_truncatedToValid()2369     Object[][] data_truncatedToValid() {
2370         return new Object[][] {
2371                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), NANOS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789)},
2372                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MICROS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_000)},
2373                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MILLIS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 1230_00_000)},
2374                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), SECONDS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 0)},
2375                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MINUTES, Duration.ofSeconds(86400 + 3600 + 60, 0)},
2376                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), HOURS, Duration.ofSeconds(86400 + 3600, 0)},
2377                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), DAYS, Duration.ofSeconds(86400, 0)},
2378 
2379                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 0, 0)},
2380                 {Duration.ofSeconds(86400 + 7200 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 5400, 0)},
2381                 {Duration.ofSeconds(86400 + 10800 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 10800, 0)},
2382 
2383                 {Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_456_789), MINUTES, Duration.ofSeconds(-86400 - 3600 - 60, 0 )},
2384                 {Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_456_789), MICROS, Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_457_000)},
2385 
2386                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 0), SECONDS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 0)},
2387                 {Duration.ofSeconds(-86400 - 3600 - 120, 0), MINUTES, Duration.ofSeconds(-86400 - 3600 - 120, 0)},
2388 
2389                 {Duration.ofSeconds(-1, 0), SECONDS, Duration.ofSeconds(-1, 0)},
2390                 {Duration.ofSeconds(-1, 123_456_789), SECONDS, Duration.ofSeconds(0, 0)},
2391                 {Duration.ofSeconds(-1, 123_456_789), NANOS, Duration.ofSeconds(0, -876_543_211)},
2392                 {Duration.ofSeconds(0, 123_456_789), SECONDS, Duration.ofSeconds(0, 0)},
2393                 {Duration.ofSeconds(0, 123_456_789), NANOS, Duration.ofSeconds(0, 123_456_789)},
2394         };
2395     }
2396 
2397     @Test(dataProvider="truncatedToValid")
test_truncatedTo_valid(Duration input, TemporalUnit unit, Duration expected)2398     public void test_truncatedTo_valid(Duration input, TemporalUnit unit, Duration expected) {
2399         assertEquals(input.truncatedTo(unit), expected);
2400     }
2401 
2402     @DataProvider(name="truncatedToInvalid")
data_truncatedToInvalid()2403     Object[][] data_truncatedToInvalid() {
2404         return new Object[][] {
2405                 {Duration.ofSeconds(1, 123_456_789), NINETY_FIVE_MINS},
2406                 {Duration.ofSeconds(1, 123_456_789), WEEKS},
2407                 {Duration.ofSeconds(1, 123_456_789), MONTHS},
2408                 {Duration.ofSeconds(1, 123_456_789), YEARS},
2409         };
2410     }
2411 
2412     @Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
test_truncatedTo_invalid(Duration input, TemporalUnit unit)2413     public void test_truncatedTo_invalid(Duration input, TemporalUnit unit) {
2414         input.truncatedTo(unit);
2415     }
2416 
2417     @Test(expectedExceptions=NullPointerException.class)
test_truncatedTo_null()2418     public void test_truncatedTo_null() {
2419         Duration.ofSeconds(1234).truncatedTo(null);
2420     }
2421 
2422     //-----------------------------------------------------------------------
2423     // dividedBy()
2424     //-----------------------------------------------------------------------
2425     @DataProvider(name="DividedBy")
provider_dividedBy()2426     Object[][] provider_dividedBy() {
2427        return new Object[][] {
2428           {-4, 666666667, -3,  1, 111111111},
2429           {-4, 666666667, -2,  1, 666666666},
2430           {-4, 666666667, -1,  3, 333333333},
2431           {-4, 666666667,  1, -4, 666666667},
2432           {-4, 666666667,  2, -2, 333333334},
2433           {-4, 666666667,  3, -2, 888888889},
2434 
2435           {-3, 0, -3,  1, 0},
2436           {-3, 0, -2,  1, 500000000},
2437           {-3, 0, -1,  3, 0},
2438           {-3, 0,  1, -3, 0},
2439           {-3, 0,  2, -2, 500000000},
2440           {-3, 0,  3, -1, 0},
2441 
2442           {-2, 0, -3,  0, 666666666},
2443           {-2, 0, -2,  1,         0},
2444           {-2, 0, -1,  2,         0},
2445           {-2, 0,  1, -2,         0},
2446           {-2, 0,  2, -1,         0},
2447           {-2, 0,  3, -1, 333333334},
2448 
2449           {-1, 0, -3,  0, 333333333},
2450           {-1, 0, -2,  0, 500000000},
2451           {-1, 0, -1,  1,         0},
2452           {-1, 0,  1, -1,         0},
2453           {-1, 0,  2, -1, 500000000},
2454           {-1, 0,  3, -1, 666666667},
2455 
2456           {-1, 500000000, -3,  0, 166666666},
2457           {-1, 500000000, -2,  0, 250000000},
2458           {-1, 500000000, -1,  0, 500000000},
2459           {-1, 500000000,  1, -1, 500000000},
2460           {-1, 500000000,  2, -1, 750000000},
2461           {-1, 500000000,  3, -1, 833333334},
2462 
2463           {0, 0, -3, 0, 0},
2464           {0, 0, -2, 0, 0},
2465           {0, 0, -1, 0, 0},
2466           {0, 0,  1, 0, 0},
2467           {0, 0,  2, 0, 0},
2468           {0, 0,  3, 0, 0},
2469 
2470           {0, 500000000, -3, -1, 833333334},
2471           {0, 500000000, -2, -1, 750000000},
2472           {0, 500000000, -1, -1, 500000000},
2473           {0, 500000000,  1,  0, 500000000},
2474           {0, 500000000,  2,  0, 250000000},
2475           {0, 500000000,  3,  0, 166666666},
2476 
2477           {1, 0, -3, -1, 666666667},
2478           {1, 0, -2, -1, 500000000},
2479           {1, 0, -1, -1,         0},
2480           {1, 0,  1,  1,         0},
2481           {1, 0,  2,  0, 500000000},
2482           {1, 0,  3,  0, 333333333},
2483 
2484           {2, 0, -3, -1, 333333334},
2485           {2, 0, -2, -1,         0},
2486           {2, 0, -1, -2,         0},
2487           {2, 0,  1,  2,         0},
2488           {2, 0,  2,  1,         0},
2489           {2, 0,  3,  0, 666666666},
2490 
2491           {3, 0, -3, -1,         0},
2492           {3, 0, -2, -2, 500000000},
2493           {3, 0, -1, -3,         0},
2494           {3, 0,  1,  3,         0},
2495           {3, 0,  2,  1, 500000000},
2496           {3, 0,  3,  1,         0},
2497 
2498           {3, 333333333, -3, -2, 888888889},
2499           {3, 333333333, -2, -2, 333333334},
2500           {3, 333333333, -1, -4, 666666667},
2501           {3, 333333333,  1,  3, 333333333},
2502           {3, 333333333,  2,  1, 666666666},
2503           {3, 333333333,  3,  1, 111111111},
2504        };
2505     }
2506 
2507     @Test(dataProvider="DividedBy")
dividedBy(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos)2508     public void dividedBy(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos) {
2509         Duration t = Duration.ofSeconds(seconds, nanos);
2510         t = t.dividedBy(divisor);
2511         assertEquals(t.getSeconds(), expectedSeconds);
2512         assertEquals(t.getNano(), expectedNanos);
2513     }
2514 
2515     @Test(dataProvider="DividedBy", expectedExceptions=ArithmeticException.class)
dividedByZero(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos)2516     public void dividedByZero(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos) {
2517        Duration t = Duration.ofSeconds(seconds, nanos);
2518        t.dividedBy(0);
2519        fail(t + " divided by zero did not throw ArithmeticException");
2520     }
2521 
2522     @Test
dividedBy_max()2523     public void dividedBy_max() {
2524         Duration test = Duration.ofSeconds(Long.MAX_VALUE);
2525         assertEquals(test.dividedBy(Long.MAX_VALUE), Duration.ofSeconds(1));
2526     }
2527 
2528     //-----------------------------------------------------------------------
2529     // dividedbyDur()
2530     //-----------------------------------------------------------------------
2531 
2532     @DataProvider(name="dividedByDur_provider")
provider_dividedByDur()2533     Object[][] provider_dividedByDur() {
2534         return new Object[][] {
2535             {Duration.ofSeconds(0, 0), Duration.ofSeconds(1, 0), 0},
2536             {Duration.ofSeconds(1, 0), Duration.ofSeconds(1, 0), 1},
2537             {Duration.ofSeconds(6, 0), Duration.ofSeconds(3, 0), 2},
2538             {Duration.ofSeconds(3, 0), Duration.ofSeconds(6, 0), 0},
2539             {Duration.ofSeconds(7, 0), Duration.ofSeconds(3, 0), 2},
2540 
2541             {Duration.ofSeconds(0, 333_333_333), Duration.ofSeconds(0, 333_333_333), 1},
2542             {Duration.ofSeconds(0, 666_666_666), Duration.ofSeconds(0, 333_333_333), 2},
2543             {Duration.ofSeconds(0, 333_333_333), Duration.ofSeconds(0, 666_666_666), 0},
2544             {Duration.ofSeconds(0, 777_777_777), Duration.ofSeconds(0, 333_333_333), 2},
2545 
2546             {Duration.ofSeconds(-7, 0), Duration.ofSeconds(3, 0), -2},
2547             {Duration.ofSeconds(0, 7), Duration.ofSeconds(0, -3), -2},
2548             {Duration.ofSeconds(0, -777_777_777), Duration.ofSeconds(0, 333_333_333), -2},
2549 
2550             {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), 29},
2551             {Duration.ofSeconds(-432000L, 777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), -29},
2552             {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), -29},
2553             {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(14400L, -333_333_333L), -30},
2554             {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(-14400L, 333_333_333L), -30},
2555             {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(-14400L, -333_333_333L), -29},
2556             {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(-14400L, -333_333_333L), 29},
2557 
2558             {Duration.ofSeconds(Long.MAX_VALUE, 0), Duration.ofSeconds(1, 0), Long.MAX_VALUE},
2559             {Duration.ofSeconds(Long.MAX_VALUE, 0), Duration.ofSeconds(Long.MAX_VALUE, 0), 1},
2560         };
2561     }
2562 
2563     @Test(dataProvider="dividedByDur_provider")
test_dividedByDur(Duration dividend, Duration divisor, long expected)2564     public void test_dividedByDur(Duration dividend, Duration divisor, long expected) {
2565         assertEquals(dividend.dividedBy(divisor), expected);
2566     }
2567 
2568     @Test(expectedExceptions=ArithmeticException.class)
test_dividedByDur_zero()2569     public void test_dividedByDur_zero() {
2570        Duration t = Duration.ofSeconds(1, 0);
2571        t.dividedBy(Duration.ZERO);
2572     }
2573 
2574     @Test(expectedExceptions=NullPointerException.class)
test_dividedByDur_null()2575     public void test_dividedByDur_null() {
2576        Duration t = Duration.ofSeconds(1, 0);
2577        t.dividedBy(null);
2578     }
2579 
2580     @Test(expectedExceptions=ArithmeticException.class)
test_dividedByDur_overflow()2581     public void test_dividedByDur_overflow() {
2582        Duration dur1 = Duration.ofSeconds(Long.MAX_VALUE, 0);
2583        Duration dur2 = Duration.ofNanos(1);
2584        dur1.dividedBy(dur2);
2585     }
2586 
2587     //-----------------------------------------------------------------------
2588     // negated()
2589     //-----------------------------------------------------------------------
2590     @Test
test_negated()2591     public void test_negated() {
2592         assertEquals(Duration.ofSeconds(0).negated(), Duration.ofSeconds(0));
2593         assertEquals(Duration.ofSeconds(12).negated(), Duration.ofSeconds(-12));
2594         assertEquals(Duration.ofSeconds(-12).negated(), Duration.ofSeconds(12));
2595         assertEquals(Duration.ofSeconds(12, 20).negated(), Duration.ofSeconds(-12, -20));
2596         assertEquals(Duration.ofSeconds(12, -20).negated(), Duration.ofSeconds(-12, 20));
2597         assertEquals(Duration.ofSeconds(-12, -20).negated(), Duration.ofSeconds(12, 20));
2598         assertEquals(Duration.ofSeconds(-12, 20).negated(), Duration.ofSeconds(12, -20));
2599         assertEquals(Duration.ofSeconds(Long.MAX_VALUE).negated(), Duration.ofSeconds(-Long.MAX_VALUE));
2600     }
2601 
2602     @Test(expectedExceptions=ArithmeticException.class)
test_negated_overflow()2603     public void test_negated_overflow() {
2604         Duration.ofSeconds(Long.MIN_VALUE).negated();
2605     }
2606 
2607     //-----------------------------------------------------------------------
2608     // abs()
2609     //-----------------------------------------------------------------------
2610     @Test
test_abs()2611     public void test_abs() {
2612         assertEquals(Duration.ofSeconds(0).abs(), Duration.ofSeconds(0));
2613         assertEquals(Duration.ofSeconds(12).abs(), Duration.ofSeconds(12));
2614         assertEquals(Duration.ofSeconds(-12).abs(), Duration.ofSeconds(12));
2615         assertEquals(Duration.ofSeconds(12, 20).abs(), Duration.ofSeconds(12, 20));
2616         assertEquals(Duration.ofSeconds(12, -20).abs(), Duration.ofSeconds(12, -20));
2617         assertEquals(Duration.ofSeconds(-12, -20).abs(), Duration.ofSeconds(12, 20));
2618         assertEquals(Duration.ofSeconds(-12, 20).abs(), Duration.ofSeconds(12, -20));
2619         assertEquals(Duration.ofSeconds(Long.MAX_VALUE).abs(), Duration.ofSeconds(Long.MAX_VALUE));
2620     }
2621 
2622     @Test(expectedExceptions=ArithmeticException.class)
test_abs_overflow()2623     public void test_abs_overflow() {
2624         Duration.ofSeconds(Long.MIN_VALUE).abs();
2625     }
2626 
2627     //-----------------------------------------------------------------------
2628     // toNanos()
2629     //-----------------------------------------------------------------------
2630     // Android-changed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
2631     @Test(enabled = false)
test_toNanos()2632     public void test_toNanos() {
2633         assertEquals(Duration.ofSeconds(321, 123456789).toNanos(), 321123456789L);
2634         assertEquals(Duration.ofNanos(Long.MAX_VALUE).toNanos(), 9223372036854775807L);
2635         assertEquals(Duration.ofNanos(Long.MIN_VALUE).toNanos(), -9223372036854775808L);
2636     }
2637 
2638     @Test
test_toNanos_max()2639     public void test_toNanos_max() {
2640         Duration test = Duration.ofSeconds(0, Long.MAX_VALUE);
2641         assertEquals(test.toNanos(), Long.MAX_VALUE);
2642     }
2643 
2644     @Test(expectedExceptions=ArithmeticException.class)
test_toNanos_tooBig()2645     public void test_toNanos_tooBig() {
2646         Duration test = Duration.ofSeconds(0, Long.MAX_VALUE).plusNanos(1);
2647         test.toNanos();
2648     }
2649 
2650     // Android-changed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
2651     @Test(enabled = false)
test_toNanos_min()2652     public void test_toNanos_min() {
2653         Duration test = Duration.ofSeconds(0, Long.MIN_VALUE);
2654         assertEquals(test.toNanos(), Long.MIN_VALUE);
2655     }
2656 
2657     @Test(expectedExceptions=ArithmeticException.class)
test_toNanos_tooSmall()2658     public void test_toNanos_tooSmall() {
2659         Duration test = Duration.ofSeconds(0, Long.MIN_VALUE).minusNanos(1);
2660         test.toNanos();
2661     }
2662 
2663     //-----------------------------------------------------------------------
2664     // toMillis()
2665     //-----------------------------------------------------------------------
2666     // Android-changed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
2667     @Test(enabled = false)
test_toMillis()2668     public void test_toMillis() {
2669         assertEquals(Duration.ofSeconds(321, 123456789).toMillis(), 321000 + 123);
2670         assertEquals(Duration.ofMillis(Long.MAX_VALUE).toMillis(), 9223372036854775807L);
2671         assertEquals(Duration.ofMillis(Long.MIN_VALUE).toMillis(), -9223372036854775808L);
2672     }
2673 
2674     @Test
test_toMillis_max()2675     public void test_toMillis_max() {
2676         Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, (Long.MAX_VALUE % 1000) * 1000000);
2677         assertEquals(test.toMillis(), Long.MAX_VALUE);
2678     }
2679 
2680     @Test(expectedExceptions=ArithmeticException.class)
test_toMillis_tooBig()2681     public void test_toMillis_tooBig() {
2682         Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, ((Long.MAX_VALUE % 1000) + 1) * 1000000);
2683         test.toMillis();
2684     }
2685 
2686     // Android-changed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
2687     @Test(enabled = false)
test_toMillis_min()2688     public void test_toMillis_min() {
2689         Duration test = Duration.ofSeconds(Long.MIN_VALUE / 1000, (Long.MIN_VALUE % 1000) * 1000000);
2690         assertEquals(test.toMillis(), Long.MIN_VALUE);
2691     }
2692 
2693     @Test(expectedExceptions=ArithmeticException.class)
test_toMillis_tooSmall()2694     public void test_toMillis_tooSmall() {
2695         Duration test = Duration.ofSeconds(Long.MIN_VALUE / 1000, ((Long.MIN_VALUE % 1000) - 1) * 1000000);
2696         test.toMillis();
2697     }
2698 
2699     //-----------------------------------------------------------------------
2700     // toSeconds()
2701     //-----------------------------------------------------------------------
2702     @DataProvider(name="toSeconds_provider")
provider_toSeconds()2703     Object[][] provider_toSeconds() {
2704         return new Object[][] {
2705             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 31556926L},
2706             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -31556927L},
2707             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, 123_456_789), -31556926L},
2708             {Duration.ofSeconds(0), 0L},
2709             {Duration.ofSeconds(0, 123_456_789), 0L},
2710             {Duration.ofSeconds(0, -123_456_789), -1L},
2711             {Duration.ofSeconds(Long.MAX_VALUE), 9223372036854775807L},
2712             {Duration.ofSeconds(Long.MIN_VALUE), -9223372036854775808L},
2713         };
2714     }
2715 
2716     @Test(dataProvider="toSeconds_provider")
test_toSeconds(Duration dur, long seconds)2717     public void test_toSeconds(Duration dur, long seconds) {
2718         assertEquals(dur.toSeconds(), seconds);
2719     }
2720 
2721     //-----------------------------------------------------------------------
2722     // toDaysPart()
2723     //-----------------------------------------------------------------------
2724     @DataProvider(name="toDaysPart_provider")
provider_toDaysPart()2725     Object[][] provider_toDaysPart() {
2726         return new Object[][] {
2727             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 365L},
2728             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -365L},
2729             {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 123_456_789), 0L},
2730             {Duration.ofDays(365), 365L},
2731             {Duration.ofHours(2), 0L},
2732             {Duration.ofHours(-2), 0L},
2733         };
2734     }
2735 
2736     @Test(dataProvider="toDaysPart_provider")
test_toDaysPart(Duration dur, long days)2737     public void test_toDaysPart(Duration dur, long days) {
2738         assertEquals(dur.toDaysPart(), days);
2739     }
2740 
2741     //-----------------------------------------------------------------------
2742     // toHoursPart()
2743     //-----------------------------------------------------------------------
2744     @DataProvider(name="toHoursPart_provider")
provider_toHoursPart()2745     Object[][] provider_toHoursPart() {
2746         return new Object[][] {
2747             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 5},
2748             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -5},
2749             {Duration.ofSeconds(48 * 60 + 46, 123_456_789), 0},
2750             {Duration.ofHours(2), 2},
2751             {Duration.ofHours(-2), -2},
2752         };
2753     }
2754 
2755     @Test(dataProvider="toHoursPart_provider")
test_toHoursPart(Duration dur, int hours)2756     public void test_toHoursPart(Duration dur, int hours) {
2757         assertEquals(dur.toHoursPart(), hours);
2758     }
2759 
2760     //-----------------------------------------------------------------------
2761     // toMinutesPart()
2762     //-----------------------------------------------------------------------
2763     @DataProvider(name="toMinutesPart_provider")
provider_toMinutesPart()2764     Object[][] provider_toMinutesPart() {
2765         return new Object[][] {
2766             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 48},
2767             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -48},
2768             {Duration.ofSeconds(46, 123_456_789),0},
2769             {Duration.ofMinutes(48), 48},
2770             {Duration.ofHours(2), 0},
2771             {Duration.ofHours(-2),0},
2772         };
2773     }
2774 
2775     @Test(dataProvider="toMinutesPart_provider")
test_toMinutesPart(Duration dur, int minutes)2776     public void test_toMinutesPart(Duration dur, int minutes) {
2777         assertEquals(dur.toMinutesPart(), minutes);
2778     }
2779 
2780     //-----------------------------------------------------------------------
2781     // toSecondsPart()
2782     //-----------------------------------------------------------------------
2783     @DataProvider(name="toSecondsPart_provider")
provider_toSecondsPart()2784     Object[][] provider_toSecondsPart() {
2785         return new Object[][] {
2786             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 46},
2787             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -47},
2788             {Duration.ofSeconds(0, 123_456_789), 0},
2789             {Duration.ofSeconds(46), 46},
2790             {Duration.ofHours(2), 0},
2791             {Duration.ofHours(-2), 0},
2792         };
2793     }
2794 
2795     @Test(dataProvider="toSecondsPart_provider")
test_toSecondsPart(Duration dur, int seconds)2796     public void test_toSecondsPart(Duration dur, int seconds) {
2797         assertEquals(dur.toSecondsPart(), seconds);
2798     }
2799 
2800     //-----------------------------------------------------------------------
2801     // toMillisPart()
2802     //-----------------------------------------------------------------------
2803     @DataProvider(name="toMillisPart_provider")
provider_toMillisPart()2804     Object[][] provider_toMillisPart() {
2805         return new Object[][] {
2806             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 123},
2807             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), 876},
2808             {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 0), 0},
2809             {Duration.ofMillis(123), 123},
2810             {Duration.ofHours(2), 0},
2811             {Duration.ofHours(-2), 0},
2812         };
2813     }
2814 
2815     @Test(dataProvider="toMillisPart_provider")
test_toMillisPart(Duration dur, int millis)2816     public void test_toMillisPart(Duration dur, int millis) {
2817         assertEquals(dur.toMillisPart(), millis);
2818     }
2819 
2820     //-----------------------------------------------------------------------
2821     // toNanosPart()
2822     //-----------------------------------------------------------------------
2823     @DataProvider(name="toNanosPart_provider")
provider_toNanosPart()2824     Object[][] provider_toNanosPart() {
2825         return new Object[][] {
2826             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 123_456_789},
2827             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), 876_543_211},
2828             {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 0), 0},
2829             {Duration.ofNanos(123_456_789), 123_456_789},
2830             {Duration.ofHours(2), 0},
2831             {Duration.ofHours(-2), 0},
2832         };
2833     }
2834 
2835     @Test(dataProvider="toNanosPart_provider")
test_toNanosPart(Duration dur, int nanos)2836     public void test_toNanosPart(Duration dur, int nanos) {
2837         assertEquals(dur.toNanosPart(), nanos);
2838     }
2839 
2840     //-----------------------------------------------------------------------
2841     // compareTo()
2842     //-----------------------------------------------------------------------
2843     @Test
test_comparisons()2844     public void test_comparisons() {
2845         doTest_comparisons_Duration(
2846             Duration.ofSeconds(-2L, 0),
2847             Duration.ofSeconds(-2L, 999999998),
2848             Duration.ofSeconds(-2L, 999999999),
2849             Duration.ofSeconds(-1L, 0),
2850             Duration.ofSeconds(-1L, 1),
2851             Duration.ofSeconds(-1L, 999999998),
2852             Duration.ofSeconds(-1L, 999999999),
2853             Duration.ofSeconds(0L, 0),
2854             Duration.ofSeconds(0L, 1),
2855             Duration.ofSeconds(0L, 2),
2856             Duration.ofSeconds(0L, 999999999),
2857             Duration.ofSeconds(1L, 0),
2858             Duration.ofSeconds(2L, 0)
2859         );
2860     }
2861 
doTest_comparisons_Duration(Duration... durations)2862     void doTest_comparisons_Duration(Duration... durations) {
2863         for (int i = 0; i < durations.length; i++) {
2864             Duration a = durations[i];
2865             for (int j = 0; j < durations.length; j++) {
2866                 Duration b = durations[j];
2867                 if (i < j) {
2868                     assertEquals(a.compareTo(b)< 0, true, a + " <=> " + b);
2869                     assertEquals(a.equals(b), false, a + " <=> " + b);
2870                 } else if (i > j) {
2871                     assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b);
2872                     assertEquals(a.equals(b), false, a + " <=> " + b);
2873                 } else {
2874                     assertEquals(a.compareTo(b), 0, a + " <=> " + b);
2875                     assertEquals(a.equals(b), true, a + " <=> " + b);
2876                 }
2877             }
2878         }
2879     }
2880 
2881     @Test(expectedExceptions=NullPointerException.class)
test_compareTo_ObjectNull()2882     public void test_compareTo_ObjectNull() {
2883         Duration a = Duration.ofSeconds(0L, 0);
2884         a.compareTo(null);
2885     }
2886 
2887     @Test(expectedExceptions=ClassCastException.class)
2888     @SuppressWarnings({ "unchecked", "rawtypes" })
compareToNonDuration()2889     public void compareToNonDuration() {
2890        Comparable c = Duration.ofSeconds(0L);
2891        c.compareTo(new Object());
2892     }
2893 
2894     //-----------------------------------------------------------------------
2895     // equals()
2896     //-----------------------------------------------------------------------
2897     @Test
test_equals()2898     public void test_equals() {
2899         Duration test5a = Duration.ofSeconds(5L, 20);
2900         Duration test5b = Duration.ofSeconds(5L, 20);
2901         Duration test5n = Duration.ofSeconds(5L, 30);
2902         Duration test6 = Duration.ofSeconds(6L, 20);
2903 
2904         assertEquals(test5a.equals(test5a), true);
2905         assertEquals(test5a.equals(test5b), true);
2906         assertEquals(test5a.equals(test5n), false);
2907         assertEquals(test5a.equals(test6), false);
2908 
2909         assertEquals(test5b.equals(test5a), true);
2910         assertEquals(test5b.equals(test5b), true);
2911         assertEquals(test5b.equals(test5n), false);
2912         assertEquals(test5b.equals(test6), false);
2913 
2914         assertEquals(test5n.equals(test5a), false);
2915         assertEquals(test5n.equals(test5b), false);
2916         assertEquals(test5n.equals(test5n), true);
2917         assertEquals(test5n.equals(test6), false);
2918 
2919         assertEquals(test6.equals(test5a), false);
2920         assertEquals(test6.equals(test5b), false);
2921         assertEquals(test6.equals(test5n), false);
2922         assertEquals(test6.equals(test6), true);
2923     }
2924 
2925     @Test
test_equals_null()2926     public void test_equals_null() {
2927         Duration test5 = Duration.ofSeconds(5L, 20);
2928         assertEquals(test5.equals(null), false);
2929     }
2930 
2931     @Test
test_equals_otherClass()2932     public void test_equals_otherClass() {
2933         Duration test5 = Duration.ofSeconds(5L, 20);
2934         assertEquals(test5.equals(""), false);
2935     }
2936 
2937     //-----------------------------------------------------------------------
2938     // hashCode()
2939     //-----------------------------------------------------------------------
2940     @Test
test_hashCode()2941     public void test_hashCode() {
2942         Duration test5a = Duration.ofSeconds(5L, 20);
2943         Duration test5b = Duration.ofSeconds(5L, 20);
2944         Duration test5n = Duration.ofSeconds(5L, 30);
2945         Duration test6 = Duration.ofSeconds(6L, 20);
2946 
2947         assertEquals(test5a.hashCode() == test5a.hashCode(), true);
2948         assertEquals(test5a.hashCode() == test5b.hashCode(), true);
2949         assertEquals(test5b.hashCode() == test5b.hashCode(), true);
2950 
2951         assertEquals(test5a.hashCode() == test5n.hashCode(), false);
2952         assertEquals(test5a.hashCode() == test6.hashCode(), false);
2953     }
2954 
2955     //-----------------------------------------------------------------------
2956     @DataProvider(name="withNanos")
provider_withNanos_int()2957     Object[][] provider_withNanos_int() {
2958         return new Object[][] {
2959             {0, 0, 0,           0, 0},
2960             {0, 0, 1,           0, 1},
2961             {0, 0, 999999999,   0, 999999999},
2962 
2963             {1, 0, 0,           1, 0},
2964             {1, 0, 1,           1, 1},
2965             {1, 0, 999999999,   1, 999999999},
2966 
2967             {-1, 0, 0,           -1, 0},
2968             {-1, 0, 1,           -1, 1},
2969             {-1, 0, 999999999,   -1, 999999999},
2970 
2971             {1, 999999999, 0,           1, 0},
2972             {1, 999999999, 1,           1, 1},
2973             {1, 999999998, 2,           1, 2},
2974 
2975             {Long.MAX_VALUE, 0, 999999999, Long.MAX_VALUE, 999999999},
2976             {Long.MIN_VALUE, 0, 999999999, Long.MIN_VALUE, 999999999},
2977         };
2978     }
2979 
2980     @Test(dataProvider="withNanos")
withNanos_long(long seconds, int nanos, int amount, long expectedSeconds, int expectedNanoOfSecond)2981     public void withNanos_long(long seconds, int nanos, int amount, long expectedSeconds, int expectedNanoOfSecond) {
2982         Duration t = Duration.ofSeconds(seconds, nanos);
2983         t = t.withNanos(amount);
2984         assertEquals(t.getSeconds(), expectedSeconds);
2985         assertEquals(t.getNano(), expectedNanoOfSecond);
2986     }
2987 
2988     //-----------------------------------------------------------------------
2989     @DataProvider(name="withSeconds")
provider_withSeconds_long()2990     Object[][] provider_withSeconds_long() {
2991         return new Object[][] {
2992             {0, 0, 0, 0, 0},
2993             {0, 0, 1, 1, 0},
2994             {0, 0, -1, -1, 0},
2995             {0, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0},
2996             {0, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0},
2997 
2998             {1, 0, 0, 0, 0},
2999             {1, 0, 2, 2, 0},
3000             {1, 0, -1, -1, 0},
3001             {1, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0},
3002             {1, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0},
3003 
3004             {-1, 1, 0, 0, 1},
3005             {-1, 1, 1, 1, 1},
3006             {-1, 1, -1, -1, 1},
3007             {-1, 1, Long.MAX_VALUE, Long.MAX_VALUE, 1},
3008             {-1, 1, Long.MIN_VALUE, Long.MIN_VALUE, 1},
3009         };
3010     }
3011 
3012     @Test(dataProvider="withSeconds")
withSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)3013     public void withSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
3014         Duration t = Duration.ofSeconds(seconds, nanos);
3015         t = t.withSeconds(amount);
3016         assertEquals(t.getSeconds(), expectedSeconds);
3017         assertEquals(t.getNano(), expectedNanoOfSecond);
3018     }
3019 
3020     //-----------------------------------------------------------------------
3021     // toString()
3022     //-----------------------------------------------------------------------
3023     @DataProvider(name="toString")
provider_toString()3024     Object[][] provider_toString() {
3025         return new Object[][] {
3026             {0, 0, "PT0S"},
3027             {0, 1, "PT0.000000001S"},
3028             {0, 10, "PT0.00000001S"},
3029             {0, 100, "PT0.0000001S"},
3030             {0, 1000, "PT0.000001S"},
3031             {0, 10000, "PT0.00001S"},
3032             {0, 100000, "PT0.0001S"},
3033             {0, 1000000, "PT0.001S"},
3034             {0, 10000000, "PT0.01S"},
3035             {0, 100000000, "PT0.1S"},
3036             {0, 120000000, "PT0.12S"},
3037             {0, 123000000, "PT0.123S"},
3038             {0, 123400000, "PT0.1234S"},
3039             {0, 123450000, "PT0.12345S"},
3040             {0, 123456000, "PT0.123456S"},
3041             {0, 123456700, "PT0.1234567S"},
3042             {0, 123456780, "PT0.12345678S"},
3043             {0, 123456789, "PT0.123456789S"},
3044             {1, 0, "PT1S"},
3045             {59, 0, "PT59S"},
3046             {60, 0, "PT1M"},
3047             {61, 0, "PT1M1S"},
3048             {3599, 0, "PT59M59S"},
3049             {3600, 0, "PT1H"},
3050             {3601, 0, "PT1H1S"},
3051             {3661, 0, "PT1H1M1S"},
3052             {86399, 0, "PT23H59M59S"},
3053             {86400, 0, "PT24H"},
3054             {59, 0, "PT59S"},
3055             {59, 0, "PT59S"},
3056             {-1, 0, "PT-1S"},
3057             {-1, 1000, "PT-0.999999S"},
3058             {-1, 900000000, "PT-0.1S"},
3059             // Android-removed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
3060             // {-60, 100_000_000, "PT-59.9S"},
3061             // {-59, -900_000_000, "PT-59.9S"},
3062             // {-60, -100_000_000, "PT-1M-0.1S"},
3063             {Long.MAX_VALUE, 0, "PT" + (Long.MAX_VALUE / 3600) + "H" +
3064                     ((Long.MAX_VALUE % 3600) / 60) + "M" + (Long.MAX_VALUE % 60) + "S"},
3065             {Long.MIN_VALUE, 0, "PT" + (Long.MIN_VALUE / 3600) + "H" +
3066                     ((Long.MIN_VALUE % 3600) / 60) + "M" + (Long.MIN_VALUE % 60) + "S"},
3067         };
3068     }
3069 
3070     @Test(dataProvider="toString")
test_toString(long seconds, int nanos, String expected)3071     public void test_toString(long seconds, int nanos, String expected) {
3072         Duration t = Duration.ofSeconds(seconds, nanos);
3073         assertEquals(t.toString(), expected);
3074     }
3075 
3076     //-----------------------------------------------------------------------
3077     @Test(groups="{tck}")
test_duration_getUnits()3078     public void test_duration_getUnits() {
3079         Duration duration = Duration.ofSeconds(5000, 1000);
3080         List<TemporalUnit> units = duration.getUnits();
3081         assertEquals(units.size(), 2, "Period.getUnits length");
3082         assertTrue(units.contains(ChronoUnit.SECONDS), "Period.getUnits contains ChronoUnit.SECONDS");
3083         assertTrue(units.contains(ChronoUnit.NANOS), "contains ChronoUnit.NANOS");
3084     }
3085 
3086     @Test()
test_getUnit()3087     public void test_getUnit() {
3088         Duration test = Duration.ofSeconds(2000, 1000);
3089         long seconds = test.get(ChronoUnit.SECONDS);
3090         assertEquals(seconds, 2000, "duration.get(SECONDS)");
3091         long nanos = test.get(ChronoUnit.NANOS);
3092         assertEquals(nanos, 1000, "duration.get(NANOS)");
3093     }
3094 
3095     @DataProvider(name="BadTemporalUnit")
provider_factory_of_badTemporalUnit()3096     Object[][] provider_factory_of_badTemporalUnit() {
3097         return new Object[][] {
3098             {0, MICROS},
3099             {0, MILLIS},
3100             {0, MINUTES},
3101             {0, HOURS},
3102             {0, HALF_DAYS},
3103             {0, DAYS},
3104             {0, ChronoUnit.MONTHS},
3105             {0, ChronoUnit.YEARS},
3106             {0, ChronoUnit.DECADES},
3107             {0, ChronoUnit.CENTURIES},
3108             {0, ChronoUnit.MILLENNIA},
3109         };
3110     }
3111 
3112     @Test(dataProvider="BadTemporalUnit", expectedExceptions=DateTimeException.class)
test_bad_getUnit(long amount, TemporalUnit unit)3113     public void test_bad_getUnit(long amount, TemporalUnit unit) {
3114         Duration t = Duration.of(amount, unit);
3115         t.get(unit);
3116     }
3117 }
3118