-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTemporalCoreTest.cpp
More file actions
3347 lines (2913 loc) · 178 KB
/
TemporalCoreTest.cpp
File metadata and controls
3347 lines (2913 loc) · 178 KB
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) 2026 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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.
*/
#include "config.h"
#include "TemporalCoreTest.h"
#include "CalendarArithmetic.h"
#include "CalendarFields.h"
#include "CalendarICUBridge.h"
#include "DurationArithmetic.h"
#include "ISO8601.h"
#include "ISOArithmetic.h"
#include "InstantCore.h"
#include "JSCTimeZone.h"
#include "PlainDateTimeCore.h"
#include "Rounding.h"
#include "TemporalCoreTypes.h"
#include "TemporalEnums.h"
#include "TimeZoneICUBridge.h"
#include "ZonedDateTimeCore.h"
#include <stdio.h>
#include <wtf/Int128.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
namespace TemporalCore {
// ---------------------------------------------------------------------------
// Assertion helpers
// ---------------------------------------------------------------------------
static int s_failures = 0;
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
#define TCHECK_EQ(actual, expected, name) \
do { \
if ((actual) != (expected)) { \
fprintf(stderr, "FAIL [%s]: got %s, expected %s\n", name, #actual, #expected); \
s_failures++; \
} \
} while (0)
#define TCHECK_TRUE(cond, name) \
do { \
if (!(cond)) { \
fprintf(stderr, "FAIL [%s]: condition false: %s\n", name, #cond); \
s_failures++; \
} \
} while (0)
#define TCHECK_ERR(result, name) \
do { \
if ((result)) { \
fprintf(stderr, "FAIL [%s]: expected error but got success\n", name); \
s_failures++; \
} \
} while (0)
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
// ---------------------------------------------------------------------------
// ISOArithmetic tests — mirrors temporal_rs src/iso.rs tests
// ---------------------------------------------------------------------------
static void testBalanceISODate()
{
// temporal_rs: test balance_iso_date
// 2020-01-32 -> 2020-02-01
auto r = balanceISODate(2020, 1, 32);
TCHECK_EQ(r.year(), 2020, "balanceISODate: 2020-01-32 year");
TCHECK_EQ(r.month(), 2u, "balanceISODate: 2020-01-32 month");
TCHECK_EQ(r.day(), 1u, "balanceISODate: 2020-01-32 day");
// 2020-12-32 -> 2021-01-01
auto r2 = balanceISODate(2020, 12, 32);
TCHECK_EQ(r2.year(), 2021, "balanceISODate: 2020-12-32 year");
TCHECK_EQ(r2.month(), 1u, "balanceISODate: 2020-12-32 month");
TCHECK_EQ(r2.day(), 1u, "balanceISODate: 2020-12-32 day");
// 2020-01-00 -> 2019-12-31
auto r3 = balanceISODate(2020, 1, 0);
TCHECK_EQ(r3.year(), 2019, "balanceISODate: 2020-01-00 year");
TCHECK_EQ(r3.month(), 12u, "balanceISODate: 2020-01-00 month");
TCHECK_EQ(r3.day(), 31u, "balanceISODate: 2020-01-00 day");
// 2020-03-01 (unchanged)
auto r4 = balanceISODate(2020, 3, 1);
TCHECK_EQ(r4.year(), 2020, "balanceISODate: 2020-03-01 year");
TCHECK_EQ(r4.month(), 3u, "balanceISODate: 2020-03-01 month");
TCHECK_EQ(r4.day(), 1u, "balanceISODate: 2020-03-01 day");
}
static void testRegulateISODate()
{
// temporal_rs: test regulate_iso_date
// constrain: 2020-02-30 -> 2020-02-29 (2020 is leap year)
auto r = regulateISODate(2020, 2, 30, TemporalOverflow::Constrain);
TCHECK_TRUE(r.has_value(), "regulateISODate: constrain 2020-02-30 ok");
TCHECK_EQ(r->month(), 2u, "regulateISODate: constrain month");
TCHECK_EQ(r->day(), 29u, "regulateISODate: constrain day");
// constrain: 2021-02-30 -> 2021-02-28 (2021 not leap)
auto r2 = regulateISODate(2021, 2, 30, TemporalOverflow::Constrain);
TCHECK_TRUE(r2.has_value(), "regulateISODate: constrain 2021-02-30 ok");
TCHECK_EQ(r2->day(), 28u, "regulateISODate: constrain 2021-02-30 day");
// reject: 2020-13-01 -> error
auto r3 = regulateISODate(2020, 13, 1, TemporalOverflow::Reject);
TCHECK_TRUE(!r3.has_value(), "regulateISODate: reject 2020-13-01 errors");
// reject: valid 2020-06-15 -> ok
auto r4 = regulateISODate(2020, 6, 15, TemporalOverflow::Reject);
TCHECK_TRUE(r4.has_value(), "regulateISODate: reject 2020-06-15 ok");
TCHECK_EQ(r4->year(), 2020, "regulateISODate: reject year");
TCHECK_EQ(r4->month(), 6u, "regulateISODate: reject month");
TCHECK_EQ(r4->day(), 15u, "regulateISODate: reject day");
}
static void testISODateAdd()
{
// temporal_rs: plain_date.rs test simple_date_add
// 1976-11-18 + P43Y -> 2019-11-18
auto r1 = isoDateAdd({ 1976, 11, 18 }, ISO8601::Duration(43, 0, 0, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r1.has_value(), "isoDateAdd: +43y ok");
TCHECK_EQ(r1->year(), 2019, "isoDateAdd: +43y year");
TCHECK_EQ(r1->month(), 11u, "isoDateAdd: +43y month");
TCHECK_EQ(r1->day(), 18u, "isoDateAdd: +43y day");
// 1976-11-18 + P3M -> 1977-02-18
auto r2 = isoDateAdd({ 1976, 11, 18 }, ISO8601::Duration(0, 3, 0, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r2.has_value(), "isoDateAdd: +3m ok");
TCHECK_EQ(r2->year(), 1977, "isoDateAdd: +3m year");
TCHECK_EQ(r2->month(), 2u, "isoDateAdd: +3m month");
TCHECK_EQ(r2->day(), 18u, "isoDateAdd: +3m day");
// 1976-11-18 + P20D -> 1976-12-08
auto r3 = isoDateAdd({ 1976, 11, 18 }, ISO8601::Duration(0, 0, 0, 20, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r3.has_value(), "isoDateAdd: +20d ok");
TCHECK_EQ(r3->year(), 1976, "isoDateAdd: +20d year");
TCHECK_EQ(r3->month(), 12u, "isoDateAdd: +20d month");
TCHECK_EQ(r3->day(), 8u, "isoDateAdd: +20d day");
// 2019-11-18 - P43Y -> 1976-11-18
auto r4 = isoDateAdd({ 2019, 11, 18 }, ISO8601::Duration(-43, 0, 0, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r4.has_value(), "isoDateAdd: -43y ok");
TCHECK_EQ(r4->year(), 1976, "isoDateAdd: -43y year");
// constrain: 2021-01-31 + P1M -> 2021-02-28 (not 2021-02-31)
auto r5 = isoDateAdd({ 2021, 1, 31 }, ISO8601::Duration(0, 1, 0, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r5.has_value(), "isoDateAdd: end-of-month constrain ok");
TCHECK_EQ(r5->month(), 2u, "isoDateAdd: end-of-month month");
TCHECK_EQ(r5->day(), 28u, "isoDateAdd: end-of-month day");
}
static void testISODateCompare()
{
// temporal_rs: src/iso.rs IsoDate::cmp
TCHECK_EQ(isoDateCompare({ 2020, 1, 1 }, { 2020, 1, 1 }), 0, "isoDateCompare: equal");
TCHECK_EQ(isoDateCompare({ 2020, 1, 2 }, { 2020, 1, 1 }), 1, "isoDateCompare: later day");
TCHECK_EQ(isoDateCompare({ 2020, 1, 1 }, { 2020, 1, 2 }), -1, "isoDateCompare: earlier day");
TCHECK_EQ(isoDateCompare({ 2021, 1, 1 }, { 2020, 12, 31 }), 1, "isoDateCompare: later year");
TCHECK_EQ(isoDateCompare({ 2020, 6, 1 }, { 2020, 7, 1 }), -1, "isoDateCompare: earlier month");
}
static void testDiffISODate()
{
// temporal_rs: plain_date.rs test simple_date_until
// 1969-07-24 until 1969-10-05 in days = 73
auto r1 = diffISODate({ 1969, 7, 24 }, { 1969, 10, 5 }, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(r1.days()), 73LL, "diffISODate: 73 days");
// 1969-07-24 until 1996-03-03 in days = 9719
auto r2 = diffISODate({ 1969, 7, 24 }, { 1996, 3, 3 }, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(r2.days()), 9719LL, "diffISODate: 9719 days");
// 1969-07-24 until 1969-10-05 in months = 2m12d
auto r3 = diffISODate({ 1969, 7, 24 }, { 1969, 10, 5 }, TemporalUnit::Month);
TCHECK_EQ(static_cast<int64_t>(r3.months()), 2LL, "diffISODate: months");
TCHECK_EQ(static_cast<int64_t>(r3.days()), 11LL, "diffISODate: remaining days");
// Same date -> zero
auto r4 = diffISODate({ 2020, 6, 15 }, { 2020, 6, 15 }, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(r4.days()), 0LL, "diffISODate: same date");
// Negative diff: 1969-10-05 until 1969-07-24 in days = -73
auto r5 = diffISODate({ 1969, 10, 5 }, { 1969, 7, 24 }, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(r5.days()), -73LL, "diffISODate: negative days");
}
// ---------------------------------------------------------------------------
// Rounding tests — mirrors temporal_rs src/rounding.rs tests
// ---------------------------------------------------------------------------
static void testRoundNumberToIncrementDouble()
{
// temporal_rs: round_number_to_increment tests
// 5.5 with increment 1, HalfExpand -> 6
TCHECK_EQ(roundNumberToIncrementDouble(5.5, 1.0, RoundingMode::HalfExpand), 6.0, "round: 5.5 HalfExpand");
// 5.5 with increment 1, HalfTrunc -> 5
TCHECK_EQ(roundNumberToIncrementDouble(5.5, 1.0, RoundingMode::HalfTrunc), 5.0, "round: 5.5 HalfTrunc");
// 5.5 with increment 1, HalfEven -> 6 (round to even)
TCHECK_EQ(roundNumberToIncrementDouble(5.5, 1.0, RoundingMode::HalfEven), 6.0, "round: 5.5 HalfEven");
// 4.5 with increment 1, HalfEven -> 4 (round to even)
TCHECK_EQ(roundNumberToIncrementDouble(4.5, 1.0, RoundingMode::HalfEven), 4.0, "round: 4.5 HalfEven");
// Increment > 1: 23 / increment 5, Trunc -> 20
TCHECK_EQ(roundNumberToIncrementDouble(23.0, 5.0, RoundingMode::Trunc), 20.0, "round: 23 inc5 Trunc");
// 23 / increment 5, Ceil -> 25
TCHECK_EQ(roundNumberToIncrementDouble(23.0, 5.0, RoundingMode::Ceil), 25.0, "round: 23 inc5 Ceil");
// 23 / increment 5, Floor -> 20
TCHECK_EQ(roundNumberToIncrementDouble(23.0, 5.0, RoundingMode::Floor), 20.0, "round: 23 inc5 Floor");
// Negative: -23 / increment 5, Trunc -> -20
TCHECK_EQ(roundNumberToIncrementDouble(-23.0, 5.0, RoundingMode::Trunc), -20.0, "round: -23 inc5 Trunc");
// -23 / increment 5, Floor -> -25
TCHECK_EQ(roundNumberToIncrementDouble(-23.0, 5.0, RoundingMode::Floor), -25.0, "round: -23 inc5 Floor");
}
static void testRoundNumberToIncrementInt128()
{
// temporal_rs: round_number_to_increment (integer path)
// 5 / inc 2, HalfExpand -> 6
TCHECK_EQ(roundNumberToIncrementInt128(Int128(5), Int128(2), RoundingMode::HalfExpand), Int128(6), "roundInt128: 5 inc2 HalfExpand");
// 5 / inc 2, HalfTrunc -> 4
TCHECK_EQ(roundNumberToIncrementInt128(Int128(5), Int128(2), RoundingMode::HalfTrunc), Int128(4), "roundInt128: 5 inc2 HalfTrunc");
// 4 / inc 2, HalfEven -> 4
TCHECK_EQ(roundNumberToIncrementInt128(Int128(4), Int128(2), RoundingMode::HalfEven), Int128(4), "roundInt128: 4 inc2 HalfEven");
// 6 / inc 2, HalfEven -> 6
TCHECK_EQ(roundNumberToIncrementInt128(Int128(6), Int128(2), RoundingMode::HalfEven), Int128(6), "roundInt128: 6 inc2 HalfEven");
// Nanoseconds: 1500000000 / inc 1000000000 (1s), HalfExpand -> 2000000000
Int128 ns = Int128(1500000000LL);
Int128 inc = Int128(1000000000LL);
TCHECK_EQ(roundNumberToIncrementInt128(ns, inc, RoundingMode::HalfExpand), Int128(2000000000LL), "roundInt128: 1.5s HalfExpand");
// temporal_rs: duration rounding 25h with inc=1day (86400s = 86400000000000ns)
// 25h in ns = 90000000000000, inc = 86400000000000, Trunc -> 86400000000000
Int128 h25 = Int128(90000000000000LL);
Int128 day = Int128(86400000000000LL);
TCHECK_EQ(roundNumberToIncrementInt128(h25, day, RoundingMode::Trunc), day, "roundInt128: 25h Trunc to 1day");
// Floor same result
TCHECK_EQ(roundNumberToIncrementInt128(h25, day, RoundingMode::Floor), day, "roundInt128: 25h Floor to 1day");
}
static void testMaximumRoundingIncrement()
{
// temporal_rs: maximum_temporal_duration_rounding_increment
TCHECK_TRUE(!maximumRoundingIncrement(TemporalUnit::Year).has_value(), "maxIncrement: Year = unlimited");
TCHECK_TRUE(!maximumRoundingIncrement(TemporalUnit::Month).has_value(), "maxIncrement: Month = unlimited");
TCHECK_TRUE(!maximumRoundingIncrement(TemporalUnit::Week).has_value(), "maxIncrement: Week = unlimited");
TCHECK_TRUE(!maximumRoundingIncrement(TemporalUnit::Day).has_value(), "maxIncrement: Day = unlimited");
TCHECK_EQ(maximumRoundingIncrement(TemporalUnit::Hour).value_or(0), 24u, "maxIncrement: Hour = 24");
TCHECK_EQ(maximumRoundingIncrement(TemporalUnit::Minute).value_or(0), 60u, "maxIncrement: Minute = 60");
TCHECK_EQ(maximumRoundingIncrement(TemporalUnit::Second).value_or(0), 60u, "maxIncrement: Second = 60");
TCHECK_EQ(maximumRoundingIncrement(TemporalUnit::Millisecond).value_or(0), 1000u, "maxIncrement: Millisecond = 1000");
TCHECK_EQ(maximumRoundingIncrement(TemporalUnit::Microsecond).value_or(0), 1000u, "maxIncrement: Microsecond = 1000");
TCHECK_EQ(maximumRoundingIncrement(TemporalUnit::Nanosecond).value_or(0), 1000u, "maxIncrement: Nanosecond = 1000");
}
// ---------------------------------------------------------------------------
// DurationArithmetic tests — mirrors temporal_rs src/builtins/core/duration.rs
// ---------------------------------------------------------------------------
static void testTimeDurationFromComponents()
{
// temporal_rs: TimeDuration::from_components
// 1h = 3600000000000 ns
Int128 h1 = timeDurationFromComponents(1, 0, 0, 0, 0, 0);
TCHECK_EQ(h1, Int128(3600000000000LL), "timeDuration: 1h");
// 1m = 60000000000 ns
Int128 m1 = timeDurationFromComponents(0, 1, 0, 0, 0, 0);
TCHECK_EQ(m1, Int128(60000000000LL), "timeDuration: 1m");
// 1s = 1000000000 ns
Int128 s1 = timeDurationFromComponents(0, 0, 1, 0, 0, 0);
TCHECK_EQ(s1, Int128(1000000000LL), "timeDuration: 1s");
// 1ms = 1000000 ns
Int128 ms1 = timeDurationFromComponents(0, 0, 0, 1, 0, 0);
TCHECK_EQ(ms1, Int128(1000000LL), "timeDuration: 1ms");
// 1µs = 1000 ns
Int128 us1 = timeDurationFromComponents(0, 0, 0, 0, 1, 0);
TCHECK_EQ(us1, Int128(1000LL), "timeDuration: 1µs");
// 1ns
Int128 ns1 = timeDurationFromComponents(0, 0, 0, 0, 0, 1);
TCHECK_EQ(ns1, Int128(1LL), "timeDuration: 1ns");
// Combined: 1h2m3s = 3723000000000 ns
Int128 combined = timeDurationFromComponents(1, 2, 3, 0, 0, 0);
TCHECK_EQ(combined, Int128(3723000000000LL), "timeDuration: 1h2m3s");
// 25h = 90000000000000 ns
Int128 h25 = timeDurationFromComponents(25, 0, 0, 0, 0, 0);
TCHECK_EQ(h25, Int128(90000000000000LL), "timeDuration: 25h");
}
static void testDurationSign()
{
// temporal_rs: Duration::sign
ISO8601::Duration pos(1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
TCHECK_EQ(durationSign(pos), 1, "durationSign: positive");
ISO8601::Duration neg(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
TCHECK_EQ(durationSign(neg), -1, "durationSign: negative");
ISO8601::Duration zero;
TCHECK_EQ(durationSign(zero), 0, "durationSign: zero");
// Mixed field signs -> should not occur in valid durations, but sign uses first nonzero
ISO8601::Duration negHours(0, 0, 0, 0, -5, 0, 0, 0, 0, 0);
TCHECK_EQ(durationSign(negHours), -1, "durationSign: negative hours");
}
static void testLargestSubduration()
{
// temporal_rs: Duration::default_largest_unit
ISO8601::Duration d1(1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
TCHECK_EQ(largestSubduration(d1), TemporalUnit::Year, "largestSub: years");
ISO8601::Duration d2(0, 2, 0, 0, 0, 0, 0, 0, 0, 0);
TCHECK_EQ(largestSubduration(d2), TemporalUnit::Month, "largestSub: months");
ISO8601::Duration d3(0, 0, 0, 0, 3, 0, 0, 0, 0, 0);
TCHECK_EQ(largestSubduration(d3), TemporalUnit::Hour, "largestSub: hours");
ISO8601::Duration d4(0, 0, 0, 0, 0, 0, 0, 0, 0, 5);
TCHECK_EQ(largestSubduration(d4), TemporalUnit::Nanosecond, "largestSub: nanoseconds");
// Zero duration -> Nanosecond (first nonzero not found, returns last)
ISO8601::Duration d5;
TCHECK_EQ(largestSubduration(d5), TemporalUnit::Nanosecond, "largestSub: zero");
}
static void testAdjustDateDurationRecord()
{
// temporal_rs: AdjustDateDurationRecord
ISO8601::Duration base(2, 3, 1, 5, 0, 0, 0, 0, 0, 0);
// Override days only
auto r1 = adjustDateDurationRecord(base, 10.0, std::nullopt, std::nullopt);
TCHECK_TRUE(r1.has_value(), "adjustDateDur: days override ok");
TCHECK_EQ(static_cast<int64_t>(r1->years()), 2LL, "adjustDateDur: years preserved");
TCHECK_EQ(static_cast<int64_t>(r1->months()), 3LL, "adjustDateDur: months preserved");
TCHECK_EQ(static_cast<int64_t>(r1->days()), 10LL, "adjustDateDur: days overridden");
// Override weeks
auto r2 = adjustDateDurationRecord(base, 5.0, 2.0, std::nullopt);
TCHECK_TRUE(r2.has_value(), "adjustDateDur: weeks override ok");
TCHECK_EQ(static_cast<int64_t>(r2->weeks()), 2LL, "adjustDateDur: weeks overridden");
// Mixed signs -> invalid, should error
auto r3 = adjustDateDurationRecord(base, -10.0, std::nullopt, std::nullopt);
TCHECK_TRUE(!r3.has_value(), "adjustDateDur: mixed sign rejected");
}
// ---------------------------------------------------------------------------
// CalendarArithmetic tests — mirrors temporal_rs src/builtins/core/calendar.rs
// ---------------------------------------------------------------------------
static void testCalendarDateAdd()
{
// ISO8601 path only (no ICU needed)
// temporal_rs: Calendar::date_add (iso8601)
// 1976-11-18 + P43Y = 2019-11-18
auto r1 = calendarDateAdd({ 1976, 11, 18 }, ISO8601::Duration(43, 0, 0, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r1.has_value(), "calendarDateAdd: +43y ok");
TCHECK_EQ(r1->year(), 2019, "calendarDateAdd: +43y year");
// 1976-11-18 + P-43Y = 1933-11-18
auto r2 = calendarDateAdd({ 1976, 11, 18 }, ISO8601::Duration(-43, 0, 0, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r2.has_value(), "calendarDateAdd: -43y ok");
TCHECK_EQ(r2->year(), 1933, "calendarDateAdd: -43y year");
// End-of-month constrain: 2020-01-31 + P1M = 2020-02-29 (leap year)
auto r3 = calendarDateAdd({ 2020, 1, 31 }, ISO8601::Duration(0, 1, 0, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r3.has_value(), "calendarDateAdd: eom constrain ok");
TCHECK_EQ(r3->month(), 2u, "calendarDateAdd: eom month");
TCHECK_EQ(r3->day(), 29u, "calendarDateAdd: eom day");
// Weeks: 1976-11-18 + P2W = 1976-12-02
auto r4 = calendarDateAdd({ 1976, 11, 18 }, ISO8601::Duration(0, 0, 2, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r4.has_value(), "calendarDateAdd: +2w ok");
TCHECK_EQ(r4->month(), 12u, "calendarDateAdd: +2w month");
TCHECK_EQ(r4->day(), 2u, "calendarDateAdd: +2w day");
}
static void testCalendarDateUntil()
{
// ISO8601 path only — mirrors temporal_rs: Calendar::date_until + date_until_largest_year
// 1969-07-24 until 1996-03-03 in days = 9719
auto r1 = calendarDateUntil({ 1969, 7, 24 }, { 1996, 3, 3 }, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(r1.days()), 9719LL, "calendarDateUntil: 9719 days");
// Same date -> zero
auto r2 = calendarDateUntil({ 2020, 6, 15 }, { 2020, 6, 15 }, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(r2.days()), 0LL, "calendarDateUntil: same date");
// 1969-07-24 until 1969-10-05 in months = 2m11d
auto r3 = calendarDateUntil({ 1969, 7, 24 }, { 1969, 10, 5 }, TemporalUnit::Month);
TCHECK_EQ(static_cast<int64_t>(r3.months()), 2LL, "calendarDateUntil: 2 months");
// Negative: later until earlier
auto r4 = calendarDateUntil({ 1996, 3, 3 }, { 1969, 7, 24 }, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(r4.days()), -9719LL, "calendarDateUntil: -9719 days");
// temporal_rs: date_until_largest_year — full ISO8601 table
// Each entry: (one, two, (years, months, weeks, days))
struct DateUntilCase {
ISO8601::PlainDate one;
ISO8601::PlainDate two;
int64_t years, months, days;
};
static const DateUntilCase cases[] = {
{ { 2021, 7, 16 }, { 2021, 7, 16 }, 0, 0, 0 }, // same
{ { 2021, 7, 16 }, { 2021, 7, 17 }, 0, 0, 1 }, // +1d
{ { 2021, 7, 16 }, { 2021, 7, 23 }, 0, 0, 7 }, // +7d
{ { 2021, 7, 16 }, { 2021, 8, 16 }, 0, 1, 0 }, // +1m
{ { 2020, 12, 16 }, { 2021, 1, 16 }, 0, 1, 0 }, // month wrap
{ { 2021, 1, 5 }, { 2021, 2, 5 }, 0, 1, 0 }, // +1m
{ { 2021, 1, 7 }, { 2021, 3, 7 }, 0, 2, 0 }, // +2m
{ { 2021, 7, 16 }, { 2021, 8, 17 }, 0, 1, 1 }, // +1m1d
{ { 2021, 7, 16 }, { 2021, 8, 13 }, 0, 0, 28 }, // sub-month days
{ { 2021, 7, 16 }, { 2021, 9, 16 }, 0, 2, 0 }, // +2m
{ { 2021, 7, 16 }, { 2022, 7, 16 }, 1, 0, 0 }, // exactly 1y
{ { 2021, 7, 16 }, { 2031, 7, 16 }, 10, 0, 0 }, // exactly 10y
{ { 2021, 7, 16 }, { 2022, 7, 19 }, 1, 0, 3 }, // 1y3d
{ { 2021, 7, 16 }, { 2022, 9, 19 }, 1, 2, 3 }, // 1y2m3d
{ { 2021, 7, 16 }, { 2031, 12, 16 }, 10, 5, 0 }, // 10y5m
{ { 1997, 12, 16 }, { 2021, 7, 16 }, 23, 7, 0 }, // large: 23y7m
{ { 1997, 7, 16 }, { 2021, 7, 16 }, 24, 0, 0 }, // large: 24y
{ { 1997, 7, 16 }, { 2021, 7, 15 }, 23, 11, 29 }, // just under 24y
{ { 1997, 6, 16 }, { 2021, 6, 15 }, 23, 11, 30 }, // just under 24y
{ { 1960, 2, 16 }, { 2020, 3, 16 }, 60, 1, 0 }, // 60y1m
{ { 1960, 2, 16 }, { 2021, 3, 15 }, 61, 0, 27 }, // 61y27d
{ { 1960, 2, 16 }, { 2020, 3, 15 }, 60, 0, 28 }, // 60y28d
{ { 2021, 3, 30 }, { 2021, 7, 16 }, 0, 3, 16 }, // 3m16d
{ { 2020, 3, 30 }, { 2021, 7, 16 }, 1, 3, 16 }, // 1y3m16d
{ { 1960, 3, 30 }, { 2021, 7, 16 }, 61, 3, 16 }, // 61y3m16d
{ { 2019, 12, 30 }, { 2021, 7, 16 }, 1, 6, 16 }, // 1y6m16d
{ { 2020, 12, 30 }, { 2021, 7, 16 }, 0, 6, 16 }, // 6m16d
{ { 1997, 12, 30 }, { 2021, 7, 16 }, 23, 6, 16 }, // 23y6m16d
{ { 1, 12, 25 }, { 2021, 7, 16 }, 2019, 6, 21 }, // ancient date
{ { 2019, 12, 30 }, { 2021, 3, 5 }, 1, 2, 5 }, // 1y2m5d
// Negative cases
{ { 2021, 7, 17 }, { 2021, 7, 16 }, 0, 0, -1 },
{ { 2021, 7, 23 }, { 2021, 7, 16 }, 0, 0, -7 },
{ { 2021, 8, 16 }, { 2021, 7, 16 }, 0, -1, 0 },
{ { 2021, 1, 16 }, { 2020, 12, 16 }, 0, -1, 0 },
{ { 2021, 2, 5 }, { 2021, 1, 5 }, 0, -1, 0 },
{ { 2021, 3, 7 }, { 2021, 1, 7 }, 0, -2, 0 },
{ { 2021, 8, 17 }, { 2021, 7, 16 }, 0, -1, -1 },
};
for (auto& c : cases) {
auto r = calendarDateUntil(c.one, c.two, TemporalUnit::Year);
TCHECK_EQ(static_cast<int64_t>(r.years()), c.years, "dateUntilLargestYear: years");
TCHECK_EQ(static_cast<int64_t>(r.months()), c.months, "dateUntilLargestYear: months");
TCHECK_EQ(static_cast<int64_t>(r.days()), c.days, "dateUntilLargestYear: days");
}
}
// ---------------------------------------------------------------------------
// ISO date limit tests — mirrors temporal_rs src/iso.rs limit tests
// ---------------------------------------------------------------------------
static void testISODateLimits()
{
// Max valid ISO date for Temporal: +275760-09-13 (±1e8 epoch days)
// balanceISODate returns outOfRangeYear only when year exceeds the year representation limit
auto rMax = balanceISODate(275760, 9, 13);
TCHECK_EQ(rMax.year(), 275760, "limits: max date year");
TCHECK_EQ(rMax.month(), 9u, "limits: max date month");
TCHECK_EQ(rMax.day(), 13u, "limits: max date day");
// Min valid ISO date: -271821-04-19
auto rMin = balanceISODate(-271821, 4, 19);
TCHECK_EQ(rMin.year(), -271821, "limits: min date year");
TCHECK_EQ(rMin.month(), 4u, "limits: min date month");
TCHECK_EQ(rMin.day(), 19u, "limits: min date day");
// Unix epoch: 1970-01-01
auto rEpoch = balanceISODate(1970, 1, 1);
TCHECK_EQ(rEpoch.year(), 1970, "limits: epoch year");
TCHECK_EQ(rEpoch.month(), 1u, "limits: epoch month");
TCHECK_EQ(rEpoch.day(), 1u, "limits: epoch day");
// Day before epoch: 1969-12-31
auto rEpochMinus1 = balanceISODate(1969, 12, 31);
TCHECK_EQ(rEpochMinus1.year(), 1969, "limits: epoch-1 year");
TCHECK_EQ(rEpochMinus1.month(), 12u, "limits: epoch-1 month");
TCHECK_EQ(rEpochMinus1.day(), 31u, "limits: epoch-1 day");
// isoDateAdd past Temporal max -> error (±1e8 days limit)
// Max date + P1D exceeds the epoch day limit
auto rOverMax = isoDateAdd({ 275760, 9, 13 }, ISO8601::Duration(0, 0, 0, 1, 0, 0, 0, 0, 0, 0), TemporalOverflow::Reject);
TCHECK_TRUE(!rOverMax.has_value(), "limits: isoDateAdd max+1d rejects");
// Min date - P1D exceeds minimum
auto rUnderMin = isoDateAdd({ -271821, 4, 19 }, ISO8601::Duration(0, 0, 0, -1, 0, 0, 0, 0, 0, 0), TemporalOverflow::Reject);
TCHECK_TRUE(!rUnderMin.has_value(), "limits: isoDateAdd min-1d rejects");
// regulateISODate validates month/day range (not Temporal epoch limits)
auto rRegValid = regulateISODate(275760, 9, 13, TemporalOverflow::Reject);
TCHECK_TRUE(rRegValid.has_value(), "limits: regulate 275760-09-13 ok");
auto rRegBadMonth = regulateISODate(2020, 13, 1, TemporalOverflow::Reject);
TCHECK_TRUE(!rRegBadMonth.has_value(), "limits: regulate month 13 rejects");
}
// ---------------------------------------------------------------------------
// Negative number rounding — mirrors temporal_rs src/rounding.rs tests
// ---------------------------------------------------------------------------
static void testNegativeRounding()
{
// temporal_rs: test_basic_rounding_cases (negative values)
// -101 / 10
TCHECK_EQ(roundNumberToIncrementDouble(-101.0, 10.0, RoundingMode::Ceil), -100.0, "negRound: -101 inc10 Ceil");
TCHECK_EQ(roundNumberToIncrementDouble(-101.0, 10.0, RoundingMode::Floor), -110.0, "negRound: -101 inc10 Floor");
TCHECK_EQ(roundNumberToIncrementDouble(-101.0, 10.0, RoundingMode::Expand), -110.0, "negRound: -101 inc10 Expand");
TCHECK_EQ(roundNumberToIncrementDouble(-101.0, 10.0, RoundingMode::Trunc), -100.0, "negRound: -101 inc10 Trunc");
// -105 / 10 (midpoint)
TCHECK_EQ(roundNumberToIncrementDouble(-105.0, 10.0, RoundingMode::HalfCeil), -100.0, "negRound: -105 HalfCeil");
TCHECK_EQ(roundNumberToIncrementDouble(-105.0, 10.0, RoundingMode::HalfFloor), -110.0, "negRound: -105 HalfFloor");
TCHECK_EQ(roundNumberToIncrementDouble(-105.0, 10.0, RoundingMode::HalfExpand), -110.0, "negRound: -105 HalfExpand");
TCHECK_EQ(roundNumberToIncrementDouble(-105.0, 10.0, RoundingMode::HalfTrunc), -100.0, "negRound: -105 HalfTrunc");
TCHECK_EQ(roundNumberToIncrementDouble(-105.0, 10.0, RoundingMode::HalfEven), -100.0, "negRound: -105 HalfEven (even=10)");
// -115 / 10: HalfEven -> -120 (even=12)
TCHECK_EQ(roundNumberToIncrementDouble(-115.0, 10.0, RoundingMode::HalfEven), -120.0, "negRound: -115 HalfEven (even=12)");
// -107 / 10 (not midpoint)
TCHECK_EQ(roundNumberToIncrementDouble(-107.0, 10.0, RoundingMode::Ceil), -100.0, "negRound: -107 Ceil");
TCHECK_EQ(roundNumberToIncrementDouble(-107.0, 10.0, RoundingMode::Floor), -110.0, "negRound: -107 Floor");
// Int128 negative rounding
TCHECK_EQ(roundNumberToIncrementInt128(Int128(-101), Int128(10), RoundingMode::Ceil), Int128(-100), "negRoundI128: Ceil");
TCHECK_EQ(roundNumberToIncrementInt128(Int128(-101), Int128(10), RoundingMode::Floor), Int128(-110), "negRoundI128: Floor");
TCHECK_EQ(roundNumberToIncrementInt128(Int128(-105), Int128(10), RoundingMode::HalfExpand), Int128(-110), "negRoundI128: HalfExpand midpoint");
TCHECK_EQ(roundNumberToIncrementInt128(Int128(-105), Int128(10), RoundingMode::HalfTrunc), Int128(-100), "negRoundI128: HalfTrunc midpoint");
}
// ---------------------------------------------------------------------------
// roundNumberToIncrementAsIfPositive — mirrors temporal_rs round_as_if_positive
// ---------------------------------------------------------------------------
static void testRoundAsIfPositive()
{
// roundNumberToIncrementAsIfPositive treats negative x as positive for rounding direction.
// x=-107 inc=10: Trunc→-110 (toward -∞ when treated positive), Expand→-100.
TCHECK_EQ(roundNumberToIncrementAsIfPositive(Int128(-107), Int128(10), RoundingMode::Trunc), Int128(-110), "asIfPos: -107 Trunc=-110");
TCHECK_EQ(roundNumberToIncrementAsIfPositive(Int128(-107), Int128(10), RoundingMode::Expand), Int128(-100), "asIfPos: -107 Expand=-100");
TCHECK_EQ(roundNumberToIncrementAsIfPositive(Int128(-107), Int128(10), RoundingMode::Ceil), Int128(-100), "asIfPos: -107 Ceil=-100");
TCHECK_EQ(roundNumberToIncrementAsIfPositive(Int128(-107), Int128(10), RoundingMode::Floor), Int128(-110), "asIfPos: -107 Floor=-110");
// Positive values: same as regular roundNumberToIncrementInt128
TCHECK_EQ(roundNumberToIncrementAsIfPositive(Int128(107), Int128(10), RoundingMode::Trunc), Int128(100), "asIfPos: 107 Trunc=100");
TCHECK_EQ(roundNumberToIncrementAsIfPositive(Int128(107), Int128(10), RoundingMode::Expand), Int128(110), "asIfPos: 107 Expand=110");
// Zero is unchanged
TCHECK_EQ(roundNumberToIncrementAsIfPositive(Int128(0), Int128(10), RoundingMode::HalfExpand), Int128(0), "asIfPos: 0 = 0");
}
// ---------------------------------------------------------------------------
// negateTemporalRoundingMode — mirrors temporal_rs RoundingMode::negate
// ---------------------------------------------------------------------------
static void testNegateRoundingMode()
{
// temporal_rs: RoundingMode::negate
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::Floor)), static_cast<int>(RoundingMode::Ceil), "negate: Floor->Ceil");
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::Ceil)), static_cast<int>(RoundingMode::Floor), "negate: Ceil->Floor");
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::HalfFloor)), static_cast<int>(RoundingMode::HalfCeil), "negate: HalfFloor->HalfCeil");
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::HalfCeil)), static_cast<int>(RoundingMode::HalfFloor), "negate: HalfCeil->HalfFloor");
// Symmetric modes unchanged
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::Trunc)), static_cast<int>(RoundingMode::Trunc), "negate: Trunc unchanged");
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::Expand)), static_cast<int>(RoundingMode::Expand), "negate: Expand unchanged");
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::HalfExpand)), static_cast<int>(RoundingMode::HalfExpand), "negate: HalfExpand unchanged");
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::HalfTrunc)), static_cast<int>(RoundingMode::HalfTrunc), "negate: HalfTrunc unchanged");
TCHECK_EQ(static_cast<int>(negateTemporalRoundingMode(RoundingMode::HalfEven)), static_cast<int>(RoundingMode::HalfEven), "negate: HalfEven unchanged");
}
// ---------------------------------------------------------------------------
// Duration balancing — mirrors temporal_rs balance tests
// ---------------------------------------------------------------------------
static void testDurationBalancing()
{
// temporal_rs: balance_days_up_to_both_years_and_months
// 2020-01-01 + 11M = 2020-12-01, then + 396D = 2022-01-01
auto r = isoDateAdd({ 2020, 1, 1 }, ISO8601::Duration(0, 11, 0, 396, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(r.has_value(), "balance: 11m+396d from 2020-01-01 ok");
TCHECK_EQ(r->year(), 2022, "balance: 11m+396d year=2022");
TCHECK_EQ(r->month(), 1u, "balance: 11m+396d month=1");
// temporal_rs: negative balance
// -60h = -3d (using timeDurationFromComponents)
Int128 neg60h = timeDurationFromComponents(-60, 0, 0, 0, 0, 0);
TCHECK_EQ(neg60h, Int128(-216000000000000LL), "balance: -60h in ns");
// Subsecond balancing: -999ms + -999999µs + -999999999ns = -2.998998999s
Int128 negMs = timeDurationFromComponents(0, 0, 0, -999, -999999, -999999999);
// Total = -999*1e6 - 999999*1e3 - 999999999 = -999000000 - 999999000 - 999999999 = -2998998999 ns
TCHECK_EQ(negMs, Int128(-2998998999LL), "balance: -999ms-999999µs-999999999ns");
}
// ---------------------------------------------------------------------------
// isoDateAdd boundary/error cases
// ---------------------------------------------------------------------------
static void testISODateAddBoundaries()
{
// temporal_rs: date_add_limits — adding to max date
// Max date + P1D -> error (out of range)
auto rOverMax = isoDateAdd({ 275760, 9, 13 }, ISO8601::Duration(0, 0, 0, 1, 0, 0, 0, 0, 0, 0), TemporalOverflow::Reject);
TCHECK_TRUE(!rOverMax.has_value(), "addBounds: max+1d rejects");
// Min date - P1D -> error
auto rUnderMin = isoDateAdd({ -271821, 4, 19 }, ISO8601::Duration(0, 0, 0, -1, 0, 0, 0, 0, 0, 0), TemporalOverflow::Reject);
TCHECK_TRUE(!rUnderMin.has_value(), "addBounds: min-1d rejects");
// Max date itself is valid
auto rMax = isoDateAdd({ 275760, 9, 12 }, ISO8601::Duration(0, 0, 0, 1, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
TCHECK_TRUE(rMax.has_value(), "addBounds: max date ok");
TCHECK_EQ(rMax->year(), 275760, "addBounds: max year");
TCHECK_EQ(rMax->day(), 13u, "addBounds: max day");
// Constrain: exceed max -> constrain clamps
auto rConstrain = isoDateAdd({ 275760, 9, 13 }, ISO8601::Duration(0, 1, 0, 0, 0, 0, 0, 0, 0, 0), TemporalOverflow::Constrain);
// +1M from 275760-09-13 would be 275760-10-13 which exceeds max -> out of range
TCHECK_TRUE(!rConstrain.has_value(), "addBounds: exceed max+1M errors");
}
// ---------------------------------------------------------------------------
// New tests — ISOArithmetic, DurationArithmetic, Rounding, Instant, Calendar,
// TimeZone, PlainDateTime
// ---------------------------------------------------------------------------
static void testBalanceISOYearMonth()
{
// Month overflow: 2021-13 -> 2022-01
auto r1 = balanceISOYearMonth(2021, 13);
TCHECK_EQ(r1.year(), 2022, "balanceYM: 2021m13 year");
TCHECK_EQ(r1.month(), 1u, "balanceYM: 2021m13 month");
// Month underflow: 2021-00 -> 2020-12
auto r2 = balanceISOYearMonth(2021, 0);
TCHECK_EQ(r2.year(), 2020, "balanceYM: 2021m0 year");
TCHECK_EQ(r2.month(), 12u, "balanceYM: 2021m0 month");
// No-op: 2020-06 -> 2020-06
auto r3 = balanceISOYearMonth(2020, 6);
TCHECK_EQ(r3.year(), 2020, "balanceYM: identity year");
TCHECK_EQ(r3.month(), 6u, "balanceYM: identity month");
// Large overflow: 2021-25 -> 2023-01
auto r4 = balanceISOYearMonth(2021, 25);
TCHECK_EQ(r4.year(), 2023, "balanceYM: 2021m25 year");
TCHECK_EQ(r4.month(), 1u, "balanceYM: 2021m25 month");
}
static void testISOTimeCompare()
{
// Equal
TCHECK_EQ(isoTimeCompare({ 12, 0, 0, 0, 0, 0 }, { 12, 0, 0, 0, 0, 0 }), 0, "timeCompare: equal");
// Hour difference
TCHECK_EQ(isoTimeCompare({ 14, 0, 0, 0, 0, 0 }, { 12, 0, 0, 0, 0, 0 }), 1, "timeCompare: later hour");
TCHECK_EQ(isoTimeCompare({ 12, 0, 0, 0, 0, 0 }, { 14, 0, 0, 0, 0, 0 }), -1, "timeCompare: earlier hour");
// Nanosecond difference
ISO8601::PlainTime t1(0, 0, 0, 0, 0, 1), t2(0, 0, 0, 0, 0, 0);
TCHECK_EQ(isoTimeCompare(t1, t2), 1, "timeCompare: 1ns later");
TCHECK_EQ(isoTimeCompare(t2, t1), -1, "timeCompare: 1ns earlier");
// Max time vs min time
ISO8601::PlainTime maxT(23, 59, 59, 999, 999, 999), minT(0, 0, 0, 0, 0, 0);
TCHECK_EQ(isoTimeCompare(maxT, minT), 1, "timeCompare: max > min");
}
static void testApplyUnsignedRoundingMode()
{
// x between r1 and r2 — direction modes
TCHECK_EQ(applyUnsignedRoundingMode(1.3, 1.0, 2.0, UnsignedRoundingMode::Zero), 1.0, "applyURM: 1.3 Zero");
TCHECK_EQ(applyUnsignedRoundingMode(1.3, 1.0, 2.0, UnsignedRoundingMode::Infinity), 2.0, "applyURM: 1.3 Inf");
// x == r1 (exact lower bound)
TCHECK_EQ(applyUnsignedRoundingMode(1.0, 1.0, 2.0, UnsignedRoundingMode::Zero), 1.0, "applyURM: exact=r1");
// HalfZero at midpoint
TCHECK_EQ(applyUnsignedRoundingMode(1.5, 1.0, 2.0, UnsignedRoundingMode::HalfZero), 1.0, "applyURM: 1.5 HalfZero");
TCHECK_EQ(applyUnsignedRoundingMode(1.5, 1.0, 2.0, UnsignedRoundingMode::HalfInfinity), 2.0, "applyURM: 1.5 HalfInf");
// HalfEven: 2.5 -> 2 (even lower), 3.5 -> 4 (even upper)
TCHECK_EQ(applyUnsignedRoundingMode(2.5, 2.0, 3.0, UnsignedRoundingMode::HalfEven), 2.0, "applyURM: 2.5 HalfEven->2");
TCHECK_EQ(applyUnsignedRoundingMode(3.5, 3.0, 4.0, UnsignedRoundingMode::HalfEven), 4.0, "applyURM: 3.5 HalfEven->4");
}
static void testNegateDuration()
{
ISO8601::Duration d(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
auto neg = negateDuration(d);
TCHECK_EQ(static_cast<int64_t>(neg.years()), -1LL, "negate: years");
TCHECK_EQ(static_cast<int64_t>(neg.months()), -2LL, "negate: months");
TCHECK_EQ(static_cast<int64_t>(neg.days()), -4LL, "negate: days");
TCHECK_EQ(static_cast<int64_t>(neg.hours()), -5LL, "negate: hours");
// Double negation = identity
auto back = negateDuration(neg);
TCHECK_EQ(static_cast<int64_t>(back.years()), 1LL, "negate: double neg years");
// Zero unchanged
ISO8601::Duration zero;
auto negZero = negateDuration(zero);
TCHECK_EQ(durationSign(negZero), 0, "negate: zero");
}
static void testAbsDuration()
{
// Positive unchanged
ISO8601::Duration pos(1, 2, 0, 4, 0, 0, 0, 0, 0, 0);
auto absPos = absDuration(pos);
TCHECK_EQ(static_cast<int64_t>(absPos.years()), 1LL, "abs: pos years");
TCHECK_EQ(static_cast<int64_t>(absPos.days()), 4LL, "abs: pos days");
// Negative -> positive
ISO8601::Duration neg(-1, -2, 0, -4, 0, 0, 0, 0, 0, 0);
auto absNeg = absDuration(neg);
TCHECK_EQ(static_cast<int64_t>(absNeg.years()), 1LL, "abs: neg years");
TCHECK_EQ(static_cast<int64_t>(absNeg.months()), 2LL, "abs: neg months");
TCHECK_EQ(static_cast<int64_t>(absNeg.days()), 4LL, "abs: neg days");
TCHECK_EQ(durationSign(absNeg), 1, "abs: sign positive");
// Zero unchanged
ISO8601::Duration zero;
auto absZero = absDuration(zero);
TCHECK_EQ(durationSign(absZero), 0, "abs: zero");
}
static void testGetUTCEpochNanoseconds()
{
// Unix epoch = 0
auto r0 = getUTCEpochNanoseconds({ 1970, 1, 1 }, { 0, 0, 0, 0, 0, 0 });
TCHECK_EQ(r0, Int128(0LL), "utcEpoch: 1970-01-01 00:00:00 = 0");
// 1 day = 86400000000000 ns
auto r1 = getUTCEpochNanoseconds({ 1970, 1, 2 }, { 0, 0, 0, 0, 0, 0 });
TCHECK_EQ(r1, Int128(86400000000000LL), "utcEpoch: 1970-01-02 = 1 day");
// 1 second
auto r2 = getUTCEpochNanoseconds({ 1970, 1, 1 }, { 0, 0, 1, 0, 0, 0 });
TCHECK_EQ(r2, Int128(1000000000LL), "utcEpoch: 1970-01-01 00:00:01 = 1s");
// 2001-09-09T01:46:40Z = 1000000000 seconds = 1e18 ns
auto r3 = getUTCEpochNanoseconds({ 2001, 9, 9 }, { 1, 46, 40, 0, 0, 0 });
TCHECK_EQ(r3, Int128(1000000000LL) * Int128(1000000000LL), "utcEpoch: unix billion");
// Negative: 1969-12-31 = -1 day
auto r4 = getUTCEpochNanoseconds({ 1969, 12, 31 }, { 0, 0, 0, 0, 0, 0 });
TCHECK_EQ(r4, Int128(-86400000000000LL), "utcEpoch: 1969-12-31 = -1day");
}
static void testSplitTimeDuration()
{
// 25 hours = 90000000000000 ns -> 1 overflow day, 1h remainder
auto [days1, rem1] = splitTimeDuration(Int128(90000000000000LL));
TCHECK_EQ(days1, 1LL, "split: 25h = 1 overflow day");
TCHECK_EQ(rem1, Int128(3600000000000LL), "split: 25h remainder = 1h");
// Exact 1 day
auto [days2, rem2] = splitTimeDuration(Int128(86400000000000LL));
TCHECK_EQ(days2, 1LL, "split: 1day overflow");
TCHECK_EQ(rem2, Int128(0LL), "split: 1day remainder=0");
// Less than 1 day — no overflow
auto [days3, rem3] = splitTimeDuration(Int128(3600000000000LL));
TCHECK_EQ(days3, 0LL, "split: 1h no overflow");
TCHECK_EQ(rem3, Int128(3600000000000LL), "split: 1h remainder");
// Negative: -25h -> floor(-25/24) = -2, remainder = 23h = 82800000000000 ns
auto [days4, rem4] = splitTimeDuration(Int128(-90000000000000LL));
TCHECK_EQ(days4, -2LL, "split: -25h overflow=-2 (floor)");
TCHECK_EQ(rem4, Int128(82800000000000LL), "split: -25h remainder=23h");
}
static void testPlainTimeFromSubdayNs()
{
// 0 -> midnight
auto t0 = plainTimeFromSubdayNs(Int128(0));
TCHECK_EQ(t0.hour(), 0u, "ptFromNs: midnight hour");
TCHECK_EQ(t0.nanosecond(), 0u, "ptFromNs: midnight ns");
// 1 hour = 3600000000000 ns -> 01:00:00
auto t1 = plainTimeFromSubdayNs(Int128(3600000000000LL));
TCHECK_EQ(t1.hour(), 1u, "ptFromNs: 1h hour");
TCHECK_EQ(t1.minute(), 0u, "ptFromNs: 1h minute");
// 1 ns -> 00:00:00.000000001
auto t2 = plainTimeFromSubdayNs(Int128(1));
TCHECK_EQ(t2.nanosecond(), 1u, "ptFromNs: 1ns");
// 13:00:00 = 46800000000000 ns
auto t3 = plainTimeFromSubdayNs(Int128(46800000000000LL));
TCHECK_EQ(t3.hour(), 13u, "ptFromNs: 13h hour");
}
static void testAdd24HourDaysToTimeDuration()
{
// Add 1 day (86400000000000 ns) to 1h (3600000000000 ns) = 90000000000000 ns
auto r1 = add24HourDaysToTimeDuration(Int128(3600000000000LL), 1.0);
TCHECK_TRUE(r1.has_value(), "add24h: 1h+1d ok");
TCHECK_EQ(*r1, Int128(90000000000000LL), "add24h: 1h+1d = 25h");
// Add 0 days -> unchanged
auto r2 = add24HourDaysToTimeDuration(Int128(3600000000000LL), 0.0);
TCHECK_TRUE(r2.has_value(), "add24h: +0d ok");
TCHECK_EQ(*r2, Int128(3600000000000LL), "add24h: +0d unchanged");
// Negative days: 25h - 1d = 1h
auto r3 = add24HourDaysToTimeDuration(Int128(90000000000000LL), -1.0);
TCHECK_TRUE(r3.has_value(), "add24h: -1d ok");
TCHECK_EQ(*r3, Int128(3600000000000LL), "add24h: 25h-1d = 1h");
}
static void testTemporalDurationFromInternal()
{
// 4 days as time nanoseconds -> largestUnit=Day yields 4 days, 0 hours
Int128 fourDays = Int128(4LL) * Int128(86400000000000LL);
auto internal = ISO8601::InternalDuration::combineDateAndTimeDuration(ISO8601::Duration(), fourDays);
auto result = temporalDurationFromInternal(internal, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(result.days()), 4LL, "fromInternal: 4d days");
TCHECK_EQ(static_cast<int64_t>(result.hours()), 0LL, "fromInternal: 4d hours=0");
// largestUnit=Hour: 4 days = 96 hours, 0 days
auto result2 = temporalDurationFromInternal(internal, TemporalUnit::Hour);
TCHECK_EQ(static_cast<int64_t>(result2.hours()), 96LL, "fromInternal: 96h");
TCHECK_EQ(static_cast<int64_t>(result2.days()), 0LL, "fromInternal: 96h days=0");
}
static void testCompareISODateTime()
{
ISO8601::PlainDate d1 { 2019, 1, 8 }, d2 { 2021, 9, 7 };
ISO8601::PlainTime t1 { 8, 22, 36, 0, 0, 0 }, t2 { 12, 39, 40, 0, 0, 0 };
// Equal
TCHECK_EQ(compareISODateTime(d1, t1, d1, t1), 0, "compareIDT: equal");
// Different date — earlier vs later
TCHECK_EQ(compareISODateTime(d1, t1, d2, t2), -1, "compareIDT: earlier");
TCHECK_EQ(compareISODateTime(d2, t2, d1, t1), 1, "compareIDT: later");
// Same date, different time
ISO8601::PlainTime earlyT { 8, 22, 35, 0, 0, 0 };
TCHECK_EQ(compareISODateTime(d1, earlyT, d1, t1), -1, "compareIDT: earlier time");
}
static void testMaximumInstantIncrement()
{
TCHECK_EQ(maximumInstantIncrement(TemporalUnit::Hour), 24.0, "maxInstInc: Hour=24");
TCHECK_EQ(maximumInstantIncrement(TemporalUnit::Minute), 1440.0, "maxInstInc: Minute=1440");
TCHECK_EQ(maximumInstantIncrement(TemporalUnit::Second), 86400.0, "maxInstInc: Second=86400");
TCHECK_EQ(maximumInstantIncrement(TemporalUnit::Millisecond), 8.64e7, "maxInstInc: Ms");
TCHECK_EQ(maximumInstantIncrement(TemporalUnit::Microsecond), 8.64e10, "maxInstInc: µs");
}
static void testToDateDurationRecordWithoutTime()
{
// Strip time fields, keep date fields
ISO8601::Duration d(1, 2, 0, 4, 5, 6, 7, 8, 9, 10);
auto r = toDateDurationRecordWithoutTime(d);
TCHECK_TRUE(r.has_value(), "stripTime: ok");
TCHECK_EQ(static_cast<int64_t>(r->years()), 1LL, "stripTime: years");
TCHECK_EQ(static_cast<int64_t>(r->months()), 2LL, "stripTime: months");
TCHECK_EQ(static_cast<int64_t>(r->days()), 4LL, "stripTime: days");
TCHECK_EQ(static_cast<int64_t>(r->hours()), 0LL, "stripTime: hours=0");
TCHECK_EQ(static_cast<int64_t>(r->minutes()), 0LL, "stripTime: minutes=0");
}
// ---------------------------------------------------------------------------
// totalSeconds / totalSubseconds — internal balance helpers
// ---------------------------------------------------------------------------
static void testTotalSecondsAndSubseconds()
{
// temporal_rs: internal balance helpers
// 1h30m = 5400s
ISO8601::Duration d1(0, 0, 0, 0, 1, 30, 0, 0, 0, 0);
TCHECK_EQ(totalSeconds(d1), 5400LL, "totalSec: 1h30m=5400s");
// 1d2h = 26*3600 = 93600s
ISO8601::Duration d2(0, 0, 0, 1, 2, 0, 0, 0, 0, 0);
TCHECK_EQ(totalSeconds(d2), 93600LL, "totalSec: 1d2h=93600s");
// 0 duration -> 0s
ISO8601::Duration z;
TCHECK_EQ(totalSeconds(z), 0LL, "totalSec: zero");
// 999ms + 999999µs + 999999999ns = 999*1e6 + 999999*1e3 + 999999999 = 2998998999 ns
ISO8601::Duration d3(0, 0, 0, 0, 0, 0, 0, 999, 999999, 999999999);
Int128 expected = Int128(2998998999LL);
TCHECK_EQ(totalSubseconds(d3), expected, "totalSub: max subseconds");
// 1s = 0 subseconds (only ms/µs/ns contribute)
ISO8601::Duration d4(0, 0, 0, 0, 0, 0, 1, 0, 0, 0);
TCHECK_EQ(totalSubseconds(d4), Int128(0LL), "totalSub: 1s=0 subseconds");
}
// ---------------------------------------------------------------------------
// totalTimeDuration — fractional unit conversion
// ---------------------------------------------------------------------------
static void testTotalTimeDuration()
{
// temporal_rs: internal nanosecond-to-unit conversion
// 3600000000000 ns = 1 hour
TCHECK_EQ(totalTimeDuration(Int128(3600000000000LL), TemporalUnit::Hour), 1.0, "totalTD: 1h");
// 86400000000000 ns = 1 day
TCHECK_EQ(totalTimeDuration(Int128(86400000000000LL), TemporalUnit::Day), 1.0, "totalTD: 1day");
// 1000000000 ns = 1 second
TCHECK_EQ(totalTimeDuration(Int128(1000000000LL), TemporalUnit::Second), 1.0, "totalTD: 1s");
// 90000000000000 ns (25h) in hours = 25.0
TCHECK_EQ(totalTimeDuration(Int128(90000000000000LL), TemporalUnit::Hour), 25.0, "totalTD: 25h");
// 1000000 ns = 1 ms
TCHECK_EQ(totalTimeDuration(Int128(1000000LL), TemporalUnit::Millisecond), 1.0, "totalTD: 1ms");
}
// ---------------------------------------------------------------------------
// balanceDuration — redistribute time fields
// ---------------------------------------------------------------------------
static void testBalanceDuration()
{
// temporal_rs: Duration::balance — redistributes seconds/minutes/hours
// 90min -> 1h30m when largestUnit=Hour
ISO8601::Duration d1(0, 0, 0, 0, 0, 90, 0, 0, 0, 0);
balanceDuration(d1, TemporalUnit::Hour);
TCHECK_EQ(static_cast<int64_t>(d1.hours()), 1LL, "balance: 90m -> 1h");
TCHECK_EQ(static_cast<int64_t>(d1.minutes()), 30LL, "balance: 90m -> 30m");
// 3600s -> 1h when largestUnit=Hour
ISO8601::Duration d2(0, 0, 0, 0, 0, 0, 3600, 0, 0, 0);
balanceDuration(d2, TemporalUnit::Hour);
TCHECK_EQ(static_cast<int64_t>(d2.hours()), 1LL, "balance: 3600s -> 1h");
TCHECK_EQ(static_cast<int64_t>(d2.seconds()), 0LL, "balance: 3600s -> 0s");
// 2000ms -> 2s when largestUnit=Second (ms overflow folds into seconds)
ISO8601::Duration d3(0, 0, 0, 0, 0, 0, 0, 2000, 0, 0);
balanceDuration(d3, TemporalUnit::Second);
TCHECK_EQ(static_cast<int64_t>(d3.seconds()), 2LL, "balance: 2000ms -> 2s");
TCHECK_EQ(static_cast<int64_t>(d3.milliseconds()), 0LL, "balance: 2000ms -> 0ms");
// 500ms with largestUnit=Millisecond -> unchanged
ISO8601::Duration d4(0, 0, 0, 0, 0, 0, 0, 500, 0, 0);
balanceDuration(d4, TemporalUnit::Millisecond);
TCHECK_EQ(static_cast<int64_t>(d4.milliseconds()), 500LL, "balance: 500ms unchanged");
}
// ---------------------------------------------------------------------------
// toInternalDuration / toInternalDurationRecordWith24HourDays
// ---------------------------------------------------------------------------
static void testToInternalDuration()
{
// temporal_rs: internal conversion helpers
// P1DT2H -> InternalDuration with time portion = 2h in ns, date = 1 day (NOT folded)
ISO8601::Duration d(0, 0, 0, 1, 2, 0, 0, 0, 0, 0);
auto internal = toInternalDuration(d);
TCHECK_EQ(static_cast<int64_t>(internal.dateDuration().days()), 1LL, "toInternal: days=1");
TCHECK_EQ(internal.time(), Int128(7200000000000LL), "toInternal: time=2h ns");
// toInternalDurationRecordWith24HourDays: folds days into time
auto r = toInternalDurationRecordWith24HourDays(d);
TCHECK_TRUE(r.has_value(), "toInternal24h: ok");
// days folded into time: 1d + 2h = 26h = 93600000000000 ns, date part = 0 days
TCHECK_EQ(static_cast<int64_t>(r->dateDuration().days()), 0LL, "toInternal24h: days=0");
TCHECK_EQ(r->time(), Int128(93600000000000LL), "toInternal24h: time=26h");
}
// ---------------------------------------------------------------------------
// diffISODateTime — unrounded internal duration between datetimes
// ---------------------------------------------------------------------------
static void testDiffISODateTime()
{
// temporal_rs: IsoDateTime::diff (unrounded portion)
ISO8601::PlainDate d1 { 2019, 1, 8 }, d2 { 2021, 9, 7 };
ISO8601::PlainTime t1 { 8, 22, 36, 0, 0, 0 }, t2 { 12, 39, 40, 0, 0, 0 };
// 2019-01-08T08:22:36 until 2021-09-07T12:39:40, largestUnit=Day
auto r = diffISODateTime(d1, t1, d2, t2, TemporalUnit::Day);
// 973 days + 4h 17m 4s
TCHECK_EQ(static_cast<int64_t>(r.dateDuration().days()), 973LL, "diffIDT: days=973");
Int128 expected4h17m4s = timeDurationFromComponents(4, 17, 4, 0, 0, 0);
TCHECK_EQ(r.time(), expected4h17m4s, "diffIDT: time=4h17m4s");
// Same datetime -> zero
auto r2 = diffISODateTime(d1, t1, d1, t1, TemporalUnit::Day);
TCHECK_EQ(static_cast<int64_t>(r2.dateDuration().days()), 0LL, "diffIDT: same=0 days");
TCHECK_EQ(r2.time(), Int128(0LL), "diffIDT: same=0 time");
// Negative: later until earlier
auto r3 = diffISODateTime(d2, t2, d1, t1, TemporalUnit::Day);