Completed BitConverterTest, fixed bugs found in BitConverter.ToString methods
[mono.git] / mcs / class / corlib / Test / System / TimeSpanTest.cs
1 //
2 // TimeSpanTest.cs - NUnit Test Cases for the System.TimeSpan struct
3 //
4 // author:
5 //   Duco Fijma (duco@lorentz.xs4all.nl)
6 //
7 //   (C) 2001 Duco Fijma
8 //
9
10 using NUnit.Framework;
11 using System;
12
13 namespace MonoTests.System
14 {
15
16 public class TimeSpanTest : TestCase
17 {
18         public TimeSpanTest() : base ("MonoTests.System.TimeSpanTest testcase") {}
19         public TimeSpanTest (string name): base (name) {}
20
21         public static ITest Suite
22         {
23                 get {
24                         return new TestSuite (typeof(TimeSpanTest));
25                 }
26         }
27
28         public void TestCtors ()
29         {
30                 TimeSpan t1 = new TimeSpan (1234567890);
31
32                 AssertEquals ("A1", "00:02:03.4567890", t1.ToString ());
33                 t1 = new TimeSpan (1,2,3);
34                 AssertEquals ("A2", "01:02:03", t1.ToString ());
35                 t1 = new TimeSpan (1,2,3,4);
36                 AssertEquals ("A3", "1.02:03:04", t1.ToString ());
37                 t1 = new TimeSpan (1,2,3,4,5);
38                 AssertEquals ("A4", "1.02:03:04.0050000", t1.ToString ());
39                 t1 = new TimeSpan (-1,2,-3,4,-5);
40                 AssertEquals ("A5", "-22:02:56.0050000", t1.ToString ());
41                 t1 = new TimeSpan (0,25,0,0,0);
42                 AssertEquals ("A6", "1.01:00:00", t1.ToString ());
43         }
44
45         public void TestProperties ()
46         {
47                 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
48                 TimeSpan t2 = -t1;
49
50                 AssertEquals ("A1", 1, t1.Days);
51                 AssertEquals ("A2", 2, t1.Hours);
52                 AssertEquals ("A3", 3, t1.Minutes);
53                 AssertEquals ("A4", 4, t1.Seconds);
54                 AssertEquals ("A5", 5, t1.Milliseconds);
55                 AssertEquals ("A6", -1, t2.Days);
56                 AssertEquals ("A7", -2, t2.Hours);
57                 AssertEquals ("A8", -3, t2.Minutes);
58                 AssertEquals ("A9", -4, t2.Seconds);
59                 AssertEquals ("A10", -5, t2.Milliseconds);
60         }
61
62         public void TestAdd ()
63         {
64                 TimeSpan t1 = new TimeSpan (2,3,4,5,6);
65                 TimeSpan t2 = new TimeSpan (1,2,3,4,5);
66                 TimeSpan t3 = t1 + t2;
67                 TimeSpan t4 = t1.Add (t2);
68                 TimeSpan t5;
69                 bool exception;
70
71                 AssertEquals ("A1", 3, t3.Days);
72                 AssertEquals ("A2", 5, t3.Hours);
73                 AssertEquals ("A3", 7, t3.Minutes);
74                 AssertEquals ("A4", 9, t3.Seconds);
75                 AssertEquals ("A5", 11, t3.Milliseconds);
76                 AssertEquals ("A6", "3.05:07:09.0110000", t4.ToString ());
77                 try
78                 {
79                         t5 = TimeSpan.MaxValue + new TimeSpan (1);                      
80                         exception = false;
81                 }
82                 catch (OverflowException)
83                 {
84                         exception = true;
85                 }
86                 Assert ("A7", exception);
87         }
88
89         public void TestCompare ()
90         {
91                 TimeSpan t1 = new TimeSpan (-1);
92                 TimeSpan t2 = new TimeSpan (1);
93                 int res;
94                 bool exception;
95
96                 AssertEquals ("A1", -1, TimeSpan.Compare (t1, t2));
97                 AssertEquals ("A2", 1, TimeSpan.Compare (t2, t1));
98                 AssertEquals ("A3", 0, TimeSpan.Compare (t2, t2));
99                 AssertEquals ("A4", -1, TimeSpan.Compare (TimeSpan.MinValue, TimeSpan.MaxValue));
100                 AssertEquals ("A5", -1, t1.CompareTo (t2));
101                 AssertEquals ("A6", 1, t2.CompareTo (t1));
102                 AssertEquals ("A7", 0, t2.CompareTo (t2));
103                 AssertEquals ("A8", -1, TimeSpan.Compare (TimeSpan.MinValue, TimeSpan.MaxValue));
104
105                 AssertEquals ("A9", 1, TimeSpan.Zero.CompareTo (null));
106                 
107                 try
108                 {
109                         res = TimeSpan.Zero.CompareTo("");
110                         exception = false;      
111                 }
112                 catch (ArgumentException)
113                 {
114                         exception = true;
115                 }
116                 Assert ("A10", exception);
117
118                 AssertEquals ("A11", false, t1 == t2);
119                 AssertEquals ("A12", false, t1 > t2);
120                 AssertEquals ("A13", false, t1 >= t2);
121                 AssertEquals ("A14", true, t1 != t2);
122                 AssertEquals ("A15", true, t1 < t2);
123                 AssertEquals ("A16", true, t1 <= t2);
124         }
125
126         public void TestNegateAndDuration ()
127         {
128                 TimeSpan t1;
129                 bool exception;
130
131                 AssertEquals ("A1", "-00:00:00.0012345", new TimeSpan (12345).Negate ().ToString ());
132                 AssertEquals ("A2", "00:00:00.0012345", new TimeSpan (-12345).Duration ().ToString ());
133                         
134                 try
135                 {
136                         t1 = TimeSpan.MinValue.Negate ();
137                         exception = false;
138                 }
139                 catch (OverflowException)
140                 {
141                         exception = true;
142                 }
143                 Assert ("A3", exception);
144
145                 try
146                 {
147                         t1 = TimeSpan.MinValue.Duration ();
148                         exception = false;
149                 }
150                 catch (OverflowException) {
151                         exception = true;
152                 }
153                 Assert ("A4", exception);
154
155                 AssertEquals ("A5", "-00:00:00.0000077", (-(new TimeSpan (77))).ToString ());
156                 AssertEquals("A6", "00:00:00.0000077", (+(new TimeSpan(77))).ToString());
157         }
158
159         public void TestEquals ()
160         {
161                 TimeSpan t1 = new TimeSpan (1);
162                 TimeSpan t2 = new TimeSpan (2);
163                 string s = "justastring";
164
165                 AssertEquals ("A1", true, t1.Equals (t1));
166                 AssertEquals ("A2", false, t1.Equals (t2));
167                 AssertEquals ("A3", false, t1.Equals (s));
168                 AssertEquals ("A4", false, t1.Equals (null));
169                 AssertEquals ("A5", true, TimeSpan.Equals (t1, t1));
170                 AssertEquals ("A6", false, TimeSpan.Equals (t1, t2));
171                 AssertEquals ("A7", false, TimeSpan.Equals (t1, null));
172                 AssertEquals ("A8", false, TimeSpan.Equals (t1, s));
173                 AssertEquals ("A9", false, TimeSpan.Equals (s, t2));
174                 AssertEquals ("A10", true, TimeSpan.Equals (null,null));
175         }
176
177         public void TestFromXXXX ()
178         {
179                 AssertEquals ("A1", "12.08:16:48", TimeSpan.FromDays (12.345).ToString ());
180                 AssertEquals ("A2", "12:20:42", TimeSpan.FromHours (12.345).ToString ());
181                 AssertEquals ("A3", "00:12:20.7000000", TimeSpan.FromMinutes (12.345).ToString ());
182                 AssertEquals ("A4", "00:00:12.3450000", TimeSpan.FromSeconds (12.345).ToString ());
183                 AssertEquals ("A5", "00:00:00.0120000", TimeSpan.FromMilliseconds (12.345).ToString ());
184                 AssertEquals ("A6", "00:00:00.0012345", TimeSpan.FromTicks (12345).ToString ());
185         }
186
187         public void TestGetHashCode ()
188         {
189                 AssertEquals ("A1", 77, new TimeSpan (77).GetHashCode ());
190         }
191
192         private void TestParseHelper (string s, bool expectFormat, bool expectOverflow, string expect)
193         {
194                 bool formatException = false;
195                 bool overflowException = false;
196                 string result = "junk ";
197
198                 try {
199                         result =  TimeSpan.Parse (s).ToString ();
200                 }
201                 catch (OverflowException) {
202                         overflowException = true;
203                 }
204                 catch (FormatException) {
205                         formatException = true;
206                 }
207                 AssertEquals ("A1", expectFormat, formatException);
208                 AssertEquals ("A2", expectOverflow, overflowException);
209
210                 if (!expectOverflow && !expectFormat) {
211                         AssertEquals ("A3", expect, result);
212                 }
213         }
214
215         public void TestParse ()
216         {
217                 TestParseHelper (" 13:45:15 ",false, false, "13:45:15");
218                 TestParseHelper (" -1:2:3 ", false, false, "-01:02:03");
219
220                 TestParseHelper (" 25:0:0 ",false, true, "dontcare");
221                 TestParseHelper ("aaa", true, false, "dontcare");
222
223                 TestParseHelper ("-21.23:59:59.9999999", false, false, "-21.23:59:59.9999999");
224
225                 TestParseHelper ("100000000000000.1:1:1", false, true, "dontcare");
226                 TestParseHelper ("24:60:60", false, true, "dontcare");
227                 TestParseHelper ("0001:0002:0003.12     ", false, false, "01:02:03.1200000");
228
229                 TestParseHelper (" 1:2:3:12345678 ", true, false, "dontcare"); 
230         }
231
232         public void TestSubstract ()
233         {
234                 TimeSpan t1 = new TimeSpan (2,3,4,5,6);
235                 TimeSpan t2 = new TimeSpan (1,2,3,4,5);
236                 TimeSpan t3 = t1 - t2;
237                 TimeSpan t4 = t1.Subtract (t2);
238                 TimeSpan t5;
239                 bool exception;
240
241                 AssertEquals ("A1", "1.01:01:01.0010000", t3.ToString ());
242                 AssertEquals ("A2", "1.01:01:01.0010000", t4.ToString ());
243                 try {
244                         t5 = TimeSpan.MinValue - new TimeSpan (1);
245                         exception = false;
246                 }
247                 catch (OverflowException) {
248                         exception = true;
249                 }
250                 Assert ("A3", exception);
251         }
252
253         public void TestToString () 
254         {
255                 TimeSpan t1 = new TimeSpan (1,2,3,4,5);
256                 TimeSpan t2 = -t1;
257                 
258                 AssertEquals ("A1", "1.02:03:04.0050000", t1.ToString ());
259                 AssertEquals ("A2", "-1.02:03:04.0050000", t2.ToString ());
260                 AssertEquals ("A3", "10675199.02:48:05.4775807", TimeSpan.MaxValue.ToString ());
261                 AssertEquals ("A4", "-10675199.02:48:05.4775808", TimeSpan.MinValue.ToString ());
262         }
263
264 }
265
266 }