/
TCKDuration.java
3124 lines (2799 loc) · 124 KB
/
TCKDuration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tck.java.time;
import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.HALF_DAYS;
import static java.time.temporal.ChronoUnit.HOURS;
import static java.time.temporal.ChronoUnit.MICROS;
import static java.time.temporal.ChronoUnit.MILLIS;
import static java.time.temporal.ChronoUnit.MINUTES;
import static java.time.temporal.ChronoUnit.MONTHS;
import static java.time.temporal.ChronoUnit.NANOS;
import static java.time.temporal.ChronoUnit.SECONDS;
import static java.time.temporal.ChronoUnit.WEEKS;
import static java.time.temporal.ChronoUnit.YEARS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Test Duration.
*/
@Test
public class TCKDuration extends AbstractTCKTest {
private static final long CYCLE_SECS = 146097L * 86400L;
//-----------------------------------------------------------------------
// constants
//-----------------------------------------------------------------------
@Test
public void test_zero() {
assertEquals(Duration.ZERO.getSeconds(), 0L);
assertEquals(Duration.ZERO.getNano(), 0);
}
//-----------------------------------------------------------------------
// ofSeconds(long)
//-----------------------------------------------------------------------
@Test
public void factory_seconds_long() {
for (long i = -2; i <= 2; i++) {
Duration t = Duration.ofSeconds(i);
assertEquals(t.getSeconds(), i);
assertEquals(t.getNano(), 0);
}
}
//-----------------------------------------------------------------------
// ofSeconds(long,long)
//-----------------------------------------------------------------------
@Test
public void factory_seconds_long_long() {
for (long i = -2; i <= 2; i++) {
for (int j = 0; j < 10; j++) {
Duration t = Duration.ofSeconds(i, j);
assertEquals(t.getSeconds(), i);
assertEquals(t.getNano(), j);
}
for (int j = -10; j < 0; j++) {
Duration t = Duration.ofSeconds(i, j);
assertEquals(t.getSeconds(), i - 1);
assertEquals(t.getNano(), j + 1000000000);
}
for (int j = 999999990; j < 1000000000; j++) {
Duration t = Duration.ofSeconds(i, j);
assertEquals(t.getSeconds(), i);
assertEquals(t.getNano(), j);
}
}
}
@Test
public void factory_seconds_long_long_nanosNegativeAdjusted() {
Duration test = Duration.ofSeconds(2L, -1);
assertEquals(test.getSeconds(), 1);
assertEquals(test.getNano(), 999999999);
}
@Test(expectedExceptions=ArithmeticException.class)
public void factory_seconds_long_long_tooBig() {
Duration.ofSeconds(Long.MAX_VALUE, 1000000000);
}
//-----------------------------------------------------------------------
// ofMillis(long)
//-----------------------------------------------------------------------
@DataProvider(name="MillisDurationNoNanos")
Object[][] provider_factory_millis_long() {
return new Object[][] {
{0, 0, 0},
{1, 0, 1000000},
{2, 0, 2000000},
{999, 0, 999000000},
{1000, 1, 0},
{1001, 1, 1000000},
{-1, -1, 999000000},
{-2, -1, 998000000},
{-999, -1, 1000000},
{-1000, -1, 0},
{-1001, -2, 999000000},
};
}
@Test(dataProvider="MillisDurationNoNanos")
public void factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond) {
Duration test = Duration.ofMillis(millis);
assertEquals(test.getSeconds(), expectedSeconds);
assertEquals(test.getNano(), expectedNanoOfSecond);
}
//-----------------------------------------------------------------------
// ofNanos(long)
//-----------------------------------------------------------------------
@Test
public void factory_nanos_nanos() {
Duration test = Duration.ofNanos(1);
assertEquals(test.getSeconds(), 0);
assertEquals(test.getNano(), 1);
}
@Test
public void factory_nanos_nanosSecs() {
Duration test = Duration.ofNanos(1000000002);
assertEquals(test.getSeconds(), 1);
assertEquals(test.getNano(), 2);
}
@Test
public void factory_nanos_negative() {
Duration test = Duration.ofNanos(-2000000001);
assertEquals(test.getSeconds(), -3);
assertEquals(test.getNano(), 999999999);
}
@Test
public void factory_nanos_max() {
Duration test = Duration.ofNanos(Long.MAX_VALUE);
assertEquals(test.getSeconds(), Long.MAX_VALUE / 1000000000);
assertEquals(test.getNano(), Long.MAX_VALUE % 1000000000);
}
@Test
public void factory_nanos_min() {
Duration test = Duration.ofNanos(Long.MIN_VALUE);
assertEquals(test.getSeconds(), Long.MIN_VALUE / 1000000000 - 1);
assertEquals(test.getNano(), Long.MIN_VALUE % 1000000000 + 1000000000);
}
//-----------------------------------------------------------------------
// ofMinutes()
//-----------------------------------------------------------------------
@Test
public void factory_minutes() {
Duration test = Duration.ofMinutes(2);
assertEquals(test.getSeconds(), 120);
assertEquals(test.getNano(), 0);
}
@Test
public void factory_minutes_max() {
Duration test = Duration.ofMinutes(Long.MAX_VALUE / 60);
assertEquals(test.getSeconds(), (Long.MAX_VALUE / 60) * 60);
assertEquals(test.getNano(), 0);
}
@Test
public void factory_minutes_min() {
Duration test = Duration.ofMinutes(Long.MIN_VALUE / 60);
assertEquals(test.getSeconds(), (Long.MIN_VALUE / 60) * 60);
assertEquals(test.getNano(), 0);
}
@Test(expectedExceptions=ArithmeticException.class)
public void factory_minutes_tooBig() {
Duration.ofMinutes(Long.MAX_VALUE / 60 + 1);
}
@Test(expectedExceptions=ArithmeticException.class)
public void factory_minutes_tooSmall() {
Duration.ofMinutes(Long.MIN_VALUE / 60 - 1);
}
//-----------------------------------------------------------------------
// ofHours()
//-----------------------------------------------------------------------
@Test
public void factory_hours() {
Duration test = Duration.ofHours(2);
assertEquals(test.getSeconds(), 2 * 3600);
assertEquals(test.getNano(), 0);
}
@Test
public void factory_hours_max() {
Duration test = Duration.ofHours(Long.MAX_VALUE / 3600);
assertEquals(test.getSeconds(), (Long.MAX_VALUE / 3600) * 3600);
assertEquals(test.getNano(), 0);
}
@Test
public void factory_hours_min() {
Duration test = Duration.ofHours(Long.MIN_VALUE / 3600);
assertEquals(test.getSeconds(), (Long.MIN_VALUE / 3600) * 3600);
assertEquals(test.getNano(), 0);
}
@Test(expectedExceptions=ArithmeticException.class)
public void factory_hours_tooBig() {
Duration.ofHours(Long.MAX_VALUE / 3600 + 1);
}
@Test(expectedExceptions=ArithmeticException.class)
public void factory_hours_tooSmall() {
Duration.ofHours(Long.MIN_VALUE / 3600 - 1);
}
//-----------------------------------------------------------------------
// ofDays()
//-----------------------------------------------------------------------
@Test
public void factory_days() {
Duration test = Duration.ofDays(2);
assertEquals(test.getSeconds(), 2 * 86400);
assertEquals(test.getNano(), 0);
}
@Test
public void factory_days_max() {
Duration test = Duration.ofDays(Long.MAX_VALUE / 86400);
assertEquals(test.getSeconds(), (Long.MAX_VALUE / 86400) * 86400);
assertEquals(test.getNano(), 0);
}
@Test
public void factory_days_min() {
Duration test = Duration.ofDays(Long.MIN_VALUE / 86400);
assertEquals(test.getSeconds(), (Long.MIN_VALUE / 86400) * 86400);
assertEquals(test.getNano(), 0);
}
@Test(expectedExceptions=ArithmeticException.class)
public void factory_days_tooBig() {
Duration.ofDays(Long.MAX_VALUE / 86400 + 1);
}
@Test(expectedExceptions=ArithmeticException.class)
public void factory_days_tooSmall() {
Duration.ofDays(Long.MIN_VALUE / 86400 - 1);
}
//-----------------------------------------------------------------------
// of(long,TemporalUnit)
//-----------------------------------------------------------------------
@DataProvider(name="OfTemporalUnit")
Object[][] provider_factory_of_longTemporalUnit() {
return new Object[][] {
{0, NANOS, 0, 0},
{0, MICROS, 0, 0},
{0, MILLIS, 0, 0},
{0, SECONDS, 0, 0},
{0, MINUTES, 0, 0},
{0, HOURS, 0, 0},
{0, HALF_DAYS, 0, 0},
{0, DAYS, 0, 0},
{1, NANOS, 0, 1},
{1, MICROS, 0, 1000},
{1, MILLIS, 0, 1000000},
{1, SECONDS, 1, 0},
{1, MINUTES, 60, 0},
{1, HOURS, 3600, 0},
{1, HALF_DAYS, 43200, 0},
{1, DAYS, 86400, 0},
{3, NANOS, 0, 3},
{3, MICROS, 0, 3000},
{3, MILLIS, 0, 3000000},
{3, SECONDS, 3, 0},
{3, MINUTES, 3 * 60, 0},
{3, HOURS, 3 * 3600, 0},
{3, HALF_DAYS, 3 * 43200, 0},
{3, DAYS, 3 * 86400, 0},
{-1, NANOS, -1, 999999999},
{-1, MICROS, -1, 999999000},
{-1, MILLIS, -1, 999000000},
{-1, SECONDS, -1, 0},
{-1, MINUTES, -60, 0},
{-1, HOURS, -3600, 0},
{-1, HALF_DAYS, -43200, 0},
{-1, DAYS, -86400, 0},
{-3, NANOS, -1, 999999997},
{-3, MICROS, -1, 999997000},
{-3, MILLIS, -1, 997000000},
{-3, SECONDS, -3, 0},
{-3, MINUTES, -3 * 60, 0},
{-3, HOURS, -3 * 3600, 0},
{-3, HALF_DAYS, -3 * 43200, 0},
{-3, DAYS, -3 * 86400, 0},
{Long.MAX_VALUE, NANOS, Long.MAX_VALUE / 1000000000, (int) (Long.MAX_VALUE % 1000000000)},
{Long.MIN_VALUE, NANOS, Long.MIN_VALUE / 1000000000 - 1, (int) (Long.MIN_VALUE % 1000000000 + 1000000000)},
{Long.MAX_VALUE, MICROS, Long.MAX_VALUE / 1000000, (int) ((Long.MAX_VALUE % 1000000) * 1000)},
{Long.MIN_VALUE, MICROS, Long.MIN_VALUE / 1000000 - 1, (int) ((Long.MIN_VALUE % 1000000 + 1000000) * 1000)},
{Long.MAX_VALUE, MILLIS, Long.MAX_VALUE / 1000, (int) ((Long.MAX_VALUE % 1000) * 1000000)},
{Long.MIN_VALUE, MILLIS, Long.MIN_VALUE / 1000 - 1, (int) ((Long.MIN_VALUE % 1000 + 1000) * 1000000)},
{Long.MAX_VALUE, SECONDS, Long.MAX_VALUE, 0},
{Long.MIN_VALUE, SECONDS, Long.MIN_VALUE, 0},
{Long.MAX_VALUE / 60, MINUTES, (Long.MAX_VALUE / 60) * 60, 0},
{Long.MIN_VALUE / 60, MINUTES, (Long.MIN_VALUE / 60) * 60, 0},
{Long.MAX_VALUE / 3600, HOURS, (Long.MAX_VALUE / 3600) * 3600, 0},
{Long.MIN_VALUE / 3600, HOURS, (Long.MIN_VALUE / 3600) * 3600, 0},
{Long.MAX_VALUE / 43200, HALF_DAYS, (Long.MAX_VALUE / 43200) * 43200, 0},
{Long.MIN_VALUE / 43200, HALF_DAYS, (Long.MIN_VALUE / 43200) * 43200, 0},
};
}
@Test(dataProvider="OfTemporalUnit")
public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) {
Duration t = Duration.of(amount, unit);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@DataProvider(name="OfTemporalUnitOutOfRange")
Object[][] provider_factory_of_longTemporalUnit_outOfRange() {
return new Object[][] {
{Long.MAX_VALUE / 60 + 1, MINUTES},
{Long.MIN_VALUE / 60 - 1, MINUTES},
{Long.MAX_VALUE / 3600 + 1, HOURS},
{Long.MIN_VALUE / 3600 - 1, HOURS},
{Long.MAX_VALUE / 43200 + 1, HALF_DAYS},
{Long.MIN_VALUE / 43200 - 1, HALF_DAYS},
};
}
@Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class)
public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
Duration.of(amount, unit);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_of_longTemporalUnit_estimatedUnit() {
Duration.of(2, WEEKS);
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_of_longTemporalUnit_null() {
Duration.of(1, (TemporalUnit) null);
}
//-----------------------------------------------------------------------
// from(TemporalAmount)
//-----------------------------------------------------------------------
@Test
public void factory_from_TemporalAmount_Duration() {
TemporalAmount amount = Duration.ofHours(3);
assertEquals(Duration.from(amount), Duration.ofHours(3));
}
@Test
public void factory_from_TemporalAmount_DaysNanos() {
TemporalAmount amount = new TemporalAmount() {
@Override
public long get(TemporalUnit unit) {
if (unit == DAYS) {
return 23;
} else {
return 45;
}
}
@Override
public List<TemporalUnit> getUnits() {
List<TemporalUnit> list = new ArrayList<>();
list.add(DAYS);
list.add(NANOS);
return list;
}
@Override
public Temporal addTo(Temporal temporal) {
throw new UnsupportedOperationException();
}
@Override
public Temporal subtractFrom(Temporal temporal) {
throw new UnsupportedOperationException();
}
};
Duration t = Duration.from(amount);
assertEquals(t.getSeconds(), 23 * 86400);
assertEquals(t.getNano(), 45);
}
@Test(expectedExceptions = ArithmeticException.class)
public void factory_from_TemporalAmount_Minutes_tooBig() {
TemporalAmount amount = new TemporalAmount() {
@Override
public long get(TemporalUnit unit) {
return (Long.MAX_VALUE / 60) + 2;
}
@Override
public List<TemporalUnit> getUnits() {
return Collections.<TemporalUnit>singletonList(MINUTES);
}
@Override
public Temporal addTo(Temporal temporal) {
throw new UnsupportedOperationException();
}
@Override
public Temporal subtractFrom(Temporal temporal) {
throw new UnsupportedOperationException();
}
};
Duration.from(amount);
}
@Test(expectedExceptions = DateTimeException.class)
public void factory_from_TemporalAmount_Period() {
Duration.from(Period.ZERO);
}
@Test(expectedExceptions = NullPointerException.class)
public void factory_from_TemporalAmount_null() {
Duration.from(null);
}
//-----------------------------------------------------------------------
// parse(String)
//-----------------------------------------------------------------------
@DataProvider(name="parseSuccess")
Object[][] data_parseSuccess() {
return new Object[][] {
{"PT0S", 0, 0},
{"PT1S", 1, 0},
{"PT12S", 12, 0},
{"PT123456789S", 123456789, 0},
{"PT" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0},
{"PT+1S", 1, 0},
{"PT+12S", 12, 0},
{"PT+123456789S", 123456789, 0},
{"PT+" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0},
{"PT-1S", -1, 0},
{"PT-12S", -12, 0},
{"PT-123456789S", -123456789, 0},
{"PT" + Long.MIN_VALUE + "S", Long.MIN_VALUE, 0},
{"PT0.1S", 0, 100000000},
{"PT1.1S", 1, 100000000},
{"PT1.12S", 1, 120000000},
{"PT1.123S", 1, 123000000},
{"PT1.1234S", 1, 123400000},
{"PT1.12345S", 1, 123450000},
{"PT1.123456S", 1, 123456000},
{"PT1.1234567S", 1, 123456700},
{"PT1.12345678S", 1, 123456780},
{"PT1.123456789S", 1, 123456789},
{"PT-0.1S", -1, 1000000000 - 100000000},
{"PT-1.1S", -2, 1000000000 - 100000000},
{"PT-1.12S", -2, 1000000000 - 120000000},
{"PT-1.123S", -2, 1000000000 - 123000000},
{"PT-1.1234S", -2, 1000000000 - 123400000},
{"PT-1.12345S", -2, 1000000000 - 123450000},
{"PT-1.123456S", -2, 1000000000 - 123456000},
{"PT-1.1234567S", -2, 1000000000 - 123456700},
{"PT-1.12345678S", -2, 1000000000 - 123456780},
{"PT-1.123456789S", -2, 1000000000 - 123456789},
{"PT" + Long.MAX_VALUE + ".123456789S", Long.MAX_VALUE, 123456789},
{"PT" + Long.MIN_VALUE + ".000000000S", Long.MIN_VALUE, 0},
{"PT12M", 12 * 60, 0},
{"PT12M0.35S", 12 * 60, 350000000},
{"PT12M1.35S", 12 * 60 + 1, 350000000},
{"PT12M-0.35S", 12 * 60 - 1, 1000000000 - 350000000},
{"PT12M-1.35S", 12 * 60 - 2, 1000000000 - 350000000},
{"PT12H", 12 * 3600, 0},
{"PT12H0.35S", 12 * 3600, 350000000},
{"PT12H1.35S", 12 * 3600 + 1, 350000000},
{"PT12H-0.35S", 12 * 3600 - 1, 1000000000 - 350000000},
{"PT12H-1.35S", 12 * 3600 - 2, 1000000000 - 350000000},
{"P12D", 12 * 24 * 3600, 0},
{"P12DT0.35S", 12 * 24 * 3600, 350000000},
{"P12DT1.35S", 12 * 24 * 3600 + 1, 350000000},
{"P12DT-0.35S", 12 * 24 * 3600 - 1, 1000000000 - 350000000},
{"P12DT-1.35S", 12 * 24 * 3600 - 2, 1000000000 - 350000000},
{"PT01S", 1, 0},
{"PT001S", 1, 0},
{"PT000S", 0, 0},
{"PT+01S", 1, 0},
{"PT-01S", -1, 0},
{"PT1.S", 1, 0},
{"PT+1.S", 1, 0},
{"PT-1.S", -1, 0},
{"P0D", 0, 0},
{"P0DT0H", 0, 0},
{"P0DT0M", 0, 0},
{"P0DT0S", 0, 0},
{"P0DT0H0S", 0, 0},
{"P0DT0M0S", 0, 0},
{"P0DT0H0M0S", 0, 0},
{"P1D", 86400, 0},
{"P1DT0H", 86400, 0},
{"P1DT0M", 86400, 0},
{"P1DT0S", 86400, 0},
{"P1DT0H0S", 86400, 0},
{"P1DT0M0S", 86400, 0},
{"P1DT0H0M0S", 86400, 0},
{"P3D", 86400 * 3, 0},
{"P3DT2H", 86400 * 3 + 3600 * 2, 0},
{"P3DT2M", 86400 * 3 + 60 * 2, 0},
{"P3DT2S", 86400 * 3 + 2, 0},
{"P3DT2H1S", 86400 * 3 + 3600 * 2 + 1, 0},
{"P3DT2M1S", 86400 * 3 + 60 * 2 + 1, 0},
{"P3DT2H1M1S", 86400 * 3 + 3600 * 2 + 60 + 1, 0},
{"P-3D", -86400 * 3, 0},
{"P-3DT2H", -86400 * 3 + 3600 * 2, 0},
{"P-3DT2M", -86400 * 3 + 60 * 2, 0},
{"P-3DT2S", -86400 * 3 + 2, 0},
{"P-3DT2H1S", -86400 * 3 + 3600 * 2 + 1, 0},
{"P-3DT2M1S", -86400 * 3 + 60 * 2 + 1, 0},
{"P-3DT2H1M1S", -86400 * 3 + 3600 * 2 + 60 + 1, 0},
{"P-3DT-2H", -86400 * 3 - 3600 * 2, 0},
{"P-3DT-2M", -86400 * 3 - 60 * 2, 0},
{"P-3DT-2S", -86400 * 3 - 2, 0},
{"P-3DT-2H1S", -86400 * 3 - 3600 * 2 + 1, 0},
{"P-3DT-2M1S", -86400 * 3 - 60 * 2 + 1, 0},
{"P-3DT-2H1M1S", -86400 * 3 - 3600 * 2 + 60 + 1, 0},
{"PT0H", 0, 0},
{"PT0H0M", 0, 0},
{"PT0H0S", 0, 0},
{"PT0H0M0S", 0, 0},
{"PT1H", 3600, 0},
{"PT3H", 3600 * 3, 0},
{"PT-1H", -3600, 0},
{"PT-3H", -3600 * 3, 0},
{"PT2H5M", 3600 * 2 + 60 * 5, 0},
{"PT2H5S", 3600 * 2 + 5, 0},
{"PT2H5M8S", 3600 * 2 + 60 * 5 + 8, 0},
{"PT-2H5M", -3600 * 2 + 60 * 5, 0},
{"PT-2H5S", -3600 * 2 + 5, 0},
{"PT-2H5M8S", -3600 * 2 + 60 * 5 + 8, 0},
{"PT-2H-5M", -3600 * 2 - 60 * 5, 0},
{"PT-2H-5S", -3600 * 2 - 5, 0},
{"PT-2H-5M8S", -3600 * 2 - 60 * 5 + 8, 0},
{"PT-2H-5M-8S", -3600 * 2 - 60 * 5 - 8, 0},
{"PT0M", 0, 0},
{"PT1M", 60, 0},
{"PT3M", 60 * 3, 0},
{"PT-1M", -60, 0},
{"PT-3M", -60 * 3, 0},
{"P0DT3M", 60 * 3, 0},
{"P0DT-3M", -60 * 3, 0},
};
}
@Test(dataProvider="parseSuccess")
public void factory_parse(String text, long expectedSeconds, int expectedNanoOfSecond) {
Duration test = Duration.parse(text);
assertEquals(test.getSeconds(), expectedSeconds);
assertEquals(test.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="parseSuccess")
public void factory_parse_plus(String text, long expectedSeconds, int expectedNanoOfSecond) {
Duration test = Duration.parse("+" + text);
assertEquals(test.getSeconds(), expectedSeconds);
assertEquals(test.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="parseSuccess")
public void factory_parse_minus(String text, long expectedSeconds, int expectedNanoOfSecond) {
Duration test;
try {
test = Duration.parse("-" + text);
} catch (DateTimeParseException ex) {
assertEquals(expectedSeconds == Long.MIN_VALUE, true);
return;
}
// not inside try/catch or it breaks test
assertEquals(test, Duration.ofSeconds(expectedSeconds, expectedNanoOfSecond).negated());
}
@Test(dataProvider="parseSuccess")
public void factory_parse_comma(String text, long expectedSeconds, int expectedNanoOfSecond) {
text = text.replace('.', ',');
Duration test = Duration.parse(text);
assertEquals(test.getSeconds(), expectedSeconds);
assertEquals(test.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="parseSuccess")
public void factory_parse_lowerCase(String text, long expectedSeconds, int expectedNanoOfSecond) {
Duration test = Duration.parse(text.toLowerCase(Locale.ENGLISH));
assertEquals(test.getSeconds(), expectedSeconds);
assertEquals(test.getNano(), expectedNanoOfSecond);
}
@DataProvider(name="parseFailure")
Object[][] data_parseFailure() {
return new Object[][] {
{""},
{"ABCDEF"},
{" PT0S"},
{"PT0S "},
{"PTS"},
{"AT0S"},
{"PA0S"},
{"PT0A"},
{"P0Y"},
{"P1Y"},
{"P-2Y"},
{"P0M"},
{"P1M"},
{"P-2M"},
{"P3Y2D"},
{"P3M2D"},
{"P3W"},
{"P-3W"},
{"P2YT30S"},
{"P2MT30S"},
{"P1DT"},
{"PT+S"},
{"PT-S"},
{"PT.S"},
{"PTAS"},
{"PT-.S"},
{"PT+.S"},
{"PT1ABC2S"},
{"PT1.1ABC2S"},
{"PT123456789123456789123456789S"},
{"PT0.1234567891S"},
{"PT2.-3"},
{"PT-2.-3"},
{"PT2.+3"},
{"PT-2.+3"},
};
}
@Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
public void factory_parseFailures(String text) {
Duration.parse(text);
}
@Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
public void factory_parseFailures_comma(String text) {
text = text.replace('.', ',');
Duration.parse(text);
}
@Test(expectedExceptions=DateTimeParseException.class)
public void factory_parse_tooBig() {
Duration.parse("PT" + Long.MAX_VALUE + "1S");
}
@Test(expectedExceptions=DateTimeParseException.class)
public void factory_parse_tooBig_decimal() {
Duration.parse("PT" + Long.MAX_VALUE + "1.1S");
}
@Test(expectedExceptions=DateTimeParseException.class)
public void factory_parse_tooSmall() {
Duration.parse("PT" + Long.MIN_VALUE + "1S");
}
@Test(expectedExceptions=DateTimeParseException.class)
public void factory_parse_tooSmall_decimal() {
Duration.parse("PT" + Long.MIN_VALUE + ".1S");
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_nullText() {
Duration.parse(null);
}
//-----------------------------------------------------------------------
// between()
//-----------------------------------------------------------------------
@DataProvider(name="durationBetweenInstant")
Object[][] data_durationBetweenInstant() {
return new Object[][] {
{0, 0, 0, 0, 0, 0},
{3, 0, 7, 0, 4, 0},
{7, 0, 3, 0, -4, 0},
{3, 20, 7, 50, 4, 30},
{3, 80, 7, 50, 3, 999999970},
{3, 80, 7, 79, 3, 999999999},
{3, 80, 7, 80, 4, 0},
{3, 80, 7, 81, 4, 1},
};
}
@Test(dataProvider="durationBetweenInstant")
public void factory_between_TemporalTemporal_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) {
Instant start = Instant.ofEpochSecond(secs1, nanos1);
Instant end = Instant.ofEpochSecond(secs2, nanos2);
Duration t = Duration.between(start, end);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="durationBetweenInstant")
public void factory_between_TemporalTemporal_Instant_negated(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) {
Instant start = Instant.ofEpochSecond(secs1, nanos1);
Instant end = Instant.ofEpochSecond(secs2, nanos2);
assertEquals(Duration.between(end, start), Duration.between(start, end).negated());
}
@DataProvider(name="durationBetweenLocalTime")
Object[][] data_durationBetweenLocalTime() {
return new Object[][] {
{LocalTime.of(11, 0, 30), LocalTime.of(11, 0, 45), 15L, 0},
{LocalTime.of(11, 0, 30), LocalTime.of(11, 0, 25), -5L, 0},
};
}
@Test(dataProvider="durationBetweenLocalTime")
public void factory_between_TemporalTemporal_LT(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond) {
Duration t = Duration.between(start, end);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="durationBetweenLocalTime")
public void factory_between_TemporalTemporal_LT_negated(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond) {
assertEquals(Duration.between(end, start), Duration.between(start, end).negated());
}
@DataProvider(name="durationBetweenLocalDateTime")
Object[][] data_durationBetweenLocalDateTime() {
return new Object[][] {
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 30, 65_000_000), -1L, 0},
{LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), 0L, 0},
{LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 32, 65_000_000), 1L, 0},
{LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 33, 65_000_000), 2L, 0},
{LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 34, 65_000_000), 3L, 0},
{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},
{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},
{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},
};
}
@Test(dataProvider="durationBetweenLocalDateTime")
public void factory_between_TemporalTemporal_LDT(LocalDateTime start, LocalDateTime end, long expectedSeconds, int expectedNanoOfSecond) {
Duration t = Duration.between(start, end);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="durationBetweenLocalDateTime")
public void factory_between_TemporalTemporal_LDT_negated(LocalDateTime start, LocalDateTime end, long expectedSeconds, int expectedNanoOfSecond) {
assertEquals(Duration.between(end, start), Duration.between(start, end).negated());
}
@Test
public void factory_between_TemporalTemporal_mixedTypes() {
Instant start = Instant.ofEpochSecond(1);
ZonedDateTime end = Instant.ofEpochSecond(4).atZone(ZoneOffset.UTC);
assertEquals(Duration.between(start, end), Duration.ofSeconds(3));
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_between_TemporalTemporal_invalidMixedTypes() {
Instant start = Instant.ofEpochSecond(1);
LocalDate end = LocalDate.of(2010, 6, 20);
Duration.between(start, end);
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_between__TemporalTemporal_startNull() {
Instant end = Instant.ofEpochSecond(1);
Duration.between(null, end);
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_between__TemporalTemporal_endNull() {
Instant start = Instant.ofEpochSecond(1);
Duration.between(start, null);
}
//-----------------------------------------------------------------------
// isZero(), isPositive(), isPositiveOrZero(), isNegative(), isNegativeOrZero()
//-----------------------------------------------------------------------
@Test
public void test_isZero() {
assertEquals(Duration.ofNanos(0).isZero(), true);
assertEquals(Duration.ofSeconds(0).isZero(), true);
assertEquals(Duration.ofNanos(1).isZero(), false);
assertEquals(Duration.ofSeconds(1).isZero(), false);
assertEquals(Duration.ofSeconds(1, 1).isZero(), false);
assertEquals(Duration.ofNanos(-1).isZero(), false);
assertEquals(Duration.ofSeconds(-1).isZero(), false);
assertEquals(Duration.ofSeconds(-1, -1).isZero(), false);
}
@Test
public void test_isPositive() {
assertEquals(Duration.ofNanos(0).isPositive(), false);
assertEquals(Duration.ofSeconds(0).isPositive(), false);
assertEquals(Duration.ofNanos(1).isPositive(), true);
assertEquals(Duration.ofSeconds(1).isPositive(), true);
assertEquals(Duration.ofSeconds(1, 1).isPositive(), true);
assertEquals(Duration.ofSeconds(Long.MAX_VALUE, 999_999_999).isPositive(), true);
assertEquals(Duration.ofNanos(-1).isPositive(), false);
assertEquals(Duration.ofSeconds(-1).isPositive(), false);
assertEquals(Duration.ofSeconds(-1, -1).isPositive(), false);
assertEquals(Duration.ofSeconds(Long.MIN_VALUE).isPositive(), false);
}
@Test
public void test_isNegative() {
assertEquals(Duration.ofNanos(0).isNegative(), false);
assertEquals(Duration.ofSeconds(0).isNegative(), false);
assertEquals(Duration.ofNanos(1).isNegative(), false);
assertEquals(Duration.ofSeconds(1).isNegative(), false);
assertEquals(Duration.ofSeconds(1, 1).isNegative(), false);
assertEquals(Duration.ofSeconds(Long.MAX_VALUE, 999_999_999).isNegative(), false);
assertEquals(Duration.ofNanos(-1).isNegative(), true);
assertEquals(Duration.ofSeconds(-1).isNegative(), true);
assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
assertEquals(Duration.ofSeconds(Long.MIN_VALUE).isNegative(), true);
}
//-----------------------------------------------------------------------
// plus()
//-----------------------------------------------------------------------
@DataProvider(name="Plus")
Object[][] provider_plus() {
return new Object[][] {
{Long.MIN_VALUE, 0, Long.MAX_VALUE, 0, -1, 0},
{-4, 666666667, -4, 666666667, -7, 333333334},
{-4, 666666667, -3, 0, -7, 666666667},
{-4, 666666667, -2, 0, -6, 666666667},
{-4, 666666667, -1, 0, -5, 666666667},
{-4, 666666667, -1, 333333334, -4, 1},
{-4, 666666667, -1, 666666667, -4, 333333334},
{-4, 666666667, -1, 999999999, -4, 666666666},
{-4, 666666667, 0, 0, -4, 666666667},
{-4, 666666667, 0, 1, -4, 666666668},
{-4, 666666667, 0, 333333333, -3, 0},
{-4, 666666667, 0, 666666666, -3, 333333333},
{-4, 666666667, 1, 0, -3, 666666667},
{-4, 666666667, 2, 0, -2, 666666667},
{-4, 666666667, 3, 0, -1, 666666667},
{-4, 666666667, 3, 333333333, 0, 0},
{-3, 0, -4, 666666667, -7, 666666667},
{-3, 0, -3, 0, -6, 0},
{-3, 0, -2, 0, -5, 0},
{-3, 0, -1, 0, -4, 0},
{-3, 0, -1, 333333334, -4, 333333334},
{-3, 0, -1, 666666667, -4, 666666667},
{-3, 0, -1, 999999999, -4, 999999999},
{-3, 0, 0, 0, -3, 0},
{-3, 0, 0, 1, -3, 1},
{-3, 0, 0, 333333333, -3, 333333333},
{-3, 0, 0, 666666666, -3, 666666666},
{-3, 0, 1, 0, -2, 0},
{-3, 0, 2, 0, -1, 0},
{-3, 0, 3, 0, 0, 0},
{-3, 0, 3, 333333333, 0, 333333333},
{-2, 0, -4, 666666667, -6, 666666667},
{-2, 0, -3, 0, -5, 0},
{-2, 0, -2, 0, -4, 0},
{-2, 0, -1, 0, -3, 0},
{-2, 0, -1, 333333334, -3, 333333334},
{-2, 0, -1, 666666667, -3, 666666667},
{-2, 0, -1, 999999999, -3, 999999999},
{-2, 0, 0, 0, -2, 0},
{-2, 0, 0, 1, -2, 1},
{-2, 0, 0, 333333333, -2, 333333333},
{-2, 0, 0, 666666666, -2, 666666666},
{-2, 0, 1, 0, -1, 0},
{-2, 0, 2, 0, 0, 0},
{-2, 0, 3, 0, 1, 0},
{-2, 0, 3, 333333333, 1, 333333333},
{-1, 0, -4, 666666667, -5, 666666667},
{-1, 0, -3, 0, -4, 0},
{-1, 0, -2, 0, -3, 0},
{-1, 0, -1, 0, -2, 0},
{-1, 0, -1, 333333334, -2, 333333334},
{-1, 0, -1, 666666667, -2, 666666667},