Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,119 @@
//
// AsyncVoidMethodBuilderTest.cs
//
// Authors:
// Marek Safar <marek.safar@gmail.com>
//
// Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_4_5
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using System.Runtime.CompilerServices;
namespace MonoTests.System.Runtime.CompilerServices
{
[TestFixture]
public class AsyncVoidMethodBuilderTest
{
class MyContext : SynchronizationContext
{
public int Started;
public int Completed;
public int PostCounter;
public int SendCounter;
public override void OperationStarted ()
{
++Started;
base.OperationStarted ();
}
public override void OperationCompleted ()
{
++Completed;
base.OperationCompleted ();
}
public override void Post (SendOrPostCallback d, object state)
{
if (state is Exception) {
++PostCounter;
}
}
public override void Send (SendOrPostCallback d, object state)
{
if (state is Exception) {
++SendCounter;
base.Send (d, state);
}
}
}
[Test]
public void SetResult ()
{
var awaiter = AsyncVoidMethodBuilder.Create ();
awaiter.SetResult ();
}
[Test]
public void SetException ()
{
var context = new MyContext ();
var old = SynchronizationContext.Current;
try {
SynchronizationContext.SetSynchronizationContext (context);
var awaiter = AsyncVoidMethodBuilder.Create ();
Assert.AreEqual (1, context.Started, "#1");
Assert.AreEqual (0, context.Completed, "#2");
Assert.AreEqual (0, context.SendCounter, "#3");
Assert.AreEqual (0, context.PostCounter, "#4");
awaiter.SetException (new ApplicationException ());
Assert.AreEqual (1, context.Started, "#5");
Assert.AreEqual (1, context.Completed, "#6");
Assert.AreEqual (0, context.SendCounter, "#7");
Assert.AreEqual (1, context.PostCounter, "#8");
awaiter.SetResult ();
Assert.AreEqual (1, context.Started, "#9");
Assert.AreEqual (2, context.Completed, "#10");
Assert.AreEqual (0, context.SendCounter, "#11");
Assert.AreEqual (1, context.PostCounter, "#12");
} finally {
SynchronizationContext.SetSynchronizationContext (old);
}
}
}
}
#endif

View File

@ -0,0 +1,51 @@
2010-06-21 Rodrigo Kumpera <rkumpera@novell.com>
* ConditionalWeakTableTest.cs: Make test more resilient.
2010-06-18 Rodrigo Kumpera <rkumpera@novell.com>
* ConditionalWeakTableTest.cs: Fix another test.
2010-06-18 Rodrigo Kumpera <rkumpera@novell.com>
* ConditionalWeakTableTest.cs: Fix it.
2010-05-11 Rodrigo Kumpera <rkumpera@novell.com>
* ConditionalWeakTableTest.cs: New test suite.
2009-10-15 Sebastien Pouliot <sebastien@ximian.com>
* RuntimeHelpersTest.cs: Add more test cases for validations
2009-06-20 Zoltan Varga <vargaz@gmail.com>
* *.cs: Convert all tests to new-style nunit classes/methods.
2009-06-10 Gert Driesen <drieseng@users.sourceforge.net>
* InternalVisibleToAttributeTest.cs: Added test for .ctor and
AllInternalsVisible property.
2006-11-01 Sebastien Pouliot <sebastien@ximian.com>
* RuntimeWrappedExceptionCas.cs: New. CAS tests for
RuntimeWrappedException.
* RuntimeWrappedExceptionTest.cs: New. Unit tests for
RuntimeWrappedException.
2003-04-18 Zoltan Varga <vargaz@freemail.hu>
* RuntimeHelpersTest.cs: Added tests for Equals() and GetHashCode ()
methods.
2002-12-21 Nick Drochak <ndrochak@gol.com>
* all: make tests build and run under nunit2
2002-09-21 Zoltan Varga <vargaz@freemail.hu>
* AllTests.cs: new file
* RuntimeHelpersTest.cs: new file

View File

