| 1 |
// RUNNABLE_PHOBOS_TEST |
| 2 |
import std.math; |
| 3 |
|
| 4 |
extern(C) int printf(const char*, ...); |
| 5 |
|
| 6 |
string abc; |
| 7 |
|
| 8 |
template Floating(T) |
| 9 |
{ |
| 10 |
T[3] a; |
| 11 |
T[3] b; |
| 12 |
T[3] c; |
| 13 |
|
| 14 |
T[] A() |
| 15 |
{ |
| 16 |
printf("A\n"); |
| 17 |
abc ~= "A"; |
| 18 |
return a; |
| 19 |
} |
| 20 |
|
| 21 |
T[] B() |
| 22 |
{ |
| 23 |
printf("B\n"); |
| 24 |
abc ~= "B"; |
| 25 |
return b; |
| 26 |
} |
| 27 |
|
| 28 |
T[] C() |
| 29 |
{ |
| 30 |
printf("C\n"); |
| 31 |
abc ~= "C"; |
| 32 |
return c; |
| 33 |
} |
| 34 |
|
| 35 |
T D() |
| 36 |
{ |
| 37 |
printf("D\n"); |
| 38 |
abc ~= "D"; |
| 39 |
return 4; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
void testx() |
| 44 |
{ |
| 45 |
a = [11, 22, 33]; |
| 46 |
b = [1, 2, 3]; |
| 47 |
c = [4, 5, 6]; |
| 48 |
|
| 49 |
abc = null; |
| 50 |
A()[] = B()[] + C()[]; |
| 51 |
assert(abc == "ABC"); |
| 52 |
assert(a[0] == 5); |
| 53 |
assert(a[1] == 7); |
| 54 |
assert(a[2] == 9); |
| 55 |
|
| 56 |
abc = null; |
| 57 |
A()[] = B()[] + 4; |
| 58 |
assert(abc == "AB"); |
| 59 |
assert(a[0] == 5); |
| 60 |
assert(a[1] == 6); |
| 61 |
assert(a[2] == 7); |
| 62 |
|
| 63 |
abc = null; |
| 64 |
A()[] = 4 + B()[]; |
| 65 |
assert(abc == "AB"); |
| 66 |
assert(a[0] == 5); |
| 67 |
assert(a[1] == 6); |
| 68 |
assert(a[2] == 7); |
| 69 |
|
| 70 |
abc = null; |
| 71 |
A()[] = D() + B()[]; |
| 72 |
assert(abc == "ADB"); |
| 73 |
assert(a[0] == 5); |
| 74 |
assert(a[1] == 6); |
| 75 |
assert(a[2] == 7); |
| 76 |
|
| 77 |
a = [11, 22, 33]; |
| 78 |
abc = null; |
| 79 |
A()[] += B()[]; |
| 80 |
assert(abc == "AB"); |
| 81 |
assert(a[0] == 12); |
| 82 |
assert(a[1] == 24); |
| 83 |
assert(a[2] == 36); |
| 84 |
|
| 85 |
a = [11, 22, 33]; |
| 86 |
A()[] += 4; |
| 87 |
assert(a[0] == 15); |
| 88 |
assert(a[1] == 26); |
| 89 |
assert(a[2] == 37); |
| 90 |
|
| 91 |
a = [11, 22, 33]; |
| 92 |
A()[] -= 4; |
| 93 |
assert(a[0] == 7); |
| 94 |
assert(a[1] == 18); |
| 95 |
assert(a[2] == 29); |
| 96 |
|
| 97 |
a = [11, 22, 33]; |
| 98 |
A()[] *= 4; |
| 99 |
assert(a[0] == 44); |
| 100 |
assert(a[1] == 88); |
| 101 |
assert(a[2] == 132); |
| 102 |
|
| 103 |
a = [4, 8, 32]; |
| 104 |
A()[] /= 4; |
| 105 |
assert(a[0] == 1); |
| 106 |
assert(a[1] == 2); |
| 107 |
assert(a[2] == 8); |
| 108 |
|
| 109 |
a = [4, 8, 33]; |
| 110 |
A()[] %= 4; |
| 111 |
assert(a[0] == 0); |
| 112 |
assert(a[1] == 0); |
| 113 |
assert(a[2] == 1); |
| 114 |
|
| 115 |
a = [11, 22, 33]; |
| 116 |
abc = null; |
| 117 |
A()[] += 4 + B()[]; |
| 118 |
assert(abc == "AB"); |
| 119 |
assert(a[0] == 16); |
| 120 |
assert(a[1] == 28); |
| 121 |
assert(a[2] == 40); |
| 122 |
|
| 123 |
abc = null; |
| 124 |
A()[] = B()[] - C()[]; |
| 125 |
assert(abc == "ABC"); |
| 126 |
printf("%Lg, %Lg, %Lg\n", cast(real)a[0], cast(real)a[1], cast(real)a[2]); |
| 127 |
assert(a[0] == -3); |
| 128 |
assert(a[1] == -3); |
| 129 |
assert(a[2] == -3); |
| 130 |
|
| 131 |
abc = null; |
| 132 |
A()[] = -B()[] - C()[]; |
| 133 |
assert(abc == "ABC"); |
| 134 |
printf("%Lg, %Lg, %Lg\n", cast(real)a[0], cast(real)a[1], cast(real)a[2]); |
| 135 |
assert(a[0] == -5); |
| 136 |
assert(a[1] == -7); |
| 137 |
assert(a[2] == -9); |
| 138 |
|
| 139 |
abc = null; |
| 140 |
A()[] = B()[] + C()[] * 4; |
| 141 |
assert(abc == "ABC"); |
| 142 |
printf("%Lg, %Lg, %Lg\n", cast(real)a[0], cast(real)a[1], cast(real)a[2]); |
| 143 |
assert(a[0] == 17); |
| 144 |
assert(a[1] == 22); |
| 145 |
assert(a[2] == 27); |
| 146 |
|
| 147 |
abc = null; |
| 148 |
A()[] = B()[] + C()[] * B()[]; |
| 149 |
assert(abc == "ABCB"); |
| 150 |
printf("%Lg, %Lg, %Lg\n", cast(real)a[0], cast(real)a[1], cast(real)a[2]); |
| 151 |
assert(a[0] == 5); |
| 152 |
assert(a[1] == 12); |
| 153 |
assert(a[2] == 21); |
| 154 |
|
| 155 |
abc = null; |
| 156 |
A()[] = B()[] + C()[] / 2; |
| 157 |
assert(abc == "ABC"); |
| 158 |
printf("%Lg, %Lg, %Lg\n", cast(real)a[0], cast(real)a[1], cast(real)a[2]); |
| 159 |
assert(a[0] == 3); |
| 160 |
assert(a[1] == 4.5); |
| 161 |
assert(a[2] == 6); |
| 162 |
|
| 163 |
abc = null; |
| 164 |
A()[] = B()[] + C()[] % 2; |
| 165 |
assert(abc == "ABC"); |
| 166 |
printf("%Lg, %Lg, %Lg\n", cast(real)a[0], cast(real)a[1], cast(real)a[2]); |
| 167 |
assert(a[0] == 1); |
| 168 |
assert(a[1] == 3); |
| 169 |
assert(a[2] == 3); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
mixin Floating!(float) Ffloat; |
| 174 |
mixin Floating!(double) Fdouble; |
| 175 |
mixin Floating!(real) Freal; |
| 176 |
|
| 177 |
void test1() |
| 178 |
{ |
| 179 |
Ffloat.testx(); |
| 180 |
Fdouble.testx(); |
| 181 |
Freal.testx(); |
| 182 |
} |
| 183 |
|
| 184 |
/************************************************************************/ |
| 185 |
|
| 186 |
template Integral(T) |
| 187 |
{ |
| 188 |
T[3] a; |
| 189 |
T[3] b; |
| 190 |
T[3] c; |
| 191 |
|
| 192 |
T[] A() |
| 193 |
{ |
| 194 |
printf("A\n"); |
| 195 |
abc ~= "A"; |
| 196 |
return a; |
| 197 |
} |
| 198 |
|
| 199 |
T[] B() |
| 200 |
{ |
| 201 |
printf("B\n"); |
| 202 |
abc ~= "B"; |
| 203 |
return b; |
| 204 |
} |
| 205 |
|
| 206 |
T[] C() |
| 207 |
{ |
| 208 |
printf("C\n"); |
| 209 |
abc ~= "C"; |
| 210 |
return c; |
| 211 |
} |
| 212 |
|
| 213 |
T D() |
| 214 |
{ |
| 215 |
printf("D\n"); |
| 216 |
abc ~= "D"; |
| 217 |
return 4; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
void testx() |
| 222 |
{ |
| 223 |
a = [11, 22, 33]; |
| 224 |
b = [1, 2, 3]; |
| 225 |
c = [4, 5, 6]; |
| 226 |
|
| 227 |
abc = null; |
| 228 |
A()[] = B()[] + C()[]; |
| 229 |
assert(abc == "ABC"); |
| 230 |
assert(a[0] == 5); |
| 231 |
assert(a[1] == 7); |
| 232 |
assert(a[2] == 9); |
| 233 |
|
| 234 |
abc = null; |
| 235 |
A()[] = B()[] + 4; |
| 236 |
assert(abc == "AB"); |
| 237 |
assert(a[0] == 5); |
| 238 |
assert(a[1] == 6); |
| 239 |
assert(a[2] == 7); |
| 240 |
|
| 241 |
abc = null; |
| 242 |
A()[] = 4 + B()[]; |
| 243 |
assert(abc == "AB"); |
| 244 |
assert(a[0] == 5); |
| 245 |
assert(a[1] == 6); |
| 246 |
assert(a[2] == 7); |
| 247 |
|
| 248 |
abc = null; |
| 249 |
A()[] = D() + B()[]; |
| 250 |
assert(abc == "ADB"); |
| 251 |
assert(a[0] == 5); |
| 252 |
assert(a[1] == 6); |
| 253 |
assert(a[2] == 7); |
| 254 |
|
| 255 |
a = [11, 22, 33]; |
| 256 |
abc = null; |
| 257 |
A()[] += B()[]; |
| 258 |
assert(abc == "AB"); |
| 259 |
assert(a[0] == 12); |
| 260 |
assert(a[1] == 24); |
| 261 |
assert(a[2] == 36); |
| 262 |
|
| 263 |
a = [11, 22, 33]; |
| 264 |
A()[] += 4; |
| 265 |
assert(a[0] == 15); |
| 266 |
assert(a[1] == 26); |
| 267 |
assert(a[2] == 37); |
| 268 |
|
| 269 |
a = [11, 22, 33]; |
| 270 |
A()[] -= 4; |
| 271 |
assert(a[0] == 7); |
| 272 |
assert(a[1] == 18); |
| 273 |
assert(a[2] == 29); |
| 274 |
|
| 275 |
a = [11, 22, 27]; |
| 276 |
A()[] *= 4; |
| 277 |
assert(a[0] == 44); |
| 278 |
assert(a[1] == 88); |
| 279 |
assert(a[2] == 108); |
| 280 |
|
| 281 |
a = [11, 22, 33]; |
| 282 |
A()[] /= 4; |
| 283 |
assert(a[0] == 2); |
| 284 |
assert(a[1] == 5); |
| 285 |
assert(a[2] == 8); |
| 286 |
|
| 287 |
a = [11, 22, 33]; |
| 288 |
A()[] %= 4; |
| 289 |
assert(a[0] == 3); |
| 290 |
assert(a[1] == 2); |
| 291 |
assert(a[2] == 1); |
| 292 |
|
| 293 |
a = [1, 2, 7]; |
| 294 |
A()[] &= 4; |
| 295 |
assert(a[0] == 0); |
| 296 |
assert(a[1] == 0); |
| 297 |
assert(a[2] == 4); |
| 298 |
|
| 299 |
a = [1, 2, 7]; |
| 300 |
A()[] |= 4; |
| 301 |
assert(a[0] == 5); |
| 302 |
assert(a[1] == 6); |
| 303 |
assert(a[2] == 7); |
| 304 |
|
| 305 |
a = [1, 2, 7]; |
| 306 |
A()[] ^= 4; |
| 307 |
assert(a[0] == 5); |
| 308 |
assert(a[1] == 6); |
| 309 |
assert(a[2] == 3); |
| 310 |
|
| 311 |
a = [11, 22, 33]; |
| 312 |
abc = null; |
| 313 |
A()[] += 4 + B()[]; |
| 314 |
assert(abc == "AB"); |
| 315 |
assert(a[0] == 16); |
| 316 |
assert(a[1] == 28); |
| 317 |
assert(a[2] == 40); |
| 318 |
|
| 319 |
abc = null; |
| 320 |
A()[] = B()[] - C()[]; |
| 321 |
assert(abc == "ABC"); |
| 322 |
printf("%lld, %lld, %lld\n", cast(long)a[0], cast(long)a[1], cast(long)a[2]); |
| 323 |
assert(a[0] == -3); |
| 324 |
assert(a[1] == -3); |
| 325 |
assert(a[2] == -3); |
| 326 |
|
| 327 |
abc = null; |
| 328 |
A()[] = -B()[] - C()[]; |
| 329 |
assert(abc == "ABC"); |
| 330 |
printf("%lld, %lld, %lld\n", cast(long)a[0], cast(long)a[1], cast(long)a[2]); |
| 331 |
assert(a[0] == -5); |
| 332 |
assert(a[1] == -7); |
| 333 |
assert(a[2] == -9); |
| 334 |
|
| 335 |
abc = null; |
| 336 |
A()[] = B()[] + C()[] * 4; |
| 337 |
assert(abc == "ABC"); |
| 338 |
printf("%lld, %lld, %lld\n", cast(long)a[0], cast(long)a[1], cast(long)a[2]); |
| 339 |
assert(a[0] == 17); |
| 340 |
assert(a[1] == 22); |
| 341 |
assert(a[2] == 27); |
| 342 |
|
| 343 |
abc = null; |
| 344 |
A()[] = B()[] + C()[] * B()[]; |
| 345 |
assert(abc == "ABCB"); |
| 346 |
printf("%lld, %lld, %lld\n", cast(long)a[0], cast(long)a[1], cast(long)a[2]); |
| 347 |
assert(a[0] == 5); |
| 348 |
assert(a[1] == 12); |
| 349 |
assert(a[2] == 21); |
| 350 |
|
| 351 |
abc = null; |
| 352 |
A()[] = B()[] + C()[] / 2; |
| 353 |
assert(abc == "ABC"); |
| 354 |
printf("%lld, %lld, %lld\n", cast(long)a[0], cast(long)a[1], cast(long)a[2]); |
| 355 |
assert(a[0] == 3); |
| 356 |
assert(a[1] == 4); |
| 357 |
assert(a[2] == 6); |
| 358 |
|
| 359 |
abc = null; |
| 360 |
A()[] = B()[] + C()[] % 2; |
| 361 |
assert(abc == "ABC"); |
| 362 |
printf("%lld, %lld, %lld\n", cast(long)a[0], cast(long)a[1], cast(long)a[2]); |
| 363 |
assert(a[0] == 1); |
| 364 |
assert(a[1] == 3); |
| 365 |
assert(a[2] == 3); |
| 366 |
|
| 367 |
abc = null; |
| 368 |
A()[] = ~B()[]; |
| 369 |
assert(abc == "AB"); |
| 370 |
assert(a[0] == cast(T) ~1); |
| 371 |
assert(a[1] == cast(T) ~2); |
| 372 |
assert(a[2] == cast(T) ~3); |
| 373 |
|
| 374 |
abc = null; |
| 375 |
A()[] = B()[] & 2; |
| 376 |
assert(abc == "AB"); |
| 377 |
assert(a[0] == 0); |
| 378 |
assert(a[1] == 2); |
| 379 |
assert(a[2] == 2); |
| 380 |
|
| 381 |
abc = null; |
| 382 |
A()[] = B()[] | 2; |
| 383 |
assert(abc == "AB"); |
| 384 |
assert(a[0] == 3); |
| 385 |
assert(a[1] == 2); |
| 386 |
assert(a[2] == 3); |
| 387 |
|
| 388 |
abc = null; |
| 389 |
A()[] = B()[] ^ 2; |
| 390 |
assert(abc == "AB"); |
| 391 |
assert(a[0] == 3); |
| 392 |
assert(a[1] == 0); |
| 393 |
assert(a[2] == 1); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/************************************************************************/ |
| 398 |
|
| 399 |
mixin Integral!(byte) Fbyte; |
| 400 |
mixin Integral!(short) Fshort; |
| 401 |
mixin Integral!(int) Fint; |
| 402 |
mixin Integral!(long) Flong; |
| 403 |
|
| 404 |
void test2() |
| 405 |
{ |
| 406 |
Fbyte.testx(); |
| 407 |
Fshort.testx(); |
| 408 |
Fint.testx(); |
| 409 |
Flong.testx(); |
| 410 |
} |
| 411 |
|
| 412 |
/************************************************************************/ |
| 413 |
|
| 414 |
void test3() |
| 415 |
{ |
| 416 |
auto a = new double[10], b = a.dup, c = a.dup, d = a.dup; |
| 417 |
a[] = -(b[] * (c[] + 4)) + 5 * d[] / 3.0; |
| 418 |
} |
| 419 |
|
| 420 |
/************************************************************************/ |
| 421 |
|
| 422 |
void test4() |
| 423 |
{ |
| 424 |
int[] a, b; |
| 425 |
if (a && b) {} |
| 426 |
} |
| 427 |
|
| 428 |
/***************************************************/ |
| 429 |
|
| 430 |
void test4662() |
| 431 |
{ |
| 432 |
immutable double[] nums = [1.0, 2.0]; |
| 433 |
|
| 434 |
static assert(!is(typeof({ nums[] += nums[]; }))); |
| 435 |
static assert(!is(typeof({ nums[] -= nums[]; }))); |
| 436 |
static assert(!is(typeof({ nums[] /= nums[]; }))); |
| 437 |
static assert(!is(typeof({ nums[] += 4; }))); |
| 438 |
static assert(!is(typeof({ nums[] /= 7; }))); |
| 439 |
} |
| 440 |
|
| 441 |
/***************************************************/ |
| 442 |
// https://issues.dlang.org/show_bug.cgi?id=5284 |
| 443 |
|
| 444 |
void bug5284_1() |
| 445 |
{ |
| 446 |
class C { int v; } |
| 447 |
|
| 448 |
C [] mda; |
| 449 |
immutable(C)[] ida; |
| 450 |
static assert(!__traits(compiles, (mda[] = ida[]))); |
| 451 |
|
| 452 |
C [1] msa; |
| 453 |
immutable(C)[1] isa; |
| 454 |
static assert(!__traits(compiles, (msa[] = isa[]))); |
| 455 |
|
| 456 |
C m; |
| 457 |
immutable(C) i; |
| 458 |
static assert(!__traits(compiles, m = i)); |
| 459 |
} |
| 460 |
void bug5284_2a() |
| 461 |
{ |
| 462 |
struct S { int v; } |
| 463 |
|
| 464 |
S [] mda; |
| 465 |
immutable(S)[] ida; |
| 466 |
mda[] = ida[]; |
| 467 |
|
| 468 |
S [1] msa; |
| 469 |
immutable(S)[1] isa; |
| 470 |
msa[] = isa[]; |
| 471 |
|
| 472 |
S m = S(); |
| 473 |
immutable(S) i = immutable(S)(); |
| 474 |
m = i; |
| 475 |
} |
| 476 |
void bug5284_2b() |
| 477 |
{ |
| 478 |
struct S { int v; int[] arr; } |
| 479 |
|
| 480 |
S [] mda; |
| 481 |
immutable(S)[] ida; |
| 482 |
static assert(!__traits(compiles, (mda[] = ida[]))); |
| 483 |
|
| 484 |
S [1] msa; |
| 485 |
immutable(S)[1] isa; |
| 486 |
static assert(!__traits(compiles, (msa[] = isa[]))); |
| 487 |
|
| 488 |
S m; |
| 489 |
immutable(S) i; |
| 490 |
static assert(!__traits(compiles, m = i)); |
| 491 |
} |
| 492 |
void bug5284_3() |
| 493 |
{ |
| 494 |
int [] ma; |
| 495 |
immutable(int)[] ia; |
| 496 |
ma[] = ia[]; |
| 497 |
|
| 498 |
int m; |
| 499 |
immutable(int) i; |
| 500 |
m = i; |
| 501 |
} |
| 502 |
|
| 503 |
void test5() |
| 504 |
{ |
| 505 |
bug5284_1(); |
| 506 |
bug5284_2a(); |
| 507 |
bug5284_2b(); |
| 508 |
bug5284_3(); |
| 509 |
} |
| 510 |
|
| 511 |
/************************************************************************/ |
| 512 |
|
| 513 |
void test6() |
| 514 |
{ |
| 515 |
int[10] a = [1,2,3,4,5,6,7,8,9,10]; |
| 516 |
int[10] b; |
| 517 |
|
| 518 |
b = a[] ^^ 2; |
| 519 |
assert(b[0] == 1); |
| 520 |
assert(b[1] == 4); |
| 521 |
assert(b[2] == 9); |
| 522 |
assert(b[3] == 16); |
| 523 |
assert(b[4] == 25); |
| 524 |
assert(b[5] == 36); |
| 525 |
assert(b[6] == 49); |
| 526 |
assert(b[7] == 64); |
| 527 |
assert(b[8] == 81); |
| 528 |
assert(b[9] == 100); |
| 529 |
|
| 530 |
int[10] c = 3; |
| 531 |
b = a[] ^^ c[]; |
| 532 |
assert(b[0] == 1); |
| 533 |
assert(b[1] == 8); |
| 534 |
assert(b[2] == 27); |
| 535 |
assert(b[3] == 64); |
| 536 |
assert(b[4] == 125); |
| 537 |
assert(b[5] == 216); |
| 538 |
assert(b[6] == 343); |
| 539 |
assert(b[7] == 512); |
| 540 |
assert(b[8] == 729); |
| 541 |
assert(b[9] == 1000); |
| 542 |
} |
| 543 |
|
| 544 |
/************************************************************************/ |
| 545 |
|
| 546 |
void test8390() { |
| 547 |
const int[] a = new int[5]; |
| 548 |
int[] b = new int[5]; |
| 549 |
b[] += a[]; |
| 550 |
} |
| 551 |
|
| 552 |
/************************************************************************/ |
| 553 |
// https://issues.dlang.org/show_bug.cgi?id=8651 |
| 554 |
|
| 555 |
void test8651() |
| 556 |
{ |
| 557 |
void test(T)() @safe pure nothrow |
| 558 |
{ |
| 559 |
T[3] a = [11, 22, 33]; |
| 560 |
T[3] b = [1, 2, 3]; |
| 561 |
T[3] c = [4, 5, 6]; |
| 562 |
T d = 4; |
| 563 |
|
| 564 |
// Arithmetic array ops |
| 565 |
{ |
| 566 |
a[] = b[] + c[]; |
| 567 |
a[] = b[] + 4; |
| 568 |
a[] = 4 + b[]; |
| 569 |
a[] = d + b[]; |
| 570 |
a[] += b[]; |
| 571 |
a[] += 4; |
| 572 |
a[] -= 4; |
| 573 |
a[] *= 4; |
| 574 |
a[] /= 4; |
| 575 |
a[] %= 4; |
| 576 |
a[] += 4 + b[]; |
| 577 |
a[] = b[] - c[]; |
| 578 |
a[] = -b[] - c[]; |
| 579 |
a[] = b[] + c[] * 4; |
| 580 |
a[] = b[] + c[] * b[]; |
| 581 |
a[] = b[] + c[] / 2; |
| 582 |
a[] = b[] + c[] % 2; |
| 583 |
} |
| 584 |
// Bitwise array ops |
| 585 |
static if (is(typeof(T.init & T.init))) |
| 586 |
{ |
| 587 |
a[] &= 4; |
| 588 |
a[] |= 4; |
| 589 |
a[] ^= 4; |
| 590 |
a[] = ~b[]; |
| 591 |
a[] = b[] & 2; |
| 592 |
a[] = b[] | 2; |
| 593 |
a[] = b[] ^ 2; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
test!float(); |
| 598 |
test!double(); |
| 599 |
test!real(); |
| 600 |
|
| 601 |
test!byte(); |
| 602 |
test!short(); |
| 603 |
test!int(); |
| 604 |
test!long(); |
| 605 |
} |
| 606 |
|
| 607 |
/************************************************************************/ |
| 608 |
// https://issues.dlang.org/show_bug.cgi?id=9656 |
| 609 |
|
| 610 |
void test9656() |
| 611 |
{ |
| 612 |
static class C {} |
| 613 |
static struct S |
| 614 |
{ |
| 615 |
immutable int[] narr1; |
| 616 |
immutable int[] narr2; |
| 617 |
immutable C[] carr1; |
| 618 |
immutable C[] carr2; |
| 619 |
this(int n) { |
| 620 |
narr1 = new int[](3); // OK, expected |
| 621 |
narr2 = [1,2,3].dup; // NG -> OK |
| 622 |
carr1 = [new C].dup; // NG -> OK |
| 623 |
|
| 624 |
C c = new C; |
| 625 |
static assert(!__traits(compiles, carr2 = [c])); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
{ |
| 630 |
int[] ma = [1,2,3]; |
| 631 |
immutable ia = ma.idup; |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
{ |
| 636 |
static struct V { int val; } |
| 637 |
V[] ma = [V(1), V(2)]; |
| 638 |
immutable ia = ma.idup; |
| 639 |
} |
| 640 |
|
| 641 |
{ |
| 642 |
static struct R { int* ptr; } |
| 643 |
R[] ma = [R(new int), R(null)]; |
| 644 |
static assert(!__traits(compiles, { immutable ia = rarr.dup; })); |
| 645 |
} |
| 646 |
|
| 647 |
{ |
| 648 |
C[] ma = [new C(), new C()]; |
| 649 |
static assert(!__traits(compiles, { immutable ia = carr.dup; })); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
/************************************************************************/ |
| 654 |
// https://issues.dlang.org/show_bug.cgi?id=10282 |
| 655 |
|
| 656 |
void test10282() |
| 657 |
{ |
| 658 |
int[3] a1 = [1, 3, 6]; |
| 659 |
int[3] a2 = [1, 3, 6] * 3; // OK |
| 660 |
const int[3] a3 = a1[] * 3; // OK <- Error |
| 661 |
const int[3] a4 = [1, 3, 6] * 3; // OK <- Error |
| 662 |
immutable int[3] a5 = [1, 3, 6] * 3; // OK <- Error |
| 663 |
|
| 664 |
assert(a1[0] == 1 && a1[1] == 3 && a1[2] == 6); |
| 665 |
assert(a2[0] == 3 && a2[1] == 9 && a2[2] == 18); |
| 666 |
assert(a3[0] == 3 && a3[1] == 9 && a3[2] == 18); |
| 667 |
assert(a4[0] == 3 && a4[1] == 9 && a4[2] == 18); |
| 668 |
assert(a5[0] == 3 && a5[1] == 9 && a5[2] == 18); |
| 669 |
} |
| 670 |
|
| 671 |
/************************************************************************/ |
| 672 |
// https://issues.dlang.org/show_bug.cgi?id=10433 |
| 673 |
|
| 674 |
void test10433() |
| 675 |
{ |
| 676 |
void foo(T)(in int[] v1, in T v2) |
| 677 |
{ |
| 678 |
int[2] r; |
| 679 |
r[] = v1[] + v2[]; |
| 680 |
} |
| 681 |
|
| 682 |
immutable int[] v = [10, 20]; |
| 683 |
foo(v, v); |
| 684 |
} |
| 685 |
|
| 686 |
/************************************************************************/ |
| 687 |
// https://issues.dlang.org/show_bug.cgi?id=10684 |
| 688 |
|
| 689 |
void test10684a() |
| 690 |
{ |
| 691 |
int[] a = [0, 0]; |
| 692 |
a[] += [10, 20][]; |
| 693 |
} |
| 694 |
|
| 695 |
void test10684b() |
| 696 |
{ |
| 697 |
int[] a = [1, 2, 3]; |
| 698 |
int[] b = [4, 5, 6]; |
| 699 |
|
| 700 |
// Allow array literal as the operand of array oeration |
| 701 |
a[] += [1, 2, 3]; |
| 702 |
assert(a == [2, 4, 6]); |
| 703 |
|
| 704 |
a[] *= b[] + [1, 1, 1]; |
| 705 |
assert(a == [2*(4+1), 4*(5+1), 6*(6+1)]); |
| 706 |
|
| 707 |
a[] = [9, 8, 7] - [1, 2, 3]; |
| 708 |
assert(a == [8, 6, 4]); |
| 709 |
|
| 710 |
a[] = [2, 4, 6] / 2; |
| 711 |
assert(a == [1,2,3]); |
| 712 |
|
| 713 |
// Disallow: [1,2,3] is not an lvalue |
| 714 |
static assert(!__traits(compiles, { [1,2,3] = a[] * 2; })); |
| 715 |
static assert(!__traits(compiles, { [1,2,3] += a[] * b[]; })); |
| 716 |
} |
| 717 |
|
| 718 |
/************************************************************************/ |
| 719 |
// https://issues.dlang.org/show_bug.cgi?id=11376 |
| 720 |
|
| 721 |
template TL11376(T...) |
| 722 |
{ |
| 723 |
alias TL11376 = T; |
| 724 |
} |
| 725 |
|
| 726 |
auto sumArrs11376(T0, T1)(T0[] a, T1[] b) |
| 727 |
{ |
| 728 |
a[] += b[]; //no ICE without this line |
| 729 |
return a; |
| 730 |
} |
| 731 |
|
| 732 |
static assert(!__traits(compiles, sumArrs11376(TL11376!(string[], string).init))); |
| 733 |
|
| 734 |
/************************************************************************/ |
| 735 |
// https://issues.dlang.org/show_bug.cgi?id=11525 |
| 736 |
|
| 737 |
void test11525() |
| 738 |
{ |
| 739 |
static struct Complex(T) |
| 740 |
{ |
| 741 |
T re, im; |
| 742 |
|
| 743 |
ref opOpAssign(string op : "*")(Complex z) |
| 744 |
{ |
| 745 |
auto temp = re*z.re - im*z.im; |
| 746 |
im = im*z.re + re*z.im; |
| 747 |
re = temp; |
| 748 |
return this; |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
auto a = [Complex!double(2, 2)]; |
| 753 |
assert(a.length == 1 && a[0].re == 2 && a[0].im == 2); |
| 754 |
a[] *= a[]; |
| 755 |
assert(a.length == 1 && a[0].re == 0 && a[0].im == 8); |
| 756 |
} |
| 757 |
|
| 758 |
/************************************************************************/ |
| 759 |
// https://issues.dlang.org/show_bug.cgi?id=12250 |
| 760 |
|
| 761 |
void f12250(inout int[] p, inout int[] q, int[] r) |
| 762 |
{ |
| 763 |
r[] = p[] + q[]; |
| 764 |
assert(r == [5,7,9]); |
| 765 |
r[] -= p[] - q[]; |
| 766 |
assert(r == [8,10,12]); |
| 767 |
} |
| 768 |
|
| 769 |
void test12250() |
| 770 |
{ |
| 771 |
immutable int[3] x = [1,2,3], y = [4,5,6]; |
| 772 |
int[3] z; |
| 773 |
f12250(x[], y[], z[]); |
| 774 |
} |
| 775 |
|
| 776 |
/************************************************************************/ |
| 777 |
// https://issues.dlang.org/show_bug.cgi?id=12179 |
| 778 |
|
| 779 |
void test12179() |
| 780 |
{ |
| 781 |
void foo(int[]) {} |
| 782 |
int[1] a; |
| 783 |
|
| 784 |
foo(a[] = a[]); |
| 785 |
foo(a[] += a[]); |
| 786 |
foo(a[] -= a[]); |
| 787 |
foo(a[] *= a[]); |
| 788 |
foo(a[] /= a[]); |
| 789 |
foo(a[] %= a[]); |
| 790 |
foo(a[] ^= a[]); |
| 791 |
foo(a[] &= a[]); |
| 792 |
foo(a[] |= a[]); |
| 793 |
foo(a[] ^^= a[]); |
| 794 |
|
| 795 |
// from https://issues.dlang.org/show_bug.cgi?id=11992 |
| 796 |
int[] arr1; |
| 797 |
int[][] arr2; |
| 798 |
arr1 ~= (a[] = [1] + a[]); // OK |
| 799 |
arr2 ~= (a[] = [1] + a[]); // OK |
| 800 |
} |
| 801 |
|
| 802 |
/************************************************************************/ |
| 803 |
// https://issues.dlang.org/show_bug.cgi?id=12780 |
| 804 |
|
| 805 |
void test12780() |
| 806 |
{ |
| 807 |
int ival = 2; |
| 808 |
int[] iarr = [1, 2, 3]; |
| 809 |
double dval = 2.0; |
| 810 |
double[] darr = [4, 5, 6]; |
| 811 |
|
| 812 |
double[] oarr = [0, 0, 0]; |
| 813 |
|
| 814 |
// multiply array operations |
| 815 |
oarr[] = dval * iarr[]; |
| 816 |
assert(oarr == [dval * iarr[0], |
| 817 |
dval * iarr[1], |
| 818 |
dval * iarr[2]]); |
| 819 |
|
| 820 |
oarr[] = iarr[] / dval; |
| 821 |
assert(oarr == [iarr[0] / dval, |
| 822 |
iarr[1] / dval, |
| 823 |
iarr[2] / dval]); |
| 824 |
|
| 825 |
oarr[] = dval * (ival + iarr[]); |
| 826 |
assert(oarr == [dval * (ival + iarr[0]), |
| 827 |
dval * (ival + iarr[1]), |
| 828 |
dval * (ival + iarr[2])]); |
| 829 |
|
| 830 |
oarr[] = (iarr[] & ival) / dval; |
| 831 |
assert(oarr == [(iarr[0] & ival) / dval, |
| 832 |
(iarr[1] & ival) / dval, |
| 833 |
(iarr[2] & ival) / dval]); |
| 834 |
|
| 835 |
oarr[] = darr[] + iarr[]; |
| 836 |
assert(oarr == [darr[0] + iarr[0], |
| 837 |
darr[1] + iarr[1], |
| 838 |
darr[2] + iarr[2]]); |
| 839 |
|
| 840 |
oarr[] = iarr[] - darr[]; |
| 841 |
assert(oarr == [iarr[0] - darr[0], |
| 842 |
iarr[1] - darr[1], |
| 843 |
iarr[2] - darr[2]]); |
| 844 |
|
| 845 |
oarr[] = darr[] * (ival & iarr[]); |
| 846 |
assert(oarr == [darr[0] * (ival & iarr[0]), |
| 847 |
darr[1] * (ival & iarr[1]), |
| 848 |
darr[2] * (ival & iarr[2])]); |
| 849 |
|
| 850 |
oarr[] = (iarr[] ^ ival) / darr[]; |
| 851 |
assert(oarr == [(iarr[0] ^ ival) / darr[0], |
| 852 |
(iarr[1] ^ ival) / darr[1], |
| 853 |
(iarr[2] ^ ival) / darr[2]]); |
| 854 |
} |
| 855 |
|
| 856 |
/************************************************************************/ |
| 857 |
// https://issues.dlang.org/show_bug.cgi?id=13497 |
| 858 |
|
| 859 |
void test13497() |
| 860 |
{ |
| 861 |
int[1] a = [2], b = [3]; |
| 862 |
int[1] c1 = a[] * b[]; |
| 863 |
int[1] c2 = (a[] * b[])[]; |
| 864 |
assert(c1 == [6]); |
| 865 |
assert(c2 == [6]); |
| 866 |
} |
| 867 |
|
| 868 |
/************************************************************************/ |
| 869 |
// https://issues.dlang.org/show_bug.cgi?id=14649 |
| 870 |
|
| 871 |
void test14649() |
| 872 |
{ |
| 873 |
char[] a = "abc".dup; |
| 874 |
char[] b = [char(1), char(2), char(3)]; |
| 875 |
string x = "abc"; |
| 876 |
string y = [char(1), char(2), char(3)]; |
| 877 |
char[] r = new char[](3); |
| 878 |
|
| 879 |
r[] = a[] + b[]; |
| 880 |
assert(r == "bdf"); |
| 881 |
|
| 882 |
r[] = x[] + y[]; |
| 883 |
assert(r == "bdf"); |
| 884 |
|
| 885 |
r[] = "hel"[] + "lo."[]; |
| 886 |
assert(r == [('h'+'l'), ('e'+'o'), ('l'+'.')]); |
| 887 |
|
| 888 |
enum s = "abc"; |
| 889 |
r[] = s[0..3] + "def"[0..3]; |
| 890 |
assert(r == [('a'+'d'), ('b'+'e'), ('c'+'f')]); |
| 891 |
} |
| 892 |
|
| 893 |
/************************************************************************/ |
| 894 |
// https://issues.dlang.org/show_bug.cgi?id=14851 |
| 895 |
|
| 896 |
void test14851() |
| 897 |
{ |
| 898 |
int[8] a, b, c; |
| 899 |
|
| 900 |
c = a[] | b[]; // OK <- NG from 2.068.0-b2 |
| 901 |
c = a[] ^ b[]; // OK <- NG from 2.068.0-b2 |
| 902 |
|
| 903 |
c[] = a[] | b[]; // OK |
| 904 |
c[] = a[] ^ b[]; // OK |
| 905 |
} |
| 906 |
|
| 907 |
/************************************************************************/ |
| 908 |
|
| 909 |
int main() |
| 910 |
{ |
| 911 |
version(X86) |
| 912 |
{ |
| 913 |
test1(); |
| 914 |
test2(); |
| 915 |
} |
| 916 |
else version(X86_64) |
| 917 |
{ |
| 918 |
test1(); |
| 919 |
test2(); |
| 920 |
} |
| 921 |
else |
| 922 |
{ |
| 923 |
//pragma(msg, "Test skipped because arrayop evaluation order is ill-defined."); |
| 924 |
} |
| 925 |
test3(); |
| 926 |
test4(); |
| 927 |
test5(); |
| 928 |
test6(); |
| 929 |
test8390(); |
| 930 |
test8651(); |
| 931 |
test9656(); |
| 932 |
test10282(); |
| 933 |
test10433(); |
| 934 |
test10684a(); |
| 935 |
test10684b(); |
| 936 |
test11525(); |
| 937 |
test12250(); |
| 938 |
test12780(); |
| 939 |
test13497(); |
| 940 |
test14649(); |
| 941 |
test14851(); |
| 942 |
|
| 943 |
printf("Success\n"); |
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
version (none) |
| 949 |
{ |
| 950 |
extern (C) T[] _arraySliceSliceAddSliceAssignd(T[] a, T[] c, T[] b) |
| 951 |
{ |
| 952 |
foreach (i; 0 .. a.length) |
| 953 |
a[i] = b[i] + c[i]; |
| 954 |
return a; |
| 955 |
} |
| 956 |
} |