summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu-xetex/test/intltest/tzrulets.cpp
blob: 702b502ea2c671fff25be710643f1c387aed6679 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
/*
*******************************************************************************
* Copyright (C) 2007, International Business Machines Corporation and         *
* others. All Rights Reserved.                                                *
*******************************************************************************
*/

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING

#include "unicode/dtrule.h"
#include "unicode/tzrule.h"
#include "unicode/rbtz.h"
#include "unicode/simpletz.h"
#include "unicode/tzrule.h"
#include "unicode/calendar.h"
#include "unicode/gregocal.h"
#include "unicode/ucal.h"
#include "unicode/unistr.h"
#include "unicode/tztrans.h"
#include "unicode/vtzone.h"
#include "tzrulets.h"

#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
#define HOUR (60*60*1000)

static const char *const TESTZIDS[] = {
        "AGT",
        "America/New_York",
        "America/Los_Angeles",
        "America/Indiana/Indianapolis",
        "America/Havana",
        "Europe/Lisbon",
        "Europe/Paris",
        "Asia/Tokyo",
        "Asia/Sakhalin",
        "Africa/Cairo",
        "Africa/Windhoek",
        "Australia/Sydney",
        "Etc/GMT+8"
};

class TestZIDEnumeration : public StringEnumeration {
public:
    TestZIDEnumeration(UBool all = FALSE);
    ~TestZIDEnumeration();

    virtual int32_t count(UErrorCode& /*status*/) const {
        return len;
    }
    virtual const UnicodeString *snext(UErrorCode& status);
    virtual void reset(UErrorCode& status);
    static inline UClassID getStaticClassID() {
        return (UClassID)&fgClassID;
    }
    virtual UClassID getDynamicClassID() const {
        return getStaticClassID();
    }
private:
    static const char fgClassID;
    int32_t idx;
    int32_t len;
    StringEnumeration   *tzenum;
};

const char TestZIDEnumeration::fgClassID = 0;

TestZIDEnumeration::TestZIDEnumeration(UBool all)
: idx(0) {
    UErrorCode status = U_ZERO_ERROR;
    if (all) {
        tzenum = TimeZone::createEnumeration();
        len = tzenum->count(status);
    } else {
        tzenum = NULL;
        len = (int32_t)sizeof(TESTZIDS)/sizeof(TESTZIDS[0]);
    }
}

TestZIDEnumeration::~TestZIDEnumeration() {
    if (tzenum != NULL) {
        delete tzenum;
    }
}

const UnicodeString*
TestZIDEnumeration::snext(UErrorCode& status) {
    if (tzenum != NULL) {
        return tzenum->snext(status);
    } else if (U_SUCCESS(status) && idx < len) {
        unistr = UnicodeString(TESTZIDS[idx++], "");
        return &unistr;
    }
    return NULL;
}

void
TestZIDEnumeration::reset(UErrorCode& status) {
    if (tzenum != NULL) {
        tzenum->reset(status);
    } else {
        idx = 0;
    }
}


void TimeZoneRuleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
    if (exec) {
        logln("TestSuite TestTimeZoneRule");
    }
    switch (index) {
        CASE(0, TestSimpleRuleBasedTimeZone);
        CASE(1, TestHistoricalRuleBasedTimeZone);
        CASE(2, TestOlsonTransition);
        CASE(3, TestRBTZTransition);
        CASE(4, TestHasEquivalentTransitions);
        CASE(5, TestVTimeZoneRoundTrip);
        CASE(6, TestVTimeZoneRoundTripPartial);
        CASE(7, TestVTimeZoneSimpleWrite);
        CASE(8, TestVTimeZoneHeaderProps);
        CASE(9, TestGetSimpleRules);
        CASE(10, TestTimeZoneRuleCoverage);
        CASE(11, TestSimpleTimeZoneCoverage);
        CASE(12, TestVTimeZoneCoverage);
        CASE(13, TestVTimeZoneParse);
        default: name = ""; break;
    }
}

/*
 * Compare SimpleTimeZone with equivalent RBTZ
 */
