PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/test/runnable/foreach3.d

https://github.com/fugalh/dmd
D | 285 lines | 220 code | 54 blank | 11 comment | 45 complexity | df39c6588569e9632744150028dbccbf MD5 | raw file
Possible License(s): AGPL-1.0
  1. import std.c.stdio;
  2. struct Foo
  3. {
  4. uint array[2];
  5. int opApply(int delegate(ref uint) dg)
  6. { int result;
  7. for (int i = 0; i < array.length; i++)
  8. {
  9. result = dg(array[i]);
  10. if (result)
  11. break;
  12. }
  13. return result;
  14. }
  15. }
  16. /**************************************************/
  17. void test1()
  18. {
  19. Foo a;
  20. int i;
  21. a.array[0] = 73;
  22. a.array[1] = 82;
  23. foreach (uint u; a)
  24. {
  25. i++;
  26. u++;
  27. }
  28. assert(i == 2);
  29. assert(a.array[0] == 73);
  30. assert(a.array[1] == 82);
  31. }
  32. /**************************************************/
  33. void test2()
  34. {
  35. Foo a;
  36. int i;
  37. a.array[0] = 73;
  38. a.array[1] = 82;
  39. foreach (ref uint u; a)
  40. {
  41. i++;
  42. u++;
  43. }
  44. assert(i == 2);
  45. assert(a.array[0] == 74);
  46. assert(a.array[1] == 83);
  47. }
  48. /**************************************************/
  49. void test3()
  50. {
  51. Foo a;
  52. int i;
  53. a.array[0] = 73;
  54. a.array[1] = 82;
  55. foreach (ref uint u; a)
  56. {
  57. i++;
  58. if (i)
  59. break;
  60. u++;
  61. }
  62. assert(i == 1);
  63. assert(a.array[0] == 73);
  64. assert(a.array[1] == 82);
  65. }
  66. /**************************************************/
  67. void test4()
  68. {
  69. Foo a;
  70. int i;
  71. a.array[0] = 73;
  72. a.array[1] = 82;
  73. foreach (ref uint u; a)
  74. {
  75. i++;
  76. if (i == 1)
  77. continue;
  78. u++;
  79. }
  80. assert(i == 2);
  81. assert(a.array[0] == 73 && a.array[1] == 83);
  82. }
  83. /**************************************************/
  84. void test5()
  85. {
  86. Foo a;
  87. int i;
  88. a.array[0] = 73;
  89. a.array[1] = 82;
  90. Loop:
  91. while (1)
  92. {
  93. foreach (ref uint u; a)
  94. {
  95. i++;
  96. if (i)
  97. break Loop;
  98. u++;
  99. }
  100. }
  101. assert(i == 1);
  102. assert(a.array[0] == 73);
  103. assert(a.array[1] == 82);
  104. }
  105. /**************************************************/
  106. void test6()
  107. {
  108. Foo a;
  109. int i;
  110. a.array[0] = 73;
  111. a.array[1] = 82;
  112. Loop:
  113. while (1)
  114. {
  115. foreach (ref uint u; a)
  116. {
  117. i++;
  118. if (i == 1)
  119. continue Loop;
  120. u++;
  121. }
  122. break;
  123. }
  124. assert(i == 3);
  125. assert(a.array[0] == 74);
  126. assert(a.array[1] == 83);
  127. }
  128. /**************************************************/
  129. void test7()
  130. {
  131. Foo a;
  132. int i;
  133. a.array[0] = 73;
  134. a.array[1] = 82;
  135. foreach (ref uint u; a)
  136. {
  137. i++;
  138. if (i)
  139. goto Label;
  140. u++;
  141. }
  142. assert(0);
  143. Label:
  144. assert(i == 1);
  145. assert(a.array[0] == 73);
  146. assert(a.array[1] == 82);
  147. }
  148. /**************************************************/
  149. void test8_x(Foo a)
  150. { int i;
  151. foreach (ref uint u; a)
  152. {
  153. i++;
  154. if (i)
  155. return;
  156. u++;
  157. }
  158. }
  159. void test8()
  160. {
  161. Foo a;
  162. int i;
  163. a.array[0] = 73;
  164. a.array[1] = 82;
  165. test8_x(a);
  166. assert(i == 0);
  167. assert(a.array[0] == 73);
  168. assert(a.array[1] == 82);
  169. }
  170. /**************************************************/
  171. int test9_x(Foo a)
  172. { int i;
  173. foreach (ref uint u; a)
  174. {
  175. i++;
  176. if (i)
  177. return 67;
  178. u++;
  179. }
  180. return 23;
  181. }
  182. void test9()
  183. {
  184. Foo a;
  185. int i;
  186. a.array[0] = 73;
  187. a.array[1] = 82;
  188. i = test9_x(a);
  189. assert(i == 67);
  190. assert(a.array[0] == 73);
  191. assert(a.array[1] == 82);
  192. }
  193. /**************************************************/
  194. int test10_x(Foo a)
  195. { int i;
  196. foreach (ref uint u; a)
  197. {
  198. i++;
  199. if (i)
  200. return i;
  201. u++;
  202. }
  203. return 23;
  204. }
  205. void test10()
  206. {
  207. Foo a;
  208. int i;
  209. a.array[0] = 73;
  210. a.array[1] = 82;
  211. i = test10_x(a);
  212. assert(i == 1);
  213. assert(a.array[0] == 73);
  214. assert(a.array[1] == 82);
  215. }
  216. /**************************************************/
  217. int main()
  218. {
  219. test1();
  220. test2();
  221. test3();
  222. test4();
  223. test5();
  224. test6();
  225. test7();
  226. test8();
  227. test9();
  228. test10();
  229. printf("Success\n");
  230. return 0;
  231. }