RuntimeHelpersTest.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // RuntimeHelpersTest.cs - NUnit Test Cases for the System.Runtime.CompilerServices.RuntimeHelpers class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. using System;
  8. using System.Runtime.CompilerServices;
  9. using NUnit.Framework;
  10. namespace MonoTests.System.Runtime.CompilerServices {
  11. public class RuntimeHelpersTest : TestCase {
  12. struct FooStruct {
  13. public int i;
  14. public string j;
  15. }
  16. class FooClass {
  17. public static int counter = 0;
  18. static FooClass () {
  19. counter = counter + 1;
  20. }
  21. }
  22. public void TestOffsetToStringData ()
  23. {
  24. AssertEquals ("OffsetToStringData is not constant",
  25. RuntimeHelpers.OffsetToStringData,
  26. RuntimeHelpers.OffsetToStringData);
  27. }
  28. public void TestGetObjectValue ()
  29. {
  30. FooStruct s1;
  31. FooStruct s2;
  32. // Test null
  33. AssertEquals ("",
  34. RuntimeHelpers.GetObjectValue (null),
  35. null);
  36. // Test non-valuetype
  37. AssertEquals ("",
  38. RuntimeHelpers.GetObjectValue (this),
  39. this);
  40. // Test valuetype
  41. s1.i = 42;
  42. s1.j = "FOO";
  43. s2 = (FooStruct)RuntimeHelpers.GetObjectValue(s1);
  44. s1.i = 43;
  45. s1.j = "BAR";
  46. AssertEquals ("", s2.i, 42);
  47. AssertEquals ("", s2.j, "FOO");
  48. }
  49. public void TestRunClassConstructor ()
  50. {
  51. RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
  52. AssertEquals ("", FooClass.counter, 1);
  53. // Each static constructor should only be run once
  54. RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
  55. AssertEquals ("", FooClass.counter, 1);
  56. }
  57. }
  58. }