| 1 |
import core.vararg; |
| 2 |
|
| 3 |
extern(C) int printf(const char*, ...); |
| 4 |
|
| 5 |
/*****************************************/ |
| 6 |
|
| 7 |
class A1 |
| 8 |
{ |
| 9 |
union |
| 10 |
{ |
| 11 |
struct |
| 12 |
{ |
| 13 |
int x; |
| 14 |
public int y; |
| 15 |
} |
| 16 |
protected int z; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
class A2 |
| 21 |
{ |
| 22 |
struct |
| 23 |
{ |
| 24 |
int x; |
| 25 |
public int y; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
class A3 |
| 30 |
{ |
| 31 |
union |
| 32 |
{ |
| 33 |
int x; |
| 34 |
public int y; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
void test1() |
| 39 |
{ |
| 40 |
A1 a1 = new A1(); |
| 41 |
A2 a2 = new A2(); |
| 42 |
A3 a3 = new A3(); |
| 43 |
|
| 44 |
a1.x = 1; |
| 45 |
a1.y = 2; |
| 46 |
a1.z = 3; |
| 47 |
assert(a1.x == 3); |
| 48 |
assert(a1.y == 2); |
| 49 |
|
| 50 |
a2.x = 1; |
| 51 |
a2.y = 2; |
| 52 |
assert(a2.x == 1); |
| 53 |
|
| 54 |
a3.x = 1; |
| 55 |
a3.y = 2; |
| 56 |
assert(a3.x == 2); |
| 57 |
} |
| 58 |
|
| 59 |
/*****************************************/ |
| 60 |
|
| 61 |
struct A4 |
| 62 |
{ |
| 63 |
union |
| 64 |
{ |
| 65 |
struct |
| 66 |
{ |
| 67 |
int x = 13; |
| 68 |
} |
| 69 |
int y; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void test4() |
| 74 |
{ |
| 75 |
printf("A4.sizeof = %zd\n", A4.sizeof); |
| 76 |
assert(A4.sizeof == 1 * int.sizeof); |
| 77 |
|
| 78 |
A4 q; |
| 79 |
assert(q.y == 13); |
| 80 |
} |
| 81 |
|
| 82 |
/*****************************************/ |
| 83 |
|
| 84 |
class A5 |
| 85 |
{ |
| 86 |
union { |
| 87 |
struct { |
| 88 |
int x; |
| 89 |
} |
| 90 |
public int y; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void test5() |
| 95 |
{ |
| 96 |
A5 a = new A5; |
| 97 |
|
| 98 |
a.x = 3; |
| 99 |
a.y = 4; |
| 100 |
assert(a.x == 4); |
| 101 |
} |
| 102 |
|
| 103 |
/*****************************************/ |
| 104 |
|
| 105 |
int i6 = 5; |
| 106 |
|
| 107 |
float m6 = 5.0; |
| 108 |
|
| 109 |
void test6() |
| 110 |
{ |
| 111 |
void f() |
| 112 |
{ |
| 113 |
void i6(float j) |
| 114 |
{ |
| 115 |
m6 = j; |
| 116 |
} |
| 117 |
|
| 118 |
void g() |
| 119 |
{ |
| 120 |
i6 = 1; |
| 121 |
} |
| 122 |
g(); |
| 123 |
} |
| 124 |
|
| 125 |
f(); |
| 126 |
|
| 127 |
printf( "%d %f\n", i6, m6); |
| 128 |
assert(i6 == 5); |
| 129 |
assert(m6 == 1); |
| 130 |
} |
| 131 |
|
| 132 |
/*****************************************/ |
| 133 |
|
| 134 |
const int a1 = 50; |
| 135 |
const int a2 = 50; |
| 136 |
const int a3 = a1 * a2; |
| 137 |
|
| 138 |
const int b1 = a1 - 1; |
| 139 |
const int b2 = a2 - 1; |
| 140 |
|
| 141 |
const int c1 = 50*50; |
| 142 |
const int c2 = (a1-1)*(a2-1); |
| 143 |
const int c3 = b1*b2; |
| 144 |
|
| 145 |
int[4*c1] array1; // illegal! |
| 146 |
int[4*c2] array2; // illegal! |
| 147 |
int[4*c3] array3; // illegal! |
| 148 |
|
| 149 |
int[a3] array4; // valid! no error! |
| 150 |
|
| 151 |
void test7() |
| 152 |
{ |
| 153 |
assert(a1 == 50); |
| 154 |
assert(a2 == 50); |
| 155 |
assert(a3 == 2500); |
| 156 |
|
| 157 |
assert(b1 == 49); |
| 158 |
assert(b2 == 49); |
| 159 |
|
| 160 |
assert(c1 == 2500); |
| 161 |
assert(c2 == 2401); |
| 162 |
assert(c3 == 2401); |
| 163 |
|
| 164 |
assert(array1.length == 10000); |
| 165 |
assert(array2.length == 9604); |
| 166 |
assert(array3.length == 9604); |
| 167 |
} |
| 168 |
|
| 169 |
/*****************************************/ |
| 170 |
|
| 171 |
struct Foo8 |
| 172 |
{ |
| 173 |
static Foo8 bar() |
| 174 |
{ |
| 175 |
Foo8 res; |
| 176 |
return res; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
void test8() |
| 181 |
{ |
| 182 |
Foo8[8] x; |
| 183 |
x[] = Foo8.bar; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
/*****************************************/ |
| 188 |
|
| 189 |
void test9() |
| 190 |
{ |
| 191 |
try { |
| 192 |
} catch (Exception e) { |
| 193 |
debug printf("Exception happened\n"); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/*****************************************/ |
| 198 |
|
| 199 |
struct Foo10 { |
| 200 |
const bool opEquals(const ref Foo10 x) { |
| 201 |
return this.normalize is x.normalize; |
| 202 |
} |
| 203 |
const Foo10 normalize() { |
| 204 |
Foo10 res; |
| 205 |
return res; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void test10() |
| 210 |
{ |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
/*****************************************/ |
| 215 |
|
| 216 |
class T11 |
| 217 |
{ |
| 218 |
this(){} |
| 219 |
~this(){} |
| 220 |
} |
| 221 |
|
| 222 |
void test11() |
| 223 |
{ |
| 224 |
scope T11 t=new T11(); |
| 225 |
int i=1; |
| 226 |
switch(i) |
| 227 |
{ |
| 228 |
case 1: |
| 229 |
break; |
| 230 |
|
| 231 |
default: |
| 232 |
break; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/*****************************************/ |
| 237 |
|
| 238 |
void test12() |
| 239 |
{ |
| 240 |
char[] s; |
| 241 |
char[] t; |
| 242 |
|
| 243 |
if (true) |
| 244 |
s = null; |
| 245 |
s = (true) ? null : t; |
| 246 |
t = (true) ? s : null; |
| 247 |
} |
| 248 |
|
| 249 |
/*****************************************/ |
| 250 |
|
| 251 |
class Foo13 |
| 252 |
{ |
| 253 |
int init (int x) { return 1; } |
| 254 |
|
| 255 |
static int init (long y) { return 2; } |
| 256 |
} |
| 257 |
|
| 258 |
void test13() |
| 259 |
{ |
| 260 |
Foo13 f = new Foo13(); |
| 261 |
int i; |
| 262 |
|
| 263 |
i = f.init(1); |
| 264 |
assert(i == 1); |
| 265 |
i = f.init(1L); |
| 266 |
assert(i == 2); |
| 267 |
} |
| 268 |
|
| 269 |
/*****************************************/ |
| 270 |
|
| 271 |
void write14(bool[] c) |
| 272 |
{ |
| 273 |
printf("[%2zd]: ", c.length); |
| 274 |
foreach (bool x; c) |
| 275 |
printf("%d,", x); |
| 276 |
printf("\n"); |
| 277 |
} |
| 278 |
|
| 279 |
void test14() |
| 280 |
{ |
| 281 |
static bool[] a = [ 1, 1, 0, 1, 0 ]; |
| 282 |
static bool[] b = [ 1, 0, 0, 1 ]; |
| 283 |
bool[] c = a ~ b; |
| 284 |
|
| 285 |
static bool[] r1 = [1,1,0,1,0,1,0,0,1]; |
| 286 |
static bool[] r2 = [1,1,0,1,0,1,0,0,1,1,1,0,1,0]; |
| 287 |
static bool[] r3 = [1,1,0,1,0,1,0,0,1,1,1,0,1,0,0]; |
| 288 |
static bool[] r4 = [1,1,0,1,0,1,0,0,1,1,1,0,1,0,0,1]; |
| 289 |
|
| 290 |
|
| 291 |
write14(c); |
| 292 |
assert(c == r1); |
| 293 |
|
| 294 |
c ~= a; |
| 295 |
write14(c); |
| 296 |
assert(c == r2); |
| 297 |
|
| 298 |
c ~= 0; |
| 299 |
write14(c); |
| 300 |
assert(c == r3); |
| 301 |
|
| 302 |
c ~= 1; |
| 303 |
write14(c); |
| 304 |
assert(c == r4); |
| 305 |
} |
| 306 |
|
| 307 |
/*****************************************/ |
| 308 |
|
| 309 |
void test15() |
| 310 |
{ |
| 311 |
bool[] b; |
| 312 |
bool[] c; |
| 313 |
|
| 314 |
b.length = 10; |
| 315 |
c = b[0..4]; |
| 316 |
c[] = true; |
| 317 |
|
| 318 |
assert(b[0] == true); |
| 319 |
assert(b[1] == true); |
| 320 |
assert(b[2] == true); |
| 321 |
assert(b[3] == true); |
| 322 |
|
| 323 |
assert(b[4] == false); |
| 324 |
assert(b[5] == false); |
| 325 |
assert(b[6] == false); |
| 326 |
assert(b[7] == false); |
| 327 |
assert(b[8] == false); |
| 328 |
assert(b[9] == false); |
| 329 |
} |
| 330 |
|
| 331 |
/*****************************************/ |
| 332 |
|
| 333 |
int y16; |
| 334 |
|
| 335 |
class C16 |
| 336 |
{ |
| 337 |
int x; |
| 338 |
this() |
| 339 |
{ |
| 340 |
x = 4; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
void test16() |
| 345 |
{ |
| 346 |
C16 c = new C16(); |
| 347 |
assert(c.x == 4); |
| 348 |
} |
| 349 |
|
| 350 |
/*****************************************/ |
| 351 |
|
| 352 |
ubyte* ptr17; |
| 353 |
|
| 354 |
void test17() |
| 355 |
{ |
| 356 |
ubyte[16] foo; |
| 357 |
|
| 358 |
printf("foo = %p\n", foo.ptr); |
| 359 |
ptr17 = foo.ptr; |
| 360 |
abc17(foo); |
| 361 |
} |
| 362 |
|
| 363 |
void abc17(ref ubyte[16] bar) |
| 364 |
{ |
| 365 |
printf("bar = %p\n", bar.ptr); |
| 366 |
assert(bar.ptr == ptr17); |
| 367 |
} |
| 368 |
|
| 369 |
/*****************************************/ |
| 370 |
|
| 371 |
struct Iterator18(T) |
| 372 |
{ |
| 373 |
T* m_ptr; |
| 374 |
|
| 375 |
const bool opEquals(const ref Iterator18 iter) |
| 376 |
{ |
| 377 |
return (m_ptr == iter.m_ptr); |
| 378 |
} |
| 379 |
|
| 380 |
const int opCmp(const ref Iterator18 iter) |
| 381 |
{ |
| 382 |
return cast(int)(m_ptr - iter.m_ptr); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
void test18() |
| 387 |
{ |
| 388 |
Iterator18!(int) iter; |
| 389 |
} |
| 390 |
|
| 391 |
/*****************************************/ |
| 392 |
|
| 393 |
struct S29(T) |
| 394 |
{ |
| 395 |
const bool opEquals(const ref S29!(T) len2) |
| 396 |
{ |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
const int opCmp(const ref S29!(T) len2) |
| 401 |
{ |
| 402 |
return 0; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
void test19() |
| 408 |
{ |
| 409 |
TypeInfo info = typeid(S29!(int)); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
/*****************************************/ |
| 414 |
|
| 415 |
class Mapped : Buffer |
| 416 |
{ |
| 417 |
this() |
| 418 |
{ |
| 419 |
} |
| 420 |
|
| 421 |
~this() |
| 422 |
{ |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
class Buffer |
| 427 |
{ |
| 428 |
private uint limit; |
| 429 |
private uint capacity; |
| 430 |
private uint position; |
| 431 |
|
| 432 |
invariant() |
| 433 |
{ |
| 434 |
assert (position <= limit); |
| 435 |
assert (limit <= capacity); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
void test20() |
| 440 |
{ |
| 441 |
Buffer b = new Buffer(); |
| 442 |
destroy(b); |
| 443 |
} |
| 444 |
|
| 445 |
/*****************************************/ |
| 446 |
|
| 447 |
class T21 |
| 448 |
{ |
| 449 |
char[1] p1; |
| 450 |
char[1] p2; |
| 451 |
public: |
| 452 |
char[] P() {return p1~p2;} |
| 453 |
} |
| 454 |
|
| 455 |
void test21() |
| 456 |
{ |
| 457 |
T21 t = new T21; |
| 458 |
t.p1[0] = 'a'; |
| 459 |
t.p2[0] = 'b'; |
| 460 |
assert(t.P() == "ab"); |
| 461 |
} |
| 462 |
|
| 463 |
/*****************************************/ |
| 464 |
|
| 465 |
void test22() |
| 466 |
{ |
| 467 |
struct foo1 { int a; int b; int c; } |
| 468 |
|
| 469 |
class foo2 |
| 470 |
{ |
| 471 |
foo1[] x; |
| 472 |
|
| 473 |
foo1 fooret(foo1 foo2) |
| 474 |
{ |
| 475 |
x.length = 9; |
| 476 |
return x[0] = foo2; // Here |
| 477 |
|
| 478 |
// x[0] = foo2; --- This version does not |
| 479 |
// return x[0]; --- cause the error. |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
/*****************************************/ |
| 486 |
|
| 487 |
interface I24 { } |
| 488 |
|
| 489 |
void test24() |
| 490 |
{ |
| 491 |
} |
| 492 |
|
| 493 |
/*****************************************/ |
| 494 |
|
| 495 |
interface D25{} |
| 496 |
interface C25:D25{} |
| 497 |
interface B25:C25{} |
| 498 |
interface A25:B25{} |
| 499 |
|
| 500 |
void test25() |
| 501 |
{ |
| 502 |
} |
| 503 |
|
| 504 |
class A26:B26{} |
| 505 |
interface B26:C26{} |
| 506 |
interface C26:D26{} |
| 507 |
interface D26{} |
| 508 |
|
| 509 |
void test26() |
| 510 |
{ |
| 511 |
} |
| 512 |
|
| 513 |
interface A27:B27{} |
| 514 |
interface B27:C27{} |
| 515 |
interface C27:D27{} |
| 516 |
interface D27{} |
| 517 |
|
| 518 |
void test27() |
| 519 |
{ |
| 520 |
} |
| 521 |
|
| 522 |
/*****************************************/ |
| 523 |
|
| 524 |
void test28() |
| 525 |
{ |
| 526 |
const double d = -1.0; |
| 527 |
int i = cast(int)d; |
| 528 |
printf("i = %d\n", i); |
| 529 |
assert(-1 == i); |
| 530 |
} |
| 531 |
|
| 532 |
/*****************************************/ |
| 533 |
|
| 534 |
static int[1][5] array = [[1],[2],[3],[4],[5] ]; |
| 535 |
|
| 536 |
void Lookup( int which ) |
| 537 |
{ |
| 538 |
switch( which ) |
| 539 |
{ |
| 540 |
case 0 : return cast(void)array[which]; |
| 541 |
default: assert(0); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
void test29() |
| 546 |
{ |
| 547 |
} |
| 548 |
|
| 549 |
/*****************************************/ |
| 550 |
|
| 551 |
void foo31(...) |
| 552 |
{ |
| 553 |
byte b = va_arg!byte(_argptr); |
| 554 |
assert(b == 8); |
| 555 |
} |
| 556 |
|
| 557 |
void test31() |
| 558 |
{ |
| 559 |
byte x = 9; |
| 560 |
foo31(--x); |
| 561 |
--x; |
| 562 |
foo31(++x); |
| 563 |
} |
| 564 |
|
| 565 |
/*****************************************/ |
| 566 |
|
| 567 |
template Foo33(T, int L) |
| 568 |
{ |
| 569 |
T[L] arr; |
| 570 |
class Bar { |
| 571 |
int before = 6; |
| 572 |
T[L] arr; |
| 573 |
int after = 7; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
void test33() |
| 578 |
{ |
| 579 |
alias Foo33!(int, 100) foo; |
| 580 |
foreach (int x; foo.arr) |
| 581 |
assert(x == int.init); |
| 582 |
|
| 583 |
foo.Bar bar = new foo.Bar(); |
| 584 |
foreach (int x; bar.arr) |
| 585 |
{ //printf("%d\n", x); |
| 586 |
assert(x == int.init); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
/*****************************************/ |
| 592 |
|
| 593 |
void test34() |
| 594 |
{ |
| 595 |
bool[1] a; |
| 596 |
bool[1] b; |
| 597 |
|
| 598 |
bool[] concat() { |
| 599 |
return a~b; |
| 600 |
} |
| 601 |
|
| 602 |
a[]=0; |
| 603 |
b[]=1; |
| 604 |
|
| 605 |
bool[] arr=concat(); |
| 606 |
|
| 607 |
assert(arr.length==2); |
| 608 |
assert(arr[0]==0); |
| 609 |
assert(arr[1]==1); |
| 610 |
} |
| 611 |
|
| 612 |
/*****************************************/ |
| 613 |
|
| 614 |
void dummy35(...) {} |
| 615 |
|
| 616 |
void test35() |
| 617 |
{ |
| 618 |
byte x = 9; |
| 619 |
dummy35(x); |
| 620 |
int y = --x; |
| 621 |
assert (y == 8); |
| 622 |
assert (x == 8); |
| 623 |
} |
| 624 |
|
| 625 |
/*****************************************/ |
| 626 |
|
| 627 |
void test36() |
| 628 |
{ |
| 629 |
int[] a; |
| 630 |
a.length = 2; |
| 631 |
a[0]=1; |
| 632 |
a[1]=2; |
| 633 |
|
| 634 |
int[] b; |
| 635 |
b.length = 1; |
| 636 |
b[0]=3; |
| 637 |
|
| 638 |
a~=b; |
| 639 |
|
| 640 |
assert(a.length==3); |
| 641 |
assert(a[0]==1); |
| 642 |
assert(a[1]==2); |
| 643 |
assert(a[2]==3); |
| 644 |
|
| 645 |
assert(b.length==1); |
| 646 |
assert(b[0]==3); |
| 647 |
} |
| 648 |
|
| 649 |
/*****************************************/ |
| 650 |
|
| 651 |
struct Range{ |
| 652 |
int width(){ |
| 653 |
return 1; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
class Container { |
| 658 |
Range opIndex(int i){ |
| 659 |
return data[i]; |
| 660 |
} |
| 661 |
|
| 662 |
Range[2] data; |
| 663 |
} |
| 664 |
|
| 665 |
void test37() |
| 666 |
{ |
| 667 |
Container ranges=new Container; |
| 668 |
|
| 669 |
// fails with -inline |
| 670 |
// assert(ranges[0].width() == 1); |
| 671 |
} |
| 672 |
|
| 673 |
/*****************************************/ |
| 674 |
|
| 675 |
void test38() |
| 676 |
{ |
| 677 |
uint mask = (uint.max >> 1); |
| 678 |
assert(mask == (uint.max >> 1)); |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
/*****************************************/ |
| 684 |
|
| 685 |
void test41() |
| 686 |
{ |
| 687 |
assert(new void[40] == new void[40]); |
| 688 |
} |
| 689 |
|
| 690 |
/*****************************************/ |
| 691 |
|
| 692 |
void test42() |
| 693 |
{ |
| 694 |
static ubyte[] master = [ |
| 695 |
0xE3u, 0x83u, 0xAFu, 0xE3u, 0x83u, 0xADu, 0xE3u, 0x82u, |
| 696 |
0xB9u, 0xEFu, 0xBDu, 0x97u |
| 697 |
]; |
| 698 |
|
| 699 |
string string1 = "ワロスw"; |
| 700 |
string string2 = r"ワロスw"; |
| 701 |
string string3 = `ワロスw`; |
| 702 |
string string4 = "\xE3\x83\xAF\xE3\x83\xAD\xE3\x82\xB9\xEF\xBD\x97"; |
| 703 |
|
| 704 |
assert(string1.length==master.length); |
| 705 |
|
| 706 |
for(int i=0; i<master.length; i++){ |
| 707 |
assert(string1[i]==master[i]); |
| 708 |
} |
| 709 |
|
| 710 |
assert(string2.length==master.length); |
| 711 |
|
| 712 |
for(int i=0; i<master.length; i++){ |
| 713 |
assert(string2[i]==master[i]); |
| 714 |
} |
| 715 |
|
| 716 |
assert(string3.length==master.length); |
| 717 |
|
| 718 |
for(int i=0; i<master.length; i++){ |
| 719 |
assert(string3[i]==master[i]); |
| 720 |
} |
| 721 |
|
| 722 |
assert(string4.length==master.length); |
| 723 |
|
| 724 |
for(int i=0; i<master.length; i++){ |
| 725 |
assert(string4[i]==master[i]); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
/*****************************************/ |
| 730 |
|
| 731 |
struct S43 |
| 732 |
{ |
| 733 |
int i=1; |
| 734 |
} |
| 735 |
|
| 736 |
static S43[3] s= [ 1: { 2 } ]; |
| 737 |
|
| 738 |
void test43() |
| 739 |
{ |
| 740 |
assert(s[0].i==1); |
| 741 |
assert(s[1].i==2); |
| 742 |
assert(s[2].i==1); |
| 743 |
} |
| 744 |
|
| 745 |
/*****************************************/ |
| 746 |
|
| 747 |
struct S44 |
| 748 |
{ |
| 749 |
int i; |
| 750 |
} |
| 751 |
|
| 752 |
static S44[12] S44s = []; |
| 753 |
|
| 754 |
void test44() |
| 755 |
{ |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
/*****************************************/ |
| 760 |
|
| 761 |
struct S45 |
| 762 |
{ |
| 763 |
double x = 0, y = 0; |
| 764 |
static S45 opCall(int i) { S45 r; r.x = i; return r; } |
| 765 |
S45 opBinary(string op)(S45 s) if (op == "*") |
| 766 |
{ |
| 767 |
S45 r; |
| 768 |
r.x = x * s.x; |
| 769 |
r.y = y * s.y; |
| 770 |
return r; |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
template sqr(F) { F sqr(F x) { return x * x; } } |
| 775 |
|
| 776 |
template pow(F) |
| 777 |
{ |
| 778 |
F pow(F x, int i) { |
| 779 |
if (i < 1) |
| 780 |
{ |
| 781 |
static if (is(F : real)) |
| 782 |
return 1; |
| 783 |
else |
| 784 |
return F(1); |
| 785 |
} |
| 786 |
if (i & 1) |
| 787 |
{ |
| 788 |
if (i == 1) |
| 789 |
return x; |
| 790 |
else |
| 791 |
return x * pow(x,i-1); |
| 792 |
} |
| 793 |
return sqr!(F)(pow(x,i/2)); |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
void test45() |
| 798 |
{ |
| 799 |
S45 s = S45(10); |
| 800 |
S45 val = pow!(S45)(s,2); |
| 801 |
printf("x = %2.2f, y = %2.2f\n", val.x, val.y); |
| 802 |
assert(val.x == 100); |
| 803 |
assert(val.y == 0); |
| 804 |
double d = pow!(double)(10,3); |
| 805 |
printf("%2.2f\n", d); |
| 806 |
assert(d == 1000); |
| 807 |
} |
| 808 |
|
| 809 |
/*****************************************/ |
| 810 |
|
| 811 |
struct Node46 |
| 812 |
{ |
| 813 |
Node46* left,right,parent; |
| 814 |
int color = 5; |
| 815 |
char[] key; |
| 816 |
char[] value; |
| 817 |
} |
| 818 |
|
| 819 |
void test46() |
| 820 |
{ |
| 821 |
Node46* x = new Node46; |
| 822 |
assert( x.left is null ); |
| 823 |
assert( x.right is null ); |
| 824 |
assert( x.parent is null ); |
| 825 |
assert( x.color is 5 ); |
| 826 |
assert( x.key is null ); |
| 827 |
assert( x.value is null ); |
| 828 |
} |
| 829 |
|
| 830 |
/*****************************************/ |
| 831 |
|
| 832 |
string foo48(string s) |
| 833 |
{ |
| 834 |
return s; |
| 835 |
} |
| 836 |
|
| 837 |
void test48() |
| 838 |
{ |
| 839 |
char c = '?'; |
| 840 |
string s; |
| 841 |
|
| 842 |
s = foo48("is this it" ~ c ~ " yep!"); |
| 843 |
assert(s == "is this it? yep!"); |
| 844 |
|
| 845 |
s = foo48("is this it" ~ c); |
| 846 |
assert(s == "is this it?"); |
| 847 |
|
| 848 |
s = foo48(c ~ "is this it"); |
| 849 |
assert(s == "?is this it"); |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
/*****************************************/ |
| 854 |
|
| 855 |
char[10] foo49 = void; |
| 856 |
|
| 857 |
void test49() |
| 858 |
{ |
| 859 |
int i = void; |
| 860 |
//printf("i = %d\n", i); |
| 861 |
int[10] a; |
| 862 |
foreach (int x; a) |
| 863 |
{ |
| 864 |
printf("\tx = %d\n", x); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
/*****************************************/ |
| 869 |
|
| 870 |
void foo50(string s) { assert(s == "is this it? yep!"); } |
| 871 |
|
| 872 |
void test50() |
| 873 |
{ |
| 874 |
char c = '?'; |
| 875 |
foo50("is this it"~c~" yep!"); |
| 876 |
} |
| 877 |
|
| 878 |
/*****************************************/ |
| 879 |
|
| 880 |
void test51() |
| 881 |
{ |
| 882 |
bool[9][3] qwert; |
| 883 |
|
| 884 |
printf("qwert.sizeof = %zd\n", qwert.sizeof); |
| 885 |
|
| 886 |
for (int i = 0; i < 3; i++) |
| 887 |
for (int j = 0; j < 9; j++) |
| 888 |
assert(qwert[i][j] == false); |
| 889 |
|
| 890 |
version (D_Bits) |
| 891 |
assert(qwert.sizeof == 12); |
| 892 |
else |
| 893 |
assert(qwert.sizeof == 27); |
| 894 |
|
| 895 |
qwert[0][3] = true; |
| 896 |
qwert[0][5] = true; |
| 897 |
qwert[0][7] = true; |
| 898 |
|
| 899 |
qwert[1..3] = qwert[0]; |
| 900 |
|
| 901 |
for (int i = 0; i < 3; i++) |
| 902 |
{ |
| 903 |
assert(qwert[i][0] == false); |
| 904 |
assert(qwert[i][1] == false); |
| 905 |
assert(qwert[i][2] == false); |
| 906 |
assert(qwert[i][3] == true); |
| 907 |
assert(qwert[i][4] == false); |
| 908 |
assert(qwert[i][5] == true); |
| 909 |
assert(qwert[i][6] == false); |
| 910 |
assert(qwert[i][7] == true); |
| 911 |
assert(qwert[i][8] == false); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
/*****************************************/ |
| 917 |
|
| 918 |
void test52() |
| 919 |
{ |
| 920 |
size_t vsize = void.sizeof; |
| 921 |
assert(vsize == 1); |
| 922 |
} |
| 923 |
|
| 924 |
/*****************************************/ |
| 925 |
|
| 926 |
const char[3][13] month = [ |
| 927 |
1: "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 928 |
8: "Aug", "Sep", "Oct", "Nov", "Dec" |
| 929 |
]; |
| 930 |
|
| 931 |
void test53() |
| 932 |
{ |
| 933 |
printf("%.*s\n", cast(int)month[1].length, month[1].ptr); |
| 934 |
printf("%.*s\n", cast(int)month[2].length, month[2].ptr); |
| 935 |
printf("%.*s\n", cast(int)month[3].length, month[3].ptr); |
| 936 |
printf("%.*s\n", cast(int)month[4].length, month[4].ptr); |
| 937 |
printf("%.*s\n", cast(int)month[5].length, month[5].ptr); |
| 938 |
printf("%.*s\n", cast(int)month[6].length, month[6].ptr); |
| 939 |
printf("%.*s\n", cast(int)month[8].length, month[8].ptr); |
| 940 |
|
| 941 |
assert(month[1] == "Jan"); |
| 942 |
assert(month[2] == "Feb"); |
| 943 |
assert(month[3] == "Mar"); |
| 944 |
assert(month[4] == "Apr"); |
| 945 |
assert(month[5] == "May"); |
| 946 |
assert(month[6] == "Jun"); |
| 947 |
assert(month[8] == "Aug"); |
| 948 |
assert(month[9] == "Sep"); |
| 949 |
assert(month[10] == "Oct"); |
| 950 |
assert(month[11] == "Nov"); |
| 951 |
assert(month[12] == "Dec"); |
| 952 |
|
| 953 |
assert(month[0][0] == 0xFF); |
| 954 |
assert(month[0][1] == 0xFF); |
| 955 |
assert(month[0][2] == 0xFF); |
| 956 |
|
| 957 |
assert(month[7][0] == 0xFF); |
| 958 |
assert(month[7][1] == 0xFF); |
| 959 |
assert(month[7][2] == 0xFF); |
| 960 |
} |
| 961 |
|
| 962 |
/*****************************************/ |
| 963 |
|
| 964 |
struct S54 |
| 965 |
{ |
| 966 |
static S54 foo() |
| 967 |
{ |
| 968 |
S54 s; |
| 969 |
with (s) {} // bug |
| 970 |
return s; |
| 971 |
} |
| 972 |
|
| 973 |
static S54 bar() |
| 974 |
{ |
| 975 |
return S54.foo() * S54.foo(); |
| 976 |
} |
| 977 |
|
| 978 |
S54 opBinary(string op)(S54 s) if (op == "*") |
| 979 |
{ |
| 980 |
return s; |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
void test54() |
| 985 |
{ |
| 986 |
S54.bar(); |
| 987 |
} |
| 988 |
|
| 989 |
/*****************************************/ |
| 990 |
|
| 991 |
void test55() |
| 992 |
{ |
| 993 |
char c = 'a'; |
| 994 |
char[] str, uvw; |
| 995 |
str = str ~ c; |
| 996 |
uvw = c ~ uvw; |
| 997 |
|
| 998 |
printf("%.*s\n", cast(int)str.length, str.ptr); |
| 999 |
assert(str == "a"); |
| 1000 |
assert(uvw == "a"); |
| 1001 |
|
| 1002 |
c = 'b'; |
| 1003 |
printf("%.*s\n", cast(int)str.length, str.ptr); |
| 1004 |
assert(str == "a"); |
| 1005 |
assert(uvw == "a"); |
| 1006 |
} |
| 1007 |
|
| 1008 |
/*****************************************/ |
| 1009 |
|
| 1010 |
void test56() |
| 1011 |
{ |
| 1012 |
char[4] a, b; |
| 1013 |
a[] = 'a'; b[] = 'a'; |
| 1014 |
|
| 1015 |
for (int i = 0; i < 4; i++) |
| 1016 |
{ |
| 1017 |
assert(a[i] == 'a'); |
| 1018 |
assert(b[i] == 'a'); |
| 1019 |
} |
| 1020 |
|
| 1021 |
a[] = b[] = 'b'; |
| 1022 |
|
| 1023 |
for (int i = 0; i < 4; i++) |
| 1024 |
{ |
| 1025 |
assert(a[i] == 'b'); |
| 1026 |
assert(b[i] == 'b'); |
| 1027 |
} |
| 1028 |
|
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
/*****************************************/ |
| 1033 |
|
| 1034 |
void test57() |
| 1035 |
{ |
| 1036 |
char[] a; |
| 1037 |
char[] b; |
| 1038 |
|
| 1039 |
a = a ~ 'x'; |
| 1040 |
assert(a == "x"); |
| 1041 |
b = 'c' ~ a ~ 'b'; |
| 1042 |
assert(b == "cxb"); |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
/*****************************************/ |
| 1047 |
|
| 1048 |
struct Foo58 |
| 1049 |
{ |
| 1050 |
ubyte a, b, c; |
| 1051 |
} |
| 1052 |
|
| 1053 |
void test58() |
| 1054 |
{ |
| 1055 |
Foo58 d; |
| 1056 |
foobar58(d); |
| 1057 |
return cast(void)0; |
| 1058 |
} |
| 1059 |
|
| 1060 |
void foobar58(Foo58 e) {} |
| 1061 |
|
| 1062 |
|
| 1063 |
/*****************************************/ |
| 1064 |
|
| 1065 |
void foo59(string s) |
| 1066 |
{ |
| 1067 |
assert(s == "hello"); |
| 1068 |
} |
| 1069 |
|
| 1070 |
void foo59(wstring s) |
| 1071 |
{ |
| 1072 |
assert(s == "baby"); |
| 1073 |
} |
| 1074 |
|
| 1075 |
void foo59(dstring s) |
| 1076 |
{ |
| 1077 |
assert(s == "jane"); |
| 1078 |
} |
| 1079 |
|
| 1080 |
void test59() |
| 1081 |
{ |
| 1082 |
string h = "hello"c; |
| 1083 |
wstring b = "baby"w; |
| 1084 |
dstring j = "jane"d; |
| 1085 |
|
| 1086 |
//writefln("%s %s %s", h, b, j); |
| 1087 |
|
| 1088 |
foo59(h); |
| 1089 |
foo59("hello"c); |
| 1090 |
foo59(b); |
| 1091 |
foo59("baby"w); |
| 1092 |
foo59(j); |
| 1093 |
foo59("jane"d); |
| 1094 |
} |
| 1095 |
|
| 1096 |
/*****************************************/ |
| 1097 |
|
| 1098 |
class C60 |
| 1099 |
{ |
| 1100 |
static const int x; |
| 1101 |
const int y; |
| 1102 |
|
| 1103 |
void foo() |
| 1104 |
{ |
| 1105 |
} |
| 1106 |
|
| 1107 |
shared static this() |
| 1108 |
{ |
| 1109 |
x = 5; |
| 1110 |
} |
| 1111 |
|
| 1112 |
this() |
| 1113 |
{ |
| 1114 |
y = 7; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
const int z60; |
| 1119 |
|
| 1120 |
shared static this() |
| 1121 |
{ |
| 1122 |
z60 = 3; |
| 1123 |
} |
| 1124 |
|
| 1125 |
void test60() |
| 1126 |
{ |
| 1127 |
C60 c = new C60(); |
| 1128 |
|
| 1129 |
assert(c.x == 5); |
| 1130 |
assert(c.y == 7); |
| 1131 |
assert(z60 == 3); |
| 1132 |
} |
| 1133 |
|
| 1134 |
/*****************************************/ |
| 1135 |
|
| 1136 |
void foo61(real[] arr) |
| 1137 |
{ |
| 1138 |
// i = 0 doesn't trigger the bug, everything bigger seems to |
| 1139 |
// also, making this const fixes the bug |
| 1140 |
size_t i = 1; |
| 1141 |
|
| 1142 |
for (size_t j = i; j >= i; j -= i) |
| 1143 |
{ |
| 1144 |
// interesting results follow from this: |
| 1145 |
printf("%zd ", i); |
| 1146 |
// it prints a _lot_ of ones |
| 1147 |
|
| 1148 |
arr[j] = arr[j - i]; |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
void test61() |
| 1153 |
{ |
| 1154 |
real[] array; |
| 1155 |
array.length = 2; // whatever, as long as it's more than 1 |
| 1156 |
|
| 1157 |
foreach (ref real i; array) |
| 1158 |
i = 1; // just something |
| 1159 |
|
| 1160 |
foo61(array); |
| 1161 |
} |
| 1162 |
|
| 1163 |
/*****************************************/ |
| 1164 |
|
| 1165 |
void bug7493() |
| 1166 |
{ |
| 1167 |
string str = "abcde"; |
| 1168 |
const(void) [][1] arr = [str]; |
| 1169 |
assert(arr[0].length == str.length); |
| 1170 |
const(void) [][1] arr2; |
| 1171 |
arr2 = [str]; |
| 1172 |
assert(arr[0].length == str.length); |
| 1173 |
} |
| 1174 |
|
| 1175 |
/*****************************************/ |
| 1176 |
|
| 1177 |
int main() |
| 1178 |
{ |
| 1179 |
test1(); |
| 1180 |
test4(); |
| 1181 |
test5(); |
| 1182 |
test6(); |
| 1183 |
test7(); |
| 1184 |
test8(); |
| 1185 |
test9(); |
| 1186 |
test10(); |
| 1187 |
test11(); |
| 1188 |
test12(); |
| 1189 |
test13(); |
| 1190 |
test14(); |
| 1191 |
test15(); |
| 1192 |
test16(); |
| 1193 |
test17(); |
| 1194 |
test18(); |
| 1195 |
test19(); |
| 1196 |
test20(); |
| 1197 |
test21(); |
| 1198 |
test22(); |
| 1199 |
test24(); |
| 1200 |
test25(); |
| 1201 |
test26(); |
| 1202 |
test27(); |
| 1203 |
test28(); |
| 1204 |
test29(); |
| 1205 |
test31(); |
| 1206 |
test33(); |
| 1207 |
test34(); |
| 1208 |
test35(); |
| 1209 |
test36(); |
| 1210 |
test37(); |
| 1211 |
test38(); |
| 1212 |
test41(); |
| 1213 |
test42(); |
| 1214 |
test43(); |
| 1215 |
test44(); |
| 1216 |
test45(); |
| 1217 |
test46(); |
| 1218 |
test48(); |
| 1219 |
test49(); |
| 1220 |
test50(); |
| 1221 |
test51(); |
| 1222 |
test52(); |
| 1223 |
test53(); |
| 1224 |
test54(); |
| 1225 |
test55(); |
| 1226 |
test56(); |
| 1227 |
test57(); |
| 1228 |
test58(); |
| 1229 |
test59(); |
| 1230 |
test60(); |
| 1231 |
test61(); |
| 1232 |
bug7493(); |
| 1233 |
|
| 1234 |
printf("Success\n"); |
| 1235 |
return 0; |
| 1236 |
} |