void
TimeZoneRuleTest::TestSimpleRuleBasedTimeZone(void) {
    UErrorCode status = U_ZERO_ERROR;
    SimpleTimeZone stz(-1*HOUR, "TestSTZ",
        UCAL_SEPTEMBER, -30, -UCAL_SATURDAY, 1*HOUR, SimpleTimeZone::WALL_TIME,
        UCAL_FEBRUARY, 2, UCAL_SUNDAY, 1*HOUR, SimpleTimeZone::WALL_TIME,
        1*HOUR, status);
    if (U_FAILURE(status)) {
        errln("FAIL: Couldn't create SimpleTimezone.");
    }

    DateTimeRule *dtr;
    AnnualTimeZoneRule *atzr;
    int32_t STARTYEAR = 2000;

    InitialTimeZoneRule *ir = new InitialTimeZoneRule(
        "RBTZ_Initial", // Initial time Name
        -1*HOUR,        // Raw offset
        1*HOUR);        // DST saving amount

    // Original rules
    RuleBasedTimeZone *rbtz1 = new RuleBasedTimeZone("RBTZ1", ir->clone());
    dtr = new DateTimeRule(UCAL_SEPTEMBER, 30, UCAL_SATURDAY, FALSE,
        1*HOUR, DateTimeRule::WALL_TIME); // SUN<=30 in September, at 1AM wall time
    atzr = new AnnualTimeZoneRule("RBTZ_DST1",
        -1*HOUR /*rawOffset*/, 1*HOUR /*dstSavings*/, dtr,
        STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
    rbtz1->addTransitionRule(atzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 1-1.");
    }
    dtr = new DateTimeRule(UCAL_FEBRUARY, 2, UCAL_SUNDAY,
        1*HOUR, DateTimeRule::WALL_TIME);  // 2nd Sunday in February, at 1AM wall time
    atzr = new AnnualTimeZoneRule("RBTZ_STD1",
        -1*HOUR /*rawOffset*/, 0 /*dstSavings*/, dtr,
        STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
    rbtz1->addTransitionRule(atzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 1-2.");
    }
    rbtz1->complete(status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't complete RBTZ 1.");
    }

    // Equivalent, but different date rule type
    RuleBasedTimeZone *rbtz2 = new RuleBasedTimeZone("RBTZ2", ir->clone());
    dtr = new DateTimeRule(UCAL_SEPTEMBER, -1, UCAL_SATURDAY,
        1*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in September at 1AM wall time
    atzr = new AnnualTimeZoneRule("RBTZ_DST2", -1*HOUR, 1*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
    rbtz2->addTransitionRule(atzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 2-1.");
    }
    dtr = new DateTimeRule(UCAL_FEBRUARY, 8, UCAL_SUNDAY, true,
        1*HOUR, DateTimeRule::WALL_TIME); // SUN>=8 in February, at 1AM wall time
    atzr = new AnnualTimeZoneRule("RBTZ_STD2", -1*HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
    rbtz2->addTransitionRule(atzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 2-2.");
    }
    rbtz2->complete(status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't complete RBTZ 2");
    }

    // Equivalent, but different time rule type
    RuleBasedTimeZone *rbtz3 = new RuleBasedTimeZone("RBTZ3", ir->clone());
    dtr = new DateTimeRule(UCAL_SEPTEMBER, 30, UCAL_SATURDAY, false,
        2*HOUR, DateTimeRule::UTC_TIME);
    atzr = new AnnualTimeZoneRule("RBTZ_DST3", -1*HOUR, 1*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
    rbtz3->addTransitionRule(atzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 3-1.");
    }
    dtr = new DateTimeRule(UCAL_FEBRUARY, 2, UCAL_SUNDAY,
        0*HOUR, DateTimeRule::STANDARD_TIME);
    atzr = new AnnualTimeZoneRule("RBTZ_STD3", -1*HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
    rbtz3->addTransitionRule(atzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 3-2.");
    }
    rbtz3->complete(status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't complete RBTZ 3");
    }

    // Check equivalency for 10 years
    UDate start = getUTCMillis(STARTYEAR, UCAL_JANUARY, 1);
    UDate until = getUTCMillis(STARTYEAR + 10, UCAL_JANUARY, 1);

    if (!(stz.hasEquivalentTransitions(*rbtz1, start, until, TRUE, status))) {
        errln("FAIL: rbtz1 must be equivalent to the SimpleTimeZone in the time range.");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error returned from hasEquivalentTransitions");
    }
    if (!(stz.hasEquivalentTransitions(*rbtz2, start, until, TRUE, status))) {
        errln("FAIL: rbtz2 must be equivalent to the SimpleTimeZone in the time range.");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error returned from hasEquivalentTransitions");
    }
    if (!(stz.hasEquivalentTransitions(*rbtz3, start, until, TRUE, status))) {
        errln("FAIL: rbtz3 must be equivalent to the SimpleTimeZone in the time range.");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error returned from hasEquivalentTransitions");
    }

    // hasSameRules
    if (rbtz1->hasSameRules(*rbtz2)) {
        errln("FAIL: rbtz1 and rbtz2 have different rules, but returned true.");
    }
    if (rbtz1->hasSameRules(*rbtz3)) {
        errln("FAIL: rbtz1 and rbtz3 have different rules, but returned true.");
    }
    RuleBasedTimeZone *rbtz1c = (RuleBasedTimeZone*)rbtz1->clone();
    if (!rbtz1->hasSameRules(*rbtz1c)) {
        errln("FAIL: Cloned RuleBasedTimeZone must have the same rules with the original.");
    }

    // getOffset
    int32_t era, year, month, dayOfMonth, dayOfWeek, millisInDay;
    UDate time;
    int32_t offset, dstSavings;
    UBool dst;

    GregorianCalendar *cal = new GregorianCalendar(status);
    if (U_FAILURE(status)) {
        errln("FAIL: Could not create a Gregorian calendar instance.");
    }
    cal->setTimeZone(*rbtz1);
    cal->clear();

    // Jan 1, 1000 BC
    cal->set(UCAL_ERA, GregorianCalendar::BC);
    cal->set(1000, UCAL_JANUARY, 1);

    era = cal->get(UCAL_ERA, status);
    year = cal->get(UCAL_YEAR, status);
    month = cal->get(UCAL_MONTH, status);
    dayOfMonth = cal->get(UCAL_DAY_OF_MONTH, status);
    dayOfWeek = cal->get(UCAL_DAY_OF_WEEK, status);
    millisInDay = cal->get(UCAL_MILLISECONDS_IN_DAY, status);
    time = cal->getTime(status);
    if (U_FAILURE(status)) {
        errln("FAIL: Could not get calendar field values.");
    }
    offset = rbtz1->getOffset(era, year, month, dayOfMonth, dayOfWeek, millisInDay, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(7 args) failed.");
    }
    if (offset != 0) {
        errln(UnicodeString("FAIL: Invalid time zone offset: ") + offset + " /expected: 0");
    }
    dst = rbtz1->inDaylightTime(time, status);
    if (U_FAILURE(status)) {
        errln("FAIL: inDaylightTime failed.");
    }
    if (!dst) {
        errln("FAIL: Invalid daylight saving time");
    }
    rbtz1->getOffset(time, TRUE, offset, dstSavings, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(5 args) failed.");
    }
    if (offset != -3600000) {
        errln(UnicodeString("FAIL: Invalid time zone raw offset: ") + offset + " /expected: -3600000");
    }
    if (dstSavings != 3600000) {            
        errln(UnicodeString("FAIL: Invalid DST amount: ") + dstSavings + " /expected: 3600000");
    }

    // July 1, 2000, AD
    cal->set(UCAL_ERA, GregorianCalendar::AD);
    cal->set(2000, UCAL_JULY, 1);

    era = cal->get(UCAL_ERA, status);
    year = cal->get(UCAL_YEAR, status);
    month = cal->get(UCAL_MONTH, status);
    dayOfMonth = cal->get(UCAL_DAY_OF_MONTH, status);
    dayOfWeek = cal->get(UCAL_DAY_OF_WEEK, status);
    millisInDay = cal->get(UCAL_MILLISECONDS_IN_DAY, status);
    time = cal->getTime(status);
    if (U_FAILURE(status)) {
        errln("FAIL: Could not get calendar field values.");
    }
    offset = rbtz1->getOffset(era, year, month, dayOfMonth, dayOfWeek, millisInDay, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(7 args) failed.");
    }
    if (offset != -3600000) {
        errln((UnicodeString)"FAIL: Invalid time zone offset: " + offset + " /expected: -3600000");
    }
    dst = rbtz1->inDaylightTime(time, status);
    if (U_FAILURE(status)) {
        errln("FAIL: inDaylightTime failed.");
    }
    if (dst) {
        errln("FAIL: Invalid daylight saving time");
    }
    rbtz1->getOffset(time, TRUE, offset, dstSavings, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(5 args) failed.");
    }
    if (offset != -3600000) {
        errln((UnicodeString)"FAIL: Invalid time zone raw offset: " + offset + " /expected: -3600000");
    }
    if (dstSavings != 0) {            
        errln((UnicodeString)"FAIL: Invalid DST amount: " + dstSavings + " /expected: 0");
    }

    // getRawOffset
    offset = rbtz1->getRawOffset();
    if (offset != -1*HOUR) {
        errln((UnicodeString)"FAIL: Invalid time zone raw offset returned by getRawOffset: "
            + offset + " /expected: -3600000");
    }

    // operator=/==/!=
    RuleBasedTimeZone rbtz0("RBTZ1", ir->clone());
    if (rbtz0 == *rbtz1 || !(rbtz0 != *rbtz1)) {
        errln("FAIL: RuleBasedTimeZone rbtz0 is not equal to rbtz1, but got wrong result");
    }
    rbtz0 = *rbtz1;
    if (rbtz0 != *rbtz1 || !(rbtz0 == *rbtz1)) {
        errln("FAIL: RuleBasedTimeZone rbtz0 is equal to rbtz1, but got wrong result");
    }

    // setRawOffset
    const int32_t RAW = -10*HOUR;
    rbtz0.setRawOffset(RAW);
    if (rbtz0.getRawOffset() != RAW) {
        logln("setRawOffset is implemented in RuleBasedTimeZone");
    }

    // useDaylightTime
    if (!rbtz1->useDaylightTime()) {
        errln("FAIL: useDaylightTime returned FALSE");
    }

    // Try to add 3rd final rule
    dtr = new DateTimeRule(UCAL_OCTOBER, 15, 1*HOUR, DateTimeRule::WALL_TIME);
    atzr = new AnnualTimeZoneRule("3RD_ATZ", -1*HOUR, 2*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
    rbtz1->addTransitionRule(atzr, status);
    if (U_SUCCESS(status)) {
        errln("FAIL: 3rd final rule must be rejected");
    } else {
        delete atzr;
    }

    // Try to add an initial rule
    InitialTimeZoneRule *ir1 = new InitialTimeZoneRule("Test Initial", 2*HOUR, 0);
    rbtz1->addTransitionRule(ir1, status);
    if (U_SUCCESS(status)) {
        errln("FAIL: InitialTimeZoneRule must be rejected");
    } else {
        delete ir1;
    }

    delete ir;
    delete rbtz1;
    delete rbtz2;
    delete rbtz3;
    delete rbtz1c;
    delete cal;
}

/*
 * Test equivalency between OlsonTimeZone and custom RBTZ representing the
 * equivalent rules in a certain time range
 */
void
TimeZoneRuleTest::TestHistoricalRuleBasedTimeZone(void) {
    UErrorCode status = U_ZERO_ERROR;

    // Compare to America/New_York with equivalent RBTZ
    BasicTimeZone *ny = (BasicTimeZone*)TimeZone::createTimeZone("America/New_York");

    //RBTZ
    InitialTimeZoneRule *ir = new InitialTimeZoneRule("EST", -5*HOUR, 0);
    RuleBasedTimeZone *rbtz = new RuleBasedTimeZone("EST5EDT", ir);

    DateTimeRule *dtr;
    AnnualTimeZoneRule *tzr;

    // Standard time
    dtr = new DateTimeRule(UCAL_OCTOBER, -1, UCAL_SUNDAY,
        2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in October, at 2AM wall time
    tzr = new AnnualTimeZoneRule("EST", -5*HOUR /*rawOffset*/, 0 /*dstSavings*/, dtr, 1967, 2006);
    rbtz->addTransitionRule(tzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 1.");
    }

    dtr = new DateTimeRule(UCAL_NOVEMBER, 1, UCAL_SUNDAY,
        true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=1 in November, at 2AM wall time
    tzr = new AnnualTimeZoneRule("EST", -5*HOUR, 0, dtr, 2007, AnnualTimeZoneRule::MAX_YEAR);
    rbtz->addTransitionRule(tzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 2.");
    }

    // Daylight saving time
    dtr = new DateTimeRule(UCAL_APRIL, -1, UCAL_SUNDAY,
        2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in April, at 2AM wall time
    tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1967, 1973);
    rbtz->addTransitionRule(tzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 3.");
    }

    dtr = new DateTimeRule(UCAL_JANUARY, 6,
        2*HOUR, DateTimeRule::WALL_TIME); // January 6, at 2AM wall time
    tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1974, 1974);
    rbtz->addTransitionRule(tzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 4.");
    }
    
    dtr = new DateTimeRule(UCAL_FEBRUARY, 23,
        2*HOUR, DateTimeRule::WALL_TIME); // February 23, at 2AM wall time
    tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1975, 1975);
    rbtz->addTransitionRule(tzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 5.");
    }

    dtr = new DateTimeRule(UCAL_APRIL, -1, UCAL_SUNDAY,
        2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in April, at 2AM wall time
    tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1976, 1986);
    rbtz->addTransitionRule(tzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 6.");
    }

    dtr = new DateTimeRule(UCAL_APRIL, 1, UCAL_SUNDAY,
        true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=1 in April, at 2AM wall time
    tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1987, 2006);
    rbtz->addTransitionRule(tzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 7.");
    }

    dtr = new DateTimeRule(UCAL_MARCH, 8, UCAL_SUNDAY,
        true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=8 in March, at 2AM wall time
    tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 2007, AnnualTimeZoneRule::MAX_YEAR);
    rbtz->addTransitionRule(tzr, status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't add AnnualTimeZoneRule 7.");
    }

    rbtz->complete(status);
    if (U_FAILURE(status)) {
        errln("FAIL: couldn't complete RBTZ.");
    }

    // hasEquivalentTransitions
    UDate jan1_1950 = getUTCMillis(1950, UCAL_JANUARY, 1);
    UDate jan1_1967 = getUTCMillis(1971, UCAL_JANUARY, 1);
    UDate jan1_2010 = getUTCMillis(2010, UCAL_JANUARY, 1);        

    if (!ny->hasEquivalentTransitions(*rbtz, jan1_1967, jan1_2010, TRUE, status)) {
        errln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error returned from hasEquivalentTransitions for ny/rbtz 1967-2010");
    }
    if (ny->hasEquivalentTransitions(*rbtz, jan1_1950, jan1_2010, TRUE, status)) {
        errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error returned from hasEquivalentTransitions for ny/rbtz 1950-2010");
    }

    // Same with above, but calling RBTZ#hasEquivalentTransitions against OlsonTimeZone
    if (!rbtz->hasEquivalentTransitions(*ny, jan1_1967, jan1_2010, TRUE, status)) {
        errln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error returned from hasEquivalentTransitions for rbtz/ny 1967-2010");
    }
    if (rbtz->hasEquivalentTransitions(*ny, jan1_1950, jan1_2010, TRUE, status)) {
        errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error returned from hasEquivalentTransitions for rbtz/ny 1950-2010");
    }

    // TimeZone APIs
    if (ny->hasSameRules(*rbtz) || rbtz->hasSameRules(*ny)) {
        errln("FAIL: hasSameRules must return false");
    }
    RuleBasedTimeZone *rbtzc = (RuleBasedTimeZone*)rbtz->clone();
    if (!rbtz->hasSameRules(*rbtzc) || !rbtz->hasEquivalentTransitions(*rbtzc, jan1_1950, jan1_2010, TRUE, status)) {
        errln("FAIL: hasSameRules/hasEquivalentTransitions must return true for cloned RBTZs");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error returned from hasEquivalentTransitions for rbtz/rbtzc 1950-2010");
    }

    UDate times[] = {
        getUTCMillis(2006, UCAL_MARCH, 15),
        getUTCMillis(2006, UCAL_NOVEMBER, 1),
        getUTCMillis(2007, UCAL_MARCH, 15),
        getUTCMillis(2007, UCAL_NOVEMBER, 1),
        getUTCMillis(2008, UCAL_MARCH, 15),
        getUTCMillis(2008, UCAL_NOVEMBER, 1),
        0
    };
    int32_t offset1, dst1;
    int32_t offset2, dst2;

    for (int i = 0; times[i] != 0; i++) {
        // Check getOffset - must return the same results for these time data
        rbtz->getOffset(times[i], FALSE, offset1, dst1, status);
        if (U_FAILURE(status)) {
            errln("FAIL: rbtz->getOffset failed");
        }
        ny->getOffset(times[i], FALSE, offset2, dst2, status);
        if (U_FAILURE(status)) {
            errln("FAIL: ny->getOffset failed");
        }
        if (offset1 != offset2 || dst1 != dst2) {
            errln("FAIL: Incompatible time zone offset/dstSavings for ny and rbtz");
        }

        // Check inDaylightTime
        if (rbtz->inDaylightTime(times[i], status) != ny->inDaylightTime(times[i], status)) {
            errln("FAIL: Incompatible daylight saving time for ny and rbtz");
        }
        if (U_FAILURE(status)) {
            errln("FAIL: inDaylightTime failed");
        }
    }

    delete ny;
    delete rbtz;
    delete rbtzc;
}

/*
 * Check if transitions returned by getNextTransition/getPreviousTransition
 * are actual time transitions.
 */
void
TimeZoneRuleTest::TestOlsonTransition(void) {

    const int32_t TESTYEARS[][2] = {
        {1895, 1905}, // including int32 minimum second
        {1965, 1975}, // including the epoch
        {1995, 2015}, // practical year range
        {0,0}
    };

    UErrorCode status = U_ZERO_ERROR;
    TestZIDEnumeration tzenum(!quick);
    while (TRUE) {
        const UnicodeString *tzid = tzenum.snext(status);
        if (tzid == NULL) {
            break;
        }
        if (U_FAILURE(status)) {
            errln("FAIL: error returned while enumerating timezone IDs.");
            break;
        }
        BasicTimeZone *tz = (BasicTimeZone*)TimeZone::createTimeZone(*tzid);
        for (int32_t i = 0; TESTYEARS[i][0] != 0 || TESTYEARS[i][1] != 0; i++) {
            UDate lo = getUTCMillis(TESTYEARS[i][0], UCAL_JANUARY, 1);
            UDate hi = getUTCMillis(TESTYEARS[i][1], UCAL_JANUARY, 1);
            verifyTransitions(*tz, lo, hi);
        }
        delete tz;
    }
}

/*
 * Check if an OlsonTimeZone and its equivalent RBTZ have the exact same
 * transitions.
 */
void
TimeZoneRuleTest::TestRBTZTransition(void) {
    const int32_t STARTYEARS[] = {
        1900,
        1960,
        1990,
        2010,
        0
    };

    UErrorCode status = U_ZERO_ERROR;
    TestZIDEnumeration tzenum(!quick);
    while (TRUE) {
        const UnicodeString *tzid = tzenum.snext(status);
        if (tzid == NULL) {
            break;
        }
        if (U_FAILURE(status)) {
            errln("FAIL: error returned while enumerating timezone IDs.");
            break;
        }
        BasicTimeZone *tz = (BasicTimeZone*)TimeZone::createTimeZone(*tzid);
        int32_t ruleCount = tz->countTransitionRules(status);

        const InitialTimeZoneRule *initial;
        const TimeZoneRule **trsrules = new const TimeZoneRule*[ruleCount];
        tz->getTimeZoneRules(initial, trsrules, ruleCount, status);
        if (U_FAILURE(status)) {
            errln((UnicodeString)"FAIL: failed to get the TimeZoneRules from time zone " + *tzid);
        }
        RuleBasedTimeZone *rbtz = new RuleBasedTimeZone(*tzid, initial->clone());
        if (U_FAILURE(status)) {
            errln((UnicodeString)"FAIL: failed to get the transition rule count from time zone " + *tzid);
        }
        for (int32_t i = 0; i < ruleCount; i++) {
            rbtz->addTransitionRule(trsrules[i]->clone(), status);
            if (U_FAILURE(status)) {
                errln((UnicodeString)"FAIL: failed to add a transition rule at index " + i + " to the RBTZ for " + *tzid);
            }
        }
        rbtz->complete(status);
        if (U_FAILURE(status)) {
            errln((UnicodeString)"FAIL: complete() failed for the RBTZ for " + *tzid);
        }

        for (int32_t idx = 0; STARTYEARS[idx] != 0; idx++) {
            UDate start = getUTCMillis(STARTYEARS[idx], UCAL_JANUARY, 1);
            UDate until = getUTCMillis(STARTYEARS[idx] + 20, UCAL_JANUARY, 1);
            // Compare the original OlsonTimeZone with the RBTZ starting the startTime for 20 years

            // Ascending
            compareTransitionsAscending(*tz, *rbtz, start, until, FALSE);
            // Ascending/inclusive
            compareTransitionsAscending(*tz, *rbtz, start + 1, until, TRUE);
            // Descending
            compareTransitionsDescending(*tz, *rbtz, start, until, FALSE);
            // Descending/inclusive
            compareTransitionsDescending(*tz, *rbtz, start + 1, until, TRUE);
        }
        delete [] trsrules;
        delete rbtz;
        delete tz;
    }
}

void
TimeZoneRuleTest::TestHasEquivalentTransitions(void) {
    // America/New_York and America/Indiana/Indianapolis are equivalent
    // since 2006
    UErrorCode status = U_ZERO_ERROR;
    BasicTimeZone *newyork = (BasicTimeZone*)TimeZone::createTimeZone("America/New_York");
    BasicTimeZone *indianapolis = (BasicTimeZone*)TimeZone::createTimeZone("America/Indiana/Indianapolis");
    BasicTimeZone *gmt_5 = (BasicTimeZone*)TimeZone::createTimeZone("Etc/GMT+5");

    UDate jan1_1971 = getUTCMillis(1971, UCAL_JANUARY, 1);
    UDate jan1_2005 = getUTCMillis(2005, UCAL_JANUARY, 1);
    UDate jan1_2006 = getUTCMillis(2006, UCAL_JANUARY, 1);
    UDate jan1_2007 = getUTCMillis(2007, UCAL_JANUARY, 1);
    UDate jan1_2011 = getUTCMillis(2010, UCAL_JANUARY, 1);

    if (newyork->hasEquivalentTransitions(*indianapolis, jan1_2005, jan1_2011, TRUE, status)) {
        errln("FAIL: New_York is not equivalent to Indianapolis between 2005 and 2010");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error status is returned from hasEquivalentTransition");
    }
    if (!newyork->hasEquivalentTransitions(*indianapolis, jan1_2006, jan1_2011, TRUE, status)) {
        errln("FAIL: New_York is equivalent to Indianapolis between 2006 and 2010");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error status is returned from hasEquivalentTransition");
    }

    if (!indianapolis->hasEquivalentTransitions(*gmt_5, jan1_1971, jan1_2006, TRUE, status)) {
        errln("FAIL: Indianapolis is equivalent to GMT+5 between 1971 and 2005");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error status is returned from hasEquivalentTransition");
    }
    if (indianapolis->hasEquivalentTransitions(*gmt_5, jan1_1971, jan1_2007, TRUE, status)) {
        errln("FAIL: Indianapolis is not equivalent to GMT+5 between 1971 and 2006");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error status is returned from hasEquivalentTransition");
    }

    // Cloned TimeZone
    BasicTimeZone *newyork2 = (BasicTimeZone*)newyork->clone();
    if (!newyork->hasEquivalentTransitions(*newyork2, jan1_1971, jan1_2011, FALSE, status)) {
        errln("FAIL: Cloned TimeZone must have the same transitions");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error status is returned from hasEquivalentTransition for newyork/newyork2");
    }
    if (!newyork->hasEquivalentTransitions(*newyork2, jan1_1971, jan1_2011, TRUE, status)) {
        errln("FAIL: Cloned TimeZone must have the same transitions");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error status is returned from hasEquivalentTransition for newyork/newyork2");
    }

    // America/New_York and America/Los_Angeles has same DST start rules, but
    // raw offsets are different
    BasicTimeZone *losangeles = (BasicTimeZone*)TimeZone::createTimeZone("America/Los_Angeles");
    if (newyork->hasEquivalentTransitions(*losangeles, jan1_2006, jan1_2011, TRUE, status)) {
        errln("FAIL: New_York is not equivalent to Los Angeles, but returned true");
    }
    if (U_FAILURE(status)) {
        errln("FAIL: error status is returned from hasEquivalentTransition for newyork/losangeles");
    }

    delete newyork;
    delete newyork2;
    delete indianapolis;
    delete gmt_5;
    delete losangeles;
}

/*
 * Write out time zone rules of OlsonTimeZone into VTIMEZONE format, create a new
 * VTimeZone from the VTIMEZONE data, then compare transitions
 */
void
TimeZoneRuleTest::TestVTimeZoneRoundTrip(void) {
    UDate startTime = getUTCMillis(1850, UCAL_JANUARY, 1);
    UDate endTime = getUTCMillis(2050, UCAL_JANUARY, 1);

    UErrorCode status = U_ZERO_ERROR;
    TestZIDEnumeration tzenum(!quick);
    while (TRUE) {
        const UnicodeString *tzid = tzenum.snext(status);
        if (tzid == NULL) {
            break;
        }
        if (U_FAILURE(status)) {
            errln("FAIL: error returned while enumerating timezone IDs.");
            break;
        }
        BasicTimeZone *tz = (BasicTimeZone*)TimeZone::createTimeZone(*tzid);
        VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
        vtz_org->setTZURL("http://source.icu-project.org/timezone");
        vtz_org->setLastModified(Calendar::getNow());
        VTimeZone *vtz_new = NULL;
        UnicodeString vtzdata;
        // Write out VTIMEZONE data
        vtz_org->write(vtzdata, status);
        if (U_FAILURE(status)) {
            errln((UnicodeString)"FAIL: error returned while writing time zone rules for " +
                *tzid + " into VTIMEZONE format.");
        } else {
            // Read VTIMEZONE data
            vtz_new = VTimeZone::createVTimeZone(vtzdata, status);
            if (U_FAILURE(status)) {
                errln((UnicodeString)"FAIL: error returned while reading VTIMEZONE data for " + *tzid);
            } else {
                // Write out VTIMEZONE one more time
                UnicodeString vtzdata1;
                vtz_new->write(vtzdata1, status);
                if (U_FAILURE(status)) {
                    errln((UnicodeString)"FAIL: error returned while writing time zone rules for " +
                        *tzid + "(vtz_new) into VTIMEZONE format.");
                } else {
                    // Make sure VTIMEZONE data is exactly same with the first one
                    if (vtzdata != vtzdata1) {
                        errln((UnicodeString)"FAIL: different VTIMEZONE data after round trip for " + *tzid);
                    }
                }
                // Check equivalency after the first transition.
                // The DST information before the first transition might be lost
                // because there is no good way to represent the initial time with
                // VTIMEZONE.
                int32_t raw1, raw2, dst1, dst2;
                tz->getOffset(startTime, FALSE, raw1, dst1, status);
                vtz_new->getOffset(startTime, FALSE, raw2, dst2, status);
                if (U_FAILURE(status)) {
                    errln("FAIL: error status is returned from getOffset");
                } else {
                    if (raw1 + dst1 != raw2 + dst2) {
                        errln("FAIL: VTimeZone for " + *tzid +
                            " is not equivalent to its OlsonTimeZone corresponding at "
                            + dateToString(startTime));
                    }
                    TimeZoneTransition trans;
                    UBool avail = tz->getNextTransition(startTime, FALSE, trans);
                    if (avail) {
                        if (!vtz_new->hasEquivalentTransitions(*tz, trans.getTime(),
                                endTime, TRUE, status)) {
                            errln("FAIL: VTimeZone for " + *tzid +
                                " is not equivalent to its OlsonTimeZone corresponding.");
                        }
                        if (U_FAILURE(status)) {
                            errln("FAIL: error status is returned from hasEquivalentTransition");
                        }
                    }
                }
            }
            if (vtz_new != NULL) {
                delete vtz_new;
                vtz_new = NULL;
            }
        }
        delete tz;
        delete vtz_org;
    }
}

/*
 * Write out time zone rules of OlsonTimeZone after a cutover date into VTIMEZONE format,
 * create a new VTimeZone from the VTIMEZONE data, then compare transitions
 */
void
TimeZoneRuleTest::TestVTimeZoneRoundTripPartial(void) {
    const int32_t STARTYEARS[] = {
        1900,
        1950,
        2020,
        0
    };
    UDate endTime = getUTCMillis(2050, UCAL_JANUARY, 1);

    UErrorCode status = U_ZERO_ERROR;
    TestZIDEnumeration tzenum(!quick);
    while (TRUE) {
        const UnicodeString *tzid = tzenum.snext(status);
        if (tzid == NULL) {
            break;
        }
        if (U_FAILURE(status)) {
            errln("FAIL: error returned while enumerating timezone IDs.");
            break;
        }
        BasicTimeZone *tz = (BasicTimeZone*)TimeZone::createTimeZone(*tzid);
        VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
        VTimeZone *vtz_new = NULL;
        UnicodeString vtzdata;

        for (int32_t i = 0; STARTYEARS[i] != 0; i++) {
            // Write out VTIMEZONE
            UDate startTime = getUTCMillis(STARTYEARS[i], UCAL_JANUARY, 1);
            vtz_org->write(startTime, vtzdata, status);
            if (U_FAILURE(status)) {
                errln((UnicodeString)"FAIL: error returned while writing time zone rules for " +
                    *tzid + " into VTIMEZONE format since " + dateToString(startTime));
            } else {
                // Read VTIMEZONE data
                vtz_new = VTimeZone::createVTimeZone(vtzdata, status);
                if (U_FAILURE(status)) {
                    errln((UnicodeString)"FAIL: error returned while reading VTIMEZONE data for " + *tzid
                        + " since " + dateToString(startTime));
                } else {
                    // Check equivalency after the first transition.
                    // The DST information before the first transition might be lost
                    // because there is no good way to represent the initial time with
                    // VTIMEZONE.
                    int32_t raw1, raw2, dst1, dst2;
                    tz->getOffset(startTime, FALSE, raw1, dst1, status);
                    vtz_new->getOffset(startTime, FALSE, raw2, dst2, status);
                    if (U_FAILURE(status)) {
                        errln("FAIL: error status is returned from getOffset");
                    } else {
                        if (raw1 + dst1 != raw2 + dst2) {
                            errln("FAIL: VTimeZone for " + *tzid +
                                " is not equivalent to its OlsonTimeZone corresponding at "
                                + dateToString(startTime));
                        }
                        TimeZoneTransition trans;
                        UBool avail = tz->getNextTransition(startTime, FALSE, trans);
                        if (avail) {
                            if (!vtz_new->hasEquivalentTransitions(*tz, trans.getTime(),
                                    endTime, TRUE, status)) {
                                errln("FAIL: VTimeZone for " + *tzid +
                                    " is not equivalent to its OlsonTimeZone corresponding.");
                            }
                            if (U_FAILURE(status)) {
                                errln("FAIL: error status is returned from hasEquivalentTransition");
                            }
                        }
                    }
                }
            }
            if (vtz_new != NULL) {
                delete vtz_new;
                vtz_new = NULL;
            }
        }
        delete tz;
        delete vtz_org;
    }
}

/*
 * Write out simple time zone rules from an OlsonTimeZone at various time into VTIMEZONE
 * format and create a new VTimeZone from the VTIMEZONE data, then make sure the raw offset
 * and DST savings are same in these two time zones.
 */
void
TimeZoneRuleTest::TestVTimeZoneSimpleWrite(void) {
    const int32_t TESTDATES[][3] = {
        {2006,  UCAL_JANUARY,   1},
        {2006,  UCAL_MARCH,     15},
        {2006,  UCAL_MARCH,     31},
        {2006,  UCAL_OCTOBER,   25},
        {2006,  UCAL_NOVEMBER,  1},
        {2006,  UCAL_NOVEMBER,  5},
        {2007,  UCAL_JANUARY,   1},
        {0,     0,              0}
    };

    UErrorCode status = U_ZERO_ERROR;
    TestZIDEnumeration tzenum(!quick);
    while (TRUE) {
        const UnicodeString *tzid = tzenum.snext(status);
        if (tzid == NULL) {
            break;
        }
        if (U_FAILURE(status)) {
            errln("FAIL: error returned while enumerating timezone IDs.");
            break;
        }
        VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
        VTimeZone *vtz_new = NULL;
        UnicodeString vtzdata;

        for (int32_t i = 0; TESTDATES[i][0] != 0; i++) {
            // Write out VTIMEZONE
            UDate time = getUTCMillis(TESTDATES[i][0], TESTDATES[i][1], TESTDATES[i][2]);
            vtz_org->writeSimple(time, vtzdata, status);
            if (U_FAILURE(status)) {
                errln((UnicodeString)"FAIL: error returned while writing simple time zone rules for " +
                    *tzid + " into VTIMEZONE format at " + dateToString(time));
            } else {
                // Read VTIMEZONE data
                vtz_new = VTimeZone::createVTimeZone(vtzdata, status);
                if (U_FAILURE(status)) {
                    errln((UnicodeString)"FAIL: error returned while reading simple VTIMEZONE data for " + *tzid
                        + " at " + dateToString(time));
                } else {
                    // Check equivalency
                    int32_t raw0, dst0;
                    int32_t raw1, dst1;
                    vtz_org->getOffset(time, FALSE, raw0, dst0, status);
                    vtz_new->getOffset(time, FALSE, raw1, dst1, status);
                    if (U_SUCCESS(status)) {
                        if (raw0 != raw1 || dst0 != dst1) {
                            errln("FAIL: VTimeZone writeSimple for " + *tzid + " at "
                                + dateToString(time) + " failed to the round trip.");
                        }
                    } else {
                        errln("FAIL: getOffset returns error status");
                    }
                }
            }
            if (vtz_new != NULL) {
                delete vtz_new;
                vtz_new = NULL;
            }
        }
        delete vtz_org;
    }
}

/*
 * Write out time zone rules of OlsonTimeZone into VTIMEZONE format with RFC2445 header TZURL and
 * LAST-MODIFIED, create a new VTimeZone from the VTIMEZONE data to see if the headers are preserved.
 */
void
TimeZoneRuleTest::TestVTimeZoneHeaderProps(void) {
    const UnicodeString TESTURL1("http://source.icu-project.org");
    const UnicodeString TESTURL2("http://www.ibm.com");

    UErrorCode status = U_ZERO_ERROR;
    UnicodeString tzurl;
    UDate lmod;
    UDate lastmod = getUTCMillis(2007, UCAL_JUNE, 1);
    VTimeZone *vtz = VTimeZone::createVTimeZoneByID("America/Chicago");
    vtz->setTZURL(TESTURL1);
    vtz->setLastModified(lastmod);

    // Roundtrip conversion
    UnicodeString vtzdata;
    vtz->write(vtzdata, status);
    VTimeZone *newvtz1 = NULL;
    if (U_FAILURE(status)) {
        errln("FAIL: error returned while writing VTIMEZONE data 1");
        return;
    }
    // Create a new one
    newvtz1 = VTimeZone::createVTimeZone(vtzdata, status);
    if (U_FAILURE(status)) {
        errln("FAIL: error returned while loading VTIMEZONE data 1");
    } else {
        // Check if TZURL and LAST-MODIFIED properties are preserved
        newvtz1->getTZURL(tzurl);
        if (tzurl != TESTURL1) {
            errln("FAIL: TZURL 1 was not preserved");
        }
        vtz->getLastModified(lmod);
        if (lastmod != lmod) {
            errln("FAIL: LAST-MODIFIED was not preserved");
        }
    }

    if (U_SUCCESS(status)) {
        // Set different tzurl
        newvtz1->setTZURL(TESTURL2);

        // Second roundtrip, with a cutover
        newvtz1->write(vtzdata, status);
        if (U_FAILURE(status)) {
            errln("FAIL: error returned while writing VTIMEZONE data 2");
        } else {
            VTimeZone *newvtz2 = VTimeZone::createVTimeZone(vtzdata, status);
            if (U_FAILURE(status)) {
                errln("FAIL: error returned while loading VTIMEZONE data 2");
            } else {
                // Check if TZURL and LAST-MODIFIED properties are preserved
                newvtz2->getTZURL(tzurl);
                if (tzurl != TESTURL2) {
                    errln("FAIL: TZURL was not preserved in the second roundtrip");
                }
                vtz->getLastModified(lmod);
                if (lastmod != lmod) {
                    errln("FAIL: LAST-MODIFIED was not preserved in the second roundtrip");
                }
            }
            delete newvtz2;
        }
    }
    delete newvtz1;
    delete vtz;
}

/*
 * Extract simple rules from an OlsonTimeZone and make sure the rule format matches
 * the expected format.
 */
void
TimeZoneRuleTest::TestGetSimpleRules(void) {
    UDate testTimes[] = {
        getUTCMillis(1970, UCAL_JANUARY, 1),
        getUTCMillis(2000, UCAL_MARCH, 31),
        getUTCMillis(2005, UCAL_JULY, 1),
        getUTCMillis(2010, UCAL_NOVEMBER, 1),        
    };
    int32_t numTimes = sizeof(testTimes)/sizeof(UDate);
    UErrorCode status = U_ZERO_ERROR;
    TestZIDEnumeration tzenum(!quick);
    InitialTimeZoneRule *initial;
    AnnualTimeZoneRule *std, *dst;
    for (int32_t i = 0; i < numTimes ; i++) {
        while (TRUE) {
            const UnicodeString *tzid = tzenum.snext(status);
            if (tzid == NULL) {
                break;
            }
            if (U_FAILURE(status)) {
                errln("FAIL: error returned while enumerating timezone IDs.");
                break;
            }
            BasicTimeZone *tz = (BasicTimeZone*)TimeZone::createTimeZone(*tzid);
            initial = NULL;
            std = dst = NULL;
            tz->getSimpleRulesNear(testTimes[i], initial, std, dst, status);
            if (U_FAILURE(status)) {
                errln("FAIL: getSimpleRules failed.");
                break;
            }
            if (initial == NULL) {
                errln("FAIL: initial rule must not be NULL");
                break;
            } else if (!(std == NULL && dst == NULL || std != NULL && dst != NULL)) {
                errln("FAIL: invalid std/dst pair.");
                break;
            }
            if (std != NULL) {
                const DateTimeRule *dtr = std->getRule();
                if (dtr->getDateRuleType() != DateTimeRule::DOW) {
                    errln("FAIL: simple std rull must use DateTimeRule::DOW as date rule.");
                    break;
                }
                if (dtr->getTimeRuleType() != DateTimeRule::WALL_TIME) {
                    errln("FAIL: simple std rull must use DateTimeRule::WALL_TIME as time rule.");
                    break;
                }
                dtr = dst->getRule();
                if (dtr->getDateRuleType() != DateTimeRule::DOW) {
                    errln("FAIL: simple dst rull must use DateTimeRule::DOW as date rule.");
                    break;
                }
                if (dtr->getTimeRuleType() != DateTimeRule::WALL_TIME) {
                    errln("FAIL: simple dst rull must use DateTimeRule::WALL_TIME as time rule.");
                    break;
                }                
            }
            // Create an RBTZ from the rules and compare the offsets at the date
            RuleBasedTimeZone *rbtz = new RuleBasedTimeZone(*tzid, initial);
            if (std != NULL) {
                rbtz->addTransitionRule(std, status);
                if (U_FAILURE(status)) {
                    errln("FAIL: couldn't add std rule.");
                }
                rbtz->addTransitionRule(dst, status);
                if (U_FAILURE(status)) {
                    errln("FAIL: couldn't add dst rule.");
                }
            }
            rbtz->complete(status);
            if (U_FAILURE(status)) {
                errln("FAIL: couldn't complete rbtz for " + *tzid);
            }

            int32_t raw0, dst0, raw1, dst1;
            tz->getOffset(testTimes[i], FALSE, raw0, dst0, status);
            if (U_FAILURE(status)) {
                errln("FAIL: couldn't get offsets from tz for " + *tzid);
            }
            rbtz->getOffset(testTimes[i], FALSE, raw1, dst1, status);
            if (U_FAILURE(status)) {
                errln("FAIL: couldn't get offsets from rbtz for " + *tzid);
            }
            if (raw0 != raw1 || dst0 != dst1) {
                errln("FAIL: rbtz created by simple rule does not match the original tz for tzid " + *tzid);
            }
            delete rbtz;
            delete tz;
        }
    }
}

/*
 * API coverage tests for TimeZoneRule 
 */
void
TimeZoneRuleTest::TestTimeZoneRuleCoverage(void) {
    UDate time1 = getUTCMillis(2005, UCAL_JULY, 4);
    UDate time2 = getUTCMillis(2015, UCAL_JULY, 4);
    UDate time3 = getUTCMillis(1950, UCAL_JULY, 4);

    DateTimeRule *dtr1 = new DateTimeRule(UCAL_FEBRUARY, 29, UCAL_SUNDAY, FALSE,
            3*HOUR, DateTimeRule::WALL_TIME); // Last Sunday on or before Feb 29, at 3 AM, wall time
    DateTimeRule *dtr2 = new DateTimeRule(UCAL_MARCH, 11, 2*HOUR,
            DateTimeRule::STANDARD_TIME); // Mar 11, at 2 AM, standard time
    DateTimeRule *dtr3 = new DateTimeRule(UCAL_OCTOBER, -1, UCAL_SATURDAY,
            6*HOUR, DateTimeRule::UTC_TIME); //Last Saturday in Oct, at 6 AM, UTC
    DateTimeRule *dtr4 = new DateTimeRule(UCAL_MARCH, 8, UCAL_SUNDAY, TRUE,
            2*HOUR, DateTimeRule::WALL_TIME); // First Sunday on or after Mar 8, at 2 AM, wall time

    AnnualTimeZoneRule *a1 = new AnnualTimeZoneRule("a1", -3*HOUR, 1*HOUR, *dtr1,
            2000, AnnualTimeZoneRule::MAX_YEAR);
    AnnualTimeZoneRule *a2 = new AnnualTimeZoneRule("a2", -3*HOUR, 1*HOUR, *dtr1,
            2000, AnnualTimeZoneRule::MAX_YEAR);
    AnnualTimeZoneRule *a3 = new AnnualTimeZoneRule("a3", -3*HOUR, 1*HOUR, *dtr1,
            2000, 2010);

    InitialTimeZoneRule *i1 = new InitialTimeZoneRule("i1", -3*HOUR, 0);
    InitialTimeZoneRule *i2 = new InitialTimeZoneRule("i2", -3*HOUR, 0);
    InitialTimeZoneRule *i3 = new InitialTimeZoneRule("i3", -3*HOUR, 1*HOUR);

    UDate trtimes1[] = {0.0};
    UDate trtimes2[] = {0.0, 10000000.0};

    TimeArrayTimeZoneRule *t1 = new TimeArrayTimeZoneRule("t1", -3*HOUR, 0, trtimes1, 1, DateTimeRule::UTC_TIME);
    TimeArrayTimeZoneRule *t2 = new TimeArrayTimeZoneRule("t2", -3*HOUR, 0, trtimes1, 1, DateTimeRule::UTC_TIME);
    TimeArrayTimeZoneRule *t3 = new TimeArrayTimeZoneRule("t3", -3*HOUR, 0, trtimes2, 2, DateTimeRule::UTC_TIME);
    TimeArrayTimeZoneRule *t4 = new TimeArrayTimeZoneRule("t4", -3*HOUR, 0, trtimes1, 1, DateTimeRule::STANDARD_TIME);
    TimeArrayTimeZoneRule *t5 = new TimeArrayTimeZoneRule("t5", -4*HOUR, 1*HOUR, trtimes1, 1, DateTimeRule::WALL_TIME);

    // DateTimeRule::operator=/clone
    DateTimeRule dtr0(UCAL_MAY, 31, 2*HOUR, DateTimeRule::WALL_TIME);
    if (dtr0 == *dtr1 || !(dtr0 != *dtr1)) {
        errln("FAIL: DateTimeRule dtr0 is not equal to dtr1, but got wrong result");
    }
    dtr0 = *dtr1;
    if (dtr0 != *dtr1 || !(dtr0 == *dtr1)) {
        errln("FAIL: DateTimeRule dtr0 is equal to dtr1, but got wrong result");
    }
    DateTimeRule *dtr0c = dtr0.clone();
    if (*dtr0c != *dtr1 || !(*dtr0c == *dtr1)) {
        errln("FAIL: DateTimeRule dtr0c is equal to dtr1, but got wrong result");
    }
    delete dtr0c;

    // AnnualTimeZonerule::operator=/clone
    AnnualTimeZoneRule a0("a0", 5*HOUR, 1*HOUR, *dtr1, 1990, AnnualTimeZoneRule::MAX_YEAR);
    if (a0 == *a1 || !(a0 != *a1)) {
        errln("FAIL: AnnualTimeZoneRule a0 is not equal to a1, but got wrong result");
    }
    a0 = *a1;
    if (a0 != *a1 || !(a0 == *a1)) {
        errln("FAIL: AnnualTimeZoneRule a0 is equal to a1, but got wrong result");
    }
    AnnualTimeZoneRule *a0c = a0.clone();
    if (*a0c != *a1 || !(*a0c == *a1)) {
        errln("FAIL: AnnualTimeZoneRule a0c is equal to a1, but got wrong result");
    }
    delete a0c;

    // AnnualTimeZoneRule::getRule
    if (*(a1->getRule()) != *(a2->getRule())) {
        errln("FAIL: The same DateTimeRule must be returned from AnnualTimeZoneRule a1 and a2");
    }

    // AnnualTimeZoneRule::getStartYear
    int32_t startYear = a1->getStartYear();
    if (startYear != 2000) {
        errln((UnicodeString)"FAIL: The start year of AnnualTimeZoneRule a1 must be 2000 - returned: " + startYear);
    }

    // AnnualTimeZoneRule::getEndYear
    int32_t endYear = a1->getEndYear();
    if (endYear != AnnualTimeZoneRule::MAX_YEAR) {
        errln((UnicodeString)"FAIL: The start year of AnnualTimeZoneRule a1 must be MAX_YEAR - returned: " + endYear);
    }
    endYear = a3->getEndYear();
    if (endYear != 2010) {
        errln((UnicodeString)"FAIL: The start year of AnnualTimeZoneRule a3 must be 2010 - returned: " + endYear);
    }

    // AnnualTimeZone::getStartInYear
    UBool b1, b2;
    UDate d1, d2;
    b1 = a1->getStartInYear(2005, -3*HOUR, 0, d1);
    b2 = a3->getStartInYear(2005, -3*HOUR, 0, d2);
    if (!b1 || !b2 || d1 != d2) {
        errln("FAIL: AnnualTimeZoneRule::getStartInYear did not work as expected");
    }
    b2 = a3->getStartInYear(2015, -3*HOUR, 0, d2);
    if (b2) {
        errln("FAIL: AnnualTimeZoneRule::getStartInYear returned TRUE for 2015 which is out of rule range");
    }

    // AnnualTimeZone::getFirstStart
    b1 = a1->getFirstStart(-3*HOUR, 0, d1);
    b2 = a1->getFirstStart(-4*HOUR, 1*HOUR, d2);
    if (!b1 || !b2 || d1 != d2) {
        errln("FAIL: The same start time should be returned by getFirstStart");
    }

    // AnnualTimeZone::getFinalStart
    b1 = a1->getFinalStart(-3*HOUR, 0, d1);
    if (b1) {
        errln("FAIL: getFinalStart returned TRUE for a1");
    }
    b1 = a1->getStartInYear(2010, -3*HOUR, 0, d1);
    b2 = a3->getFinalStart(-3*HOUR, 0, d2);
    if (!b1 || !b2 || d1 != d2) {
        errln("FAIL: Bad date is returned by getFinalStart");
    }

    // AnnualTimeZone::getNextStart / getPreviousStart
    b1 = a1->getNextStart(time1, -3*HOUR, 0, FALSE, d1);
    if (!b1) {
        errln("FAIL: getNextStart returned FALSE for ai");
    } else {
        b2 = a1->getPreviousStart(d1, -3*HOUR, 0, TRUE, d2);
        if (!b2 || d1 != d2) {
            errln("FAIL: Bad Date is returned by getPreviousStart");
        }
    }
    b1 = a3->getNextStart(time2, -3*HOUR, 0, FALSE, d1);
    if (b1) {
        errln("FAIL: getNextStart must return FALSE when no start time is available after the base time");
    }
    b1 = a3->getFinalStart(-3*HOUR, 0, d1);
    b2 = a3->getPreviousStart(time2, -3*HOUR, 0, FALSE, d2);
    if (!b1 || !b2 || d1 != d2) {
        errln("FAIL: getPreviousStart does not match with getFinalStart after the end year");
    }

    // AnnualTimeZone::isEquavalentTo
    if (!a1->isEquivalentTo(*a2)) {
        errln("FAIL: AnnualTimeZoneRule a1 is equivalent to a2, but returned FALSE");
    }
    if (a1->isEquivalentTo(*a3)) {
        errln("FAIL: AnnualTimeZoneRule a1 is not equivalent to a3, but returned TRUE");
    }
    if (!a1->isEquivalentTo(*a1)) {
        errln("FAIL: AnnualTimeZoneRule a1 is equivalent to itself, but returned FALSE");
    }
    if (a1->isEquivalentTo(*t1)) {
        errln("FAIL: AnnualTimeZoneRule is not equivalent to TimeArrayTimeZoneRule, but returned TRUE");
    }

    // InitialTimezoneRule::operator=/clone
    InitialTimeZoneRule i0("i0", 10*HOUR, 0);
    if (i0 == *i1 || !(i0 != *i1)) {
        errln("FAIL: InitialTimeZoneRule i0 is not equal to i1, but got wrong result");
    }
    i0 = *i1;
    if (i0 != *i1 || !(i0 == *i1)) {
        errln("FAIL: InitialTimeZoneRule i0 is equal to i1, but got wrong result");
    }
    InitialTimeZoneRule *i0c = i0.clone();
    if (*i0c != *i1 || !(*i0c == *i1)) {
        errln("FAIL: InitialTimeZoneRule i0c is equal to i1, but got wrong result");
    }
    delete i0c;

    // InitialTimeZoneRule::isEquivalentRule
    if (!i1->isEquivalentTo(*i2)) {
        errln("FAIL: InitialTimeZoneRule i1 is equivalent to i2, but returned FALSE");
    }
    if (i1->isEquivalentTo(*i3)) {
        errln("FAIL: InitialTimeZoneRule i1 is not equivalent to i3, but returned TRUE");
    }
    if (i1->isEquivalentTo(*a1)) {
        errln("FAIL: An InitialTimeZoneRule is not equivalent to an AnnualTimeZoneRule, but returned TRUE");
    }

    // InitialTimeZoneRule::getFirstStart/getFinalStart/getNextStart/getPreviousStart
    b1 = i1->getFirstStart(0, 0, d1);
    if (b1) {
        errln("FAIL: InitialTimeZone::getFirstStart returned TRUE");
    }
    b1 = i1->getFinalStart(0, 0, d1);
    if (b1) {
        errln("FAIL: InitialTimeZone::getFinalStart returned TRUE");
    }
    b1 = i1->getNextStart(time1, 0, 0, FALSE, d1);
    if (b1) {
        errln("FAIL: InitialTimeZone::getNextStart returned TRUE");
    }
    b1 = i1->getPreviousStart(time1, 0, 0, FALSE, d1);
    if (b1) {
        errln("FAIL: InitialTimeZone::getPreviousStart returned TRUE");
    }

    // TimeArrayTimeZoneRule::operator=/clone
    TimeArrayTimeZoneRule t0("t0", 4*HOUR, 0, trtimes1, 1, DateTimeRule::UTC_TIME);
    if (t0 == *t1 || !(t0 != *t1)) {
        errln("FAIL: TimeArrayTimeZoneRule t0 is not equal to t1, but got wrong result");
    }
    t0 = *t1;
    if (t0 != *t1 || !(t0 == *t1)) {
        errln("FAIL: TimeArrayTimeZoneRule t0 is equal to t1, but got wrong result");
    }
    TimeArrayTimeZoneRule *t0c = t0.clone();
    if (*t0c != *t1 || !(*t0c == *t1)) {
        errln("FAIL: TimeArrayTimeZoneRule t0c is equal to t1, but got wrong result");
    }
    delete t0c;

    // TimeArrayTimeZoneRule::countStartTimes
    if (t1->countStartTimes() != 1) {
        errln("FAIL: Bad start time count is returned by TimeArrayTimeZoneRule::countStartTimes");
    }

    // TimeArrayTimeZoneRule::getStartTimeAt
    b1 = t1->getStartTimeAt(-1, d1);
    if (b1) {
        errln("FAIL: TimeArrayTimeZoneRule::getStartTimeAt returned TRUE for index -1");
    }
    b1 = t1->getStartTimeAt(0, d1);
    if (!b1 || d1 != trtimes1[0]) {
        errln("FAIL: TimeArrayTimeZoneRule::getStartTimeAt returned incorrect result for index 0");
    }
    b1 = t1->getStartTimeAt(1, d1);
    if (b1) {
        errln("FAIL: TimeArrayTimeZoneRule::getStartTimeAt returned TRUE for index 1");
    }

    // TimeArrayTimeZoneRule::getTimeType
    if (t1->getTimeType() != DateTimeRule::UTC_TIME) {
        errln("FAIL: TimeArrayTimeZoneRule t1 uses UTC_TIME, but different type is returned");
    }
    if (t4->getTimeType() != DateTimeRule::STANDARD_TIME) {
        errln("FAIL: TimeArrayTimeZoneRule t4 uses STANDARD_TIME, but different type is returned");
    }
    if (t5->getTimeType() != DateTimeRule::WALL_TIME) {
        errln("FAIL: TimeArrayTimeZoneRule t5 uses WALL_TIME, but different type is returned");
    }

    // TimeArrayTimeZoneRule::getFirstStart/getFinalStart
    b1 = t1->getFirstStart(0, 0, d1);
    if (!b1 || d1 != trtimes1[0]) {
        errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t1");
    }
    b1 = t1->getFinalStart(0, 0, d1);
    if (!b1 || d1 != trtimes1[0]) {
        errln("FAIL: Bad final start time returned from TimeArrayTimeZoneRule t1");
    }
    b1 = t4->getFirstStart(-4*HOUR, 1*HOUR, d1);
    if (!b1 || d1 != (trtimes1[0] + 4*HOUR)) {
        errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t4");
    }
    b1 = t5->getFirstStart(-4*HOUR, 1*HOUR, d1);
    if (!b1 || d1 != (trtimes1[0] + 3*HOUR)) {
        errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t5");
    }

    // TimeArrayTimeZoneRule::getNextStart/getPreviousStart
    b1 = t3->getNextStart(time1, -3*HOUR, 1*HOUR, FALSE, d1);
    if (b1) {
        errln("FAIL: getNextStart returned TRUE after the final transition for t3");
    }
    b1 = t3->getPreviousStart(time1, -3*HOUR, 1*HOUR, FALSE, d1);
    if (!b1 || d1 != trtimes2[1]) {
        errln("FAIL: Bad start time returned by getPreviousStart for t3");
    } else {
        b2 = t3->getPreviousStart(d1, -3*HOUR, 1*HOUR, FALSE, d2);
        if (!b2 || d2 != trtimes2[0]) {
            errln("FAIL: Bad start time returned by getPreviousStart for t3");
        }
    }
    b1 = t3->getPreviousStart(time3, -3*HOUR, 1*HOUR, FALSE, d1); //time3 - year 1950, no result expected
    if (b1) {
        errln("FAIL: getPreviousStart returned TRUE before the first transition for t3");
    }

    // TimeArrayTimeZoneRule::isEquivalentTo
    if (!t1->isEquivalentTo(*t2)) {
        errln("FAIL: TimeArrayTimeZoneRule t1 is equivalent to t2, but returned FALSE");
    }
    if (t1->isEquivalentTo(*t3)) {
        errln("FAIL: TimeArrayTimeZoneRule t1 is not equivalent to t3, but returned TRUE");
    }
    if (t1->isEquivalentTo(*t4)) {
        errln("FAIL: TimeArrayTimeZoneRule t1 is not equivalent to t4, but returned TRUE");
    }
    if (t1->isEquivalentTo(*a1)) {
        errln("FAIL: TimeArrayTimeZoneRule is not equivalent to AnnualTimeZoneRule, but returned TRUE");
    }

    delete dtr1;
    delete dtr2;
    delete dtr3;
    delete dtr4;
    delete a1;
    delete a2;
    delete a3;
    delete i1;
    delete i2;
    delete i3;
    delete t1;
    delete t2;
    delete t3;
    delete t4;
    delete t5;
}

/*
 * API coverage test for BasicTimeZone APIs in SimpleTimeZone
 */
void
TimeZoneRuleTest::TestSimpleTimeZoneCoverage(void) {
    UDate time1 = getUTCMillis(1990, UCAL_JUNE, 1);
    UDate time2 = getUTCMillis(2000, UCAL_JUNE, 1);

    TimeZoneTransition tzt1, tzt2;
    UBool avail1, avail2;
    UErrorCode status = U_ZERO_ERROR;
    const TimeZoneRule *trrules[2];
    const InitialTimeZoneRule *ir = NULL;
    int32_t numTzRules;

    // BasicTimeZone API implementation in SimpleTimeZone
    SimpleTimeZone *stz1 = new SimpleTimeZone(-5*HOUR, "GMT-5");

    avail1 = stz1->getNextTransition(time1, FALSE, tzt1);
    if (avail1) {
        errln("FAIL: No transition must be returned by getNextTranstion for SimpleTimeZone with no DST rule");
    }
    avail1 = stz1->getPreviousTransition(time1, FALSE, tzt1);
    if (avail1) {
        errln("FAIL: No transition must be returned by getPreviousTransition  for SimpleTimeZone with no DST rule");
    }

    numTzRules = stz1->countTransitionRules(status);
    if (U_FAILURE(status)) {
        errln("FAIL: countTransitionRules failed");
    }
    if (numTzRules != 0) {
        errln((UnicodeString)"FAIL: countTransitionRules returned " + numTzRules);
    }
    numTzRules = 2;
    stz1->getTimeZoneRules(ir, trrules, numTzRules, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getTimeZoneRules failed");
    }
    if (numTzRules != 0) {
        errln("FAIL: Incorrect transition rule count");
    }
    if (ir == NULL || ir->getRawOffset() != stz1->getRawOffset()) {
        errln("FAIL: Bad initial time zone rule");
    }

    // Set DST rule
    stz1->setStartRule(UCAL_MARCH, 11, 2*HOUR, status); // March 11
    stz1->setEndRule(UCAL_NOVEMBER, 1, UCAL_SUNDAY, 2*HOUR, status); // First Sunday in November
    if (U_FAILURE(status)) {
        errln("FAIL: Failed to set DST rules in a SimpleTimeZone");
    }

    avail1 = stz1->getNextTransition(time1, FALSE, tzt1);
    if (!avail1) {
        errln("FAIL: Non-null transition must be returned by getNextTranstion for SimpleTimeZone with a DST rule");
    }
    avail1 = stz1->getPreviousTransition(time1, FALSE, tzt1);
    if (!avail1) {
        errln("FAIL: Non-null transition must be returned by getPreviousTransition  for SimpleTimeZone with a DST rule");
    }

    numTzRules = stz1->countTransitionRules(status);
    if (U_FAILURE(status)) {
        errln("FAIL: countTransitionRules failed");
    }
    if (numTzRules != 2) {
        errln((UnicodeString)"FAIL: countTransitionRules returned " + numTzRules);
    }

    numTzRules = 2;
    trrules[0] = NULL;
    trrules[1] = NULL;
    stz1->getTimeZoneRules(ir, trrules, numTzRules, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getTimeZoneRules failed");
    }
    if (numTzRules != 2) {
        errln("FAIL: Incorrect transition rule count");
    }
    if (ir == NULL || ir->getRawOffset() != stz1->getRawOffset()) {
        errln("FAIL: Bad initial time zone rule");
    }
    if (trrules[0] == NULL || trrules[0]->getRawOffset() != stz1->getRawOffset()) {
        errln("FAIL: Bad transition rule 0");
    }
    if (trrules[1] == NULL || trrules[1]->getRawOffset() != stz1->getRawOffset()) {
        errln("FAIL: Bad transition rule 1");
    }

    // Set DST start year
    stz1->setStartYear(2007);
    avail1 = stz1->getPreviousTransition(time1, FALSE, tzt1);
    if (avail1) {
        errln("FAIL: No transition must be returned before 1990");
    }
    avail1 = stz1->getNextTransition(time1, FALSE, tzt1); // transition after 1990-06-01
    avail2 = stz1->getNextTransition(time2, FALSE, tzt2); // transition after 2000-06-01
    if (!avail1 || !avail2 || tzt1 != tzt2) {
        errln("FAIL: Bad transition returned by SimpleTimeZone::getNextTransition");
    }
    delete stz1;
}

/*
 * API coverage test for VTimeZone
 */
void
TimeZoneRuleTest::TestVTimeZoneCoverage(void) {
    UErrorCode status = U_ZERO_ERROR;
    UnicodeString TZID("Europe/Moscow");

    BasicTimeZone *otz = (BasicTimeZone*)TimeZone::createTimeZone(TZID);
    VTimeZone *vtz = VTimeZone::createVTimeZoneByID(TZID);

    // getOffset(era, year, month, day, dayOfWeek, milliseconds, ec)
    int32_t offset1 = otz->getOffset(GregorianCalendar::AD, 2007, UCAL_JULY, 1, UCAL_SUNDAY, 0, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(7 args) failed for otz");
    }
    int32_t offset2 = vtz->getOffset(GregorianCalendar::AD, 2007, UCAL_JULY, 1, UCAL_SUNDAY, 0, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(7 args) failed for vtz");
    }
    if (offset1 != offset2) {
        errln("FAIL: getOffset(7 args) returned different results in VTimeZone and OlsonTimeZone");
    }

    // getOffset(era, year, month, day, dayOfWeek, milliseconds, monthLength, ec)
    offset1 = otz->getOffset(GregorianCalendar::AD, 2007, UCAL_JULY, 1, UCAL_SUNDAY, 0, 31, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(8 args) failed for otz");
    }
    offset2 = vtz->getOffset(GregorianCalendar::AD, 2007, UCAL_JULY, 1, UCAL_SUNDAY, 0, 31, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(8 args) failed for vtz");
    }
    if (offset1 != offset2) {
        errln("FAIL: getOffset(8 args) returned different results in VTimeZone and OlsonTimeZone");
    }


    // getOffset(date, local, rawOffset, dstOffset, ec)
    UDate t = Calendar::getNow();
    int32_t rawOffset1, dstSavings1;
    int32_t rawOffset2, dstSavings2;

    otz->getOffset(t, FALSE, rawOffset1, dstSavings1, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(5 args) failed for otz");
    }
    vtz->getOffset(t, FALSE, rawOffset2, dstSavings2, status);
    if (U_FAILURE(status)) {
        errln("FAIL: getOffset(5 args) failed for vtz");
    }
    if (rawOffset1 != rawOffset2 || dstSavings1 != dstSavings2) {
        errln("FAIL: getOffset(long,boolean,int[]) returned different results in VTimeZone and OlsonTimeZone");
    }

    // getRawOffset
    if (otz->getRawOffset() != vtz->getRawOffset()) {
        errln("FAIL: getRawOffset returned different results in VTimeZone and OlsonTimeZone");
    }

    // inDaylightTime
    UBool inDst1, inDst2;
    inDst1 = otz->inDaylightTime(t, status);
    if (U_FAILURE(status)) {
        errln("FAIL: inDaylightTime failed for otz");
    }
    inDst2 = vtz->inDaylightTime(t, status);
    if (U_FAILURE(status)) {
        errln("FAIL: inDaylightTime failed for vtz");
    }
    if (inDst1 != inDst2) {
        errln("FAIL: inDaylightTime returned different results in VTimeZone and OlsonTimeZone");
    }

    // useDaylightTime
    if (otz->useDaylightTime() != vtz->useDaylightTime()) {
        errln("FAIL: useDaylightTime returned different results in VTimeZone and OlsonTimeZone");
    }

    // setRawOffset
    const int32_t RAW = -10*HOUR;
    VTimeZone *tmpvtz = (VTimeZone*)vtz->clone();
    tmpvtz->setRawOffset(RAW);
    if (tmpvtz->getRawOffset() != RAW) {
        logln("setRawOffset is implemented in VTimeZone");
    }

    // hasSameRules
    UBool bSame = otz->hasSameRules(*vtz);
    logln((UnicodeString)"OlsonTimeZone::hasSameRules(VTimeZone) should return FALSE always for now - actual: " + bSame);

    // getTZURL/setTZURL
    UnicodeString TZURL("http://icu-project.org/timezone");
    UnicodeString url;
    if (vtz->getTZURL(url)) {
        errln("FAIL: getTZURL returned TRUE");
    }
    vtz->setTZURL(TZURL);
    if (!vtz->getTZURL(url) || url != TZURL) {
        errln("FAIL: URL returned by getTZURL does not match the one set by setTZURL");
    }

    // getLastModified/setLastModified
    UDate lastmod;
    if (vtz->getLastModified(lastmod)) {
        errln("FAIL: getLastModified returned TRUE");
    }
    vtz->setLastModified(t);
    if (!vtz->getLastModified(lastmod) || lastmod != t) {
        errln("FAIL: Date returned by getLastModified does not match the one set by setLastModified");
    }

    // getNextTransition/getPreviousTransition
    UDate base = getUTCMillis(2007, UCAL_JULY, 1);
    TimeZoneTransition tzt1, tzt2;
    UBool btr1 = otz->getNextTransition(base, TRUE, tzt1);
    UBool btr2 = vtz->getNextTransition(base, TRUE, tzt2);
    if (!btr1 || !btr2 || tzt1 != tzt2) {
        errln("FAIL: getNextTransition returned different results in VTimeZone and OlsonTimeZone");
    }
    btr1 = otz->getPreviousTransition(base, FALSE, tzt1);
    btr2 = vtz->getPreviousTransition(base, FALSE, tzt2);
    if (!btr1 || !btr2 || tzt1 != tzt2) {
        errln("FAIL: getPreviousTransition returned different results in VTimeZone and OlsonTimeZone");
    }

    // TimeZoneTransition constructor/clone
    TimeZoneTransition *tzt1c = tzt1.clone();
    if (*tzt1c != tzt1 || !(*tzt1c == tzt1)) {
        errln("FAIL: TimeZoneTransition tzt1c is equal to tzt1, but got wrong result");
    }
    delete tzt1c;
    TimeZoneTransition tzt3(tzt1);
    if (tzt3 != tzt1 || !(tzt3 == tzt1)) {
        errln("FAIL: TimeZoneTransition tzt3 is equal to tzt1, but got wrong result");
    }

    // hasEquivalentTransitions
    UDate time1 = getUTCMillis(1950, UCAL_JANUARY, 1);
    UDate time2 = getUTCMillis(2020, UCAL_JANUARY, 1);
    UBool equiv = vtz->hasEquivalentTransitions(*otz, time1, time2, FALSE, status);
    if (U_FAILURE(status)) {
        errln("FAIL: hasEquivalentTransitions failed for vtz/otz");
    }
    if (!equiv) {
        errln("FAIL: hasEquivalentTransitons returned false for the same time zone");
    }

    // operator=/operator==/operator!=
    VTimeZone *vtz1 = VTimeZone::createVTimeZoneByID("America/Los_Angeles");
    if (*vtz1 == *vtz || !(*vtz1 != *vtz)) {
        errln("FAIL: VTimeZone vtz1 is not equal to vtz, but got wrong result");
    }
    *vtz1 = *vtz;
    if (*vtz1 != *vtz || !(*vtz1 == *vtz)) {
        errln("FAIL: VTimeZone vtz1 is equal to vtz, but got wrong result");
    }

    delete otz;
    delete vtz;
    delete tmpvtz;
    delete vtz1;
}


void
TimeZoneRuleTest::TestVTimeZoneParse(void) {
    UErrorCode status = U_ZERO_ERROR;

    // Trying to create VTimeZone from empty data
    UnicodeString emptyData;
    VTimeZone *empty = VTimeZone::createVTimeZone(emptyData, status);
    if (U_SUCCESS(status) || empty != NULL) {
        delete empty;
        errln("FAIL: Non-null VTimeZone is returned for empty VTIMEZONE data");
    }
    status = U_ZERO_ERROR;

    // Create VTimeZone for Asia/Tokyo
    UnicodeString asiaTokyoID("Asia/Tokyo");
    static const UChar asiaTokyo[] = {
        /* "BEGIN:VTIMEZONE\x0D\x0A" */
        0x42,0x45,0x47,0x49,0x4E,0x3A,0x56,0x54,0x49,0x4D,0x45,0x5A,0x4F,0x4E,0x45,0x0D,0x0A,
        /* "TZID:Asia\x0D\x0A" */
        0x54,0x5A,0x49,0x44,0x3A,0x41,0x73,0x69,0x61,0x0D,0x0A,
        /* "\x09/Tokyo\x0D\x0A" */
        0x09,0x2F,0x54,0x6F,0x6B,0x79,0x6F,0x0D,0x0A,
        /* "BEGIN:STANDARD\x0D\x0A" */
        0x42,0x45,0x47,0x49,0x4E,0x3A,0x53,0x54,0x41,0x4E,0x44,0x41,0x52,0x44,0x0D,0x0A,
        /* "TZOFFSETFROM:+0900\x0D\x0A" */
        0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4F,0x4D,0x3A,0x2B,0x30,0x39,0x30,0x30,0x0D,0x0A,
        /* "TZOFFSETTO:+0900\x0D\x0A" */
        0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x54,0x4F,0x3A,0x2B,0x30,0x39,0x30,0x30,0x0D,0x0A,
        /* "TZNAME:JST\x0D\x0A" */
        0x54,0x5A,0x4E,0x41,0x4D,0x45,0x3A,0x4A,0x53,0x54,0x0D,0x0A,
        /* "DTSTART:19700101\x0D\x0A" */
        0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3A,0x31,0x39,0x37,0x30,0x30,0x31,0x30,0x31,0x0D,0x0A,
        /* " T000000\x0D\x0A" */
        0x20,0x54,0x30,0x30,0x30,0x30,0x30,0x30,0x0D,0x0A,
        /* "END:STANDARD\x0D\x0A" */
        0x45,0x4E,0x44,0x3A,0x53,0x54,0x41,0x4E,0x44,0x41,0x52,0x44,0x0D,0x0A,
        /* "END:VTIMEZONE" */
        0x45,0x4E,0x44,0x3A,0x56,0x54,0x49,0x4D,0x45,0x5A,0x4F,0x4E,0x45,
        0
    };
    VTimeZone *tokyo = VTimeZone::createVTimeZone(asiaTokyo, status);
    if (U_FAILURE(status) || tokyo == NULL) {
        errln("FAIL: Failed to create a VTimeZone tokyo");
    } else {
        // Check ID
        UnicodeString tzid;
        tokyo->getID(tzid);
        if (tzid != asiaTokyoID) {
            errln((UnicodeString)"FAIL: Invalid TZID: " + tzid);
        }
        // Make sure offsets are correct
        int32_t rawOffset, dstSavings;
        tokyo->getOffset(Calendar::getNow(), FALSE, rawOffset, dstSavings, status);
        if (U_FAILURE(status)) {
            errln("FAIL: getOffset failed for tokyo");
        }
        if (rawOffset != 9*HOUR || dstSavings != 0) {
            errln("FAIL: Bad offsets returned by a VTimeZone created for Tokyo");
        }
    }
    delete tokyo;

        // Create VTimeZone from VTIMEZONE data
    static const UChar fooData[] = {
        /* "BEGIN:VCALENDAR\x0D\x0A" */
        0x42,0x45,0x47,0x49,0x4E,0x3A,0x56,0x43,0x41,0x4C,0x45,0x4E,0x44,0x41,0x52,0x0D,0x0A,
        /* "BEGIN:VTIMEZONE\x0D\x0A" */
        0x42,0x45,0x47,0x49,0x4E,0x3A,0x56,0x54,0x49,0x4D,0x45,0x5A,0x4F,0x4E,0x45,0x0D,0x0A,
        /* "TZID:FOO\x0D\x0A" */
        0x54,0x5A,0x49,0x44,0x3A,0x46,0x4F,0x4F,0x0D,0x0A,
        /* "BEGIN:STANDARD\x0D\x0A" */
        0x42,0x45,0x47,0x49,0x4E,0x3A,0x53,0x54,0x41,0x4E,0x44,0x41,0x52,0x44,0x0D,0x0A,
        /* "TZOFFSETFROM:-0700\x0D\x0A" */
        0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4F,0x4D,0x3A,0x2D,0x30,0x37,0x30,0x30,0x0D,0x0A,
        /* "TZOFFSETTO:-0800\x0D\x0A" */
        0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x54,0x4F,0x3A,0x2D,0x30,0x38,0x30,0x30,0x0D,0x0A,
        /* "TZNAME:FST\x0D\x0A" */
        0x54,0x5A,0x4E,0x41,0x4D,0x45,0x3A,0x46,0x53,0x54,0x0D,0x0A,
        /* "DTSTART:20071010T010000\x0D\x0A" */
        0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3A,0x32,0x30,0x30,0x37,0x31,0x30,0x31,0x30,0x54,0x30,0x31,0x30,0x30,0x30,0x30,0x0D,0x0A,
        /* "RRULE:FREQ=YEARLY;BYDAY=WE;BYMONTHDAY=10,11,12,13,14,15,16;BYMONTH=10\x0D\x0A" */
        0x52,0x52,0x55,0x4C,0x45,0x3A,0x46,0x52,0x45,0x51,0x3D,0x59,0x45,0x41,0x52,0x4C,0x59,0x3B,0x42,0x59,0x44,0x41,0x59,0x3D,0x57,0x45,0x3B,0x42,0x59,0x4D,0x4F,0x4E,0x54,0x48,0x44,0x41,0x59,0x3D,0x31,0x30,0x2C,0x31,0x31,0x2C,0x31,0x32,0x2C,0x31,0x33,0x2C,0x31,0x34,0x2C,0x31,0x35,0x2C,0x31,0x36,0x3B,0x42,0x59,0x4D,0x4F,0x4E,0x54,0x48,0x3D,0x31,0x30,0x0D,0x0A,
        /* "END:STANDARD\x0D\x0A" */
        0x45,0x4E,0x44,0x3A,0x53,0x54,0x41,0x4E,0x44,0x41,0x52,0x44,0x0D,0x0A,
        /* "BEGIN:DAYLIGHT\x0D\x0A" */
        0x42,0x45,0x47,0x49,0x4E,0x3A,0x44,0x41,0x59,0x4C,0x49,0x47,0x48,0x54,0x0D,0x0A,
        /* "TZOFFSETFROM:-0800\x0D\x0A" */
        0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4F,0x4D,0x3A,0x2D,0x30,0x38,0x30,0x30,0x0D,0x0A,
        /* "TZOFFSETTO:-0700\x0D\x0A" */
        0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x54,0x4F,0x3A,0x2D,0x30,0x37,0x30,0x30,0x0D,0x0A,
        /* "TZNAME:FDT\x0D\x0A" */
        0x54,0x5A,0x4E,0x41,0x4D,0x45,0x3A,0x46,0x44,0x54,0x0D,0x0A,
        /* "DTSTART:20070415T010000\x0D\x0A" */
        0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3A,0x32,0x30,0x30,0x37,0x30,0x34,0x31,0x35,0x54,0x30,0x31,0x30,0x30,0x30,0x30,0x0D,0x0A,
        /* "RRULE:FREQ=YEARLY;BYMONTHDAY=15;BYMONTH=4\x0D\x0A" */
        0x52,0x52,0x55,0x4C,0x45,0x3A,0x46,0x52,0x45,0x51,0x3D,0x59,0x45,0x41,0x52,0x4C,0x59,0x3B,0x42,0x59,0x4D,0x4F,0x4E,0x54,0x48,0x44,0x41,0x59,0x3D,0x31,0x35,0x3B,0x42,0x59,0x4D,0x4F,0x4E,0x54,0x48,0x3D,0x34,0x0D,0x0A,
        /* "END:DAYLIGHT\x0D\x0A" */
        0x45,0x4E,0x44,0x3A,0x44,0x41,0x59,0x4C,0x49,0x47,0x48,0x54,0x0D,0x0A,
        /* "END:VTIMEZONE\x0D\x0A" */
        0x45,0x4E,0x44,0x3A,0x56,0x54,0x49,0x4D,0x45,0x5A,0x4F,0x4E,0x45,0x0D,0x0A,
        /* "END:VCALENDAR" */
        0x45,0x4E,0x44,0x3A,0x56,0x43,0x41,0x4C,0x45,0x4E,0x44,0x41,0x52,
        0
    };

    VTimeZone *foo = VTimeZone::createVTimeZone(fooData, status);
    if (U_FAILURE(status) || foo == NULL) {
        errln("FAIL: Failed to create a VTimeZone foo");
    } else {
        // Write VTIMEZONE data
        UnicodeString fooData2;
        foo->write(getUTCMillis(2005, UCAL_JANUARY, 1), fooData2, status);
        if (U_FAILURE(status)) {
            errln("FAIL: Failed to write VTIMEZONE data for foo");
        }
        logln(fooData2);
    }
    delete foo;
}

//----------- private test helpers -------------------------------------------------

UDate
TimeZoneRuleTest::getUTCMillis(int32_t y, int32_t m, int32_t d,
                               int32_t hr, int32_t min, int32_t sec, int32_t msec) {
    UErrorCode status = U_ZERO_ERROR;
    const TimeZone *tz = TimeZone::getGMT();
    Calendar *cal = Calendar::createInstance(*tz, status);
    if (U_FAILURE(status)) {
        delete cal;
        errln("FAIL: Calendar::createInstance failed");
        return 0.0;
    }
    cal->set(y, m, d, hr, min, sec);
    cal->set(UCAL_MILLISECOND, msec);
    UDate utc = cal->getTime(status);
    if (U_FAILURE(status)) {
        delete cal;
        errln("FAIL: Calendar::getTime failed");
        return 0.0;
    }
    delete cal;
    return utc;
}

/*
 * Check if a time shift really happens on each transition returned by getNextTransition or
 * getPreviousTransition in the specified time range
 */
void
TimeZoneRuleTest::verifyTransitions(BasicTimeZone& icutz, UDate start, UDate end) {
    UErrorCode status = U_ZERO_ERROR;
    UDate time;
    int32_t raw, dst, raw0, dst0;
    TimeZoneTransition tzt, tzt0;
    UBool avail;
    UBool first = TRUE;
    UnicodeString tzid;

    // Ascending
    time = start;
    while (TRUE) {
        avail = icutz.getNextTransition(time, FALSE, tzt);
        if (!avail) {
            break;
        }
        time = tzt.getTime();
        if (time >= end) {
            break;
        }
        icutz.getOffset(time, FALSE, raw, dst, status);
        icutz.getOffset(time - 1, FALSE, raw0, dst0, status);
        if (U_FAILURE(status)) {
            errln("FAIL: Error in getOffset");
            break;
        }

        if (raw == raw0 && dst == dst0) {
            errln((UnicodeString)"FAIL: False transition returned by getNextTransition for "
                + icutz.getID(tzid) + " at " + dateToString(time));
        }
        if (!first &&
                (tzt0.getTo()->getRawOffset() != tzt.getFrom()->getRawOffset()
                || tzt0.getTo()->getDSTSavings() != tzt.getFrom()->getDSTSavings())) {
            errln((UnicodeString)"FAIL: TO rule of the previous transition does not match FROM rule of this transtion at "
                    + dateToString(time) + " for " + icutz.getID(tzid));                
        }
        tzt0 = tzt;
        first = FALSE;
    }

    // Descending
    first = TRUE;
    time = end;
    while(true) {
        avail = icutz.getPreviousTransition(time, FALSE, tzt);
        if (!avail) {
            break;
        }
        time = tzt.getTime();
        if (time <= start) {
            break;
        }
        icutz.getOffset(time, FALSE, raw, dst, status);
        icutz.getOffset(time - 1, FALSE, raw0, dst0, status);
        if (U_FAILURE(status)) {
            errln("FAIL: Error in getOffset");
            break;
        }

        if (raw == raw0 && dst == dst0) {
            errln((UnicodeString)"FAIL: False transition returned by getPreviousTransition for "
                + icutz.getID(tzid) + " at " + dateToString(time));
        }

        if (!first &&
                (tzt0.getFrom()->getRawOffset() != tzt.getTo()->getRawOffset()
                || tzt0.getFrom()->getDSTSavings() != tzt.getTo()->getDSTSavings())) {
            errln((UnicodeString)"FAIL: TO rule of the next transition does not match FROM rule in this transtion at "
                    + dateToString(time) + " for " + icutz.getID(tzid));                
        }
        tzt0 = tzt;
        first = FALSE;
    }
}

/*
 * Compare all time transitions in 2 time zones in the specified time range in ascending order
 */
void
TimeZoneRuleTest::compareTransitionsAscending(BasicTimeZone& z1, BasicTimeZone& z2,
                                              UDate start, UDate end, UBool inclusive) {
    UnicodeString zid1, zid2;
    TimeZoneTransition tzt1, tzt2;
    UBool avail1, avail2;
    UBool inRange1, inRange2;

    z1.getID(zid1);
    z2.getID(zid2);

    UDate time = start;
    while (TRUE) {
        avail1 = z1.getNextTransition(time, inclusive, tzt1);
        avail2 = z2.getNextTransition(time, inclusive, tzt2);

        inRange1 = inRange2 = FALSE;
        if (avail1) {
            if (tzt1.getTime() < end || (inclusive && tzt1.getTime() == end)) {
                inRange1 = TRUE;
            }
        }
        if (avail2) {
            if (tzt2.getTime() < end || (inclusive && tzt2.getTime() == end)) {
                inRange2 = TRUE;
            }
        }
        if (!inRange1 && !inRange2) {
            // No more transition in the range
            break;
        }
        if (!inRange1) {
            errln((UnicodeString)"FAIL: " + zid1 + " does not have any transitions after "
                + dateToString(time) + " before " + dateToString(end));
            break;
        }
        if (!inRange2) {
            errln((UnicodeString)"FAIL: " + zid2 + " does not have any transitions after "
                + dateToString(time) + " before " + dateToString(end));
            break;
        }
        if (tzt1.getTime() != tzt2.getTime()) {
            errln((UnicodeString)"FAIL: First transition after " + dateToString(time) + " "
                    + zid1 + "[" + dateToString(tzt1.getTime()) + "] "
                    + zid2 + "[" + dateToString(tzt2.getTime()) + "]");
            break;
        }
        time = tzt1.getTime();
        if (inclusive) {
            time += 1;
        }
    }
}

/*
 * Compare all time transitions in 2 time zones in the specified time range in descending order
 */
void
TimeZoneRuleTest::compareTransitionsDescending(BasicTimeZone& z1, BasicTimeZone& z2,
                                               UDate start, UDate end, UBool inclusive) {
    UnicodeString zid1, zid2;
    TimeZoneTransition tzt1, tzt2;
    UBool avail1, avail2;
    UBool inRange1, inRange2;

    z1.getID(zid1);
    z2.getID(zid2);

    UDate time = end;
    while (TRUE) {
        avail1 = z1.getPreviousTransition(time, inclusive, tzt1);
        avail2 = z2.getPreviousTransition(time, inclusive, tzt2);

        inRange1 = inRange2 = FALSE;
        if (avail1) {
            if (tzt1.getTime() > start || (inclusive && tzt1.getTime() == start)) {
                inRange1 = TRUE;
            }
        }
        if (avail2) {
            if (tzt2.getTime() > start || (inclusive && tzt2.getTime() == start)) {
                inRange2 = TRUE;
            }
        }
        if (!inRange1 && !inRange2) {
            // No more transition in the range
            break;
        }
        if (!inRange1) {
            errln((UnicodeString)"FAIL: " + zid1 + " does not have any transitions before "
                + dateToString(time) + " after " + dateToString(start));
            break;
        }
        if (!inRange2) {
            errln((UnicodeString)"FAIL: " + zid2 + " does not have any transitions before "
                + dateToString(time) + " after " + dateToString(start));
            break;
        }
        if (tzt1.getTime() != tzt2.getTime()) {
            errln((UnicodeString)"FAIL: Last transition before " + dateToString(time) + " "
                    + zid1 + "[" + dateToString(tzt1.getTime()) + "] "
                    + zid2 + "[" + dateToString(tzt2.getTime()) + "]");
            break;
        }
        time = tzt1.getTime();
        if (inclusive) {
            time -= 1;
        }
    }
}

#endif /* #if !UCONFIG_NO_FORMATTING */

//eof