• 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_isPositive()916     public void test_isPositive() {
917         assertEquals(Duration.ofNanos(0).isPositive(), false);
918         assertEquals(Duration.ofSeconds(0).isPositive(), false);
919         assertEquals(Duration.ofNanos(1).isPositive(), true);
920         assertEquals(Duration.ofSeconds(1).isPositive(), true);
921         assertEquals(Duration.ofSeconds(1, 1).isPositive(), true);
922         assertEquals(Duration.ofSeconds(Long.MAX_VALUE, 999_999_999).isPositive(), true);
923         assertEquals(Duration.ofNanos(-1).isPositive(), false);
924         assertEquals(Duration.ofSeconds(-1).isPositive(), false);
925         assertEquals(Duration.ofSeconds(-1, -1).isPositive(), false);
926         assertEquals(Duration.ofSeconds(Long.MIN_VALUE).isPositive(), false);
927     }
928 
929     @Test
test_isNegative()930     public void test_isNegative() {
931         assertEquals(Duration.ofNanos(0).isNegative(), false);
932         assertEquals(Duration.ofSeconds(0).isNegative(), false);
933         assertEquals(Duration.ofNanos(1).isNegative(), false);
934         assertEquals(Duration.ofSeconds(1).isNegative(), false);
935         assertEquals(Duration.ofSeconds(1, 1).isNegative(), false);
936         assertEquals(Duration.ofNanos(-1).isNegative(), true);
937         assertEquals(Duration.ofSeconds(-1).isNegative(), true);
938         assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
939     }
940 
941     //-----------------------------------------------------------------------
942     // plus()
943     //-----------------------------------------------------------------------
944     @DataProvider(name="Plus")
provider_plus()945     Object[][] provider_plus() {
946         return new Object[][] {
947             {Long.MIN_VALUE, 0, Long.MAX_VALUE, 0, -1, 0},
948 
949             {-4, 666666667, -4, 666666667, -7, 333333334},
950             {-4, 666666667, -3,         0, -7, 666666667},
951             {-4, 666666667, -2,         0, -6, 666666667},
952             {-4, 666666667, -1,         0, -5, 666666667},
953             {-4, 666666667, -1, 333333334, -4,         1},
954             {-4, 666666667, -1, 666666667, -4, 333333334},
955             {-4, 666666667, -1, 999999999, -4, 666666666},
956             {-4, 666666667,  0,         0, -4, 666666667},
957             {-4, 666666667,  0,         1, -4, 666666668},
958             {-4, 666666667,  0, 333333333, -3,         0},
959             {-4, 666666667,  0, 666666666, -3, 333333333},
960             {-4, 666666667,  1,         0, -3, 666666667},
961             {-4, 666666667,  2,         0, -2, 666666667},
962             {-4, 666666667,  3,         0, -1, 666666667},
963             {-4, 666666667,  3, 333333333,  0,         0},
964 
965             {-3, 0, -4, 666666667, -7, 666666667},
966             {-3, 0, -3,         0, -6,         0},
967             {-3, 0, -2,         0, -5,         0},
968             {-3, 0, -1,         0, -4,         0},
969             {-3, 0, -1, 333333334, -4, 333333334},
970             {-3, 0, -1, 666666667, -4, 666666667},
971             {-3, 0, -1, 999999999, -4, 999999999},
972             {-3, 0,  0,         0, -3,         0},
973             {-3, 0,  0,         1, -3,         1},
974             {-3, 0,  0, 333333333, -3, 333333333},
975             {-3, 0,  0, 666666666, -3, 666666666},
976             {-3, 0,  1,         0, -2,         0},
977             {-3, 0,  2,         0, -1,         0},
978             {-3, 0,  3,         0,  0,         0},
979             {-3, 0,  3, 333333333,  0, 333333333},
980 
981             {-2, 0, -4, 666666667, -6, 666666667},
982             {-2, 0, -3,         0, -5,         0},
983             {-2, 0, -2,         0, -4,         0},
984             {-2, 0, -1,         0, -3,         0},
985             {-2, 0, -1, 333333334, -3, 333333334},
986             {-2, 0, -1, 666666667, -3, 666666667},
987             {-2, 0, -1, 999999999, -3, 999999999},
988             {-2, 0,  0,         0, -2,         0},
989             {-2, 0,  0,         1, -2,         1},
990             {-2, 0,  0, 333333333, -2, 333333333},
991             {-2, 0,  0, 666666666, -2, 666666666},
992             {-2, 0,  1,         0, -1,         0},
993             {-2, 0,  2,         0,  0,         0},
994             {-2, 0,  3,         0,  1,         0},
995             {-2, 0,  3, 333333333,  1, 333333333},
996 
997             {-1, 0, -4, 666666667, -5, 666666667},
998             {-1, 0, -3,         0, -4,         0},
999             {-1, 0, -2,         0, -3,         0},
1000             {-1, 0, -1,         0, -2,         0},
1001             {-1, 0, -1, 333333334, -2, 333333334},
1002             {-1, 0, -1, 666666667, -2, 666666667},
1003             {-1, 0, -1, 999999999, -2, 999999999},
1004             {-1, 0,  0,         0, -1,         0},
1005             {-1, 0,  0,         1, -1,         1},
1006             {-1, 0,  0, 333333333, -1, 333333333},
1007             {-1, 0,  0, 666666666, -1, 666666666},
1008             {-1, 0,  1,         0,  0,         0},
1009             {-1, 0,  2,         0,  1,         0},
1010             {-1, 0,  3,         0,  2,         0},
1011             {-1, 0,  3, 333333333,  2, 333333333},
1012 
1013             {-1, 666666667, -4, 666666667, -4, 333333334},
1014             {-1, 666666667, -3,         0, -4, 666666667},
1015             {-1, 666666667, -2,         0, -3, 666666667},
1016             {-1, 666666667, -1,         0, -2, 666666667},
1017             {-1, 666666667, -1, 333333334, -1,         1},
1018             {-1, 666666667, -1, 666666667, -1, 333333334},
1019             {-1, 666666667, -1, 999999999, -1, 666666666},
1020             {-1, 666666667,  0,         0, -1, 666666667},
1021             {-1, 666666667,  0,         1, -1, 666666668},
1022             {-1, 666666667,  0, 333333333,  0,         0},
1023             {-1, 666666667,  0, 666666666,  0, 333333333},
1024             {-1, 666666667,  1,         0,  0, 666666667},
1025             {-1, 666666667,  2,         0,  1, 666666667},
1026             {-1, 666666667,  3,         0,  2, 666666667},
1027             {-1, 666666667,  3, 333333333,  3,         0},
1028 
1029             {0, 0, -4, 666666667, -4, 666666667},
1030             {0, 0, -3,         0, -3,         0},
1031             {0, 0, -2,         0, -2,         0},
1032             {0, 0, -1,         0, -1,         0},
1033             {0, 0, -1, 333333334, -1, 333333334},
1034             {0, 0, -1, 666666667, -1, 666666667},
1035             {0, 0, -1, 999999999, -1, 999999999},
1036             {0, 0,  0,         0,  0,         0},
1037             {0, 0,  0,         1,  0,         1},
1038             {0, 0,  0, 333333333,  0, 333333333},
1039             {0, 0,  0, 666666666,  0, 666666666},
1040             {0, 0,  1,         0,  1,         0},
1041             {0, 0,  2,         0,  2,         0},
1042             {0, 0,  3,         0,  3,         0},
1043             {0, 0,  3, 333333333,  3, 333333333},
1044 
1045             {0, 333333333, -4, 666666667, -3,         0},
1046             {0, 333333333, -3,         0, -3, 333333333},
1047             {0, 333333333, -2,         0, -2, 333333333},
1048             {0, 333333333, -1,         0, -1, 333333333},
1049             {0, 333333333, -1, 333333334, -1, 666666667},
1050             {0, 333333333, -1, 666666667,  0,         0},
1051             {0, 333333333, -1, 999999999,  0, 333333332},
1052             {0, 333333333,  0,         0,  0, 333333333},
1053             {0, 333333333,  0,         1,  0, 333333334},
1054             {0, 333333333,  0, 333333333,  0, 666666666},
1055             {0, 333333333,  0, 666666666,  0, 999999999},
1056             {0, 333333333,  1,         0,  1, 333333333},
1057             {0, 333333333,  2,         0,  2, 333333333},
1058             {0, 333333333,  3,         0,  3, 333333333},
1059             {0, 333333333,  3, 333333333,  3, 666666666},
1060 
1061             {1, 0, -4, 666666667, -3, 666666667},
1062             {1, 0, -3,         0, -2,         0},
1063             {1, 0, -2,         0, -1,         0},
1064             {1, 0, -1,         0,  0,         0},
1065             {1, 0, -1, 333333334,  0, 333333334},
1066             {1, 0, -1, 666666667,  0, 666666667},
1067             {1, 0, -1, 999999999,  0, 999999999},
1068             {1, 0,  0,         0,  1,         0},
1069             {1, 0,  0,         1,  1,         1},
1070             {1, 0,  0, 333333333,  1, 333333333},
1071             {1, 0,  0, 666666666,  1, 666666666},
1072             {1, 0,  1,         0,  2,         0},
1073             {1, 0,  2,         0,  3,         0},
1074             {1, 0,  3,         0,  4,         0},
1075             {1, 0,  3, 333333333,  4, 333333333},
1076 
1077             {2, 0, -4, 666666667, -2, 666666667},
1078             {2, 0, -3,         0, -1,         0},
1079             {2, 0, -2,         0,  0,         0},
1080             {2, 0, -1,         0,  1,         0},
1081             {2, 0, -1, 333333334,  1, 333333334},
1082             {2, 0, -1, 666666667,  1, 666666667},
1083             {2, 0, -1, 999999999,  1, 999999999},
1084             {2, 0,  0,         0,  2,         0},
1085             {2, 0,  0,         1,  2,         1},
1086             {2, 0,  0, 333333333,  2, 333333333},
1087             {2, 0,  0, 666666666,  2, 666666666},
1088             {2, 0,  1,         0,  3,         0},
1089             {2, 0,  2,         0,  4,         0},
1090             {2, 0,  3,         0,  5,         0},
1091             {2, 0,  3, 333333333,  5, 333333333},
1092 
1093             {3, 0, -4, 666666667, -1, 666666667},
1094             {3, 0, -3,         0,  0,         0},
1095             {3, 0, -2,         0,  1,         0},
1096             {3, 0, -1,         0,  2,         0},
1097             {3, 0, -1, 333333334,  2, 333333334},
1098             {3, 0, -1, 666666667,  2, 666666667},
1099             {3, 0, -1, 999999999,  2, 999999999},
1100             {3, 0,  0,         0,  3,         0},
1101             {3, 0,  0,         1,  3,         1},
1102             {3, 0,  0, 333333333,  3, 333333333},
1103             {3, 0,  0, 666666666,  3, 666666666},
1104             {3, 0,  1,         0,  4,         0},
1105             {3, 0,  2,         0,  5,         0},
1106             {3, 0,  3,         0,  6,         0},
1107             {3, 0,  3, 333333333,  6, 333333333},
1108 
1109             {3, 333333333, -4, 666666667,  0,         0},
1110             {3, 333333333, -3,         0,  0, 333333333},
1111             {3, 333333333, -2,         0,  1, 333333333},
1112             {3, 333333333, -1,         0,  2, 333333333},
1113             {3, 333333333, -1, 333333334,  2, 666666667},
1114             {3, 333333333, -1, 666666667,  3,         0},
1115             {3, 333333333, -1, 999999999,  3, 333333332},
1116             {3, 333333333,  0,         0,  3, 333333333},
1117             {3, 333333333,  0,         1,  3, 333333334},
1118             {3, 333333333,  0, 333333333,  3, 666666666},
1119             {3, 333333333,  0, 666666666,  3, 999999999},
1120             {3, 333333333,  1,         0,  4, 333333333},
1121             {3, 333333333,  2,         0,  5, 333333333},
1122             {3, 333333333,  3,         0,  6, 333333333},
1123             {3, 333333333,  3, 333333333,  6, 666666666},
1124 
1125             {Long.MAX_VALUE, 0, Long.MIN_VALUE, 0, -1, 0},
1126        };
1127     }
1128 
1129     @Test(dataProvider="Plus")
plus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond)1130     public void plus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
1131        Duration t = Duration.ofSeconds(seconds, nanos).plus(Duration.ofSeconds(otherSeconds, otherNanos));
1132        assertEquals(t.getSeconds(), expectedSeconds);
1133        assertEquals(t.getNano(), expectedNanoOfSecond);
1134     }
1135 
1136     @Test(expectedExceptions=ArithmeticException.class)
plusOverflowTooBig()1137     public void plusOverflowTooBig() {
1138        Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1139        t.plus(Duration.ofSeconds(0, 1));
1140     }
1141 
1142     @Test(expectedExceptions=ArithmeticException.class)
plusOverflowTooSmall()1143     public void plusOverflowTooSmall() {
1144        Duration t = Duration.ofSeconds(Long.MIN_VALUE);
1145        t.plus(Duration.ofSeconds(-1, 999999999));
1146     }
1147 
1148     //-----------------------------------------------------------------------
1149     @Test
plus_longTemporalUnit_seconds()1150     public void plus_longTemporalUnit_seconds() {
1151         Duration t = Duration.ofSeconds(1);
1152         t = t.plus(1, SECONDS);
1153         assertEquals(2, t.getSeconds());
1154         assertEquals(0, t.getNano());
1155      }
1156 
1157     @Test
plus_longTemporalUnit_millis()1158     public void plus_longTemporalUnit_millis() {
1159         Duration t = Duration.ofSeconds(1);
1160         t = t.plus(1, MILLIS);
1161         assertEquals(1, t.getSeconds());
1162         assertEquals(1000000, t.getNano());
1163      }
1164 
1165     @Test
plus_longTemporalUnit_micros()1166     public void plus_longTemporalUnit_micros() {
1167         Duration t = Duration.ofSeconds(1);
1168         t = t.plus(1, MICROS);
1169         assertEquals(1, t.getSeconds());
1170         assertEquals(1000, t.getNano());
1171      }
1172 
1173     @Test
plus_longTemporalUnit_nanos()1174     public void plus_longTemporalUnit_nanos() {
1175         Duration t = Duration.ofSeconds(1);
1176         t = t.plus(1, NANOS);
1177         assertEquals(1, t.getSeconds());
1178         assertEquals(1, t.getNano());
1179      }
1180 
1181     @Test(expectedExceptions=NullPointerException.class)
plus_longTemporalUnit_null()1182     public void plus_longTemporalUnit_null() {
1183        Duration t = Duration.ofSeconds(1);
1184        t.plus(1, (TemporalUnit) null);
1185     }
1186 
1187     //-----------------------------------------------------------------------
1188     @DataProvider(name="PlusDays")
provider_plusDays_long()1189     Object[][] provider_plusDays_long() {
1190         return new Object[][] {
1191             {0, 0, 0},
1192             {0, 1, 1},
1193             {0, -1, -1},
1194             {Long.MAX_VALUE/3600/24, 0, Long.MAX_VALUE/3600/24},
1195             {Long.MIN_VALUE/3600/24, 0, Long.MIN_VALUE/3600/24},
1196             {1, 0, 1},
1197             {1, 1, 2},
1198             {1, -1, 0},
1199             {1, Long.MIN_VALUE/3600/24, Long.MIN_VALUE/3600/24 + 1},
1200             {1, 0, 1},
1201             {1, 1, 2},
1202             {1, -1, 0},
1203             {-1, 0, -1},
1204             {-1, 1, 0},
1205             {-1, -1, -2},
1206             {-1, Long.MAX_VALUE/3600/24, Long.MAX_VALUE/3600/24 - 1},
1207         };
1208     }
1209 
1210     @Test(dataProvider="PlusDays")
plusDays_long(long days, long amount, long expectedDays)1211     public void plusDays_long(long days, long amount, long expectedDays) {
1212         Duration t = Duration.ofDays(days);
1213         t = t.plusDays(amount);
1214         assertEquals(t.toDays(), expectedDays);
1215     }
1216 
1217     @Test(expectedExceptions = {ArithmeticException.class})
plusDays_long_overflowTooBig()1218     public void plusDays_long_overflowTooBig() {
1219         Duration t = Duration.ofDays(1);
1220         t.plusDays(Long.MAX_VALUE/3600/24);
1221     }
1222 
1223     @Test(expectedExceptions = {ArithmeticException.class})
plusDays_long_overflowTooSmall()1224     public void plusDays_long_overflowTooSmall() {
1225         Duration t = Duration.ofDays(-1);
1226         t.plusDays(Long.MIN_VALUE/3600/24);
1227     }
1228 
1229     //-----------------------------------------------------------------------
1230     @DataProvider(name="PlusHours")
provider_plusHours_long()1231     Object[][] provider_plusHours_long() {
1232         return new Object[][] {
1233             {0, 0, 0},
1234             {0, 1, 1},
1235             {0, -1, -1},
1236             {Long.MAX_VALUE/3600, 0, Long.MAX_VALUE/3600},
1237             {Long.MIN_VALUE/3600, 0, Long.MIN_VALUE/3600},
1238             {1, 0, 1},
1239             {1, 1, 2},
1240             {1, -1, 0},
1241             {1, Long.MIN_VALUE/3600, Long.MIN_VALUE/3600 + 1},
1242             {1, 0, 1},
1243             {1, 1, 2},
1244             {1, -1, 0},
1245             {-1, 0, -1},
1246             {-1, 1, 0},
1247             {-1, -1, -2},
1248             {-1, Long.MAX_VALUE/3600, Long.MAX_VALUE/3600 - 1},
1249         };
1250     }
1251 
1252     @Test(dataProvider="PlusHours")
plusHours_long(long hours, long amount, long expectedHours)1253     public void plusHours_long(long hours, long amount, long expectedHours) {
1254         Duration t = Duration.ofHours(hours);
1255         t = t.plusHours(amount);
1256         assertEquals(t.toHours(), expectedHours);
1257     }
1258 
1259     @Test(expectedExceptions = {ArithmeticException.class})
plusHours_long_overflowTooBig()1260     public void plusHours_long_overflowTooBig() {
1261         Duration t = Duration.ofHours(1);
1262         t.plusHours(Long.MAX_VALUE/3600);
1263     }
1264 
1265     @Test(expectedExceptions = {ArithmeticException.class})
plusHours_long_overflowTooSmall()1266     public void plusHours_long_overflowTooSmall() {
1267         Duration t = Duration.ofHours(-1);
1268         t.plusHours(Long.MIN_VALUE/3600);
1269     }
1270 
1271     //-----------------------------------------------------------------------
1272     @DataProvider(name="PlusMinutes")
provider_plusMinutes_long()1273     Object[][] provider_plusMinutes_long() {
1274         return new Object[][] {
1275             {0, 0, 0},
1276             {0, 1, 1},
1277             {0, -1, -1},
1278             {Long.MAX_VALUE/60, 0, Long.MAX_VALUE/60},
1279             {Long.MIN_VALUE/60, 0, Long.MIN_VALUE/60},
1280             {1, 0, 1},
1281             {1, 1, 2},
1282             {1, -1, 0},
1283             {1, Long.MIN_VALUE/60, Long.MIN_VALUE/60 + 1},
1284             {1, 0, 1},
1285             {1, 1, 2},
1286             {1, -1, 0},
1287             {-1, 0, -1},
1288             {-1, 1, 0},
1289             {-1, -1, -2},
1290             {-1, Long.MAX_VALUE/60, Long.MAX_VALUE/60 - 1},
1291         };
1292     }
1293 
1294     @Test(dataProvider="PlusMinutes")
plusMinutes_long(long minutes, long amount, long expectedMinutes)1295     public void plusMinutes_long(long minutes, long amount, long expectedMinutes) {
1296         Duration t = Duration.ofMinutes(minutes);
1297         t = t.plusMinutes(amount);
1298         assertEquals(t.toMinutes(), expectedMinutes);
1299     }
1300 
1301     @Test(expectedExceptions = {ArithmeticException.class})
plusMinutes_long_overflowTooBig()1302     public void plusMinutes_long_overflowTooBig() {
1303         Duration t = Duration.ofMinutes(1);
1304         t.plusMinutes(Long.MAX_VALUE/60);
1305     }
1306 
1307     @Test(expectedExceptions = {ArithmeticException.class})
plusMinutes_long_overflowTooSmall()1308     public void plusMinutes_long_overflowTooSmall() {
1309         Duration t = Duration.ofMinutes(-1);
1310         t.plusMinutes(Long.MIN_VALUE/60);
1311     }
1312 
1313     //-----------------------------------------------------------------------
1314     @DataProvider(name="PlusSeconds")
provider_plusSeconds_long()1315     Object[][] provider_plusSeconds_long() {
1316         return new Object[][] {
1317             {0, 0, 0, 0, 0},
1318             {0, 0, 1, 1, 0},
1319             {0, 0, -1, -1, 0},
1320             {0, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0},
1321             {0, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0},
1322             {1, 0, 0, 1, 0},
1323             {1, 0, 1, 2, 0},
1324             {1, 0, -1, 0, 0},
1325             {1, 0, Long.MAX_VALUE - 1, Long.MAX_VALUE, 0},
1326             {1, 0, Long.MIN_VALUE, Long.MIN_VALUE + 1, 0},
1327             {1, 1, 0, 1, 1},
1328             {1, 1, 1, 2, 1},
1329             {1, 1, -1, 0, 1},
1330             {1, 1, Long.MAX_VALUE - 1, Long.MAX_VALUE, 1},
1331             {1, 1, Long.MIN_VALUE, Long.MIN_VALUE + 1, 1},
1332             {-1, 1, 0, -1, 1},
1333             {-1, 1, 1, 0, 1},
1334             {-1, 1, -1, -2, 1},
1335             {-1, 1, Long.MAX_VALUE, Long.MAX_VALUE - 1, 1},
1336             {-1, 1, Long.MIN_VALUE + 1, Long.MIN_VALUE, 1},
1337         };
1338     }
1339 
1340     @Test(dataProvider="PlusSeconds")
plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1341     public void plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1342         Duration t = Duration.ofSeconds(seconds, nanos);
1343         t = t.plusSeconds(amount);
1344         assertEquals(t.getSeconds(), expectedSeconds);
1345         assertEquals(t.getNano(), expectedNanoOfSecond);
1346     }
1347 
1348     @Test(expectedExceptions = {ArithmeticException.class})
plusSeconds_long_overflowTooBig()1349     public void plusSeconds_long_overflowTooBig() {
1350         Duration t = Duration.ofSeconds(1, 0);
1351         t.plusSeconds(Long.MAX_VALUE);
1352     }
1353 
1354     @Test(expectedExceptions = {ArithmeticException.class})
plusSeconds_long_overflowTooSmall()1355     public void plusSeconds_long_overflowTooSmall() {
1356         Duration t = Duration.ofSeconds(-1, 0);
1357         t.plusSeconds(Long.MIN_VALUE);
1358     }
1359 
1360     //-----------------------------------------------------------------------
1361     @DataProvider(name="PlusMillis")
provider_plusMillis_long()1362     Object[][] provider_plusMillis_long() {
1363         return new Object[][] {
1364             {0, 0, 0,       0, 0},
1365             {0, 0, 1,       0, 1000000},
1366             {0, 0, 999,     0, 999000000},
1367             {0, 0, 1000,    1, 0},
1368             {0, 0, 1001,    1, 1000000},
1369             {0, 0, 1999,    1, 999000000},
1370             {0, 0, 2000,    2, 0},
1371             {0, 0, -1,      -1, 999000000},
1372             {0, 0, -999,    -1, 1000000},
1373             {0, 0, -1000,   -1, 0},
1374             {0, 0, -1001,   -2, 999000000},
1375             {0, 0, -1999,   -2, 1000000},
1376 
1377             {0, 1, 0,       0, 1},
1378             {0, 1, 1,       0, 1000001},
1379             {0, 1, 998,     0, 998000001},
1380             {0, 1, 999,     0, 999000001},
1381             {0, 1, 1000,    1, 1},
1382             {0, 1, 1998,    1, 998000001},
1383             {0, 1, 1999,    1, 999000001},
1384             {0, 1, 2000,    2, 1},
1385             {0, 1, -1,      -1, 999000001},
1386             {0, 1, -2,      -1, 998000001},
1387             {0, 1, -1000,   -1, 1},
1388             {0, 1, -1001,   -2, 999000001},
1389 
1390             {0, 1000000, 0,       0, 1000000},
1391             {0, 1000000, 1,       0, 2000000},
1392             {0, 1000000, 998,     0, 999000000},
1393             {0, 1000000, 999,     1, 0},
1394             {0, 1000000, 1000,    1, 1000000},
1395             {0, 1000000, 1998,    1, 999000000},
1396             {0, 1000000, 1999,    2, 0},
1397             {0, 1000000, 2000,    2, 1000000},
1398             {0, 1000000, -1,      0, 0},
1399             {0, 1000000, -2,      -1, 999000000},
1400             {0, 1000000, -999,    -1, 2000000},
1401             {0, 1000000, -1000,   -1, 1000000},
1402             {0, 1000000, -1001,   -1, 0},
1403             {0, 1000000, -1002,   -2, 999000000},
1404 
1405             {0, 999999999, 0,     0, 999999999},
1406             {0, 999999999, 1,     1, 999999},
1407             {0, 999999999, 999,   1, 998999999},
1408             {0, 999999999, 1000,  1, 999999999},
1409             {0, 999999999, 1001,  2, 999999},
1410             {0, 999999999, -1,    0, 998999999},
1411             {0, 999999999, -1000, -1, 999999999},
1412             {0, 999999999, -1001, -1, 998999999},
1413         };
1414     }
1415 
1416     @Test(dataProvider="PlusMillis")
plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1417     public void plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1418         Duration t = Duration.ofSeconds(seconds, nanos);
1419         t = t.plusMillis(amount);
1420         assertEquals(t.getSeconds(), expectedSeconds);
1421         assertEquals(t.getNano(), expectedNanoOfSecond);
1422     }
1423     @Test(dataProvider="PlusMillis")
plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1424     public void plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1425         Duration t = Duration.ofSeconds(seconds + 1, nanos);
1426         t = t.plusMillis(amount);
1427         assertEquals(t.getSeconds(), expectedSeconds + 1);
1428         assertEquals(t.getNano(), expectedNanoOfSecond);
1429     }
1430     @Test(dataProvider="PlusMillis")
plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1431     public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1432         Duration t = Duration.ofSeconds(seconds - 1, nanos);
1433         t = t.plusMillis(amount);
1434         assertEquals(t.getSeconds(), expectedSeconds - 1);
1435         assertEquals(t.getNano(), expectedNanoOfSecond);
1436     }
1437 
1438     @Test
plusMillis_long_max()1439     public void plusMillis_long_max() {
1440         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 998999999);
1441         t = t.plusMillis(1);
1442         assertEquals(t.getSeconds(), Long.MAX_VALUE);
1443         assertEquals(t.getNano(), 999999999);
1444     }
1445 
1446     @Test(expectedExceptions = {ArithmeticException.class})
plusMillis_long_overflowTooBig()1447     public void plusMillis_long_overflowTooBig() {
1448         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999000000);
1449         t.plusMillis(1);
1450     }
1451 
1452     @Test
plusMillis_long_min()1453     public void plusMillis_long_min() {
1454         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 1000000);
1455         t = t.plusMillis(-1);
1456         assertEquals(t.getSeconds(), Long.MIN_VALUE);
1457         assertEquals(t.getNano(), 0);
1458     }
1459 
1460     @Test(expectedExceptions = {ArithmeticException.class})
plusMillis_long_overflowTooSmall()1461     public void plusMillis_long_overflowTooSmall() {
1462         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
1463         t.plusMillis(-1);
1464     }
1465 
1466     //-----------------------------------------------------------------------
1467     @DataProvider(name="PlusNanos")
provider_plusNanos_long()1468     Object[][] provider_plusNanos_long() {
1469         return new Object[][] {
1470             {0, 0, 0,           0, 0},
1471             {0, 0, 1,           0, 1},
1472             {0, 0, 999999999,   0, 999999999},
1473             {0, 0, 1000000000,  1, 0},
1474             {0, 0, 1000000001,  1, 1},
1475             {0, 0, 1999999999,  1, 999999999},
1476             {0, 0, 2000000000,  2, 0},
1477             {0, 0, -1,          -1, 999999999},
1478             {0, 0, -999999999,  -1, 1},
1479             {0, 0, -1000000000, -1, 0},
1480             {0, 0, -1000000001, -2, 999999999},
1481             {0, 0, -1999999999, -2, 1},
1482 
1483             {1, 0, 0,           1, 0},
1484             {1, 0, 1,           1, 1},
1485             {1, 0, 999999999,   1, 999999999},
1486             {1, 0, 1000000000,  2, 0},
1487             {1, 0, 1000000001,  2, 1},
1488             {1, 0, 1999999999,  2, 999999999},
1489             {1, 0, 2000000000,  3, 0},
1490             {1, 0, -1,          0, 999999999},
1491             {1, 0, -999999999,  0, 1},
1492             {1, 0, -1000000000, 0, 0},
1493             {1, 0, -1000000001, -1, 999999999},
1494             {1, 0, -1999999999, -1, 1},
1495 
1496             {-1, 0, 0,           -1, 0},
1497             {-1, 0, 1,           -1, 1},
1498             {-1, 0, 999999999,   -1, 999999999},
1499             {-1, 0, 1000000000,  0, 0},
1500             {-1, 0, 1000000001,  0, 1},
1501             {-1, 0, 1999999999,  0, 999999999},
1502             {-1, 0, 2000000000,  1, 0},
1503             {-1, 0, -1,          -2, 999999999},
1504             {-1, 0, -999999999,  -2, 1},
1505             {-1, 0, -1000000000, -2, 0},
1506             {-1, 0, -1000000001, -3, 999999999},
1507             {-1, 0, -1999999999, -3, 1},
1508 
1509             {1, 1, 0,           1, 1},
1510             {1, 1, 1,           1, 2},
1511             {1, 1, 999999998,   1, 999999999},
1512             {1, 1, 999999999,   2, 0},
1513             {1, 1, 1000000000,  2, 1},
1514             {1, 1, 1999999998,  2, 999999999},
1515             {1, 1, 1999999999,  3, 0},
1516             {1, 1, 2000000000,  3, 1},
1517             {1, 1, -1,          1, 0},
1518             {1, 1, -2,          0, 999999999},
1519             {1, 1, -1000000000, 0, 1},
1520             {1, 1, -1000000001, 0, 0},
1521             {1, 1, -1000000002, -1, 999999999},
1522             {1, 1, -2000000000, -1, 1},
1523 
1524             {1, 999999999, 0,           1, 999999999},
1525             {1, 999999999, 1,           2, 0},
1526             {1, 999999999, 999999999,   2, 999999998},
1527             {1, 999999999, 1000000000,  2, 999999999},
1528             {1, 999999999, 1000000001,  3, 0},
1529             {1, 999999999, -1,          1, 999999998},
1530             {1, 999999999, -1000000000, 0, 999999999},
1531             {1, 999999999, -1000000001, 0, 999999998},
1532             {1, 999999999, -1999999999, 0, 0},
1533             {1, 999999999, -2000000000, -1, 999999999},
1534 
1535             {Long.MAX_VALUE, 0, 999999999, Long.MAX_VALUE, 999999999},
1536             {Long.MAX_VALUE - 1, 0, 1999999999, Long.MAX_VALUE, 999999999},
1537             {Long.MIN_VALUE, 1, -1, Long.MIN_VALUE, 0},
1538             {Long.MIN_VALUE + 1, 1, -1000000001, Long.MIN_VALUE, 0},
1539         };
1540     }
1541 
1542     @Test(dataProvider="PlusNanos")
plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1543     public void plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1544         Duration t = Duration.ofSeconds(seconds, nanos);
1545         t = t.plusNanos(amount);
1546         assertEquals(t.getSeconds(), expectedSeconds);
1547         assertEquals(t.getNano(), expectedNanoOfSecond);
1548     }
1549 
1550     @Test(expectedExceptions = {ArithmeticException.class})
plusNanos_long_overflowTooBig()1551     public void plusNanos_long_overflowTooBig() {
1552         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1553         t.plusNanos(1);
1554     }
1555 
1556     @Test(expectedExceptions = {ArithmeticException.class})
plusNanos_long_overflowTooSmall()1557     public void plusNanos_long_overflowTooSmall() {
1558         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
1559         t.plusNanos(-1);
1560     }
1561 
1562     //-----------------------------------------------------------------------
1563     @DataProvider(name="Minus")
provider_minus()1564     Object[][] provider_minus() {
1565         return new Object[][] {
1566             {Long.MIN_VALUE, 0, Long.MIN_VALUE + 1, 0, -1, 0},
1567 
1568             {-4, 666666667, -4, 666666667,  0,         0},
1569             {-4, 666666667, -3,         0, -1, 666666667},
1570             {-4, 666666667, -2,         0, -2, 666666667},
1571             {-4, 666666667, -1,         0, -3, 666666667},
1572             {-4, 666666667, -1, 333333334, -3, 333333333},
1573             {-4, 666666667, -1, 666666667, -3,         0},
1574             {-4, 666666667, -1, 999999999, -4, 666666668},
1575             {-4, 666666667,  0,         0, -4, 666666667},
1576             {-4, 666666667,  0,         1, -4, 666666666},
1577             {-4, 666666667,  0, 333333333, -4, 333333334},
1578             {-4, 666666667,  0, 666666666, -4,         1},
1579             {-4, 666666667,  1,         0, -5, 666666667},
1580             {-4, 666666667,  2,         0, -6, 666666667},
1581             {-4, 666666667,  3,         0, -7, 666666667},
1582             {-4, 666666667,  3, 333333333, -7, 333333334},
1583 
1584             {-3, 0, -4, 666666667,  0, 333333333},
1585             {-3, 0, -3,         0,  0,         0},
1586             {-3, 0, -2,         0, -1,         0},
1587             {-3, 0, -1,         0, -2,         0},
1588             {-3, 0, -1, 333333334, -3, 666666666},
1589             {-3, 0, -1, 666666667, -3, 333333333},
1590             {-3, 0, -1, 999999999, -3,         1},
1591             {-3, 0,  0,         0, -3,         0},
1592             {-3, 0,  0,         1, -4, 999999999},
1593             {-3, 0,  0, 333333333, -4, 666666667},
1594             {-3, 0,  0, 666666666, -4, 333333334},
1595             {-3, 0,  1,         0, -4,         0},
1596             {-3, 0,  2,         0, -5,         0},
1597             {-3, 0,  3,         0, -6,         0},
1598             {-3, 0,  3, 333333333, -7, 666666667},
1599 
1600             {-2, 0, -4, 666666667,  1, 333333333},
1601             {-2, 0, -3,         0,  1,         0},
1602             {-2, 0, -2,         0,  0,         0},
1603             {-2, 0, -1,         0, -1,         0},
1604             {-2, 0, -1, 333333334, -2, 666666666},
1605             {-2, 0, -1, 666666667, -2, 333333333},
1606             {-2, 0, -1, 999999999, -2,         1},
1607             {-2, 0,  0,         0, -2,         0},
1608             {-2, 0,  0,         1, -3, 999999999},
1609             {-2, 0,  0, 333333333, -3, 666666667},
1610             {-2, 0,  0, 666666666, -3, 333333334},
1611             {-2, 0,  1,         0, -3,         0},
1612             {-2, 0,  2,         0, -4,         0},
1613             {-2, 0,  3,         0, -5,         0},
1614             {-2, 0,  3, 333333333, -6, 666666667},
1615 
1616             {-1, 0, -4, 666666667,  2, 333333333},
1617             {-1, 0, -3,         0,  2,         0},
1618             {-1, 0, -2,         0,  1,         0},
1619             {-1, 0, -1,         0,  0,         0},
1620             {-1, 0, -1, 333333334, -1, 666666666},
1621             {-1, 0, -1, 666666667, -1, 333333333},
1622             {-1, 0, -1, 999999999, -1,         1},
1623             {-1, 0,  0,         0, -1,         0},
1624             {-1, 0,  0,         1, -2, 999999999},
1625             {-1, 0,  0, 333333333, -2, 666666667},
1626             {-1, 0,  0, 666666666, -2, 333333334},
1627             {-1, 0,  1,         0, -2,         0},
1628             {-1, 0,  2,         0, -3,         0},
1629             {-1, 0,  3,         0, -4,         0},
1630             {-1, 0,  3, 333333333, -5, 666666667},
1631 
1632             {-1, 666666667, -4, 666666667,  3,         0},
1633             {-1, 666666667, -3,         0,  2, 666666667},
1634             {-1, 666666667, -2,         0,  1, 666666667},
1635             {-1, 666666667, -1,         0,  0, 666666667},
1636             {-1, 666666667, -1, 333333334,  0, 333333333},
1637             {-1, 666666667, -1, 666666667,  0,         0},
1638             {-1, 666666667, -1, 999999999, -1, 666666668},
1639             {-1, 666666667,  0,         0, -1, 666666667},
1640             {-1, 666666667,  0,         1, -1, 666666666},
1641             {-1, 666666667,  0, 333333333, -1, 333333334},
1642             {-1, 666666667,  0, 666666666, -1,         1},
1643             {-1, 666666667,  1,         0, -2, 666666667},
1644             {-1, 666666667,  2,         0, -3, 666666667},
1645             {-1, 666666667,  3,         0, -4, 666666667},
1646             {-1, 666666667,  3, 333333333, -4, 333333334},
1647 
1648             {0, 0, -4, 666666667,  3, 333333333},
1649             {0, 0, -3,         0,  3,         0},
1650             {0, 0, -2,         0,  2,         0},
1651             {0, 0, -1,         0,  1,         0},
1652             {0, 0, -1, 333333334,  0, 666666666},
1653             {0, 0, -1, 666666667,  0, 333333333},
1654             {0, 0, -1, 999999999,  0,         1},
1655             {0, 0,  0,         0,  0,         0},
1656             {0, 0,  0,         1, -1, 999999999},
1657             {0, 0,  0, 333333333, -1, 666666667},
1658             {0, 0,  0, 666666666, -1, 333333334},
1659             {0, 0,  1,         0, -1,         0},
1660             {0, 0,  2,         0, -2,         0},
1661             {0, 0,  3,         0, -3,         0},
1662             {0, 0,  3, 333333333, -4, 666666667},
1663 
1664             {0, 333333333, -4, 666666667,  3, 666666666},
1665             {0, 333333333, -3,         0,  3, 333333333},
1666             {0, 333333333, -2,         0,  2, 333333333},
1667             {0, 333333333, -1,         0,  1, 333333333},
1668             {0, 333333333, -1, 333333334,  0, 999999999},
1669             {0, 333333333, -1, 666666667,  0, 666666666},
1670             {0, 333333333, -1, 999999999,  0, 333333334},
1671             {0, 333333333,  0,         0,  0, 333333333},
1672             {0, 333333333,  0,         1,  0, 333333332},
1673             {0, 333333333,  0, 333333333,  0,         0},
1674             {0, 333333333,  0, 666666666, -1, 666666667},
1675             {0, 333333333,  1,         0, -1, 333333333},
1676             {0, 333333333,  2,         0, -2, 333333333},
1677             {0, 333333333,  3,         0, -3, 333333333},
1678             {0, 333333333,  3, 333333333, -3,         0},
1679 
1680             {1, 0, -4, 666666667,  4, 333333333},
1681             {1, 0, -3,         0,  4,         0},
1682             {1, 0, -2,         0,  3,         0},
1683             {1, 0, -1,         0,  2,         0},
1684             {1, 0, -1, 333333334,  1, 666666666},
1685             {1, 0, -1, 666666667,  1, 333333333},
1686             {1, 0, -1, 999999999,  1,         1},
1687             {1, 0,  0,         0,  1,         0},
1688             {1, 0,  0,         1,  0, 999999999},
1689             {1, 0,  0, 333333333,  0, 666666667},
1690             {1, 0,  0, 666666666,  0, 333333334},
1691             {1, 0,  1,         0,  0,         0},
1692             {1, 0,  2,         0, -1,         0},
1693             {1, 0,  3,         0, -2,         0},
1694             {1, 0,  3, 333333333, -3, 666666667},
1695 
1696             {2, 0, -4, 666666667,  5, 333333333},
1697             {2, 0, -3,         0,  5,         0},
1698             {2, 0, -2,         0,  4,         0},
1699             {2, 0, -1,         0,  3,         0},
1700             {2, 0, -1, 333333334,  2, 666666666},
1701             {2, 0, -1, 666666667,  2, 333333333},
1702             {2, 0, -1, 999999999,  2,         1},
1703             {2, 0,  0,         0,  2,         0},
1704             {2, 0,  0,         1,  1, 999999999},
1705             {2, 0,  0, 333333333,  1, 666666667},
1706             {2, 0,  0, 666666666,  1, 333333334},
1707             {2, 0,  1,         0,  1,         0},
1708             {2, 0,  2,         0,  0,         0},
1709             {2, 0,  3,         0, -1,         0},
1710             {2, 0,  3, 333333333, -2, 666666667},
1711 
1712             {3, 0, -4, 666666667,  6, 333333333},
1713             {3, 0, -3,         0,  6,         0},
1714             {3, 0, -2,         0,  5,         0},
1715             {3, 0, -1,         0,  4,         0},
1716             {3, 0, -1, 333333334,  3, 666666666},
1717             {3, 0, -1, 666666667,  3, 333333333},
1718             {3, 0, -1, 999999999,  3,         1},
1719             {3, 0,  0,         0,  3,         0},
1720             {3, 0,  0,         1,  2, 999999999},
1721             {3, 0,  0, 333333333,  2, 666666667},
1722             {3, 0,  0, 666666666,  2, 333333334},
1723             {3, 0,  1,         0,  2,         0},
1724             {3, 0,  2,         0,  1,         0},
1725             {3, 0,  3,         0,  0,         0},
1726             {3, 0,  3, 333333333, -1, 666666667},
1727 
1728             {3, 333333333, -4, 666666667,  6, 666666666},
1729             {3, 333333333, -3,         0,  6, 333333333},
1730             {3, 333333333, -2,         0,  5, 333333333},
1731             {3, 333333333, -1,         0,  4, 333333333},
1732             {3, 333333333, -1, 333333334,  3, 999999999},
1733             {3, 333333333, -1, 666666667,  3, 666666666},
1734             {3, 333333333, -1, 999999999,  3, 333333334},
1735             {3, 333333333,  0,         0,  3, 333333333},
1736             {3, 333333333,  0,         1,  3, 333333332},
1737             {3, 333333333,  0, 333333333,  3,         0},
1738             {3, 333333333,  0, 666666666,  2, 666666667},
1739             {3, 333333333,  1,         0,  2, 333333333},
1740             {3, 333333333,  2,         0,  1, 333333333},
1741             {3, 333333333,  3,         0,  0, 333333333},
1742             {3, 333333333,  3, 333333333,  0,         0},
1743 
1744             {Long.MAX_VALUE, 0, Long.MAX_VALUE, 0, 0, 0},
1745        };
1746     }
1747 
1748     @Test(dataProvider="Minus")
minus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond)1749     public void minus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
1750        Duration t = Duration.ofSeconds(seconds, nanos).minus(Duration.ofSeconds(otherSeconds, otherNanos));
1751        assertEquals(t.getSeconds(), expectedSeconds);
1752        assertEquals(t.getNano(), expectedNanoOfSecond);
1753     }
1754 
1755     @Test(expectedExceptions=ArithmeticException.class)
minusOverflowTooSmall()1756     public void minusOverflowTooSmall() {
1757        Duration t = Duration.ofSeconds(Long.MIN_VALUE);
1758        t.minus(Duration.ofSeconds(0, 1));
1759     }
1760 
1761     @Test(expectedExceptions=ArithmeticException.class)
minusOverflowTooBig()1762     public void minusOverflowTooBig() {
1763        Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
1764        t.minus(Duration.ofSeconds(-1, 999999999));
1765     }
1766 
1767     //-----------------------------------------------------------------------
1768     @Test
minus_longTemporalUnit_seconds()1769     public void minus_longTemporalUnit_seconds() {
1770         Duration t = Duration.ofSeconds(1);
1771         t = t.minus(1, SECONDS);
1772         assertEquals(0, t.getSeconds());
1773         assertEquals(0, t.getNano());
1774      }
1775 
1776     @Test
minus_longTemporalUnit_millis()1777     public void minus_longTemporalUnit_millis() {
1778         Duration t = Duration.ofSeconds(1);
1779         t = t.minus(1, MILLIS);
1780         assertEquals(0, t.getSeconds());
1781         assertEquals(999000000, t.getNano());
1782      }
1783 
1784     @Test
minus_longTemporalUnit_micros()1785     public void minus_longTemporalUnit_micros() {
1786         Duration t = Duration.ofSeconds(1);
1787         t = t.minus(1, MICROS);
1788         assertEquals(0, t.getSeconds());
1789         assertEquals(999999000, t.getNano());
1790      }
1791 
1792     @Test
minus_longTemporalUnit_nanos()1793     public void minus_longTemporalUnit_nanos() {
1794         Duration t = Duration.ofSeconds(1);
1795         t = t.minus(1, NANOS);
1796         assertEquals(0, t.getSeconds());
1797         assertEquals(999999999, t.getNano());
1798      }
1799 
1800     @Test(expectedExceptions=NullPointerException.class)
minus_longTemporalUnit_null()1801     public void minus_longTemporalUnit_null() {
1802        Duration t = Duration.ofSeconds(1);
1803        t.minus(1, (TemporalUnit) null);
1804     }
1805 
1806     //-----------------------------------------------------------------------
1807     @DataProvider(name="MinusDays")
provider_minusDays_long()1808     Object[][] provider_minusDays_long() {
1809         return new Object[][] {
1810             {0, 0, 0},
1811             {0, 1, -1},
1812             {0, -1, 1},
1813             {Long.MAX_VALUE/3600/24, 0, Long.MAX_VALUE/3600/24},
1814             {Long.MIN_VALUE/3600/24, 0, Long.MIN_VALUE/3600/24},
1815             {1, 0, 1},
1816             {1, 1, 0},
1817             {1, -1, 2},
1818             {Long.MAX_VALUE/3600/24, 1, Long.MAX_VALUE/3600/24 - 1},
1819             {Long.MIN_VALUE/3600/24, -1, Long.MIN_VALUE/3600/24 + 1},
1820             {1, 0, 1},
1821             {1, 1, 0},
1822             {1, -1, 2},
1823             {-1, 0, -1},
1824             {-1, 1, -2},
1825             {-1, -1, 0},
1826         };
1827     }
1828 
1829     @Test(dataProvider="MinusDays")
minusDays_long(long days, long amount, long expectedDays)1830     public void minusDays_long(long days, long amount, long expectedDays) {
1831         Duration t = Duration.ofDays(days);
1832         t = t.minusDays(amount);
1833         assertEquals(t.toDays(), expectedDays);
1834     }
1835 
1836     @Test(expectedExceptions = {ArithmeticException.class})
minusDays_long_overflowTooBig()1837     public void minusDays_long_overflowTooBig() {
1838         Duration t = Duration.ofDays(Long.MAX_VALUE/3600/24);
1839         t.minusDays(-1);
1840     }
1841 
1842     @Test(expectedExceptions = {ArithmeticException.class})
minusDays_long_overflowTooSmall()1843     public void minusDays_long_overflowTooSmall() {
1844         Duration t = Duration.ofDays(Long.MIN_VALUE/3600/24);
1845         t.minusDays(1);
1846     }
1847 
1848     //-----------------------------------------------------------------------
1849     @DataProvider(name="MinusHours")
provider_minusHours_long()1850     Object[][] provider_minusHours_long() {
1851         return new Object[][] {
1852             {0, 0, 0},
1853             {0, 1, -1},
1854             {0, -1, 1},
1855             {Long.MAX_VALUE/3600, 0, Long.MAX_VALUE/3600},
1856             {Long.MIN_VALUE/3600, 0, Long.MIN_VALUE/3600},
1857             {1, 0, 1},
1858             {1, 1, 0},
1859             {1, -1, 2},
1860             {Long.MAX_VALUE/3600, 1, Long.MAX_VALUE/3600 - 1},
1861             {Long.MIN_VALUE/3600, -1, Long.MIN_VALUE/3600 + 1},
1862             {1, 0, 1},
1863             {1, 1, 0},
1864             {1, -1, 2},
1865             {-1, 0, -1},
1866             {-1, 1, -2},
1867             {-1, -1, 0},
1868         };
1869     }
1870 
1871     @Test(dataProvider="MinusHours")
minusHours_long(long hours, long amount, long expectedHours)1872     public void minusHours_long(long hours, long amount, long expectedHours) {
1873         Duration t = Duration.ofHours(hours);
1874         t = t.minusHours(amount);
1875         assertEquals(t.toHours(), expectedHours);
1876     }
1877 
1878     @Test(expectedExceptions = {ArithmeticException.class})
minusHours_long_overflowTooBig()1879     public void minusHours_long_overflowTooBig() {
1880         Duration t = Duration.ofHours(Long.MAX_VALUE/3600);
1881         t.minusHours(-1);
1882     }
1883 
1884     @Test(expectedExceptions = {ArithmeticException.class})
minusHours_long_overflowTooSmall()1885     public void minusHours_long_overflowTooSmall() {
1886         Duration t = Duration.ofHours(Long.MIN_VALUE/3600);
1887         t.minusHours(1);
1888     }
1889 
1890     //-----------------------------------------------------------------------
1891     @DataProvider(name="MinusMinutes")
provider_minusminutes_long()1892     Object[][] provider_minusminutes_long() {
1893         return new Object[][] {
1894             {0, 0, 0},
1895             {0, 1, -1},
1896             {0, -1, 1},
1897             {Long.MAX_VALUE/60, 0, Long.MAX_VALUE/60},
1898             {Long.MIN_VALUE/60, 0, Long.MIN_VALUE/60},
1899             {1, 0, 1},
1900             {1, 1, 0},
1901             {1, -1, 2},
1902             {Long.MAX_VALUE/60, 1, Long.MAX_VALUE/60 - 1},
1903             {Long.MIN_VALUE/60, -1, Long.MIN_VALUE/60 + 1},
1904             {1, 0, 1},
1905             {1, 1, 0},
1906             {1, -1, 2},
1907             {-1, 0, -1},
1908             {-1, 1, -2},
1909             {-1, -1, 0},
1910         };
1911     }
1912 
1913     @Test(dataProvider="MinusMinutes")
minusMinutes_long(long minutes, long amount, long expectedMinutes)1914     public void minusMinutes_long(long minutes, long amount, long expectedMinutes) {
1915         Duration t = Duration.ofMinutes(minutes);
1916         t = t.minusMinutes(amount);
1917         assertEquals(t.toMinutes(), expectedMinutes);
1918     }
1919 
1920     @Test(expectedExceptions = {ArithmeticException.class})
minusMinutes_long_overflowTooBig()1921     public void minusMinutes_long_overflowTooBig() {
1922         Duration t = Duration.ofMinutes(Long.MAX_VALUE/60);
1923         t.minusMinutes(-1);
1924     }
1925 
1926     @Test(expectedExceptions = {ArithmeticException.class})
minusMinutes_long_overflowTooSmall()1927     public void minusMinutes_long_overflowTooSmall() {
1928         Duration t = Duration.ofMinutes(Long.MIN_VALUE/60);
1929         t.minusMinutes(1);
1930     }
1931 
1932     //-----------------------------------------------------------------------
1933     @DataProvider(name="MinusSeconds")
provider_minusSeconds_long()1934     Object[][] provider_minusSeconds_long() {
1935         return new Object[][] {
1936             {0, 0, 0, 0, 0},
1937             {0, 0, 1, -1, 0},
1938             {0, 0, -1, 1, 0},
1939             {0, 0, Long.MAX_VALUE, -Long.MAX_VALUE, 0},
1940             {0, 0, Long.MIN_VALUE + 1, Long.MAX_VALUE, 0},
1941             {1, 0, 0, 1, 0},
1942             {1, 0, 1, 0, 0},
1943             {1, 0, -1, 2, 0},
1944             {1, 0, Long.MAX_VALUE - 1, -Long.MAX_VALUE + 2, 0},
1945             {1, 0, Long.MIN_VALUE + 2, Long.MAX_VALUE, 0},
1946             {1, 1, 0, 1, 1},
1947             {1, 1, 1, 0, 1},
1948             {1, 1, -1, 2, 1},
1949             {1, 1, Long.MAX_VALUE, -Long.MAX_VALUE + 1, 1},
1950             {1, 1, Long.MIN_VALUE + 2, Long.MAX_VALUE, 1},
1951             {-1, 1, 0, -1, 1},
1952             {-1, 1, 1, -2, 1},
1953             {-1, 1, -1, 0, 1},
1954             {-1, 1, Long.MAX_VALUE, Long.MIN_VALUE, 1},
1955             {-1, 1, Long.MIN_VALUE + 1, Long.MAX_VALUE - 1, 1},
1956         };
1957     }
1958 
1959     @Test(dataProvider="MinusSeconds")
minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)1960     public void minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
1961         Duration t = Duration.ofSeconds(seconds, nanos);
1962         t = t.minusSeconds(amount);
1963         assertEquals(t.getSeconds(), expectedSeconds);
1964         assertEquals(t.getNano(), expectedNanoOfSecond);
1965     }
1966 
1967     @Test(expectedExceptions = {ArithmeticException.class})
minusSeconds_long_overflowTooBig()1968     public void minusSeconds_long_overflowTooBig() {
1969         Duration t = Duration.ofSeconds(1, 0);
1970         t.minusSeconds(Long.MIN_VALUE + 1);
1971     }
1972 
1973     @Test(expectedExceptions = {ArithmeticException.class})
minusSeconds_long_overflowTooSmall()1974     public void minusSeconds_long_overflowTooSmall() {
1975         Duration t = Duration.ofSeconds(-2, 0);
1976         t.minusSeconds(Long.MAX_VALUE);
1977     }
1978 
1979     //-----------------------------------------------------------------------
1980     @DataProvider(name="MinusMillis")
provider_minusMillis_long()1981     Object[][] provider_minusMillis_long() {
1982         return new Object[][] {
1983             {0, 0, 0,       0, 0},
1984             {0, 0, 1,      -1, 999000000},
1985             {0, 0, 999,    -1, 1000000},
1986             {0, 0, 1000,   -1, 0},
1987             {0, 0, 1001,   -2, 999000000},
1988             {0, 0, 1999,   -2, 1000000},
1989             {0, 0, 2000,   -2, 0},
1990             {0, 0, -1,      0, 1000000},
1991             {0, 0, -999,    0, 999000000},
1992             {0, 0, -1000,   1, 0},
1993             {0, 0, -1001,   1, 1000000},
1994             {0, 0, -1999,   1, 999000000},
1995 
1996             {0, 1, 0,       0, 1},
1997             {0, 1, 1,      -1, 999000001},
1998             {0, 1, 998,    -1, 2000001},
1999             {0, 1, 999,    -1, 1000001},
2000             {0, 1, 1000,   -1, 1},
2001             {0, 1, 1998,   -2, 2000001},
2002             {0, 1, 1999,   -2, 1000001},
2003             {0, 1, 2000,   -2, 1},
2004             {0, 1, -1,      0, 1000001},
2005             {0, 1, -2,      0, 2000001},
2006             {0, 1, -1000,   1, 1},
2007             {0, 1, -1001,   1, 1000001},
2008 
2009             {0, 1000000, 0,       0, 1000000},
2010             {0, 1000000, 1,       0, 0},
2011             {0, 1000000, 998,    -1, 3000000},
2012             {0, 1000000, 999,    -1, 2000000},
2013             {0, 1000000, 1000,   -1, 1000000},
2014             {0, 1000000, 1998,   -2, 3000000},
2015             {0, 1000000, 1999,   -2, 2000000},
2016             {0, 1000000, 2000,   -2, 1000000},
2017             {0, 1000000, -1,      0, 2000000},
2018             {0, 1000000, -2,      0, 3000000},
2019             {0, 1000000, -999,    1, 0},
2020             {0, 1000000, -1000,   1, 1000000},
2021             {0, 1000000, -1001,   1, 2000000},
2022             {0, 1000000, -1002,   1, 3000000},
2023 
2024             {0, 999999999, 0,     0, 999999999},
2025             {0, 999999999, 1,     0, 998999999},
2026             {0, 999999999, 999,   0, 999999},
2027             {0, 999999999, 1000, -1, 999999999},
2028             {0, 999999999, 1001, -1, 998999999},
2029             {0, 999999999, -1,    1, 999999},
2030             {0, 999999999, -1000, 1, 999999999},
2031             {0, 999999999, -1001, 2, 999999},
2032         };
2033     }
2034 
2035     @Test(dataProvider="MinusMillis")
minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)2036     public void minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
2037         Duration t = Duration.ofSeconds(seconds, nanos);
2038         t = t.minusMillis(amount);
2039         assertEquals(t.getSeconds(), expectedSeconds);
2040         assertEquals(t.getNano(), expectedNanoOfSecond);
2041     }
2042     @Test(dataProvider="MinusMillis")
minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)2043     public void minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
2044         Duration t = Duration.ofSeconds(seconds + 1, nanos);
2045         t = t.minusMillis(amount);
2046         assertEquals(t.getSeconds(), expectedSeconds + 1);
2047         assertEquals(t.getNano(), expectedNanoOfSecond);
2048     }
2049     @Test(dataProvider="MinusMillis")
minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)2050     public void minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
2051         Duration t = Duration.ofSeconds(seconds - 1, nanos);
2052         t = t.minusMillis(amount);
2053         assertEquals(t.getSeconds(), expectedSeconds - 1);
2054         assertEquals(t.getNano(), expectedNanoOfSecond);
2055     }
2056 
2057     @Test
minusMillis_long_max()2058     public void minusMillis_long_max() {
2059         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 998999999);
2060         t = t.minusMillis(-1);
2061         assertEquals(t.getSeconds(), Long.MAX_VALUE);
2062         assertEquals(t.getNano(), 999999999);
2063     }
2064 
2065     @Test(expectedExceptions = {ArithmeticException.class})
minusMillis_long_overflowTooBig()2066     public void minusMillis_long_overflowTooBig() {
2067         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999000000);
2068         t.minusMillis(-1);
2069     }
2070 
2071     @Test
minusMillis_long_min()2072     public void minusMillis_long_min() {
2073         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 1000000);
2074         t = t.minusMillis(1);
2075         assertEquals(t.getSeconds(), Long.MIN_VALUE);
2076         assertEquals(t.getNano(), 0);
2077     }
2078 
2079     @Test(expectedExceptions = {ArithmeticException.class})
minusMillis_long_overflowTooSmall()2080     public void minusMillis_long_overflowTooSmall() {
2081         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
2082         t.minusMillis(1);
2083     }
2084 
2085     //-----------------------------------------------------------------------
2086     @DataProvider(name="MinusNanos")
provider_minusNanos_long()2087     Object[][] provider_minusNanos_long() {
2088         return new Object[][] {
2089             {0, 0, 0,           0, 0},
2090             {0, 0, 1,          -1, 999999999},
2091             {0, 0, 999999999,  -1, 1},
2092             {0, 0, 1000000000, -1, 0},
2093             {0, 0, 1000000001, -2, 999999999},
2094             {0, 0, 1999999999, -2, 1},
2095             {0, 0, 2000000000, -2, 0},
2096             {0, 0, -1,          0, 1},
2097             {0, 0, -999999999,  0, 999999999},
2098             {0, 0, -1000000000, 1, 0},
2099             {0, 0, -1000000001, 1, 1},
2100             {0, 0, -1999999999, 1, 999999999},
2101 
2102             {1, 0, 0,            1, 0},
2103             {1, 0, 1,            0, 999999999},
2104             {1, 0, 999999999,    0, 1},
2105             {1, 0, 1000000000,   0, 0},
2106             {1, 0, 1000000001,  -1, 999999999},
2107             {1, 0, 1999999999,  -1, 1},
2108             {1, 0, 2000000000,  -1, 0},
2109             {1, 0, -1,           1, 1},
2110             {1, 0, -999999999,   1, 999999999},
2111             {1, 0, -1000000000,  2, 0},
2112             {1, 0, -1000000001,  2, 1},
2113             {1, 0, -1999999999,  2, 999999999},
2114 
2115             {-1, 0, 0,           -1, 0},
2116             {-1, 0, 1,           -2, 999999999},
2117             {-1, 0, 999999999,   -2, 1},
2118             {-1, 0, 1000000000,  -2, 0},
2119             {-1, 0, 1000000001,  -3, 999999999},
2120             {-1, 0, 1999999999,  -3, 1},
2121             {-1, 0, 2000000000,  -3, 0},
2122             {-1, 0, -1,          -1, 1},
2123             {-1, 0, -999999999,  -1, 999999999},
2124             {-1, 0, -1000000000,  0, 0},
2125             {-1, 0, -1000000001,  0, 1},
2126             {-1, 0, -1999999999,  0, 999999999},
2127 
2128             {1, 1, 0,           1, 1},
2129             {1, 1, 1,           1, 0},
2130             {1, 1, 999999998,   0, 3},
2131             {1, 1, 999999999,   0, 2},
2132             {1, 1, 1000000000,  0, 1},
2133             {1, 1, 1999999998, -1, 3},
2134             {1, 1, 1999999999, -1, 2},
2135             {1, 1, 2000000000, -1, 1},
2136             {1, 1, -1,          1, 2},
2137             {1, 1, -2,          1, 3},
2138             {1, 1, -1000000000, 2, 1},
2139             {1, 1, -1000000001, 2, 2},
2140             {1, 1, -1000000002, 2, 3},
2141             {1, 1, -2000000000, 3, 1},
2142 
2143             {1, 999999999, 0,           1, 999999999},
2144             {1, 999999999, 1,           1, 999999998},
2145             {1, 999999999, 999999999,   1, 0},
2146             {1, 999999999, 1000000000,  0, 999999999},
2147             {1, 999999999, 1000000001,  0, 999999998},
2148             {1, 999999999, -1,          2, 0},
2149             {1, 999999999, -1000000000, 2, 999999999},
2150             {1, 999999999, -1000000001, 3, 0},
2151             {1, 999999999, -1999999999, 3, 999999998},
2152             {1, 999999999, -2000000000, 3, 999999999},
2153 
2154             {Long.MAX_VALUE, 0, -999999999, Long.MAX_VALUE, 999999999},
2155             {Long.MAX_VALUE - 1, 0, -1999999999, Long.MAX_VALUE, 999999999},
2156             {Long.MIN_VALUE, 1, 1, Long.MIN_VALUE, 0},
2157             {Long.MIN_VALUE + 1, 1, 1000000001, Long.MIN_VALUE, 0},
2158         };
2159     }
2160 
2161     @Test(dataProvider="MinusNanos")
minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)2162     public void minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
2163         Duration t = Duration.ofSeconds(seconds, nanos);
2164         t = t.minusNanos(amount);
2165         assertEquals(t.getSeconds(), expectedSeconds);
2166         assertEquals(t.getNano(), expectedNanoOfSecond);
2167     }
2168 
2169     @Test(expectedExceptions = {ArithmeticException.class})
minusNanos_long_overflowTooBig()2170     public void minusNanos_long_overflowTooBig() {
2171         Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
2172         t.minusNanos(-1);
2173     }
2174 
2175     @Test(expectedExceptions = {ArithmeticException.class})
minusNanos_long_overflowTooSmall()2176     public void minusNanos_long_overflowTooSmall() {
2177         Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0);
2178         t.minusNanos(1);
2179     }
2180 
2181     //-----------------------------------------------------------------------
2182     // multipliedBy()
2183     //-----------------------------------------------------------------------
2184     @DataProvider(name="MultipliedBy")
provider_multipliedBy()2185     Object[][] provider_multipliedBy() {
2186        return new Object[][] {
2187           {-4, 666666667, -3,   9, 999999999},
2188           {-4, 666666667, -2,   6, 666666666},
2189           {-4, 666666667, -1,   3, 333333333},
2190           {-4, 666666667,  0,   0,         0},
2191           {-4, 666666667,  1,  -4, 666666667},
2192           {-4, 666666667,  2,  -7, 333333334},
2193           {-4, 666666667,  3, -10, 000000001},
2194 
2195           {-3, 0, -3,  9, 0},
2196           {-3, 0, -2,  6, 0},
2197           {-3, 0, -1,  3, 0},
2198           {-3, 0,  0,  0, 0},
2199           {-3, 0,  1, -3, 0},
2200           {-3, 0,  2, -6, 0},
2201           {-3, 0,  3, -9, 0},
2202 
2203           {-2, 0, -3,  6, 0},
2204           {-2, 0, -2,  4, 0},
2205           {-2, 0, -1,  2, 0},
2206           {-2, 0,  0,  0, 0},
2207           {-2, 0,  1, -2, 0},
2208           {-2, 0,  2, -4, 0},
2209           {-2, 0,  3, -6, 0},
2210 
2211           {-1, 0, -3,  3, 0},
2212           {-1, 0, -2,  2, 0},
2213           {-1, 0, -1,  1, 0},
2214           {-1, 0,  0,  0, 0},
2215           {-1, 0,  1, -1, 0},
2216           {-1, 0,  2, -2, 0},
2217           {-1, 0,  3, -3, 0},
2218 
2219           {-1, 500000000, -3,  1, 500000000},
2220           {-1, 500000000, -2,  1,         0},
2221           {-1, 500000000, -1,  0, 500000000},
2222           {-1, 500000000,  0,  0,         0},
2223           {-1, 500000000,  1, -1, 500000000},
2224           {-1, 500000000,  2, -1,         0},
2225           {-1, 500000000,  3, -2, 500000000},
2226 
2227           {0, 0, -3, 0, 0},
2228           {0, 0, -2, 0, 0},
2229           {0, 0, -1, 0, 0},
2230           {0, 0,  0, 0, 0},
2231           {0, 0,  1, 0, 0},
2232           {0, 0,  2, 0, 0},
2233           {0, 0,  3, 0, 0},
2234 
2235           {0, 500000000, -3, -2, 500000000},
2236           {0, 500000000, -2, -1,         0},
2237           {0, 500000000, -1, -1, 500000000},
2238           {0, 500000000,  0,  0,         0},
2239           {0, 500000000,  1,  0, 500000000},
2240           {0, 500000000,  2,  1,         0},
2241           {0, 500000000,  3,  1, 500000000},
2242 
2243           {1, 0, -3, -3, 0},
2244           {1, 0, -2, -2, 0},
2245           {1, 0, -1, -1, 0},
2246           {1, 0,  0,  0, 0},
2247           {1, 0,  1,  1, 0},
2248           {1, 0,  2,  2, 0},
2249           {1, 0,  3,  3, 0},
2250 
2251           {2, 0, -3, -6, 0},
2252           {2, 0, -2, -4, 0},
2253           {2, 0, -1, -2, 0},
2254           {2, 0,  0,  0, 0},
2255           {2, 0,  1,  2, 0},
2256           {2, 0,  2,  4, 0},
2257           {2, 0,  3,  6, 0},
2258 
2259           {3, 0, -3, -9, 0},
2260           {3, 0, -2, -6, 0},
2261           {3, 0, -1, -3, 0},
2262           {3, 0,  0,  0, 0},
2263           {3, 0,  1,  3, 0},
2264           {3, 0,  2,  6, 0},
2265           {3, 0,  3,  9, 0},
2266 
2267           {3, 333333333, -3, -10, 000000001},
2268           {3, 333333333, -2,  -7, 333333334},
2269           {3, 333333333, -1,  -4, 666666667},
2270           {3, 333333333,  0,   0,         0},
2271           {3, 333333333,  1,   3, 333333333},
2272           {3, 333333333,  2,   6, 666666666},
2273           {3, 333333333,  3,   9, 999999999},
2274        };
2275     }
2276 
2277     @Test(dataProvider="MultipliedBy")
multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos)2278     public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) {
2279         Duration t = Duration.ofSeconds(seconds, nanos);
2280         t = t.multipliedBy(multiplicand);
2281         assertEquals(t.getSeconds(), expectedSeconds);
2282         assertEquals(t.getNano(), expectedNanos);
2283     }
2284 
2285     @Test
multipliedBy_max()2286     public void multipliedBy_max() {
2287         Duration test = Duration.ofSeconds(1);
2288         assertEquals(test.multipliedBy(Long.MAX_VALUE), Duration.ofSeconds(Long.MAX_VALUE));
2289     }
2290 
2291     @Test
multipliedBy_min()2292     public void multipliedBy_min() {
2293         Duration test = Duration.ofSeconds(1);
2294         assertEquals(test.multipliedBy(Long.MIN_VALUE), Duration.ofSeconds(Long.MIN_VALUE));
2295     }
2296 
2297     @Test(expectedExceptions=ArithmeticException.class)
multipliedBy_tooBig()2298     public void multipliedBy_tooBig() {
2299         Duration test = Duration.ofSeconds(1, 1);
2300         test.multipliedBy(Long.MAX_VALUE);
2301     }
2302 
2303     @Test(expectedExceptions=ArithmeticException.class)
multipliedBy_tooBig_negative()2304     public void multipliedBy_tooBig_negative() {
2305         Duration test = Duration.ofSeconds(1, 1);
2306         test.multipliedBy(Long.MIN_VALUE);
2307     }
2308 
2309     //-----------------------------------------------------------------------
2310     //  truncated(TemporalUnit)
2311     //-----------------------------------------------------------------------
2312     TemporalUnit NINETY_MINS = new TemporalUnit() {
2313         @Override
2314         public Duration getDuration() {
2315             return Duration.ofMinutes(90);
2316         }
2317         @Override
2318         public boolean isDurationEstimated() {
2319             return false;
2320         }
2321         @Override
2322         public boolean isDateBased() {
2323             return false;
2324         }
2325         @Override
2326         public boolean isTimeBased() {
2327             return true;
2328         }
2329         @Override
2330         public boolean isSupportedBy(Temporal temporal) {
2331             return false;
2332         }
2333         @Override
2334         public <R extends Temporal> R addTo(R temporal, long amount) {
2335             throw new UnsupportedOperationException();
2336         }
2337         @Override
2338         public long between(Temporal temporal1, Temporal temporal2) {
2339             throw new UnsupportedOperationException();
2340         }
2341         @Override
2342         public String toString() {
2343             return "NinetyMins";
2344         }
2345     };
2346 
2347     TemporalUnit NINETY_FIVE_MINS = new TemporalUnit() {
2348         @Override
2349         public Duration getDuration() {
2350             return Duration.ofMinutes(95);
2351         }
2352         @Override
2353         public boolean isDurationEstimated() {
2354             return false;
2355         }
2356         @Override
2357         public boolean isDateBased() {
2358             return false;
2359         }
2360         @Override
2361         public boolean isTimeBased() {
2362             return false;
2363         }
2364         @Override
2365         public boolean isSupportedBy(Temporal temporal) {
2366             return false;
2367         }
2368         @Override
2369         public <R extends Temporal> R addTo(R temporal, long amount) {
2370             throw new UnsupportedOperationException();
2371         }
2372         @Override
2373         public long between(Temporal temporal1, Temporal temporal2) {
2374             throw new UnsupportedOperationException();
2375         }
2376         @Override
2377         public String toString() {
2378             return "NinetyFiveMins";
2379         }
2380     };
2381 
2382     @DataProvider(name="truncatedToValid")
data_truncatedToValid()2383     Object[][] data_truncatedToValid() {
2384         return new Object[][] {
2385                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), NANOS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789)},
2386                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MICROS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_000)},
2387                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MILLIS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 1230_00_000)},
2388                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), SECONDS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 0)},
2389                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MINUTES, Duration.ofSeconds(86400 + 3600 + 60, 0)},
2390                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), HOURS, Duration.ofSeconds(86400 + 3600, 0)},
2391                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), DAYS, Duration.ofSeconds(86400, 0)},
2392 
2393                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 0, 0)},
2394                 {Duration.ofSeconds(86400 + 7200 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 5400, 0)},
2395                 {Duration.ofSeconds(86400 + 10800 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 10800, 0)},
2396 
2397                 {Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_456_789), MINUTES, Duration.ofSeconds(-86400 - 3600 - 60, 0 )},
2398                 {Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_456_789), MICROS, Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_457_000)},
2399 
2400                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 0), SECONDS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 0)},
2401                 {Duration.ofSeconds(-86400 - 3600 - 120, 0), MINUTES, Duration.ofSeconds(-86400 - 3600 - 120, 0)},
2402 
2403                 {Duration.ofSeconds(-1, 0), SECONDS, Duration.ofSeconds(-1, 0)},
2404                 {Duration.ofSeconds(-1, 123_456_789), SECONDS, Duration.ofSeconds(0, 0)},
2405                 {Duration.ofSeconds(-1, 123_456_789), NANOS, Duration.ofSeconds(0, -876_543_211)},
2406                 {Duration.ofSeconds(0, 123_456_789), SECONDS, Duration.ofSeconds(0, 0)},
2407                 {Duration.ofSeconds(0, 123_456_789), NANOS, Duration.ofSeconds(0, 123_456_789)},
2408         };
2409     }
2410 
2411     @Test(dataProvider="truncatedToValid")
test_truncatedTo_valid(Duration input, TemporalUnit unit, Duration expected)2412     public void test_truncatedTo_valid(Duration input, TemporalUnit unit, Duration expected) {
2413         assertEquals(input.truncatedTo(unit), expected);
2414     }
2415 
2416     @DataProvider(name="truncatedToInvalid")
data_truncatedToInvalid()2417     Object[][] data_truncatedToInvalid() {
2418         return new Object[][] {
2419                 {Duration.ofSeconds(1, 123_456_789), NINETY_FIVE_MINS},
2420                 {Duration.ofSeconds(1, 123_456_789), WEEKS},
2421                 {Duration.ofSeconds(1, 123_456_789), MONTHS},
2422                 {Duration.ofSeconds(1, 123_456_789), YEARS},
2423         };
2424     }
2425 
2426     @Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
test_truncatedTo_invalid(Duration input, TemporalUnit unit)2427     public void test_truncatedTo_invalid(Duration input, TemporalUnit unit) {
2428         input.truncatedTo(unit);
2429     }
2430 
2431     @Test(expectedExceptions=NullPointerException.class)
test_truncatedTo_null()2432     public void test_truncatedTo_null() {
2433         Duration.ofSeconds(1234).truncatedTo(null);
2434     }
2435 
2436     //-----------------------------------------------------------------------
2437     // dividedBy()
2438     //-----------------------------------------------------------------------
2439     @DataProvider(name="DividedBy")
provider_dividedBy()2440     Object[][] provider_dividedBy() {
2441        return new Object[][] {
2442           {-4, 666666667, -3,  1, 111111111},
2443           {-4, 666666667, -2,  1, 666666666},
2444           {-4, 666666667, -1,  3, 333333333},
2445           {-4, 666666667,  1, -4, 666666667},
2446           {-4, 666666667,  2, -2, 333333334},
2447           {-4, 666666667,  3, -2, 888888889},
2448 
2449           {-3, 0, -3,  1, 0},
2450           {-3, 0, -2,  1, 500000000},
2451           {-3, 0, -1,  3, 0},
2452           {-3, 0,  1, -3, 0},
2453           {-3, 0,  2, -2, 500000000},
2454           {-3, 0,  3, -1, 0},
2455 
2456           {-2, 0, -3,  0, 666666666},
2457           {-2, 0, -2,  1,         0},
2458           {-2, 0, -1,  2,         0},
2459           {-2, 0,  1, -2,         0},
2460           {-2, 0,  2, -1,         0},
2461           {-2, 0,  3, -1, 333333334},
2462 
2463           {-1, 0, -3,  0, 333333333},
2464           {-1, 0, -2,  0, 500000000},
2465           {-1, 0, -1,  1,         0},
2466           {-1, 0,  1, -1,         0},
2467           {-1, 0,  2, -1, 500000000},
2468           {-1, 0,  3, -1, 666666667},
2469 
2470           {-1, 500000000, -3,  0, 166666666},
2471           {-1, 500000000, -2,  0, 250000000},
2472           {-1, 500000000, -1,  0, 500000000},
2473           {-1, 500000000,  1, -1, 500000000},
2474           {-1, 500000000,  2, -1, 750000000},
2475           {-1, 500000000,  3, -1, 833333334},
2476 
2477           {0, 0, -3, 0, 0},
2478           {0, 0, -2, 0, 0},
2479           {0, 0, -1, 0, 0},
2480           {0, 0,  1, 0, 0},
2481           {0, 0,  2, 0, 0},
2482           {0, 0,  3, 0, 0},
2483 
2484           {0, 500000000, -3, -1, 833333334},
2485           {0, 500000000, -2, -1, 750000000},
2486           {0, 500000000, -1, -1, 500000000},
2487           {0, 500000000,  1,  0, 500000000},
2488           {0, 500000000,  2,  0, 250000000},
2489           {0, 500000000,  3,  0, 166666666},
2490 
2491           {1, 0, -3, -1, 666666667},
2492           {1, 0, -2, -1, 500000000},
2493           {1, 0, -1, -1,         0},
2494           {1, 0,  1,  1,         0},
2495           {1, 0,  2,  0, 500000000},
2496           {1, 0,  3,  0, 333333333},
2497 
2498           {2, 0, -3, -1, 333333334},
2499           {2, 0, -2, -1,         0},
2500           {2, 0, -1, -2,         0},
2501           {2, 0,  1,  2,         0},
2502           {2, 0,  2,  1,         0},
2503           {2, 0,  3,  0, 666666666},
2504 
2505           {3, 0, -3, -1,         0},
2506           {3, 0, -2, -2, 500000000},
2507           {3, 0, -1, -3,         0},
2508           {3, 0,  1,  3,         0},
2509           {3, 0,  2,  1, 500000000},
2510           {3, 0,  3,  1,         0},
2511 
2512           {3, 333333333, -3, -2, 888888889},
2513           {3, 333333333, -2, -2, 333333334},
2514           {3, 333333333, -1, -4, 666666667},
2515           {3, 333333333,  1,  3, 333333333},
2516           {3, 333333333,  2,  1, 666666666},
2517           {3, 333333333,  3,  1, 111111111},
2518        };
2519     }
2520 
2521     @Test(dataProvider="DividedBy")
dividedBy(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos)2522     public void dividedBy(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos) {
2523         Duration t = Duration.ofSeconds(seconds, nanos);
2524         t = t.dividedBy(divisor);
2525         assertEquals(t.getSeconds(), expectedSeconds);
2526         assertEquals(t.getNano(), expectedNanos);
2527     }
2528 
2529     @Test(dataProvider="DividedBy", expectedExceptions=ArithmeticException.class)
dividedByZero(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos)2530     public void dividedByZero(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos) {
2531        Duration t = Duration.ofSeconds(seconds, nanos);
2532        t.dividedBy(0);
2533        fail(t + " divided by zero did not throw ArithmeticException");
2534     }
2535 
2536     @Test
dividedBy_max()2537     public void dividedBy_max() {
2538         Duration test = Duration.ofSeconds(Long.MAX_VALUE);
2539         assertEquals(test.dividedBy(Long.MAX_VALUE), Duration.ofSeconds(1));
2540     }
2541 
2542     //-----------------------------------------------------------------------
2543     // dividedbyDur()
2544     //-----------------------------------------------------------------------
2545 
2546     @DataProvider(name="dividedByDur_provider")
provider_dividedByDur()2547     Object[][] provider_dividedByDur() {
2548         return new Object[][] {
2549             {Duration.ofSeconds(0, 0), Duration.ofSeconds(1, 0), 0},
2550             {Duration.ofSeconds(1, 0), Duration.ofSeconds(1, 0), 1},
2551             {Duration.ofSeconds(6, 0), Duration.ofSeconds(3, 0), 2},
2552             {Duration.ofSeconds(3, 0), Duration.ofSeconds(6, 0), 0},
2553             {Duration.ofSeconds(7, 0), Duration.ofSeconds(3, 0), 2},
2554 
2555             {Duration.ofSeconds(0, 333_333_333), Duration.ofSeconds(0, 333_333_333), 1},
2556             {Duration.ofSeconds(0, 666_666_666), Duration.ofSeconds(0, 333_333_333), 2},
2557             {Duration.ofSeconds(0, 333_333_333), Duration.ofSeconds(0, 666_666_666), 0},
2558             {Duration.ofSeconds(0, 777_777_777), Duration.ofSeconds(0, 333_333_333), 2},
2559 
2560             {Duration.ofSeconds(-7, 0), Duration.ofSeconds(3, 0), -2},
2561             {Duration.ofSeconds(0, 7), Duration.ofSeconds(0, -3), -2},
2562             {Duration.ofSeconds(0, -777_777_777), Duration.ofSeconds(0, 333_333_333), -2},
2563 
2564             {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), 29},
2565             {Duration.ofSeconds(-432000L, 777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), -29},
2566             {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), -29},
2567             {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(14400L, -333_333_333L), -30},
2568             {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(-14400L, 333_333_333L), -30},
2569             {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(-14400L, -333_333_333L), -29},
2570             {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(-14400L, -333_333_333L), 29},
2571 
2572             {Duration.ofSeconds(Long.MAX_VALUE, 0), Duration.ofSeconds(1, 0), Long.MAX_VALUE},
2573             {Duration.ofSeconds(Long.MAX_VALUE, 0), Duration.ofSeconds(Long.MAX_VALUE, 0), 1},
2574         };
2575     }
2576 
2577     @Test(dataProvider="dividedByDur_provider")
test_dividedByDur(Duration dividend, Duration divisor, long expected)2578     public void test_dividedByDur(Duration dividend, Duration divisor, long expected) {
2579         assertEquals(dividend.dividedBy(divisor), expected);
2580     }
2581 
2582     @Test(expectedExceptions=ArithmeticException.class)
test_dividedByDur_zero()2583     public void test_dividedByDur_zero() {
2584        Duration t = Duration.ofSeconds(1, 0);
2585        t.dividedBy(Duration.ZERO);
2586     }
2587 
2588     @Test(expectedExceptions=NullPointerException.class)
test_dividedByDur_null()2589     public void test_dividedByDur_null() {
2590        Duration t = Duration.ofSeconds(1, 0);
2591        t.dividedBy(null);
2592     }
2593 
2594     @Test(expectedExceptions=ArithmeticException.class)
test_dividedByDur_overflow()2595     public void test_dividedByDur_overflow() {
2596        Duration dur1 = Duration.ofSeconds(Long.MAX_VALUE, 0);
2597        Duration dur2 = Duration.ofNanos(1);
2598        dur1.dividedBy(dur2);
2599     }
2600 
2601     //-----------------------------------------------------------------------
2602     // negated()
2603     //-----------------------------------------------------------------------
2604     @Test
test_negated()2605     public void test_negated() {
2606         assertEquals(Duration.ofSeconds(0).negated(), Duration.ofSeconds(0));
2607         assertEquals(Duration.ofSeconds(12).negated(), Duration.ofSeconds(-12));
2608         assertEquals(Duration.ofSeconds(-12).negated(), Duration.ofSeconds(12));
2609         assertEquals(Duration.ofSeconds(12, 20).negated(), Duration.ofSeconds(-12, -20));
2610         assertEquals(Duration.ofSeconds(12, -20).negated(), Duration.ofSeconds(-12, 20));
2611         assertEquals(Duration.ofSeconds(-12, -20).negated(), Duration.ofSeconds(12, 20));
2612         assertEquals(Duration.ofSeconds(-12, 20).negated(), Duration.ofSeconds(12, -20));
2613         assertEquals(Duration.ofSeconds(Long.MAX_VALUE).negated(), Duration.ofSeconds(-Long.MAX_VALUE));
2614     }
2615 
2616     @Test(expectedExceptions=ArithmeticException.class)
test_negated_overflow()2617     public void test_negated_overflow() {
2618         Duration.ofSeconds(Long.MIN_VALUE).negated();
2619     }
2620 
2621     //-----------------------------------------------------------------------
2622     // abs()
2623     //-----------------------------------------------------------------------
2624     @Test
test_abs()2625     public void test_abs() {
2626         assertEquals(Duration.ofSeconds(0).abs(), Duration.ofSeconds(0));
2627         assertEquals(Duration.ofSeconds(12).abs(), Duration.ofSeconds(12));
2628         assertEquals(Duration.ofSeconds(-12).abs(), Duration.ofSeconds(12));
2629         assertEquals(Duration.ofSeconds(12, 20).abs(), Duration.ofSeconds(12, 20));
2630         assertEquals(Duration.ofSeconds(12, -20).abs(), Duration.ofSeconds(12, -20));
2631         assertEquals(Duration.ofSeconds(-12, -20).abs(), Duration.ofSeconds(12, 20));
2632         assertEquals(Duration.ofSeconds(-12, 20).abs(), Duration.ofSeconds(12, -20));
2633         assertEquals(Duration.ofSeconds(Long.MAX_VALUE).abs(), Duration.ofSeconds(Long.MAX_VALUE));
2634     }
2635 
2636     @Test(expectedExceptions=ArithmeticException.class)
test_abs_overflow()2637     public void test_abs_overflow() {
2638         Duration.ofSeconds(Long.MIN_VALUE).abs();
2639     }
2640 
2641     //-----------------------------------------------------------------------
2642     // toNanos()
2643     //-----------------------------------------------------------------------
2644     // Android-changed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
2645     @Test(enabled = false)
test_toNanos()2646     public void test_toNanos() {
2647         assertEquals(Duration.ofSeconds(321, 123456789).toNanos(), 321123456789L);
2648         assertEquals(Duration.ofNanos(Long.MAX_VALUE).toNanos(), 9223372036854775807L);
2649         assertEquals(Duration.ofNanos(Long.MIN_VALUE).toNanos(), -9223372036854775808L);
2650     }
2651 
2652     @Test
test_toNanos_max()2653     public void test_toNanos_max() {
2654         Duration test = Duration.ofSeconds(0, Long.MAX_VALUE);
2655         assertEquals(test.toNanos(), Long.MAX_VALUE);
2656     }
2657 
2658     @Test(expectedExceptions=ArithmeticException.class)
test_toNanos_tooBig()2659     public void test_toNanos_tooBig() {
2660         Duration test = Duration.ofSeconds(0, Long.MAX_VALUE).plusNanos(1);
2661         test.toNanos();
2662     }
2663 
2664     // Android-changed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
2665     @Test(enabled = false)
test_toNanos_min()2666     public void test_toNanos_min() {
2667         Duration test = Duration.ofSeconds(0, Long.MIN_VALUE);
2668         assertEquals(test.toNanos(), Long.MIN_VALUE);
2669     }
2670 
2671     @Test(expectedExceptions=ArithmeticException.class)
test_toNanos_tooSmall()2672     public void test_toNanos_tooSmall() {
2673         Duration test = Duration.ofSeconds(0, Long.MIN_VALUE).minusNanos(1);
2674         test.toNanos();
2675     }
2676 
2677     //-----------------------------------------------------------------------
2678     // toMillis()
2679     //-----------------------------------------------------------------------
2680     // Android-changed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
2681     @Test(enabled = false)
test_toMillis()2682     public void test_toMillis() {
2683         assertEquals(Duration.ofSeconds(321, 123456789).toMillis(), 321000 + 123);
2684         assertEquals(Duration.ofMillis(Long.MAX_VALUE).toMillis(), 9223372036854775807L);
2685         assertEquals(Duration.ofMillis(Long.MIN_VALUE).toMillis(), -9223372036854775808L);
2686     }
2687 
2688     @Test
test_toMillis_max()2689     public void test_toMillis_max() {
2690         Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, (Long.MAX_VALUE % 1000) * 1000000);
2691         assertEquals(test.toMillis(), Long.MAX_VALUE);
2692     }
2693 
2694     @Test(expectedExceptions=ArithmeticException.class)
test_toMillis_tooBig()2695     public void test_toMillis_tooBig() {
2696         Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, ((Long.MAX_VALUE % 1000) + 1) * 1000000);
2697         test.toMillis();
2698     }
2699 
2700     // Android-changed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
2701     @Test(enabled = false)
test_toMillis_min()2702     public void test_toMillis_min() {
2703         Duration test = Duration.ofSeconds(Long.MIN_VALUE / 1000, (Long.MIN_VALUE % 1000) * 1000000);
2704         assertEquals(test.toMillis(), Long.MIN_VALUE);
2705     }
2706 
2707     @Test(expectedExceptions=ArithmeticException.class)
test_toMillis_tooSmall()2708     public void test_toMillis_tooSmall() {
2709         Duration test = Duration.ofSeconds(Long.MIN_VALUE / 1000, ((Long.MIN_VALUE % 1000) - 1) * 1000000);
2710         test.toMillis();
2711     }
2712 
2713     //-----------------------------------------------------------------------
2714     // toSeconds()
2715     //-----------------------------------------------------------------------
2716     @DataProvider(name="toSeconds_provider")
provider_toSeconds()2717     Object[][] provider_toSeconds() {
2718         return new Object[][] {
2719             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 31556926L},
2720             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -31556927L},
2721             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, 123_456_789), -31556926L},
2722             {Duration.ofSeconds(0), 0L},
2723             {Duration.ofSeconds(0, 123_456_789), 0L},
2724             {Duration.ofSeconds(0, -123_456_789), -1L},
2725             {Duration.ofSeconds(Long.MAX_VALUE), 9223372036854775807L},
2726             {Duration.ofSeconds(Long.MIN_VALUE), -9223372036854775808L},
2727         };
2728     }
2729 
2730     @Test(dataProvider="toSeconds_provider")
test_toSeconds(Duration dur, long seconds)2731     public void test_toSeconds(Duration dur, long seconds) {
2732         assertEquals(dur.toSeconds(), seconds);
2733     }
2734 
2735     //-----------------------------------------------------------------------
2736     // toDaysPart()
2737     //-----------------------------------------------------------------------
2738     @DataProvider(name="toDaysPart_provider")
provider_toDaysPart()2739     Object[][] provider_toDaysPart() {
2740         return new Object[][] {
2741             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 365L},
2742             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -365L},
2743             {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 123_456_789), 0L},
2744             {Duration.ofDays(365), 365L},
2745             {Duration.ofHours(2), 0L},
2746             {Duration.ofHours(-2), 0L},
2747         };
2748     }
2749 
2750     @Test(dataProvider="toDaysPart_provider")
test_toDaysPart(Duration dur, long days)2751     public void test_toDaysPart(Duration dur, long days) {
2752         assertEquals(dur.toDaysPart(), days);
2753     }
2754 
2755     //-----------------------------------------------------------------------
2756     // toHoursPart()
2757     //-----------------------------------------------------------------------
2758     @DataProvider(name="toHoursPart_provider")
provider_toHoursPart()2759     Object[][] provider_toHoursPart() {
2760         return new Object[][] {
2761             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 5},
2762             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -5},
2763             {Duration.ofSeconds(48 * 60 + 46, 123_456_789), 0},
2764             {Duration.ofHours(2), 2},
2765             {Duration.ofHours(-2), -2},
2766         };
2767     }
2768 
2769     @Test(dataProvider="toHoursPart_provider")
test_toHoursPart(Duration dur, int hours)2770     public void test_toHoursPart(Duration dur, int hours) {
2771         assertEquals(dur.toHoursPart(), hours);
2772     }
2773 
2774     //-----------------------------------------------------------------------
2775     // toMinutesPart()
2776     //-----------------------------------------------------------------------
2777     @DataProvider(name="toMinutesPart_provider")
provider_toMinutesPart()2778     Object[][] provider_toMinutesPart() {
2779         return new Object[][] {
2780             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 48},
2781             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -48},
2782             {Duration.ofSeconds(46, 123_456_789),0},
2783             {Duration.ofMinutes(48), 48},
2784             {Duration.ofHours(2), 0},
2785             {Duration.ofHours(-2),0},
2786         };
2787     }
2788 
2789     @Test(dataProvider="toMinutesPart_provider")
test_toMinutesPart(Duration dur, int minutes)2790     public void test_toMinutesPart(Duration dur, int minutes) {
2791         assertEquals(dur.toMinutesPart(), minutes);
2792     }
2793 
2794     //-----------------------------------------------------------------------
2795     // toSecondsPart()
2796     //-----------------------------------------------------------------------
2797     @DataProvider(name="toSecondsPart_provider")
provider_toSecondsPart()2798     Object[][] provider_toSecondsPart() {
2799         return new Object[][] {
2800             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 46},
2801             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -47},
2802             {Duration.ofSeconds(0, 123_456_789), 0},
2803             {Duration.ofSeconds(46), 46},
2804             {Duration.ofHours(2), 0},
2805             {Duration.ofHours(-2), 0},
2806         };
2807     }
2808 
2809     @Test(dataProvider="toSecondsPart_provider")
test_toSecondsPart(Duration dur, int seconds)2810     public void test_toSecondsPart(Duration dur, int seconds) {
2811         assertEquals(dur.toSecondsPart(), seconds);
2812     }
2813 
2814     //-----------------------------------------------------------------------
2815     // toMillisPart()
2816     //-----------------------------------------------------------------------
2817     @DataProvider(name="toMillisPart_provider")
provider_toMillisPart()2818     Object[][] provider_toMillisPart() {
2819         return new Object[][] {
2820             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 123},
2821             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), 876},
2822             {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 0), 0},
2823             {Duration.ofMillis(123), 123},
2824             {Duration.ofHours(2), 0},
2825             {Duration.ofHours(-2), 0},
2826         };
2827     }
2828 
2829     @Test(dataProvider="toMillisPart_provider")
test_toMillisPart(Duration dur, int millis)2830     public void test_toMillisPart(Duration dur, int millis) {
2831         assertEquals(dur.toMillisPart(), millis);
2832     }
2833 
2834     //-----------------------------------------------------------------------
2835     // toNanosPart()
2836     //-----------------------------------------------------------------------
2837     @DataProvider(name="toNanosPart_provider")
provider_toNanosPart()2838     Object[][] provider_toNanosPart() {
2839         return new Object[][] {
2840             {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 123_456_789},
2841             {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), 876_543_211},
2842             {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 0), 0},
2843             {Duration.ofNanos(123_456_789), 123_456_789},
2844             {Duration.ofHours(2), 0},
2845             {Duration.ofHours(-2), 0},
2846         };
2847     }
2848 
2849     @Test(dataProvider="toNanosPart_provider")
test_toNanosPart(Duration dur, int nanos)2850     public void test_toNanosPart(Duration dur, int nanos) {
2851         assertEquals(dur.toNanosPart(), nanos);
2852     }
2853 
2854     //-----------------------------------------------------------------------
2855     // compareTo()
2856     //-----------------------------------------------------------------------
2857     @Test
test_comparisons()2858     public void test_comparisons() {
2859         doTest_comparisons_Duration(
2860             Duration.ofSeconds(-2L, 0),
2861             Duration.ofSeconds(-2L, 999999998),
2862             Duration.ofSeconds(-2L, 999999999),
2863             Duration.ofSeconds(-1L, 0),
2864             Duration.ofSeconds(-1L, 1),
2865             Duration.ofSeconds(-1L, 999999998),
2866             Duration.ofSeconds(-1L, 999999999),
2867             Duration.ofSeconds(0L, 0),
2868             Duration.ofSeconds(0L, 1),
2869             Duration.ofSeconds(0L, 2),
2870             Duration.ofSeconds(0L, 999999999),
2871             Duration.ofSeconds(1L, 0),
2872             Duration.ofSeconds(2L, 0)
2873         );
2874     }
2875 
doTest_comparisons_Duration(Duration... durations)2876     void doTest_comparisons_Duration(Duration... durations) {
2877         for (int i = 0; i < durations.length; i++) {
2878             Duration a = durations[i];
2879             for (int j = 0; j < durations.length; j++) {
2880                 Duration b = durations[j];
2881                 if (i < j) {
2882                     assertEquals(a.compareTo(b)< 0, true, a + " <=> " + b);
2883                     assertEquals(a.equals(b), false, a + " <=> " + b);
2884                 } else if (i > j) {
2885                     assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b);
2886                     assertEquals(a.equals(b), false, a + " <=> " + b);
2887                 } else {
2888                     assertEquals(a.compareTo(b), 0, a + " <=> " + b);
2889                     assertEquals(a.equals(b), true, a + " <=> " + b);
2890                 }
2891             }
2892         }
2893     }
2894 
2895     @Test(expectedExceptions=NullPointerException.class)
test_compareTo_ObjectNull()2896     public void test_compareTo_ObjectNull() {
2897         Duration a = Duration.ofSeconds(0L, 0);
2898         a.compareTo(null);
2899     }
2900 
2901     @Test(expectedExceptions=ClassCastException.class)
2902     @SuppressWarnings({ "unchecked", "rawtypes" })
compareToNonDuration()2903     public void compareToNonDuration() {
2904        Comparable c = Duration.ofSeconds(0L);
2905        c.compareTo(new Object());
2906     }
2907 
2908     //-----------------------------------------------------------------------
2909     // equals()
2910     //-----------------------------------------------------------------------
2911     @Test
test_equals()2912     public void test_equals() {
2913         Duration test5a = Duration.ofSeconds(5L, 20);
2914         Duration test5b = Duration.ofSeconds(5L, 20);
2915         Duration test5n = Duration.ofSeconds(5L, 30);
2916         Duration test6 = Duration.ofSeconds(6L, 20);
2917 
2918         assertEquals(test5a.equals(test5a), true);
2919         assertEquals(test5a.equals(test5b), true);
2920         assertEquals(test5a.equals(test5n), false);
2921         assertEquals(test5a.equals(test6), false);
2922 
2923         assertEquals(test5b.equals(test5a), true);
2924         assertEquals(test5b.equals(test5b), true);
2925         assertEquals(test5b.equals(test5n), false);
2926         assertEquals(test5b.equals(test6), false);
2927 
2928         assertEquals(test5n.equals(test5a), false);
2929         assertEquals(test5n.equals(test5b), false);
2930         assertEquals(test5n.equals(test5n), true);
2931         assertEquals(test5n.equals(test6), false);
2932 
2933         assertEquals(test6.equals(test5a), false);
2934         assertEquals(test6.equals(test5b), false);
2935         assertEquals(test6.equals(test5n), false);
2936         assertEquals(test6.equals(test6), true);
2937     }
2938 
2939     @Test
test_equals_null()2940     public void test_equals_null() {
2941         Duration test5 = Duration.ofSeconds(5L, 20);
2942         assertEquals(test5.equals(null), false);
2943     }
2944 
2945     @Test
test_equals_otherClass()2946     public void test_equals_otherClass() {
2947         Duration test5 = Duration.ofSeconds(5L, 20);
2948         assertEquals(test5.equals(""), false);
2949     }
2950 
2951     //-----------------------------------------------------------------------
2952     // hashCode()
2953     //-----------------------------------------------------------------------
2954     @Test
test_hashCode()2955     public void test_hashCode() {
2956         Duration test5a = Duration.ofSeconds(5L, 20);
2957         Duration test5b = Duration.ofSeconds(5L, 20);
2958         Duration test5n = Duration.ofSeconds(5L, 30);
2959         Duration test6 = Duration.ofSeconds(6L, 20);
2960 
2961         assertEquals(test5a.hashCode() == test5a.hashCode(), true);
2962         assertEquals(test5a.hashCode() == test5b.hashCode(), true);
2963         assertEquals(test5b.hashCode() == test5b.hashCode(), true);
2964 
2965         assertEquals(test5a.hashCode() == test5n.hashCode(), false);
2966         assertEquals(test5a.hashCode() == test6.hashCode(), false);
2967     }
2968 
2969     //-----------------------------------------------------------------------
2970     @DataProvider(name="withNanos")
provider_withNanos_int()2971     Object[][] provider_withNanos_int() {
2972         return new Object[][] {
2973             {0, 0, 0,           0, 0},
2974             {0, 0, 1,           0, 1},
2975             {0, 0, 999999999,   0, 999999999},
2976 
2977             {1, 0, 0,           1, 0},
2978             {1, 0, 1,           1, 1},
2979             {1, 0, 999999999,   1, 999999999},
2980 
2981             {-1, 0, 0,           -1, 0},
2982             {-1, 0, 1,           -1, 1},
2983             {-1, 0, 999999999,   -1, 999999999},
2984 
2985             {1, 999999999, 0,           1, 0},
2986             {1, 999999999, 1,           1, 1},
2987             {1, 999999998, 2,           1, 2},
2988 
2989             {Long.MAX_VALUE, 0, 999999999, Long.MAX_VALUE, 999999999},
2990             {Long.MIN_VALUE, 0, 999999999, Long.MIN_VALUE, 999999999},
2991         };
2992     }
2993 
2994     @Test(dataProvider="withNanos")
withNanos_long(long seconds, int nanos, int amount, long expectedSeconds, int expectedNanoOfSecond)2995     public void withNanos_long(long seconds, int nanos, int amount, long expectedSeconds, int expectedNanoOfSecond) {
2996         Duration t = Duration.ofSeconds(seconds, nanos);
2997         t = t.withNanos(amount);
2998         assertEquals(t.getSeconds(), expectedSeconds);
2999         assertEquals(t.getNano(), expectedNanoOfSecond);
3000     }
3001 
3002     //-----------------------------------------------------------------------
3003     @DataProvider(name="withSeconds")
provider_withSeconds_long()3004     Object[][] provider_withSeconds_long() {
3005         return new Object[][] {
3006             {0, 0, 0, 0, 0},
3007             {0, 0, 1, 1, 0},
3008             {0, 0, -1, -1, 0},
3009             {0, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0},
3010             {0, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0},
3011 
3012             {1, 0, 0, 0, 0},
3013             {1, 0, 2, 2, 0},
3014             {1, 0, -1, -1, 0},
3015             {1, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0},
3016             {1, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0},
3017 
3018             {-1, 1, 0, 0, 1},
3019             {-1, 1, 1, 1, 1},
3020             {-1, 1, -1, -1, 1},
3021             {-1, 1, Long.MAX_VALUE, Long.MAX_VALUE, 1},
3022             {-1, 1, Long.MIN_VALUE, Long.MIN_VALUE, 1},
3023         };
3024     }
3025 
3026     @Test(dataProvider="withSeconds")
withSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond)3027     public void withSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
3028         Duration t = Duration.ofSeconds(seconds, nanos);
3029         t = t.withSeconds(amount);
3030         assertEquals(t.getSeconds(), expectedSeconds);
3031         assertEquals(t.getNano(), expectedNanoOfSecond);
3032     }
3033 
3034     //-----------------------------------------------------------------------
3035     // toString()
3036     //-----------------------------------------------------------------------
3037     @DataProvider(name="toString")
provider_toString()3038     Object[][] provider_toString() {
3039         return new Object[][] {
3040             {0, 0, "PT0S"},
3041             {0, 1, "PT0.000000001S"},
3042             {0, 10, "PT0.00000001S"},
3043             {0, 100, "PT0.0000001S"},
3044             {0, 1000, "PT0.000001S"},
3045             {0, 10000, "PT0.00001S"},
3046             {0, 100000, "PT0.0001S"},
3047             {0, 1000000, "PT0.001S"},
3048             {0, 10000000, "PT0.01S"},
3049             {0, 100000000, "PT0.1S"},
3050             {0, 120000000, "PT0.12S"},
3051             {0, 123000000, "PT0.123S"},
3052             {0, 123400000, "PT0.1234S"},
3053             {0, 123450000, "PT0.12345S"},
3054             {0, 123456000, "PT0.123456S"},
3055             {0, 123456700, "PT0.1234567S"},
3056             {0, 123456780, "PT0.12345678S"},
3057             {0, 123456789, "PT0.123456789S"},
3058             {1, 0, "PT1S"},
3059             {59, 0, "PT59S"},
3060             {60, 0, "PT1M"},
3061             {61, 0, "PT1M1S"},
3062             {3599, 0, "PT59M59S"},
3063             {3600, 0, "PT1H"},
3064             {3601, 0, "PT1H1S"},
3065             {3661, 0, "PT1H1M1S"},
3066             {86399, 0, "PT23H59M59S"},
3067             {86400, 0, "PT24H"},
3068             {59, 0, "PT59S"},
3069             {59, 0, "PT59S"},
3070             {-1, 0, "PT-1S"},
3071             {-1, 1000, "PT-0.999999S"},
3072             {-1, 900000000, "PT-0.1S"},
3073             // Android-removed: Disable this OpenJDK 11 test until java.time synced to 11. http://b/180577079
3074             // {-60, 100_000_000, "PT-59.9S"},
3075             // {-59, -900_000_000, "PT-59.9S"},
3076             // {-60, -100_000_000, "PT-1M-0.1S"},
3077             {Long.MAX_VALUE, 0, "PT" + (Long.MAX_VALUE / 3600) + "H" +
3078                     ((Long.MAX_VALUE % 3600) / 60) + "M" + (Long.MAX_VALUE % 60) + "S"},
3079             {Long.MIN_VALUE, 0, "PT" + (Long.MIN_VALUE / 3600) + "H" +
3080                     ((Long.MIN_VALUE % 3600) / 60) + "M" + (Long.MIN_VALUE % 60) + "S"},
3081         };
3082     }
3083 
3084     @Test(dataProvider="toString")
test_toString(long seconds, int nanos, String expected)3085     public void test_toString(long seconds, int nanos, String expected) {
3086         Duration t = Duration.ofSeconds(seconds, nanos);
3087         assertEquals(t.toString(), expected);
3088     }
3089 
3090     //-----------------------------------------------------------------------
3091     @Test(groups="{tck}")
test_duration_getUnits()3092     public void test_duration_getUnits() {
3093         Duration duration = Duration.ofSeconds(5000, 1000);
3094         List<TemporalUnit> units = duration.getUnits();
3095         assertEquals(units.size(), 2, "Period.getUnits length");
3096         assertTrue(units.contains(ChronoUnit.SECONDS), "Period.getUnits contains ChronoUnit.SECONDS");
3097         assertTrue(units.contains(ChronoUnit.NANOS), "contains ChronoUnit.NANOS");
3098     }
3099 
3100     @Test()
test_getUnit()3101     public void test_getUnit() {
3102         Duration test = Duration.ofSeconds(2000, 1000);
3103         long seconds = test.get(ChronoUnit.SECONDS);
3104         assertEquals(seconds, 2000, "duration.get(SECONDS)");
3105         long nanos = test.get(ChronoUnit.NANOS);
3106         assertEquals(nanos, 1000, "duration.get(NANOS)");
3107     }
3108 
3109     @DataProvider(name="BadTemporalUnit")
provider_factory_of_badTemporalUnit()3110     Object[][] provider_factory_of_badTemporalUnit() {
3111         return new Object[][] {
3112             {0, MICROS},
3113             {0, MILLIS},
3114             {0, MINUTES},
3115             {0, HOURS},
3116             {0, HALF_DAYS},
3117             {0, DAYS},
3118             {0, ChronoUnit.MONTHS},
3119             {0, ChronoUnit.YEARS},
3120             {0, ChronoUnit.DECADES},
3121             {0, ChronoUnit.CENTURIES},
3122             {0, ChronoUnit.MILLENNIA},
3123         };
3124     }
3125 
3126     @Test(dataProvider="BadTemporalUnit", expectedExceptions=DateTimeException.class)
test_bad_getUnit(long amount, TemporalUnit unit)3127     public void test_bad_getUnit(long amount, TemporalUnit unit) {
3128         Duration t = Duration.of(amount, unit);
3129         t.get(unit);
3130     }
3131 }
3132