Skip to content

Commit 296050d

Browse files
authored
Fix Guid.TryParseGuidWithDashes() and Guid constructor with string parameter (#240)
1 parent eaff60b commit 296050d

File tree

2 files changed

+226
-84
lines changed

2 files changed

+226
-84
lines changed

Tests/NFUnitTestSystemLib/UnitTestGuid.cs

+155-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
@@ -60,7 +60,7 @@ public void ByteArrayConstructor_Test2()
6060
/// </summary>
6161
///
6262

63-
Byte[] guid16 = GetRandomBytes(16);
63+
byte[] guid16 = GetRandomBytes(16);
6464
Guid myGuid1 = new Guid(guid16);
6565
}
6666

@@ -82,7 +82,7 @@ public void ArgumentException_Test3()
8282
{
8383
size = random.Next(100);
8484
}
85-
Byte[] guidNot16 = GetRandomBytes(size);
85+
byte[] guidNot16 = GetRandomBytes(size);
8686
Assert.ThrowsException(typeof(ArgumentException), () => { Guid myGuid1 = new Guid(guidNot16); });
8787
}
8888

@@ -95,7 +95,7 @@ public void ArgumentNullException_Test4()
9595
/// </summary>
9696
///
9797

98-
Byte[] nullByte = null;
98+
byte[] nullByte = null;
9999
Assert.ThrowsException(typeof(ArgumentNullException), () => { Guid myGuid1 = new Guid(nullByte); });
100100
}
101101

@@ -157,11 +157,11 @@ public void Int_Short_Constructor_Test6()
157157
///
158158

159159
Random random = new Random();
160-
int _int = random.Next(Int32.MaxValue);
160+
int _int = random.Next(int.MaxValue);
161161
short _short1 = (short)random.Next(32768);
162162
short _short2 = (short)random.Next(32768);
163-
Byte[] _bArr = GetRandomBytes(8);
164-
Guid _guid = new Guid(_int, _short1, _short2, _bArr[0], _bArr[1], _bArr[2], _bArr[3], _bArr[4], _bArr[5], _bArr[6], _bArr[7]);
163+
byte[] _bArr = GetRandomBytes(8);
164+
_ = new Guid(_int, _short1, _short2, _bArr[0], _bArr[1], _bArr[2], _bArr[3], _bArr[4], _bArr[5], _bArr[6], _bArr[7]);
165165
}
166166

167167
[TestMethod]
@@ -174,11 +174,11 @@ public void UInt_Ushort_Constructor_Test7()
174174
///
175175

176176
Random random = new Random();
177-
int randoInt = random.Next(Int32.MaxValue);
177+
int randoInt = random.Next(int.MaxValue);
178178
uint _uInt = (uint)(randoInt * 2);
179179
ushort _uShort1 = (ushort)random.Next(65536);
180180
ushort _uShort2 = (ushort)random.Next(65536);
181-
Byte[] _bArr = GetRandomBytes(8);
181+
byte[] _bArr = GetRandomBytes(8);
182182
Guid _guid = new Guid(_uInt, _uShort1, _uShort1, _bArr[0], _bArr[1], _bArr[2], _bArr[3], _bArr[4], _bArr[5], _bArr[6], _bArr[7]);
183183
}
184184

