Skip to content

Instantly share code, notes, and snippets.

@schani
Created August 20, 2010 15:03
Show Gist options
  • Select an option

  • Save schani/540501 to your computer and use it in GitHub Desktop.

Select an option

Save schani/540501 to your computer and use it in GitHub Desktop.
using NUnit.Framework;
using System.Threading;
using System;
public class Test {
public static void CompareExchange_Generic ()
{
object a = null;
Assert.IsNull (Interlocked.CompareExchange<object> (ref a, a, a), "null,null,null");
/*
object b = new object ();
Assert.IsNull (Interlocked.CompareExchange<object> (ref a, a, b), "null,non-null,non-null");
Assert.IsNull (Interlocked.CompareExchange<object> (ref a, b, a), "null,non-null,null");
Assert.AreSame (b, Interlocked.CompareExchange<object> (ref a, b, b), "null,null,non-null");
Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, a, a), "non-null,null,null");
Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, a, b), "non-null,null,non-null");
Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, b, a), "non-null,non-null,null");
Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, b, b), "non-null,non-null,non-null");
*/
}
public static object Null2 (int i) {
if (i == 0)
return null;
return Null2 (i - 1);
}
/* force non-inline */
public static object Null () {
return Null2 (123);
}
public static bool Correctness () {
object a = "abc";
object b = a;
object c = Null ();
Interlocked.CompareExchange<object> (ref a, b, c);
if (c != null) {
Console.WriteLine ("error");
return false;
}
return true;
}
public static object Ident (object x) {
return x;
}
public static void Dummy (object a, object b, object c) {
}
public static object Crash () {
object a = "abc";
object b = Ident (a);
object c = Null ();
object d = Interlocked.CompareExchange<object> (ref a, b, c);
a = Interlocked.CompareExchange<object> (ref b, c, d);
b = Interlocked.CompareExchange<object> (ref c, d, a);
c = Interlocked.CompareExchange<object> (ref d, a, b);
return c;
}
public static int Main () {
CompareExchange_Generic ();
//if (!Correctness ())
//return 1;
//Crash ();
/*
object a = null;
Interlocked.CompareExchange<object> (ref a, a, a);
object b = new object ();
Interlocked.CompareExchange<object> (ref a, a, b);
Interlocked.CompareExchange<object> (ref a, b, a);
Interlocked.CompareExchange<object> (ref a, b, b);
Interlocked.CompareExchange<object> (ref b, a, a);
Interlocked.CompareExchange<object> (ref b, a, b);
Interlocked.CompareExchange<object> (ref b, b, a);
Interlocked.CompareExchange<object> (ref b, b, b);
*/
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment