summaryrefslogtreecommitdiff
path: root/tests/src/CoreMangLib
diff options
context:
space:
mode:
authorJan Kotas <jkotas@microsoft.com>2016-08-28 23:18:11 -0700
committerJan Kotas <jkotas@microsoft.com>2016-10-30 17:27:31 -0700
commite7dfda575898129eb77a3a8541990c43c4e29149 (patch)
tree885188360a88c9c7ed43472a6b6e994928fddc7d /tests/src/CoreMangLib
parent9c7c1b741b132cd78bfc9db2654227bc2aec2e45 (diff)
downloadcoreclr-e7dfda575898129eb77a3a8541990c43c4e29149.tar.gz
coreclr-e7dfda575898129eb77a3a8541990c43c4e29149.tar.bz2
coreclr-e7dfda575898129eb77a3a8541990c43c4e29149.zip
Fix reference types (#6954)
- Generic ArrayPinningHelper resulted into incorrect array data offset for reference types. Use non-generic one instead. - Add array covariance checks to guarantee type safety.
Diffstat (limited to 'tests/src/CoreMangLib')
-rw-r--r--tests/src/CoreMangLib/system/span/BasicSpanTest.cs78
1 files changed, 74 insertions, 4 deletions
diff --git a/tests/src/CoreMangLib/system/span/BasicSpanTest.cs b/tests/src/CoreMangLib/system/span/BasicSpanTest.cs
index c7c9618b9a..496234f8a6 100644
--- a/tests/src/CoreMangLib/system/span/BasicSpanTest.cs
+++ b/tests/src/CoreMangLib/system/span/BasicSpanTest.cs
@@ -1,8 +1,23 @@
using System;
using System.Collections.Generic;
+class ReferenceType
+{
+ internal byte Value;
+ public ReferenceType(byte value) { Value = value; }
+}
+
class My
{
+ static void AssertTrue(bool condition, string message)
+ {
+ if (!condition)
+ {
+ Console.WriteLine(message);
+ Environment.Exit(1);
+ }
+ }
+
static int Sum(Span<int> span)
{
int sum = 0;
@@ -11,12 +26,67 @@ class My
return sum;
}
- static void Main()
+ static void TestSum()
{
- int[] a = new int[] { 1, 2, 3 };
+ int[] a = new int[] { 1, 2, 3, 4 };
Span<int> span = new Span<int>(a);
- Console.WriteLine(Sum(span).ToString());
+ AssertTrue(Sum(span) == 10, "Unexpected sum of array");
Span<int> slice = span.Slice(1, 2);
- Console.WriteLine(Sum(slice).ToString());
+ AssertTrue(Sum(slice) == 5, "Unexpected sum of slice");
+ }
+
+ static void TestReferenceTypes()
+ {
+ var underlyingArray = new ReferenceType[] { new ReferenceType(0), new ReferenceType(1), new ReferenceType(2) };
+ var slice = new Span<ReferenceType>(underlyingArray);
+
+ for (int i = 0; i < underlyingArray.Length; i++)
+ {
+ AssertTrue(underlyingArray[i].Value == slice[i].Value, "Values are different");
+ AssertTrue(object.ReferenceEquals(underlyingArray[i], slice[i]), "References are broken");
+ }
+ }
+
+ static void TestArrayCoVariance()
+ {
+ var array = new ReferenceType[1];
+ var objArray = (object[])array;
+ try
+ {
+ new Span<object>(objArray);
+ AssertTrue(false, "Expected exception not thrown");
+ }
+ catch (ArrayTypeMismatchException)
+ {
+ }
+
+ var objEmptyArray = Array.Empty<ReferenceType>();
+ try
+ {
+ new Span<object>(objEmptyArray);
+ AssertTrue(false, "Expected exception not thrown");
+ }
+ catch (ArrayTypeMismatchException)
+ {
+ }
+ }
+
+ static void TestArrayCoVarianceReadOnly()
+ {
+ var array = new ReferenceType[1];
+ var objArray = (object[])array;
+ AssertTrue(new ReadOnlySpan<object>(objArray).Length == 1, "Unexpected length");
+
+ var objEmptyArray = Array.Empty<ReferenceType>();
+ AssertTrue(new ReadOnlySpan<object>(objEmptyArray).Length == 0, "Unexpected length");
+ }
+
+ static void Main()
+ {
+ TestSum();
+ TestReferenceTypes();
+ TestArrayCoVariance();
+ TestArrayCoVarianceReadOnly();
+ Console.WriteLine("All tests passed");
}
}