/ICSharpCode.Decompiler/Tests/ValueTypes.cs

http://github.com/icsharpcode/ILSpy · C# · 188 lines · 139 code · 31 blank · 18 comment · 5 complexity · 5348510929248c613e20912d2d018caa MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. public static class ValueTypes
  20. {
  21. public struct S
  22. {
  23. public int Field;
  24. public S(int field)
  25. {
  26. this.Field = field;
  27. }
  28. public void SetField()
  29. {
  30. this.Field = 5;
  31. }
  32. public void MethodCalls()
  33. {
  34. this.SetField();
  35. ValueTypes.S.Test(this);
  36. ValueTypes.S.Test(ref this);
  37. }
  38. private static void Test(ValueTypes.S byVal)
  39. {
  40. }
  41. private static void Test(ref ValueTypes.S byRef)
  42. {
  43. }
  44. }
  45. private static readonly ValueTypes.S ReadOnlyS = default(ValueTypes.S);
  46. private static ValueTypes.S MutableS = default(ValueTypes.S);
  47. private static volatile int VolatileInt;
  48. public static void CallMethodViaField()
  49. {
  50. ValueTypes.ReadOnlyS.SetField();
  51. ValueTypes.MutableS.SetField();
  52. ValueTypes.S mutableS = ValueTypes.MutableS;
  53. mutableS.SetField();
  54. }
  55. public static ValueTypes.S InitObj1()
  56. {
  57. ValueTypes.S result = default(ValueTypes.S);
  58. ValueTypes.MakeArray();
  59. return result;
  60. }
  61. public static ValueTypes.S InitObj2()
  62. {
  63. return default(ValueTypes.S);
  64. }
  65. public static void InitObj3(out ValueTypes.S p)
  66. {
  67. p = default(ValueTypes.S);
  68. }
  69. public static ValueTypes.S CallValueTypeCtor1()
  70. {
  71. return new ValueTypes.S(10);
  72. }
  73. public static ValueTypes.S CallValueTypeCtor2()
  74. {
  75. ValueTypes.S result = new ValueTypes.S(10);
  76. return result;
  77. }
  78. public static ValueTypes.S Copy1(ValueTypes.S p)
  79. {
  80. return p;
  81. }
  82. public static ValueTypes.S Copy2(ref ValueTypes.S p)
  83. {
  84. return p;
  85. }
  86. public static void Copy3(ValueTypes.S p, out ValueTypes.S o)
  87. {
  88. o = p;
  89. }
  90. public static void Copy4(ref ValueTypes.S p, out ValueTypes.S o)
  91. {
  92. o = p;
  93. }
  94. public static void Copy4b(ref ValueTypes.S p, out ValueTypes.S o)
  95. {
  96. // test passing through by-ref arguments
  97. ValueTypes.Copy4(ref p, out o);
  98. }
  99. public static void Issue56(int i, out string str)
  100. {
  101. str = "qq";
  102. str += i.ToString();
  103. }
  104. public static void CopyAroundAndModifyField(ValueTypes.S s)
  105. {
  106. ValueTypes.S s2 = s;
  107. s2.Field += 10;
  108. s = s2;
  109. }
  110. private static int[] MakeArray()
  111. {
  112. return null;
  113. }
  114. public static void IncrementArrayLocation()
  115. {
  116. ValueTypes.MakeArray()[Environment.TickCount]++;
  117. }
  118. public static bool Is(object obj)
  119. {
  120. return obj is ValueTypes.S;
  121. }
  122. public static bool IsNullable(object obj)
  123. {
  124. return obj is ValueTypes.S?;
  125. }
  126. public static ValueTypes.S? As(object obj)
  127. {
  128. return obj as ValueTypes.S?;
  129. }
  130. public static ValueTypes.S OnlyChangeTheCopy(ValueTypes.S p)
  131. {
  132. ValueTypes.S s = p;
  133. s.SetField();
  134. return p;
  135. }
  136. public static void UseRefBoolInCondition(ref bool x)
  137. {
  138. if (x)
  139. {
  140. Console.WriteLine("true");
  141. }
  142. }
  143. public static void CompareNotEqual0IsReallyNotEqual(IComparable<int> a)
  144. {
  145. if (a.CompareTo(0) != 0)
  146. {
  147. Console.WriteLine("true");
  148. }
  149. }
  150. public static void CompareEqual0IsReallyEqual(IComparable<int> a)
  151. {
  152. if (a.CompareTo(0) == 0)
  153. {
  154. Console.WriteLine("true");
  155. }
  156. }
  157. }