Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / corlib / Test / System.Globalization / CalendarTest.cs
1 // CalendarTest.cs
2 //
3 // (C) 2002 Ulrich Kunitz
4 //
5
6 using System.Collections.Generic;
7 using NUnit.Framework;
8 using System;
9 using System.Globalization;
10 using System.IO;
11
12 namespace MonoTests.System.Globalization
13 {
14
15 sealed class Date {
16         private int _day, _month, _year, _era;
17
18         public int Day {
19                 get {
20                         return _day;
21                 }
22                 set {
23                         if (value < 1 || value > 31)
24                                 throw new ArgumentOutOfRangeException(
25                                         "Day",
26                                         "Day must be between 1 and 31.");
27                         _day = value;
28                 }
29         }
30
31         public int Month {
32                 get {
33                         return _month;
34                 }
35                 set {
36                         if (value < 1 || value > 13)
37                                 throw new ArgumentOutOfRangeException(
38                                         "Month",
39                                         "Month must be between 1 and 13.");
40                         _month = value;
41                 }
42         }
43
44         public int Year {
45                 get {
46                         return _year;
47                 }
48                 set {
49                         _year = value;
50                 }
51         }
52
53         public int Era {
54                 get {
55                         return _era;
56                 }
57                 set {
58                         if (value < 1 || value > 10)
59                                 throw new ArgumentOutOfRangeException(
60                                         "Era",
61                                         "Era must be between 1 and 10.");
62                         _era = value;
63                 }
64         }
65                                         
66         public Date(int day, int month, int year, int era) {
67                 Day = day;
68                 Month = month;
69                 Year = year;
70                 Era = era;
71         }
72         public Date(int day, int month, int year) : this(day,month,year,1) {}
73         public Date() : this(1,1,1,1) {}
74
75         public DateTime ToDateTime(Calendar cal) {
76                 return cal.ToDateTime(Year,Month,Day,0,0,0,0,Era);
77         }
78
79         public void FromDateTime(Calendar cal, DateTime time) {
80                 Date dmy = new Date();
81                 dmy.Day = cal.GetDayOfMonth(time);
82                 dmy.Month = cal.GetMonth(time);
83                 dmy.Year = cal.GetYear(time);
84                 dmy.Era = cal.GetEra(time);
85                 Day = dmy.Day;
86                 Month = dmy.Month;
87                 Year = dmy.Year;
88                 Era = dmy.Era;
89         }
90
91         public override string ToString() {
92                 StringWriter sw = new StringWriter();
93                 sw.Write("{0}.{1}.{2}", Day, Month, Year);
94                 if (Era != 1) {
95                         sw.Write(" era {0}", Era);
96                 }
97                 return sw.ToString();
98         }
99
100         public override bool Equals(Object b) {
101                 if (b == null || GetType() != b.GetType())
102                         return false;
103                 return Equals(this, (Date)b);
104         }
105
106         public static bool Equals(Date a, Date b) {
107                 if (a == b)
108                         return true;
109                 if (a.Year != b.Year)
110                         return false;
111                 if (a.Month != b.Month)
112                         return false;
113                 if (a.Day != b.Day)
114                         return false;
115                 if (a.Era != b.Era)
116                         return false;
117                 return true;
118         }
119
120         public override int GetHashCode() {
121                 return ToString().GetHashCode();
122         }
123 } // class Date
124
125 [TestFixture]
126 public class CalendarTest {
127         private Calendar[] acal;
128         private GregorianCalendar gcal;
129         private JulianCalendar jucal;
130         private HijriCalendar hical;
131         private HebrewCalendar hecal;
132         private JapaneseCalendar jacal;
133         private TaiwanCalendar tacal;
134         private KoreanCalendar kcal;
135         private ThaiBuddhistCalendar tbcal;
136 #if NET_2_0
137         private ChineseLunisolarCalendar clcal;
138         private TaiwanLunisolarCalendar tlcal;
139         private JapaneseLunisolarCalendar jlcal;
140         private KoreanLunisolarCalendar klcal;
141 #endif
142
143         [SetUp]
144         protected void SetUp() {
145                 gcal = new GregorianCalendar();
146                 jucal = new JulianCalendar();
147                 hical = new HijriCalendar();
148                 hecal = new HebrewCalendar();
149                 jacal = new JapaneseCalendar();
150                 tacal = new TaiwanCalendar();
151                 kcal = new KoreanCalendar();
152                 tbcal = new ThaiBuddhistCalendar();
153                 acal = new Calendar[] {
154                         gcal, jucal, hical, hecal, jacal,
155                         tacal, kcal, tbcal};
156 #if NET_2_0
157                 clcal = new ChineseLunisolarCalendar ();
158                 tlcal = new TaiwanLunisolarCalendar ();
159                 jlcal = new JapaneseLunisolarCalendar ();
160                 klcal = new KoreanLunisolarCalendar ();
161 #endif
162         }
163
164         private void RowCheck(params Date[] adate) {
165                 if (adate.Length != acal.Length)
166                         throw new ArgumentException(
167                                 "Number of Date arguments doesn't match " +
168                                 "length of calendar array.");
169
170                 DateTime timeG = adate[0].ToDateTime(gcal);
171                 for (int i = 0; i < acal.Length; i++) {
172                         Date date = adate[i];
173                         if (date == null)
174                                 continue;
175                         Calendar cal = acal[i];
176
177                         DateTime time = date.ToDateTime(cal);
178                         StringWriter sw = new StringWriter();
179                         sw.Write("Calendar {0} computes wrong DateTime.",
180                                 cal);
181                         Assert.AreEqual(timeG, time, sw.ToString());
182
183                         sw = new StringWriter();
184                         Date ndate = new Date();
185                         ndate.FromDateTime(cal, time);
186                         sw.Write("Calendar {0} computes wrong date", cal);
187                         Assert.AreEqual(date, ndate, sw.ToString());
188                 }
189         }
190
191         // We are testing the implementation against the reference dates in
192         // Calendrical Calcualation. Please note that the CLR uses another
193         // epoch for the HijriCalendar, which might be the perfect thing
194         // to do.
195         [Test]
196         public void TestCCTable() {
197                 // Gr Ju Hi He Ja Ta Ko TB
198                 RowCheck(new Date(24,9,70,1),
199                         new Date(26,9,70,1),
200                         null,
201                         null,
202                         null,
203                         null,
204                         new Date(24,9,2403,1),
205                         new Date(24,9,613,1));
206                 RowCheck(new Date(2,10,135,1),
207                         new Date(3,10,135,1),
208                         null,
209                         null,
210                         null,
211                         null,
212                         new Date(2,10,2468,1),
213                         new Date(2,10,678,1));
214                 RowCheck(new Date(8,1,470,1),
215                         new Date(7,1,470,1),
216                         null,
217                         null,
218                         null,
219                         null,
220                         new Date(8,1,2803,1),
221                         new Date(8,1,1013,1));
222                 RowCheck(new Date(20,5,576,1),
223                         new Date(18,5,576,1),
224                         null,
225                         null,
226                         null,
227                         null,
228                         new Date(20,5,2909,1),
229                         new Date(20,5,1119,1));
230                 RowCheck(new Date(10,11,694,1),
231                         new Date(7,11,694,1),
232                         new Date(14,7,75,1),
233                         null,
234                         null,
235                         null,
236                         new Date(10,11,3027,1),
237                         new Date(10,11,1237,1));
238                 RowCheck(new Date(25,4,1013,1),
239                         new Date(19,4,1013,1),
240                         new Date(6,10,403,1),
241                         null,
242                         null,
243                         null,
244                         new Date(25,4,3346,1),
245                         new Date(25,4,1556,1));
246                 RowCheck(new Date(24,5,1096,1),
247                         new Date(18,5,1096,1),
248                         new Date(23,5,489,1),
249                         null,
250                         null,
251                         null,
252                         new Date(24,5,3429,1),
253                         new Date(24,5,1639,1));
254                 RowCheck(new Date(23,3,1190,1),
255                         new Date(16,3,1190,1),
256                         new Date(8,2,586,1),
257                         null,
258                         null,
259                         null,
260                         new Date(23,3,3523,1),
261                         new Date(23,3,1733,1));
262                 RowCheck(new Date(10,3,1240,1),
263                         new Date(3,3,1240,1),
264                         new Date(8,8,637,1),
265                         null,
266                         null,
267                         null,
268                         new Date(10,3,3573,1),
269                         new Date(10,3,1783,1));
270                 RowCheck(new Date(2,4,1288,1),
271                         new Date(26,3,1288,1),
272                         new Date(21,2,687,1),
273                         null,
274                         null,
275                         null,
276                         new Date(2,4,3621,1),
277                         new Date(2,4,1831,1));
278                 RowCheck(new Date(27,4,1298,1),
279                         new Date(20,4,1298,1),
280                         new Date(8,7,697,1),
281                         null,
282                         null,
283                         null,
284                         new Date(27,4,3631,1),
285                         new Date(27,4,1841,1));
286                 RowCheck(new Date(12,6,1391,1),
287                         new Date(4,6,1391,1),
288                         new Date(2,7,793,1),
289                         null,
290                         null,
291                         null,
292                         new Date(12,6,3724,1),
293                         new Date(12,6,1934,1));
294                 RowCheck(new Date(3,2,1436,1),
295                         new Date(25,1,1436,1),
296                         new Date(7,7,839,1),
297                         null,
298                         null,
299                         null,
300                         new Date(3,2,3769,1),
301                         new Date(3,2,1979,1));
302                 RowCheck(new Date(9,4,1492,1),
303                         new Date(31,3,1492,1),
304                         new Date(2,6,897,1),
305                         null,
306                         null,
307                         null,
308                         new Date(9,4,3825,1),
309                         new Date(9,4,2035,1));
310                 RowCheck(new Date(19,9,1553,1),
311                         new Date(9,9,1553,1),
312                         new Date(1,10,960,1),
313                         null,
314                         null,
315                         null,
316                         new Date(19,9,3886,1),
317                         new Date(19,9,2096,1));
318                 RowCheck(new Date(5,3,1560,1),
319                         new Date(24,2,1560,1),
320                         new Date(28,5,967,1),
321                         null,
322                         null,
323                         null,
324                         new Date(5,3,3893,1),
325                         new Date(5,3,2103,1));
326                 RowCheck(new Date(10,6,1648,1),
327                         new Date(31,5,1648,1),
328                         new Date(19,5,1058,1),
329                         new Date(20,9,5408,1),
330                         null,
331                         null,
332                         new Date(10,6,3981,1),
333                         new Date(10,6,2191,1));
334                 RowCheck(new Date(30,6,1680,1),
335                         new Date(20,6,1680,1),
336                         new Date(3,6,1091,1),
337                         new Date(3,11,5440,1),
338                         null,
339                         null,
340                         new Date(30,6,4013,1),
341                         new Date(30,6,2223,1));
342                 RowCheck(new Date(24,7,1716,1),
343                         new Date(13,7,1716,1),
344                         new Date(5,8,1128,1),
345                         new Date(5,11,5476,1),
346                         null,
347                         null,
348                         new Date(24,7,4049,1),
349                         new Date(24,7,2259,1));
350                 RowCheck(new Date(19,6,1768,1),
351                         new Date(8,6,1768,1),
352                         new Date(4,2,1182,1),
353                         new Date(4,10,5528,1),
354                         null,
355                         null,
356                         new Date(19,6,4101,1),
357                         new Date(19,6,2311,1));
358                 RowCheck(new Date(2,8,1819,1),
359                         new Date(21,7,1819,1),
360                         new Date(11,10,1234,1),
361                         new Date(11,11,5579,1),
362                         null,
363                         null,
364                         new Date(2,8,4152,1),
365                         new Date(2,8,2362,1));
366                 RowCheck(new Date(27,3,1839,1),
367                         new Date(15,3,1839,1),
368                         new Date(12,1,1255,1),
369                         new Date(12,7,5599,1),
370                         null,
371                         null,
372                         new Date(27,3,4172,1),
373                         new Date(27,3,2382,1));
374                 RowCheck(new Date(19,4,1903,1),
375                         new Date(6,4,1903,1),
376                         new Date(22,1,1321,1),
377                         new Date(22,7,5663,1),
378                         new Date(19,4,36,1),
379                         null,
380                         new Date(19,4,4236,1),
381                         new Date(19,4,2446,1));
382                 RowCheck(new Date(25,8,1929,1),
383                         new Date(12,8,1929,1),
384                         new Date(20,3,1348,1),
385                         new Date(19,12,5689,1),
386                         new Date(25,8,4,3),
387                         new Date(25,8,18,1),
388                         new Date(25,8,4262,1),
389                         new Date(25,8,2472,1));
390                 RowCheck(new Date(29,9,1941,1),
391                         new Date(16,9,1941,1),
392                         new Date(9,9,1360,1),
393                         new Date(8,1,5702,1),
394                         new Date(29,9,16,3),
395                         new Date(29,9,30,1),
396                         new Date(29,9,4274,1),
397                         new Date(29,9,2484,1));
398                 RowCheck(new Date(19,4,1943,1),
399                         new Date(6,4,1943,1),
400                         new Date(14,4,1362,1),
401                         new Date(14,8,5703,1),
402                         new Date(19,4,18,3),
403                         new Date(19,4,32,1),
404                         new Date(19,4,4276,1),
405                         new Date(19,4,2486,1));
406                 RowCheck(new Date(7,10,1943,1),
407                         new Date(24,9,1943,1),
408                         new Date(8,10,1362,1),
409                         new Date(8,1,5704,1),
410                         new Date(7,10,18,3),
411                         new Date(7,10,32,1),
412                         new Date(7,10,4276,1),
413                         new Date(7,10,2486,1));
414                 RowCheck(new Date(17,3,1992,1),
415                         new Date(4,3,1992,1),
416                         new Date(14,9,1412,1),
417                         new Date(12,7,5752,1),
418                         new Date(17,3,4,4),
419                         new Date(17,3,81,1),
420                         new Date(17,3,4325,1),
421                         new Date(17,3,2535,1));
422                 RowCheck(new Date(25,5,1996,1),
423                         new Date(12,5,1996,1),
424                         new Date(8,1,1417,1),
425                         new Date(7,9,5756,1),
426                         new Date(25,5,8,4),
427                         new Date(25,5,85,1),
428                         new Date(25,5,4329,1),
429                         new Date(25,5,2539,1));
430                 RowCheck(new Date(10,11,2038,1),
431                         new Date(28,10,2038,1),
432                         new Date(13,10,1460,1),
433                         new Date(12,2,5799,1),
434                         new Date(10,11,50,4),
435                         new Date(10,11,127,1),
436                         new Date(10,11,4371,1),
437                         new Date(10,11,2581,1));
438                 RowCheck(new Date(18,7,2094,1),
439                         new Date(5,7,2094,1),
440                         new Date(6,3,1518,1),
441                         new Date(5,11,5854,1),
442                         new Date(18,7,106,4),
443                         new Date(18,7,183,1),
444                         new Date(18,7,4427,1),
445                         new Date(18,7,2637,1));
446         }
447
448         [Test]
449         public void TestCalendarType() {
450                 GregorianCalendar gc = new GregorianCalendar(
451                         GregorianCalendarTypes.Arabic);
452                 Assert.AreEqual(GregorianCalendarTypes.Arabic,
453                                                 gc.CalendarType,
454                                                 "A01 Gregorian ctor with GregorianCalendarTypes parameter");
455                 gc.CalendarType = GregorianCalendarTypes.MiddleEastFrench;
456                 Assert.AreEqual(GregorianCalendarTypes.MiddleEastFrench, 
457                                          gc.CalendarType, 
458                                          "A02 GregorianCalendar.CalendarType");
459                         
460         }
461
462         [Test]
463         public void TestStandardEras() {
464                 Assert.AreEqual(1, GregorianCalendar.ADEra, "B01 ADEra");
465                 Assert.AreEqual(1, HebrewCalendar.HebrewEra, "B02 HebrewEra");
466                 Assert.AreEqual(1, HijriCalendar.HijriEra, "B03 HjriEra");
467                 Assert.AreEqual(1, JulianCalendar.JulianEra, "B04 JulianEra");
468                 Assert.AreEqual(1, KoreanCalendar.KoreanEra, "B05 KoreanEra");
469                 Assert.AreEqual(1, ThaiBuddhistCalendar.ThaiBuddhistEra, "B06 ThaiBuddhistEra");
470                         
471 #if NET_2_0
472                 Assert.AreEqual(1, ChineseLunisolarCalendar.ChineseEra, "CNLunisor");
473                 Assert.AreEqual(1, JapaneseLunisolarCalendar.JapaneseEra, "JPLunisor");
474                 Assert.AreEqual(1, KoreanLunisolarCalendar.GregorianEra, "KRLunisor");
475 #endif
476         }
477
478         [Test]
479         public void TestCurrentEra() {
480                 Assert.AreEqual(0, GregorianCalendar.CurrentEra, 
481                                          "C01 GregorianCalendar.CurrentEra");
482                 Assert.AreEqual(0, HebrewCalendar.CurrentEra,
483                                          "C02 HebrewCalendar.CurrentEra");
484                 Assert.AreEqual(0, HijriCalendar.CurrentEra, 
485                                          "C03 HijriCalendar.CurrentEra");
486                 Assert.AreEqual(0, JapaneseCalendar.CurrentEra, 
487                                          "C04 JapaneseCalendar.CurrentEra");
488                 Assert.AreEqual(0, JulianCalendar.CurrentEra, 
489                                          "C05 JulianCalendar.CurrentEra");
490                 Assert.AreEqual(0, KoreanCalendar.CurrentEra,
491                                          "C06 KoreanCalendar.CurrentEra");
492                 Assert.AreEqual(0, TaiwanCalendar.CurrentEra,
493                                          "C07 TaiwanCalendar.CurrentEra");
494                 Assert.AreEqual(0, ThaiBuddhistCalendar.CurrentEra,
495                                          "C08 ThaiBuddhistCalendar.CurrentEra");
496         }
497
498         [Test]
499         public void TestErasProperty() {
500                 foreach (Calendar cal in acal) {
501                         int check = 1;
502                         if (cal is JapaneseCalendar)
503                                 check = 4;
504                         Assert.AreEqual(check, cal.Eras.Length,
505                                                  String.Format("D01 {0}.Eras.Length", cal));
506                         cal.Eras[0] = 29;
507                         Assert.IsTrue(cal.Eras[0] != 29, String.Format("D02 {0}.Eras readonly", cal));
508                 }
509         }
510
511 #if NET_2_0
512         [Category ("NotWorking")]
513         [Test]
514         public void TestErasProperty2() {
515                 Assert.AreEqual(1, clcal.Eras.Length, "cn");
516                 Assert.AreEqual(1, tlcal.Eras.Length, "tw");
517                 Assert.AreEqual(2, jlcal.Eras.Length, "jp");
518                 Assert.AreEqual(1, klcal.Eras.Length, "kr");
519
520                 Assert.AreEqual(4, jlcal.Eras [0], "jp.1");
521                 Assert.AreEqual(3, jlcal.Eras [1], "jp.2");
522         }
523 #endif
524
525         [Test]
526         public void TestTwoDigitYearMax() {
527                 Assert.AreEqual(2029, gcal.TwoDigitYearMax,
528                                          "E01 TwoDigitYearMax GregorianCalendar");
529                 Assert.AreEqual(5790, hecal.TwoDigitYearMax,
530                                          "E02 TwoDigitYearMax HebrewCalendar");
531                 Assert.AreEqual(1451, hical.TwoDigitYearMax,
532                                          "E03 TwoDigitYearMax HijriCalendar");
533                 Assert.AreEqual(99, jacal.TwoDigitYearMax,
534                                          "E04 TwoDigitYearMax JapaneseCalendar");
535                 Assert.AreEqual(2029, jucal.TwoDigitYearMax,
536                                          "E05 TwoDigitYearMax JulianCalendar");
537                 Assert.AreEqual(4362, kcal.TwoDigitYearMax,
538                                          "E06 TwoDigitYearMax KoreanCalendar");
539                 Assert.AreEqual(99, tacal.TwoDigitYearMax,
540                                          "E07 TwoDigitYearMax TaiwanCalendar");
541                 Assert.AreEqual(2572, tbcal.TwoDigitYearMax,
542                                          "E08 TwoDigitYearMax ThaiBuddhistCalendar");
543                 foreach (Calendar cal in acal) {
544                         bool exception = false;
545                         try { 
546                                 cal.TwoDigitYearMax = 99;
547                         }
548                         catch (ArgumentOutOfRangeException) {
549                                 exception = true;
550                         }
551                         Assert.IsTrue(exception,
552                                    String.Format("E09 {0}.TwoDigitYearMax 99 " +
553                                                                  " out of range exception", cal));
554
555                         exception = false;
556                         int m = 10000;
557                         try {
558                                 m = cal.GetYear(DateTime.MaxValue)+1;
559                                 cal.TwoDigitYearMax = m;
560                         }
561                         catch (ArgumentException) {
562                                 exception = true;
563                         }
564                         Assert.IsTrue(exception,
565                                    String.Format("E10 {0}.TwoDigitYearMax out " +
566                                                                  " of range exception value {1}",
567                                                                  cal, m));
568                 }
569         }
570
571         [Test] // wrt bug #76252.
572         public void HebrewCalendarGetDaysInMonth ()
573         {
574                 HebrewCalendar c = new HebrewCalendar ();
575                 int year = c.GetYear (new DateTime (2005, 9, 1));
576                 Assert.AreEqual (5765, year);
577                 int days = c.GetDaysInMonth (year, 13, 1);
578                 Assert.AreEqual (29, days);
579         }
580
581         [Test] // bug #81783
582         public void GregorianAddMonth ()
583         {
584                 GregorianCalendar c = new GregorianCalendar ();
585                 DateTime d = new DateTime (2007, 5, 31);
586                 DateTime prev = c.AddMonths (d, -1);
587                 Assert.AreEqual (4, prev.Month, "prev");
588                 DateTime next = c.AddMonths (d, 1);
589                 Assert.AreEqual (6, next.Month, "next");
590
591                 d = new DateTime (2003, 12, 5);
592                 prev = c.AddMonths (d, -13);
593                 Assert.AreEqual (new DateTime (2002, 11, 5), prev, "prev2");
594                 next = c.AddMonths (d, 6);
595                 Assert.AreEqual (new DateTime (2004, 6, 5), next, "next2");
596         }
597
598         [Test]
599         public void AddYearOnLeapYear ()
600         {
601                 GregorianCalendar c = new GregorianCalendar ();
602                 DateTime d = new DateTime (2004, 2, 29);
603                 DateTime prev = c.AddYears (d, -1);
604                 Assert.AreEqual (2, prev.Month, "prev");
605                 DateTime next = c.AddYears (d, 1);
606                 Assert.AreEqual (2, next.Month, "next");
607         }
608
609 #if NET_2_0
610         [Test]
611         [Category ("NotWorking")]
612         public void GetLeapMonth ()
613         {
614                 GregorianCalendar gc = new GregorianCalendar ();
615                 Assert.AreEqual (0, gc.GetLeapMonth (2007), "#1-1");
616                 Assert.AreEqual (0, gc.GetLeapMonth (2008), "#1-2");
617                 Assert.AreEqual (0, gc.GetLeapMonth (2100), "#1-3");
618                 Assert.AreEqual (0, gc.GetLeapMonth (2000), "#1-4");
619
620                 JulianCalendar jc = new JulianCalendar ();
621                 Assert.AreEqual (0, jc.GetLeapMonth (2007), "#2-1");
622                 Assert.AreEqual (0, jc.GetLeapMonth (2008), "#2-2");
623                 Assert.AreEqual (0, jc.GetLeapMonth (2100), "#2-3");
624                 Assert.AreEqual (0, jc.GetLeapMonth (2000), "#2-4");
625                 Assert.AreEqual (0, jc.GetLeapMonth (2009), "#2-5");
626                 Assert.AreEqual (0, jc.GetLeapMonth (2010), "#2-6");
627
628                 HebrewCalendar hc = new HebrewCalendar ();
629                 // 3rd, 6th, 8th, 11th 14th and 17th year in every 19 are leap.
630                 // 5339 % 19 = 0.
631                 Assert.AreEqual (0, hc.GetLeapMonth (5343), "#3-1");
632                 Assert.AreEqual (0, hc.GetLeapMonth (5344), "#3-2");
633                 Assert.AreEqual (7, hc.GetLeapMonth (5345), "#3-3");
634                 Assert.AreEqual (0, hc.GetLeapMonth (5346), "#3-4");
635                 Assert.AreEqual (7, hc.GetLeapMonth (5347), "#3-5");
636                 Assert.AreEqual (0, hc.GetLeapMonth (5348), "#3-6");
637                 Assert.AreEqual (0, hc.GetLeapMonth (5349), "#3-7");
638
639                 ThaiBuddhistCalendar tc = new ThaiBuddhistCalendar ();
640                 Assert.AreEqual (0, tc.GetLeapMonth (2520), "#4-1");
641                 Assert.AreEqual (0, tc.GetLeapMonth (2521), "#4-2");
642                 Assert.AreEqual (0, tc.GetLeapMonth (2522), "#4-3");
643                 Assert.AreEqual (0, tc.GetLeapMonth (2523), "#4-4");
644
645                 ChineseLunisolarCalendar cc = new ChineseLunisolarCalendar ();
646                 Assert.AreEqual (0, cc.GetLeapMonth (2000), "#5-1");
647                 Assert.AreEqual (5, cc.GetLeapMonth (2001), "#5-2");
648                 Assert.AreEqual (0, cc.GetLeapMonth (2002), "#5-3");
649                 Assert.AreEqual (0, cc.GetLeapMonth (2003), "#5-4");
650                 Assert.AreEqual (3, cc.GetLeapMonth (2004), "#5-5");
651                 Assert.AreEqual (0, cc.GetLeapMonth (2005), "#5-6");
652                 Assert.AreEqual (8, cc.GetLeapMonth (2006), "#5-7");
653                 Assert.AreEqual (0, cc.GetLeapMonth (2007), "#5-8");
654                 Assert.AreEqual (0, cc.GetLeapMonth (2008), "#5-9");
655                 Assert.AreEqual (6, cc.GetLeapMonth (2009), "#5-10");
656                 Assert.AreEqual (0, cc.GetLeapMonth (2010), "#5-11");
657                 Assert.AreEqual (0, cc.GetLeapMonth (2011), "#5-12");
658                 Assert.AreEqual (5, cc.GetLeapMonth (2012), "#5-13");
659                 Assert.AreEqual (0, cc.GetLeapMonth (2013), "#5-14");
660                 Assert.AreEqual (10, cc.GetLeapMonth (2014), "#5-15");
661                 Assert.AreEqual (0, cc.GetLeapMonth (2015), "#5-16");
662                 Assert.AreEqual (0, cc.GetLeapMonth (2016), "#5-17");
663                 Assert.AreEqual (7, cc.GetLeapMonth (2017), "#5-18");
664                 Assert.AreEqual (0, cc.GetLeapMonth (2018), "#5-19");
665                 Assert.AreEqual (0, cc.GetLeapMonth (2019), "#5-20");
666
667                 KoreanLunisolarCalendar kc = new KoreanLunisolarCalendar ();
668                 Assert.AreEqual (0, kc.GetLeapMonth (2000), "#6-1");
669                 Assert.AreEqual (5, kc.GetLeapMonth (2001), "#6-2");
670                 Assert.AreEqual (0, kc.GetLeapMonth (2002), "#6-3");
671                 Assert.AreEqual (0, kc.GetLeapMonth (2003), "#6-4");
672                 Assert.AreEqual (3, kc.GetLeapMonth (2004), "#6-5");
673                 Assert.AreEqual (0, kc.GetLeapMonth (2005), "#6-6");
674                 Assert.AreEqual (8, kc.GetLeapMonth (2006), "#6-7");
675                 Assert.AreEqual (0, kc.GetLeapMonth (2007), "#6-8");
676                 Assert.AreEqual (0, kc.GetLeapMonth (2008), "#6-9");
677                 Assert.AreEqual (6, kc.GetLeapMonth (2009), "#6-10");
678                 Assert.AreEqual (0, kc.GetLeapMonth (2010), "#6-11");
679                 Assert.AreEqual (0, kc.GetLeapMonth (2011), "#6-12");
680                 Assert.AreEqual (4, kc.GetLeapMonth (2012)); // off from cn by 1, "#6-13");
681                 Assert.AreEqual (0, kc.GetLeapMonth (2013), "#6-14");
682                 Assert.AreEqual (10, kc.GetLeapMonth (2014), "#6-15");
683                 Assert.AreEqual (0, kc.GetLeapMonth (2015), "#6-16");
684                 Assert.AreEqual (0, kc.GetLeapMonth (2016), "#6-17");
685                 Assert.AreEqual (6, kc.GetLeapMonth (2017)); // off from cn by 1, "#6-18");
686                 Assert.AreEqual (0, kc.GetLeapMonth (2018), "#6-19");
687                 Assert.AreEqual (0, kc.GetLeapMonth (2019), "#6-20");
688         }
689
690         [Test]
691         public void GetWeekOfYear ()
692         {
693                 GregorianCalendar gc = new GregorianCalendar ();
694                 Assert.AreEqual (1, gc.GetWeekOfYear (new DateTime (2007, 1, 1), CalendarWeekRule.FirstDay, DayOfWeek.Sunday), "#1");
695                 //Assert.AreEqual (1, gc.GetWeekOfYear (new DateTime (2000, 1, 1), CalendarWeekRule.FirstDay, DayOfWeek.Sunday), "#2");
696                 Assert.AreEqual (3, gc.GetWeekOfYear (new DateTime (2000, 1, 10), CalendarWeekRule.FirstDay, DayOfWeek.Sunday), "#2");
697                 Assert.AreEqual (2, gc.GetWeekOfYear (new DateTime (2000, 1, 10), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday), "#3");
698                 Assert.AreEqual (2, gc.GetWeekOfYear (new DateTime (2000, 1, 10), CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday), "#4");
699                 Assert.AreEqual (52, gc.GetWeekOfYear (new DateTime (2000, 1, 1), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday), "#5");
700                 Assert.AreEqual (52, gc.GetWeekOfYear (new DateTime (2000, 1, 1), CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday), "#6");
701         }
702 #endif
703
704         [Test]
705         public void TestToFourDigitYear() {
706                 foreach (Calendar cal in acal) {
707                         bool working = true;
708                         int mod = 2000;
709                         if (cal is HebrewCalendar)
710                                 mod = 5500; 
711                         if (cal is KoreanCalendar)
712                                 mod = 3000;
713                         if (cal is JapaneseCalendar)
714                                 working = false;
715                         if (cal is TaiwanCalendar)
716                                 working = false;
717                         cal.TwoDigitYearMax = mod + 229;
718                         Assert.AreEqual(mod+229 , cal.TwoDigitYearMax,
719                                                  String.Format("F01 {0}.TwoDigitYearMax", cal));
720                         Assert.AreEqual(working ? mod+229 : 29,
721                                                  cal.ToFourDigitYear(29),
722                                                  String.Format("F02 {0}.ToFourDigitYear(29)",cal));
723                         Assert.AreEqual(
724                                 working ? mod+130 : 30,
725                                 cal.ToFourDigitYear(30),
726                                 String.Format("F03 {0}.ToFourDigitYear(30)",
727                                                           cal));
728                         Assert.AreEqual(mod, cal.ToFourDigitYear(mod),
729                                 String.Format("F04 {0}.ToFourDigitYear({1})",
730                                                           cal, mod));
731
732                         bool exception = false;
733                         try {
734                                 cal.ToFourDigitYear(-1);
735                         }
736                         catch (ArgumentOutOfRangeException) {
737                                 exception = true;
738                         }
739                         Assert.IsTrue(exception, String.Format(
740                                 "F05 {0}.ToFourDigitYear(-1) exception",
741                                 cal));
742                         exception = false;
743                         try {
744                                 cal.ToFourDigitYear(15000);
745                         }
746                         catch (ArgumentOutOfRangeException) {
747                                 exception = true;
748                         }
749                         Assert.IsTrue(exception, String.Format(
750                                 "F05 {0}.ToFourDigitYear(15000) exception",
751                                 cal));
752                 }
753         }
754
755         [Test]
756         public void TestToFourDigitYear2 ()
757         {
758                 GregorianCalendar gc = new GregorianCalendar ();
759                 Assert.AreEqual (2029, gc.ToFourDigitYear (29), "#1-1");
760                 Assert.AreEqual (1930, gc.ToFourDigitYear (30), "#1-2");
761                 Assert.AreEqual (2029, gc.ToFourDigitYear (2029), "#1-3");
762                 Assert.AreEqual (2030, gc.ToFourDigitYear (2030), "#1-4");
763
764                 HebrewCalendar hbc = new HebrewCalendar ();
765                 Assert.AreEqual (5790, hbc.ToFourDigitYear (90), "#2-1");
766                 Assert.AreEqual (5691, hbc.ToFourDigitYear (91), "#2-2");
767                 Assert.AreEqual (5790, hbc.ToFourDigitYear (5790), "#2-3");
768                 Assert.AreEqual (5691, hbc.ToFourDigitYear (5691), "#2-4");
769                 Assert.AreEqual (5999, hbc.ToFourDigitYear (5999), "#2-5");
770                 // LAMESPEC: .NET fails to throw an exception unlike documented
771                 /*
772                 try {
773                         hbc.ToFourDigitYear (6000);
774                         Assert.Fail ("#2-6");
775                 } catch (ArgumentOutOfRangeException) {
776                 }
777                 */
778
779                 ThaiBuddhistCalendar tc = new ThaiBuddhistCalendar ();
780                 Assert.AreEqual (2572, tc.ToFourDigitYear (72), "#3-1");
781                 Assert.AreEqual (2473, tc.ToFourDigitYear (73), "#3-2");
782                 Assert.AreEqual (2572, tc.ToFourDigitYear (2572), "#3-3");
783                 Assert.AreEqual (2573, tc.ToFourDigitYear (2573), "#3-4");
784                 Assert.AreEqual (9999, tc.ToFourDigitYear (9999), "#3-5");
785                 // LAMESPEC: .NET fails to throw an exception unlike documented
786                 /*
787                 try {
788                         tc.ToFourDigitYear (10000);
789                         Assert.Fail ("#3-6");
790                 } catch (ArgumentOutOfRangeException) {
791                 }
792                 */
793
794                 KoreanCalendar kc = new KoreanCalendar ();
795                 Assert.AreEqual (4362, kc.ToFourDigitYear (62), "#4-1");
796                 Assert.AreEqual (4263, kc.ToFourDigitYear (63), "#4-2");
797                 Assert.AreEqual (4362, kc.ToFourDigitYear (4362), "#4-3");
798                 Assert.AreEqual (4363, kc.ToFourDigitYear (4363), "#4-4");
799         }
800
801         public void TestDaysInYear (Calendar calendar, int year)
802         {
803                 var daysInYear = calendar.GetDaysInYear (year);
804                 var daysInMonths = 0;
805                 var monthInYear = calendar.GetMonthsInYear (year);
806                 for (var m = 1; m <= monthInYear; m++)
807                         daysInMonths += calendar.GetDaysInMonth (year, m);
808
809                 Assert.AreEqual (daysInYear, daysInMonths, string.Format("Calendar:{0} Year:{1}",calendar.GetType(), year));
810         }
811
812         [Test]
813         public void DaysInYear ()
814         {
815                 var calendars = new List<Calendar> (acal) {
816                         new UmAlQuraCalendar ()
817                 };
818
819                 foreach (var calendar in calendars) {
820                         var minYear = calendar.GetYear (calendar.MinSupportedDateTime);
821                         var maxYear = calendar.GetYear (calendar.MaxSupportedDateTime) - 1 ;
822                         var midYear = calendar.GetYear (DateTime.Now);
823                         var yearsTested = Math.Min (1000, (maxYear - minYear) / 2);
824
825                         midYear -= yearsTested / 2;
826
827                         int y1 = minYear, y2 = maxYear, y3 = midYear;
828                         for (var i = 0; i < yearsTested; i++) {
829                                 TestDaysInYear (calendar, y1);
830                                 TestDaysInYear (calendar, y2);
831                                 if (y3 > minYear && y3 < maxYear)
832                                         TestDaysInYear (calendar, y3);
833
834                                 y1++; y2--; y3++;
835                         }
836                 }
837         }
838
839         // TODO: more tests :-)
840 } // class CalendarTest
841         
842 } // namespace MonoTests.System.Globalization