2 // GuidTest.cs - NUnit Test Cases for the System.Guid struct
5 // Duco Fijma (duco@lorentz.xs4all.nl)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 using NUnit.Framework;
15 namespace MonoTests.System {
18 public class GuidTest {
21 [ExpectedException (typeof (ArgumentNullException))]
22 public void Constructor_ByteArray_Null ()
24 new Guid ((byte[]) null);
28 [ExpectedException (typeof (ArgumentException))]
29 public void Constructor_ByteArray_InvalidLength ()
31 new Guid (new byte[] {0x00, 0x01, 0x02});
35 public void Constructor_ByteArray ()
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 ());
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Constructor_String_Null ()
45 new Guid ((string) null);
49 public void Constructor_String ()
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 Guid g6 = new Guid ("\t {\t 0x00000001 , 0x0002 , \t 0x0003 \t , { 0x04 , 0x05 , \t 0x06\t,\t0x07 , 0x08 , 0x09 , 0x0a , 0x0b } \t }\t ");
58 string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
59 Assert.AreEqual (expected, g0.ToString (), "A0");
60 Assert.AreEqual (expected, g1.ToString (), "A1");
61 Assert.AreEqual (expected, g2.ToString (), "A2");
62 Assert.AreEqual (expected, g3.ToString (), "A3");
63 Assert.AreEqual (expected, g4.ToString (), "A4");
64 Assert.AreEqual (expected, g5.ToString (), "A5");
65 Assert.AreEqual ("00000001-0002-0003-0405-060708090a0b", g6.ToString (), "A6");
69 [ExpectedException (typeof (FormatException))]
70 public void Constructor_String_Invalid ()
76 [ExpectedException (typeof (FormatException))]
77 public void Constructor_String_MissingAllSeparators ()
79 new Guid ("{000102030405060708090A0B0C0D0E0F}"); // missing all -
83 [ExpectedException (typeof (FormatException))]
84 public void Constructor_String_MissingSeparator ()
86 new Guid ("000102030405-0607-0809-0a0b0c0d0e0f"); // missing first -
90 [ExpectedException (typeof (FormatException))]
91 public void Constructor_String_Mismatch ()
93 new Guid ("(000102030405-0607-0809-0a0b0c0d0e0f}"); // open (, close }
97 public void Constructor_Int32_2xInt16_8xByte ()
99 Guid g1 = new Guid (0x00010203, (short) 0x0405, (short) 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
100 Guid g2 = new Guid (unchecked ((int) 0xffffffff), unchecked ((short) 0xffff), unchecked((short) 0xffff), 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
101 Guid g3 = new Guid (0x00010203u, (ushort) 0x0405u, (ushort) 0x0607u, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
102 Guid g4 = new Guid (0xffffffffu, (ushort) 0xffffu, (ushort) 0xffffu, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
104 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
105 Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
106 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A3");
107 Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A4");
111 public void Constructor_UInt32_2xUInt16_8xByte ()
113 Guid g1 = new Guid (0x00010203u, (ushort) 0x0405u, (ushort) 0x0607u, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
114 Guid g2 = new Guid (0xffffffffu, (ushort) 0xffffu, (ushort) 0xffffu, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
116 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
117 Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
121 public void Constructor_Int32_2xInt16_ByteArray ()
123 Guid g1 = new Guid (0x00010203, (short) 0x0405, (short) 0x0607, new byte[] { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f });
124 Guid g2 = new Guid (unchecked ((int) 0xffffffff), unchecked ((short) 0xffff), unchecked((short) 0xffff), new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
126 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
127 Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
133 Assert.AreEqual ("00000000-0000-0000-0000-000000000000", Guid.Empty.ToString (), "ToString");
134 Assert.AreEqual (new byte [16], Guid.Empty.ToByteArray (), "ToByteArray");
138 public void NewGuid ()
140 Guid g1 = Guid.NewGuid ();
141 Guid g2 = Guid.NewGuid ();
142 Assert.IsFalse (g1 == g2);
146 public void NewGuid_Stressed ()
148 for (int i = 0; i < 256; i++) {
149 Guid g = Guid.NewGuid ();
150 Assert.AreNotEqual (0, g.CompareTo (Guid.Empty), i.ToString ());
154 #pragma warning disable 1718
156 public void EqualityOp ()
158 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
159 Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
160 Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
162 Assert.IsTrue (g1 == g1, "A1");
163 Assert.IsTrue (g1 == g2, "A2");
164 Assert.IsFalse (g1 == g3, "A3");
168 public void InequalityOp ()
170 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
171 Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
172 Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
174 Assert.IsFalse (g1 != g1, "A1");
175 Assert.IsFalse (g1 != g2, "A2");
176 Assert.IsTrue (g1 != g3, "A3");
178 #pragma warning restore 1718
181 public void EqualsObject ()
183 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
184 Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
185 Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
186 // cast everything to object so the test still works under 2.0
187 Assert.IsTrue (g1.Equals ((object)g1), "A1");
188 Assert.IsTrue (g1.Equals ((object)g2), "A2");
189 Assert.IsFalse (g1.Equals ((object)g3), "A3");
190 Assert.IsFalse (g1.Equals ((object)null), "A4");
191 Assert.IsFalse (g1.Equals ((object)"This is not a Guid!"), "A5");
196 public void EqualsGuid ()
198 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
199 Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
200 Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
202 Assert.IsTrue (g1.Equals (g1), "A1");
203 Assert.IsTrue (g1.Equals (g2), "A2");
204 Assert.IsFalse (g1.Equals (g3), "A3");
205 Assert.IsFalse (g1.Equals (null), "A4");
206 Assert.IsFalse (g1.Equals ("This is not a Guid!"), "A5");
210 public void CompareToObject ()
212 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
213 Guid g2 = new Guid (0x00010204, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
214 Guid g3 = new Guid (0x00010203, 0x0405, 0x0607, 0x09, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
215 Guid g4 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x1f);
216 // cast everything to object so the test still works under 2.0
217 Assert.IsTrue (g1.CompareTo ((object)g2) < 0, "A1");
218 Assert.IsTrue (g1.CompareTo ((object)g3) < 0, "A2");
219 Assert.IsTrue (g1.CompareTo ((object)g4) < 0, "A3");
220 Assert.IsTrue (g2.CompareTo ((object)g1) > 0, "A4");
221 Assert.IsTrue (g3.CompareTo ((object)g1) > 0, "A5");
222 Assert.IsTrue (g4.CompareTo ((object)g1) > 0, "A6");
223 Assert.IsTrue (g1.CompareTo ((object)g1) == 0, "A7");
224 Assert.IsTrue (g1.CompareTo ((object)null) > 0, "A8");
228 [ExpectedException (typeof (ArgumentException))]
229 public void CompareToObject_Invalid ()
231 Guid.Empty.CompareTo ("Say what?");
235 public void CompareToGuid ()
237 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
238 Guid g2 = new Guid (0x00010204, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
239 Guid g3 = new Guid (0x00010203, 0x0405, 0x0607, 0x09, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
240 Guid g4 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x1f);
242 Assert.IsTrue (g1.CompareTo (g2) < 0, "A1");
243 Assert.IsTrue (g1.CompareTo (g3) < 0, "A2");
244 Assert.IsTrue (g1.CompareTo (g4) < 0, "A3");
245 Assert.IsTrue (g2.CompareTo (g1) > 0, "A4");
246 Assert.IsTrue (g3.CompareTo (g1) > 0, "A5");
247 Assert.IsTrue (g4.CompareTo (g1) > 0, "A6");
248 Assert.IsTrue (g1.CompareTo (g1) == 0, "A7");
252 public void CompareToGuid_2 ()
254 var g1 = new Guid ("d1c5088bc188464fb77b0fd2be2d005e");
255 var g2 = new Guid ("d2c5088bc188464fb77b0fd2be2d005e");
256 var g3 = new Guid ("00c5088bc188464fb77b0fd2be2d005e");
258 Assert.AreEqual (-1, g1.CompareTo (g2), "#1");
259 Assert.AreEqual (1, g1.CompareTo (g3), "#2");
260 Assert.AreEqual (1, g1.CompareTo (Guid.Empty), "#3");
264 public void GetHashCode_Same ()
266 Guid copy = new Guid (Guid.Empty.ToString ());
267 Assert.AreEqual (Guid.Empty.GetHashCode (), copy.GetHashCode (), "GetHashCode");
271 public void GetHashCode_Different ()
273 Guid g = Guid.NewGuid ();
274 Assert.IsFalse (Guid.Empty.GetHashCode () == g.GetHashCode (), "GetHashCode");
278 public void ToByteArray ()
280 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
281 byte[] expected = new byte[] { 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
282 Assert.AreEqual (expected, g1.ToByteArray (), "ToByteArray");
286 public void ToString_AllFormats ()
288 Guid g = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
289 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (), "A1");
290 Assert.AreEqual ("000102030405060708090a0b0c0d0e0f", g.ToString ("N"), "A2");
291 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ("D"), "A3");
292 Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B"), "A4");
293 Assert.AreEqual ("(00010203-0405-0607-0809-0a0b0c0d0e0f)", g.ToString ("P"), "A5");
294 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (""), "A6");
295 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ((string)null), "A7");
296 Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B", null), "A10");
297 Assert.AreEqual ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", g.ToString ("x"), "A11");
298 Assert.AreEqual ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", g.ToString ("X"), "A11");
303 [ExpectedException (typeof (FormatException))]
304 public void ToString_InvalidFormat ()
306 new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("This is invalid");
310 [ExpectedException (typeof (FormatException))]
311 public void ParseExtraJunkN ()
313 new Guid ("000102030405060708090a0b0c0d0e0faaaaaa");
317 [ExpectedException (typeof (FormatException))]
318 public void ParseExtraJunkD ()
320 new Guid ("00010203-0405-0607-0809-0a0b0c0d0e0faaaaaa");
324 [ExpectedException (typeof (FormatException))]
325 public void ParseExtraJunkB ()
327 new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}aaaa");
331 [ExpectedException (typeof (FormatException))]
332 public void ParseExtraJunkP ()
334 new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)aaaa");
338 [ExpectedException (typeof (FormatException))]
339 public void ParseExtraJunkX ()
341 new Guid ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}aaaa");
346 N = new Guid ("000102030405060708090a0b0c0d0e0f");
347 D = new Guid ("00010203-0405-0607-0809-0a0b0c0d0e0f");
348 B = new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}");
349 P = new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)");
350 X = new Guid ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}");
352 string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
356 public void ParseExact ()
358 const string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
360 var guid = Guid.ParseExact ("000102030405060708090a0b0c0d0e0f", "N");
361 Assert.AreEqual (expected, guid.ToString ());
363 guid = Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "D");
364 Assert.AreEqual (expected, guid.ToString ());
366 guid = Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "d");
367 Assert.AreEqual (expected, guid.ToString ());
369 guid = Guid.ParseExact ("{00010203-0405-0607-0809-0A0B0C0D0E0F}", "B");
370 Assert.AreEqual (expected, guid.ToString ());
372 guid = Guid.ParseExact ("{00010203-0405-0607-0809-0A0B0C0D0E0F}", "b");
373 Assert.AreEqual (expected, guid.ToString ());
375 guid = Guid.ParseExact ("(00010203-0405-0607-0809-0A0B0C0D0E0F)", "P");
376 Assert.AreEqual (expected, guid.ToString ());
378 guid = Guid.ParseExact ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", "X");
379 Assert.AreEqual (expected, guid.ToString ());
381 guid = Guid.ParseExact ("\t {\t 0x00000001 , 0x0002 , \t 0x0003 \t , { 0x04 , 0x05 , \t 0x06\t,\t0x07 , 0x08 , 0x09 , 0x0a , 0x0b } \t }\t ", "X");
382 Assert.AreEqual ("00000001-0002-0003-0405-060708090a0b", guid.ToString (), "#8");
388 var guid = Guid.Parse ("\t {\t 0x00000001 , 0x0002 , \t 0x0003 \t , { 0x04 , 0x05 , \t 0x06\t,\t0x07 , 0x08 , 0x09 , 0x0a , 0x0b } \t }\t ");
389 Assert.AreEqual ("00000001-0002-0003-0405-060708090a0b", guid.ToString (), "#1");
393 [ExpectedException (typeof (FormatException))]
394 public void ParseError_1 ()
396 Guid.Parse("08888888-0444-444-0444-012121212121");
400 [ExpectedException (typeof (FormatException))]
401 public void ParseExactN ()
403 Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "N");
407 [ExpectedException (typeof (FormatException))]
408 public void ParseExactD ()
410 Guid.ParseExact ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", "D");
414 public void TryParse()
417 Assert.IsFalse (Guid.TryParse(null, out guid), "A1");
418 Assert.AreEqual (Guid.Empty, guid, "A2");
419 Assert.IsFalse (Guid.TryParse("", out guid), "A3");
420 Assert.AreEqual (Guid.Empty, guid, "A4");
421 Assert.IsFalse (Guid.TryParse("foobar", out guid), "A5");
422 Assert.AreEqual (Guid.Empty, guid, "A6");
423 Assert.IsFalse (Guid.TryParse ("08888888-0444-444-0444-012121212121", out guid), "A7");
427 public void TryParseExact()
430 Assert.IsFalse (Guid.TryParseExact(null, null, out guid), "A1");
431 Assert.AreEqual (Guid.Empty, guid, "A2");
432 Assert.IsFalse (Guid.TryParseExact("", null, out guid), "A3");
433 Assert.AreEqual (Guid.Empty, guid, "A4");
434 Assert.IsFalse (Guid.TryParseExact("foobar", null, out guid), "A5");
435 Assert.AreEqual (Guid.Empty, guid, "A6");