@ -0,0 +1,492 @@
//
// ConditionalWeakTableTest.cs
//
// Author:
// Rodrigo Kumpera <rkumpera@novell.com>
//
// Copyright (C) 2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_4_0
using NUnit.Framework;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Collections.Generic;
using System.Threading;
namespace MonoTests.System.Runtime.CompilerServices {
[TestFixture]
public class ConditionalWeakTableTest {
public class Link {
public object obj;
public Link(object obj) {
this.obj = obj;
}
}
class Key {
public int Foo;
public override string ToString () {
return "key-" + Foo;
}
}
class Val {
public int Foo;
public override string ToString () {
return "value-" + Foo;
}
}
[Test]
public void GetValue () {
var cwt = new ConditionalWeakTable <object,object> ();
try {
cwt.GetValue (null, k => null);
Assert.Fail ("#0");
} catch (ArgumentNullException) {}
try {
cwt.GetValue (20, null);
Assert.Fail ("#1");
} catch (ArgumentNullException) {}
object key = "foo";
object val = cwt.GetValue (key, k => new Link (k));
Assert.IsTrue (val != null, "#2");
Assert.AreEqual (typeof (Link), val.GetType () , "#3");
Assert.AreEqual (val, cwt.GetValue (key, k => new object ()), "#4");
}
[Test]
public void GetOrCreateValue () {
var cwt = new ConditionalWeakTable <object,object> ();
try {
cwt.GetOrCreateValue (null);
Assert.Fail ("#0");
} catch (ArgumentNullException) {}
object key = "foo";
object val = cwt.GetOrCreateValue (key);
Assert.IsTrue (val != null, "#2");
Assert.AreEqual (typeof (object), val.GetType () , "#3");
Assert.AreEqual (val, cwt.GetOrCreateValue (key), "#4");
var cwt2 = new ConditionalWeakTable <object, string> ();
try {
cwt2.GetOrCreateValue (key);
Assert.Fail ("#5");
} catch (MissingMethodException) {}
}
[Test]
public void Remove () {
var cwt = new ConditionalWeakTable <object,object> ();
object c = new Key ();
cwt.Add (c, "x");
try {
cwt.Remove (null);
Assert.Fail ("#0");
} catch (ArgumentNullException) {}
Assert.IsFalse (cwt.Remove ("x"), "#1");
Assert.IsTrue (cwt.Remove (c), "#2");
Assert.IsFalse (cwt.Remove (c), "#3");
}
[Test]
public void Add () {
var cwt = new ConditionalWeakTable <object,object> ();
object c = new Key ();
cwt.Add (c, new Link (c));
try {
cwt.Add (c, new Link (c));
Assert.Fail ("#0");
} catch (ArgumentException) {}
cwt.Add ("zzz", null);//ok
try {
cwt.Add (null, new Link (c));
Assert.Fail ("#1");
} catch (ArgumentNullException) {}
}
[Test]
public void TryGetValue () {
var cwt = new ConditionalWeakTable <object,object> ();
object res;
object c = new Key ();
cwt.Add (c, "foo");
try {
cwt.TryGetValue (null, out res);
Assert.Fail ("#0");
} catch (ArgumentNullException) {}
Assert.IsFalse (cwt.TryGetValue ("foo", out res), "#1");
Assert.IsTrue (cwt.TryGetValue (c, out res), "#2");
Assert.AreEqual ("foo", res, "#3");
}
static void FillStuff (ConditionalWeakTable <object,object> cwt,
out List<object> keepAlive, out List<WeakReference> keys) {
keepAlive = new List<object> ();
keys = new List<WeakReference> ();
object a = new Key ();
object b = new Key ();
object c = new Key ();
cwt.Add (a, new string ("str0".ToCharArray ()));
cwt.Add (b, "str1");
cwt.Add (c, new Link (a));
keepAlive.Add (c);
keys.Add (new WeakReference(a));
keys.Add (new WeakReference(b));
keys.Add (new WeakReference(c));
}
[Test]
public void Reachability () {
if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
Assert.Ignore ("Not working on Boehm.");
var cwt = new ConditionalWeakTable <object,object> ();
List<object> keepAlive;
List<WeakReference> keys;
FillStuff (cwt, out keepAlive, out keys);
GC.Collect ();
Assert.IsTrue (keys [0].IsAlive, "r0");
Assert.IsFalse (keys [1].IsAlive, "r1");
Assert.IsTrue (keys [2].IsAlive, "r2");
object res;
Assert.IsTrue (cwt.TryGetValue (keepAlive [0], out res), "ka0");
Assert.IsTrue (res is Link, "ka1");
Link link = res as Link;
Assert.IsTrue (cwt.TryGetValue (link.obj, out res), "ka2");
Assert.AreEqual ("str0", res, "ka3");
}
static List<WeakReference> FillWithNetwork (ConditionalWeakTable <object,object> cwt)
{
const int K = 500;
object[] keys = new object [K];
for (int i = 0; i < K; ++i)
keys[i] = new object ();
Random rand = new Random ();
/*produce a complex enough network of links*/
for (int i = 0; i < K; ++i)
cwt.Add (keys [i], new Link (keys [rand.Next (K)]));
var res = new List<WeakReference> ();
for (int i = 0; i < 10; ++i)
res.Add (new WeakReference (keys [rand.Next (K)]));
Array.Clear (keys, 0, keys.Length);
return res;
}
[Test]
public void InsertStress () {
if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
Assert.Ignore ("Not working on Boehm.");
var cwt = new ConditionalWeakTable <object,object> ();
var a = new object ();
var b = new object ();
cwt.Add (a, new object ());
cwt.Add (b, new object ());
List<WeakReference> res = null;
ThreadStart dele = () => { res = FillWithNetwork (cwt); };
var th = new Thread(dele);
th.Start ();
th.Join ();
GC.Collect ();
GC.Collect ();
for (int i = 0; i < res.Count; ++i)
Assert.IsFalse (res [i].IsAlive, "#r" + i);
}
static List<WeakReference> FillWithNetwork2 (ConditionalWeakTable <object,object>[] cwt) {
if (cwt [0] == null)
cwt[0] = new ConditionalWeakTable <object,object> ();
var res = FillWithNetwork (cwt[0]);
return res;
}
static void ForcePromotion () {
var o = new object[64000];
for (int i = 0; i < 64000; ++i)
o[i] = new int[10];
}
static List<object> FillReachable (ConditionalWeakTable <object,object>[] cwt)
{
var res = new List<object> ();
for (int i = 0; i < 10; ++i) {
res.Add (new object ());
cwt[0].Add (res [i], i);
}
return res;
}
[Test]
public void OldGenStress () {
if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
Assert.Ignore ("Not working on Boehm.");
var cwt = new ConditionalWeakTable <object,object>[1];
List<object> k = null;
List<WeakReference> res, res2;
res = res2 = null;
ThreadStart dele = () => {
res = FillWithNetwork2 (cwt);
ForcePromotion ();
k = FillReachable (cwt);
res2 = FillWithNetwork2 (cwt);
};
var th = new Thread(dele);
th.Start ();
th.Join ();
GC.Collect ();
for (int i = 0; i < res.Count; ++i)
Assert.IsFalse (res [i].IsAlive, "#r0-" + i);
for (int i = 0; i < res2.Count; ++i)
Assert.IsFalse (res2 [i].IsAlive, "#r1-" + i);
for (int i = 0; i < k.Count; ++i) {
object val;
Assert.IsTrue (cwt[0].TryGetValue (k [i], out val), "k0-" + i);
Assert.AreEqual (i, val, "k1-" + i);
}
}
static List<GCHandle> FillTable3 (ConditionalWeakTable <object,object> cwt) {
var handles = new List<GCHandle> ();
var a = (object)10;
var b = (object)20;
var k1 = (object)30;
var k2 = (object)40;
handles.Add (GCHandle.Alloc (a, GCHandleType.Pinned));
handles.Add (GCHandle.Alloc (b, GCHandleType.Pinned));
handles.Add (GCHandle.Alloc (k1, GCHandleType.Pinned));
handles.Add (GCHandle.Alloc (k2, GCHandleType.Pinned));
cwt.Add (a, k1);
cwt.Add (b, k2);
return handles;
}
static void MakeObjMovable (List<GCHandle> handles)
{
for (int i = 0; i < handles.Count; ++i) {
object o = handles[i].Target;
handles[i].Free ();
handles[i] = GCHandle.Alloc (o, GCHandleType.Normal);
}
}
static void ForceMinor () {
for (int i = 0; i < 64000; ++i)
new object ();
}
public void PromotedCwtPointingToYoungStuff () {
var cwt = new ConditionalWeakTable <object,object> ();
var handles = FillTable3 (cwt);
GC.Collect (0);
/*Be 100% sure it will be on the young gen*/
/*cwt array now will be on old gen*/
ForceMinor ();
ForceMinor ();
ForceMinor ();
//Make them non pinned
MakeObjMovable (handles);
GC.Collect (0);
//Force a minor GC - this should cause
ForceMinor ();
ForceMinor ();
ForceMinor ();
ForceMinor ();
GC.Collect (0);
object r1, r2;
Assert.IsTrue (cwt.TryGetValue (handles[0].Target, out r1), "#1");
Assert.IsTrue (cwt.TryGetValue (handles[1].Target, out r2), "#2");
GC.Collect ();
cwt.GetHashCode ();
}
static object _lock1 = new object ();
static object _lock2 = new object ();
static int reachable = 0;
public class FinalizableLink {
object obj;
ConditionalWeakTable <object,object> cwt;
int id;
public FinalizableLink (int id, object obj, ConditionalWeakTable <object,object> cwt) {
this.id = id;
this.obj = obj;
this.cwt = cwt;
}
~FinalizableLink () {
lock (_lock1) { ; }
object obj;
bool res = cwt.TryGetValue (this, out obj);
if (res)
++reachable;
if (reachable == 20)
lock (_lock2) { Monitor.Pulse (_lock2); }
}
}
static void FillWithFinalizable (ConditionalWeakTable <object,object> cwt)
{
object a = new object ();
object b = new FinalizableLink (0, a, cwt);
cwt.Add (a, "foo");
cwt.Add (b, "bar");
for (int i = 1; i < 20; ++i) {
b = new FinalizableLink (i, b, cwt);
cwt.Add (b, i);
}
}
[Test]
public void FinalizableObjectsThatRetainDeadKeys ()
{
if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
Assert.Ignore ("Not working on Boehm.");
lock (_lock1) {
var cwt = new ConditionalWeakTable <object,object> ();
ThreadStart dele = () => { FillWithFinalizable (cwt); };
var th = new Thread(dele);
th.Start ();
th.Join ();
GC.Collect ();
GC.Collect ();
Assert.AreEqual (0, reachable, "#1");
}
GC.Collect ();
GC.Collect ();
lock (_lock2) { Monitor.Wait (_lock2, 1000); }
Assert.AreEqual (20, reachable, "#1");
}
[Test]
public void OldGenKeysMakeNewGenObjectsReachable ()
{
if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
Assert.Ignore ("Not working on Boehm.");
ConditionalWeakTable<object, Val> table = new ConditionalWeakTable<object, Val>();
List<Key> keys = new List<Key>();
//
// This list references all keys for the duration of the program, so none
// should be collected ever.
//
for (int x = 0; x < 1000; x++)
keys.Add (new Key () { Foo = x });
for (int i = 0; i < 1000; ++i) {
// Insert all keys into the ConditionalWeakTable
foreach (var key in keys)
table.Add (key, new Val () { Foo = key.Foo });
// Look up all keys to verify that they are still there
Val val;
foreach (var key in keys)
Assert.IsTrue (table.TryGetValue (key, out val), "#1-" + i + "-k-" + key);
// Remove all keys from the ConditionalWeakTable
foreach (var key in keys)
Assert.IsTrue (table.Remove (key), "#2-" + i + "-k-" + key);
}
}
}
}
#endif

