2009-08-26 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System / TimeSpanTest.cs
1 //
2 // TimeSpanTest.cs - NUnit Test Cases for the System.TimeSpan struct
3 //
4 // Authors:
5 //      Duco Fijma (duco@lorentz.xs4all.nl)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2001 Duco Fijma
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using NUnit.Framework;
13 using System;
14
15 namespace MonoTests.System
16 {
17
18 [TestFixture]
19 public class TimeSpanTest {
20
21         private void Debug (TimeSpan ts) 
22         {
23                 Console.Out.WriteLine ("Days {0}", ts.Days);
24                 Console.Out.WriteLine ("Hours {0}", ts.Hours);
25                 Console.Out.WriteLine ("Minutes {0}", ts.Minutes);
26                 Console.Out.WriteLine ("Seconds {0}", ts.Seconds);
27                 Console.Out.WriteLine ("Milliseconds {0}", ts.Milliseconds);
28                 Console.Out.WriteLine ("Ticks {0}", ts.Ticks);
29         }
30
31         public void TestCtors ()
32         {
33                 TimeSpan t1 = new TimeSpan (1234567890);
34
35                 Assert.AreEqual ("00:02:03.4567890", t1.ToString (), "A1");
36                 t1 = new TimeSpan (1,2,3);
37                 Assert.AreEqual ("01:02:03", t1.ToString (), "A2");
38                 t1 = new TimeSpan (1,2,3,4);
39                 Assert.AreEqual ("1.02:03:04", t1.ToString (), "A3");
40                 t1 = new TimeSpan (1,2,3,4,5);
41                 Assert.AreEqual ("1.02:03:04.0050000", t1.ToString (), "A4");
42                 t1 = new TimeSpan (-1,2,-3,4,-5);
43                 Assert.AreEqual ("-22:02:56.0050000", t1.ToString (), "A5");
44                 t1 = new TimeSpan (0,25,0,0,0);
45                 Assert.AreEqual ("1.01:00:00", t1.ToString (), "A6");
46         }
47
48         [Test]
49         [ExpectedException (typeof (ArgumentOutOfRangeException))]
50         public void DaysOverflow () 
51         {
52                 int days = (int) (Int64.MaxValue / TimeSpan.TicksPerDay) + 1;
53                 TimeSpan ts = new TimeSpan (days, 0, 0, 0, 0);
54         }
55
56         [Test]
57 #if NET_2_0
58         [ExpectedException (typeof (ArgumentOutOfRangeException))]
59         [Category ("NotWorking")]
60 #endif
61         public void TemporaryOverflow () 
62         {
63                 // calculating part of this results in overflow (days)
64                 // but the negative hours, minutes, seconds & ms correct this
65                 int days = (int) (Int64.MaxValue / TimeSpan.TicksPerDay) + 1;
66                 TimeSpan ts = new TimeSpan (days, Int32.MinValue, Int32.MinValue, Int32.MinValue, Int32.MinValue);
67                 Assert.AreEqual (10650320, ts.Days, "Days");
68                 Assert.AreEqual (0, ts.Hours, "Hours");
69                 Assert.AreEqual (14, ts.Minutes, "Minutes");
70                 Assert.AreEqual (28, ts.Seconds, "Seconds");
71                 Assert.AreEqual (352, ts.Milliseconds, "Milliseconds");
72                 Assert.AreEqual (9201876488683520000, ts.Ticks, "Ticks");
73         }
74
75         [Test]
76 #if NET_2_0
77         [ExpectedException (typeof (ArgumentOutOfRangeException))]
78         [Category ("NotWorking")]
79 #endif
80         public void NoOverflowInHoursMinsSecondsMS () 
81         {
82                 TimeSpan ts = new TimeSpan (0, Int32.MaxValue, Int32.MaxValue, Int32.MaxValue, Int32.MaxValue);
83                 Assert.AreEqual (24879, ts.Days, "Days");
84                 Assert.AreEqual (22, ts.Hours, "Hours");
85                 Assert.AreEqual (44, ts.Minutes, "Minutes");
86                 Assert.AreEqual (30, ts.Seconds, "Seconds");
87                 Assert.AreEqual (647, ts.Milliseconds, "Milliseconds");
88                 Assert.AreEqual (21496274706470000, ts.Ticks, "Ticks");
89         }
90
91         [Test]
92         [ExpectedException (typeof (ArgumentOutOfRangeException))]
93         public void MaxDays () 
94         {
95                 new TimeSpan (Int32.MaxValue, 0, 0, 0, 0);
96         }
97
98         [Test]
99         [ExpectedException (typeof (ArgumentOutOfRangeException))]
100         public void MinDays () 
101         {
102                 new TimeSpan (Int32.MinValue, 0, 0, 0, 0);
103         }
104
105         [Test]
106         [Ignore ("too long")]
107         public void MaxHours_TooLong () 
108         {
109                 // LAMESPEC: the highest hours are "special"
110                 for (int i=0; i < 596523; i++) {
111                         TimeSpan ts = new TimeSpan (0, Int32.MaxValue - i, 0, 0, 0);
112                         int h = i + 1;
113                         string prefix = i.ToString () + '-';
114                         Assert.AreEqual (-(h / 24), ts.Days, prefix + "Days");
115                         Assert.AreEqual (-(h % 24), ts.Hours, prefix + "Hours");
116                         Assert.AreEqual (0, ts.Minutes, prefix + "Minutes");
117                         Assert.AreEqual (0, ts.Seconds, prefix + "Seconds");
118                         Assert.AreEqual (0, ts.Milliseconds, prefix + "Milliseconds");
119                         Assert.AreEqual (-36000000000 * h, ts.Ticks, prefix + "Ticks");
120                 }
121         }
122
123         [Test]
124 #if NET_2_0
125         [ExpectedException (typeof (ArgumentOutOfRangeException))]
126         [Category ("NotWorking")]
127 #endif
128         public void MaxHours () 
129         {
130                 // LAMESPEC: the highest hours are "special"
131                 TimeSpan ts = new TimeSpan (0, Int32.MaxValue, 0, 0, 0);
132                 Assert.AreEqual (0, ts.Days, "Max-Days");
133                 Assert.AreEqual (-1, ts.Hours, "Max-Hours");
134                 Assert.AreEqual (0, ts.Minutes, "Max-Minutes");
135                 Assert.AreEqual (0, ts.Seconds, "Max-Seconds");
136                 Assert.AreEqual (0, ts.Milliseconds, "Max-Milliseconds");
137                 Assert.AreEqual (-36000000000, ts.Ticks, "Max-Ticks");
138
139                 ts = new TimeSpan (0, Int32.MaxValue - 596522, 0, 0, 0);
140                 Assert.AreEqual (-24855, ts.Days, "Days");
141                 Assert.AreEqual (-3, ts.Hours, "Hours");
142                 Assert.AreEqual (0, ts.Minutes, "Minutes");
143                 Assert.AreEqual (0, ts.Seconds, "Seconds");
144                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
145                 Assert.AreEqual (-21474828000000000, ts.Ticks, "Ticks");
146         }
147
148         [Test]
149 #if NET_2_0
150         [ExpectedException (typeof (ArgumentOutOfRangeException))]
151         [Category ("NotWorking")]
152 #endif
153         public void MaxHours_BreakPoint () 
154         {
155                 TimeSpan ts = new TimeSpan (0, Int32.MaxValue - 596523, 0, 0, 0);
156                 Assert.AreEqual (24855, ts.Days, "Days");
157                 Assert.AreEqual (2, ts.Hours, "Hours");
158                 Assert.AreEqual (28, ts.Minutes, "Minutes");
159                 Assert.AreEqual (16, ts.Seconds, "Seconds");
160                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
161                 Assert.AreEqual (21474808960000000, ts.Ticks, "Ticks");
162         }
163
164         [Test]
165         [Ignore ("too long")]
166         public void MinHours_TooLong () 
167         {
168                 // LAMESPEC: the lowest hours are "special"
169                 for (int i=Int32.MinValue; i < -2146887124; i++) {
170                         TimeSpan ts = new TimeSpan (0, i, 0, 0, 0);
171                         int h = i + Int32.MaxValue + 1;
172                         string prefix = i.ToString () + '-';
173                         Assert.AreEqual ((h / 24), ts.Days, prefix + "Days");
174                         Assert.AreEqual ((h % 24), ts.Hours, prefix + "Hours");
175                         Assert.AreEqual (0, ts.Minutes, prefix + "Minutes");
176                         Assert.AreEqual (0, ts.Seconds, prefix + "Seconds");
177                         Assert.AreEqual (0, ts.Milliseconds, prefix + "Milliseconds");
178                         Assert.AreEqual (36000000000 * h, ts.Ticks, prefix + "Ticks");
179                 }
180         }
181
182         [Test]
183 #if NET_2_0
184         [Category ("NotWorking")]
185 #endif
186         public void MinHours () 
187         {
188 #if NET_2_0
189                 TimeSpan ts = new TimeSpan (0, -256204778, 0, 0, 0);
190                 Assert.AreEqual (-10675199, ts.Days, "Days");
191                 Assert.AreEqual (-2, ts.Hours, "Hours");
192                 Assert.AreEqual (0, ts.Minutes, "Minutes");
193                 Assert.AreEqual (0, ts.Seconds, "Seconds");
194                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
195                 Assert.AreEqual (-9223372008000000000, ts.Ticks, "Ticks");
196 #else
197                 // LAMESPEC: the lowest hours are "special"
198                 TimeSpan ts = new TimeSpan (0, Int32.MinValue, 0, 0, 0);
199                 Assert.AreEqual (0, ts.Days, "Min-Days");
200                 Assert.AreEqual (0, ts.Hours, "Min-Hours");
201                 Assert.AreEqual (0, ts.Minutes, "Min-Minutes");
202                 Assert.AreEqual (0, ts.Seconds, "Min-Seconds");
203                 Assert.AreEqual (0, ts.Milliseconds, "Min-Milliseconds");
204                 Assert.AreEqual (0, ts.Ticks, "Min-Ticks");
205
206                 ts = new TimeSpan (0, -2146887125, 0, 0, 0);
207                 Assert.AreEqual (24855, ts.Days, "Days");
208                 Assert.AreEqual (3, ts.Hours, "Hours");
209                 Assert.AreEqual (0, ts.Minutes, "Minutes");
210                 Assert.AreEqual (0, ts.Seconds, "Seconds");
211                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
212                 Assert.AreEqual (21474828000000000, ts.Ticks, "Ticks");
213 #endif
214         }
215
216         [Test]
217 #if NET_2_0
218         [ExpectedException (typeof (ArgumentOutOfRangeException))]
219         [Category ("NotWorking")]
220 #endif
221         public void MinHours_BreakPoint () 
222         {
223                 TimeSpan ts = new TimeSpan (0, -2146887124, 0, 0, 0);
224                 Assert.AreEqual (-24855, ts.Days, "Days");
225                 Assert.AreEqual (-2, ts.Hours, "Hours");
226                 Assert.AreEqual (-28, ts.Minutes, "Minutes");
227                 Assert.AreEqual (-16, ts.Seconds, "Seconds");
228                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
229                 Assert.AreEqual (-21474808960000000, ts.Ticks, "Ticks");
230         }
231
232         [Test]
233         [Ignore ("too long")]
234         public void MaxMinutes_TooLong () 
235         {
236                 // LAMESPEC: the highest minutes are "special"
237                 for (int i=0; i < 35791394; i++) {
238                         TimeSpan ts = new TimeSpan (0, 0, Int32.MaxValue - i, 0, 0);
239                         long h = -(i + 1);
240                         string prefix = i.ToString () + '-';
241                         Assert.AreEqual ((h / 1440), ts.Days, prefix + "Days");
242                         Assert.AreEqual (((h / 60) % 24), ts.Hours, prefix + "Hours");
243                         Assert.AreEqual ((h % 60), ts.Minutes, prefix + "Minutes");
244                         Assert.AreEqual (0, ts.Seconds, prefix + "Seconds");
245                         Assert.AreEqual (0, ts.Milliseconds, prefix + "Milliseconds");
246                         Assert.AreEqual ((600000000L * h), ts.Ticks, prefix + "Ticks");
247                 }
248         }
249
250         [Test]
251 #if NET_2_0
252         [Category ("NotWorking")]
253 #endif
254         public void MaxMinutes () 
255         {
256                 TimeSpan ts;
257 #if NET_2_0
258                 ts = new TimeSpan (0, 0, 256204778, 0, 0);
259                 Assert.AreEqual (177919, ts.Days, "Max-Days");
260                 Assert.AreEqual (23, ts.Hours, "Max-Hours");
261                 Assert.AreEqual (38, ts.Minutes, "Max-Minutes");
262                 Assert.AreEqual (0, ts.Seconds, "Max-Seconds");
263                 Assert.AreEqual (0, ts.Milliseconds, "Max-Milliseconds");
264                 Assert.AreEqual (153722866800000000, ts.Ticks, "Max-Ticks");
265 #else
266                 // LAMESPEC: the highest minutes are "special"
267                 ts = new TimeSpan (0, 0, Int32.MaxValue, 0, 0);
268                 Assert.AreEqual (0, ts.Days, "Max-Days");
269                 Assert.AreEqual (0, ts.Hours, "Max-Hours");
270                 Assert.AreEqual (-1, ts.Minutes, "Max-Minutes");
271                 Assert.AreEqual (0, ts.Seconds, "Max-Seconds");
272                 Assert.AreEqual (0, ts.Milliseconds, "Max-Milliseconds");
273                 Assert.AreEqual (-600000000, ts.Ticks, "Max-Ticks");
274
275                 ts = new TimeSpan (0, 0, Int32.MaxValue - 35791393, 0, 0);
276                 Assert.AreEqual (-24855, ts.Days, "Days");
277                 Assert.AreEqual (-3, ts.Hours, "Hours");
278                 Assert.AreEqual (-14, ts.Minutes, "Minutes");
279                 Assert.AreEqual (0, ts.Seconds, "Seconds");
280                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
281                 Assert.AreEqual (-21474836400000000, ts.Ticks, "Ticks");
282 #endif
283         }
284
285         [Test]
286 #if NET_2_0
287         [ExpectedException (typeof (ArgumentOutOfRangeException))]
288         [Category ("NotWorking")]
289 #endif
290         public void MaxMinutes_BreakPoint () 
291         {
292                 TimeSpan ts = new TimeSpan (0, Int32.MaxValue - 35791394, 0, 0, 0);
293                 Assert.AreEqual (0, ts.Days, "Days");
294                 Assert.AreEqual (0, ts.Hours, "Hours");
295                 Assert.AreEqual (-52, ts.Minutes, "Minutes");
296                 Assert.AreEqual (0, ts.Seconds, "Seconds");
297                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
298                 Assert.AreEqual (-31200000000, ts.Ticks, "Ticks");
299         }
300
301         [Test]
302         [Ignore ("too long")]
303         public void MinMinutes_TooLong () 
304         {
305                 // LAMESPEC: the highest minutes are "special"
306                 for (int i=Int32.MinValue; i < -2111692253; i++) {
307                         TimeSpan ts = new TimeSpan (0, 0, i, 0, 0);
308                         long h = i + Int32.MaxValue + 1;
309                         string prefix = i.ToString () + '-';
310                         Assert.AreEqual ((h / 1440), ts.Days, prefix + "Days");
311                         Assert.AreEqual (((h / 60) % 24), ts.Hours, prefix + "Hours");
312                         Assert.AreEqual ((h % 60), ts.Minutes, prefix + "Minutes");
313                         Assert.AreEqual (0, ts.Seconds, prefix + "Seconds");
314                         Assert.AreEqual (0, ts.Milliseconds, prefix + "Milliseconds");
315                         Assert.AreEqual ((600000000L * h), ts.Ticks, prefix + "Ticks");
316                 }
317         }
318
319         [Test]
320 #if NET_2_0
321         [Category ("NotWorking")]
322 #endif
323         public void MinMinutes () 
324         {
325                 TimeSpan ts;
326 #if NET_2_0
327                 ts = new TimeSpan (0, 0, Int32.MinValue, 0, 0);
328                 Assert.AreEqual (-1491308, ts.Days, "Days");
329                 Assert.AreEqual (-2, ts.Hours, "Hours");
330                 Assert.AreEqual (-8, ts.Minutes, "Minutes");
331                 Assert.AreEqual (0, ts.Seconds, "Seconds");
332                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
333                 Assert.AreEqual (-1288490188800000000, ts.Ticks, "Ticks");
334 #else
335                 // LAMESPEC: the highest minutes are "special"
336                 ts = new TimeSpan (0, 0, Int32.MinValue, 0, 0);
337                 Assert.AreEqual (0, ts.Days, "Min-Days");
338                 Assert.AreEqual (0, ts.Hours, "Min-Hours");
339                 Assert.AreEqual (0, ts.Minutes, "Min-Minutes");
340                 Assert.AreEqual (0, ts.Seconds, "Min-Seconds");
341                 Assert.AreEqual (0, ts.Milliseconds, "Min-Milliseconds");
342                 Assert.AreEqual (0, ts.Ticks, "Min-Ticks");
343
344                 ts = new TimeSpan (0, 0, -2111692254, 0, 0);
345                 Assert.AreEqual (24855, ts.Days, "Days");
346                 Assert.AreEqual (3, ts.Hours, "Hours");
347                 Assert.AreEqual (14, ts.Minutes, "Minutes");
348                 Assert.AreEqual (0, ts.Seconds, "Seconds");
349                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
350                 Assert.AreEqual (21474836400000000, ts.Ticks, "Ticks");
351 #endif
352         }
353
354         [Test]
355 #if NET_2_0
356         [Category ("NotWorking")]
357 #endif
358         public void MinMinutes_BreakPoint () 
359         {
360 #if NET_2_0
361                 TimeSpan ts = new TimeSpan (0, 0, -2111692253, 0, 0);
362                 Assert.AreEqual (-1466452, ts.Days, "Days");
363                 Assert.AreEqual (-22, ts.Hours, "Hours");
364                 Assert.AreEqual (-53, ts.Minutes, "Minutes");
365                 Assert.AreEqual (-0, ts.Seconds, "Seconds");
366                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
367                 Assert.AreEqual (-1267015351800000000, ts.Ticks, "Ticks");
368 #else
369                 TimeSpan ts = new TimeSpan (0, 0, -2111692253, 0, 0);
370                 Assert.AreEqual (-24855, ts.Days, "Days");
371                 Assert.AreEqual (-3, ts.Hours, "Hours");
372                 Assert.AreEqual (-13, ts.Minutes, "Minutes");
373                 Assert.AreEqual (-16, ts.Seconds, "Seconds");
374                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
375                 Assert.AreEqual (-21474835960000000, ts.Ticks, "Ticks");
376 #endif
377         }
378
379         [Test]
380         public void MaxSeconds () 
381         {
382                 TimeSpan ts = new TimeSpan (0, 0, 0, Int32.MaxValue, 0);
383                 Assert.AreEqual (24855, ts.Days, "Days");
384                 Assert.AreEqual (3, ts.Hours, "Hours");
385                 Assert.AreEqual (14, ts.Minutes, "Minutes");
386                 Assert.AreEqual (7, ts.Seconds, "Seconds");
387                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
388                 Assert.AreEqual (21474836470000000, ts.Ticks, "Ticks");
389         }
390
391         [Test]
392         public void MinSeconds () 
393         {
394                 TimeSpan ts = new TimeSpan (0, 0, 0, Int32.MinValue, 0);
395                 Assert.AreEqual (-24855, ts.Days, "Days");
396                 Assert.AreEqual (-3, ts.Hours, "Hours");
397                 Assert.AreEqual (-14, ts.Minutes, "Minutes");
398                 Assert.AreEqual (-8, ts.Seconds, "Seconds");
399                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
400                 Assert.AreEqual (-21474836480000000, ts.Ticks, "Ticks");
401         }
402
403         [Test]
404         public void MaxMilliseconds () 
405         {
406                 TimeSpan ts = new TimeSpan (0, 0, 0, 0, Int32.MaxValue);
407                 Assert.AreEqual (24, ts.Days, "Days");
408                 Assert.AreEqual (20, ts.Hours, "Hours");
409                 Assert.AreEqual (31, ts.Minutes, "Minutes");
410                 Assert.AreEqual (23, ts.Seconds, "Seconds");
411                 Assert.AreEqual (647, ts.Milliseconds, "Milliseconds");
412                 Assert.AreEqual (21474836470000, ts.Ticks, "Ticks");
413         }
414
415         [Test]
416         public void MinMilliseconds () 
417         {
418                 TimeSpan ts = new TimeSpan (0, 0, 0, 0, Int32.MinValue);
419                 Assert.AreEqual (-24, ts.Days, "Days");
420                 Assert.AreEqual (-20, ts.Hours, "Hours");
421                 Assert.AreEqual (-31, ts.Minutes, "Minutes");
422                 Assert.AreEqual (-23, ts.Seconds, "Seconds");
423                 Assert.AreEqual (-648, ts.Milliseconds, "Milliseconds");
424                 Assert.AreEqual (-21474836480000, ts.Ticks, "Ticks");
425         }
426
427         [Test]
428         public void NegativeTimeSpan () 
429         {
430                 TimeSpan ts = new TimeSpan (-23, -59, -59);
431                 Assert.AreEqual (0, ts.Days, "Days");
432                 Assert.AreEqual (-23, ts.Hours, "Hours");
433                 Assert.AreEqual (-59, ts.Minutes, "Minutes");
434                 Assert.AreEqual (-59, ts.Seconds, "Seconds");
435                 Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
436                 Assert.AreEqual (-863990000000, ts.Ticks, "Ticks");
437         }
438
439         public void TestProperties ()
440         {
441                 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
442                 TimeSpan t2 = -t1;
443
444                 Assert.AreEqual (1, t1.Days, "A1");
445                 Assert.AreEqual (2, t1.Hours, "A2");
446                 Assert.AreEqual (3, t1.Minutes, "A3");
447                 Assert.AreEqual (4, t1.Seconds, "A4");
448                 Assert.AreEqual (5, t1.Milliseconds, "A5");
449                 Assert.AreEqual (-1, t2.Days, "A6");
450                 Assert.AreEqual (-2, t2.Hours, "A7");
451                 Assert.AreEqual (-3, t2.Minutes, "A8");
452                 Assert.AreEqual (-4, t2.Seconds, "A9");
453                 Assert.AreEqual (-5, t2.Milliseconds, "A10");
454         }
455
456         public void TestAdd ()
457         {
458                 TimeSpan t1 = new TimeSpan (2,3,4,5,6);
459                 TimeSpan t2 = new TimeSpan (1,2,3,4,5);
460                 TimeSpan t3 = t1 + t2;
461                 TimeSpan t4 = t1.Add (t2);
462                 TimeSpan t5;
463                 bool exception;
464
465                 Assert.AreEqual (3, t3.Days, "A1");
466                 Assert.AreEqual (5, t3.Hours, "A2");
467                 Assert.AreEqual (7, t3.Minutes, "A3");
468                 Assert.AreEqual (9, t3.Seconds, "A4");
469                 Assert.AreEqual (11, t3.Milliseconds, "A5");
470                 Assert.AreEqual ("3.05:07:09.0110000", t4.ToString (), "A6");
471                 try
472                 {
473                         t5 = TimeSpan.MaxValue + new TimeSpan (1);                      
474                         exception = false;
475                 }
476                 catch (OverflowException)
477                 {
478                         exception = true;
479                 }
480                 Assert.IsTrue (exception, "A7");
481         }
482
483         public void TestCompare ()
484         {
485                 TimeSpan t1 = new TimeSpan (-1);
486                 TimeSpan t2 = new TimeSpan (1);
487                 int res;
488                 bool exception;
489
490                 Assert.AreEqual (-1, TimeSpan.Compare (t1, t2), "A1");
491                 Assert.AreEqual (1, TimeSpan.Compare (t2, t1), "A2");
492                 Assert.AreEqual (0, TimeSpan.Compare (t2, t2), "A3");
493                 Assert.AreEqual (-1, TimeSpan.Compare (TimeSpan.MinValue, TimeSpan.MaxValue), "A4");
494                 Assert.AreEqual (-1, t1.CompareTo (t2), "A5");
495                 Assert.AreEqual (1, t2.CompareTo (t1), "A6");
496                 Assert.AreEqual (0, t2.CompareTo (t2), "A7");
497                 Assert.AreEqual (-1, TimeSpan.Compare (TimeSpan.MinValue, TimeSpan.MaxValue), "A8");
498
499                 Assert.AreEqual (1, TimeSpan.Zero.CompareTo (null), "A9");
500                 
501                 try
502                 {
503                         res = TimeSpan.Zero.CompareTo("");
504                         exception = false;      
505                 }
506                 catch (ArgumentException)
507                 {
508                         exception = true;
509                 }
510                 Assert.IsTrue (exception, "A10");
511
512                 Assert.AreEqual (false, t1 == t2, "A11");
513                 Assert.AreEqual (false, t1 > t2, "A12");
514                 Assert.AreEqual (false, t1 >= t2, "A13");
515                 Assert.AreEqual (true, t1 != t2, "A14");
516                 Assert.AreEqual (true, t1 < t2, "A15");
517                 Assert.AreEqual (true, t1 <= t2, "A16");
518         }
519
520         [Test]
521         [ExpectedException (typeof (OverflowException))]
522         public void NoNegateMinValue() {
523                 TimeSpan t1 = TimeSpan.MinValue.Negate ();
524         }
525
526         public void TestNegateAndDuration ()
527         {
528                 TimeSpan t1;
529                 bool exception;
530
531                 Assert.AreEqual ("-00:00:00.0012345", new TimeSpan (12345).Negate ().ToString (), "A1");
532                 Assert.AreEqual ("00:00:00.0012345", new TimeSpan (-12345).Duration ().ToString (), "A2");
533                         
534                 try
535                 {
536                         t1 = TimeSpan.MinValue.Duration ();
537                         exception = false;
538                 }
539                 catch (OverflowException) {
540                         exception = true;
541                 }
542                 Assert.IsTrue (exception, "A4");
543
544                 Assert.AreEqual ("-00:00:00.0000077", (-(new TimeSpan (77))).ToString (), "A5");
545                 Assert.AreEqual ("00:00:00.0000077", (+(new TimeSpan(77))).ToString(), "A6");
546         }
547
548         public void TestEquals ()
549         {
550                 TimeSpan t1 = new TimeSpan (1);
551                 TimeSpan t2 = new TimeSpan (2);
552                 string s = "justastring";
553
554                 Assert.AreEqual (true, t1.Equals (t1), "A1");
555                 Assert.AreEqual (false, t1.Equals (t2), "A2");
556                 Assert.AreEqual (false, t1.Equals (s), "A3");
557                 Assert.AreEqual (false, t1.Equals (null), "A4");
558                 Assert.AreEqual (true, TimeSpan.Equals (t1, t1), "A5");
559                 Assert.AreEqual (false, TimeSpan.Equals (t1, t2), "A6");
560                 Assert.AreEqual (false, TimeSpan.Equals (t1, null), "A7");
561                 Assert.AreEqual (false, TimeSpan.Equals (t1, s), "A8");
562                 Assert.AreEqual (false, TimeSpan.Equals (s, t2), "A9");
563                 Assert.AreEqual (true, TimeSpan.Equals (null, null), "A10");
564         }
565
566         public void TestFromXXXX ()
567         {
568                 Assert.AreEqual ("12.08:16:48", TimeSpan.FromDays (12.345).ToString (), "A1");
569                 Assert.AreEqual ("12:20:42", TimeSpan.FromHours (12.345).ToString (), "A2");
570                 Assert.AreEqual ("00:12:20.7000000", TimeSpan.FromMinutes (12.345).ToString (), "A3");
571                 Assert.AreEqual ("00:00:12.3450000", TimeSpan.FromSeconds (12.345).ToString (), "A4");
572                 Assert.AreEqual ("00:00:00.0120000", TimeSpan.FromMilliseconds (12.345).ToString (), "A5");
573                 Assert.AreEqual ("00:00:00.0012345", TimeSpan.FromTicks (12345).ToString (), "A6");
574         }
575
576         [Test]
577         [ExpectedException (typeof (OverflowException))]
578         public void FromDays_MinValue ()
579         {
580                 TimeSpan.FromDays (Double.MinValue);
581         }
582
583         [Test]
584         [ExpectedException (typeof (OverflowException))]
585         public void FromDays_MaxValue ()
586         {
587                 TimeSpan.FromDays (Double.MaxValue);
588         }
589
590         [Test]
591         [ExpectedException (typeof (ArgumentException))]
592         public void FromDays_NaN ()
593         {
594                 TimeSpan.FromDays (Double.NaN);
595         }
596
597         [Test]
598         [ExpectedException (typeof (OverflowException))]
599         public void FromDays_PositiveInfinity ()
600         {
601                 // LAMESPEC: Document to return TimeSpan.MaxValue
602                 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromDays (Double.PositiveInfinity));
603         }
604
605         [Test]
606         [ExpectedException (typeof (OverflowException))]
607         public void FromDays_NegativeInfinity ()
608         {
609                 // LAMESPEC: Document to return TimeSpan.MinValue
610                 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromDays (Double.NegativeInfinity));
611         }
612
613         [Test]
614         [ExpectedException (typeof (OverflowException))]
615         public void FromHours_MinValue ()
616         {
617                 TimeSpan.FromHours (Double.MinValue);
618         }
619
620         [Test]
621         [ExpectedException (typeof (OverflowException))]
622         public void FromHours_MaxValue ()
623         {
624                 TimeSpan.FromHours (Double.MaxValue);
625         }
626
627         [Test]
628         [ExpectedException (typeof (ArgumentException))]
629         public void FromHours_NaN ()
630         {
631                 TimeSpan.FromHours (Double.NaN);
632         }
633
634         [Test]
635         [ExpectedException (typeof (OverflowException))]
636         public void FromHours_PositiveInfinity ()
637         {
638                 // LAMESPEC: Document to return TimeSpan.MaxValue
639                 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromHours (Double.PositiveInfinity));
640         }
641
642         [Test]
643         [ExpectedException (typeof (OverflowException))]
644         public void FromHours_NegativeInfinity ()
645         {
646                 // LAMESPEC: Document to return TimeSpan.MinValue
647                 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromHours (Double.NegativeInfinity));
648         }
649
650         [Test]
651         [ExpectedException (typeof (OverflowException))]
652         public void FromMilliseconds_MinValue ()
653         {
654                 TimeSpan.FromMilliseconds (Double.MinValue);
655         }
656
657         [Test]
658         [ExpectedException (typeof (OverflowException))]
659         public void FromMilliseconds_MaxValue ()
660         {
661                 TimeSpan.FromMilliseconds (Double.MaxValue);
662         }
663
664         [Test]
665         [ExpectedException (typeof (ArgumentException))]
666         public void FromMilliseconds_NaN ()
667         {
668                 TimeSpan.FromMilliseconds (Double.NaN);
669         }
670
671         [Test]
672         [ExpectedException (typeof (OverflowException))]
673         public void FromMilliseconds_PositiveInfinity ()
674         {
675                 // LAMESPEC: Document to return TimeSpan.MaxValue
676                 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromMilliseconds (Double.PositiveInfinity));
677         }
678
679         [Test]
680         [ExpectedException (typeof (OverflowException))]
681         public void FromMilliseconds_NegativeInfinity ()
682         {
683                 // LAMESPEC: Document to return TimeSpan.MinValue
684                 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromMilliseconds (Double.NegativeInfinity));
685         }
686
687         [Test]
688         [ExpectedException (typeof (OverflowException))]
689         public void FromMinutes_MinValue ()
690         {
691                 TimeSpan.FromMinutes (Double.MinValue);
692         }
693
694         [Test]
695         [ExpectedException (typeof (OverflowException))]
696         public void FromMinutes_MaxValue ()
697         {
698                 TimeSpan.FromMinutes (Double.MaxValue);
699         }
700
701         [Test]
702         [ExpectedException (typeof (ArgumentException))]
703         public void FromMinutes_NaN ()
704         {
705                 TimeSpan.FromMinutes (Double.NaN);
706         }
707
708         [Test]
709         [ExpectedException (typeof (OverflowException))]
710         public void FromMinutes_PositiveInfinity ()
711         {
712                 // LAMESPEC: Document to return TimeSpan.MaxValue
713                 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromMinutes (Double.PositiveInfinity));
714         }
715
716         [Test]
717         [ExpectedException (typeof (OverflowException))]
718         public void FromMinutes_NegativeInfinity ()
719         {
720                 // LAMESPEC: Document to return TimeSpan.MinValue
721                 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromMinutes (Double.NegativeInfinity));
722         }
723
724         [Test]
725         [ExpectedException (typeof (OverflowException))]
726         public void FromSeconds_MinValue ()
727         {
728                 TimeSpan.FromSeconds (Double.MinValue);
729         }
730
731         [Test]
732         [ExpectedException (typeof (OverflowException))]
733         public void FromSeconds_MaxValue ()
734         {
735                 TimeSpan.FromSeconds (Double.MaxValue);
736         }
737
738         [Test]
739         [ExpectedException (typeof (ArgumentException))]
740         public void FromSeconds_NaN ()
741         {
742                 TimeSpan.FromSeconds (Double.NaN);
743         }
744
745         [Test]
746         [ExpectedException (typeof (OverflowException))]
747         public void FromSeconds_PositiveInfinity ()
748         {
749                 // LAMESPEC: Document to return TimeSpan.MaxValue
750                 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromSeconds (Double.PositiveInfinity));
751         }
752
753         [Test]
754         [ExpectedException (typeof (OverflowException))]
755         public void FromSeconds_NegativeInfinity ()
756         {
757                 // LAMESPEC: Document to return TimeSpan.MinValue
758                 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromSeconds (Double.NegativeInfinity));
759         }
760
761         public void TestGetHashCode ()
762         {
763                 Assert.AreEqual (77, new TimeSpan (77).GetHashCode (), "A1");
764         }
765
766         private void ParseHelper (string s, bool expectFormat, bool expectOverflow, string expect)
767         {
768                 bool formatException = false;
769                 bool overflowException = false;
770                 string result = "junk ";
771
772                 try {
773                         result =  TimeSpan.Parse (s).ToString ();
774                 }
775                 catch (OverflowException) {
776                         overflowException = true;
777                 }
778                 catch (FormatException) {
779                         formatException = true;
780                 }
781                 Assert.AreEqual (expectFormat, formatException, "A1");
782                 Assert.AreEqual (expectOverflow, overflowException, "A2");
783
784                 if (!expectOverflow && !expectFormat) {
785                         Assert.AreEqual (expect, result, "A3");
786                 }
787         }
788
789         public void TestParse ()
790         {
791                 ParseHelper (" 13:45:15 ",false, false, "13:45:15");
792                 ParseHelper (" -1:2:3 ", false, false, "-01:02:03");
793
794                 ParseHelper (" 25:0:0 ",false, true, "dontcare");
795                 ParseHelper ("aaa", true, false, "dontcare");
796
797                 ParseHelper ("-21.23:59:59.9999999", false, false, "-21.23:59:59.9999999");
798
799                 ParseHelper ("100000000000000.1:1:1", false, true, "dontcare");
800                 ParseHelper ("24:60:60", false, true, "dontcare");
801                 ParseHelper ("0001:0002:0003.12     ", false, false, "01:02:03.1200000");
802
803                 ParseHelper (" 1:2:3:12345678 ", true, false, "dontcare"); 
804         }
805
806         // LAMESPEC: timespan in documentation is wrong - hh:mm:ss isn't mandatory
807         [Test]
808         public void Parse_Days_WithoutColon () 
809         {
810                 TimeSpan ts = TimeSpan.Parse ("1");
811                 Assert.AreEqual (1, ts.Days, "Days");
812         }
813
814         public void TestSubstract ()
815         {
816                 TimeSpan t1 = new TimeSpan (2,3,4,5,6);
817                 TimeSpan t2 = new TimeSpan (1,2,3,4,5);
818                 TimeSpan t3 = t1 - t2;
819                 TimeSpan t4 = t1.Subtract (t2);
820                 TimeSpan t5;
821                 bool exception;
822
823                 Assert.AreEqual ("1.01:01:01.0010000", t3.ToString (), "A1");
824                 Assert.AreEqual ("1.01:01:01.0010000", t4.ToString (), "A2");
825                 try {
826                         t5 = TimeSpan.MinValue - new TimeSpan (1);
827                         exception = false;
828                 }
829                 catch (OverflowException) {
830                         exception = true;
831                 }
832                 Assert.IsTrue (exception, "A3");
833         }
834
835         public void TestToString () 
836         {
837                 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
838                 TimeSpan t2 = -t1;
839                 
840                 Assert.AreEqual ("1.02:03:04.0050000", t1.ToString (), "A1");
841                 Assert.AreEqual ("-1.02:03:04.0050000", t2.ToString (), "A2");
842                 Assert.AreEqual ("10675199.02:48:05.4775807", TimeSpan.MaxValue.ToString (), "A3");
843                 Assert.AreEqual ("-10675199.02:48:05.4775808", TimeSpan.MinValue.ToString (), "A4");
844         }
845
846         [Test]
847         public void ToString_Constants () 
848         {
849                 Assert.AreEqual ("00:00:00", TimeSpan.Zero.ToString (), "Zero");
850                 Assert.AreEqual ("10675199.02:48:05.4775807", TimeSpan.MaxValue.ToString (), "MaxValue");
851                 Assert.AreEqual ("-10675199.02:48:05.4775808", TimeSpan.MinValue.ToString (), "MinValue");
852         }
853
854         [Test]
855         [ExpectedException (typeof (OverflowException))]
856         public void Parse_InvalidValuesAndFormat_ExceptionOrder () 
857         {
858                 // hours should be between 0 and 23 but format is also invalid (too many dots)
859                 TimeSpan.Parse ("0.99.99.0");
860         }
861
862         [Test]
863         public void Parse_MinMaxValues () 
864         {
865                 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.Parse ("10675199.02:48:05.4775807"), "MaxValue");
866                 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.Parse ("-10675199.02:48:05.4775808"), "MinValue");
867         }
868
869         [Test]
870         [ExpectedException (typeof (OverflowException))]
871         public void Parse_OverMaxValue() 
872         {
873                 TimeSpan.Parse ("10675199.02:48:05.4775808");
874         }
875
876         [Test]
877         [ExpectedException (typeof (OverflowException))]
878         public void Parse_UnderMinValue() 
879         {
880                 TimeSpan.Parse ("-10675199.02:48:05.4775809");
881         }
882
883         [Test]
884         public void ParseMissingSeconds ()
885         {
886                 // as seen in ML for http://resources.esri.com/arcgisserver/apis/silverlight/
887                 TimeSpan ts = TimeSpan.Parse ("0:0:.75");
888
889                 Assert.AreEqual (0, ts.Days, "Days");
890                 Assert.AreEqual (0, ts.Hours, "Hours");
891                 Assert.AreEqual (750, ts.Milliseconds, "Milliseconds");
892                 Assert.AreEqual (0, ts.Minutes, "Minutes");
893                 Assert.AreEqual (0, ts.Seconds, "Seconds");
894                 Assert.AreEqual (7500000, ts.Ticks, "Ticks");
895                 Assert.AreEqual (0.0000086805555555555555, ts.TotalDays, 0.00000000000000001, "TotalDays");
896                 Assert.AreEqual (0.00020833333333333332, ts.TotalHours, 0.00000000000000001, "TotalHours");
897                 Assert.AreEqual (750.0, ts.TotalMilliseconds, "TotalMilliseconds");
898                 Assert.AreEqual (0.0125, ts.TotalMinutes, "TotalMinutes");
899                 Assert.AreEqual (0.75, ts.TotalSeconds, "TotalSeconds");
900         }
901 }
902
903 }