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 : Assertion {
21 public void TestCtors ()
23 TimeSpan t1 = new TimeSpan (1234567890);
25 AssertEquals ("A1", "00:02:03.4567890", t1.ToString ());
26 t1 = new TimeSpan (1,2,3);
27 AssertEquals ("A2", "01:02:03", t1.ToString ());
28 t1 = new TimeSpan (1,2,3,4);
29 AssertEquals ("A3", "1.02:03:04", t1.ToString ());
30 t1 = new TimeSpan (1,2,3,4,5);
31 AssertEquals ("A4", "1.02:03:04.0050000", t1.ToString ());
32 t1 = new TimeSpan (-1,2,-3,4,-5);
33 AssertEquals ("A5", "-22:02:56.0050000", t1.ToString ());
34 t1 = new TimeSpan (0,25,0,0,0);
35 AssertEquals ("A6", "1.01:00:00", t1.ToString ());
39 [ExpectedException (typeof (ArgumentOutOfRangeException))]
40 public void DaysOverflow ()
42 int days = (int) (Int64.MaxValue / TimeSpan.TicksPerDay) + 1;
43 TimeSpan ts = new TimeSpan (days, 0, 0, 0, 0);
47 public void TemporaryOverflow ()
49 // calculating part of this results in overflow (days)
50 // but the negative hours, minutes, seconds & ms correct this
51 int days = (int) (Int64.MaxValue / TimeSpan.TicksPerDay) + 1;
52 TimeSpan ts = new TimeSpan (days, Int32.MinValue, Int32.MinValue, Int32.MinValue, Int32.MinValue);
53 AssertEquals ("Ticks", 9201876488683520000, ts.Ticks);
57 public void NoOverflowInHoursMinsSecondsMS ()
59 TimeSpan ts = new TimeSpan (0, Int32.MaxValue, Int32.MaxValue, Int32.MaxValue, Int32.MaxValue);
60 AssertEquals ("Ticks", 21496274706470000, ts.Ticks);
64 public void NegativeTimeSpan ()
66 TimeSpan ts = new TimeSpan (-23, -59, -59);
67 AssertEquals ("Hours", -23, ts.Hours);
68 AssertEquals ("Minutes", -59, ts.Minutes);
69 AssertEquals ("Seconds", -59, ts.Seconds);
70 AssertEquals ("Ticks", -863990000000, ts.Ticks);
73 public void TestProperties ()
75 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
78 AssertEquals ("A1", 1, t1.Days);
79 AssertEquals ("A2", 2, t1.Hours);
80 AssertEquals ("A3", 3, t1.Minutes);
81 AssertEquals ("A4", 4, t1.Seconds);
82 AssertEquals ("A5", 5, t1.Milliseconds);
83 AssertEquals ("A6", -1, t2.Days);
84 AssertEquals ("A7", -2, t2.Hours);
85 AssertEquals ("A8", -3, t2.Minutes);
86 AssertEquals ("A9", -4, t2.Seconds);
87 AssertEquals ("A10", -5, t2.Milliseconds);
90 public void TestAdd ()
92 TimeSpan t1 = new TimeSpan (2,3,4,5,6);
93 TimeSpan t2 = new TimeSpan (1,2,3,4,5);
94 TimeSpan t3 = t1 + t2;
95 TimeSpan t4 = t1.Add (t2);
99 AssertEquals ("A1", 3, t3.Days);
100 AssertEquals ("A2", 5, t3.Hours);
101 AssertEquals ("A3", 7, t3.Minutes);
102 AssertEquals ("A4", 9, t3.Seconds);
103 AssertEquals ("A5", 11, t3.Milliseconds);
104 AssertEquals ("A6", "3.05:07:09.0110000", t4.ToString ());
107 t5 = TimeSpan.MaxValue + new TimeSpan (1);
110 catch (OverflowException)
114 Assert ("A7", exception);
117 public void TestCompare ()
119 TimeSpan t1 = new TimeSpan (-1);
120 TimeSpan t2 = new TimeSpan (1);
124 AssertEquals ("A1", -1, TimeSpan.Compare (t1, t2));
125 AssertEquals ("A2", 1, TimeSpan.Compare (t2, t1));
126 AssertEquals ("A3", 0, TimeSpan.Compare (t2, t2));
127 AssertEquals ("A4", -1, TimeSpan.Compare (TimeSpan.MinValue, TimeSpan.MaxValue));
128 AssertEquals ("A5", -1, t1.CompareTo (t2));
129 AssertEquals ("A6", 1, t2.CompareTo (t1));
130 AssertEquals ("A7", 0, t2.CompareTo (t2));
131 AssertEquals ("A8", -1, TimeSpan.Compare (TimeSpan.MinValue, TimeSpan.MaxValue));
133 AssertEquals ("A9", 1, TimeSpan.Zero.CompareTo (null));
137 res = TimeSpan.Zero.CompareTo("");
140 catch (ArgumentException)
144 Assert ("A10", exception);
146 AssertEquals ("A11", false, t1 == t2);
147 AssertEquals ("A12", false, t1 > t2);
148 AssertEquals ("A13", false, t1 >= t2);
149 AssertEquals ("A14", true, t1 != t2);
150 AssertEquals ("A15", true, t1 < t2);
151 AssertEquals ("A16", true, t1 <= t2);
155 [ExpectedException (typeof (OverflowException))]
156 public void NoNegateMinValue() {
157 TimeSpan t1 = TimeSpan.MinValue.Negate ();
160 public void TestNegateAndDuration ()
165 AssertEquals ("A1", "-00:00:00.0012345", new TimeSpan (12345).Negate ().ToString ());
166 AssertEquals ("A2", "00:00:00.0012345", new TimeSpan (-12345).Duration ().ToString ());
170 t1 = TimeSpan.MinValue.Duration ();
173 catch (OverflowException) {
176 Assert ("A4", exception);
178 AssertEquals ("A5", "-00:00:00.0000077", (-(new TimeSpan (77))).ToString ());
179 AssertEquals("A6", "00:00:00.0000077", (+(new TimeSpan(77))).ToString());
182 public void TestEquals ()
184 TimeSpan t1 = new TimeSpan (1);
185 TimeSpan t2 = new TimeSpan (2);
186 string s = "justastring";
188 AssertEquals ("A1", true, t1.Equals (t1));
189 AssertEquals ("A2", false, t1.Equals (t2));
190 AssertEquals ("A3", false, t1.Equals (s));
191 AssertEquals ("A4", false, t1.Equals (null));
192 AssertEquals ("A5", true, TimeSpan.Equals (t1, t1));
193 AssertEquals ("A6", false, TimeSpan.Equals (t1, t2));
194 AssertEquals ("A7", false, TimeSpan.Equals (t1, null));
195 AssertEquals ("A8", false, TimeSpan.Equals (t1, s));
196 AssertEquals ("A9", false, TimeSpan.Equals (s, t2));
197 AssertEquals ("A10", true, TimeSpan.Equals (null,null));
200 public void TestFromXXXX ()
202 AssertEquals ("A1", "12.08:16:48", TimeSpan.FromDays (12.345).ToString ());
203 AssertEquals ("A2", "12:20:42", TimeSpan.FromHours (12.345).ToString ());
204 AssertEquals ("A3", "00:12:20.7000000", TimeSpan.FromMinutes (12.345).ToString ());
205 AssertEquals ("A4", "00:00:12.3450000", TimeSpan.FromSeconds (12.345).ToString ());
206 AssertEquals ("A5", "00:00:00.0120000", TimeSpan.FromMilliseconds (12.345).ToString ());
207 AssertEquals ("A6", "00:00:00.0012345", TimeSpan.FromTicks (12345).ToString ());
211 [ExpectedException (typeof (OverflowException))]
212 public void FromDays_MinValue ()
214 TimeSpan.FromDays (Double.MinValue);
218 [ExpectedException (typeof (OverflowException))]
219 public void FromDays_MaxValue ()
221 TimeSpan.FromDays (Double.MaxValue);
225 [ExpectedException (typeof (ArgumentException))]
226 public void FromDays_NaN ()
228 TimeSpan.FromDays (Double.NaN);
232 [ExpectedException (typeof (OverflowException))]
233 public void FromDays_PositiveInfinity ()
235 // LAMESPEC: Document to return TimeSpan.MaxValue
236 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromDays (Double.PositiveInfinity));
240 [ExpectedException (typeof (OverflowException))]
241 public void FromDays_NegativeInfinity ()
243 // LAMESPEC: Document to return TimeSpan.MinValue
244 AssertEquals (TimeSpan.MinValue, TimeSpan.FromDays (Double.NegativeInfinity));
248 [ExpectedException (typeof (OverflowException))]
249 public void FromHours_MinValue ()
251 TimeSpan.FromHours (Double.MinValue);
255 [ExpectedException (typeof (OverflowException))]
256 public void FromHours_MaxValue ()
258 TimeSpan.FromHours (Double.MaxValue);
262 [ExpectedException (typeof (ArgumentException))]
263 public void FromHours_NaN ()
265 TimeSpan.FromHours (Double.NaN);
269 [ExpectedException (typeof (OverflowException))]
270 public void FromHours_PositiveInfinity ()
272 // LAMESPEC: Document to return TimeSpan.MaxValue
273 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromHours (Double.PositiveInfinity));
277 [ExpectedException (typeof (OverflowException))]
278 public void FromHours_NegativeInfinity ()
280 // LAMESPEC: Document to return TimeSpan.MinValue
281 AssertEquals (TimeSpan.MinValue, TimeSpan.FromHours (Double.NegativeInfinity));
285 [ExpectedException (typeof (OverflowException))]
286 public void FromMilliseconds_MinValue ()
288 TimeSpan.FromMilliseconds (Double.MinValue);
292 [ExpectedException (typeof (OverflowException))]
293 public void FromMilliseconds_MaxValue ()
295 TimeSpan.FromMilliseconds (Double.MaxValue);
299 [ExpectedException (typeof (ArgumentException))]
300 public void FromMilliseconds_NaN ()
302 TimeSpan.FromMilliseconds (Double.NaN);
306 [ExpectedException (typeof (OverflowException))]
307 public void FromMilliseconds_PositiveInfinity ()
309 // LAMESPEC: Document to return TimeSpan.MaxValue
310 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromMilliseconds (Double.PositiveInfinity));
314 [ExpectedException (typeof (OverflowException))]
315 public void FromMilliseconds_NegativeInfinity ()
317 // LAMESPEC: Document to return TimeSpan.MinValue
318 AssertEquals (TimeSpan.MinValue, TimeSpan.FromMilliseconds (Double.NegativeInfinity));
322 [ExpectedException (typeof (OverflowException))]
323 public void FromMinutes_MinValue ()
325 TimeSpan.FromMinutes (Double.MinValue);
329 [ExpectedException (typeof (OverflowException))]
330 public void FromMinutes_MaxValue ()
332 TimeSpan.FromMinutes (Double.MaxValue);
336 [ExpectedException (typeof (ArgumentException))]
337 public void FromMinutes_NaN ()
339 TimeSpan.FromMinutes (Double.NaN);
343 [ExpectedException (typeof (OverflowException))]
344 public void FromMinutes_PositiveInfinity ()
346 // LAMESPEC: Document to return TimeSpan.MaxValue
347 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromMinutes (Double.PositiveInfinity));
351 [ExpectedException (typeof (OverflowException))]
352 public void FromMinutes_NegativeInfinity ()
354 // LAMESPEC: Document to return TimeSpan.MinValue
355 AssertEquals (TimeSpan.MinValue, TimeSpan.FromMinutes (Double.NegativeInfinity));
359 [ExpectedException (typeof (OverflowException))]
360 public void FromSeconds_MinValue ()
362 TimeSpan.FromSeconds (Double.MinValue);
366 [ExpectedException (typeof (OverflowException))]
367 public void FromSeconds_MaxValue ()
369 TimeSpan.FromSeconds (Double.MaxValue);
373 [ExpectedException (typeof (ArgumentException))]
374 public void FromSeconds_NaN ()
376 TimeSpan.FromSeconds (Double.NaN);
380 [ExpectedException (typeof (OverflowException))]
381 public void FromSeconds_PositiveInfinity ()
383 // LAMESPEC: Document to return TimeSpan.MaxValue
384 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromSeconds (Double.PositiveInfinity));
388 [ExpectedException (typeof (OverflowException))]
389 public void FromSeconds_NegativeInfinity ()
391 // LAMESPEC: Document to return TimeSpan.MinValue
392 AssertEquals (TimeSpan.MinValue, TimeSpan.FromSeconds (Double.NegativeInfinity));
395 public void TestGetHashCode ()
397 AssertEquals ("A1", 77, new TimeSpan (77).GetHashCode ());
400 private void ParseHelper (string s, bool expectFormat, bool expectOverflow, string expect)
402 bool formatException = false;
403 bool overflowException = false;
404 string result = "junk ";
407 result = TimeSpan.Parse (s).ToString ();
409 catch (OverflowException) {
410 overflowException = true;
412 catch (FormatException) {
413 formatException = true;
415 AssertEquals ("A1", expectFormat, formatException);
416 AssertEquals ("A2", expectOverflow, overflowException);
418 if (!expectOverflow && !expectFormat) {
419 AssertEquals ("A3", expect, result);
423 public void TestParse ()
425 ParseHelper (" 13:45:15 ",false, false, "13:45:15");
426 ParseHelper (" -1:2:3 ", false, false, "-01:02:03");
428 ParseHelper (" 25:0:0 ",false, true, "dontcare");
429 ParseHelper ("aaa", true, false, "dontcare");
431 ParseHelper ("-21.23:59:59.9999999", false, false, "-21.23:59:59.9999999");
433 ParseHelper ("100000000000000.1:1:1", false, true, "dontcare");
434 ParseHelper ("24:60:60", false, true, "dontcare");
435 ParseHelper ("0001:0002:0003.12 ", false, false, "01:02:03.1200000");
437 ParseHelper (" 1:2:3:12345678 ", true, false, "dontcare");
440 // LAMESPEC: timespan in documentation is wrong - hh:mm:ss isn't mandatory
442 public void Parse_Days_WithoutColon ()
444 TimeSpan ts = TimeSpan.Parse ("1");
445 AssertEquals ("Days", 1, ts.Days);
448 public void TestSubstract ()
450 TimeSpan t1 = new TimeSpan (2,3,4,5,6);
451 TimeSpan t2 = new TimeSpan (1,2,3,4,5);
452 TimeSpan t3 = t1 - t2;
453 TimeSpan t4 = t1.Subtract (t2);
457 AssertEquals ("A1", "1.01:01:01.0010000", t3.ToString ());
458 AssertEquals ("A2", "1.01:01:01.0010000", t4.ToString ());
460 t5 = TimeSpan.MinValue - new TimeSpan (1);
463 catch (OverflowException) {
466 Assert ("A3", exception);
469 public void TestToString ()
471 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
474 AssertEquals ("A1", "1.02:03:04.0050000", t1.ToString ());
475 AssertEquals ("A2", "-1.02:03:04.0050000", t2.ToString ());
476 AssertEquals ("A3", "10675199.02:48:05.4775807", TimeSpan.MaxValue.ToString ());
477 AssertEquals ("A4", "-10675199.02:48:05.4775808", TimeSpan.MinValue.ToString ());