2 // TimeSpanTest.cs - NUnit Test Cases for the System.TimeSpan struct
5 // Duco Fijma (duco@lorentz.xs4all.nl)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2004 Novell (http://www.novell.com)
12 using NUnit.Framework;
15 namespace MonoTests.System
19 public class TimeSpanTest {
21 private void Debug (TimeSpan ts)
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);
31 public void TestCtors ()
33 TimeSpan t1 = new TimeSpan (1234567890);
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");
49 [ExpectedException (typeof (ArgumentOutOfRangeException))]
50 public void DaysOverflow ()
52 int days = (int) (Int64.MaxValue / TimeSpan.TicksPerDay) + 1;
53 TimeSpan ts = new TimeSpan (days, 0, 0, 0, 0);
58 [ExpectedException (typeof (ArgumentOutOfRangeException))]
59 [Category ("NotWorking")]
61 public void TemporaryOverflow ()
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");
77 [ExpectedException (typeof (ArgumentOutOfRangeException))]
78 [Category ("NotWorking")]
80 public void NoOverflowInHoursMinsSecondsMS ()
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");
92 [ExpectedException (typeof (ArgumentOutOfRangeException))]
93 public void MaxDays ()
95 new TimeSpan (Int32.MaxValue, 0, 0, 0, 0);
99 [ExpectedException (typeof (ArgumentOutOfRangeException))]
100 public void MinDays ()
102 new TimeSpan (Int32.MinValue, 0, 0, 0, 0);
106 [Ignore ("too long")]
107 public void MaxHours_TooLong ()
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);
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");
125 [ExpectedException (typeof (ArgumentOutOfRangeException))]
126 [Category ("NotWorking")]
128 public void MaxHours ()
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");
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");
150 [ExpectedException (typeof (ArgumentOutOfRangeException))]
151 [Category ("NotWorking")]
153 public void MaxHours_BreakPoint ()
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");
165 [Ignore ("too long")]
166 public void MinHours_TooLong ()
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");
184 [Category ("NotWorking")]
186 public void MinHours ()
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");
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");
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");
218 [ExpectedException (typeof (ArgumentOutOfRangeException))]
219 [Category ("NotWorking")]
221 public void MinHours_BreakPoint ()
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");
233 [Ignore ("too long")]
234 public void MaxMinutes_TooLong ()
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);
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");
252 [Category ("NotWorking")]
254 public void MaxMinutes ()
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");
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");
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");
287 [ExpectedException (typeof (ArgumentOutOfRangeException))]
288 [Category ("NotWorking")]
290 public void MaxMinutes_BreakPoint ()
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");
302 [Ignore ("too long")]
303 public void MinMinutes_TooLong ()
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");
321 [Category ("NotWorking")]
323 public void MinMinutes ()
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");
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");
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");
356 [Category ("NotWorking")]
358 public void MinMinutes_BreakPoint ()
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");
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");
380 public void MaxSeconds ()
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");
392 public void MinSeconds ()
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");
404 public void MaxMilliseconds ()
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");
416 public void MinMilliseconds ()
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");
428 public void NegativeTimeSpan ()
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");
439 public void TestProperties ()
441 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
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");
456 public void TestAdd ()
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);
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");
473 t5 = TimeSpan.MaxValue + new TimeSpan (1);
476 catch (OverflowException)
480 Assert.IsTrue (exception, "A7");
483 public void TestCompare ()
485 TimeSpan t1 = new TimeSpan (-1);
486 TimeSpan t2 = new TimeSpan (1);
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");
499 Assert.AreEqual (1, TimeSpan.Zero.CompareTo (null), "A9");
503 res = TimeSpan.Zero.CompareTo("");
506 catch (ArgumentException)
510 Assert.IsTrue (exception, "A10");
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");
521 [ExpectedException (typeof (OverflowException))]
522 public void NoNegateMinValue() {
523 TimeSpan t1 = TimeSpan.MinValue.Negate ();
526 public void TestNegateAndDuration ()
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");
536 t1 = TimeSpan.MinValue.Duration ();
539 catch (OverflowException) {
542 Assert.IsTrue (exception, "A4");
544 Assert.AreEqual ("-00:00:00.0000077", (-(new TimeSpan (77))).ToString (), "A5");
545 Assert.AreEqual ("00:00:00.0000077", (+(new TimeSpan(77))).ToString(), "A6");
548 public void TestEquals ()
550 TimeSpan t1 = new TimeSpan (1);
551 TimeSpan t2 = new TimeSpan (2);
552 string s = "justastring";
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");
566 public void TestFromXXXX ()
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");
577 [ExpectedException (typeof (OverflowException))]
578 public void FromDays_MinValue ()
580 TimeSpan.FromDays (Double.MinValue);
584 [ExpectedException (typeof (OverflowException))]
585 public void FromDays_MaxValue ()
587 TimeSpan.FromDays (Double.MaxValue);
591 [ExpectedException (typeof (ArgumentException))]
592 public void FromDays_NaN ()
594 TimeSpan.FromDays (Double.NaN);
598 [ExpectedException (typeof (OverflowException))]
599 public void FromDays_PositiveInfinity ()
601 // LAMESPEC: Document to return TimeSpan.MaxValue
602 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromDays (Double.PositiveInfinity));
606 [ExpectedException (typeof (OverflowException))]
607 public void FromDays_NegativeInfinity ()
609 // LAMESPEC: Document to return TimeSpan.MinValue
610 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromDays (Double.NegativeInfinity));
614 [ExpectedException (typeof (OverflowException))]
615 public void FromHours_MinValue ()
617 TimeSpan.FromHours (Double.MinValue);
621 [ExpectedException (typeof (OverflowException))]
622 public void FromHours_MaxValue ()
624 TimeSpan.FromHours (Double.MaxValue);
628 [ExpectedException (typeof (ArgumentException))]
629 public void FromHours_NaN ()
631 TimeSpan.FromHours (Double.NaN);
635 [ExpectedException (typeof (OverflowException))]
636 public void FromHours_PositiveInfinity ()
638 // LAMESPEC: Document to return TimeSpan.MaxValue
639 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromHours (Double.PositiveInfinity));
643 [ExpectedException (typeof (OverflowException))]
644 public void FromHours_NegativeInfinity ()
646 // LAMESPEC: Document to return TimeSpan.MinValue
647 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromHours (Double.NegativeInfinity));
651 [ExpectedException (typeof (OverflowException))]
652 public void FromMilliseconds_MinValue ()
654 TimeSpan.FromMilliseconds (Double.MinValue);
658 [ExpectedException (typeof (OverflowException))]
659 public void FromMilliseconds_MaxValue ()
661 TimeSpan.FromMilliseconds (Double.MaxValue);
665 [ExpectedException (typeof (ArgumentException))]
666 public void FromMilliseconds_NaN ()
668 TimeSpan.FromMilliseconds (Double.NaN);
672 [ExpectedException (typeof (OverflowException))]
673 public void FromMilliseconds_PositiveInfinity ()
675 // LAMESPEC: Document to return TimeSpan.MaxValue
676 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromMilliseconds (Double.PositiveInfinity));
680 [ExpectedException (typeof (OverflowException))]
681 public void FromMilliseconds_NegativeInfinity ()
683 // LAMESPEC: Document to return TimeSpan.MinValue
684 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromMilliseconds (Double.NegativeInfinity));
688 [ExpectedException (typeof (OverflowException))]
689 public void FromMinutes_MinValue ()
691 TimeSpan.FromMinutes (Double.MinValue);
695 [ExpectedException (typeof (OverflowException))]
696 public void FromMinutes_MaxValue ()
698 TimeSpan.FromMinutes (Double.MaxValue);
702 [ExpectedException (typeof (ArgumentException))]
703 public void FromMinutes_NaN ()
705 TimeSpan.FromMinutes (Double.NaN);
709 [ExpectedException (typeof (OverflowException))]
710 public void FromMinutes_PositiveInfinity ()
712 // LAMESPEC: Document to return TimeSpan.MaxValue
713 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromMinutes (Double.PositiveInfinity));
717 [ExpectedException (typeof (OverflowException))]
718 public void FromMinutes_NegativeInfinity ()
720 // LAMESPEC: Document to return TimeSpan.MinValue
721 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromMinutes (Double.NegativeInfinity));
725 [ExpectedException (typeof (OverflowException))]
726 public void FromSeconds_MinValue ()
728 TimeSpan.FromSeconds (Double.MinValue);
732 [ExpectedException (typeof (OverflowException))]
733 public void FromSeconds_MaxValue ()
735 TimeSpan.FromSeconds (Double.MaxValue);
739 [ExpectedException (typeof (ArgumentException))]
740 public void FromSeconds_NaN ()
742 TimeSpan.FromSeconds (Double.NaN);
746 [ExpectedException (typeof (OverflowException))]
747 public void FromSeconds_PositiveInfinity ()
749 // LAMESPEC: Document to return TimeSpan.MaxValue
750 Assert.AreEqual (TimeSpan.MaxValue, TimeSpan.FromSeconds (Double.PositiveInfinity));
754 [ExpectedException (typeof (OverflowException))]
755 public void FromSeconds_NegativeInfinity ()
757 // LAMESPEC: Document to return TimeSpan.MinValue
758 Assert.AreEqual (TimeSpan.MinValue, TimeSpan.FromSeconds (Double.NegativeInfinity));
761 public void TestGetHashCode ()
763 Assert.AreEqual (77, new TimeSpan (77).GetHashCode (), "A1");
766 private void ParseHelper (string s, bool expectFormat, bool expectOverflow, string expect)
768 bool formatException = false;
769 bool overflowException = false;
770 string result = "junk ";
773 result = TimeSpan.Parse (s).ToString ();
775 catch (OverflowException) {
776 overflowException = true;
778 catch (FormatException) {
779 formatException = true;
781 Assert.AreEqual (expectFormat, formatException, "A1");
782 Assert.AreEqual (expectOverflow, overflowException, "A2");
784 if (!expectOverflow && !expectFormat) {
785 Assert.AreEqual (expect, result, "A3");
789 public void TestParse ()
791 ParseHelper (" 13:45:15 ",false, false, "13:45:15");
792 ParseHelper (" -1:2:3 ", false, false, "-01:02:03");
794 ParseHelper (" 25:0:0 ",false, true, "dontcare");
795 ParseHelper ("aaa", true, false, "dontcare");
797 ParseHelper ("-21.23:59:59.9999999", false, false, "-21.23:59:59.9999999");
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");
803 ParseHelper (" 1:2:3:12345678 ", true, false, "dontcare");
806 // LAMESPEC: timespan in documentation is wrong - hh:mm:ss isn't mandatory
808 public void Parse_Days_WithoutColon ()
810 TimeSpan ts = TimeSpan.Parse ("1");
811 Assert.AreEqual (1, ts.Days, "Days");
814 public void TestSubstract ()
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);
823 Assert.AreEqual ("1.01:01:01.0010000", t3.ToString (), "A1");
824 Assert.AreEqual ("1.01:01:01.0010000", t4.ToString (), "A2");
826 t5 = TimeSpan.MinValue - new TimeSpan (1);
829 catch (OverflowException) {
832 Assert.IsTrue (exception, "A3");
835 public void TestToString ()
837 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
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");
847 public void ToString_Constants ()
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");
855 [ExpectedException (typeof (OverflowException))]
856 public void Parse_InvalidValuesAndFormat_ExceptionOrder ()
858 // hours should be between 0 and 23 but format is also invalid (too many dots)
859 TimeSpan.Parse ("0.99.99.0");
863 public void Parse_MinMaxValues ()
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");
870 [ExpectedException (typeof (OverflowException))]
871 public void Parse_OverMaxValue()
873 TimeSpan.Parse ("10675199.02:48:05.4775808");
877 [ExpectedException (typeof (OverflowException))]
878 public void Parse_UnderMinValue()
880 TimeSpan.Parse ("-10675199.02:48:05.4775809");
884 public void ParseMissingSeconds ()
886 // as seen in ML for http://resources.esri.com/arcgisserver/apis/silverlight/
887 TimeSpan ts = TimeSpan.Parse ("0:0:.75");
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");