View File

@ -0,0 +1,82 @@
//
// InternalsVisibleToAttributeTest.cs - Unit tests for
// System.Runtime.CompilerServices.InternalsVisibleToAttribute
//
// Author:
// Gert Driesen <drieseng@users.sourceforge.net>
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using System.Runtime.CompilerServices;
using NUnit.Framework;
namespace MonoTests.System.Runtime.CompilerServices {
[TestFixture]
public class InternalsVisibleToAttributeTest {
[Test] // .ctor (String)
public void Constructor1 ()
{
InternalsVisibleToAttribute ivt;
string aname;
aname = "Mono";
ivt = new InternalsVisibleToAttribute (aname);
Assert.IsTrue (ivt.AllInternalsVisible, "#A:AllInternalsVisible");
Assert.AreSame (aname, ivt.AssemblyName, "#A:AssemblyName");
Assert.AreEqual (ivt.GetType (), ivt.TypeId, "#A:TypeId");
aname = typeof (int).Assembly.FullName;
ivt = new InternalsVisibleToAttribute (aname);
Assert.IsTrue (ivt.AllInternalsVisible, "#B:AllInternalsVisible");
Assert.AreSame (aname, ivt.AssemblyName, "#B:AssemblyName");
Assert.AreEqual (ivt.GetType (), ivt.TypeId, "#B:TypeId");
aname = string.Empty;
ivt = new InternalsVisibleToAttribute (aname);
Assert.IsTrue (ivt.AllInternalsVisible, "#C:AllInternalsVisible");
Assert.AreSame (aname, ivt.AssemblyName, "#C:AssemblyName");
Assert.AreEqual (ivt.GetType (), ivt.TypeId, "#C:TypeId");
aname = null;
ivt = new InternalsVisibleToAttribute (aname);
Assert.IsTrue (ivt.AllInternalsVisible, "#D:AllInternalsVisible");
Assert.IsNull (ivt.AssemblyName, "#D:AssemblyName");
Assert.AreEqual (ivt.GetType (), ivt.TypeId, "#D:TypeId");
}
[Test]
public void AllInternalsVisible ()
{
InternalsVisibleToAttribute ivt = new InternalsVisibleToAttribute ("Mono");
ivt.AllInternalsVisible = false;
Assert.IsFalse (ivt.AllInternalsVisible, "#1");
ivt.AllInternalsVisible = true;
Assert.IsTrue (ivt.AllInternalsVisible, "#2");
}
}
}
#endif

