| 1 |
/* |
| 2 |
TEST_OUTPUT: |
| 3 |
--- |
| 4 |
This actually gets evaluated! |
| 5 |
() |
| 6 |
(bool) |
| 7 |
(bool, short) |
| 8 |
(bool, short, int) |
| 9 |
Alias Test instantiated |
| 10 |
Alias Test instantiated |
| 11 |
--- |
| 12 |
*/ |
| 13 |
import core.stdc.stdio; |
| 14 |
|
| 15 |
/*********************************************************/ |
| 16 |
|
| 17 |
template Foo(T) |
| 18 |
{ |
| 19 |
static if ( is(T : int) ) |
| 20 |
alias T t1; |
| 21 |
|
| 22 |
static if (T.sizeof == 4) |
| 23 |
alias T t2; |
| 24 |
|
| 25 |
static if ( is(T AB : int) ) |
| 26 |
alias AB t3; |
| 27 |
|
| 28 |
static if ( is(T* V : V*) ) |
| 29 |
alias V t4; |
| 30 |
|
| 31 |
static if ( is(T W) ) |
| 32 |
alias W t5; |
| 33 |
else |
| 34 |
alias char t5; |
| 35 |
|
| 36 |
static if ( is(T* X : X*) ) |
| 37 |
{ |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void test1() |
| 42 |
{ |
| 43 |
Foo!(int).t1 x1; |
| 44 |
assert(typeid(typeof(x1)) == typeid(int)); |
| 45 |
|
| 46 |
Foo!(int).t2 x2; |
| 47 |
assert(typeid(typeof(x2)) == typeid(int)); |
| 48 |
|
| 49 |
Foo!(int).t3 x3; |
| 50 |
assert(typeid(typeof(x3)) == typeid(int)); |
| 51 |
|
| 52 |
Foo!(int).t4 x4; |
| 53 |
assert(typeid(typeof(x4)) == typeid(int)); |
| 54 |
|
| 55 |
Foo!(int).t5 x5; |
| 56 |
assert(typeid(typeof(x5)) == typeid(int)); |
| 57 |
|
| 58 |
Foo!(int).X x6; |
| 59 |
assert(typeid(typeof(x6)) == typeid(int)); |
| 60 |
} |
| 61 |
|
| 62 |
/*********************************************************/ |
| 63 |
|
| 64 |
|
| 65 |
void test2() |
| 66 |
{ |
| 67 |
alias int T; |
| 68 |
|
| 69 |
static if ( is(T : int) ) |
| 70 |
alias T t1; |
| 71 |
|
| 72 |
static if (T.sizeof == 4) |
| 73 |
alias T t2; |
| 74 |
|
| 75 |
static if ( is(T U : int) ) |
| 76 |
alias U t3; |
| 77 |
|
| 78 |
static if ( is(T* V : V*) ) |
| 79 |
alias V t4; |
| 80 |
|
| 81 |
static if ( is(T W) ) |
| 82 |
alias W t5; |
| 83 |
else |
| 84 |
alias char t5; |
| 85 |
|
| 86 |
static if ( is(T* X : X*) ) |
| 87 |
{ |
| 88 |
} |
| 89 |
|
| 90 |
t1 x1; |
| 91 |
assert(typeid(typeof(x1)) == typeid(int)); |
| 92 |
|
| 93 |
t2 x2; |
| 94 |
assert(typeid(typeof(x2)) == typeid(int)); |
| 95 |
|
| 96 |
t3 x3; |
| 97 |
assert(typeid(typeof(x3)) == typeid(int)); |
| 98 |
|
| 99 |
t4 x4; |
| 100 |
assert(typeid(typeof(x4)) == typeid(int)); |
| 101 |
|
| 102 |
t5 x5; |
| 103 |
assert(typeid(typeof(x5)) == typeid(int)); |
| 104 |
|
| 105 |
X x6; |
| 106 |
assert(typeid(typeof(x6)) == typeid(int)); |
| 107 |
} |
| 108 |
|
| 109 |
/*********************************************************/ |
| 110 |
|
| 111 |
void test3() |
| 112 |
{ |
| 113 |
static if ( is(short : int) ) |
| 114 |
{ |
| 115 |
printf("1\n"); |
| 116 |
} |
| 117 |
else |
| 118 |
assert(0); |
| 119 |
static if ( is(short == int) ) |
| 120 |
assert(0); |
| 121 |
static if ( is(int == int) ) |
| 122 |
{ |
| 123 |
printf("3\n"); |
| 124 |
} |
| 125 |
else |
| 126 |
assert(0); |
| 127 |
} |
| 128 |
|
| 129 |
/*********************************************************/ |
| 130 |
|
| 131 |
void test4() |
| 132 |
{ |
| 133 |
alias void Function(int); |
| 134 |
|
| 135 |
static if (is(Function Void == function)) |
| 136 |
printf("if\n"); |
| 137 |
else |
| 138 |
assert(0); |
| 139 |
|
| 140 |
// static if (is(Void == void)) |
| 141 |
// printf("if\n"); |
| 142 |
// else |
| 143 |
// assert(0); |
| 144 |
|
| 145 |
|
| 146 |
alias byte delegate(int) Delegate; |
| 147 |
|
| 148 |
static if (is(Delegate Foo == delegate)) |
| 149 |
printf("if\n"); |
| 150 |
else |
| 151 |
assert(0); |
| 152 |
|
| 153 |
static if (is(Foo Byte == function)) |
| 154 |
printf("if\n"); |
| 155 |
else |
| 156 |
assert(0); |
| 157 |
|
| 158 |
// static if (is(Byte == byte)) |
| 159 |
// printf("if\n"); |
| 160 |
// else |
| 161 |
// assert(0); |
| 162 |
|
| 163 |
|
| 164 |
union Union { } |
| 165 |
|
| 166 |
static if (is(Union == union)) |
| 167 |
printf("if\n"); |
| 168 |
else |
| 169 |
assert(0); |
| 170 |
|
| 171 |
|
| 172 |
struct Struct { } |
| 173 |
|
| 174 |
static if (is(Struct == struct)) |
| 175 |
printf("if\n"); |
| 176 |
else |
| 177 |
assert(0); |
| 178 |
|
| 179 |
|
| 180 |
enum Enum : short { EnumMember } |
| 181 |
|
| 182 |
static if (is(Enum Short == enum)) |
| 183 |
printf("if\n"); |
| 184 |
else |
| 185 |
assert(0); |
| 186 |
|
| 187 |
static if (is(Short == short)) |
| 188 |
printf("if\n"); |
| 189 |
else |
| 190 |
assert(0); |
| 191 |
|
| 192 |
class Class { } |
| 193 |
|
| 194 |
static if (is(Class == class)) |
| 195 |
printf("if\n"); |
| 196 |
else |
| 197 |
assert(0); |
| 198 |
|
| 199 |
|
| 200 |
interface Interface { } |
| 201 |
|
| 202 |
static if (is(Interface == interface)) |
| 203 |
printf("if\n"); |
| 204 |
else |
| 205 |
assert(0); |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
/*********************************************************/ |
| 210 |
|
| 211 |
class Foo5(T) |
| 212 |
{ |
| 213 |
Node sentinel; |
| 214 |
|
| 215 |
struct Node |
| 216 |
{ |
| 217 |
int value; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
void test5() |
| 222 |
{ |
| 223 |
Foo5!(int) bar=new Foo5!(int); |
| 224 |
bar.sentinel.value = 7; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/*********************************************************/ |
| 229 |
|
| 230 |
template factorial6(int n) |
| 231 |
{ |
| 232 |
static if (n == 1) |
| 233 |
const int factorial6 = 1; |
| 234 |
else |
| 235 |
const int factorial6 = n * .factorial6!(n-1); |
| 236 |
} |
| 237 |
|
| 238 |
void test6() |
| 239 |
{ |
| 240 |
int i = factorial6!(4); |
| 241 |
printf("%d\n", i); |
| 242 |
assert(i == 24); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/*********************************************************/ |
| 247 |
|
| 248 |
template whale(string walrus) |
| 249 |
{ |
| 250 |
const char [] whale = walrus; |
| 251 |
} |
| 252 |
|
| 253 |
template dolphin(string fish) |
| 254 |
{ |
| 255 |
const char [] dolphin = whale!(fish[0..3]); |
| 256 |
} |
| 257 |
|
| 258 |
const char [] urchin1 = dolphin!("anenome"); |
| 259 |
const char [] urchin2 = whale!("anenome"[0..3]); |
| 260 |
|
| 261 |
template dolphin3(string fish) |
| 262 |
{ |
| 263 |
const char [] dolphin3 = fish[0..3]; |
| 264 |
} |
| 265 |
|
| 266 |
const char [] urchin3 = dolphin3!("anenome"); |
| 267 |
|
| 268 |
template dolphin4(string fish) |
| 269 |
{ |
| 270 |
const char [] dolphin4 = whale!(fish[0..(3)]); |
| 271 |
} |
| 272 |
|
| 273 |
const char [] urchin4 = dolphin4!("anenome"); |
| 274 |
|
| 275 |
template dolphin5(string fish) |
| 276 |
{ |
| 277 |
const char [] dolphin5 = whale!(fish[(0)..3]); |
| 278 |
} |
| 279 |
|
| 280 |
const char [] urchin5 = dolphin5!("anenome"); |
| 281 |
|
| 282 |
void test8() |
| 283 |
{ |
| 284 |
assert(urchin1 == "ane"); |
| 285 |
assert(urchin2 == "ane"); |
| 286 |
assert(urchin3 == "ane"); |
| 287 |
assert(urchin4 == "ane"); |
| 288 |
assert(urchin5 == "ane"); |
| 289 |
} |
| 290 |
|
| 291 |
/*********************************************************/ |
| 292 |
|
| 293 |
int testEmpty(string s) { return 0; } |
| 294 |
|
| 295 |
template Recurse(string pattern){ |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
template slice(string str, int from, int to) |
| 300 |
{ |
| 301 |
const string slice = str[from..to]; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
template Compile(string pattern) |
| 306 |
{ |
| 307 |
const string left = slice!(pattern,4,pattern.length); |
| 308 |
|
| 309 |
const string remaining = slice!(left,1,left.length); |
| 310 |
|
| 311 |
alias Recurse!(remaining) fn; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
template Match(string pattern) |
| 317 |
{ |
| 318 |
alias Compile!(pattern) Match; |
| 319 |
} |
| 320 |
|
| 321 |
void test9() |
| 322 |
{ |
| 323 |
alias Match!("abcdefghijk") f; |
| 324 |
} |
| 325 |
|
| 326 |
/*********************************************************/ |
| 327 |
|
| 328 |
template Foo10(string s) |
| 329 |
{ |
| 330 |
const string Foo10 = s; |
| 331 |
} |
| 332 |
|
| 333 |
void test10() |
| 334 |
{ |
| 335 |
string s; |
| 336 |
|
| 337 |
s = Foo10!("abc" ~ "e"); |
| 338 |
assert(s == "abce"); |
| 339 |
|
| 340 |
s = Foo10!("abc" ~ 'f'); |
| 341 |
assert(s == "abcf"); |
| 342 |
|
| 343 |
s = Foo10!('g' ~ "abc"); |
| 344 |
assert(s == "gabc"); |
| 345 |
|
| 346 |
s = Foo10!('g' ~ "abc" ~ 'h'); |
| 347 |
assert(s == "gabch"); |
| 348 |
} |
| 349 |
|
| 350 |
/*********************************************************/ |
| 351 |
|
| 352 |
template Foo11(string s) |
| 353 |
{ |
| 354 |
const string Foo11 = s; |
| 355 |
} |
| 356 |
|
| 357 |
void test11() |
| 358 |
{ |
| 359 |
string s; |
| 360 |
|
| 361 |
s = Foo11!("abcdef"[1..$ - 1]); |
| 362 |
assert(s == "bcde"); |
| 363 |
} |
| 364 |
|
| 365 |
/*********************************************************/ |
| 366 |
|
| 367 |
template Foo12(int i) |
| 368 |
{ |
| 369 |
const int Foo12 = i; |
| 370 |
} |
| 371 |
|
| 372 |
void test12() |
| 373 |
{ |
| 374 |
int i; |
| 375 |
|
| 376 |
i = Foo12!("abcdef" == "abcdef"); |
| 377 |
assert(i == 1); |
| 378 |
i = Foo12!("abcdef" == "abcqef"); |
| 379 |
assert(i == 0); |
| 380 |
i = Foo12!("abcdef" == "abc"); |
| 381 |
assert(i == 0); |
| 382 |
i = Foo12!("abc" == "abcdef"); |
| 383 |
assert(i == 0); |
| 384 |
} |
| 385 |
|
| 386 |
/*********************************************************/ |
| 387 |
|
| 388 |
const a13 = 3; |
| 389 |
static if (a13 == 3) |
| 390 |
int b13 = 7; |
| 391 |
|
| 392 |
template Foo13(int i) |
| 393 |
{ |
| 394 |
const int j = i + 1; |
| 395 |
static if (j == 3) |
| 396 |
const int k = 2; |
| 397 |
} |
| 398 |
|
| 399 |
void test13() |
| 400 |
{ |
| 401 |
assert(b13 == 7); |
| 402 |
assert(Foo13!(2).k == 2); |
| 403 |
} |
| 404 |
|
| 405 |
/*********************************************************/ |
| 406 |
|
| 407 |
template zebra(string w) |
| 408 |
{ |
| 409 |
static if (w.length > 2 && w[1] == 'q') |
| 410 |
const bool zebra = true; |
| 411 |
else |
| 412 |
const bool zebra = false; |
| 413 |
} |
| 414 |
|
| 415 |
template horse(string w) |
| 416 |
{ |
| 417 |
static if (w.length == 1 || w[1] == 'q') |
| 418 |
const bool horse = true; |
| 419 |
else |
| 420 |
const bool horse = false; |
| 421 |
} |
| 422 |
|
| 423 |
void test14() |
| 424 |
{ |
| 425 |
bool lion = zebra!("a"); |
| 426 |
assert(!lion); |
| 427 |
lion = zebra!("aqb"); |
| 428 |
assert(lion); |
| 429 |
|
| 430 |
lion = horse!("a"); |
| 431 |
assert(lion); |
| 432 |
lion = horse!("aqb"); |
| 433 |
assert(lion); |
| 434 |
lion = horse!("ab"); |
| 435 |
assert(!lion); |
| 436 |
} |
| 437 |
|
| 438 |
/*********************************************************/ |
| 439 |
|
| 440 |
template factorial15(int n) |
| 441 |
{ |
| 442 |
static if (n<2) const int factorial15 = 1; |
| 443 |
else const int factorial15 = n * factorial15!(n-1); |
| 444 |
} |
| 445 |
|
| 446 |
template rhino15(alias hippo) |
| 447 |
{ |
| 448 |
const int rhino15 = hippo!(3); |
| 449 |
} |
| 450 |
|
| 451 |
void test15() |
| 452 |
{ |
| 453 |
const int lion = rhino15!(factorial15); |
| 454 |
assert(lion == 6); |
| 455 |
} |
| 456 |
|
| 457 |
/*********************************************************/ |
| 458 |
|
| 459 |
// Create a constant array of int or uint sized items |
| 460 |
// as a dstring string. n is the index of the last item. |
| 461 |
template makeLookup(alias entry, int n) |
| 462 |
{ |
| 463 |
static if (n == -1) // start with an empty array... |
| 464 |
const dchar [] makeLookup = ""; |
| 465 |
else // ... and fill it up |
| 466 |
const dchar [] makeLookup = makeLookup!(entry, n-1) |
| 467 |
~ cast(dchar)entry!(n); |
| 468 |
} |
| 469 |
|
| 470 |
template factorial16(uint n) |
| 471 |
{ |
| 472 |
static if (n<2) const uint factorial16 = 1; |
| 473 |
else const factorial16 = n * factorial16!(n-1); |
| 474 |
} |
| 475 |
|
| 476 |
// Make an array of factorials from 0 to 13 (14!> uint.max) |
| 477 |
const smallfactorials = makeLookup!(factorial16, 13); |
| 478 |
|
| 479 |
const uint[14] testtable = |
| 480 |
[ |
| 481 |
1, |
| 482 |
1, |
| 483 |
2, |
| 484 |
6, |
| 485 |
24, |
| 486 |
120, |
| 487 |
720, |
| 488 |
5040, |
| 489 |
40320, |
| 490 |
362880, |
| 491 |
3628800, |
| 492 |
39916800, |
| 493 |
479001600, |
| 494 |
1932053504, |
| 495 |
]; |
| 496 |
|
| 497 |
void test16() |
| 498 |
{ |
| 499 |
for (int i=0; i<smallfactorials.length; ++i) |
| 500 |
{ |
| 501 |
printf("%d %d\n", i, smallfactorials[i]); |
| 502 |
assert(smallfactorials[i] == testtable[i]); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
/*********************************************************/ |
| 507 |
|
| 508 |
template dingo(int a) |
| 509 |
{ |
| 510 |
pragma(msg, "This actually gets evaluated!"); |
| 511 |
static if (a==2) { |
| 512 |
const int dingo = 28; |
| 513 |
} |
| 514 |
else |
| 515 |
const int dingo = a; |
| 516 |
} |
| 517 |
|
| 518 |
const int bilby = dingo!(2); |
| 519 |
|
| 520 |
void test17() |
| 521 |
{ |
| 522 |
assert(bilby == 28); |
| 523 |
} |
| 524 |
|
| 525 |
/*********************************************************/ |
| 526 |
|
| 527 |
template frog(char F) |
| 528 |
{ |
| 529 |
const int frog = 1; |
| 530 |
} |
| 531 |
|
| 532 |
template frog(int F) |
| 533 |
{ |
| 534 |
const int frog = 2; |
| 535 |
} |
| 536 |
|
| 537 |
template frog(char F: 'A') |
| 538 |
{ |
| 539 |
const int frog = 3; |
| 540 |
} |
| 541 |
|
| 542 |
template frog(int F: 65) |
| 543 |
{ |
| 544 |
const int frog = 4; |
| 545 |
} |
| 546 |
|
| 547 |
static assert( frog!('B')==1); |
| 548 |
static assert( frog!(64)==2); |
| 549 |
static assert( frog!('A')==3); |
| 550 |
static assert( frog!(65)==4); |
| 551 |
|
| 552 |
void test18() |
| 553 |
{ |
| 554 |
} |
| 555 |
|
| 556 |
/*********************************************************/ |
| 557 |
|
| 558 |
void test19() |
| 559 |
{ |
| 560 |
MakeTuple19!(int,int) t; |
| 561 |
assert(t.sizeof == int.sizeof); |
| 562 |
} |
| 563 |
|
| 564 |
template MakeTuple19( T1, T2 ) |
| 565 |
{ |
| 566 |
alias int MakeTuple19; |
| 567 |
} |
| 568 |
|
| 569 |
template MakeTuple19(T1, T2, T3=float) |
| 570 |
{ |
| 571 |
alias long MakeTuple19; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
/*********************************************************/ |
| 576 |
|
| 577 |
template sqrt(real x, real root = x/2, int ntries = 0) |
| 578 |
{ |
| 579 |
static if (ntries == 5) |
| 580 |
// precision doubles with each iteration, |
| 581 |
// 5 should be enough |
| 582 |
const sqrt = root; |
| 583 |
else static if (root * root - x == 0) |
| 584 |
const sqrt = root; // exact match |
| 585 |
else |
| 586 |
// iterate again |
| 587 |
const sqrt = sqrt!(x, (root+x/root)/2, ntries+1); |
| 588 |
} |
| 589 |
|
| 590 |
void test20() |
| 591 |
{ |
| 592 |
real x = sqrt!(2); |
| 593 |
printf("%.20Lg\n", x); // 1.4142135623730950487 |
| 594 |
} |
| 595 |
|
| 596 |
/*********************************************************/ |
| 597 |
|
| 598 |
template hash(string s, uint sofar=0) |
| 599 |
{ |
| 600 |
static if (s.length == 0) |
| 601 |
const hash = sofar; |
| 602 |
else |
| 603 |
const hash = hash!(s[1 .. $], sofar * 11 + s[0]); |
| 604 |
} |
| 605 |
|
| 606 |
uint foo21() |
| 607 |
{ |
| 608 |
return hash!("hello world"); |
| 609 |
} |
| 610 |
|
| 611 |
void test21() |
| 612 |
{ |
| 613 |
auto i = foo21(); |
| 614 |
printf("%d\n", i); |
| 615 |
assert(i == 1871483972); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
/*********************************************************/ |
| 620 |
|
| 621 |
T Foo22(T)(int i) |
| 622 |
{ |
| 623 |
printf("i = %d\n", i); |
| 624 |
return cast(T)0; |
| 625 |
} |
| 626 |
|
| 627 |
void test22() |
| 628 |
{ |
| 629 |
Foo22!(int)(3); |
| 630 |
} |
| 631 |
|
| 632 |
/*********************************************************/ |
| 633 |
|
| 634 |
template fish( char s) |
| 635 |
{ |
| 636 |
const bool fish = true; |
| 637 |
} |
| 638 |
|
| 639 |
template dog(string bird) |
| 640 |
{ |
| 641 |
static if (bird.length>99 && fish!( (bird[95])) ) |
| 642 |
const int dog = 2; |
| 643 |
else const int dog = 3; |
| 644 |
} |
| 645 |
|
| 646 |
const int pig = dog!("a"); |
| 647 |
|
| 648 |
void test23() |
| 649 |
{ |
| 650 |
} |
| 651 |
|
| 652 |
/*********************************************************/ |
| 653 |
|
| 654 |
T delegate (T) acc24 (T) (T n) |
| 655 |
{ |
| 656 |
return (T i) { return n += i; }; |
| 657 |
} |
| 658 |
|
| 659 |
void test24() |
| 660 |
{ |
| 661 |
auto acc1 = acc24 (4); |
| 662 |
} |
| 663 |
|
| 664 |
/*********************************************************/ |
| 665 |
|
| 666 |
T func25(T, T c = 1)(T x) |
| 667 |
{ |
| 668 |
return x * c; |
| 669 |
} |
| 670 |
|
| 671 |
void test25() |
| 672 |
{ |
| 673 |
double d; |
| 674 |
|
| 675 |
d = func25(1.0); |
| 676 |
assert(d == 1.0); |
| 677 |
|
| 678 |
d = func25(2.0); |
| 679 |
assert(d == 2.0); |
| 680 |
|
| 681 |
d = func25!(double)(2.0); |
| 682 |
assert(d == 2.0); |
| 683 |
|
| 684 |
d = func25!(double, 3)(2.0); |
| 685 |
assert(d == 6.0); |
| 686 |
} |
| 687 |
|
| 688 |
/*********************************************************/ |
| 689 |
|
| 690 |
class Foo26 {} |
| 691 |
class Bar26 {} |
| 692 |
|
| 693 |
string name26; |
| 694 |
|
| 695 |
template aliastest(alias A) { |
| 696 |
pragma(msg,"Alias Test instantiated"); |
| 697 |
void aliastest() { |
| 698 |
name26 = (new A!().al).classinfo.name; |
| 699 |
//writefln("Alias Test: ", name26); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
template boxtpl(alias A) { |
| 704 |
template box() { |
| 705 |
alias A al; |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
void test26() |
| 710 |
{ |
| 711 |
aliastest!(boxtpl!(Foo26).box) (); |
| 712 |
assert(name26 == "template4.Foo26"); |
| 713 |
aliastest!(boxtpl!(Bar26).box) (); |
| 714 |
assert(name26 == "template4.Bar26"); |
| 715 |
} |
| 716 |
|
| 717 |
/*********************************************************/ |
| 718 |
|
| 719 |
struct TFoo27(int x) { } |
| 720 |
alias TFoo27!(3) a; |
| 721 |
alias TFoo27!(2+1) b; |
| 722 |
alias TFoo27!(3u) c; |
| 723 |
|
| 724 |
static assert(is(TFoo27!(3) == TFoo27!(2 + 1))); |
| 725 |
static assert(is(TFoo27!(3) == TFoo27!(3u))); |
| 726 |
|
| 727 |
void test27() |
| 728 |
{ |
| 729 |
} |
| 730 |
|
| 731 |
/*********************************************************/ |
| 732 |
|
| 733 |
struct SiQuantity |
| 734 |
{ |
| 735 |
real value = 0; |
| 736 |
static assert(SiQuantity.sizeof == real.sizeof); |
| 737 |
|
| 738 |
template AddDimensions(int mul, U) { } |
| 739 |
} |
| 740 |
|
| 741 |
void test28() |
| 742 |
{ |
| 743 |
} |
| 744 |
|
| 745 |
/*********************************************************/ |
| 746 |
|
| 747 |
template Count29() { const Count29 = 5; } |
| 748 |
|
| 749 |
void test29() |
| 750 |
{ |
| 751 |
int[Count29!()] x; |
| 752 |
|
| 753 |
assert(x.length == 5); |
| 754 |
} |
| 755 |
|
| 756 |
/*********************************************************/ |
| 757 |
|
| 758 |
class FooClass(T) { T data; } |
| 759 |
|
| 760 |
struct FooStruct(T) { T data; } |
| 761 |
|
| 762 |
void bar_struct(T)(FooStruct!(T) a) {} |
| 763 |
|
| 764 |
void bar_class(T)(FooClass!(T) a) {} |
| 765 |
|
| 766 |
void test30() |
| 767 |
{ |
| 768 |
auto C = new FooClass!(double); |
| 769 |
FooStruct!(double) S; |
| 770 |
|
| 771 |
bar_struct(S); |
| 772 |
bar_class!(double)(C); |
| 773 |
bar_class(C); |
| 774 |
} |
| 775 |
|
| 776 |
/*********************************************************/ |
| 777 |
|
| 778 |
V get31(V,K)(V[K] dict, K key, V def = V.init) |
| 779 |
{ |
| 780 |
V* ptr = key in dict; |
| 781 |
return ptr? *ptr: def; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
string get31x(string[int] dict, int key, string def = null) |
| 786 |
{ |
| 787 |
string* ptr = key in dict; |
| 788 |
return ptr? *ptr: def; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
void test31() |
| 793 |
{ |
| 794 |
string[int] i2s; |
| 795 |
i2s[1] = "Hello"; |
| 796 |
i2s[5] = "There"; |
| 797 |
|
| 798 |
auto result = i2s.get31(1, "yeh"); |
| 799 |
printf("%.*s\n", cast(int)result.length, result.ptr); |
| 800 |
result = i2s.get31(2, "default"); |
| 801 |
printf("%.*s\n", cast(int)result.length, result.ptr); |
| 802 |
result = i2s.get31(1); |
| 803 |
printf("%.*s\n", cast(int)result.length, result.ptr); |
| 804 |
result = i2s.get31(2); |
| 805 |
printf("%.*s\n", cast(int)result.length, result.ptr); |
| 806 |
} |
| 807 |
|
| 808 |
/*********************************************************/ |
| 809 |
|
| 810 |
void delegate(T, S) Concat(S, T...)(void delegate(T) one, void delegate(S) two) |
| 811 |
{ |
| 812 |
return( delegate void(T t, S s){} ); |
| 813 |
} |
| 814 |
|
| 815 |
void test32() |
| 816 |
{ |
| 817 |
void delegate(char, char, int) wtf = Concat( |
| 818 |
delegate void(char a, char b) {}, |
| 819 |
delegate void(int lol) {} |
| 820 |
); |
| 821 |
} |
| 822 |
|
| 823 |
/*********************************************************/ |
| 824 |
|
| 825 |
struct Composer(T) { |
| 826 |
alias T delegate(T) Fun; |
| 827 |
Fun[] funs; |
| 828 |
public T opCall()(T x) { |
| 829 |
T result = x; |
| 830 |
foreach_reverse (f; funs) |
| 831 |
{ |
| 832 |
result = f(result); |
| 833 |
} |
| 834 |
return result; |
| 835 |
} |
| 836 |
public void opOpAssign(string op)(Fun f) if (op == "+") { |
| 837 |
funs ~= f; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
struct square(T) { |
| 842 |
T opCall(T t) { |
| 843 |
return t*t; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
struct plus1(T) { |
| 848 |
T opCall(T t) { |
| 849 |
return t+1; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
struct div3(T) { |
| 854 |
T opCall(T t) { |
| 855 |
return t/3.0; |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
T delegate(T) tofp(T : S!(T), alias S)() |
| 860 |
{ |
| 861 |
class Foo |
| 862 |
{ |
| 863 |
div3!(T) arg; |
| 864 |
|
| 865 |
T bar(T t) |
| 866 |
{ |
| 867 |
return arg(t); |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
Foo f = new Foo; |
| 872 |
return &f.bar; |
| 873 |
} |
| 874 |
|
| 875 |
void test33() { |
| 876 |
Composer!(double) comp; |
| 877 |
comp += delegate double (double x) { return x/3.0;}; |
| 878 |
comp += delegate double (double x) { return x*x;}; |
| 879 |
comp += (double x) => x + 1.0; |
| 880 |
printf("%f\n", comp(2.0)); |
| 881 |
|
| 882 |
// Try function objects |
| 883 |
Composer!(double) comp2; |
| 884 |
comp2 += tofp!(div3!(double))(); |
| 885 |
comp2 += tofp!(square!(double))(); |
| 886 |
comp2 += tofp!(plus1!(double))(); |
| 887 |
printf("%f\n", comp2( 2.0)); |
| 888 |
} |
| 889 |
|
| 890 |
/*********************************************************/ |
| 891 |
|
| 892 |
template Print34(Ts ...) { pragma (msg, Ts.stringof); } |
| 893 |
|
| 894 |
template Tuple34(Ts ...) { alias Ts Tuple34; } |
| 895 |
|
| 896 |
template Decode34( T ) { alias Tuple34!() Types; } |
| 897 |
template Decode34( T : TT!(U1), alias TT, U1 ) { alias Tuple34!(U1) Types; } |
| 898 |
template Decode34( T : TT!(U1,U2), alias TT, U1, U2 ) { alias Tuple34!(U1,U2) Types; } |
| 899 |
template Decode34( T : TT!(U1,U2,U3), alias TT, U1, U2, U3 ) { alias Tuple34!(U1,U2,U3) Types; } |
| 900 |
|
| 901 |
|
| 902 |
struct S34_1(T1) {} |
| 903 |
struct S34_2(T1, T2) {} |
| 904 |
struct S34_3(T1, T2, T3) {} |
| 905 |
|
| 906 |
alias Decode34!( bool ).Types SQ0; |
| 907 |
alias Decode34!( S34_1!(bool) ).Types SQ1; |
| 908 |
alias Decode34!( S34_2!(bool,short) ).Types SQ2; |
| 909 |
alias Decode34!( S34_3!(bool,short,int) ).Types SQ3; |
| 910 |
|
| 911 |
mixin Print34!(SQ0); |
| 912 |
mixin Print34!(SQ1); |
| 913 |
mixin Print34!(SQ2); |
| 914 |
mixin Print34!(SQ3); |
| 915 |
|
| 916 |
void test34() |
| 917 |
{ |
| 918 |
} |
| 919 |
|
| 920 |
/*********************************************************/ |
| 921 |
|
| 922 |
template strof(T) |
| 923 |
{ |
| 924 |
static string strof = T.stringof; |
| 925 |
} |
| 926 |
|
| 927 |
void test35() |
| 928 |
{ |
| 929 |
alias typeof(delegate () { return (char[]).init;} ) del; |
| 930 |
auto a = strof!(del); |
| 931 |
auto aa = strof!(int); |
| 932 |
auto ab = strof!(typeof(5)); |
| 933 |
auto meth = delegate () { return (char[]).init;}; |
| 934 |
auto b = strof!(typeof(meth)); |
| 935 |
auto c = strof!(typeof(delegate () { return 5; } )); |
| 936 |
auto d = strof!(typeof(delegate () { return (char[]).init;} )); |
| 937 |
} |
| 938 |
|
| 939 |
/*********************************************************/ |
| 940 |
|
| 941 |
struct Number36(int N) |
| 942 |
{ |
| 943 |
const int value = N; |
| 944 |
} |
| 945 |
|
| 946 |
struct Foo36(T) |
| 947 |
{ |
| 948 |
int talk() { printf("Not so special:\n"); return 1; } |
| 949 |
} |
| 950 |
|
| 951 |
struct Foo36(T : Number36!(N), int N) |
| 952 |
{ |
| 953 |
int talk() { printf("Ooh special - NUMBER N\n"); return 2; } |
| 954 |
} |
| 955 |
|
| 956 |
void test36() |
| 957 |
{ |
| 958 |
Foo36!(Number36!(5)) x; |
| 959 |
auto i = x.talk(); |
| 960 |
assert(i == 2); |
| 961 |
} |
| 962 |
|
| 963 |
/*********************************************************/ |
| 964 |
|
| 965 |
struct Base37(T) {} |
| 966 |
|
| 967 |
alias Base37 Alias37; |
| 968 |
|
| 969 |
void foo37(T)(Alias37!(T) x) {} |
| 970 |
|
| 971 |
void test37() |
| 972 |
{ |
| 973 |
Base37!(float) b; |
| 974 |
foo37(b); // fails! |
| 975 |
} |
| 976 |
|
| 977 |
/*********************************************************/ |
| 978 |
|
| 979 |
void sort(alias dg, T)(T[] arr) |
| 980 |
{ |
| 981 |
bool a = dg(1,2); |
| 982 |
printf("a = %d\n", a); |
| 983 |
assert(a == true); |
| 984 |
} |
| 985 |
|
| 986 |
void test38() |
| 987 |
{ int[] arr; |
| 988 |
int i = 3; |
| 989 |
|
| 990 |
sort!( (x,y){ return x + i > y; } )(arr); |
| 991 |
sort!( (int x,int y){ return x + i > y; } )(arr); |
| 992 |
} |
| 993 |
|
| 994 |
/*********************************************************/ |
| 995 |
|
| 996 |
void bug4652(U, T...)(long y, T x, U num) {} |
| 997 |
void bug4652default(T) (T value, int x=2) {} |
| 998 |
void bug4652default(T) (T value, int y) {} |
| 999 |
void bug4676(T...)(T args, string str) {} |
| 1000 |
void bug4676(T...)(T args) {} |
| 1001 |
|
| 1002 |
void instantiate4652() |
| 1003 |
{ |
| 1004 |
bug4652(2, 'c', 27, 'e', 'f',1); // rejects-valid |
| 1005 |
bug4652(2, 1); // infinite loop on valid code |
| 1006 |
bug4652default(true); |
| 1007 |
bug4676(1, 2, 3); |
| 1008 |
} |
| 1009 |
|
| 1010 |
/*********************************************************/ |
| 1011 |
// https://issues.dlang.org/show_bug.cgi?id=7589 |
| 1012 |
|
| 1013 |
struct T7589(T) |
| 1014 |
{ |
| 1015 |
void n; |
| 1016 |
} |
| 1017 |
static assert(!__traits(compiles, T7589!(int))); |
| 1018 |
|
| 1019 |
int bug7589b(T)() @safe { int *p; *(p + 8) = 6; } |
| 1020 |
static assert(!__traits(compiles, bug7589b!(int)()+7 )); |
| 1021 |
|
| 1022 |
|
| 1023 |
/*********************************************************/ |
| 1024 |
|
| 1025 |
int bar39(alias dg)(int i) |
| 1026 |
{ |
| 1027 |
return dg(i); |
| 1028 |
} |
| 1029 |
|
| 1030 |
void test39() |
| 1031 |
{ |
| 1032 |
auto i = bar39!(a => a + 1)(3); |
| 1033 |
if (i != 4) |
| 1034 |
assert(0); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/*********************************************************/ |
| 1038 |
// https://issues.dlang.org/show_bug.cgi?id=6701 |
| 1039 |
|
| 1040 |
uint foo_6701(uint v:0)() { return 1; } |
| 1041 |
uint foo_6701(uint v)() { return 0; } |
| 1042 |
uint foo2_6701(uint v:0, string op)() { return 1; } |
| 1043 |
uint foo2_6701(uint v, string op)() { return 0; } |
| 1044 |
|
| 1045 |
void test6701() |
| 1046 |
{ |
| 1047 |
assert(foo_6701!(0u)() == 1); |
| 1048 |
assert(foo2_6701!(0u, "+")() == 1); |
| 1049 |
} |
| 1050 |
|
| 1051 |
/******************************************/ |
| 1052 |
// https://issues.dlang.org/show_bug.cgi?id=7469 |
| 1053 |
|
| 1054 |
struct Foo7469a(int x) { } |
| 1055 |
struct Foo7469b(int x) { } |
| 1056 |
struct Foo7469c(alias v) { } |
| 1057 |
struct Foo7469d(T...) { } |
| 1058 |
struct Foo7469e(int a, T...) { } |
| 1059 |
struct Foo7469f(T, int k=1) { } |
| 1060 |
struct Foo7469g(T, int k=1) { } |
| 1061 |
struct Foo7469h(uint x) { } |
| 1062 |
|
| 1063 |
import core.demangle : demangleType; |
| 1064 |
|
| 1065 |
void test7469() |
| 1066 |
{ |
| 1067 |
static assert(demangleType(Foo7469a!(3 ) .mangleof) == "template4.Foo7469a!(3).Foo7469a"); |
| 1068 |
static assert(demangleType(Foo7469a!(3u) .mangleof) == "template4.Foo7469a!(3).Foo7469a"); |
| 1069 |
static assert(demangleType(Foo7469b!(3u) .mangleof) == "template4.Foo7469b!(3).Foo7469b"); |
| 1070 |
static assert(demangleType(Foo7469b!(3 ) .mangleof) == "template4.Foo7469b!(3).Foo7469b"); |
| 1071 |
static assert(demangleType(Foo7469c!(3 ) .mangleof) == "template4.Foo7469c!(3).Foo7469c"); |
| 1072 |
static assert(demangleType(Foo7469c!(3u) .mangleof) == "template4.Foo7469c!(3u).Foo7469c"); |
| 1073 |
static assert(demangleType(Foo7469d!(3 ) .mangleof) == "template4.Foo7469d!(3).Foo7469d"); |
| 1074 |
static assert(demangleType(Foo7469d!(3u) .mangleof) == "template4.Foo7469d!(3u).Foo7469d"); |
| 1075 |
static assert(demangleType(Foo7469e!(3u, 5u).mangleof) == "template4.Foo7469e!(3, 5u).Foo7469e"); |
| 1076 |
static assert(demangleType(Foo7469f!(int, 1).mangleof) == "template4.Foo7469f!(int, 1).Foo7469f"); |
| 1077 |
static assert(demangleType(Foo7469f!(int) .mangleof) == "template4.Foo7469f!(int, 1).Foo7469f"); |
| 1078 |
static assert(demangleType(Foo7469g!(int) .mangleof) == "template4.Foo7469g!(int, 1).Foo7469g"); |
| 1079 |
static assert(demangleType(Foo7469g!(int, 1).mangleof) == "template4.Foo7469g!(int, 1).Foo7469g"); |
| 1080 |
static assert(demangleType(Foo7469h!(3 ) .mangleof) == "template4.Foo7469h!(3u).Foo7469h"); |
| 1081 |
static assert(demangleType(Foo7469h!(3u) .mangleof) == "template4.Foo7469h!(3u).Foo7469h"); |
| 1082 |
} |
| 1083 |
|
| 1084 |
/******************************************/ |
| 1085 |
|
| 1086 |
template foo7698a(T, T val : 0) |
| 1087 |
{ |
| 1088 |
enum foo7698a = val; |
| 1089 |
} |
| 1090 |
|
| 1091 |
T foo7698b(T, T val : 0)() |
| 1092 |
{ |
| 1093 |
return val; |
| 1094 |
} |
| 1095 |
|
| 1096 |
T foo7698c(T, T val : T.init)() |
| 1097 |
{ |
| 1098 |
return val; |
| 1099 |
} |
| 1100 |
|
| 1101 |
void test7698() |
| 1102 |
{ |
| 1103 |
static assert(foo7698a!(int, 0) == 0); |
| 1104 |
assert(foo7698b!(int, 0)() == 0); |
| 1105 |
assert(foo7698c!(int, 0)() == 0); |
| 1106 |
} |
| 1107 |
|
| 1108 |
/*********************************************************/ |
| 1109 |
|
| 1110 |
int main() |
| 1111 |
{ |
| 1112 |
test1(); |
| 1113 |
test2(); |
| 1114 |
test3(); |
| 1115 |
test4(); |
| 1116 |
test5(); |
| 1117 |
test6(); |
| 1118 |
test8(); |
| 1119 |
test9(); |
| 1120 |
test10(); |
| 1121 |
test11(); |
| 1122 |
test12(); |
| 1123 |
test13(); |
| 1124 |
test14(); |
| 1125 |
test15(); |
| 1126 |
test16(); |
| 1127 |
test17(); |
| 1128 |
test18(); |
| 1129 |
test19(); |
| 1130 |
test20(); |
| 1131 |
test21(); |
| 1132 |
test22(); |
| 1133 |
test23(); |
| 1134 |
test24(); |
| 1135 |
test25(); |
| 1136 |
test26(); |
| 1137 |
test27(); |
| 1138 |
test28(); |
| 1139 |
test29(); |
| 1140 |
test30(); |
| 1141 |
test31(); |
| 1142 |
test32(); |
| 1143 |
test33(); |
| 1144 |
test34(); |
| 1145 |
test35(); |
| 1146 |
test36(); |
| 1147 |
test37(); |
| 1148 |
test38(); |
| 1149 |
test39(); |
| 1150 |
test6701(); |
| 1151 |
test7698(); |
| 1152 |
|
| 1153 |
printf("Success\n"); |
| 1154 |
return 0; |
| 1155 |
} |