1 2 import core.stdc.stdio; 3 4 struct Foo 5 { 6 uint array[2]; 7 opApplyFoo8 int opApply(int delegate(ref uint) dg) 9 { 10 int result; 11 for (int i = 0; i < array.length; i++) 12 { 13 result = dg(array[i]); 14 if (result) 15 break; 16 } 17 return result; 18 } 19 } 20 21 22 /**************************************************/ 23 test1()24void test1() 25 { 26 Foo a; 27 int i; 28 29 a.array[0] = 73; 30 a.array[1] = 82; 31 32 foreach (uint u; a) 33 { 34 i++; 35 u++; 36 } 37 assert(i == 2); 38 assert(a.array[0] == 73); 39 assert(a.array[1] == 82); 40 } 41 42 /**************************************************/ 43 test2()44void test2() 45 { 46 Foo a; 47 int i; 48 49 a.array[0] = 73; 50 a.array[1] = 82; 51 52 foreach (ref uint u; a) 53 { 54 i++; 55 u++; 56 } 57 assert(i == 2); 58 assert(a.array[0] == 74); 59 assert(a.array[1] == 83); 60 } 61 62 /**************************************************/ 63 test3()64void test3() 65 { 66 Foo a; 67 int i; 68 69 a.array[0] = 73; 70 a.array[1] = 82; 71 72 foreach (ref uint u; a) 73 { 74 i++; 75 if (i) 76 break; 77 u++; 78 } 79 assert(i == 1); 80 assert(a.array[0] == 73); 81 assert(a.array[1] == 82); 82 } 83 84 /**************************************************/ 85 test4()86void test4() 87 { 88 Foo a; 89 int i; 90 91 a.array[0] = 73; 92 a.array[1] = 82; 93 94 foreach (ref uint u; a) 95 { 96 i++; 97 if (i == 1) 98 continue; 99 u++; 100 } 101 assert(i == 2); 102 assert(a.array[0] == 73 && a.array[1] == 83); 103 } 104 105 /**************************************************/ 106 test5()107void test5() 108 { 109 Foo a; 110 int i; 111 112 a.array[0] = 73; 113 a.array[1] = 82; 114 115 Loop: 116 while (1) 117 { 118 foreach (ref uint u; a) 119 { 120 i++; 121 if (i) 122 break Loop; 123 u++; 124 } 125 } 126 assert(i == 1); 127 assert(a.array[0] == 73); 128 assert(a.array[1] == 82); 129 } 130 131 /**************************************************/ 132 test6()133void test6() 134 { 135 Foo a; 136 int i; 137 138 a.array[0] = 73; 139 a.array[1] = 82; 140 141 Loop: 142 while (1) 143 { 144 foreach (ref uint u; a) 145 { 146 i++; 147 if (i == 1) 148 continue Loop; 149 u++; 150 } 151 break; 152 } 153 assert(i == 3); 154 assert(a.array[0] == 74); 155 assert(a.array[1] == 83); 156 } 157 158 /**************************************************/ 159 test7()160void test7() 161 { 162 Foo a; 163 int i; 164 165 a.array[0] = 73; 166 a.array[1] = 82; 167 168 foreach (ref uint u; a) 169 { 170 i++; 171 if (i) 172 goto Label; 173 u++; 174 } 175 assert(0); 176 Label: 177 assert(i == 1); 178 assert(a.array[0] == 73); 179 assert(a.array[1] == 82); 180 } 181 182 /**************************************************/ 183 test8_x(Foo a)184void test8_x(Foo a) 185 { 186 int i; 187 foreach (ref uint u; a) 188 { 189 i++; 190 if (i) 191 return; 192 u++; 193 } 194 } 195 test8()196void test8() 197 { 198 Foo a; 199 int i; 200 201 a.array[0] = 73; 202 a.array[1] = 82; 203 204 test8_x(a); 205 assert(i == 0); 206 assert(a.array[0] == 73); 207 assert(a.array[1] == 82); 208 } 209 210 /**************************************************/ 211 test9_x(Foo a)212int test9_x(Foo a) 213 { 214 int i; 215 foreach (ref uint u; a) 216 { 217 i++; 218 if (i) 219 return 67; 220 u++; 221 } 222 return 23; 223 } 224 test9()225void test9() 226 { 227 Foo a; 228 int i; 229 230 a.array[0] = 73; 231 a.array[1] = 82; 232 233 i = test9_x(a); 234 assert(i == 67); 235 assert(a.array[0] == 73); 236 assert(a.array[1] == 82); 237 } 238 239 /**************************************************/ 240 test10_x(Foo a)241int test10_x(Foo a) 242 { 243 int i; 244 foreach (ref uint u; a) 245 { 246 i++; 247 if (i) 248 return i; 249 u++; 250 } 251 return 23; 252 } 253 test10()254void test10() 255 { 256 Foo a; 257 int i; 258 259 a.array[0] = 73; 260 a.array[1] = 82; 261 262 i = test10_x(a); 263 assert(i == 1); 264 assert(a.array[0] == 73); 265 assert(a.array[1] == 82); 266 } 267 268 /**************************************************/ 269 main()270int main() 271 { 272 test1(); 273 test2(); 274 test3(); 275 test4(); 276 test5(); 277 test6(); 278 test7(); 279 test8(); 280 test9(); 281 test10(); 282 283 printf("Success\n"); 284 return 0; 285 } 286