@@ -11,7 +11,7 @@ namespace System.SpanTests
1111 public static partial class ReadOnlySpanTests
1212 {
1313 [ Fact ]
14- public static void PortableCastUIntToUShort ( )
14+ public static void NonPortableCastUIntToUShort ( )
1515 {
1616 uint [ ] a = { 0x44332211 , 0x88776655 } ;
1717 ReadOnlySpan < uint > span = new ReadOnlySpan < uint > ( a ) ;
@@ -22,14 +22,34 @@ public static void PortableCastUIntToUShort()
2222 }
2323
2424 [ Fact ]
25- public static void PortableCastToTypeContainsReferences ( )
25+ public static void NonPortableCastShortToLong ( )
26+ {
27+ short [ ] a = { 0x1234 , 0x2345 , 0x3456 , 0x4567 , 0x5678 } ;
28+ ReadOnlySpan < short > span = new ReadOnlySpan < short > ( a ) ;
29+ ReadOnlySpan < long > asLong = span . NonPortableCast < short , long > ( ) ;
30+
31+ Assert . True ( Unsafe . AreSame < long > ( ref Unsafe . As < short , long > ( ref MemoryMarshal . GetReference ( span ) ) , ref MemoryMarshal . GetReference ( asLong ) ) ) ;
32+ asLong . Validate < long > ( 0x4567345623451234 ) ;
33+ }
34+
35+ [ Fact ]
36+ public static unsafe void NonPortableCastOverflow ( )
37+ {
38+ ReadOnlySpan < TestHelpers . TestStructExplicit > span = new ReadOnlySpan < TestHelpers . TestStructExplicit > ( null , Int32 . MaxValue ) ;
39+
40+ TestHelpers . AssertThrows < OverflowException , TestHelpers . TestStructExplicit > ( span , ( _span ) => _span . NonPortableCast < TestHelpers . TestStructExplicit , byte > ( ) . DontBox ( ) ) ;
41+ TestHelpers . AssertThrows < OverflowException , TestHelpers . TestStructExplicit > ( span , ( _span ) => _span . NonPortableCast < TestHelpers . TestStructExplicit , ulong > ( ) . DontBox ( ) ) ;
42+ }
43+
44+ [ Fact ]
45+ public static void NonPortableCastToTypeContainsReferences ( )
2646 {
2747 ReadOnlySpan < uint > span = new ReadOnlySpan < uint > ( Array . Empty < uint > ( ) ) ;
2848 TestHelpers . AssertThrows < ArgumentException , uint > ( span , ( _span ) => _span . NonPortableCast < uint , StructWithReferences > ( ) . DontBox ( ) ) ;
2949 }
3050
3151 [ Fact ]
32- public static void PortableCastFromTypeContainsReferences ( )
52+ public static void NonPortableCastFromTypeContainsReferences ( )
3353 {
3454 ReadOnlySpan < StructWithReferences > span = new ReadOnlySpan < StructWithReferences > ( Array . Empty < StructWithReferences > ( ) ) ;
3555 TestHelpers . AssertThrows < ArgumentException , StructWithReferences > ( span , ( _span ) => _span . NonPortableCast < StructWithReferences , uint > ( ) . DontBox ( ) ) ;
0 commit comments