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 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");
67 [ExpectedException (typeof (FormatException))]
68 public void Constructor_String_Invalid ()
74 [ExpectedException (typeof (FormatException))]
75 public void Constructor_String_MissingAllSeparators ()
77 new Guid ("{000102030405060708090A0B0C0D0E0F}"); // missing all -
81 [ExpectedException (typeof (FormatException))]
82 public void Constructor_String_MissingSeparator ()
84 new Guid ("000102030405-0607-0809-0a0b0c0d0e0f"); // missing first -
88 [ExpectedException (typeof (FormatException))]
89 public void Constructor_String_Mismatch ()
91 new Guid ("(000102030405-0607-0809-0a0b0c0d0e0f}"); // open (, close }
95 public void Constructor_Int32_2xInt16_8xByte ()
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);
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");
109 public void Constructor_UInt32_2xUInt16_8xByte ()
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);
114 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
115 Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
119 public void Constructor_Int32_2xInt16_ByteArray ()
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 });
124 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
125 Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
131 Assert.AreEqual ("00000000-0000-0000-0000-000000000000", Guid.Empty.ToString (), "ToString");
132 Assert.AreEqual (new byte [16], Guid.Empty.ToByteArray (), "ToByteArray");
136 public void NewGuid ()
138 Guid g1 = Guid.NewGuid ();
139 Guid g2 = Guid.NewGuid ();
140 Assert.IsFalse (g1 == g2);
143 #pragma warning disable 1718
145 public void EqualityOp ()
147 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
148 Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
149 Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
151 Assert.IsTrue (g1 == g1, "A1");
152 Assert.IsTrue (g1 == g2, "A2");
153 Assert.IsFalse (g1 == g3, "A3");
157 public void InequalityOp ()
159 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
160 Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
161 Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
163 Assert.IsFalse (g1 != g1, "A1");
164 Assert.IsFalse (g1 != g2, "A2");
165 Assert.IsTrue (g1 != g3, "A3");
167 #pragma warning restore 1718
170 public void EqualsObject ()
172 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
173 Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
174 Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
175 // cast everything to object so the test still works under 2.0
176 Assert.IsTrue (g1.Equals ((object)g1), "A1");
177 Assert.IsTrue (g1.Equals ((object)g2), "A2");
178 Assert.IsFalse (g1.Equals ((object)g3), "A3");
179 Assert.IsFalse (g1.Equals ((object)null), "A4");
180 Assert.IsFalse (g1.Equals ((object)"This is not a Guid!"), "A5");
185 public void EqualsGuid ()
187 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
188 Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
189 Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
191 Assert.IsTrue (g1.Equals (g1), "A1");
192 Assert.IsTrue (g1.Equals (g2), "A2");
193 Assert.IsFalse (g1.Equals (g3), "A3");
194 Assert.IsFalse (g1.Equals (null), "A4");
195 Assert.IsFalse (g1.Equals ("This is not a Guid!"), "A5");
199 public void CompareToObject ()
201 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
202 Guid g2 = new Guid (0x00010204, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
203 Guid g3 = new Guid (0x00010203, 0x0405, 0x0607, 0x09, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
204 Guid g4 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x1f);
205 // cast everything to object so the test still works under 2.0
206 Assert.IsTrue (g1.CompareTo ((object)g2) < 0, "A1");
207 Assert.IsTrue (g1.CompareTo ((object)g3) < 0, "A2");
208 Assert.IsTrue (g1.CompareTo ((object)g4) < 0, "A3");
209 Assert.IsTrue (g2.CompareTo ((object)g1) > 0, "A4");
210 Assert.IsTrue (g3.CompareTo ((object)g1) > 0, "A5");
211 Assert.IsTrue (g4.CompareTo ((object)g1) > 0, "A6");
212 Assert.IsTrue (g1.CompareTo ((object)g1) == 0, "A7");
213 Assert.IsTrue (g1.CompareTo ((object)null) > 0, "A8");
217 [ExpectedException (typeof (ArgumentException))]
218 public void CompareToObject_Invalid ()
220 Guid.Empty.CompareTo ("Say what?");
224 public void CompareToGuid ()
226 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
227 Guid g2 = new Guid (0x00010204, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
228 Guid g3 = new Guid (0x00010203, 0x0405, 0x0607, 0x09, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
229 Guid g4 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x1f);
231 Assert.IsTrue (g1.CompareTo (g2) < 0, "A1");
232 Assert.IsTrue (g1.CompareTo (g3) < 0, "A2");
233 Assert.IsTrue (g1.CompareTo (g4) < 0, "A3");
234 Assert.IsTrue (g2.CompareTo (g1) > 0, "A4");
235 Assert.IsTrue (g3.CompareTo (g1) > 0, "A5");
236 Assert.IsTrue (g4.CompareTo (g1) > 0, "A6");
237 Assert.IsTrue (g1.CompareTo (g1) == 0, "A7");
241 public void CompareToGuid_2 ()
243 var g1 = new Guid ("d1c5088bc188464fb77b0fd2be2d005e");
244 var g2 = new Guid ("d2c5088bc188464fb77b0fd2be2d005e");
245 var g3 = new Guid ("00c5088bc188464fb77b0fd2be2d005e");
247 Assert.AreEqual (-1, g1.CompareTo (g2), "#1");
248 Assert.AreEqual (1, g1.CompareTo (g3), "#2");
249 Assert.AreEqual (1, g1.CompareTo (Guid.Empty), "#3");
253 public void GetHashCode_Same ()
255 Guid copy = new Guid (Guid.Empty.ToString ());
256 Assert.AreEqual (Guid.Empty.GetHashCode (), copy.GetHashCode (), "GetHashCode");
260 public void GetHashCode_Different ()
262 Guid g = Guid.NewGuid ();
263 Assert.IsFalse (Guid.Empty.GetHashCode () == g.GetHashCode (), "GetHashCode");
267 public void ToByteArray ()
269 Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
270 byte[] expected = new byte[] { 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
271 Assert.AreEqual (expected, g1.ToByteArray (), "ToByteArray");
275 public void ToString_AllFormats ()
277 Guid g = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
278 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (), "A1");
279 Assert.AreEqual ("000102030405060708090a0b0c0d0e0f", g.ToString ("N"), "A2");
280 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ("D"), "A3");
281 Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B"), "A4");
282 Assert.AreEqual ("(00010203-0405-0607-0809-0a0b0c0d0e0f)", g.ToString ("P"), "A5");
283 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (""), "A6");
284 Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ((string)null), "A7");
285 Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B", null), "A10");
287 Assert.AreEqual ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", g.ToString ("x"), "A11");
288 Assert.AreEqual ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", g.ToString ("X"), "A11");
294 [ExpectedException (typeof (FormatException))]
295 public void ToString_UnsupportedFormat ()
297 new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("X");
302 [ExpectedException (typeof (FormatException))]
303 public void ToString_InvalidFormat ()
305 new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("This is invalid");
309 [ExpectedException (typeof (FormatException))]
310 public void ParseExtraJunkN ()
312 new Guid ("000102030405060708090a0b0c0d0e0faaaaaa");
316 [ExpectedException (typeof (FormatException))]
317 public void ParseExtraJunkD ()
319 new Guid ("00010203-0405-0607-0809-0a0b0c0d0e0faaaaaa");
323 [ExpectedException (typeof (FormatException))]
324 public void ParseExtraJunkB ()
326 new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}aaaa");
330 [ExpectedException (typeof (FormatException))]
331 public void ParseExtraJunkP ()
333 new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)aaaa");
337 [ExpectedException (typeof (FormatException))]
338 public void ParseExtraJunkX ()
340 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 ());
383 [ExpectedException (typeof (FormatException))]
384 public void ParseExactN ()
386 Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "N");
390 [ExpectedException (typeof (FormatException))]
391 public void ParseExactD ()
393 Guid.ParseExact ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", "D");
397 public void TryParse()
400 Assert.IsFalse (Guid.TryParse(null, out guid), "A1");
401 Assert.AreEqual (Guid.Empty, guid, "A2");
402 Assert.IsFalse (Guid.TryParse("", out guid), "A3");
403 Assert.AreEqual (Guid.Empty, guid, "A4");
404 Assert.IsFalse (Guid.TryParse("foobar", out guid), "A5");
405 Assert.AreEqual (Guid.Empty, guid, "A6");
409 public void TryParseExact()
412 Assert.IsFalse (Guid.TryParseExact(null, null, out guid), "A1");
413 Assert.AreEqual (Guid.Empty, guid, "A2");
414 Assert.IsFalse (Guid.TryParseExact("", null, out guid), "A3");
415 Assert.AreEqual (Guid.Empty, guid, "A4");
416 Assert.IsFalse (Guid.TryParseExact("foobar", null, out guid), "A5");
417 Assert.AreEqual (Guid.Empty, guid, "A6");