| 1 |
import core.stdc.stdio; |
| 2 |
|
| 3 |
alias bool bit; |
| 4 |
|
| 5 |
/************************************************/ |
| 6 |
|
| 7 |
class Foo |
| 8 |
{ |
| 9 |
uint[2] array; |
| 10 |
|
| 11 |
int opApply(int delegate(ref uint) dg) |
| 12 |
{ |
| 13 |
int result; |
| 14 |
for (int i = 0; i < array.length; i++) |
| 15 |
{ |
| 16 |
result = dg(array[i]); |
| 17 |
if (result) |
| 18 |
break; |
| 19 |
} |
| 20 |
return result; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
/**************************************************/ |
| 26 |
|
| 27 |
void test1() |
| 28 |
{ |
| 29 |
Foo a = new Foo(); |
| 30 |
int i; |
| 31 |
|
| 32 |
a.array[0] = 73; |
| 33 |
a.array[1] = 82; |
| 34 |
|
| 35 |
foreach (uint u; a) |
| 36 |
{ |
| 37 |
i++; |
| 38 |
u++; |
| 39 |
} |
| 40 |
assert(i == 2); |
| 41 |
assert(a.array[0] == 73); |
| 42 |
assert(a.array[1] == 82); |
| 43 |
} |
| 44 |
|
| 45 |
/**************************************************/ |
| 46 |
|
| 47 |
void test2() |
| 48 |
{ |
| 49 |
Foo a = new Foo(); |
| 50 |
int i; |
| 51 |
|
| 52 |
a.array[0] = 73; |
| 53 |
a.array[1] = 82; |
| 54 |
|
| 55 |
foreach (ref uint u; a) |
| 56 |
{ |
| 57 |
i++; |
| 58 |
u++; |
| 59 |
} |
| 60 |
assert(i == 2); |
| 61 |
assert(a.array[0] == 74); |
| 62 |
assert(a.array[1] == 83); |
| 63 |
} |
| 64 |
|
| 65 |
/**************************************************/ |
| 66 |
|
| 67 |
void test3() |
| 68 |
{ |
| 69 |
Foo a = new Foo(); |
| 70 |
int i; |
| 71 |
|
| 72 |
a.array[0] = 73; |
| 73 |
a.array[1] = 82; |
| 74 |
|
| 75 |
foreach (ref uint u; a) |
| 76 |
{ |
| 77 |
i++; |
| 78 |
if (i) |
| 79 |
break; |
| 80 |
u++; |
| 81 |
} |
| 82 |
assert(i == 1); |
| 83 |
assert(a.array[0] == 73); |
| 84 |
assert(a.array[1] == 82); |
| 85 |
} |
| 86 |
|
| 87 |
/**************************************************/ |
| 88 |
|
| 89 |
void test4() |
| 90 |
{ |
| 91 |
Foo a = new Foo(); |
| 92 |
int i; |
| 93 |
|
| 94 |
a.array[0] = 73; |
| 95 |
a.array[1] = 82; |
| 96 |
|
| 97 |
foreach (ref uint u; a) |
| 98 |
{ |
| 99 |
i++; |
| 100 |
if (i == 1) |
| 101 |
continue; |
| 102 |
u++; |
| 103 |
} |
| 104 |
assert(i == 2); |
| 105 |
assert(a.array[0] == 73 && a.array[1] == 83); |
| 106 |
} |
| 107 |
|
| 108 |
/**************************************************/ |
| 109 |
|
| 110 |
void test5() |
| 111 |
{ |
| 112 |
Foo a = new Foo(); |
| 113 |
int i; |
| 114 |
|
| 115 |
a.array[0] = 73; |
| 116 |
a.array[1] = 82; |
| 117 |
|
| 118 |
Loop: |
| 119 |
while (1) |
| 120 |
{ |
| 121 |
foreach (ref uint u; a) |
| 122 |
{ |
| 123 |
i++; |
| 124 |
if (i) |
| 125 |
break Loop; |
| 126 |
u++; |
| 127 |
} |
| 128 |
} |
| 129 |
assert(i == 1); |
| 130 |
assert(a.array[0] == 73); |
| 131 |
assert(a.array[1] == 82); |
| 132 |
} |
| 133 |
|
| 134 |
/**************************************************/ |
| 135 |
|
| 136 |
void test6() |
| 137 |
{ |
| 138 |
Foo a = new Foo(); |
| 139 |
int i; |
| 140 |
|
| 141 |
a.array[0] = 73; |
| 142 |
a.array[1] = 82; |
| 143 |
|
| 144 |
Loop: |
| 145 |
while (1) |
| 146 |
{ |
| 147 |
foreach (ref uint u; a) |
| 148 |
{ |
| 149 |
i++; |
| 150 |
if (i == 1) |
| 151 |
continue Loop; |
| 152 |
u++; |
| 153 |
} |
| 154 |
break; |
| 155 |
} |
| 156 |
assert(i == 3); |
| 157 |
assert(a.array[0] == 74); |
| 158 |
assert(a.array[1] == 83); |
| 159 |
} |
| 160 |
|
| 161 |
/**************************************************/ |
| 162 |
|
| 163 |
void test7() |
| 164 |
{ |
| 165 |
Foo a = new Foo(); |
| 166 |
int i; |
| 167 |
|
| 168 |
a.array[0] = 73; |
| 169 |
a.array[1] = 82; |
| 170 |
|
| 171 |
foreach (ref uint u; a) |
| 172 |
{ |
| 173 |
i++; |
| 174 |
if (i) |
| 175 |
goto Label; |
| 176 |
u++; |
| 177 |
} |
| 178 |
assert(0); |
| 179 |
Label: |
| 180 |
assert(i == 1); |
| 181 |
assert(a.array[0] == 73); |
| 182 |
assert(a.array[1] == 82); |
| 183 |
} |
| 184 |
|
| 185 |
/**************************************************/ |
| 186 |
|
| 187 |
void test8_x(Foo a) |
| 188 |
{ |
| 189 |
int i; |
| 190 |
foreach (ref uint u; a) |
| 191 |
{ |
| 192 |
i++; |
| 193 |
if (i) |
| 194 |
return; |
| 195 |
u++; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
void test8() |
| 200 |
{ |
| 201 |
Foo a = new Foo(); |
| 202 |
int i; |
| 203 |
|
| 204 |
a.array[0] = 73; |
| 205 |
a.array[1] = 82; |
| 206 |
|
| 207 |
test8_x(a); |
| 208 |
assert(i == 0); |
| 209 |
assert(a.array[0] == 73); |
| 210 |
assert(a.array[1] == 82); |
| 211 |
} |
| 212 |
|
| 213 |
/**************************************************/ |
| 214 |
|
| 215 |
int test9_x(Foo a) |
| 216 |
{ |
| 217 |
int i; |
| 218 |
foreach (ref uint u; a) |
| 219 |
{ |
| 220 |
i++; |
| 221 |
if (i) |
| 222 |
return 67; |
| 223 |
u++; |
| 224 |
} |
| 225 |
return 23; |
| 226 |
} |
| 227 |
|
| 228 |
void test9() |
| 229 |
{ |
| 230 |
Foo a = new Foo(); |
| 231 |
int i; |
| 232 |
|
| 233 |
a.array[0] = 73; |
| 234 |
a.array[1] = 82; |
| 235 |
|
| 236 |
i = test9_x(a); |
| 237 |
assert(i == 67); |
| 238 |
assert(a.array[0] == 73); |
| 239 |
assert(a.array[1] == 82); |
| 240 |
} |
| 241 |
|
| 242 |
/**************************************************/ |
| 243 |
|
| 244 |
int test10_x(Foo a) |
| 245 |
{ |
| 246 |
int i; |
| 247 |
foreach (ref uint u; a) |
| 248 |
{ |
| 249 |
i++; |
| 250 |
if (i) |
| 251 |
return i; |
| 252 |
u++; |
| 253 |
} |
| 254 |
return 23; |
| 255 |
} |
| 256 |
|
| 257 |
void test10() |
| 258 |
{ |
| 259 |
Foo a = new Foo(); |
| 260 |
int i; |
| 261 |
|
| 262 |
a.array[0] = 73; |
| 263 |
a.array[1] = 82; |
| 264 |
|
| 265 |
i = test10_x(a); |
| 266 |
assert(i == 1); |
| 267 |
assert(a.array[0] == 73); |
| 268 |
assert(a.array[1] == 82); |
| 269 |
} |
| 270 |
|
| 271 |
/**************************************************/ |
| 272 |
/+ |
| 273 |
the expected output: |
| 274 |
|
| 275 |
1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 |
| 276 |
1 |
| 277 |
1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1 0 |
| 278 |
1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1 0 |
| 279 |
1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1 0 |
| 280 |
+/ |
| 281 |
|
| 282 |
void test11() |
| 283 |
{ |
| 284 |
bit[25] data; |
| 285 |
int j; |
| 286 |
|
| 287 |
for (int i = 0; i < 25; i += 5) { |
| 288 |
data[i+0] = data[i+1] = true; |
| 289 |
} |
| 290 |
|
| 291 |
for (int i = 0; i < 25; i++) { |
| 292 |
printf("%d ", data[i]); |
| 293 |
if ((i % 5) < 2) |
| 294 |
assert(data[i] == true); |
| 295 |
else |
| 296 |
assert(data[i] == false); |
| 297 |
} |
| 298 |
|
| 299 |
printf("\n%d\n", data[22] = true); |
| 300 |
j = data[22] = true; |
| 301 |
assert(j == true); |
| 302 |
|
| 303 |
for (int i = 0; i < 25; i += 5) { |
| 304 |
data[i+1] = data[i+3] = true; |
| 305 |
j = i % 5; |
| 306 |
if (j == 0 || j == 1 || j == 3) |
| 307 |
assert(data[i] == true); |
| 308 |
else |
| 309 |
assert(data[i] == false); |
| 310 |
} |
| 311 |
|
| 312 |
for (int i = 0; i < 25; i++) { |
| 313 |
printf("%d ", data[i]); |
| 314 |
} |
| 315 |
|
| 316 |
printf("\n"); |
| 317 |
|
| 318 |
int k; |
| 319 |
foreach (bit b; data) { |
| 320 |
printf("%d ", b); |
| 321 |
j = k % 5; |
| 322 |
if (j == 0 || j == 1 || j == 3 || k == 22) |
| 323 |
assert(data[k] == true); |
| 324 |
else |
| 325 |
assert(data[k] == false); |
| 326 |
k++; |
| 327 |
} |
| 328 |
printf("\n"); |
| 329 |
|
| 330 |
foreach (int l, bit b; data) { |
| 331 |
printf("%d ", b); |
| 332 |
j = l % 5; |
| 333 |
if (j == 0 || j == 1 || j == 3 || l == 22) |
| 334 |
assert(data[l] == true); |
| 335 |
else |
| 336 |
assert(data[l] == false); |
| 337 |
} |
| 338 |
printf("\n"); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
/**************************************************/ |
| 343 |
|
| 344 |
void test12() |
| 345 |
{ |
| 346 |
int j; |
| 347 |
|
| 348 |
j = 0; |
| 349 |
foreach (dchar d; "hello") |
| 350 |
{ |
| 351 |
printf("d = x%x\n", d); |
| 352 |
if (j == 0) assert(d == 'h'); |
| 353 |
if (j == 1) assert(d == 'e'); |
| 354 |
if (j == 2) assert(d == 'l'); |
| 355 |
if (j == 3) assert(d == 'l'); |
| 356 |
if (j == 4) assert(d == 'o'); |
| 357 |
j++; |
| 358 |
} |
| 359 |
assert(j == 5); |
| 360 |
|
| 361 |
j = 0; |
| 362 |
foreach (size_t i, dchar d; "hello") |
| 363 |
{ |
| 364 |
printf("i = %d, d = x%x\n", cast(int)i, d); |
| 365 |
if (j == 0) assert(d == 'h'); |
| 366 |
if (j == 1) assert(d == 'e'); |
| 367 |
if (j == 2) assert(d == 'l'); |
| 368 |
if (j == 3) assert(d == 'l'); |
| 369 |
if (j == 4) assert(d == 'o'); |
| 370 |
assert(i == j); |
| 371 |
j++; |
| 372 |
} |
| 373 |
assert(j == 5); |
| 374 |
} |
| 375 |
|
| 376 |
/**************************************************/ |
| 377 |
|
| 378 |
void test13() |
| 379 |
{ |
| 380 |
int j; |
| 381 |
|
| 382 |
j = 0; |
| 383 |
foreach (wchar d; "hello") |
| 384 |
{ |
| 385 |
printf("d = x%x\n", d); |
| 386 |
if (j == 0) assert(d == 'h'); |
| 387 |
if (j == 1) assert(d == 'e'); |
| 388 |
if (j == 2) assert(d == 'l'); |
| 389 |
if (j == 3) assert(d == 'l'); |
| 390 |
if (j == 4) assert(d == 'o'); |
| 391 |
j++; |
| 392 |
} |
| 393 |
assert(j == 5); |
| 394 |
|
| 395 |
j = 0; |
| 396 |
foreach (size_t i, wchar d; "hello") |
| 397 |
{ |
| 398 |
printf("i = %d, d = x%x\n", cast(int)i, d); |
| 399 |
if (j == 0) assert(d == 'h'); |
| 400 |
if (j == 1) assert(d == 'e'); |
| 401 |
if (j == 2) assert(d == 'l'); |
| 402 |
if (j == 3) assert(d == 'l'); |
| 403 |
if (j == 4) assert(d == 'o'); |
| 404 |
assert(i == j); |
| 405 |
j++; |
| 406 |
} |
| 407 |
assert(j == 5); |
| 408 |
} |
| 409 |
|
| 410 |
/**************************************************/ |
| 411 |
|
| 412 |
void test14() |
| 413 |
{ |
| 414 |
int j; |
| 415 |
|
| 416 |
j = 0; |
| 417 |
foreach (char d; cast(wstring)"hello") |
| 418 |
{ |
| 419 |
printf("d = x%x\n", d); |
| 420 |
if (j == 0) assert(d == 'h'); |
| 421 |
if (j == 1) assert(d == 'e'); |
| 422 |
if (j == 2) assert(d == 'l'); |
| 423 |
if (j == 3) assert(d == 'l'); |
| 424 |
if (j == 4) assert(d == 'o'); |
| 425 |
j++; |
| 426 |
} |
| 427 |
assert(j == 5); |
| 428 |
|
| 429 |
j = 0; |
| 430 |
foreach (size_t i, char d; cast(wstring)"hello") |
| 431 |
{ |
| 432 |
printf("i = %d, d = x%x\n", cast(int)i, d); |
| 433 |
if (j == 0) assert(d == 'h'); |
| 434 |
if (j == 1) assert(d == 'e'); |
| 435 |
if (j == 2) assert(d == 'l'); |
| 436 |
if (j == 3) assert(d == 'l'); |
| 437 |
if (j == 4) assert(d == 'o'); |
| 438 |
assert(i == j); |
| 439 |
j++; |
| 440 |
} |
| 441 |
assert(j == 5); |
| 442 |
} |
| 443 |
|
| 444 |
/**************************************************/ |
| 445 |
|
| 446 |
void test15() |
| 447 |
{ |
| 448 |
int j; |
| 449 |
|
| 450 |
j = 0; |
| 451 |
foreach (dchar d; cast(wstring)"hello") |
| 452 |
{ |
| 453 |
printf("d = x%x\n", d); |
| 454 |
if (j == 0) assert(d == 'h'); |
| 455 |
if (j == 1) assert(d == 'e'); |
| 456 |
if (j == 2) assert(d == 'l'); |
| 457 |
if (j == 3) assert(d == 'l'); |
| 458 |
if (j == 4) assert(d == 'o'); |
| 459 |
j++; |
| 460 |
} |
| 461 |
assert(j == 5); |
| 462 |
|
| 463 |
j = 0; |
| 464 |
foreach (size_t i, dchar d; cast(wstring)"hello") |
| 465 |
{ |
| 466 |
printf("i = %d, d = x%x\n", cast(int)i, d); |
| 467 |
if (j == 0) assert(d == 'h'); |
| 468 |
if (j == 1) assert(d == 'e'); |
| 469 |
if (j == 2) assert(d == 'l'); |
| 470 |
if (j == 3) assert(d == 'l'); |
| 471 |
if (j == 4) assert(d == 'o'); |
| 472 |
assert(i == j); |
| 473 |
j++; |
| 474 |
} |
| 475 |
assert(j == 5); |
| 476 |
} |
| 477 |
|
| 478 |
/**************************************************/ |
| 479 |
|
| 480 |
void test16() |
| 481 |
{ |
| 482 |
int j; |
| 483 |
|
| 484 |
j = 0; |
| 485 |
foreach (char d; cast(dstring)"hello") |
| 486 |
{ |
| 487 |
printf("d = x%x\n", d); |
| 488 |
if (j == 0) assert(d == 'h'); |
| 489 |
if (j == 1) assert(d == 'e'); |
| 490 |
if (j == 2) assert(d == 'l'); |
| 491 |
if (j == 3) assert(d == 'l'); |
| 492 |
if (j == 4) assert(d == 'o'); |
| 493 |
j++; |
| 494 |
} |
| 495 |
assert(j == 5); |
| 496 |
|
| 497 |
j = 0; |
| 498 |
foreach (size_t i, char d; cast(dstring)"hello") |
| 499 |
{ |
| 500 |
printf("i = %d, d = x%x\n", cast(int)i, d); |
| 501 |
if (j == 0) assert(d == 'h'); |
| 502 |
if (j == 1) assert(d == 'e'); |
| 503 |
if (j == 2) assert(d == 'l'); |
| 504 |
if (j == 3) assert(d == 'l'); |
| 505 |
if (j == 4) assert(d == 'o'); |
| 506 |
assert(i == j); |
| 507 |
j++; |
| 508 |
} |
| 509 |
assert(j == 5); |
| 510 |
} |
| 511 |
|
| 512 |
/**************************************************/ |
| 513 |
|
| 514 |
void test17() |
| 515 |
{ |
| 516 |
int j; |
| 517 |
|
| 518 |
j = 0; |
| 519 |
foreach (wchar d; cast(dstring)"hello") |
| 520 |
{ |
| 521 |
printf("d = x%x\n", d); |
| 522 |
if (j == 0) assert(d == 'h'); |
| 523 |
if (j == 1) assert(d == 'e'); |
| 524 |
if (j == 2) assert(d == 'l'); |
| 525 |
if (j == 3) assert(d == 'l'); |
| 526 |
if (j == 4) assert(d == 'o'); |
| 527 |
j++; |
| 528 |
} |
| 529 |
assert(j == 5); |
| 530 |
|
| 531 |
j = 0; |
| 532 |
foreach (size_t i, wchar d; cast(dstring)"hello") |
| 533 |
{ |
| 534 |
printf("i = %d, d = x%x\n", cast(int)i, d); |
| 535 |
if (j == 0) assert(d == 'h'); |
| 536 |
if (j == 1) assert(d == 'e'); |
| 537 |
if (j == 2) assert(d == 'l'); |
| 538 |
if (j == 3) assert(d == 'l'); |
| 539 |
if (j == 4) assert(d == 'o'); |
| 540 |
assert(i == j); |
| 541 |
j++; |
| 542 |
} |
| 543 |
assert(j == 5); |
| 544 |
} |
| 545 |
|
| 546 |
/**************************************************/ |
| 547 |
|
| 548 |
void test18() |
| 549 |
{ |
| 550 |
string a = "\xE2\x89\xA0"; // \u2260 encoded as 3 UTF-8 bytes |
| 551 |
|
| 552 |
foreach (dchar c; a) |
| 553 |
{ |
| 554 |
printf("a[] = %x\n", c); // prints 'a[] = 2260' |
| 555 |
assert(c == 0x2260); |
| 556 |
} |
| 557 |
|
| 558 |
dstring b = "\u2260"; |
| 559 |
|
| 560 |
int i; |
| 561 |
foreach (char c; b) |
| 562 |
{ |
| 563 |
printf("%x, ", c); // prints e2, 89, a0 |
| 564 |
if (i == 0) assert(c == 0xE2); |
| 565 |
else if (i == 1) assert(c == 0x89); |
| 566 |
else if (i == 2) assert(c == 0xA0); |
| 567 |
else |
| 568 |
assert(0); |
| 569 |
i++; |
| 570 |
} |
| 571 |
printf("\n"); |
| 572 |
} |
| 573 |
|
| 574 |
/**************************************************/ |
| 575 |
|
| 576 |
void test19() |
| 577 |
{ |
| 578 |
string string = "\xF0\x9D\x83\x93"; |
| 579 |
|
| 580 |
int count=0; |
| 581 |
dchar tmp; |
| 582 |
foreach(dchar value ; string){ |
| 583 |
tmp=value; |
| 584 |
count++; |
| 585 |
} |
| 586 |
assert(count==1); |
| 587 |
assert(tmp==0x01D0D3); |
| 588 |
} |
| 589 |
|
| 590 |
/**************************************************/ |
| 591 |
|
| 592 |
struct S20 |
| 593 |
{ |
| 594 |
int opApply(int delegate(ref int i) dg) |
| 595 |
{ |
| 596 |
return 0; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
S20 foo20; |
| 601 |
|
| 602 |
void test20() |
| 603 |
{ |
| 604 |
label: |
| 605 |
foreach(int i; foo20) |
| 606 |
{ |
| 607 |
continue label; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
/**************************************************/ |
| 613 |
|
| 614 |
void foo21(string[] args) |
| 615 |
{ |
| 616 |
printf("args.length = %d\n", cast(int)args.length); |
| 617 |
assert(args.length == 3); |
| 618 |
foreach (i, arg; args) |
| 619 |
{ |
| 620 |
assert(typeid(typeof(i)) == typeid(size_t)); |
| 621 |
assert(typeid(typeof(arg)) == typeid(string)); |
| 622 |
printf("args[%d] = '%.*s'\n", cast(int)i, cast(int)arg.length, arg.ptr); |
| 623 |
} |
| 624 |
foreach (arg; args) |
| 625 |
{ |
| 626 |
assert(typeid(typeof(arg)) == typeid(string)); |
| 627 |
printf("args[] = '%.*s'\n", cast(int)arg.length, arg.ptr); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
void test21() |
| 632 |
{ |
| 633 |
string[] args; |
| 634 |
|
| 635 |
args.length = 3; |
| 636 |
args[0] = "a"; |
| 637 |
args[1] = "bc"; |
| 638 |
args[2] = "d"; |
| 639 |
foo21(args); |
| 640 |
} |
| 641 |
|
| 642 |
/**************************************************/ |
| 643 |
|
| 644 |
void test22() |
| 645 |
{ |
| 646 |
int[string] map; |
| 647 |
|
| 648 |
map["hello"] = 3; |
| 649 |
map["world"] = 4; |
| 650 |
|
| 651 |
foreach (key, value; map) |
| 652 |
{ |
| 653 |
assert(typeid(typeof(key)) == typeid(string)); |
| 654 |
assert(typeid(typeof(value)) == typeid(int)); |
| 655 |
printf("map[%.*s] = %d\n", cast(int)key.length, key.ptr, value); |
| 656 |
} |
| 657 |
foreach (key, int value; map) |
| 658 |
{ |
| 659 |
assert(typeid(typeof(key)) == typeid(string)); |
| 660 |
assert(typeid(typeof(value)) == typeid(int)); |
| 661 |
printf("map[%.*s] = %d\n", cast(int)key.length, key.ptr, value); |
| 662 |
} |
| 663 |
foreach (string key, value; map) |
| 664 |
{ |
| 665 |
assert(typeid(typeof(key)) == typeid(string)); |
| 666 |
assert(typeid(typeof(value)) == typeid(int)); |
| 667 |
printf("map[%.*s] = %d\n", cast(int)key.length, key.ptr, value); |
| 668 |
} |
| 669 |
foreach (value; map) |
| 670 |
{ |
| 671 |
assert(typeid(typeof(value)) == typeid(int)); |
| 672 |
printf("map[] = %d\n", value); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
/**************************************************/ |
| 677 |
|
| 678 |
class Foo23 |
| 679 |
{ |
| 680 |
int[2] array; |
| 681 |
|
| 682 |
int opApply(int delegate(ref int) dg) |
| 683 |
{ |
| 684 |
int result; |
| 685 |
for (int i = 0; i < array.length; i++) |
| 686 |
{ |
| 687 |
result = dg(array[i]); |
| 688 |
if (result) |
| 689 |
break; |
| 690 |
} |
| 691 |
return result; |
| 692 |
} |
| 693 |
|
| 694 |
int opApply(int delegate(ref size_t, ref int) dg) |
| 695 |
{ |
| 696 |
int result; |
| 697 |
for (size_t i = 0; i < array.length; i++) |
| 698 |
{ |
| 699 |
result = dg(i, array[i]); |
| 700 |
if (result) |
| 701 |
break; |
| 702 |
} |
| 703 |
return result; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
void test23() |
| 708 |
{ |
| 709 |
Foo23 a = new Foo23(); |
| 710 |
int i; |
| 711 |
|
| 712 |
a.array[0] = 73; |
| 713 |
a.array[1] = 82; |
| 714 |
|
| 715 |
foreach (u; a) |
| 716 |
{ |
| 717 |
assert(typeid(typeof(u)) == typeid(int)); |
| 718 |
i++; |
| 719 |
u++; |
| 720 |
//printf("u = %d\n", u); |
| 721 |
assert((i == 1) ? u == 74 : u == 83); |
| 722 |
} |
| 723 |
assert(i == 2); |
| 724 |
assert(a.array[0] == 73); |
| 725 |
assert(a.array[1] == 82); |
| 726 |
|
| 727 |
foreach (j, u; a) |
| 728 |
{ |
| 729 |
assert(typeid(typeof(j)) == typeid(size_t)); |
| 730 |
assert(typeid(typeof(u)) == typeid(int)); |
| 731 |
i++; |
| 732 |
u++; |
| 733 |
//printf("u = %d\n", u); |
| 734 |
assert((i == 3) ? u == 74 : u == 83); |
| 735 |
assert(j == i - 3); |
| 736 |
} |
| 737 |
assert(i == 4); |
| 738 |
assert(a.array[0] == 73); |
| 739 |
assert(a.array[1] == 82); |
| 740 |
} |
| 741 |
|
| 742 |
/**************************************************/ |
| 743 |
|
| 744 |
struct Collection24 |
| 745 |
{ |
| 746 |
int opApply(int delegate(ref int) dg){ |
| 747 |
return 0; |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
bool foo24() |
| 752 |
{ |
| 753 |
Collection24 a,b; |
| 754 |
|
| 755 |
foreach(int x; a){ |
| 756 |
foreach(int y; b){ |
| 757 |
return false; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
return true; |
| 762 |
} |
| 763 |
|
| 764 |
void test24() |
| 765 |
{ |
| 766 |
assert(foo24() == true); |
| 767 |
} |
| 768 |
|
| 769 |
/**************************************************/ |
| 770 |
|
| 771 |
void test25() |
| 772 |
{ |
| 773 |
alias void function(string[string]) FN; |
| 774 |
FN fn = function (string[string] aarray) |
| 775 |
{ |
| 776 |
foreach (string s; aarray) |
| 777 |
{ |
| 778 |
assert(s == "b"); |
| 779 |
} |
| 780 |
}; |
| 781 |
string[string] aarray; |
| 782 |
aarray["a"] = "b"; |
| 783 |
fn(aarray); |
| 784 |
} |
| 785 |
|
| 786 |
/**************************************************/ |
| 787 |
|
| 788 |
struct Foo26 |
| 789 |
{ |
| 790 |
uint[2] array; |
| 791 |
|
| 792 |
int forward(int delegate(ref uint) dg) |
| 793 |
{ |
| 794 |
int result; |
| 795 |
for (int i = 0; i < array.length; i++) |
| 796 |
{ |
| 797 |
result = dg(array[i]); |
| 798 |
if (result) |
| 799 |
break; |
| 800 |
} |
| 801 |
return result; |
| 802 |
} |
| 803 |
|
| 804 |
int forward(int delegate(ref uint) dg, int x) { return 1; } |
| 805 |
|
| 806 |
int reverse(int delegate(ref uint) dg, int x) { return 1; } |
| 807 |
|
| 808 |
int reverse(int delegate(ref uint) dg) |
| 809 |
{ |
| 810 |
int result; |
| 811 |
foreach_reverse (v; array) |
| 812 |
{ |
| 813 |
auto u = v; |
| 814 |
result = dg(u); |
| 815 |
if (result) |
| 816 |
break; |
| 817 |
} |
| 818 |
return result; |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
void test26() |
| 824 |
{ |
| 825 |
Foo26 a; |
| 826 |
int i; |
| 827 |
|
| 828 |
a.array[0] = 73; |
| 829 |
a.array[1] = 82; |
| 830 |
|
| 831 |
foreach (u; &a.forward) |
| 832 |
{ |
| 833 |
printf("%d\n", u); |
| 834 |
i++; |
| 835 |
u++; |
| 836 |
} |
| 837 |
assert(i == 2); |
| 838 |
assert(a.array[0] == 73); |
| 839 |
assert(a.array[1] == 82); |
| 840 |
|
| 841 |
foreach (uint u; &a.reverse) |
| 842 |
{ |
| 843 |
printf("%d\n", u); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
/**************************************************/ |
| 848 |
|
| 849 |
struct S27 |
| 850 |
{ |
| 851 |
int[] a; |
| 852 |
|
| 853 |
bool empty() { return a.length == 0; } |
| 854 |
|
| 855 |
void popFront() { a = a[1 .. $]; } |
| 856 |
int front() { return a[0]; } |
| 857 |
|
| 858 |
void popBack() { a = a[0 .. $ - 1]; } |
| 859 |
ref int back() { return a[$ - 1]; } |
| 860 |
} |
| 861 |
|
| 862 |
void test27() |
| 863 |
{ |
| 864 |
S27 s; |
| 865 |
s.a = [5,6,7]; |
| 866 |
string r; |
| 867 |
|
| 868 |
foreach (e; s) |
| 869 |
{ |
| 870 |
printf("%d\n", e); |
| 871 |
r ~= cast(char)(e + '0'); |
| 872 |
} |
| 873 |
assert(r == "567"); |
| 874 |
|
| 875 |
r = null; |
| 876 |
foreach_reverse (ref e; s) |
| 877 |
{ |
| 878 |
e++; |
| 879 |
printf("%d\n", e); |
| 880 |
r ~= cast(char)(e + '0'); |
| 881 |
} |
| 882 |
assert(r == "876"); |
| 883 |
|
| 884 |
r = null; |
| 885 |
foreach (e; s) |
| 886 |
{ |
| 887 |
printf("%d\n", e); |
| 888 |
r ~= cast(char)(e + '0'); |
| 889 |
} |
| 890 |
assert(r == "678"); |
| 891 |
} |
| 892 |
|
| 893 |
/**************************************************/ |
| 894 |
|
| 895 |
int main() |
| 896 |
{ |
| 897 |
test1(); |
| 898 |
test2(); |
| 899 |
test3(); |
| 900 |
test4(); |
| 901 |
test5(); |
| 902 |
test6(); |
| 903 |
test7(); |
| 904 |
test8(); |
| 905 |
test9(); |
| 906 |
test10(); |
| 907 |
test11(); |
| 908 |
test12(); |
| 909 |
test13(); |
| 910 |
test14(); |
| 911 |
test15(); |
| 912 |
test16(); |
| 913 |
test17(); |
| 914 |
test18(); |
| 915 |
test19(); |
| 916 |
test20(); |
| 917 |
test21(); |
| 918 |
test22(); |
| 919 |
test23(); |
| 920 |
test24(); |
| 921 |
test25(); |
| 922 |
test26(); |
| 923 |
test27(); |
| 924 |
|
| 925 |
printf("Success\n"); |
| 926 |
return 0; |
| 927 |
} |