forked from unicode-org/icu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalregts.cpp
3280 lines (2975 loc) · 111 KB
/
calregts.cpp
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
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2016, International Business Machines Corporation
* and others. All Rights Reserved.
********************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "calregts.h"
#include "unicode/calendar.h"
#include "unicode/gregocal.h"
#include "unicode/simpletz.h"
#include "unicode/smpdtfmt.h"
#include "unicode/strenum.h"
#include "unicode/localpointer.h"
#include "cmemory.h"
#include "caltest.h"
#include "unicode/localpointer.h"
#include <float.h>
// *****************************************************************************
// class CalendarRegressionTest
// *****************************************************************************
// these numbers correspond to using LONG_MIN and LONG_MAX in Java
// this is 2^52 - 1, the largest allowable mantissa with a 0 exponent in a 64-bit double
const UDate CalendarRegressionTest::EARLIEST_SUPPORTED_MILLIS = - 4503599627370495.0;
const UDate CalendarRegressionTest::LATEST_SUPPORTED_MILLIS = 4503599627370495.0;
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
void
CalendarRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
// if (exec) logln((UnicodeString)"TestSuite NumberFormatRegressionTest");
switch (index) {
CASE(0,test4100311);
CASE(1,test4074758);
CASE(2,test4028518);
CASE(3,test4031502);
CASE(4,test4035301);
CASE(5,test4040996);
CASE(6,test4051765);
CASE(7,test4061476);
CASE(8,test4070502);
CASE(9,test4071197);
CASE(10,test4071385);
CASE(11,test4073929);
CASE(12,test4083167);
CASE(13,test4086724);
CASE(14,test4095407);
CASE(15,test4096231);
CASE(16,test4096539);
CASE(17,test41003112);
CASE(18,test4103271);
CASE(19,test4106136);
CASE(20,test4108764);
CASE(21,test4114578);
CASE(22,test4118384);
CASE(23,test4125881);
CASE(24,test4125892);
CASE(25,test4141665);
CASE(26,test4142933);
CASE(27,test4145158);
CASE(28,test4145983);
CASE(29,test4147269);
CASE(30,Test4149677);
CASE(31,Test4162587);
CASE(32,Test4165343);
CASE(33,Test4166109);
CASE(34,Test4167060);
CASE(35,Test4197699);
CASE(36,TestJ81);
CASE(37,TestJ438);
CASE(38,TestLeapFieldDifference);
CASE(39,TestMalaysianInstance);
CASE(40,test4059654);
CASE(41,test4092362);
CASE(42,TestWeekShift);
CASE(43,TestTimeZoneTransitionAdd);
CASE(44,TestDeprecates);
CASE(45,TestT5555);
CASE(46,TestT6745);
CASE(47,TestT8057);
CASE(48,TestT8596);
CASE(49,Test9019);
CASE(50,TestT9452);
CASE(51,TestT11632);
CASE(52,TestPersianCalOverflow);
CASE(53,TestIslamicCalOverflow);
CASE(54,TestWeekOfYear13548);
CASE(55,Test13745);
CASE(56,TestUTCWrongAMPM22023);
CASE(57,TestAsiaManilaAfterSetGregorianChange22043);
CASE(58,TestRespectUExtensionFw);
default: name = ""; break;
}
}
const char* CalendarRegressionTest::FIELD_NAME [] = {
"ERA",
"YEAR",
"MONTH",
"WEEK_OF_YEAR",
"WEEK_OF_MONTH",
"DAY_OF_MONTH",
"DAY_OF_YEAR",
"DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH",
"AM_PM",
"HOUR",
"HOUR_OF_DAY",
"MINUTE",
"SECOND",
"MILLISECOND",
"ZONE_OFFSET",
"DST_OFFSET",
"YEAR_WOY",
"DOW_LOCAL"
};
UBool
CalendarRegressionTest::failure(UErrorCode status, const char* msg)
{
if(U_FAILURE(status)) {
errcheckln(status, UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
return true;
}
return false;
}
/*
* bug 4100311
*/
void
CalendarRegressionTest::test4100311()
{
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *cal = dynamic_cast<GregorianCalendar*>(Calendar::createInstance(status));
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
return;
}
failure(status, "Calendar::createInstance(status)");
cal->set(UCAL_YEAR, 1997);
cal->set(UCAL_DAY_OF_YEAR, 1);
UDate d = cal->getTime(status); // Should be Jan 1
failure(status, "cal->getTime");
logln(UnicodeString("") + d);
delete cal;
}
/**
* @bug 4074758
*/
void
CalendarRegressionTest::test4074758()
{ //Set system time to between 12-1 (am or pm) and then run
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *cal = new GregorianCalendar(status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
return;
}
failure(status, "new GregorianCalendar");
for (int32_t h=0; h<25; ++h) {
cal->set(97, UCAL_JANUARY, 1, h, 34);
//System.out.print(d);
logln(UnicodeString("HOUR=") + cal->get(UCAL_HOUR, status)); //prints 0
failure(status, "cal->get");
logln(UnicodeString("HOUR_OF_DAY=") + cal->get(UCAL_HOUR_OF_DAY, status));
failure(status, "cal->get");
}
delete cal;
}
void
CalendarRegressionTest::test4028518()
{
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *cal1 = new GregorianCalendar(status) ;
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal1;
return;
}
failure(status, "new GregorianCalendar");
GregorianCalendar *cal2 = cal1->clone() ;
printdate(cal1, "cal1: ") ;
printdate(cal2, "cal2 - cloned(): ") ;
cal1->add(UCAL_DATE, 1, status) ;
failure(status, "cal1->add");
printdate(cal1, "cal1 after adding 1 day:") ;
printdate(cal2, "cal2 should be unmodified:") ;
delete cal1;
delete cal2;
}
void
CalendarRegressionTest::Test9019()
{
UErrorCode status = U_ZERO_ERROR;
LocalPointer<GregorianCalendar> cal1(new GregorianCalendar(status), status);
LocalPointer<GregorianCalendar> cal2(new GregorianCalendar(status), status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
return;
}
cal1->set(UCAL_HOUR, 1);
cal2->set(UCAL_HOUR,2);
cal1->clear();
cal2->clear();
failure(status, "new GregorianCalendar");
cal1->set(2011,UCAL_MAY,06);
cal2->set(2012,UCAL_JANUARY,06);
printdate(cal1.getAlias(), "cal1: ") ;
cal1->setLenient(false);
cal1->add(UCAL_MONTH,8,status);
failure(status, "->add(UCAL_MONTH,8)");
printdate(cal1.getAlias(), "cal1 (lenient) after adding 8 months:") ;
printdate(cal2.getAlias(), "cal2 (expected date):") ;
if(!cal1->equals(*cal2,status)) {
errln("Error: cal1 != cal2.\n");
}
failure(status, "equals");
}
void
CalendarRegressionTest::printdate(GregorianCalendar *cal, const char *string)
{
UErrorCode status = U_ZERO_ERROR;
logln(UnicodeString(string, ""));
log(UnicodeString("") + cal->get(UCAL_MONTH, status)) ;
failure(status, "cal->get");
int32_t date = cal->get(UCAL_DATE, status) + 1 ;
failure(status, "cal->get");
log(UnicodeString("/") + date) ;
logln(UnicodeString("/") + cal->get(UCAL_YEAR, status)) ;
failure(status, "cal->get");
}
/**
* @bug 4031502
*/
void
CalendarRegressionTest::test4031502()
{
// This bug actually occurs on Windows NT as well, and doesn't
// require the host zone to be set; it can be set in Java.
UErrorCode status = U_ZERO_ERROR;
StringEnumeration* ids = TimeZone::createEnumeration(status);
if (U_FAILURE(status)) {
dataerrln("Unable to create TimeZone Enumeration.");
return;
}
UBool bad = false;
TimeZone* tz =TimeZone::createTimeZone("Asia/Riyadh87");
failure(status, "new TimeZone");
GregorianCalendar *cl = new GregorianCalendar(tz, status);
if (U_FAILURE(status)) {
dataerrln("Fail new GregorianCalendar: %s", u_errorName(status));
delete tz;
return;
}
cl->clear();
cl->set(1900, 15, 5, 5, 8, 13);
cl->get(UCAL_HOUR, status);
failure(status, "cl->get(UCAL_HOUR, status)");
status = U_ZERO_ERROR;
delete cl;
for (int32_t i=0; i<ids->count(status); ++i) {
TimeZone *zone = TimeZone::createTimeZone(*ids->snext(status));
GregorianCalendar *cal = new GregorianCalendar(zone, status);
failure(status, "new GregorianCalendar");
cal->clear();
cal->set(1900, 15, 5, 5, 8, 13);
if (cal->get(UCAL_HOUR, status) != 5 || U_FAILURE(status)) {
UnicodeString temp;
logln(zone->getID(temp) + " " +
//zone.useDaylightTime() + " " +
cal->get(UCAL_DST_OFFSET,status) / (60*60*1000) + " " +
zone->getRawOffset() / (60*60*1000) +
": HOUR = " + cal->get(UCAL_HOUR,status));
bad = true;
}
delete cal;
}
if (bad)
errln("TimeZone problems with GC");
// delete [] ids; // TODO: bad APIs
delete ids;
}
/**
* @bug 4035301
*/
void CalendarRegressionTest::test4035301()
{
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *c = new GregorianCalendar(98, 8, 7,status);
GregorianCalendar *d = new GregorianCalendar(98, 8, 7,status);
if (c->after(*d,status) ||
c->after(*c,status) ||
c->before(*d,status) ||
c->before(*c,status) ||
*c != *c ||
*c != *d)
dataerrln("Fail");
delete c;
delete d;
}
/**
* @bug 4040996
*/
void CalendarRegressionTest::test4040996()
{
int32_t count = 0;
UErrorCode status = U_ZERO_ERROR;
StringEnumeration* ids = TimeZone::createEnumerationForRawOffset(-8 * 60 * 60 * 1000, status);
if (U_FAILURE(status)) {
dataerrln("Unable to create TimeZone enumeration.");
return;
}
count = ids->count(status);
(void)count; // Suppress set but not used warning.
SimpleTimeZone *pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, *ids->snext(status));
pdt->setStartRule(UCAL_APRIL, 1, UCAL_SUNDAY, 2 * 60 * 60 * 1000, status);
pdt->setEndRule(UCAL_OCTOBER, -1, UCAL_SUNDAY, 2 * 60 * 60 * 1000, status);
Calendar *calendar = new GregorianCalendar(pdt, status);
if (U_FAILURE(status)) {
dataerrln("Fail new GregorianCalendar: %s", u_errorName(status));
return;
}
calendar->set(UCAL_MONTH,3);
calendar->set(UCAL_DATE,18);
calendar->set(UCAL_SECOND, 30);
logln(UnicodeString("MONTH: ") + calendar->get(UCAL_MONTH, status));
logln(UnicodeString("DAY_OF_MONTH: ") +
calendar->get(UCAL_DATE, status));
logln(UnicodeString("MINUTE: ") + calendar->get(UCAL_MINUTE, status));
logln(UnicodeString("SECOND: ") + calendar->get(UCAL_SECOND, status));
calendar->add(UCAL_SECOND,6, status);
//This will print out todays date for MONTH and DAY_OF_MONTH
//instead of the date it was set to.
//This happens when adding MILLISECOND or MINUTE also
logln(UnicodeString("MONTH: ") + calendar->get(UCAL_MONTH, status));
logln(UnicodeString("DAY_OF_MONTH: ") +
calendar->get(UCAL_DATE, status));
logln(UnicodeString("MINUTE: ") + calendar->get(UCAL_MINUTE, status));
logln(UnicodeString("SECOND: ") + calendar->get(UCAL_SECOND, status));
if (calendar->get(UCAL_MONTH, status) != 3 ||
calendar->get(UCAL_DATE, status) != 18 ||
calendar->get(UCAL_SECOND, status) != 36)
errln(UnicodeString("Fail: Calendar::add misbehaves"));
delete calendar;
delete ids;
// delete ids; // TODO: BAD API
}
/**
* @bug 4051765
*/
void CalendarRegressionTest::test4051765()
{
UErrorCode status = U_ZERO_ERROR;
Calendar *cal = Calendar::createInstance(status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
return;
}
cal->setLenient(false);
cal->set(UCAL_DAY_OF_WEEK, 0);
//try {
cal->getTime(status);
if( ! U_FAILURE(status))
errln("Fail: DAY_OF_WEEK 0 should be disallowed");
/*}
catch (IllegalArgumentException e) {
return;
}*/
delete cal;
}
/* User error - no bug here
void CalendarRegressionTest::test4059524() {
// Create calendar for April 10, 1997
GregorianCalendar calendar = new GregorianCalendar(status);
// print out a bunch of interesting things
logln("ERA: " + Calendar::get(Calendar::ERA));
logln("YEAR: " + Calendar::get(Calendar::YEAR));
logln("MONTH: " + Calendar::get(Calendar::MONTH));
logln("WEEK_OF_YEAR: " +
Calendar::get(Calendar::WEEK_OF_YEAR));
logln("WEEK_OF_MONTH: " +
Calendar::get(Calendar::WEEK_OF_MONTH));
logln("DATE: " + Calendar::get(Calendar::DATE));
logln("DAY_OF_MONTH: " +
Calendar::get(Calendar::DAY_OF_MONTH));
logln("DAY_OF_YEAR: " + Calendar::get(Calendar::DAY_OF_YEAR));
logln("DAY_OF_WEEK: " + Calendar::get(Calendar::DAY_OF_WEEK));
logln("DAY_OF_WEEK_IN_MONTH: " +
Calendar::get(Calendar::DAY_OF_WEEK_IN_MONTH));
logln("AM_PM: " + Calendar::get(Calendar::AM_PM));
logln("HOUR: " + Calendar::get(Calendar::HOUR));
logln("HOUR_OF_DAY: " + Calendar::get(Calendar::HOUR_OF_DAY));
logln("MINUTE: " + Calendar::get(Calendar::MINUTE));
logln("SECOND: " + Calendar::get(Calendar::SECOND));
logln("MILLISECOND: " + Calendar::get(Calendar::MILLISECOND));
logln("ZONE_OFFSET: "
+ (Calendar::get(Calendar::ZONE_OFFSET)/(60*60*1000)));
logln("DST_OFFSET: "
+ (Calendar::get(Calendar::DST_OFFSET)/(60*60*1000)));
calendar = new GregorianCalendar(1997,3,10);
Calendar::getTime();
logln("April 10, 1997");
logln("ERA: " + Calendar::get(Calendar::ERA));
logln("YEAR: " + Calendar::get(Calendar::YEAR));
logln("MONTH: " + Calendar::get(Calendar::MONTH));
logln("WEEK_OF_YEAR: " +
Calendar::get(Calendar::WEEK_OF_YEAR));
logln("WEEK_OF_MONTH: " +
Calendar::get(Calendar::WEEK_OF_MONTH));
logln("DATE: " + Calendar::get(Calendar::DATE));
logln("DAY_OF_MONTH: " +
Calendar::get(Calendar::DAY_OF_MONTH));
logln("DAY_OF_YEAR: " + Calendar::get(Calendar::DAY_OF_YEAR));
logln("DAY_OF_WEEK: " + Calendar::get(Calendar::DAY_OF_WEEK));
logln("DAY_OF_WEEK_IN_MONTH: " + Calendar::get(Calendar::DAY_OF_WEEK_IN_MONTH));
logln("AM_PM: " + Calendar::get(Calendar::AM_PM));
logln("HOUR: " + Calendar::get(Calendar::HOUR));
logln("HOUR_OF_DAY: " + Calendar::get(Calendar::HOUR_OF_DAY));
logln("MINUTE: " + Calendar::get(Calendar::MINUTE));
logln("SECOND: " + Calendar::get(Calendar::SECOND));
logln("MILLISECOND: " + Calendar::get(Calendar::MILLISECOND));
logln("ZONE_OFFSET: "
+ (Calendar::get(Calendar::ZONE_OFFSET)/(60*60*1000))); // in hours
logln("DST_OFFSET: "
+ (Calendar::get(Calendar::DST_OFFSET)/(60*60*1000))); // in hours
}
*/
/**
* @bug 4059654
*/
void CalendarRegressionTest::test4059654() {
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *gc = new GregorianCalendar(status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete gc;
return;
}
gc->set(1997, 3, 1, 15, 16, 17); // April 1, 1997
gc->set(UCAL_HOUR, 0);
gc->set(UCAL_AM_PM, UCAL_AM);
gc->set(UCAL_MINUTE, 0);
gc->set(UCAL_SECOND, 0);
gc->set(UCAL_MILLISECOND, 0);
UDate cd = gc->getTime(status);
GregorianCalendar *exp = new GregorianCalendar(1997, 3, 1, 0, 0, 0, status);
if (cd != exp->getTime(status))
errln(UnicodeString("Fail: Calendar::set broken. Got ") + cd + " Want " + exp->getTime(status));
delete gc;
delete exp;
}
/**
* @bug 4061476
*/
void CalendarRegressionTest::test4061476()
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("ddMMMyy"), Locale::getUK(),status);
Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone("GMT"),
Locale::getUK(),status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
delete fmt;
return;
}
fmt->adoptCalendar(cal);
// try {
UDate date = fmt->parse("29MAY97", status);
failure(status, "fmt->parse");
cal->setTime(date, status);
failure(status, "cal->setTime");
// }
//catch (Exception e) {;}
cal->set(UCAL_HOUR_OF_DAY, 13);
logln(UnicodeString("Hour: ")+cal->get(UCAL_HOUR_OF_DAY, status));
cal->add(UCAL_HOUR_OF_DAY, 6,status);
logln(UnicodeString("Hour: ")+cal->get(UCAL_HOUR_OF_DAY, status));
if (cal->get(UCAL_HOUR_OF_DAY, status) != 19)
errln(UnicodeString("Fail: Want 19 Got ") + cal->get(UCAL_HOUR_OF_DAY, status));
delete fmt;
}
/**
* @bug 4070502
*/
void CalendarRegressionTest::test4070502()
{
UErrorCode status = U_ZERO_ERROR;
Calendar *cal = new GregorianCalendar(status);
if(status == U_USING_FALLBACK_WARNING || U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
return;
}
UDate d = getAssociatedDate(makeDate(1998,0,30), status);
cal->setTime(d,status);
if (cal->get(UCAL_DAY_OF_WEEK,status) == UCAL_SATURDAY ||
cal->get(UCAL_DAY_OF_WEEK,status) == UCAL_SUNDAY)
errln(UnicodeString("Fail: Want weekday Got ") + d);
delete cal;
}
/**
* Get the associated date starting from a specified date
* NOTE: the unnecessary "getTime()'s" below are a work-around for a
* bug in jdk 1.1.3 (and probably earlier versions also)
* <p>
* @param date The date to start from
*/
UDate
CalendarRegressionTest::getAssociatedDate(UDate d, UErrorCode& status)
{
GregorianCalendar *cal = new GregorianCalendar(status);
cal->setTime(d,status);
//cal.add(field, amount); //<-- PROBLEM SEEN WITH field = DATE,MONTH
// cal.getTime(); // <--- REMOVE THIS TO SEE BUG
for (;;) {
int32_t wd = cal->get(UCAL_DAY_OF_WEEK, status);
if (wd == UCAL_SATURDAY || wd == UCAL_SUNDAY) {
cal->add(UCAL_DATE, 1, status);
// cal.getTime();
}
else
break;
}
UDate dd = cal->getTime(status);
delete cal;
return dd;
}
/**
* @bug 4071197
*/
void CalendarRegressionTest::test4071197()
{
dowTest(false);
dowTest(true);
}
void CalendarRegressionTest::dowTest(UBool lenient)
{
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *cal = new GregorianCalendar(status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
return;
}
cal->set(1997, UCAL_AUGUST, 12); // Wednesday
// cal.getTime(); // Force update
cal->setLenient(lenient);
cal->set(1996, UCAL_DECEMBER, 1); // Set the date to be December 1, 1996
int32_t dow = cal->get(UCAL_DAY_OF_WEEK, status);
int32_t min = cal->getMinimum(UCAL_DAY_OF_WEEK);
int32_t max = cal->getMaximum(UCAL_DAY_OF_WEEK);
//logln(cal.getTime().toString());
if (min != UCAL_SUNDAY || max != UCAL_SATURDAY)
errln("FAIL: Min/max bad");
if (dow < min || dow > max)
errln("FAIL: Day of week %d out of range [%d,%d]\n", dow, min, max);
if (dow != UCAL_SUNDAY)
errln(UnicodeString("FAIL: Day of week should be SUNDAY Got ") + dow);
if(U_FAILURE(status)) {
errln("Error checking Calendar: %s", u_errorName(status));
delete cal;
return;
}
if(cal->getActualMinimum(UCAL_DAY_OF_WEEK, status) != min) {
errln("FAIL: actual minimum differs from minimum");
}
if(cal->getActualMinimum(Calendar::DAY_OF_WEEK, status) != min) {
errln("FAIL: actual minimum (Calendar::DAY_OF_WEEK, status) differs from minimum");
}
if(cal->getActualMinimum(Calendar::DAY_OF_WEEK) != min) {
errln("FAIL: actual minimum (Calendar::DAY_OF_WEEK) differs from minimum");
}
if(cal->getActualMinimum(UCAL_DAY_OF_WEEK, status) != min) {
errln("FAIL: actual minimum (UCAL_DAY_OF_WEEK, status) differs from minimum");
}
// NOTE: This function does not exist! jitterbug #3016
// if(((Calendar*)cal)->getActualMinimum(Calendar::DAY_OF_WEEK, status) != min) {
// errln("FAIL: actual minimum (Calendar::DAY_OF_WEEK, status) differs from minimum");
// }
if(U_FAILURE(status)) {
errln("Error getting actual minimum: %s", u_errorName(status));
return;
}
delete cal;
}
/**
* @bug 4071385
*/
void CalendarRegressionTest::test4071385()
{
UErrorCode status = U_ZERO_ERROR;
Calendar *cal = Calendar::createInstance(status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
return;
}
cal->setTime(makeDate(1998, UCAL_JUNE, 24),status);
cal->set(UCAL_MONTH, UCAL_NOVEMBER); // change a field
//logln(cal.getTime().toString());
if (cal->getTime(status) != makeDate(1998, UCAL_NOVEMBER, 24))
errln("Fail");
delete cal;
}
/**
* @bug 4073929
*/
void CalendarRegressionTest::test4073929()
{
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *foo1 = new GregorianCalendar(1997, 8, 27,status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete foo1;
return;
}
logln("foo1@%.0f - %d-%d-%d %d:%d:%d.%ds\n", foo1->getTime(status),
foo1->get(UCAL_YEAR, status),
foo1->get(UCAL_MONTH, status),
foo1->get(UCAL_DATE, status),
foo1->get(UCAL_HOUR, status),
foo1->get(UCAL_MINUTE, status),
foo1->get(UCAL_SECOND, status),
foo1->get(UCAL_MILLISECOND,status));
foo1->add(UCAL_DATE, + 1, status);
logln("foo1@%.0f - %d-%d-%d %d:%d:%d.%ds after +\n", foo1->getTime(status),
foo1->get(UCAL_YEAR, status),
foo1->get(UCAL_MONTH, status),
foo1->get(UCAL_DATE, status),
foo1->get(UCAL_HOUR, status),
foo1->get(UCAL_MINUTE, status),
foo1->get(UCAL_SECOND, status),
foo1->get(UCAL_MILLISECOND ,status));
foo1->add(UCAL_DATE, - 1, status);
logln("foo1@%.0f - %d-%d-%d %d:%d:%d.%ds after -\n", foo1->getTime(status),
foo1->get(UCAL_YEAR, status),
foo1->get(UCAL_MONTH, status),
foo1->get(UCAL_DATE, status),
foo1->get(UCAL_HOUR, status),
foo1->get(UCAL_MINUTE, status),
foo1->get(UCAL_SECOND, status),
foo1->get(UCAL_MILLISECOND, status));
foo1->add(UCAL_DATE, + 1, status);
int32_t testyear = foo1->get(UCAL_YEAR, status);
int32_t testmonth = foo1->get(UCAL_MONTH, status);
int32_t testday = foo1->get(UCAL_DATE, status);
if (testyear != 1997 ||
testmonth != 8 ||
testday != 28)
errln("Fail: Calendar not initialized");
delete foo1;
}
/**
* @bug 4083167
*/
void CalendarRegressionTest::test4083167()
{
UErrorCode status = U_ZERO_ERROR;
TimeZone *saveZone = TimeZone::createDefault();
//try {
TimeZone *newZone = TimeZone::createTimeZone("UTC");
TimeZone::setDefault(*newZone);
UDate firstDate = Calendar::getNow();
Calendar *cal = new GregorianCalendar(status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
return;
}
cal->setTime(firstDate,status);
int32_t hr = cal->get(UCAL_HOUR_OF_DAY, status);
int32_t min = cal->get(UCAL_MINUTE, status);
int32_t sec = cal->get(UCAL_SECOND, status);
int32_t msec = cal->get(UCAL_MILLISECOND, status);
double firstMillisInDay = hr * 3600000 + min * 60000 + sec * 1000 + msec;
//logln("Current time: " + firstDate.toString());
for (int32_t validity=0; validity<30; validity++) {
UDate lastDate = firstDate + validity*1000*24*60*60.0;
cal->setTime(lastDate, status);
hr = cal->get(UCAL_HOUR_OF_DAY, status);
min = cal->get(UCAL_MINUTE, status);
sec = cal->get(UCAL_SECOND, status);
msec = cal->get(UCAL_MILLISECOND, status);
double millisInDay = hr * 3600000.0 + min * 60000.0 + sec * 1000.0 + msec;
if (firstMillisInDay != millisInDay)
errln(UnicodeString("Day has shifted ") + lastDate);
}
//}
//finally {
TimeZone::setDefault(*saveZone);
//}
delete saveZone;
delete newZone;
delete cal;
}
/**
* @bug 4086724
*/
void CalendarRegressionTest::test4086724()
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *date;
TimeZone *saveZone = TimeZone::createDefault();
Locale saveLocale = Locale::getDefault();
//try {
Locale::setDefault(Locale::getUK(),status);
TimeZone *newZone = TimeZone::createTimeZone("GMT");
TimeZone::setDefault(*newZone);
date = new SimpleDateFormat(UnicodeString("dd MMM yyy (zzzz) 'is in week' ww"),status);
Calendar *cal = Calendar::createInstance(status);
if(U_FAILURE(status)) {
dataerrln("Error creating Calendar: %s", u_errorName(status));
delete cal;
delete newZone;
delete date;
return;
}
cal->set(1997,UCAL_SEPTEMBER,30);
UDate now = cal->getTime(status);
UnicodeString temp;
FieldPosition pos(FieldPosition::DONT_CARE);
logln(date->format(now, temp, pos));
cal->set(1997,UCAL_JANUARY,1);
now=cal->getTime(status);
logln(date->format(now,temp, pos));
cal->set(1997,UCAL_JANUARY,8);
now=cal->getTime(status);
logln(date->format(now,temp, pos));
cal->set(1996,UCAL_DECEMBER,31);
now=cal->getTime(status);
logln(date->format(now,temp, pos));
//}
//finally {
Locale::setDefault(saveLocale,status);
TimeZone::setDefault(*saveZone);
//}
logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");
delete newZone;
delete cal;
delete date;
delete saveZone;
}
/**
* @bug 4092362
*/
void CalendarRegressionTest::test4092362() {
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *cal1 = new GregorianCalendar(1997, 10, 11, 10, 20, 40,status);
if (U_FAILURE(status)) {
dataerrln("Fail new GregorianCalendar: %s", u_errorName(status));
delete cal1;
return;
}
/*cal1.set( Calendar::YEAR, 1997 );
cal1.set( Calendar::MONTH, 10 );
cal1.set( Calendar::DATE, 11 );
cal1.set( Calendar::HOUR, 10 );
cal1.set( Calendar::MINUTE, 20 );
cal1.set( Calendar::SECOND, 40 ); */
logln( UnicodeString(" Cal1 = ") + cal1->getTime(status) );
logln( UnicodeString(" Cal1 time in ms = ") + cal1->get(UCAL_MILLISECOND,status) );
for (int32_t k = 0; k < 100 ; k++)
;
GregorianCalendar *cal2 = new GregorianCalendar(1997, 10, 11, 10, 20, 40,status);
/*cal2.set( Calendar::YEAR, 1997 );
cal2.set( Calendar::MONTH, 10 );
cal2.set( Calendar::DATE, 11 );
cal2.set( Calendar::HOUR, 10 );
cal2.set( Calendar::MINUTE, 20 );
cal2.set( Calendar::SECOND, 40 ); */
logln( UnicodeString(" Cal2 = ") + cal2->getTime(status) );
logln( UnicodeString(" Cal2 time in ms = ") + cal2->get(UCAL_MILLISECOND,status) );
if( *cal1 != *cal2 )
errln("Fail: Milliseconds randomized");
delete cal1;
delete cal2;
}
/**
* @bug 4095407
*/
void CalendarRegressionTest::test4095407()
{
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *a = new GregorianCalendar(1997,UCAL_NOVEMBER, 13,status);
if (U_FAILURE(status)) {
dataerrln("Fail new GregorianCalendar: %s", u_errorName(status));
delete a;
return;
}
int32_t dow = a->get(UCAL_DAY_OF_WEEK, status);
if (dow != UCAL_THURSDAY)
errln(UnicodeString("Fail: Want THURSDAY Got ") + dow);
delete a;
}
/**
* @bug 4096231
*/
void CalendarRegressionTest::test4096231()
{
UErrorCode status = U_ZERO_ERROR;
TimeZone *GMT = TimeZone::createTimeZone("GMT");
TimeZone *PST = TimeZone::createTimeZone("PST");
int32_t sec = 0, min = 0, hr = 0, day = 1, month = 10, year = 1997;
Calendar *cal1 = new GregorianCalendar(*PST,status);
if (U_FAILURE(status)) {
dataerrln("Failure new GregorianCalendar: %s", u_errorName(status));
delete GMT;
delete PST;
delete cal1;
return;
}
cal1->setTime(880698639000.0,status);
// Issue 1: Changing the timezone doesn't change the
// represented time. The old API, pre 1.2.2a requires
// setTime to be called in order to update the time fields after the time
// zone has been set.
int32_t h1,h2;
logln(UnicodeString("PST 1 is: ") + (h1=cal1->get(UCAL_HOUR_OF_DAY, status)));
cal1->setTimeZone(*GMT);
logln(UnicodeString("GMT 2 is: ") + (h2=cal1->get(UCAL_HOUR_OF_DAY, status)));
if ((*GMT != *PST) && (h1 == h2))
errln("Fail: Hour same in different zones");
Calendar *cal2 = new GregorianCalendar(*GMT,status);
Calendar *cal3 = new GregorianCalendar(*PST,status);
cal2->set(UCAL_MILLISECOND, 0);
cal3->set(UCAL_MILLISECOND, 0);
cal2->set(cal1->get(UCAL_YEAR,status),
cal1->get(UCAL_MONTH,status),
cal1->get(UCAL_DATE,status),
cal1->get(UCAL_HOUR_OF_DAY,status),
cal1->get(UCAL_MINUTE,status),
cal1->get(UCAL_SECOND,status));
double t1,t2,t3,t4;
logln(UnicodeString("RGMT 1 is: ") + (t1=cal2->getTime(status)));
cal3->set(year, month, day, hr, min, sec);
logln(UnicodeString("RPST 1 is: ") + (t2=cal3->getTime(status)));
cal3->setTimeZone(*GMT);
logln(UnicodeString("RGMT 2 is: ") + (t3=cal3->getTime(status)));
cal3->set(cal1->get(UCAL_YEAR,status),
cal1->get(UCAL_MONTH,status),
cal1->get(UCAL_DATE,status),
cal1->get(UCAL_HOUR_OF_DAY,status),
cal1->get(UCAL_MINUTE,status),
cal1->get(UCAL_SECOND,status));
// Issue 2: Calendar continues to use the timezone in its
// constructor for set() conversions, regardless
// of calls to setTimeZone()
logln(UnicodeString("RGMT 3 is: ") + (t4=cal3->getTime(status)));
if (t1 == t2 ||
t1 != t4 ||
t2 != t3)
errln("Fail: Calendar zone behavior faulty");
delete cal1;
delete cal2;
delete cal3;
delete GMT;
delete PST;
}
/**
* @bug 4096539
*/
void CalendarRegressionTest::test4096539()
{
UErrorCode status = U_ZERO_ERROR;
int32_t y [] = {31,28,31,30,31,30,31,31,30,31,30,31};
for (int32_t x=0;x<12;x++) {
GregorianCalendar *gc = new
GregorianCalendar(1997,x,y[x], status);
if (U_FAILURE(status)) {
dataerrln("Fail new GregorianCalendar: %s", u_errorName(status));
delete gc;
return;
}
int32_t m1,m2;
log(UnicodeString("") + (m1=gc->get(UCAL_MONTH,status)+1)+UnicodeString("/")+
gc->get(UCAL_DATE,status)+"/"+gc->get(UCAL_YEAR,status)+
" + 1mo = ");
gc->add(UCAL_MONTH, 1,status);
logln(UnicodeString("") + (m2=gc->get(UCAL_MONTH,status)+1)+UnicodeString("/")+
gc->get(UCAL_DATE,status)+"/"+gc->get(UCAL_YEAR,status)
);
int32_t m = (m1 % 12) + 1;
if (m2 != m)
errln(UnicodeString("Fail: Want ") + m + " Got " + m2);
delete gc;
}
}
/**
* @bug 4100311
*/
void CalendarRegressionTest::test41003112()
{
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *cal = dynamic_cast<GregorianCalendar*>(Calendar::createInstance(status));
if(U_FAILURE(status)) {
dataerrln("Error creating calendar: %s", u_errorName(status));
delete cal;
return;
}
cal->set(UCAL_YEAR, 1997);
cal->set(UCAL_DAY_OF_YEAR, 1);
//UDate d = cal->getTime(status); // Should be Jan 1
//logln(d.toString());
if (cal->get(UCAL_DAY_OF_YEAR, status) != 1)
errln("Fail: DAY_OF_YEAR not set");
delete cal;
}
/**
* @bug 4103271
*/
void CalendarRegressionTest::test4103271()
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat sdf(status);
int32_t numYears=40, startYear=1997, numDays=15;
UnicodeString output, testDesc, str, str2;
GregorianCalendar *testCal = dynamic_cast<GregorianCalendar*>(Calendar::createInstance(status));
if(U_FAILURE(status)) {
dataerrln("Error creating calendar: %s", u_errorName(status));