2004-06-02 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 : Assertion {
20
21         public void TestCtors ()
22         {
23                 TimeSpan t1 = new TimeSpan (1234567890);
24
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 ());
36         }
37
38         [Test]
39         [ExpectedException (typeof (ArgumentOutOfRangeException))]
40         public void DaysOverflow () 
41         {
42                 int days = (int) (Int64.MaxValue / TimeSpan.TicksPerDay) + 1;
43                 TimeSpan ts = new TimeSpan (days, 0, 0, 0, 0);
44         }
45
46         [Test]
47         public void TemporaryOverflow () 
48         {
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);
54         }
55
56         [Test]
57         public void NoOverflowInHoursMinsSecondsMS () 
58         {
59                 TimeSpan ts = new TimeSpan (0, Int32.MaxValue, Int32.MaxValue, Int32.MaxValue, Int32.MaxValue);
60                 AssertEquals ("Ticks", 21496274706470000, ts.Ticks);
61         }
62
63         [Test]
64         public void NegativeTimeSpan () 
65         {
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);
71         }
72
73         public void TestProperties ()
74         {
75                 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
76                 TimeSpan t2 = -t1;
77
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);
88         }
89
90         public void TestAdd ()
91         {
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);
96                 TimeSpan t5;
97                 bool exception;
98
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 ());
105                 try
106                 {
107                         t5 = TimeSpan.MaxValue + new TimeSpan (1);                      
108                         exception = false;
109                 }
110                 catch (OverflowException)
111                 {
112                         exception = true;
113                 }
114                 Assert ("A7", exception);
115         }
116
117         public void TestCompare ()
118         {
119                 TimeSpan t1 = new TimeSpan (-1);
120                 TimeSpan t2 = new TimeSpan (1);
121                 int res;
122                 bool exception;
123
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));
132
133                 AssertEquals ("A9", 1, TimeSpan.Zero.CompareTo (null));
134                 
135                 try
136                 {
137                         res = TimeSpan.Zero.CompareTo("");
138                         exception = false;      
139                 }
140                 catch (ArgumentException)
141                 {
142                         exception = true;
143                 }
144                 Assert ("A10", exception);
145
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);
152         }
153
154         [Test]
155         [ExpectedException (typeof (OverflowException))]
156         public void NoNegateMinValue() {
157                 TimeSpan t1 = TimeSpan.MinValue.Negate ();
158         }
159
160         public void TestNegateAndDuration ()
161         {
162                 TimeSpan t1;
163                 bool exception;
164
165                 AssertEquals ("A1", "-00:00:00.0012345", new TimeSpan (12345).Negate ().ToString ());
166                 AssertEquals ("A2", "00:00:00.0012345", new TimeSpan (-12345).Duration ().ToString ());
167                         
168                 try
169                 {
170                         t1 = TimeSpan.MinValue.Duration ();
171                         exception = false;
172                 }
173                 catch (OverflowException) {
174                         exception = true;
175                 }
176                 Assert ("A4", exception);
177
178                 AssertEquals ("A5", "-00:00:00.0000077", (-(new TimeSpan (77))).ToString ());
179                 AssertEquals("A6", "00:00:00.0000077", (+(new TimeSpan(77))).ToString());
180         }
181
182         public void TestEquals ()
183         {
184                 TimeSpan t1 = new TimeSpan (1);
185                 TimeSpan t2 = new TimeSpan (2);
186                 string s = "justastring";
187
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));
198         }
199
200         public void TestFromXXXX ()
201         {
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 ());
208         }
209
210         [Test]
211         [ExpectedException (typeof (OverflowException))]
212         public void FromDays_MinValue ()
213         {
214                 TimeSpan.FromDays (Double.MinValue);
215         }
216
217         [Test]
218         [ExpectedException (typeof (OverflowException))]
219         public void FromDays_MaxValue ()
220         {
221                 TimeSpan.FromDays (Double.MaxValue);
222         }
223
224         [Test]
225         [ExpectedException (typeof (ArgumentException))]
226         public void FromDays_NaN ()
227         {
228                 TimeSpan.FromDays (Double.NaN);
229         }
230
231         [Test]
232         [ExpectedException (typeof (OverflowException))]
233         public void FromDays_PositiveInfinity ()
234         {
235                 // LAMESPEC: Document to return TimeSpan.MaxValue
236                 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromDays (Double.PositiveInfinity));
237         }
238
239         [Test]
240         [ExpectedException (typeof (OverflowException))]
241         public void FromDays_NegativeInfinity ()
242         {
243                 // LAMESPEC: Document to return TimeSpan.MinValue
244                 AssertEquals (TimeSpan.MinValue, TimeSpan.FromDays (Double.NegativeInfinity));
245         }
246
247         [Test]
248         [ExpectedException (typeof (OverflowException))]
249         public void FromHours_MinValue ()
250         {
251                 TimeSpan.FromHours (Double.MinValue);
252         }
253
254         [Test]
255         [ExpectedException (typeof (OverflowException))]
256         public void FromHours_MaxValue ()
257         {
258                 TimeSpan.FromHours (Double.MaxValue);
259         }
260
261         [Test]
262         [ExpectedException (typeof (ArgumentException))]
263         public void FromHours_NaN ()
264         {
265                 TimeSpan.FromHours (Double.NaN);
266         }
267
268         [Test]
269         [ExpectedException (typeof (OverflowException))]
270         public void FromHours_PositiveInfinity ()
271         {
272                 // LAMESPEC: Document to return TimeSpan.MaxValue
273                 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromHours (Double.PositiveInfinity));
274         }
275
276         [Test]
277         [ExpectedException (typeof (OverflowException))]
278         public void FromHours_NegativeInfinity ()
279         {
280                 // LAMESPEC: Document to return TimeSpan.MinValue
281                 AssertEquals (TimeSpan.MinValue, TimeSpan.FromHours (Double.NegativeInfinity));
282         }
283
284         [Test]
285         [ExpectedException (typeof (OverflowException))]
286         public void FromMilliseconds_MinValue ()
287         {
288                 TimeSpan.FromMilliseconds (Double.MinValue);
289         }
290
291         [Test]
292         [ExpectedException (typeof (OverflowException))]
293         public void FromMilliseconds_MaxValue ()
294         {
295                 TimeSpan.FromMilliseconds (Double.MaxValue);
296         }
297
298         [Test]
299         [ExpectedException (typeof (ArgumentException))]
300         public void FromMilliseconds_NaN ()
301         {
302                 TimeSpan.FromMilliseconds (Double.NaN);
303         }
304
305         [Test]
306         [ExpectedException (typeof (OverflowException))]
307         public void FromMilliseconds_PositiveInfinity ()
308         {
309                 // LAMESPEC: Document to return TimeSpan.MaxValue
310                 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromMilliseconds (Double.PositiveInfinity));
311         }
312
313         [Test]
314         [ExpectedException (typeof (OverflowException))]
315         public void FromMilliseconds_NegativeInfinity ()
316         {
317                 // LAMESPEC: Document to return TimeSpan.MinValue
318                 AssertEquals (TimeSpan.MinValue, TimeSpan.FromMilliseconds (Double.NegativeInfinity));
319         }
320
321         [Test]
322         [ExpectedException (typeof (OverflowException))]
323         public void FromMinutes_MinValue ()
324         {
325                 TimeSpan.FromMinutes (Double.MinValue);
326         }
327
328         [Test]
329         [ExpectedException (typeof (OverflowException))]
330         public void FromMinutes_MaxValue ()
331         {
332                 TimeSpan.FromMinutes (Double.MaxValue);
333         }
334
335         [Test]
336         [ExpectedException (typeof (ArgumentException))]
337         public void FromMinutes_NaN ()
338         {
339                 TimeSpan.FromMinutes (Double.NaN);
340         }
341
342         [Test]
343         [ExpectedException (typeof (OverflowException))]
344         public void FromMinutes_PositiveInfinity ()
345         {
346                 // LAMESPEC: Document to return TimeSpan.MaxValue
347                 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromMinutes (Double.PositiveInfinity));
348         }
349
350         [Test]
351         [ExpectedException (typeof (OverflowException))]
352         public void FromMinutes_NegativeInfinity ()
353         {
354                 // LAMESPEC: Document to return TimeSpan.MinValue
355                 AssertEquals (TimeSpan.MinValue, TimeSpan.FromMinutes (Double.NegativeInfinity));
356         }
357
358         [Test]
359         [ExpectedException (typeof (OverflowException))]
360         public void FromSeconds_MinValue ()
361         {
362                 TimeSpan.FromSeconds (Double.MinValue);
363         }
364
365         [Test]
366         [ExpectedException (typeof (OverflowException))]
367         public void FromSeconds_MaxValue ()
368         {
369                 TimeSpan.FromSeconds (Double.MaxValue);
370         }
371
372         [Test]
373         [ExpectedException (typeof (ArgumentException))]
374         public void FromSeconds_NaN ()
375         {
376                 TimeSpan.FromSeconds (Double.NaN);
377         }
378
379         [Test]
380         [ExpectedException (typeof (OverflowException))]
381         public void FromSeconds_PositiveInfinity ()
382         {
383                 // LAMESPEC: Document to return TimeSpan.MaxValue
384                 AssertEquals (TimeSpan.MaxValue, TimeSpan.FromSeconds (Double.PositiveInfinity));
385         }
386
387         [Test]
388         [ExpectedException (typeof (OverflowException))]
389         public void FromSeconds_NegativeInfinity ()
390         {
391                 // LAMESPEC: Document to return TimeSpan.MinValue
392                 AssertEquals (TimeSpan.MinValue, TimeSpan.FromSeconds (Double.NegativeInfinity));
393         }
394
395         public void TestGetHashCode ()
396         {
397                 AssertEquals ("A1", 77, new TimeSpan (77).GetHashCode ());
398         }
399
400         private void ParseHelper (string s, bool expectFormat, bool expectOverflow, string expect)
401         {
402                 bool formatException = false;
403                 bool overflowException = false;
404                 string result = "junk ";
405
406                 try {
407                         result =  TimeSpan.Parse (s).ToString ();
408                 }
409                 catch (OverflowException) {
410                         overflowException = true;
411                 }
412                 catch (FormatException) {
413                         formatException = true;
414                 }
415                 AssertEquals ("A1", expectFormat, formatException);
416                 AssertEquals ("A2", expectOverflow, overflowException);
417
418                 if (!expectOverflow && !expectFormat) {
419                         AssertEquals ("A3", expect, result);
420                 }
421         }
422
423         public void TestParse ()
424         {
425                 ParseHelper (" 13:45:15 ",false, false, "13:45:15");
426                 ParseHelper (" -1:2:3 ", false, false, "-01:02:03");
427
428                 ParseHelper (" 25:0:0 ",false, true, "dontcare");
429                 ParseHelper ("aaa", true, false, "dontcare");
430
431                 ParseHelper ("-21.23:59:59.9999999", false, false, "-21.23:59:59.9999999");
432
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");
436
437                 ParseHelper (" 1:2:3:12345678 ", true, false, "dontcare"); 
438         }
439
440         // LAMESPEC: timespan in documentation is wrong - hh:mm:ss isn't mandatory
441         [Test]
442         public void Parse_Days_WithoutColon () 
443         {
444                 TimeSpan ts = TimeSpan.Parse ("1");
445                 AssertEquals ("Days", 1, ts.Days);
446         }
447
448         public void TestSubstract ()
449         {
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);
454                 TimeSpan t5;
455                 bool exception;
456
457                 AssertEquals ("A1", "1.01:01:01.0010000", t3.ToString ());
458                 AssertEquals ("A2", "1.01:01:01.0010000", t4.ToString ());
459                 try {
460                         t5 = TimeSpan.MinValue - new TimeSpan (1);
461                         exception = false;
462                 }
463                 catch (OverflowException) {
464                         exception = true;
465                 }
466                 Assert ("A3", exception);
467         }
468
469         public void TestToString () 
470         {
471                 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
472                 TimeSpan t2 = -t1;
473                 
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 ());
478         }
479
480 }
481
482 }