| 1 |
module test; |
| 2 |
|
| 3 |
import core.vararg; |
| 4 |
import core.stdc.stdlib; |
| 5 |
import core.stdc.stdio; |
| 6 |
|
| 7 |
/*******************************************/ |
| 8 |
|
| 9 |
struct S |
| 10 |
{ |
| 11 |
int opSliceAssign(int v, size_t i, size_t j) |
| 12 |
{ |
| 13 |
assert(v == 5); |
| 14 |
assert(i == 9); |
| 15 |
assert(j == 10); |
| 16 |
return 3; |
| 17 |
} |
| 18 |
|
| 19 |
int opSliceAssign(int v) |
| 20 |
{ |
| 21 |
assert(v == 6); |
| 22 |
return 11; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
void test1() |
| 27 |
{ |
| 28 |
S s; |
| 29 |
|
| 30 |
assert((s[9 .. 10] = 5) == 3); |
| 31 |
assert((s[] = 6) == 11); |
| 32 |
} |
| 33 |
|
| 34 |
/*******************************************/ |
| 35 |
|
| 36 |
static int i2 = 1; |
| 37 |
|
| 38 |
void test2() |
| 39 |
{ |
| 40 |
synchronized { int i2 = 2; } |
| 41 |
assert(i2 == 1); |
| 42 |
} |
| 43 |
|
| 44 |
/*******************************************/ |
| 45 |
|
| 46 |
void test3() |
| 47 |
{ |
| 48 |
size_t border = 8; |
| 49 |
|
| 50 |
for(ulong i = 0; i < border; i++) |
| 51 |
{ |
| 52 |
ulong test = 1; |
| 53 |
test <<= i; |
| 54 |
double r = test; |
| 55 |
ulong result = cast(ulong)r; |
| 56 |
|
| 57 |
if (result != test) |
| 58 |
{ |
| 59 |
assert(0); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/*******************************************/ |
| 65 |
|
| 66 |
void test5() |
| 67 |
{ |
| 68 |
int[] qwert = new int[6]; |
| 69 |
int[] yuiop; |
| 70 |
yuiop = qwert[2..5] = 3; |
| 71 |
assert(yuiop.length == 3); |
| 72 |
assert(yuiop[0] == 3); |
| 73 |
assert(yuiop[1] == 3); |
| 74 |
assert(yuiop[2] == 3); |
| 75 |
} |
| 76 |
|
| 77 |
/*******************************************/ |
| 78 |
|
| 79 |
struct Foo6 |
| 80 |
{ |
| 81 |
static int x; |
| 82 |
|
| 83 |
static int[] farray() |
| 84 |
{ |
| 85 |
printf("farray\n"); |
| 86 |
assert(x == 0); |
| 87 |
x++; |
| 88 |
return new int[6]; |
| 89 |
} |
| 90 |
|
| 91 |
static int flwr() |
| 92 |
{ |
| 93 |
printf("flwr\n"); |
| 94 |
assert(x == 1); |
| 95 |
x++; |
| 96 |
return 2; |
| 97 |
} |
| 98 |
|
| 99 |
static int fupr() |
| 100 |
{ |
| 101 |
printf("fupr\n"); |
| 102 |
assert(x == 2); |
| 103 |
x++; |
| 104 |
return 1; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
void test6() |
| 109 |
{ |
| 110 |
int[] yuiop; |
| 111 |
yuiop = |
| 112 |
Foo6.farray()[Foo6.flwr() .. $ - Foo6.fupr()] = 3; |
| 113 |
assert(Foo6.x == 3); |
| 114 |
assert(yuiop.length == 3); |
| 115 |
assert(yuiop[0] == 3); |
| 116 |
assert(yuiop[1] == 3); |
| 117 |
assert(yuiop[2] == 3); |
| 118 |
} |
| 119 |
|
| 120 |
/*******************************************/ |
| 121 |
|
| 122 |
void test7() |
| 123 |
{ |
| 124 |
real a = 3.40483; // this is treated as 3.40483L |
| 125 |
real b; |
| 126 |
b = 3.40483; |
| 127 |
assert(a==b); |
| 128 |
assert(a==3.40483); |
| 129 |
assert(a==3.40483L); |
| 130 |
assert(a==3.40483F); |
| 131 |
} |
| 132 |
|
| 133 |
/*******************************************/ |
| 134 |
|
| 135 |
void test8() |
| 136 |
{ |
| 137 |
real [5][5] m = 1; |
| 138 |
m[1][1..3] = 2; |
| 139 |
|
| 140 |
for (size_t i = 0; i < 5; i++) |
| 141 |
for (size_t j = 0; j < 5; j++) |
| 142 |
{ |
| 143 |
if (i == 1 && (j >= 1 && j < 3)) |
| 144 |
assert(m[i][j] == 2); |
| 145 |
else |
| 146 |
assert(m[i][j] == 1); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/*******************************************/ |
| 151 |
|
| 152 |
class ClassOf(Type) |
| 153 |
{ |
| 154 |
Type val; |
| 155 |
|
| 156 |
template refx() |
| 157 |
{ |
| 158 |
alias val refx; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
struct StructOf |
| 163 |
{ |
| 164 |
int val; |
| 165 |
|
| 166 |
template refx() |
| 167 |
{ |
| 168 |
alias val refx; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void test9() |
| 173 |
{ |
| 174 |
ClassOf!(int) c = new ClassOf!(int)(); |
| 175 |
StructOf s; |
| 176 |
int x = 10; |
| 177 |
|
| 178 |
c.refx!() = x; |
| 179 |
x = c.refx!(); |
| 180 |
assert(x == 10); |
| 181 |
|
| 182 |
x = 11; |
| 183 |
s.refx!() = x; |
| 184 |
x = s.refx!(); |
| 185 |
assert(x == 11); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
/*******************************************/ |
| 190 |
|
| 191 |
void test10() |
| 192 |
{ |
| 193 |
static if( int.mangleof.length > 1 && int.mangleof[1] == 'x' ) |
| 194 |
printf("'x' as second char\n"); |
| 195 |
} |
| 196 |
|
| 197 |
/*******************************************/ |
| 198 |
|
| 199 |
class Foo11 : Bar11 { } |
| 200 |
|
| 201 |
class Foo11T(V) |
| 202 |
{ |
| 203 |
public void foo() {} |
| 204 |
} |
| 205 |
|
| 206 |
class Bar11 |
| 207 |
{ |
| 208 |
public this(){ |
| 209 |
f = new Foo11T!(int); |
| 210 |
} |
| 211 |
Foo11T!(int) f; |
| 212 |
} |
| 213 |
|
| 214 |
void test11() |
| 215 |
{ |
| 216 |
Foo11 fooIt = new Foo11(); |
| 217 |
if (fooIt !is null) |
| 218 |
printf("fooIt should be valid\n"); |
| 219 |
fooIt.f.foo(); |
| 220 |
printf("it worked\n"); |
| 221 |
} |
| 222 |
|
| 223 |
/*******************************************/ |
| 224 |
|
| 225 |
struct A12 { |
| 226 |
int a; |
| 227 |
union { |
| 228 |
int c; |
| 229 |
B12 b; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
struct B12 { |
| 234 |
int b1; |
| 235 |
int b2; |
| 236 |
} |
| 237 |
|
| 238 |
void test12() |
| 239 |
{ |
| 240 |
A12 a; |
| 241 |
printf("%zd\n", A12.sizeof); |
| 242 |
assert(A12.sizeof == 12); |
| 243 |
} |
| 244 |
|
| 245 |
/*******************************************/ |
| 246 |
|
| 247 |
template declare13(X) { X declare13; } |
| 248 |
|
| 249 |
typeof(declare13!(int[0]).ptr[0]) x13; |
| 250 |
typeof(declare13!(typeof(""))[0..$]) y13; |
| 251 |
|
| 252 |
void test13() |
| 253 |
{ |
| 254 |
} |
| 255 |
|
| 256 |
/*******************************************/ |
| 257 |
|
| 258 |
interface Father {} |
| 259 |
|
| 260 |
class Mother { |
| 261 |
Father test() { |
| 262 |
printf("Called Mother.test!\n"); |
| 263 |
return new Child(42); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
class Child : Mother, Father { |
| 268 |
int data; |
| 269 |
|
| 270 |
this(int d) { data = d; } |
| 271 |
|
| 272 |
override Child test() { |
| 273 |
printf("Called Child.test!\n"); |
| 274 |
return new Child(69); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void test14() |
| 279 |
{ |
| 280 |
Child aChild = new Child(105); |
| 281 |
Mother childsMum = aChild; |
| 282 |
Child childsChild = aChild.test(); |
| 283 |
Child mumsChild = cast(Child) childsMum.test(); |
| 284 |
printf("Success2\n"); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/*******************************************/ |
| 289 |
|
| 290 |
class A15 |
| 291 |
{ |
| 292 |
int a = 3; |
| 293 |
|
| 294 |
class B |
| 295 |
{ |
| 296 |
void bar() |
| 297 |
{ |
| 298 |
assert(a == 3); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
void fork() |
| 303 |
{ |
| 304 |
assert(a == 3); |
| 305 |
B b = new B(); // This is okay |
| 306 |
b.bar(); |
| 307 |
|
| 308 |
void knife() |
| 309 |
{ |
| 310 |
assert(a == 3); |
| 311 |
B b = new B(); // No 'this' for nested class B |
| 312 |
b.bar(); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
void test15() |
| 318 |
{ |
| 319 |
A15 a = new A15(); |
| 320 |
a.fork(); |
| 321 |
} |
| 322 |
|
| 323 |
/*******************************************/ |
| 324 |
|
| 325 |
void test17() |
| 326 |
{ |
| 327 |
version(D_InlineAsm_X86_64) |
| 328 |
enum AsmX86 = true; |
| 329 |
else version(D_InlineAsm_X86) |
| 330 |
enum AsmX86 = true; |
| 331 |
else |
| 332 |
enum AsmX86 = false; |
| 333 |
|
| 334 |
version (OSX) |
| 335 |
{ |
| 336 |
} |
| 337 |
else |
| 338 |
{ |
| 339 |
const f = 1.2f; |
| 340 |
float g = void; |
| 341 |
|
| 342 |
static if (AsmX86) |
| 343 |
{ |
| 344 |
asm |
| 345 |
{ |
| 346 |
fld f; // doesn't work with PIC |
| 347 |
fstp g; |
| 348 |
} |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
g = f; |
| 353 |
} |
| 354 |
assert(g == 1.2f); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/*******************************************/ |
| 359 |
|
| 360 |
class Foo18 : Bar18 {} |
| 361 |
class FooT18(V){} |
| 362 |
class Bar18 : FooT18!(int) {} |
| 363 |
|
| 364 |
void test18() |
| 365 |
{ |
| 366 |
} |
| 367 |
|
| 368 |
/*******************************************/ |
| 369 |
|
| 370 |
struct STRUCTA19 |
| 371 |
{ |
| 372 |
union { |
| 373 |
int a; |
| 374 |
long b; |
| 375 |
} |
| 376 |
STRUCTB19 c; |
| 377 |
} |
| 378 |
|
| 379 |
struct STRUCTB19 |
| 380 |
{ |
| 381 |
int a; |
| 382 |
} |
| 383 |
|
| 384 |
void test19() |
| 385 |
{ |
| 386 |
} |
| 387 |
|
| 388 |
/*******************************************/ |
| 389 |
|
| 390 |
class Foo20 |
| 391 |
{ |
| 392 |
void bar (void * src) |
| 393 |
{ |
| 394 |
void baz (void function (void *, size_t) xyz) |
| 395 |
{ |
| 396 |
size_t foo (void [] dst) |
| 397 |
{ |
| 398 |
size_t len = dst.length; |
| 399 |
dst [0 .. len] = src [0 .. len]; |
| 400 |
xyz (dst.ptr, len); |
| 401 |
return len; |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
void test20() |
| 408 |
{ |
| 409 |
} |
| 410 |
|
| 411 |
/*******************************************/ |
| 412 |
|
| 413 |
class Baz21 |
| 414 |
{ |
| 415 |
int opApply (int delegate(ref int) dg) |
| 416 |
{ |
| 417 |
int i; |
| 418 |
return dg(i); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
class Foo21 |
| 423 |
{ |
| 424 |
Baz21 baz; |
| 425 |
|
| 426 |
int foo (int delegate() dg) |
| 427 |
{ |
| 428 |
foreach (b; baz) |
| 429 |
if (bar ()) |
| 430 |
if (dg ()) |
| 431 |
break; |
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
bool bar () |
| 436 |
{ return true; } |
| 437 |
} |
| 438 |
|
| 439 |
void test21() |
| 440 |
{ |
| 441 |
} |
| 442 |
|
| 443 |
/*******************************************/ |
| 444 |
|
| 445 |
struct Bar22 { |
| 446 |
union { |
| 447 |
struct { |
| 448 |
union { |
| 449 |
Foo22 A; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
struct Foo22 { |
| 456 |
double d = 3; |
| 457 |
} |
| 458 |
|
| 459 |
void test22() |
| 460 |
{ |
| 461 |
printf("Bar22.sizeof = %zd, double.sizeof = %zd\n", Bar22.sizeof, double.sizeof); |
| 462 |
assert(Bar22.sizeof == double.sizeof); |
| 463 |
Bar22 b; |
| 464 |
assert(b.A.d == 3); |
| 465 |
} |
| 466 |
|
| 467 |
/*******************************************/ |
| 468 |
|
| 469 |
struct Ag |
| 470 |
{ |
| 471 |
static void func(){} |
| 472 |
|
| 473 |
static void foo() |
| 474 |
{ |
| 475 |
void function() fnp; |
| 476 |
Ag a; |
| 477 |
|
| 478 |
fnp = &func; |
| 479 |
fnp = &Ag.func; |
| 480 |
with(a) fnp = &Ag.func; |
| 481 |
|
| 482 |
with(a) fnp = &func; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
class Ah |
| 487 |
{ |
| 488 |
static void func(){} |
| 489 |
|
| 490 |
static void foo() |
| 491 |
{ |
| 492 |
void function() fnp; |
| 493 |
Ah a; |
| 494 |
|
| 495 |
fnp = &func; |
| 496 |
fnp = &Ah.func; |
| 497 |
with(a) fnp = &Ah.func; |
| 498 |
|
| 499 |
with(a) fnp = &func; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
void test23() |
| 504 |
{ |
| 505 |
} |
| 506 |
|
| 507 |
/*******************************************/ |
| 508 |
|
| 509 |
void test24() |
| 510 |
{ |
| 511 |
uint[10] arr1; |
| 512 |
ulong idx = 3; |
| 513 |
uint[] arr3 = arr1[ cast(int)(idx) .. (cast(int) idx) + 3 ]; // OK |
| 514 |
uint[] arr4 = arr1[ cast(int) idx .. cast(int) idx + 3 ]; // OK |
| 515 |
uint[] arr5 = arr1[ cast(int)(idx) .. cast(int)(idx) + 3 ]; // C style cast illegal, use cast(idx)+3 |
| 516 |
uint[] arr6 = arr1[ cast(int)(idx) .. cast(int)(idx + 3) ]; // OK |
| 517 |
} |
| 518 |
|
| 519 |
/*******************************************/ |
| 520 |
|
| 521 |
void test25() |
| 522 |
{ |
| 523 |
char[6] cstr = "123456"c; |
| 524 |
auto str1 = cast(wchar[3])(cstr); |
| 525 |
|
| 526 |
assert(cast(char[])str1 == "123456"c); |
| 527 |
|
| 528 |
auto str2 = cast(wchar[3])("789abc"c); |
| 529 |
assert(cast(char[])str2 == "789abc"c); |
| 530 |
|
| 531 |
auto str3 = cast(wchar[3])("defghi"); |
| 532 |
version (LittleEndian) |
| 533 |
assert(cast(char[])str3 == "d\000e\000f\000"c); |
| 534 |
version (BigEndian) |
| 535 |
assert(cast(char[])str3 == "\000d\000e\000f"c); |
| 536 |
} |
| 537 |
|
| 538 |
/*******************************************/ |
| 539 |
|
| 540 |
void test26() |
| 541 |
{ |
| 542 |
assert(foo26(5) == 25); |
| 543 |
} |
| 544 |
|
| 545 |
int foo26(int i) |
| 546 |
{ |
| 547 |
if (auto j = i * i) |
| 548 |
return j; |
| 549 |
else |
| 550 |
return 10; |
| 551 |
} |
| 552 |
|
| 553 |
/*******************************************/ |
| 554 |
|
| 555 |
class A27 |
| 556 |
{ |
| 557 |
int am; |
| 558 |
|
| 559 |
class B |
| 560 |
{ |
| 561 |
this() |
| 562 |
{ |
| 563 |
assert(am == 3); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
void fork() |
| 568 |
{ |
| 569 |
B b = new B(); // This is okay |
| 570 |
|
| 571 |
void knife() |
| 572 |
{ |
| 573 |
B b = new B(); // No 'this' for nested class B |
| 574 |
assert(am == 3); |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
void test27() |
| 581 |
{ |
| 582 |
A27 a = new A27(); |
| 583 |
a.am = 3; |
| 584 |
|
| 585 |
a.fork(); |
| 586 |
} |
| 587 |
|
| 588 |
/*******************************************/ |
| 589 |
|
| 590 |
uint intRes() |
| 591 |
{ |
| 592 |
return 4; |
| 593 |
} |
| 594 |
|
| 595 |
void test28() |
| 596 |
{ |
| 597 |
auto s = "abc123"[intRes() % $]; |
| 598 |
assert(s == '2'); |
| 599 |
|
| 600 |
static const char[] foo = "abc123"; |
| 601 |
assert(foo[intRes() % $] == '2'); |
| 602 |
|
| 603 |
static string bar = "abc123"; |
| 604 |
assert(bar[intRes() % $] == '2'); |
| 605 |
|
| 606 |
const char[] abc = "abc123"; |
| 607 |
assert(abc[intRes() % $] == '2'); |
| 608 |
|
| 609 |
string def = "abc123"; |
| 610 |
assert(def[intRes() % $] == '2'); |
| 611 |
} |
| 612 |
|
| 613 |
/*******************************************/ |
| 614 |
|
| 615 |
class UA { |
| 616 |
A29 f() { return null; } |
| 617 |
} |
| 618 |
|
| 619 |
class UB : UA { |
| 620 |
override B29 f() { return null; } |
| 621 |
} |
| 622 |
|
| 623 |
class A29 |
| 624 |
{ |
| 625 |
} |
| 626 |
|
| 627 |
class B29 : A29 |
| 628 |
{ |
| 629 |
} |
| 630 |
|
| 631 |
void test29() |
| 632 |
{ |
| 633 |
} |
| 634 |
|
| 635 |
/*******************************************/ |
| 636 |
|
| 637 |
class Foo30 : Bar30 {} |
| 638 |
|
| 639 |
class FooT30(V) {} |
| 640 |
|
| 641 |
class Bar30 : FooT30!(int) {} |
| 642 |
|
| 643 |
void test30() |
| 644 |
{ |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
/*******************************************/ |
| 649 |
|
| 650 |
int y31; |
| 651 |
|
| 652 |
struct X31 { static void opCall() { y31 = 3; } } |
| 653 |
|
| 654 |
void test31() |
| 655 |
{ |
| 656 |
X31 x; |
| 657 |
typeof(x)(); |
| 658 |
assert(y31 == 3); |
| 659 |
} |
| 660 |
|
| 661 |
/*******************************************/ |
| 662 |
|
| 663 |
struct o_O { int a; } |
| 664 |
union O_O { int a; } |
| 665 |
class O_o { int a; } |
| 666 |
|
| 667 |
struct Foo34 |
| 668 |
{ |
| 669 |
int ok; |
| 670 |
o_O foo; |
| 671 |
O_O bar; |
| 672 |
O_o baz; |
| 673 |
} |
| 674 |
|
| 675 |
void test34() |
| 676 |
{ |
| 677 |
int o1 = Foo34.ok.offsetof; |
| 678 |
assert(o1 == 0); |
| 679 |
int o2 = Foo34.foo.offsetof; |
| 680 |
assert(o2 == 4); |
| 681 |
int o3 = Foo34.bar.offsetof; |
| 682 |
assert(o3 == 8); |
| 683 |
int o4 = Foo34.baz.offsetof; |
| 684 |
assert((o4 % (void*).sizeof) == 0); |
| 685 |
assert(o4 > o3); |
| 686 |
} |
| 687 |
|
| 688 |
/*******************************************/ |
| 689 |
|
| 690 |
class Foo37 |
| 691 |
{ |
| 692 |
float[4] array = 1.0; |
| 693 |
int count = 10; |
| 694 |
} |
| 695 |
|
| 696 |
void test37() |
| 697 |
{ |
| 698 |
Foo37 f = new Foo37(); |
| 699 |
|
| 700 |
assert(f.array[0] == 1.0); |
| 701 |
assert(f.array[1] == 1.0); |
| 702 |
assert(f.array[2] == 1.0); |
| 703 |
assert(f.array[3] == 1.0); |
| 704 |
assert(f.count == 10); |
| 705 |
} |
| 706 |
|
| 707 |
/*******************************************/ |
| 708 |
|
| 709 |
void test38() |
| 710 |
in |
| 711 |
{ |
| 712 |
static void checkParameters() |
| 713 |
{ |
| 714 |
return; |
| 715 |
} |
| 716 |
|
| 717 |
checkParameters(); |
| 718 |
} |
| 719 |
do |
| 720 |
{ |
| 721 |
} |
| 722 |
|
| 723 |
/*******************************************/ |
| 724 |
|
| 725 |
void delegate() foo39() |
| 726 |
{ |
| 727 |
return &(new class |
| 728 |
{ |
| 729 |
|
| 730 |
int a; |
| 731 |
|
| 732 |
this() { a = 3; } |
| 733 |
|
| 734 |
void dg() |
| 735 |
{ |
| 736 |
printf("delegate!\n"); |
| 737 |
assert(a == 3); |
| 738 |
} |
| 739 |
}).dg; |
| 740 |
} |
| 741 |
|
| 742 |
void test39() |
| 743 |
{ |
| 744 |
void delegate() dg = foo39(); |
| 745 |
|
| 746 |
dg(); |
| 747 |
} |
| 748 |
|
| 749 |
/*******************************************/ |
| 750 |
|
| 751 |
void test40() |
| 752 |
{ |
| 753 |
assert( typeid(int) == typeid(int) ); |
| 754 |
assert( (typeid(int) != typeid(int)) == false ); |
| 755 |
|
| 756 |
int x; |
| 757 |
|
| 758 |
bool b1 = (typeid(typeof(x)) != typeid(int)); |
| 759 |
|
| 760 |
TypeInfo t1 = typeid(typeof(x)); |
| 761 |
TypeInfo t2 = typeid(int); |
| 762 |
|
| 763 |
bool b2 = (t1 != t2); |
| 764 |
|
| 765 |
assert(b1 == b2); |
| 766 |
} |
| 767 |
|
| 768 |
/*******************************************/ |
| 769 |
|
| 770 |
int foo41(string s) |
| 771 |
{ |
| 772 |
short shift = cast(short)(s.length * 3); |
| 773 |
int answer; |
| 774 |
|
| 775 |
for (size_t i = 0; i < s.length; i++){ |
| 776 |
answer = s[i] << shift; |
| 777 |
} |
| 778 |
|
| 779 |
return answer; |
| 780 |
} |
| 781 |
|
| 782 |
void test41() |
| 783 |
{ |
| 784 |
if(foo41("\u0001") != 8){ |
| 785 |
assert(0); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
/*******************************************/ |
| 790 |
|
| 791 |
struct S42 |
| 792 |
{ |
| 793 |
int i; |
| 794 |
|
| 795 |
static S42 foo(int x){ |
| 796 |
S42 s; |
| 797 |
|
| 798 |
s.i = x; |
| 799 |
|
| 800 |
return s; |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
void test42() |
| 805 |
{ |
| 806 |
S42[] s; |
| 807 |
|
| 808 |
s = s ~ S42.foo(6); |
| 809 |
s = s ~ S42.foo(1); |
| 810 |
|
| 811 |
if(s.length != 2){ |
| 812 |
assert(0); |
| 813 |
} |
| 814 |
if(s[0].i != 6){ |
| 815 |
assert(0); |
| 816 |
} |
| 817 |
if(s[1].i != 1){ |
| 818 |
assert(0); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
/*******************************************/ |
| 823 |
|
| 824 |
struct S43 |
| 825 |
{ |
| 826 |
int i,j; |
| 827 |
|
| 828 |
static S43 foo(int x){ |
| 829 |
S43 s; |
| 830 |
|
| 831 |
s.i = x; |
| 832 |
|
| 833 |
return s; |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
void test43() |
| 838 |
{ |
| 839 |
S43[] s; |
| 840 |
|
| 841 |
s = s ~ S43.foo(6); |
| 842 |
s = s ~ S43.foo(1); |
| 843 |
|
| 844 |
if(s.length != 2){ |
| 845 |
assert(0); |
| 846 |
} |
| 847 |
if(s[0].i != 6){ |
| 848 |
assert(0); |
| 849 |
} |
| 850 |
if(s[1].i != 1){ |
| 851 |
assert(0); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
/*******************************************/ |
| 856 |
|
| 857 |
struct S44 |
| 858 |
{ |
| 859 |
int i,j,k; |
| 860 |
|
| 861 |
static S44 foo(int x){ |
| 862 |
S44 s; |
| 863 |
|
| 864 |
s.i = x; |
| 865 |
|
| 866 |
return s; |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
void test44() |
| 871 |
{ |
| 872 |
S44[] s; |
| 873 |
|
| 874 |
s = s ~ S44.foo(6); |
| 875 |
s = s ~ S44.foo(1); |
| 876 |
|
| 877 |
if(s.length != 2){ |
| 878 |
assert(0); |
| 879 |
} |
| 880 |
if(s[0].i != 6){ |
| 881 |
assert(0); |
| 882 |
} |
| 883 |
if(s[1].i != 1){ |
| 884 |
assert(0); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
/*******************************************/ |
| 889 |
|
| 890 |
void test45() |
| 891 |
{ |
| 892 |
char[] buffer = "abcdefghijklmnopqrstuvwxyz".dup; |
| 893 |
foreach(ref char c; buffer) |
| 894 |
{ |
| 895 |
if('a' <= c && c <= 'z') |
| 896 |
{ |
| 897 |
c -= cast(char)'a' - 'A'; // segfault here |
| 898 |
} |
| 899 |
} |
| 900 |
for(int i = 0; i < buffer.length; i++) |
| 901 |
{ |
| 902 |
if('a' <= buffer[i] && buffer[i] <= 'z') |
| 903 |
{ |
| 904 |
buffer[i] -= cast(char)'a' - 'A'; // segfault here |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
assert(buffer == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 909 |
} |
| 910 |
|
| 911 |
/*******************************************/ |
| 912 |
|
| 913 |
struct st46 |
| 914 |
{ |
| 915 |
template t1() |
| 916 |
{ |
| 917 |
template t2(int n2) { } |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
alias st46.t1!().t2 a46; |
| 922 |
|
| 923 |
void test46() |
| 924 |
{ |
| 925 |
} |
| 926 |
|
| 927 |
/*******************************************/ |
| 928 |
|
| 929 |
struct A47 |
| 930 |
{ |
| 931 |
static int y; |
| 932 |
void opSliceAssign(int x) |
| 933 |
{ |
| 934 |
printf("x = %d\n", x); |
| 935 |
y = x; |
| 936 |
} |
| 937 |
A47 d() { return this; } |
| 938 |
} |
| 939 |
|
| 940 |
void test47() |
| 941 |
{ |
| 942 |
A47 a; |
| 943 |
a[] = 3; |
| 944 |
printf("y = %d\n", a.y); |
| 945 |
a.d()[] = 5; |
| 946 |
printf("y = %d\n", a.y); |
| 947 |
assert(a.y == 5); |
| 948 |
a.d[] = 6; |
| 949 |
printf("y = %d\n", a.y); |
| 950 |
assert(a.y == 6); |
| 951 |
} |
| 952 |
|
| 953 |
/*******************************************/ |
| 954 |
|
| 955 |
static uint[] sarray48 = void; |
| 956 |
|
| 957 |
void test48() |
| 958 |
{ |
| 959 |
static uint[] array = void; |
| 960 |
|
| 961 |
assert(sarray48 == null); |
| 962 |
assert(array == null); |
| 963 |
} |
| 964 |
|
| 965 |
/*******************************************/ |
| 966 |
|
| 967 |
int x = 2, y = 1; |
| 968 |
|
| 969 |
void foo50(int z) |
| 970 |
{ |
| 971 |
static int t; |
| 972 |
t++; |
| 973 |
assert(t == z); |
| 974 |
} |
| 975 |
|
| 976 |
void test50() |
| 977 |
{ |
| 978 |
printf("test50()\n"); |
| 979 |
int res = 0; |
| 980 |
for(int i = 0; i < 10; i++) |
| 981 |
{ |
| 982 |
res = res + x - y; |
| 983 |
foo50(res); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
/*******************************************/ |
| 988 |
|
| 989 |
void test52() |
| 990 |
{ |
| 991 |
printf("test52()\n"); |
| 992 |
char[] s; |
| 993 |
s = ['a', 'b', 'c']; |
| 994 |
assert(s == "abc"); |
| 995 |
|
| 996 |
int[] x; |
| 997 |
x = [17, 18u, 29, 33]; |
| 998 |
assert(x.length == 4); |
| 999 |
assert(x[0] == 17); |
| 1000 |
assert(x[1] == 18); |
| 1001 |
assert(x[2] == 29); |
| 1002 |
assert(x[3] == 33); |
| 1003 |
assert(x == [17, 18, 29, 33]); |
| 1004 |
} |
| 1005 |
|
| 1006 |
/*******************************************/ |
| 1007 |
|
| 1008 |
void test54() |
| 1009 |
{ |
| 1010 |
printf("test54()\n"); |
| 1011 |
uint[500][] data; |
| 1012 |
|
| 1013 |
data.length = 1; |
| 1014 |
assert(data.length == 1); |
| 1015 |
foreach (ref foo; data) |
| 1016 |
{ |
| 1017 |
assert(foo.length == 500); |
| 1018 |
foreach (ref u; foo) |
| 1019 |
{ //printf("u = %u\n", u); |
| 1020 |
assert(u == 0); |
| 1021 |
u = 23; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
foreach (ref foo; data) |
| 1025 |
{ |
| 1026 |
assert(foo.length == 500); |
| 1027 |
foreach (u; foo) |
| 1028 |
{ assert(u == 23); |
| 1029 |
auto v = u; |
| 1030 |
v = 23; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
/*******************************************/ |
| 1036 |
|
| 1037 |
class Base56 |
| 1038 |
{ |
| 1039 |
private string myfoo; |
| 1040 |
private string mybar; |
| 1041 |
|
| 1042 |
// Get/set properties that will be overridden. |
| 1043 |
void foo(string s) { myfoo = s; } |
| 1044 |
string foo() { return myfoo; } |
| 1045 |
|
| 1046 |
// Get/set properties that will not be overridden. |
| 1047 |
void bar(string s) { mybar = s; } |
| 1048 |
string bar() { return mybar; } |
| 1049 |
} |
| 1050 |
|
| 1051 |
class Derived56: Base56 |
| 1052 |
{ |
| 1053 |
alias Base56.foo foo; // Bring in Base56's foo getter. |
| 1054 |
override void foo(string s) { super.foo = s; } // Override foo setter. |
| 1055 |
} |
| 1056 |
|
| 1057 |
void test56() |
| 1058 |
{ |
| 1059 |
Derived56 d = new Derived56; |
| 1060 |
with (d) |
| 1061 |
{ |
| 1062 |
foo = "hi"; |
| 1063 |
d.foo = "hi"; |
| 1064 |
bar = "hi"; |
| 1065 |
assert(foo == "hi"); |
| 1066 |
assert(d.foo == "hi"); |
| 1067 |
assert(bar == "hi"); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
/*******************************************/ |
| 1072 |
|
| 1073 |
bool[void[]] reg57; |
| 1074 |
|
| 1075 |
void addToReg57(const(void)[] a, int b, bool v) |
| 1076 |
{ |
| 1077 |
if (!v) |
| 1078 |
printf("X\n"); |
| 1079 |
auto key = a~(cast(void*)&b)[0..4]; |
| 1080 |
reg57[cast(immutable(void)[])key] = v; |
| 1081 |
printf("OK\n"); |
| 1082 |
} |
| 1083 |
|
| 1084 |
void test57() |
| 1085 |
{ |
| 1086 |
addToReg57("test", 1024, true); |
| 1087 |
} |
| 1088 |
|
| 1089 |
/*******************************************/ |
| 1090 |
|
| 1091 |
int bar58( string msg ){ |
| 1092 |
return 1; |
| 1093 |
} |
| 1094 |
|
| 1095 |
int foo58( lazy string dg ){ |
| 1096 |
return bar58( dg() ); |
| 1097 |
} |
| 1098 |
|
| 1099 |
void test58() |
| 1100 |
{ |
| 1101 |
printf("test58()\n"); |
| 1102 |
try{ |
| 1103 |
} |
| 1104 |
finally{ |
| 1105 |
foo58(""); |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
/*******************************************/ |
| 1110 |
|
| 1111 |
struct S59 |
| 1112 |
{ |
| 1113 |
string toString() |
| 1114 |
{ |
| 1115 |
return "foo"; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
void test59() |
| 1120 |
{ S59 s; |
| 1121 |
|
| 1122 |
string p; |
| 1123 |
p = "s = " ~ s.toString(); |
| 1124 |
assert(p == "s = foo"); |
| 1125 |
} |
| 1126 |
|
| 1127 |
/*******************************************/ |
| 1128 |
|
| 1129 |
void test60() |
| 1130 |
{ |
| 1131 |
int[2][] a; |
| 1132 |
a = [ [-1,2], [3,4] ]; |
| 1133 |
assert(a[0][0] == -1); |
| 1134 |
assert(a[0][1] == 2); |
| 1135 |
assert(a[1][0] == 3); |
| 1136 |
assert(a[1][1] == 4); |
| 1137 |
|
| 1138 |
int[][] b; |
| 1139 |
b = [ [-1,2], [3,4] ]; |
| 1140 |
assert(b[0][0] == -1); |
| 1141 |
assert(b[0][1] == 2); |
| 1142 |
assert(b[1][0] == 3); |
| 1143 |
assert(b[1][1] == 4); |
| 1144 |
} |
| 1145 |
|
| 1146 |
/*******************************************/ |
| 1147 |
|
| 1148 |
void test61() |
| 1149 |
{ |
| 1150 |
int[][] f = [[1,2],[3,4]]; |
| 1151 |
assert(f.length == 2); |
| 1152 |
assert(f[0].length == 2); |
| 1153 |
assert(f[1].length == 2); |
| 1154 |
assert(f[0][0] == 1); |
| 1155 |
assert(f[0][1] == 2); |
| 1156 |
assert(f[1][0] == 3); |
| 1157 |
assert(f[1][1] == 4); |
| 1158 |
} |
| 1159 |
|
| 1160 |
/*******************************************/ |
| 1161 |
|
| 1162 |
struct List62 { |
| 1163 |
void get() {} |
| 1164 |
} |
| 1165 |
struct Array62 { |
| 1166 |
interface Model { |
| 1167 |
List62 list(); |
| 1168 |
} |
| 1169 |
List62 list() { |
| 1170 |
return model ? model.list() : List62.init; |
| 1171 |
} |
| 1172 |
void item() { |
| 1173 |
list.get(); |
| 1174 |
} |
| 1175 |
private Model model; |
| 1176 |
} |
| 1177 |
|
| 1178 |
void test62() |
| 1179 |
{ |
| 1180 |
} |
| 1181 |
|
| 1182 |
/*******************************************/ |
| 1183 |
|
| 1184 |
void foo63(...) |
| 1185 |
{ |
| 1186 |
} |
| 1187 |
|
| 1188 |
void test63() |
| 1189 |
{ |
| 1190 |
int[] arr; |
| 1191 |
arr = [1] ~ 2; |
| 1192 |
|
| 1193 |
// runtime crash, the length == 1 |
| 1194 |
printf("%zd\n", arr.length); |
| 1195 |
assert (arr.length == 2); |
| 1196 |
assert(arr[0] == 1); |
| 1197 |
assert(arr[1] == 2); |
| 1198 |
|
| 1199 |
arr = 2 ~ [1]; |
| 1200 |
assert(arr.length == 2); |
| 1201 |
assert(arr[0] == 2); |
| 1202 |
assert(arr[1] == 1); |
| 1203 |
|
| 1204 |
arr = [2, 3] ~ [1]; |
| 1205 |
assert(arr.length == 3); |
| 1206 |
assert(arr[0] == 2); |
| 1207 |
assert(arr[1] == 3); |
| 1208 |
assert(arr[2] == 1); |
| 1209 |
|
| 1210 |
foo63([1] ~ 2, 2 ~ [1], [1,2] ~ [3,4,5]); |
| 1211 |
} |
| 1212 |
|
| 1213 |
/*******************************************/ |
| 1214 |
|
| 1215 |
void test64() |
| 1216 |
{ |
| 1217 |
printf("test64()\n"); |
| 1218 |
int[] x = [1,2,3,4]; |
| 1219 |
int j = 4; |
| 1220 |
|
| 1221 |
foreach_reverse(v; x) |
| 1222 |
{ |
| 1223 |
assert(j == v); |
| 1224 |
j--; |
| 1225 |
} |
| 1226 |
assert(j == 0); |
| 1227 |
|
| 1228 |
j = 4; |
| 1229 |
foreach_reverse(i, v; x) |
| 1230 |
{ |
| 1231 |
assert(i + 1 == j); |
| 1232 |
assert(j == v); |
| 1233 |
j--; |
| 1234 |
} |
| 1235 |
assert(j == 0); |
| 1236 |
printf("-test64()\n"); |
| 1237 |
} |
| 1238 |
|
| 1239 |
/*******************************************/ |
| 1240 |
|
| 1241 |
void test65() |
| 1242 |
{ |
| 1243 |
// Bugzilla Issue 407. |
| 1244 |
int i = *cast(int*)cast(char[4])['0', '0', '0', '0']; // compiler seg-fault |
| 1245 |
printf("i = %x\n", i); |
| 1246 |
} |
| 1247 |
|
| 1248 |
/*******************************************/ |
| 1249 |
|
| 1250 |
void test66() |
| 1251 |
{ |
| 1252 |
int[] ia; |
| 1253 |
ia ~= 3; |
| 1254 |
byte[] data = new byte[ia[0]]; |
| 1255 |
byte[] data2 = new byte[ cast(int)(ia[0])]; |
| 1256 |
} |
| 1257 |
|
| 1258 |
/*******************************************/ |
| 1259 |
|
| 1260 |
class C68 |
| 1261 |
{ |
| 1262 |
static int value; |
| 1263 |
} |
| 1264 |
|
| 1265 |
void test68() |
| 1266 |
{ |
| 1267 |
auto v1 = test.C68.value; |
| 1268 |
auto v2 = C68.classinfo; |
| 1269 |
auto v3 = test.C68.classinfo; |
| 1270 |
assert(v2 == v3); |
| 1271 |
} |
| 1272 |
|
| 1273 |
/*******************************************/ |
| 1274 |
|
| 1275 |
void test69() |
| 1276 |
{ |
| 1277 |
class Bug |
| 1278 |
{ |
| 1279 |
char[12] str = ""; |
| 1280 |
uint t = 1; |
| 1281 |
} |
| 1282 |
|
| 1283 |
class NoBug |
| 1284 |
{ |
| 1285 |
uint t = 2; |
| 1286 |
char[12] str = ""; |
| 1287 |
} |
| 1288 |
|
| 1289 |
class NoBug2 |
| 1290 |
{ |
| 1291 |
char[12] str; |
| 1292 |
uint t = 3; |
| 1293 |
} |
| 1294 |
|
| 1295 |
auto b = new Bug; |
| 1296 |
auto n = new NoBug; |
| 1297 |
auto n2 = new NoBug2; |
| 1298 |
|
| 1299 |
printf("bug %d\n", b.t); |
| 1300 |
assert(b.t == 1); |
| 1301 |
printf("nobug %d\n", n.t); |
| 1302 |
assert(n.t == 2); |
| 1303 |
printf("nobug2 %d\n", n2.t); |
| 1304 |
assert(n2.t == 3); |
| 1305 |
} |
| 1306 |
|
| 1307 |
/*******************************************/ |
| 1308 |
|
| 1309 |
void test70() |
| 1310 |
{ |
| 1311 |
void foo(char[0] p) |
| 1312 |
{ |
| 1313 |
} |
| 1314 |
|
| 1315 |
static const char[0] altsep; |
| 1316 |
foo(altsep); |
| 1317 |
} |
| 1318 |
|
| 1319 |
/*******************************************/ |
| 1320 |
|
| 1321 |
|
| 1322 |
class C71 |
| 1323 |
{ |
| 1324 |
static int cnt; |
| 1325 |
this() scope { printf("C()\n"); cnt++; } |
| 1326 |
~this() scope { printf("~C()\n"); cnt--; } |
| 1327 |
} |
| 1328 |
|
| 1329 |
class D71 |
| 1330 |
{ |
| 1331 |
static int cnt; |
| 1332 |
this() scope { printf("D()\n"); cnt++; } |
| 1333 |
~this() scope { printf("~D()\n"); cnt--; } |
| 1334 |
} |
| 1335 |
|
| 1336 |
class E71 |
| 1337 |
{ |
| 1338 |
static int cnt; |
| 1339 |
this() scope { printf("E()\n"); cnt++; } |
| 1340 |
~this() scope { printf("~E()\n"); cnt--; } |
| 1341 |
} |
| 1342 |
|
| 1343 |
void test71() |
| 1344 |
{ |
| 1345 |
{ |
| 1346 |
int i = 0; |
| 1347 |
printf("start\n"); |
| 1348 |
scope D71 d = new D71(); |
| 1349 |
assert(D71.cnt == 1); |
| 1350 |
for (scope E71 e = new E71(); i < 5; i++) |
| 1351 |
{ |
| 1352 |
assert(D71.cnt == 1); |
| 1353 |
assert(E71.cnt == 1); |
| 1354 |
scope c = new C71(); |
| 1355 |
assert(C71.cnt == 1); |
| 1356 |
} |
| 1357 |
assert(C71.cnt == 0); |
| 1358 |
assert(E71.cnt == 0); |
| 1359 |
assert(D71.cnt == 1); |
| 1360 |
printf("finish\n"); |
| 1361 |
} |
| 1362 |
assert(D71.cnt == 0); |
| 1363 |
} |
| 1364 |
|
| 1365 |
/*******************************************/ |
| 1366 |
|
| 1367 |
size_t getLength(int[] arr) { return arr.length; } |
| 1368 |
|
| 1369 |
void test13237() |
| 1370 |
{ |
| 1371 |
int[] arr = [0]; |
| 1372 |
immutable size_t len = getLength(arr); |
| 1373 |
|
| 1374 |
arr.length--; |
| 1375 |
|
| 1376 |
assert(len == 1); // ok |
| 1377 |
if (len) { auto l = len; } |
| 1378 |
assert(len == 1); // len cannot be changed, but produces Assertion failure with "-O -inline" |
| 1379 |
} |
| 1380 |
|
| 1381 |
/*******************************************/ |
| 1382 |
|
| 1383 |
void main() |
| 1384 |
{ |
| 1385 |
test1(); |
| 1386 |
test2(); |
| 1387 |
test3(); |
| 1388 |
test5(); |
| 1389 |
test6(); |
| 1390 |
test7(); |
| 1391 |
test8(); |
| 1392 |
test9(); |
| 1393 |
test10(); |
| 1394 |
test11(); |
| 1395 |
test12(); |
| 1396 |
test13(); |
| 1397 |
test14(); |
| 1398 |
test15(); |
| 1399 |
test17(); |
| 1400 |
test18(); |
| 1401 |
test19(); |
| 1402 |
test20(); |
| 1403 |
test21(); |
| 1404 |
test22(); |
| 1405 |
test23(); |
| 1406 |
test24(); |
| 1407 |
test25(); |
| 1408 |
test26(); |
| 1409 |
test27(); |
| 1410 |
test28(); |
| 1411 |
test29(); |
| 1412 |
test30(); |
| 1413 |
test31(); |
| 1414 |
|
| 1415 |
|
| 1416 |
test34(); |
| 1417 |
test37(); |
| 1418 |
test38(); |
| 1419 |
test39(); |
| 1420 |
test40(); |
| 1421 |
test41(); |
| 1422 |
test42(); |
| 1423 |
test43(); |
| 1424 |
test44(); |
| 1425 |
test45(); |
| 1426 |
test46(); |
| 1427 |
test47(); |
| 1428 |
test48(); |
| 1429 |
test50(); |
| 1430 |
test52(); |
| 1431 |
test54(); |
| 1432 |
test56(); |
| 1433 |
test57(); |
| 1434 |
test58(); |
| 1435 |
test59(); |
| 1436 |
test60(); |
| 1437 |
test61(); |
| 1438 |
test62(); |
| 1439 |
test63(); |
| 1440 |
test64(); |
| 1441 |
test65(); |
| 1442 |
test66(); |
| 1443 |
test68(); |
| 1444 |
test69(); |
| 1445 |
test70(); |
| 1446 |
test71(); |
| 1447 |
test13237(); |
| 1448 |
|
| 1449 |
printf("Success\n"); |
| 1450 |
} |