1
- // Licensed to the .NET Foundation under one or more agreements.
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System ;
@@ -60,7 +60,7 @@ public void ByteArrayConstructor_Test2()
60
60
/// </summary>
61
61
///
62
62
63
- Byte [ ] guid16 = GetRandomBytes ( 16 ) ;
63
+ byte [ ] guid16 = GetRandomBytes ( 16 ) ;
64
64
Guid myGuid1 = new Guid ( guid16 ) ;
65
65
}
66
66
@@ -82,7 +82,7 @@ public void ArgumentException_Test3()
82
82
{
83
83
size = random . Next ( 100 ) ;
84
84
}
85
- Byte [ ] guidNot16 = GetRandomBytes ( size ) ;
85
+ byte [ ] guidNot16 = GetRandomBytes ( size ) ;
86
86
Assert . ThrowsException ( typeof ( ArgumentException ) , ( ) => { Guid myGuid1 = new Guid ( guidNot16 ) ; } ) ;
87
87
}
88
88
@@ -95,7 +95,7 @@ public void ArgumentNullException_Test4()
95
95
/// </summary>
96
96
///
97
97
98
- Byte [ ] nullByte = null ;
98
+ byte [ ] nullByte = null ;
99
99
Assert . ThrowsException ( typeof ( ArgumentNullException ) , ( ) => { Guid myGuid1 = new Guid ( nullByte ) ; } ) ;
100
100
}
101
101
@@ -157,11 +157,11 @@ public void Int_Short_Constructor_Test6()
157
157
///
158
158
159
159
Random random = new Random ( ) ;
160
- int _int = random . Next ( Int32 . MaxValue ) ;
160
+ int _int = random . Next ( int . MaxValue ) ;
161
161
short _short1 = ( short ) random . Next ( 32768 ) ;
162
162
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 ] ) ;
165
165
}
166
166
167
167
[ TestMethod ]
@@ -174,11 +174,11 @@ public void UInt_Ushort_Constructor_Test7()
174
174
///
175
175
176
176
Random random = new Random ( ) ;
177
- int randoInt = random . Next ( Int32 . MaxValue ) ;
177
+ int randoInt = random . Next ( int . MaxValue ) ;
178
178
uint _uInt = ( uint ) ( randoInt * 2 ) ;
179
179
ushort _uShort1 = ( ushort ) random . Next ( 65536 ) ;
180
180
ushort _uShort2 = ( ushort ) random . Next ( 65536 ) ;
181
- Byte [ ] _bArr = GetRandomBytes ( 8 ) ;
181
+ byte [ ] _bArr = GetRandomBytes ( 8 ) ;
182
182
Guid _guid = new Guid ( _uInt , _uShort1 , _uShort1 , _bArr [ 0 ] , _bArr [ 1 ] , _bArr [ 2 ] , _bArr [ 3 ] , _bArr [ 4 ] , _bArr [ 5 ] , _bArr [ 6 ] , _bArr [ 7 ] ) ;
183
183
}
184
184
@@ -192,7 +192,7 @@ public void Guid_Empty_Test8()
192
192
/// </summary>
193
193
///
194
194
Guid guid = Guid . Empty ;
195
- Byte [ ] _bArr = guid . ToByteArray ( ) ;
195
+ byte [ ] _bArr = guid . ToByteArray ( ) ;
196
196
for ( int i = 0 ; i < 16 ; i ++ )
197
197
{
198
198
Assert . AreEqual ( _bArr [ i ] , ( byte ) 0 ) ;
@@ -211,26 +211,21 @@ public void Guid_CompareTo_Test9()
211
211
Guid guid1 = Guid . Empty ;
212
212
// Verifing any instance of Guid, regardless of its value, is greater than null
213
213
Assert . AreEqual ( guid1 . CompareTo ( null ) , 1 ) ;
214
- Byte [ ] _bArr = new Byte [ 16 ] ;
214
+ byte [ ] _bArr = new byte [ 16 ] ;
215
+
215
216
// Creating a Guid with all bytes zero
216
217
Guid guid2 = new Guid ( _bArr ) ;
217
218
Assert . AreEqual ( guid1 . CompareTo ( guid2 ) , 0 ) ;
219
+
218
220
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
+
223
223
Guid guid4 = new Guid ( 0x4dff36b5 , 0x9dde , 0x4f76 , 0x9a , 0x2a , 0x96 , 0x43 , 0x50 , 0x47 , 0x06 , 0x3d ) ;
224
224
Assert . AreEqual ( guid4 . CompareTo ( guid3 ) , 0 ) ;
225
+
225
226
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 ( ) ) ;
234
229
}
235
230
236
231
[ TestMethod ]
@@ -242,25 +237,34 @@ public void Guid_ToString_Test10()
242
237
/// </summary>
243
238
///
244
239
245
- String [ ] strArr1 = new String [ ] { "00000000-0000-0000-0000-000000000000" ,
240
+ string [ ] strArr1 = new string [ ]
241
+ {
242
+ "00000000-0000-0000-0000-000000000000" ,
246
243
"00000000-0000-0000-0000-000000000000" ,
247
244
"4dff36b5-9dde-4f76-9a2a-96435047063d" ,
248
- "ffffffff-ffff-ffff-ffff-ffffffffffff" } ;
245
+ "ffffffff-ffff-ffff-ffff-ffffffffffff"
246
+ } ;
247
+
249
248
Guid guid1 = Guid . Empty ;
250
- Byte [ ] _byteArr1 = new Byte [ 16 ] ;
249
+ byte [ ] _byteArr1 = new byte [ 16 ] ;
251
250
Guid guid2 = new Guid ( _byteArr1 ) ;
252
251
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
+
254
255
for ( int i = 0 ; i < _byteArr2 . Length ; i ++ )
255
256
{
256
- _byteArr2 [ i ] = Byte . MaxValue ;
257
+ _byteArr2 [ i ] = byte . MaxValue ;
257
258
}
259
+
258
260
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
+
260
264
for ( int i = 0 ; i < strArr1 . Length ; i ++ )
261
265
{
262
266
OutputHelper . WriteLine ( strArr1 [ i ] ) ;
263
- Assert . AreEqual ( String . Compare ( strArr1 [ i ] , strArr2 [ i ] ) , 0 ) ;
267
+ Assert . AreEqual ( string . Compare ( strArr1 [ i ] , strArr2 [ i ] ) , 0 ) ;
264
268
}
265
269
}
266
270
@@ -280,11 +284,14 @@ public void Guid_Equals_Test11()
280
284
Guid [ ] gArr1 = new Guid [ ] { guid11 , guid12 , guid13 } ;
281
285
282
286
// Creating Guids with 16 bytes constructor
283
- Byte [ ] _bArr1 = new Byte [ 16 ] ;
287
+ byte [ ] _bArr1 = new byte [ 16 ] ;
288
+
284
289
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
+
286
292
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
+
288
295
Guid guid23 = new Guid ( _bArr3 ) ;
289
296
Guid [ ] gArr2 = new Guid [ ] { guid21 , guid22 , guid23 } ;
290
297
@@ -315,5 +322,120 @@ public void Guid_Equals_Test11()
315
322
}
316
323
}
317
324
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
+ }
318
440
}
319
441
}
0 commit comments