@@ -192,7 +192,7 @@ public void Guid_Empty_Test8()
192192
/// </summary>
193193
///
194194
Guid guid = Guid.Empty;
195-
Byte[] _bArr = guid.ToByteArray();
195+
byte[] _bArr = guid.ToByteArray();
196196
for (int i = 0; i < 16; i++)
197197
{
198198
Assert.AreEqual(_bArr[i], (byte)0);
@@ -211,26 +211,21 @@ public void Guid_CompareTo_Test9()
211211
Guid guid1 = Guid.Empty;
212212
// Verifing any instance of Guid, regardless of its value, is greater than null
213213
Assert.AreEqual(guid1.CompareTo(null), 1);
214-
Byte[] _bArr = new Byte[16];
214+
byte[] _bArr = new byte[16];
215+
215216
// Creating a Guid with all bytes zero
216217
Guid guid2 = new Guid(_bArr);
217218
Assert.AreEqual(guid1.CompareTo(guid2), 0);
219+
218220
Guid guid3 = new Guid(0x4dff36b5, 0x9dde, 0x4f76, 0x9a, 0x2a, 0x96, 0x43, 0x50, 0x47, 0x06, 0x3d);
219-
if (guid3.CompareTo(guid1) <= 0)
220-
{
221-
throw new Exception("Expected : " + guid3.ToString() + " is greater than " + guid1.ToString());
222-
}
221+
Assert.IsTrue(guid3.CompareTo(guid1) > 0, "Expected : " + guid3.ToString() + " is greater than " + guid1.ToString());
222+
223223
Guid guid4 = new Guid(0x4dff36b5, 0x9dde, 0x4f76, 0x9a, 0x2a, 0x96, 0x43, 0x50, 0x47, 0x06, 0x3d);
224224
Assert.AreEqual(guid4.CompareTo(guid3), 0);
225+
225226
Guid guid5 = new Guid(0x4dff36b5, 0x9dde, 0x4f76, 0x9a, 0x2a, 0x96, 0x43, 0x50, 0x47, 0x06, 0x3e);
226-
if (guid5.CompareTo(guid4) <= 0)
227-
{
228-
throw new Exception("Expected : " + guid5.ToString() + " is greater than " + guid4.ToString());
229-
}
230-
if (guid4.CompareTo(guid5) >= 0)
231-
{
232-
throw new Exception("Expected : " + guid4.ToString() + " is less than " + guid5.ToString());
233-
}
227+
Assert.IsTrue(guid5.CompareTo(guid4) > 0, "Expected : " + guid5.ToString() + " is greater than " + guid4.ToString());
228+
Assert.IsTrue(guid4.CompareTo(guid5) < 0, "Expected : " + guid4.ToString() + " is less than " + guid5.ToString());
234229
}
235230

236231
[TestMethod]
@@ -242,25 +237,34 @@ public void Guid_ToString_Test10()
242237
/// </summary>
243238
///
244239

245-
String[] strArr1 = new String[] { "00000000-0000-0000-0000-000000000000",
240+
string[] strArr1 = new string[]
241+
{
242+
"00000000-0000-0000-0000-000000000000",
246243
"00000000-0000-0000-0000-000000000000",
247244
"4dff36b5-9dde-4f76-9a2a-96435047063d",
248-
"ffffffff-ffff-ffff-ffff-ffffffffffff"};
245+
"ffffffff-ffff-ffff-ffff-ffffffffffff"
246+
};
247+
249248
Guid guid1 = Guid.Empty;
250-
Byte[] _byteArr1 = new Byte[16];
249+
byte[] _byteArr1 = new byte[16];
251250
Guid guid2 = new Guid(_byteArr1);
252251
Guid guid3 = new Guid(0x4dff36b5, 0x9dde, 0x4f76, 0x9a, 0x2a, 0x96, 0x43, 0x50, 0x47, 0x06, 0x3d);
253-
Byte[] _byteArr2 = new Byte[16];
252+
253+
byte[] _byteArr2 = new byte[16];
254+
254255
for (int i = 0; i < _byteArr2.Length; i++)
255256
{
256-
_byteArr2[i] = Byte.MaxValue;
257+
_byteArr2[i] = byte.MaxValue;
257258
}
259+
258260
Guid guid4 = new Guid(_byteArr2);
259-
String[] strArr2 = new String[] { guid1.ToString(), guid2.ToString(), guid3.ToString(), guid4.ToString() };
261+
262+
string[] strArr2 = new string[] { guid1.ToString(), guid2.ToString(), guid3.ToString(), guid4.ToString() };
263+
260264
for (int i = 0; i < strArr1.Length; i++)
261265
{
262266
OutputHelper.WriteLine(strArr1[i]);
263-
Assert.AreEqual(String.Compare(strArr1[i], strArr2[i]), 0);
267+
Assert.AreEqual(string.Compare(strArr1[i], strArr2[i]), 0);
264268
}
265269
}
266270

