Update PointConverter.cs
[mono.git] / mcs / class / corlib / Test / System / GuidTest.cs
1 //
2 // GuidTest.cs - NUnit Test Cases for the System.Guid struct
3 //
4 // Authors:
5 //      Duco Fijma (duco@lorentz.xs4all.nl)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Duco Fijma
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11
12 using NUnit.Framework;
13 using System;
14
15 namespace MonoTests.System {
16
17         [TestFixture]
18         public class GuidTest {
19
20                 [Test]
21                 [ExpectedException (typeof (ArgumentNullException))]
22                 public void Constructor_ByteArray_Null ()
23                 {
24                         new Guid ((byte[]) null);
25                 }
26
27                 [Test]
28                 [ExpectedException (typeof (ArgumentException))]
29                 public void Constructor_ByteArray_InvalidLength ()
30                 {
31                         new Guid (new byte[] {0x00, 0x01, 0x02});
32                 }
33
34                 [Test]
35                 public void Constructor_ByteArray ()
36                 {
37                         Guid g =  new Guid (new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f});
38                         Assert.AreEqual ("03020100-0504-0706-0809-0a0b0c0d0e0f", g.ToString ());
39                 }
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void Constructor_String_Null ()
44                 {
45                         new Guid ((string) null);
46                 }
47
48                 [Test]
49                 public void Constructor_String ()
50                 {
51                         Guid g0 = new Guid ("000102030405060708090a0b0c0d0e0f"); 
52                         Guid g1 = new Guid ("00010203-0405-0607-0809-0a0b0c0d0e0f"); 
53                         Guid g2 = new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}"); 
54                         Guid g3 = new Guid ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}");
55                         Guid g4 = new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)");
56                         Guid g5 = new Guid ("\n  \r  \n 00010203-0405-0607-0809-0a0b0c0d0e0f \r\n");
57                         string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
58                         Assert.AreEqual (expected, g0.ToString (), "A0");
59                         Assert.AreEqual (expected, g1.ToString (), "A1");
60                         Assert.AreEqual (expected, g2.ToString (), "A2");
61                         Assert.AreEqual (expected, g3.ToString (), "A3");
62                         Assert.AreEqual (expected, g4.ToString (), "A4");
63                         Assert.AreEqual (expected, g5.ToString (), "A5");
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (FormatException))]
68                 public void Constructor_String_Invalid ()
69                 {
70                         new Guid ("invalid");
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (FormatException))]
75                 public void Constructor_String_MissingAllSeparators ()
76                 {
77                         new Guid ("{000102030405060708090A0B0C0D0E0F}");        // missing all -
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (FormatException))]
82                 public void Constructor_String_MissingSeparator ()
83                 {
84                         new Guid ("000102030405-0607-0809-0a0b0c0d0e0f");       // missing first -
85                 }
86
87                 [Test]
88                 [ExpectedException (typeof (FormatException))]
89                 public void Constructor_String_Mismatch ()
90                 {
91                         new Guid ("(000102030405-0607-0809-0a0b0c0d0e0f}");     // open (, close }
92                 }
93
94                 [Test]
95                 public void Constructor_Int32_2xInt16_8xByte ()
96                 {
97                         Guid g1 = new Guid (0x00010203, (short) 0x0405, (short) 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
98                         Guid g2 = new Guid (unchecked ((int) 0xffffffff), unchecked ((short) 0xffff), unchecked((short) 0xffff), 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
99                         Guid g3 = new Guid (0x00010203u, (ushort) 0x0405u, (ushort) 0x0607u, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
100                         Guid g4 = new Guid (0xffffffffu, (ushort) 0xffffu, (ushort) 0xffffu, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
101                 
102                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
103                         Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
104                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A3");
105                         Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A4");
106                 }
107
108                 [Test]
109                 public void Constructor_UInt32_2xUInt16_8xByte ()
110                 {
111                         Guid g1 = new Guid (0x00010203u, (ushort) 0x0405u, (ushort) 0x0607u, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
112                         Guid g2 = new Guid (0xffffffffu, (ushort) 0xffffu, (ushort) 0xffffu, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
113                 
114                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
115                         Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
116                 }
117
118                 [Test]
119                 public void Constructor_Int32_2xInt16_ByteArray ()
120                 {
121                         Guid g1 = new Guid (0x00010203, (short) 0x0405, (short) 0x0607, new byte[] { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f });
122                         Guid g2 = new Guid (unchecked ((int) 0xffffffff), unchecked ((short) 0xffff), unchecked((short) 0xffff), new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
123                 
124                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
125                         Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
126                 }
127
128                 [Test]
129                 public void Empty ()
130                 {
131                         Assert.AreEqual ("00000000-0000-0000-0000-000000000000", Guid.Empty.ToString (), "ToString");
132                         Assert.AreEqual (new byte [16], Guid.Empty.ToByteArray (), "ToByteArray");
133                 }
134
135                 [Test]
136                 public void NewGuid ()
137                 {
138                         Guid g1 = Guid.NewGuid ();
139                         Guid g2 = Guid.NewGuid ();
140                         Assert.IsFalse (g1 == g2);
141                 }
142
143                 [Test]
144                 public void NewGuid_Stressed ()
145                 {
146                         for (int i = 0; i < 256; i++) {
147                                 Guid g = Guid.NewGuid ();
148                                 Assert.AreNotEqual (0, g.CompareTo (Guid.Empty), i.ToString ());
149                         }
150                 }
151
152 #pragma warning disable 1718
153                 [Test]
154                 public void EqualityOp ()
155                 {
156                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
157                         Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
158                         Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
159
160                         Assert.IsTrue (g1 == g1, "A1");
161                         Assert.IsTrue (g1 == g2, "A2");
162                         Assert.IsFalse (g1 == g3, "A3");
163                 }
164
165                 [Test]
166                 public void InequalityOp ()
167                 {
168                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
169                         Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
170                         Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
171
172                         Assert.IsFalse (g1 != g1, "A1");
173                         Assert.IsFalse (g1 != g2, "A2");
174                         Assert.IsTrue (g1 != g3, "A3");
175                 }
176 #pragma warning restore 1718
177
178                 [Test]
179                 public void EqualsObject ()
180                 {
181                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
182                         Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
183                         Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
184                         // cast everything to object so the test still works under 2.0
185                         Assert.IsTrue (g1.Equals ((object)g1), "A1");
186                         Assert.IsTrue (g1.Equals ((object)g2), "A2");
187                         Assert.IsFalse (g1.Equals ((object)g3), "A3");
188                         Assert.IsFalse (g1.Equals ((object)null), "A4");
189                         Assert.IsFalse (g1.Equals ((object)"This is not a Guid!"), "A5");
190                 }
191
192
193                 [Test]
194                 public void EqualsGuid ()
195                 {
196                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
197                         Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
198                         Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
199
200                         Assert.IsTrue (g1.Equals (g1), "A1");
201                         Assert.IsTrue (g1.Equals (g2), "A2");
202                         Assert.IsFalse (g1.Equals (g3), "A3");
203                         Assert.IsFalse (g1.Equals (null), "A4");
204                         Assert.IsFalse (g1.Equals ("This is not a Guid!"), "A5");
205                 }
206
207                 [Test]
208                 public void CompareToObject ()
209                 {
210                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
211                         Guid g2 = new Guid (0x00010204, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
212                         Guid g3 = new Guid (0x00010203, 0x0405, 0x0607, 0x09, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
213                         Guid g4 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x1f);
214                         // cast everything to object so the test still works under 2.0
215                         Assert.IsTrue (g1.CompareTo ((object)g2) < 0, "A1");
216                         Assert.IsTrue (g1.CompareTo ((object)g3) < 0, "A2");
217                         Assert.IsTrue (g1.CompareTo ((object)g4) < 0, "A3");
218                         Assert.IsTrue (g2.CompareTo ((object)g1) > 0, "A4");
219                         Assert.IsTrue (g3.CompareTo ((object)g1) > 0, "A5");
220                         Assert.IsTrue (g4.CompareTo ((object)g1) > 0, "A6");
221                         Assert.IsTrue (g1.CompareTo ((object)g1) == 0, "A7");
222                         Assert.IsTrue (g1.CompareTo ((object)null) > 0, "A8");
223                 }
224
225                 [Test]
226                 [ExpectedException (typeof (ArgumentException))]
227                 public void CompareToObject_Invalid ()
228                 {
229                         Guid.Empty.CompareTo ("Say what?");
230                 }
231
232                 [Test]
233                 public void CompareToGuid ()
234                 {
235                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
236                         Guid g2 = new Guid (0x00010204, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
237                         Guid g3 = new Guid (0x00010203, 0x0405, 0x0607, 0x09, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
238                         Guid g4 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x1f);
239
240                         Assert.IsTrue (g1.CompareTo (g2) < 0, "A1");
241                         Assert.IsTrue (g1.CompareTo (g3) < 0, "A2");
242                         Assert.IsTrue (g1.CompareTo (g4) < 0, "A3");
243                         Assert.IsTrue (g2.CompareTo (g1) > 0, "A4");
244                         Assert.IsTrue (g3.CompareTo (g1) > 0, "A5");
245                         Assert.IsTrue (g4.CompareTo (g1) > 0, "A6");
246                         Assert.IsTrue (g1.CompareTo (g1) == 0, "A7");
247                 }
248
249                 [Test]
250                 public void CompareToGuid_2 ()
251                 {
252                         var g1 = new Guid ("d1c5088bc188464fb77b0fd2be2d005e");
253                         var g2 = new Guid ("d2c5088bc188464fb77b0fd2be2d005e");
254                         var g3 = new Guid ("00c5088bc188464fb77b0fd2be2d005e");
255
256                         Assert.AreEqual (-1, g1.CompareTo (g2), "#1");
257                         Assert.AreEqual (1, g1.CompareTo (g3), "#2");
258                         Assert.AreEqual (1, g1.CompareTo (Guid.Empty), "#3");
259                 }
260
261                 [Test]
262                 public void GetHashCode_Same ()
263                 {
264                         Guid copy = new Guid (Guid.Empty.ToString ());
265                         Assert.AreEqual (Guid.Empty.GetHashCode (), copy.GetHashCode (), "GetHashCode");
266                 }
267
268                 [Test]
269                 public void GetHashCode_Different ()
270                 {
271                         Guid g = Guid.NewGuid ();
272                         Assert.IsFalse (Guid.Empty.GetHashCode () == g.GetHashCode (), "GetHashCode");
273                 }
274
275                 [Test]
276                 public void ToByteArray ()
277                 {
278                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
279                         byte[] expected = new byte[] { 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
280                         Assert.AreEqual (expected, g1.ToByteArray (), "ToByteArray");
281                 }
282
283                 [Test]
284                 public void ToString_AllFormats ()
285                 {
286                         Guid g = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
287                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (), "A1");
288                         Assert.AreEqual ("000102030405060708090a0b0c0d0e0f", g.ToString ("N"), "A2");
289                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ("D"), "A3");
290                         Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B"), "A4");
291                         Assert.AreEqual ("(00010203-0405-0607-0809-0a0b0c0d0e0f)", g.ToString ("P"), "A5");
292                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (""), "A6");
293                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ((string)null), "A7");
294                         Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B", null), "A10");
295 #if NET_4_0
296                         Assert.AreEqual ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", g.ToString ("x"), "A11");
297                         Assert.AreEqual ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", g.ToString ("X"), "A11");
298 #endif
299                 }
300
301 #if !NET_4_0
302                 [Test]
303                 [ExpectedException (typeof (FormatException))]
304                 public void ToString_UnsupportedFormat ()
305                 {
306                         new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("X");
307                 }
308 #endif
309
310                 [Test]
311                 [ExpectedException (typeof (FormatException))]
312                 public void ToString_InvalidFormat ()
313                 {
314                         new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("This is invalid");
315                 }
316
317                 [Test]
318                 [ExpectedException (typeof (FormatException))]
319                 public void ParseExtraJunkN ()
320                 {
321                         new Guid ("000102030405060708090a0b0c0d0e0faaaaaa");
322                 }
323
324                 [Test]
325                 [ExpectedException (typeof (FormatException))]
326                 public void ParseExtraJunkD ()
327                 {
328                         new Guid ("00010203-0405-0607-0809-0a0b0c0d0e0faaaaaa");
329                 }
330
331                 [Test]
332                 [ExpectedException (typeof (FormatException))]
333                 public void ParseExtraJunkB ()
334                 {
335                         new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}aaaa");
336                 }
337
338                 [Test]
339                 [ExpectedException (typeof (FormatException))]
340                 public void ParseExtraJunkP ()
341                 {
342                         new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)aaaa");
343                 }
344
345                 [Test]
346                 [ExpectedException (typeof (FormatException))]
347                 public void ParseExtraJunkX ()
348                 {
349                         new Guid ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}aaaa");
350                 }
351
352 #if NET_4_0
353
354                 /*
355                         N = new Guid ("000102030405060708090a0b0c0d0e0f"); 
356                         D = new Guid ("00010203-0405-0607-0809-0a0b0c0d0e0f"); 
357                         B = new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}"); 
358                         P = new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)");
359                         X = new Guid ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}");
360
361                         string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
362                 */
363
364                 [Test]
365                 public void ParseExact ()
366                 {
367                         const string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
368
369                         var guid = Guid.ParseExact ("000102030405060708090a0b0c0d0e0f", "N");
370                         Assert.AreEqual (expected, guid.ToString ());
371
372                         guid = Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "D");
373                         Assert.AreEqual (expected, guid.ToString ());
374
375                         guid = Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "d");
376                         Assert.AreEqual (expected, guid.ToString ());
377
378                         guid = Guid.ParseExact ("{00010203-0405-0607-0809-0A0B0C0D0E0F}", "B");
379                         Assert.AreEqual (expected, guid.ToString ());
380
381                         guid = Guid.ParseExact ("{00010203-0405-0607-0809-0A0B0C0D0E0F}", "b");
382                         Assert.AreEqual (expected, guid.ToString ());
383
384                         guid = Guid.ParseExact ("(00010203-0405-0607-0809-0A0B0C0D0E0F)", "P");
385                         Assert.AreEqual (expected, guid.ToString ());
386
387                         guid = Guid.ParseExact ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", "X");
388                         Assert.AreEqual (expected, guid.ToString ());
389                 }
390
391                 [Test]
392                 [ExpectedException (typeof (FormatException))]
393                 public void ParseExactN ()
394                 {
395                         Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "N");
396                 }
397
398                 [Test]
399                 [ExpectedException (typeof (FormatException))]
400                 public void ParseExactD ()
401                 {
402                         Guid.ParseExact ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", "D");
403                 }
404
405                 [Test]
406                 public void TryParse()
407                 {
408                         Guid guid;
409                         Assert.IsFalse (Guid.TryParse(null, out guid), "A1");
410                         Assert.AreEqual (Guid.Empty, guid, "A2");
411                         Assert.IsFalse (Guid.TryParse("", out guid), "A3");
412                         Assert.AreEqual (Guid.Empty, guid, "A4");
413                         Assert.IsFalse (Guid.TryParse("foobar", out guid), "A5");
414                         Assert.AreEqual (Guid.Empty, guid, "A6");
415                 }
416
417                 [Test]
418                 public void TryParseExact()
419                 {
420                         Guid guid;
421                         Assert.IsFalse (Guid.TryParseExact(null, null, out guid), "A1");
422                         Assert.AreEqual (Guid.Empty, guid, "A2");
423                         Assert.IsFalse (Guid.TryParseExact("", null, out guid), "A3");
424                         Assert.AreEqual (Guid.Empty, guid, "A4");
425                         Assert.IsFalse (Guid.TryParseExact("foobar", null, out guid), "A5");
426                         Assert.AreEqual (Guid.Empty, guid, "A6");
427                 }
428 #endif
429         }
430 }