View File

@ -0,0 +1,185 @@
//
// RuntimeHelpersTest.cs - NUnit Test Cases for the System.Runtime.CompilerServices.RuntimeHelpers class
//
// Zoltan Varga (vargaz@freemail.hu)
//
// (C) Ximian, Inc. http://www.ximian.com
using System;
using System.Runtime.CompilerServices;
using NUnit.Framework;
namespace MonoTests.System.Runtime.CompilerServices {
[TestFixture]
public class RuntimeHelpersTest {
struct FooStruct {
public int i;
public string j;
public override int GetHashCode () {
return 5;
}
public override bool Equals (object o) {
Assert.Fail ();
return false;
}
}
class FooClass {
public static int counter = 0;
static FooClass () {
counter = counter + 1;
}
public override int GetHashCode () {
return 5;
}
public override bool Equals (object o) {
Assert.Fail ();
return true;
}
}
public void TestOffsetToStringData ()
{
Assert.AreEqual (
RuntimeHelpers.OffsetToStringData,
RuntimeHelpers.OffsetToStringData, "OffsetToStringData is not constant");
}
public void TestGetObjectValue ()
{
FooStruct s1;
FooStruct s2;
// Test null
Assert.AreEqual (RuntimeHelpers.GetObjectValue (null),
null);
// Test non-valuetype
Assert.AreEqual (RuntimeHelpers.GetObjectValue (this),
this);
// Test valuetype
s1.i = 42;
s1.j = "FOO";
s2 = (FooStruct)RuntimeHelpers.GetObjectValue(s1);
s1.i = 43;
s1.j = "BAR";
Assert.AreEqual (s2.i, 42);
Assert.AreEqual (s2.j, "FOO");
}
public void TestRunClassConstructor ()
{
RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
Assert.AreEqual (FooClass.counter, 1);
// Each static constructor should only be run once
RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
Assert.AreEqual (FooClass.counter, 1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void RunClassConstructor_Default ()
{
RuntimeTypeHandle rth = new RuntimeTypeHandle ();
Assert.AreEqual (IntPtr.Zero, rth.Value, "Value");
RuntimeHelpers.RunClassConstructor (rth);
}
static RuntimeTypeHandle handle;
[Test]
[ExpectedException (typeof (ArgumentException))]
public void RunClassConstructor_Uninitialized ()
{
RuntimeHelpers.RunClassConstructor (handle);
}
class Thrower {
static Thrower ()
{
throw new NotFiniteNumberException ();
}
}
[Test]
[ExpectedException (typeof (TypeInitializationException))]
public void RunClassConstructor_Throw ()
{
RuntimeHelpers.RunClassConstructor (typeof (Thrower).TypeHandle);
}
class Fielder {
public byte [] array = new byte [1];
}
static RuntimeFieldHandle rfh = typeof (Fielder).GetField ("array").FieldHandle;
static RuntimeFieldHandle static_rfh;
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void InitializeArray_Null ()
{
RuntimeHelpers.InitializeArray (null, rfh);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void InitializeArray_Default ()
{
RuntimeFieldHandle h = new RuntimeFieldHandle ();
RuntimeHelpers.InitializeArray (new Fielder ().array, h);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void InitializeArray_Uninitialized ()
{
RuntimeHelpers.InitializeArray (new Fielder ().array, static_rfh);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void InitializeArray ()
{
RuntimeHelpers.InitializeArray (new Fielder ().array, rfh);
}
public void TestGetHashCode ()
{
Assert.AreEqual (0, RuntimeHelpers.GetHashCode (null));
object o = new object ();
Assert.AreEqual (o.GetHashCode (), RuntimeHelpers.GetHashCode (o));
Assert.IsTrue (5 != RuntimeHelpers.GetHashCode (new FooClass ()));
}
public void TestEquals ()
{
Assert.IsTrue (RuntimeHelpers.Equals (null, null));
Assert.IsTrue (!RuntimeHelpers.Equals (new object (), null));
Assert.IsTrue (!RuntimeHelpers.Equals (null, new object ()));
FooStruct f1 = new FooStruct ();
f1.i = 5;
FooStruct f2 = new FooStruct ();
f2.i = 5;
object o1 = f1;
object o2 = o1;
object o3 = f2;
object o4 = "AAA";
Assert.IsTrue (RuntimeHelpers.Equals (o1, o2));
// This should do a bit-by-bit comparison for valuetypes
Assert.IsTrue (RuntimeHelpers.Equals (o1, o3));
Assert.IsTrue (!RuntimeHelpers.Equals (o1, o4));
}
}
}

View File

@ -0,0 +1,94 @@
//
// RuntimeWrappedExceptionCas.cs - CAS Test Cases for
// System.Runtime.CompilerServices.RuntimeWrappedException
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using MonoTests.System.Runtime.CompilerServices;
namespace MonoCasTests.System.Runtime.CompilerServices {
[TestFixture]
[Category ("CAS")]
public class RuntimeWrappedExceptionCas {
private RuntimeWrappedExceptionTest unit;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
unit = new RuntimeWrappedExceptionTest ();
unit.FixtureSetUp ();
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void UnitTests_Deny_Unrestricted ()
{
unit.WrappedException ();
unit.GetObjectData ();
// this means GetObjectData isn't protected by a Demand
// (but it may be, like documented, a LinkDemand)
}
// we use reflection to call RuntimeWrappedException as the GetObjectData method
// is protected by LinkDemand (which will be converted into full demand, i.e. a
// stack walk) when reflection is used (i.e. it gets testable).
[Test]
[SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
[ExpectedException (typeof (SecurityException))]
public void GetObjectData_Deny_SerializationFormatter ()
{
SerializationInfo info = null;
StreamingContext context = new StreamingContext (StreamingContextStates.All);
MethodInfo mi = typeof (RuntimeWrappedException).GetMethod ("GetObjectData");
mi.Invoke (unit.rwe, new object[2] { info, context });
}
[Test]
[SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
public void GetObjectData_PermitOnly_SerializationFormatter ()
{
SerializationInfo info = new SerializationInfo (typeof (RuntimeWrappedException), new FormatterConverter ());
StreamingContext context = new StreamingContext (StreamingContextStates.All);
MethodInfo mi = typeof (RuntimeWrappedException).GetMethod ("GetObjectData");
mi.Invoke (unit.rwe, new object[2] { info, context });
}
}
}
#endif

View File

@ -0,0 +1,90 @@
//
// RuntimeWrappedExceptionTest.cs - NUnit Test Cases for
// System.Runtime.CompilerServices.RuntimeWrappedException
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace MonoTests.System.Runtime.CompilerServices {
[TestFixture]
public class RuntimeWrappedExceptionTest {
internal RuntimeWrappedException rwe;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
Type rwet = typeof (RuntimeWrappedException);
ConstructorInfo[] ctors = rwet.GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic);
foreach (ConstructorInfo ctor in ctors) {
switch (ctor.GetParameters ().Length) {
case 0:
// mono
rwe = (RuntimeWrappedException) ctor.Invoke (null);
return;
case 1:
// ms
rwe = (RuntimeWrappedException) ctor.Invoke (new object[1] { null });
return;
}
}
Assert.Ignore ("uho, couldn't figure out RuntimeWrappedException ctor");
}
[Test]
public void WrappedException ()
{
Assert.IsNull (rwe.WrappedException, "WrappedException");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void GetObjectData_Null ()
{
rwe.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
}
[Test]
public void GetObjectData ()
{
SerializationInfo info = new SerializationInfo (typeof (RuntimeWrappedException), new FormatterConverter ());
rwe.GetObjectData (info, new StreamingContext (StreamingContextStates.All));
Assert.IsNull (info.GetValue ("WrappedException", typeof (object)), "WrappedException");
// a SerializationException would occur if this was a bad name
}
}
}
#endif

View File

@ -0,0 +1,312 @@
//
// TaskAwaiterTest.cs
//
// Authors:
// Marek Safar <marek.safar@gmail.com>
//
// Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_4_5
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Collections;
namespace MonoTests.System.Runtime.CompilerServices
{
[TestFixture]
public class TaskAwaiterTest
{
class Scheduler : TaskScheduler
{
string name;
public Scheduler (string name)
{
this.name = name;
}
public int InlineCalls { get; set; }
public int QueueCalls { get; set; }
protected override IEnumerable<Task> GetScheduledTasks ()
{
throw new NotImplementedException ();
}
protected override void QueueTask (Task task)
{
++QueueCalls;
ThreadPool.QueueUserWorkItem (o => {
TryExecuteTask (task);
});
}
protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
{
++InlineCalls;
return false;
}
}
class SingleThreadSynchronizationContext : SynchronizationContext
{
readonly Queue _queue = new Queue ();
public void RunOnCurrentThread ()
{
while (_queue.Count != 0) {
var workItem = (KeyValuePair<SendOrPostCallback, object>) _queue.Dequeue ();
workItem.Key (workItem.Value);
}
}
public override void Post (SendOrPostCallback d, object state)
{
if (d == null) {
throw new ArgumentNullException ("d");
}
_queue.Enqueue (new KeyValuePair<SendOrPostCallback, object> (d, state));
}
public override void Send (SendOrPostCallback d, object state)
{
throw new NotSupportedException ("Synchronously sending is not supported.");
}
}
string progress;
SynchronizationContext sc;
[SetUp]
public void Setup ()
{
sc = SynchronizationContext.Current;
}
[TearDown]
public void TearDown ()
{
SynchronizationContext.SetSynchronizationContext (sc);
}
[Test]
public void GetResultFaulted ()
{
TaskAwaiter awaiter;
var task = new Task (() => { throw new ApplicationException (); });
awaiter = task.GetAwaiter ();
task.RunSynchronously (TaskScheduler.Current);
Assert.IsTrue (awaiter.IsCompleted);
try {
awaiter.GetResult ();
Assert.Fail ();
} catch (ApplicationException) {
}
}
[Test]
public void GetResultCanceled ()
{
TaskAwaiter awaiter;
var token = new CancellationToken (true);
var task = new Task (() => { }, token);
awaiter = task.GetAwaiter ();
try {
awaiter.GetResult ();
Assert.Fail ();
} catch (TaskCanceledException) {
}
}
[Test]
public void GetResultWaitOnCompletion ()
{
TaskAwaiter awaiter;
var task = Task.Delay (30);
awaiter = task.GetAwaiter ();
awaiter.GetResult ();
Assert.AreEqual (TaskStatus.RanToCompletion, task.Status);
}
[Test]
public void CustomScheduler ()
{
// some test runners (e.g. Touch.Unit) will execute this on the main thread and that would lock them
if (!Thread.CurrentThread.IsBackground)
Assert.Ignore ("Current thread is not running in the background.");
var a = new Scheduler ("a");
var b = new Scheduler ("b");
var t = TestCS (a, b);
Assert.IsTrue (t.Wait (3000), "#0");
Assert.AreEqual (0, t.Result, "#1");
Assert.AreEqual (0, b.InlineCalls, "#2b");
Assert.AreEqual (2, a.QueueCalls, "#3a");
Assert.AreEqual (1, b.QueueCalls, "#3b");
}
static async Task<int> TestCS (TaskScheduler schedulerA, TaskScheduler schedulerB)
{
var res = await Task.Factory.StartNew (async () => {
if (TaskScheduler.Current != schedulerA)
return 1;
await Task.Factory.StartNew (
() => {
if (TaskScheduler.Current != schedulerB)
return 2;
return 0;
}, CancellationToken.None, TaskCreationOptions.None, schedulerB);
if (TaskScheduler.Current != schedulerA)
return 3;
return 0;
}, CancellationToken.None, TaskCreationOptions.None, schedulerA);
return res.Result;
}
[Test]
public void FinishedTaskOnCompleted ()
{
var mres = new ManualResetEvent (false);
var mres2 = new ManualResetEvent (false);
var tcs = new TaskCompletionSource<object> ();
tcs.SetResult (null);
var task = tcs.Task;
var awaiter = task.GetAwaiter ();
Assert.IsTrue (awaiter.IsCompleted, "#1");
awaiter.OnCompleted(() => {
if (mres.WaitOne (1000))
mres2.Set ();
});
mres.Set ();
// this will only terminate correctly if the test was not executed from the main thread
// e.g. Touch.Unit defaults to run tests on the main thread and this will return false
Assert.AreEqual (Thread.CurrentThread.IsBackground, mres2.WaitOne (2000), "#2");;
}
[Test]
public void CompletionOnSameCustomSynchronizationContext ()
{
progress = "";
var syncContext = new SingleThreadSynchronizationContext ();
SynchronizationContext.SetSynchronizationContext (syncContext);
syncContext.Post (delegate {
Go (syncContext);
}, null);
// Custom message loop
var cts = new CancellationTokenSource ();
cts.CancelAfter (5000);
while (progress.Length != 3 && !cts.IsCancellationRequested) {
syncContext.RunOnCurrentThread ();
Thread.Sleep (0);
}
Assert.AreEqual ("123", progress);
}
async void Go (SynchronizationContext ctx)
{
await Wait (ctx);
progress += "2";
}
async Task Wait (SynchronizationContext ctx)
{
await Task.Delay (10); // Force block suspend/return
ctx.Post (l => progress += "3", null);
progress += "1";
// Exiting same context - no need to post continuation
}
[Test]
public void CompletionOnDifferentCustomSynchronizationContext ()
{
progress = "";
var syncContext = new SingleThreadSynchronizationContext ();
SynchronizationContext.SetSynchronizationContext (syncContext);
syncContext.Post (delegate {
Go2 (syncContext);
}, null);
// Custom message loop
var cts = new CancellationTokenSource ();
cts.CancelAfter (5000);
while (progress.Length != 3 && !cts.IsCancellationRequested) {
syncContext.RunOnCurrentThread ();
Thread.Sleep (0);
}
Assert.AreEqual ("132", progress);
}
async void Go2 (SynchronizationContext ctx)
{
await Wait2 (ctx);
progress += "2";
}
async Task Wait2 (SynchronizationContext ctx)
{
await Task.Delay (10); // Force block suspend/return
ctx.Post (l => progress += "3", null);
progress += "1";
SynchronizationContext.SetSynchronizationContext (null);
}
}
}
#endif

View File

@ -0,0 +1,145 @@
//
// TaskAwaiterTest_T.cs
//
// Authors:
// Marek Safar <marek.safar@gmail.com>
//
// Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_4_5
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using System.Runtime.CompilerServices;
namespace MonoTests.System.Runtime.CompilerServices
{
[TestFixture]
public class TaskAwaiterTest_T
{
class MyContext : SynchronizationContext
{
public int PostCounter;
public ManualResetEvent mre = new ManualResetEvent (false);
public override void OperationStarted ()
{
base.OperationStarted ();
}
public override void OperationCompleted ()
{
base.OperationCompleted ();
}
public override void Post (SendOrPostCallback d, object state)
{
++PostCounter;
mre.Set ();
base.Post (d, state);
}
public override void Send (SendOrPostCallback d, object state)
{
base.Send (d, state);
}
}
Task<int> task;
[Test]
public void GetResultFaulted ()
{
TaskAwaiter<int> awaiter;
task = new Task<int> (() => { throw new ApplicationException (); });
awaiter = task.GetAwaiter ();
task.RunSynchronously (TaskScheduler.Current);
Assert.IsTrue (awaiter.IsCompleted);
try {
awaiter.GetResult ();
Assert.Fail ();
} catch (ApplicationException) {
}
}
[Test]
public void GetResultCanceled ()
{
TaskAwaiter<int> awaiter;
var token = new CancellationToken (true);
task = new Task<int> (() => 2, token);
awaiter = task.GetAwaiter ();
try {
awaiter.GetResult ();
Assert.Fail ();
} catch (TaskCanceledException) {
}
}
[Test]
public void ContextTest ()
{
TaskAwaiter awaiter;
var task = new Task (() => { throw new ApplicationException (); });
awaiter = task.GetAwaiter ();
task.RunSynchronously (TaskScheduler.Current);
Assert.IsTrue (awaiter.IsCompleted);
try {
awaiter.GetResult ();
Assert.Fail ();
} catch (ApplicationException) {
}
var context = new MyContext ();
var old = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext (context);
try {
var t = new Task (delegate { });
var a = t.GetAwaiter ();
a.OnCompleted (delegate { });
t.Start ();
Assert.IsTrue (t.Wait (5000), "#1");
} finally {
SynchronizationContext.SetSynchronizationContext (old);
}
Assert.IsTrue (context.mre.WaitOne (5000), "#2");
Assert.AreEqual (1, context.PostCounter, "#3");
}
}
}
#endif

View File

@ -0,0 +1,186 @@
//
// YieldAwaitebleTest.cs
//
// Authors:
// Marek Safar <marek.safar@gmail.com>
//
// Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_4_5
using System;
using System.Collections.Generic;
using NUnit.Framework;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Threading;
namespace MonoTests.System.Runtime.CompilerServices
{
[TestFixture]
public class YieldAwaitableTest
{
class MyScheduler : TaskScheduler
{
protected override IEnumerable<Task> GetScheduledTasks ()
{
throw new NotImplementedException ();
}
protected override void QueueTask (Task task)
{
TryExecuteTask (task);
}
protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
{
throw new NotImplementedException ();
}
}
class MyContext : SynchronizationContext
{
public int Started;
public int Completed;
public int PostCounter;
public int SendCounter;
public override void OperationStarted ()
{
++Started;
base.OperationStarted ();
}
public override void OperationCompleted ()
{
++Completed;
base.OperationCompleted ();
}
public override void Post (SendOrPostCallback d, object state)
{
++PostCounter;
base.Post (d, state);
}
public override void Send (SendOrPostCallback d, object state)
{
++SendCounter;
base.Send (d, state);
}
}
YieldAwaitable.YieldAwaiter a;
SynchronizationContext sc;
[SetUp]
public void Setup ()
{
sc = SynchronizationContext.Current;
a = new YieldAwaitable ().GetAwaiter ();
}
[TearDown]
public void TearDown ()
{
SynchronizationContext.SetSynchronizationContext (sc);
}
[Test]
public void IsCompleted ()
{
Assert.IsFalse (a.IsCompleted, "#1");
a.GetResult ();
Assert.IsFalse (a.IsCompleted, "#1");
}
[Test]
public void OnCompleted_1 ()
{
try {
a.OnCompleted (null);
Assert.Fail ("#1");
} catch (ArgumentException) {
}
}
[Test]
public void OnCompleted_2 ()
{
TaskScheduler scheduler = null;
SynchronizationContext.SetSynchronizationContext (null);
var mre = new ManualResetEvent (false);
a.OnCompleted (() => {
scheduler = TaskScheduler.Current;
mre.Set ();
});
Assert.IsTrue (mre.WaitOne (1000), "#1");
Assert.AreEqual (TaskScheduler.Current, scheduler, "#2");
}
[Test]
public void OnCompleted_3 ()
{
var scheduler = new MyScheduler ();
TaskScheduler ran_scheduler = null;
SynchronizationContext.SetSynchronizationContext (null);
var t = Task.Factory.StartNew (() => {
var mre = new ManualResetEvent (false);
a.OnCompleted (() => {
ran_scheduler = TaskScheduler.Current;
mre.Set ();
});
mre.WaitOne (1000);
}, CancellationToken.None, TaskCreationOptions.None, scheduler);
Assert.IsTrue (t.Wait (1000), "#1");
Assert.AreEqual (scheduler, ran_scheduler, "#2");
}
[Test]
public void OnCompleted_4 ()
{
SynchronizationContext context_ran = null;
var mre = new ManualResetEvent (false);
var context = new MyContext ();
SynchronizationContext.SetSynchronizationContext (context);
a.OnCompleted (() => {
context_ran = SynchronizationContext.Current;
mre.Set ();
});
Assert.IsTrue (mre.WaitOne (1000), "#1");
Assert.IsNull (context_ran, "#2");
}
}
}
#endif