@@ -280,11 +284,14 @@ public void Guid_Equals_Test11()
280284
Guid[] gArr1 = new Guid[] { guid11, guid12, guid13 };
281285

282286
// Creating Guids with 16 bytes constructor
283-
Byte[] _bArr1 = new Byte[16];
287+
byte[] _bArr1 = new byte[16];
288+
284289
Guid guid21 = new Guid(_bArr1);
285-
Byte[] _bArr2 = new Byte[] { 181, 54, 255, 77, 222, 157, 118, 79, 154, 42, 150, 67, 80, 71, 6, 61 };
290+
byte[] _bArr2 = new byte[] { 181, 54, 255, 77, 222, 157, 118, 79, 154, 42, 150, 67, 80, 71, 6, 61 };
291+
286292
Guid guid22 = new Guid(_bArr2);
287-
Byte[] _bArr3 = new Byte[] { 255, 255, 255, 127, 255, 127, 255, 127, 255, 255, 255, 255, 255, 255, 255, 255 };
293+
byte[] _bArr3 = new byte[] { 255, 255, 255, 127, 255, 127, 255, 127, 255, 255, 255, 255, 255, 255, 255, 255 };
294+
288295
Guid guid23 = new Guid(_bArr3);
289296
Guid[] gArr2 = new Guid[] { guid21, guid22, guid23 };
290297

@@ -315,5 +322,120 @@ public void Guid_Equals_Test11()
315322
}
316323
}
317324

