New test.
[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
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                 }
64
65                 [Test]
66                 [ExpectedException (typeof (FormatException))]
67                 public void Constructor_String_Invalid ()
68                 {
69                         new Guid ("invalid");
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (FormatException))]
74                 public void Constructor_String_MissingAllSeparators ()
75                 {
76                         new Guid ("{000102030405060708090A0B0C0D0E0F}");        // missing all -
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (FormatException))]
81                 public void Constructor_String_MissingSeparator ()
82                 {
83                         new Guid ("000102030405-0607-0809-0a0b0c0d0e0f");       // missing first -
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (FormatException))]
88                 public void Constructor_String_Mismatch ()
89                 {
90                         new Guid ("(000102030405-0607-0809-0a0b0c0d0e0f}");     // open (, close }
91                 }
92
93                 [Test]
94                 public void Constructor_Int32_2xInt16_8xByte ()
95                 {
96                         Guid g1 = new Guid (0x00010203, (short) 0x0405, (short) 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
97                         Guid g2 = new Guid (unchecked ((int) 0xffffffff), unchecked ((short) 0xffff), unchecked((short) 0xffff), 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
98                         Guid g3 = new Guid (0x00010203u, (ushort) 0x0405u, (ushort) 0x0607u, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
99                         Guid g4 = new Guid (0xffffffffu, (ushort) 0xffffu, (ushort) 0xffffu, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
100                 
101                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
102                         Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
103                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A3");
104                         Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A4");
105                 }
106
107                 [Test]
108                 public void Constructor_UInt32_2xUInt16_8xByte ()
109                 {
110                         Guid g1 = new Guid (0x00010203u, (ushort) 0x0405u, (ushort) 0x0607u, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
111                         Guid g2 = new Guid (0xffffffffu, (ushort) 0xffffu, (ushort) 0xffffu, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
112                 
113                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
114                         Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
115                 }
116
117                 [Test]
118                 public void Constructor_Int32_2xInt16_ByteArray ()
119                 {
120                         Guid g1 = new Guid (0x00010203, (short) 0x0405, (short) 0x0607, new byte[] { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f });
121                         Guid g2 = new Guid (unchecked ((int) 0xffffffff), unchecked ((short) 0xffff), unchecked((short) 0xffff), new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
122                 
123                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g1.ToString (), "A1");
124                         Assert.AreEqual ("ffffffff-ffff-ffff-ffff-ffffffffffff", g2.ToString (), "A2");
125                 }
126
127                 [Test]
128                 public void Empty ()
129                 {
130                         Assert.AreEqual ("00000000-0000-0000-0000-000000000000", Guid.Empty.ToString (), "ToString");
131                         Assert.AreEqual (new byte [16], Guid.Empty.ToByteArray (), "ToByteArray");
132                 }
133
134                 [Test]
135                 public void NewGuid ()
136                 {
137                         Guid g1 = Guid.NewGuid ();
138                         Guid g2 = Guid.NewGuid ();
139                         Assert.IsFalse (g1 == g2);
140                 }
141
142                 [Test]
143                 public void EqualityOp ()
144                 {
145                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
146                         Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
147                         Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
148
149                         Assert.IsTrue (g1 == g1, "A1");
150                         Assert.IsTrue (g1 == g2, "A2");
151                         Assert.IsFalse (g1 == g3, "A3");
152                 }
153
154                 [Test]
155                 public void InequalityOp ()
156                 {
157                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
158                         Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
159                         Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
160
161                         Assert.IsFalse (g1 != g1, "A1");
162                         Assert.IsFalse (g1 != g2, "A2");
163                         Assert.IsTrue (g1 != g3, "A3");
164                 }
165
166                 [Test]
167                 public void EqualsObject ()
168                 {
169                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
170                         Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
171                         Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
172                         // cast everything to object so the test still works under 2.0
173                         Assert.IsTrue (g1.Equals ((object)g1), "A1");
174                         Assert.IsTrue (g1.Equals ((object)g2), "A2");
175                         Assert.IsFalse (g1.Equals ((object)g3), "A3");
176                         Assert.IsFalse (g1.Equals ((object)null), "A4");
177                         Assert.IsFalse (g1.Equals ((object)"This is not a Guid!"), "A5");
178                 }
179
180 #if NET_2_0
181                 [Test]
182                 public void EqualsGuid ()
183                 {
184                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
185                         Guid g2 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
186                         Guid g3 = new Guid (0x11223344, 0x5566, 0x6677, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
187
188                         Assert.IsTrue (g1.Equals (g1), "A1");
189                         Assert.IsTrue (g1.Equals (g2), "A2");
190                         Assert.IsFalse (g1.Equals (g3), "A3");
191                         Assert.IsFalse (g1.Equals (null), "A4");
192                         Assert.IsFalse (g1.Equals ("This is not a Guid!"), "A5");
193                 }
194 #endif
195
196                 [Test]
197                 public void CompareToObject ()
198                 {
199                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
200                         Guid g2 = new Guid (0x00010204, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
201                         Guid g3 = new Guid (0x00010203, 0x0405, 0x0607, 0x09, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
202                         Guid g4 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x1f);
203                         // cast everything to object so the test still works under 2.0
204                         Assert.IsTrue (g1.CompareTo ((object)g2) < 0, "A1");
205                         Assert.IsTrue (g1.CompareTo ((object)g3) < 0, "A2");
206                         Assert.IsTrue (g1.CompareTo ((object)g4) < 0, "A3");
207                         Assert.IsTrue (g2.CompareTo ((object)g1) > 0, "A4");
208                         Assert.IsTrue (g3.CompareTo ((object)g1) > 0, "A5");
209                         Assert.IsTrue (g4.CompareTo ((object)g1) > 0, "A6");
210                         Assert.IsTrue (g1.CompareTo ((object)g1) == 0, "A7");
211                         Assert.IsTrue (g1.CompareTo ((object)null) > 0, "A8");
212                 }
213
214                 [Test]
215                 [ExpectedException (typeof (ArgumentException))]
216                 public void CompareToObject_Invalid ()
217                 {
218                         Guid.Empty.CompareTo ("Say what?");
219                 }
220
221 #if NET_2_0
222                 [Test]
223                 public void CompareToGuid ()
224                 {
225                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
226                         Guid g2 = new Guid (0x00010204, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
227                         Guid g3 = new Guid (0x00010203, 0x0405, 0x0607, 0x09, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
228                         Guid g4 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x1f);
229
230                         Assert.IsTrue (g1.CompareTo (g2) < 0, "A1");
231                         Assert.IsTrue (g1.CompareTo (g3) < 0, "A2");
232                         Assert.IsTrue (g1.CompareTo (g4) < 0, "A3");
233                         Assert.IsTrue (g2.CompareTo (g1) > 0, "A4");
234                         Assert.IsTrue (g3.CompareTo (g1) > 0, "A5");
235                         Assert.IsTrue (g4.CompareTo (g1) > 0, "A6");
236                         Assert.IsTrue (g1.CompareTo (g1) == 0, "A7");
237                 }
238 #endif
239
240                 [Test]
241                 public void GetHashCode_Same ()
242                 {
243                         Guid copy = new Guid (Guid.Empty.ToString ());
244                         Assert.AreEqual (Guid.Empty.GetHashCode (), copy.GetHashCode (), "GetHashCode");
245                 }
246
247                 [Test]
248                 public void GetHashCode_Different ()
249                 {
250                         Guid g = Guid.NewGuid ();
251                         Assert.IsFalse (Guid.Empty.GetHashCode () == g.GetHashCode (), "GetHashCode");
252                 }
253
254                 [Test]
255                 public void ToByteArray ()
256                 {
257                         Guid g1 = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
258                         byte[] expected = new byte[] { 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
259                         Assert.AreEqual (expected, g1.ToByteArray (), "ToByteArray");
260                 }
261
262                 [Test]
263                 public void ToString_AllFormats ()
264                 {
265                         Guid g = new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
266                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (), "A1");
267                         Assert.AreEqual ("000102030405060708090a0b0c0d0e0f", g.ToString ("N"), "A2");
268                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ("D"), "A3");
269                         Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B"), "A4");
270                         Assert.AreEqual ("(00010203-0405-0607-0809-0a0b0c0d0e0f)", g.ToString ("P"), "A5");
271                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (""), "A6");
272                         Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ((string)null), "A7");
273                         Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B", null), "A10");
274                 }
275
276                 [Test]
277                 [ExpectedException (typeof (FormatException))]
278                 public void ToString_UnsupportedFormat ()
279                 {
280                         new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("X");
281                 }
282
283                 [Test]
284                 [ExpectedException (typeof (FormatException))]
285                 public void ToString_InvalidFormat ()
286                 {
287                         new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("This is invalid");
288                 }
289         }
290 }