You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
5.6 KiB

  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. using System.Collections;
  20. using System.Collections.Generic;
  21. namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
  22. {
  23. public class Loops
  24. {
  25. public void ForEach(IEnumerable<string> enumerable)
  26. {
  27. foreach (string item in enumerable) {
  28. item.ToLower();
  29. }
  30. }
  31. public void ForEachOverList(List<string> list)
  32. {
  33. // List has a struct as enumerator, so produces quite different IL than foreach over the IEnumerable interface
  34. foreach (string item in list) {
  35. item.ToLower();
  36. }
  37. }
  38. public void ForEachOverNonGenericEnumerable(IEnumerable enumerable)
  39. {
  40. foreach (object item in enumerable) {
  41. item.ToString();
  42. }
  43. }
  44. public void ForEachOverNonGenericEnumerableWithAutomaticCastValueType(IEnumerable enumerable)
  45. {
  46. foreach (int item in enumerable) {
  47. item.ToString();
  48. }
  49. }
  50. public void ForEachOverNonGenericEnumerableWithAutomaticCastRefType(IEnumerable enumerable)
  51. {
  52. foreach (string item in enumerable) {
  53. Console.WriteLine(item);
  54. }
  55. }
  56. // public void ForEachOverArray(string[] array)
  57. // {
  58. // foreach (string text in array)
  59. // {
  60. // text.ToLower();
  61. // }
  62. // }
  63. public void ForOverArray(string[] array)
  64. {
  65. for (int i = 0; i < array.Length; i++) {
  66. array[i].ToLower();
  67. }
  68. }
  69. public void NestedLoops()
  70. {
  71. for (int i = 0; i < 10; i++) {
  72. if (i % 2 == 0) {
  73. for (int j = 0; j < 5; j++) {
  74. Console.WriteLine("Y");
  75. }
  76. } else {
  77. Console.WriteLine("X");
  78. }
  79. }
  80. }
  81. //public int MultipleExits()
  82. //{
  83. // int i = 0;
  84. // while (true) {
  85. // if (i % 4 == 0) { return 4; }
  86. // if (i % 7 == 0) { break; }
  87. // if (i % 9 == 0) { return 5; }
  88. // if (i % 11 == 0) { break; }
  89. // i++;
  90. // }
  91. // i = int.MinValue;
  92. // return i;
  93. //}
  94. //public int InterestingLoop()
  95. //{
  96. // int i = 0;
  97. // if (i % 11 == 0) {
  98. // while (true) {
  99. // if (i % 4 == 0) {
  100. // if (i % 7 == 0) {
  101. // if (i % 11 == 0) {
  102. // continue; // use a continue here to prevent moving the if (i%7) outside the loop
  103. // }
  104. // Console.WriteLine("7");
  105. // } else {
  106. // // this block is not part of the natural loop
  107. // Console.WriteLine("!7");
  108. // }
  109. // break;
  110. // }
  111. // i++;
  112. // }
  113. // // This instruction is still dominated by the loop header
  114. // i = int.MinValue;
  115. // }
  116. // return i;
  117. //}
  118. private bool Condition(string arg)
  119. {
  120. Console.WriteLine("Condition: " + arg);
  121. return false;
  122. }
  123. public void WhileLoop()
  124. {
  125. Console.WriteLine("Initial");
  126. if (this.Condition("if")) {
  127. while (this.Condition("while")) {
  128. Console.WriteLine("Loop Body");
  129. if (this.Condition("test")) {
  130. if (this.Condition("continue")) {
  131. continue;
  132. }
  133. if (!this.Condition("break"))
  134. break;
  135. }
  136. Console.WriteLine("End of loop body");
  137. }
  138. Console.WriteLine("After loop");
  139. }
  140. Console.WriteLine("End of method");
  141. }
  142. //public void WhileWithGoto()
  143. //{
  144. // while (this.Condition("Main Loop")) {
  145. // if (!this.Condition("Condition"))
  146. // goto block2;
  147. // block1:
  148. // Console.WriteLine("Block1");
  149. // if (this.Condition("Condition2"))
  150. // continue;
  151. // block2:
  152. // Console.WriteLine("Block2");
  153. // goto block1;
  154. // }
  155. //}
  156. //public void DoWhileLoop()
  157. //{
  158. // Console.WriteLine("Initial");
  159. // if (this.Condition("if")) {
  160. // do {
  161. // Console.WriteLine("Loop Body");
  162. // if (this.Condition("test")) {
  163. // if (this.Condition("continue")) {
  164. // continue;
  165. // }
  166. // if (!this.Condition("break"))
  167. // break;
  168. // }
  169. // Console.WriteLine("End of loop body");
  170. // } while (this.Condition("while"));
  171. // Console.WriteLine("After loop");
  172. // }
  173. // Console.WriteLine("End of method");
  174. //}
  175. public void ForLoop()
  176. {
  177. Console.WriteLine("Initial");
  178. if (this.Condition("if")) {
  179. for (int i = 0; this.Condition("for"); i++) {
  180. Console.WriteLine("Loop Body");
  181. if (this.Condition("test")) {
  182. if (this.Condition("continue")) {
  183. continue;
  184. }
  185. if (!this.Condition("not-break"))
  186. break;
  187. }
  188. Console.WriteLine("End of loop body");
  189. }
  190. Console.WriteLine("After loop");
  191. }
  192. Console.WriteLine("End of method");
  193. }
  194. public static void ForeachExceptForContinuedUse(IEnumerable<int> inputs)
  195. {
  196. Console.WriteLine("ForeachExceptForContinuedUse");
  197. int num = 0;
  198. using (IEnumerator<int> enumerator = inputs.GetEnumerator()) {
  199. while (enumerator.MoveNext()) {
  200. num = enumerator.Current;
  201. Console.WriteLine(num);
  202. }
  203. }
  204. Console.WriteLine("Last: " + num);
  205. }
  206. }
  207. }