325+
[DataRow("00000000-0000-0000-0000-000000000000")]
326+
[DataRow("4dff36b5-9dde-4f76-9a2a-96435047063d")]
327+
[DataRow("ffffffff-ffff-ffff-ffff-ffffffffffff")]
328+
[DataRow("a8a110d5-fc49-43c5-bf46-802db8f843ff")]
329+
[DataRow("44332211-6655-8877-9900-aabbccddeeff")]
330+
[DataRow("11223344-5566-7788-9900-aabbccddeeff")]
331+
[TestMethod]
332+
public void Ctor_FromString_Test00(string guidString)
333+
{
334+
/// <summary>
335+
/// 1. Creates a Guid from a string
336+
/// 2. Verifies the Guid is created correctly
337+
/// </summary>
338+
339+
Guid guid = new Guid(guidString);
340+
341+
Assert.AreEqual(guidString, guid.ToString());
342+
}
343+
[DataRow("{00000000-0000-0000-0000-000000000000}")]
344+
[DataRow("{4dff36b5-9dde-4f76-9a2a-96435047063d}")]
345+
[DataRow("{ffffffff-ffff-ffff-ffff-ffffffffffff}")]
346+
[DataRow("{a8a110d5-fc49-43c5-bf46-802db8f843ff}")]
347+
[DataRow("{44332211-6655-8877-9900-aabbccddeeff}")]
348+
[DataRow("{11223344-5566-7788-9900-aabbccddeeff}")]
349+
[TestMethod]
350+
public void Ctor_FromString_Test01(string guidString)
351+
{
352+
/// <summary>
353+
/// 1. Creates a Guid from a string
354+
/// 2. Verifies the Guid is created correctly
355+
/// </summary>
356+
357+
Guid guid = new Guid(guidString);
358+
359+
Assert.AreEqual(guidString, $"{{{guid.ToString()}}}");
360+
}
361+
362+
[TestMethod]
363+
public void Ctor_FromString_Test02()
364+
{
365+
Guid testGuid = new Guid("a8a110d5-fc49-43c5-bf46-802db8f843ff");
366+
Guid fullGuid = new Guid(uint.MaxValue, ushort.MaxValue, ushort.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
367+
368+
Assert.AreEqual(new Guid("ffffffff-ffff-ffff-ffff-ffffffffffff"), fullGuid);
369+
Assert.AreEqual((new Guid("a8a110d5-fc49-43c5-bf46-802db8f843ff")).ToString(), testGuid.ToString());
370+
}
371+
372+
[TestMethod]
373+
public void Guid_ByteArray_Test()
374+
{
375+
object[][] testData = new object[][]
376+
{
377+
new object[] { Guid.Empty, new byte[16] },
378+
new object[] { new Guid("44332211-6655-8877-9900-aabbccddeeff"), new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF } },
379+
new object[] { new Guid("11223344-5566-7788-9900-aabbccddeeff"), new byte[] { 0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF } },
380+
new object[] { new Guid("a8a110d5-fc49-43c5-bf46-802db8f843ff"), new byte[] { 0xd5, 0x10, 0xa1, 0xa8, 0x49, 0xfc, 0xc5, 0x43, 0xbf, 0x46, 0x80, 0x2d, 0xb8, 0xf8, 0x43, 0xff } }
381+
};
382+
383+
foreach (object[] item in testData)
384+
{
385+
Guid guid = new Guid((byte[])item[1]);
386+
387+
OutputHelper.WriteLine($"Actual: {guid}");
388+
OutputHelper.WriteLine($"Expected: {item[0]}");
389+
390+
Assert.AreEqual(item[0], guid);
391+
}
392+
}
393+
394+
[TestMethod]
395+
public void Ctor_NullByteArray_ThrowsArgumentNullException()
396+
{
397+
Assert.ThrowsException(
398+
typeof(ArgumentNullException),
399+
() => new Guid((byte[])null));
400+
}
401+
402+
[DataRow(15)]
403+
[DataRow(17)]
404+
[TestMethod]
405+
public void Ctor_InvalidLengthByteArray_ThrowsArgumentException(int length)
406+
{
407+
Assert.ThrowsException(
408+
typeof(ArgumentException),
409+
() => new Guid(new byte[length]));
410+
}
411+
412+
[TestMethod]
413+
public void Ctor_UInt_UShort_UShort_Byte_Byte_Byte_Byte_Byte_Byte_Byte_Byte()
414+
{
415+
Guid guid = new Guid(0xa8a110d5, 0xfc49, 0x43c5, 0xbf, 0x46, 0x80, 0x2d, 0xb8, 0xf8, 0x43, 0xff);
416+
Assert.AreEqual(new Guid("a8a110d5-fc49-43c5-bf46-802db8f843ff"), guid);
417+
}
418+
419+
[TestMethod]
420+
public void NewGuid()
421+
{
422+
Guid guid1 = Guid.NewGuid();
423+
424+
Assert.AreNotEqual(Guid.Empty, guid1);
425+
Assert.IsTrue((guid1.ToByteArray()[7] & 0xF0) == 0x40);
426+
427+
Guid guid2 = Guid.NewGuid();
428+
429+
Assert.AreNotEqual(guid1, guid2);
430+
Assert.IsTrue((guid2.ToByteArray()[7] & 0xF0) == 0x40);
431+
}
432+
433+
[TestMethod]
434+
public void ToByteArray()
435+
{
436+
byte[] myGuidAsArray = new Guid("a8a110d5-fc49-43c5-bf46-802db8f843ff").ToByteArray();
437+
438+
CollectionAssert.AreEqual(new byte[] { 0xd5, 0x10, 0xa1, 0xa8, 0x49, 0xfc, 0xc5, 0x43, 0xbf, 0x46, 0x80, 0x2d, 0xb8, 0xf8, 0x43, 0xff }, myGuidAsArray);
439+
}
318440
}
319441
}

0 commit comments

Comments
 (0)