| 1 |
/* |
| 2 |
RUN_OUTPUT: |
| 3 |
--- |
| 4 |
i = 1 |
| 5 |
i = 64 |
| 6 |
12 |
| 7 |
534 |
| 8 |
Success |
| 9 |
--- |
| 10 |
*/ |
| 11 |
|
| 12 |
import core.stdc.stdio; |
| 13 |
|
| 14 |
/**************************************/ |
| 15 |
|
| 16 |
class A1 |
| 17 |
{ |
| 18 |
int opBinary(string op)(int i) if (op == "+") { return 7 + i; } |
| 19 |
alias opBinaryRight = opBinary; |
| 20 |
} |
| 21 |
|
| 22 |
void test1() |
| 23 |
{ |
| 24 |
A1 a = new A1(); |
| 25 |
int i; |
| 26 |
|
| 27 |
i = a + 3; |
| 28 |
assert(i == 10); |
| 29 |
|
| 30 |
i = 4 + a; |
| 31 |
assert(i == 11); |
| 32 |
} |
| 33 |
|
| 34 |
/**************************************/ |
| 35 |
|
| 36 |
class A2 |
| 37 |
{ |
| 38 |
int opBinary(string op)(int i) if (op == "/") { return 9 + i; } |
| 39 |
int opBinaryRight(string op)(int i) if (op == "/") { return 17 + i; } |
| 40 |
} |
| 41 |
|
| 42 |
void test2() |
| 43 |
{ |
| 44 |
A2 a = new A2(); |
| 45 |
int i; |
| 46 |
|
| 47 |
i = a / 3; |
| 48 |
assert(i == 12); |
| 49 |
|
| 50 |
i = 4 / a; |
| 51 |
assert(i == 21); |
| 52 |
} |
| 53 |
|
| 54 |
/**************************************/ |
| 55 |
|
| 56 |
class C1 |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
class C2 |
| 61 |
{ |
| 62 |
int opBinary(string op)(D1 d) if (op == "+") { return 1; } |
| 63 |
int opBinary(string op)(D2 d) if (op == "+") { return 2; } |
| 64 |
int opBinary(string op)(D3 d) if (op == "+") { return 3; } |
| 65 |
int opBinary(string op)(D4 d) if (op == "+") { return 4; } |
| 66 |
} |
| 67 |
|
| 68 |
class C3 |
| 69 |
{ |
| 70 |
int opBinaryRight(string op)(D1 d) if (op == "+") { return 5; } |
| 71 |
int opBinaryRight(string op)(D2 d) if (op == "+") { return 6; } |
| 72 |
int opBinaryRight(string op)(D3 d) if (op == "+") { return 7; } |
| 73 |
int opBinaryRight(string op)(D4 d) if (op == "+") { return 8; } |
| 74 |
} |
| 75 |
|
| 76 |
class C4 |
| 77 |
{ |
| 78 |
int opBinary(string op)(D1 d) if (op == "+") { return 9; } |
| 79 |
int opBinary(string op)(D2 d) if (op == "+") { return 10; } |
| 80 |
int opBinary(string op)(D3 d) if (op == "+") { return 11; } |
| 81 |
int opBinary(string op)(D4 d) if (op == "+") { return 12; } |
| 82 |
|
| 83 |
int opBinaryRight(string op)(D1 d) if (op == "+") { return 13; } |
| 84 |
int opBinaryRight(string op)(D2 d) if (op == "+") { return 14; } |
| 85 |
int opBinaryRight(string op)(D3 d) if (op == "+") { return 15; } |
| 86 |
int opBinaryRight(string op)(D4 d) if (op == "+") { return 16; } |
| 87 |
} |
| 88 |
|
| 89 |
class D1 |
| 90 |
{ |
| 91 |
} |
| 92 |
|
| 93 |
class D2 |
| 94 |
{ |
| 95 |
int opBinary(string op)(C1 d) if (op == "+") { return 17; } |
| 96 |
int opBinary(string op)(C2 d) if (op == "+") { return 18; } |
| 97 |
int opBinary(string op)(C3 d) if (op == "+") { return 19; } |
| 98 |
int opBinary(string op)(C4 d) if (op == "+") { return 20; } |
| 99 |
} |
| 100 |
|
| 101 |
class D3 |
| 102 |
{ |
| 103 |
int opBinaryRight(string op)(C1 d) if (op == "+") { return 21; } |
| 104 |
int opBinaryRight(string op)(C2 d) if (op == "+") { return 22; } |
| 105 |
int opBinaryRight(string op)(C3 d) if (op == "+") { return 23; } |
| 106 |
int opBinaryRight(string op)(C4 d) if (op == "+") { return 24; } |
| 107 |
} |
| 108 |
|
| 109 |
class D4 |
| 110 |
{ |
| 111 |
int opBinary(string op)(C1 d) if (op == "+") { return 25; } |
| 112 |
int opBinary(string op)(C2 d) if (op == "+") { return 26; } |
| 113 |
int opBinary(string op)(C3 d) if (op == "+") { return 27; } |
| 114 |
int opBinary(string op)(C4 d) if (op == "+") { return 28; } |
| 115 |
|
| 116 |
int opBinaryRight(string op)(C1 d) if (op == "+") { return 29; } |
| 117 |
int opBinaryRight(string op)(C2 d) if (op == "+") { return 30; } |
| 118 |
int opBinaryRight(string op)(C3 d) if (op == "+") { return 31; } |
| 119 |
int opBinaryRight(string op)(C4 d) if (op == "+") { return 32; } |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
void test3() |
| 125 |
{ |
| 126 |
C1 c1 = new C1(); |
| 127 |
C2 c2 = new C2(); |
| 128 |
C3 c3 = new C3(); |
| 129 |
C4 c4 = new C4(); |
| 130 |
D1 d1 = new D1(); |
| 131 |
D2 d2 = new D2(); |
| 132 |
D3 d3 = new D3(); |
| 133 |
D4 d4 = new D4(); |
| 134 |
|
| 135 |
int i; |
| 136 |
|
| 137 |
//i = c1 + d1; assert(i == ); |
| 138 |
//i = c1 + d2; assert(i == ); |
| 139 |
i = c1 + d3; assert(i == 21); |
| 140 |
i = c1 + d4; assert(i == 29); |
| 141 |
|
| 142 |
i = c2 + d1; assert(i == 1); |
| 143 |
i = c2 + d2; assert(i == 2); |
| 144 |
i = c2 + d3; assert(i == 22); |
| 145 |
i = c2 + d4; assert(i == 30); |
| 146 |
|
| 147 |
//i = c3 + d1; assert(i == ); |
| 148 |
//i = c3 + d2; assert(i == ); |
| 149 |
i = c3 + d3; assert(i == 23); |
| 150 |
i = c3 + d4; assert(i == 31); |
| 151 |
|
| 152 |
i = c4 + d1; assert(i == 9); |
| 153 |
i = c4 + d2; assert(i == 10); |
| 154 |
i = c4 + d3; assert(i == 24); |
| 155 |
i = c4 + d4; assert(i == 32); |
| 156 |
|
| 157 |
//i = d1 + c1; assert(i == ); |
| 158 |
//i = d1 + c2; assert(i == ); |
| 159 |
i = d1 + c3; assert(i == 5); |
| 160 |
i = d1 + c4; assert(i == 13); |
| 161 |
|
| 162 |
i = d2 + c1; assert(i == 17); |
| 163 |
i = d2 + c2; assert(i == 18); |
| 164 |
i = d2 + c3; assert(i == 6); |
| 165 |
i = d2 + c4; assert(i == 14); |
| 166 |
|
| 167 |
//i = d3 + c1; assert(i == ); |
| 168 |
//i = d3 + c2; assert(i == ); |
| 169 |
i = d3 + c3; assert(i == 7); |
| 170 |
i = d3 + c4; assert(i == 15); |
| 171 |
|
| 172 |
i = d4 + c1; assert(i == 25); |
| 173 |
i = d4 + c2; assert(i == 26); |
| 174 |
i = d4 + c3; assert(i == 8); |
| 175 |
i = d4 + c4; assert(i == 16); |
| 176 |
} |
| 177 |
|
| 178 |
/**************************************/ |
| 179 |
|
| 180 |
struct Foo4 |
| 181 |
{ |
| 182 |
int a; |
| 183 |
int opCmp(Foo4 b) |
| 184 |
{ |
| 185 |
return a < b.a; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void test4() |
| 190 |
{ |
| 191 |
Foo4 a; |
| 192 |
Foo4 b; |
| 193 |
|
| 194 |
assert(a <= b); |
| 195 |
} |
| 196 |
|
| 197 |
/**************************************/ |
| 198 |
|
| 199 |
class A5 |
| 200 |
{ |
| 201 |
int opUnary(string op)() if (op == "-") { return 10; } |
| 202 |
int opUnary(string op)() if (op == "~") { return 11; } |
| 203 |
int opUnary(string op)() if (op == "++") { return 12; } |
| 204 |
int opUnary(string op)() if (op == "--") { return 13; } |
| 205 |
|
| 206 |
|
| 207 |
int opBinary(string op)(int j) if (op == "+") { return 14; } |
| 208 |
int opBinary(string op)(int j) if (op == "-") { return 15; } |
| 209 |
int opBinaryRight(string op)(int j) if (op == "-") { return 16; } |
| 210 |
int opBinary(string op)(int j) if (op == "*") { return 17; } |
| 211 |
int opBinary(string op)(int j) if (op == "/") { return 18; } |
| 212 |
int opBinaryRight(string op)(int j) if (op == "/") { return 19; } |
| 213 |
int opBinary(string op)(int j) if (op == "%") { return 20; } |
| 214 |
int opBinaryRight(string op)(int j) if (op == "%") { return 21; } |
| 215 |
int opBinary(string op)(int j) if (op == "&") { return 22; } |
| 216 |
int opBinary(string op)(int j) if (op == "|") { return 23; } |
| 217 |
int opBinary(string op)(int j) if (op == "^") { return 24; } |
| 218 |
int opBinary(string op)(int j) if (op == "<<") { return 25; } |
| 219 |
int opBinaryRight(string op)(int j) if (op == "<<") { return 26; } |
| 220 |
int opBinary(string op)(int j) if (op == ">>") { return 27; } |
| 221 |
int opBinaryRight(string op)(int j) if (op == ">>") { return 28; } |
| 222 |
int opBinary(string op)(int j) if (op == ">>>") { return 29; } |
| 223 |
int opBinaryRight(string op)(int j) if (op == ">>>") { return 30; } |
| 224 |
int opBinary(string op)(int j) if (op == "~") { return 31; } |
| 225 |
int opBinaryRight(string op)(int j) if (op == "~") { return 32; } |
| 226 |
int opEquals(int j) { return 33; } |
| 227 |
int opCmp(int j) { return 34; } |
| 228 |
int opOpAssign(string op)(int j) if (op == "+") { return 35; } |
| 229 |
int opOpAssign(string op)(int j) if (op == "-") { return 36; } |
| 230 |
int opOpAssign(string op)(int j) if (op == "*") { return 37; } |
| 231 |
int opOpAssign(string op)(int j) if (op == "/") { return 38; } |
| 232 |
int opOpAssign(string op)(int j) if (op == "%") { return 39; } |
| 233 |
int opOpAssign(string op)(int j) if (op == "&") { return 40; } |
| 234 |
int opOpAssign(string op)(int j) if (op == "|") { return 41; } |
| 235 |
int opOpAssign(string op)(int j) if (op == "^") { return 42; } |
| 236 |
int opOpAssign(string op)(int j) if (op == "<<") { return 43; } |
| 237 |
int opOpAssign(string op)(int j) if (op == ">>") { return 44; } |
| 238 |
int opOpAssign(string op)(int j) if (op == ">>>") { return 45; } |
| 239 |
int opOpAssign(string op)(int j) if (op == "~") { return 46; } |
| 240 |
} |
| 241 |
|
| 242 |
void test5() |
| 243 |
{ |
| 244 |
A5 a = new A5(); |
| 245 |
int i; |
| 246 |
|
| 247 |
i = -a; |
| 248 |
assert(i == 10); |
| 249 |
|
| 250 |
i = ~a; |
| 251 |
assert(i == 11); |
| 252 |
|
| 253 |
i = ++a; |
| 254 |
assert(i == 12); |
| 255 |
|
| 256 |
i = --a; |
| 257 |
assert(i == 13); |
| 258 |
|
| 259 |
i = a + 1; |
| 260 |
assert(i == 14); |
| 261 |
|
| 262 |
i = a - 1; |
| 263 |
assert(i == 15); |
| 264 |
|
| 265 |
i = 1 - a; |
| 266 |
assert(i == 16); |
| 267 |
|
| 268 |
i = a * 1; |
| 269 |
assert(i == 17); |
| 270 |
|
| 271 |
i = a / 1; |
| 272 |
assert(i == 18); |
| 273 |
|
| 274 |
i = 1 / a; |
| 275 |
assert(i == 19); |
| 276 |
|
| 277 |
i = a % 1; |
| 278 |
assert(i == 20); |
| 279 |
|
| 280 |
i = 1 % a; |
| 281 |
assert(i == 21); |
| 282 |
|
| 283 |
i = a & 1; |
| 284 |
assert(i == 22); |
| 285 |
|
| 286 |
i = a | 1; |
| 287 |
assert(i == 23); |
| 288 |
|
| 289 |
i = a ^ 1; |
| 290 |
assert(i == 24); |
| 291 |
|
| 292 |
i = a << 1; |
| 293 |
assert(i == 25); |
| 294 |
|
| 295 |
i = 1 << a; |
| 296 |
assert(i == 26); |
| 297 |
|
| 298 |
i = a >> 1; |
| 299 |
assert(i == 27); |
| 300 |
|
| 301 |
i = 1 >> a; |
| 302 |
assert(i == 28); |
| 303 |
|
| 304 |
i = a >>> 1; |
| 305 |
assert(i == 29); |
| 306 |
|
| 307 |
i = 1 >>> a; |
| 308 |
assert(i == 30); |
| 309 |
|
| 310 |
i = a ~ 1; |
| 311 |
assert(i == 31); |
| 312 |
|
| 313 |
i = 1 ~ a; |
| 314 |
assert(i == 32); |
| 315 |
|
| 316 |
i = a == 1; |
| 317 |
assert(i == 33); |
| 318 |
i = 1 == a; |
| 319 |
assert(i == 33); |
| 320 |
i = a != 1; |
| 321 |
assert(i == 0); |
| 322 |
i = 1 != a; |
| 323 |
assert(i == 0); |
| 324 |
|
| 325 |
i = a < 1; |
| 326 |
assert(i == 0); |
| 327 |
i = a <= 1; |
| 328 |
assert(i == 0); |
| 329 |
i = a > 1; |
| 330 |
assert(i == 1); |
| 331 |
i = a >= 1; |
| 332 |
assert(i == 1); |
| 333 |
|
| 334 |
i = 1 < a; |
| 335 |
printf("i = %d\n", i); |
| 336 |
assert(i == 1); |
| 337 |
i = 1 <= a; |
| 338 |
assert(i == 1); |
| 339 |
i = 1 > a; |
| 340 |
assert(i == 0); |
| 341 |
i = 1 >= a; |
| 342 |
assert(i == 0); |
| 343 |
|
| 344 |
i = (a += 1); |
| 345 |
assert(i == 35); |
| 346 |
|
| 347 |
i = (a -= 1); |
| 348 |
assert(i == 36); |
| 349 |
|
| 350 |
i = (a *= 1); |
| 351 |
assert(i == 37); |
| 352 |
|
| 353 |
i = (a /= 1); |
| 354 |
assert(i == 38); |
| 355 |
|
| 356 |
i = (a %= 1); |
| 357 |
assert(i == 39); |
| 358 |
|
| 359 |
i = (a &= 1); |
| 360 |
assert(i == 40); |
| 361 |
|
| 362 |
i = (a |= 1); |
| 363 |
assert(i == 41); |
| 364 |
|
| 365 |
i = (a ^= 1); |
| 366 |
assert(i == 42); |
| 367 |
|
| 368 |
i = (a <<= 1); |
| 369 |
assert(i == 43); |
| 370 |
|
| 371 |
i = (a >>= 1); |
| 372 |
assert(i == 44); |
| 373 |
|
| 374 |
i = (a >>>= 1); |
| 375 |
assert(i == 45); |
| 376 |
|
| 377 |
i = (a ~= 1); |
| 378 |
assert(i == 46); |
| 379 |
} |
| 380 |
|
| 381 |
/*********************************/ |
| 382 |
|
| 383 |
struct A6 |
| 384 |
{ |
| 385 |
int opUnary(string op)() if (op == "-") { return 10; } |
| 386 |
int opUnary(string op)() if (op == "~") { return 11; } |
| 387 |
int opUnary(string op)() if (op == "++") { return 12; } |
| 388 |
int opUnary(string op)() if (op == "--") { return 13; } |
| 389 |
|
| 390 |
int opBinary(string op)(int j) if (op == "+") { return 14; } |
| 391 |
int opBinary(string op)(int j) if (op == "-") { return 15; } |
| 392 |
int opBinaryRight(string op)(int j) if (op == "-") { return 16; } |
| 393 |
int opBinary(string op)(int j) if (op == "*") { return 17; } |
| 394 |
int opBinary(string op)(int j) if (op == "/") { return 18; } |
| 395 |
int opBinaryRight(string op)(int j) if (op == "/") { return 19; } |
| 396 |
int opBinary(string op)(int j) if (op == "%") { return 20; } |
| 397 |
int opBinaryRight(string op)(int j) if (op == "%") { return 21; } |
| 398 |
int opBinary(string op)(int j) if (op == "&") { return 22; } |
| 399 |
int opBinary(string op)(int j) if (op == "|") { return 23; } |
| 400 |
int opBinary(string op)(int j) if (op == "^") { return 24; } |
| 401 |
int opBinary(string op)(int j) if (op == "<<") { return 25; } |
| 402 |
int opBinaryRight(string op)(int j) if (op == "<<") { return 26; } |
| 403 |
int opBinary(string op)(int j) if (op == ">>") { return 27; } |
| 404 |
int opBinaryRight(string op)(int j) if (op == ">>") { return 28; } |
| 405 |
int opBinary(string op)(int j) if (op == ">>>") { return 29; } |
| 406 |
int opBinaryRight(string op)(int j) if (op == ">>>") { return 30; } |
| 407 |
int opBinary(string op)(int j) if (op == "~") { return 31; } |
| 408 |
int opBinaryRight(string op)(int j) if (op == "~") { return 32; } |
| 409 |
int opEquals(int j) { return 33; } |
| 410 |
const bool opEquals(const ref A6) { return false; } |
| 411 |
int opCmp(int j) { return 34; } |
| 412 |
int opOpAssign(string op)(int j) if (op == "+") { return 35; } |
| 413 |
int opOpAssign(string op)(int j) if (op == "-") { return 36; } |
| 414 |
int opOpAssign(string op)(int j) if (op == "*") { return 37; } |
| 415 |
int opOpAssign(string op)(int j) if (op == "/") { return 38; } |
| 416 |
int opOpAssign(string op)(int j) if (op == "%") { return 39; } |
| 417 |
int opOpAssign(string op)(int j) if (op == "&") { return 40; } |
| 418 |
int opOpAssign(string op)(int j) if (op == "|") { return 41; } |
| 419 |
int opOpAssign(string op)(int j) if (op == "^") { return 42; } |
| 420 |
int opOpAssign(string op)(int j) if (op == "<<") { return 43; } |
| 421 |
int opOpAssign(string op)(int j) if (op == ">>") { return 44; } |
| 422 |
int opOpAssign(string op)(int j) if (op == ">>>") { return 45; } |
| 423 |
int opOpAssign(string op)(int j) if (op == "~") { return 46; } |
| 424 |
} |
| 425 |
|
| 426 |
void test6() |
| 427 |
{ |
| 428 |
A6 a; |
| 429 |
int i; |
| 430 |
|
| 431 |
i = -a; |
| 432 |
assert(i == 10); |
| 433 |
|
| 434 |
i = ~a; |
| 435 |
assert(i == 11); |
| 436 |
|
| 437 |
i = ++a; |
| 438 |
assert(i == 12); |
| 439 |
|
| 440 |
i = --a; |
| 441 |
assert(i == 13); |
| 442 |
|
| 443 |
i = a + 1; |
| 444 |
assert(i == 14); |
| 445 |
|
| 446 |
i = a - 1; |
| 447 |
assert(i == 15); |
| 448 |
|
| 449 |
i = 1 - a; |
| 450 |
assert(i == 16); |
| 451 |
|
| 452 |
i = a * 1; |
| 453 |
assert(i == 17); |
| 454 |
|
| 455 |
i = a / 1; |
| 456 |
assert(i == 18); |
| 457 |
|
| 458 |
i = 1 / a; |
| 459 |
assert(i == 19); |
| 460 |
|
| 461 |
i = a % 1; |
| 462 |
assert(i == 20); |
| 463 |
|
| 464 |
i = 1 % a; |
| 465 |
assert(i == 21); |
| 466 |
|
| 467 |
i = a & 1; |
| 468 |
assert(i == 22); |
| 469 |
|
| 470 |
i = a | 1; |
| 471 |
assert(i == 23); |
| 472 |
|
| 473 |
i = a ^ 1; |
| 474 |
assert(i == 24); |
| 475 |
|
| 476 |
i = a << 1; |
| 477 |
assert(i == 25); |
| 478 |
|
| 479 |
i = 1 << a; |
| 480 |
assert(i == 26); |
| 481 |
|
| 482 |
i = a >> 1; |
| 483 |
assert(i == 27); |
| 484 |
|
| 485 |
i = 1 >> a; |
| 486 |
assert(i == 28); |
| 487 |
|
| 488 |
i = a >>> 1; |
| 489 |
assert(i == 29); |
| 490 |
|
| 491 |
i = 1 >>> a; |
| 492 |
assert(i == 30); |
| 493 |
|
| 494 |
i = a ~ 1; |
| 495 |
assert(i == 31); |
| 496 |
|
| 497 |
i = 1 ~ a; |
| 498 |
assert(i == 32); |
| 499 |
|
| 500 |
i = a == 1; |
| 501 |
assert(i == 33); |
| 502 |
i = 1 == a; |
| 503 |
assert(i == 33); |
| 504 |
i = a != 1; |
| 505 |
assert(i == 0); |
| 506 |
i = 1 != a; |
| 507 |
assert(i == 0); |
| 508 |
|
| 509 |
i = a < 1; |
| 510 |
assert(i == 0); |
| 511 |
i = a <= 1; |
| 512 |
assert(i == 0); |
| 513 |
i = a > 1; |
| 514 |
assert(i == 1); |
| 515 |
i = a >= 1; |
| 516 |
assert(i == 1); |
| 517 |
|
| 518 |
i = 1 < a; |
| 519 |
assert(i == 1); |
| 520 |
i = 1 <= a; |
| 521 |
assert(i == 1); |
| 522 |
i = 1 > a; |
| 523 |
assert(i == 0); |
| 524 |
i = 1 >= a; |
| 525 |
assert(i == 0); |
| 526 |
|
| 527 |
i = (a += 1); |
| 528 |
assert(i == 35); |
| 529 |
|
| 530 |
i = (a -= 1); |
| 531 |
assert(i == 36); |
| 532 |
|
| 533 |
i = (a *= 1); |
| 534 |
assert(i == 37); |
| 535 |
|
| 536 |
i = (a /= 1); |
| 537 |
assert(i == 38); |
| 538 |
|
| 539 |
i = (a %= 1); |
| 540 |
assert(i == 39); |
| 541 |
|
| 542 |
i = (a &= 1); |
| 543 |
assert(i == 40); |
| 544 |
|
| 545 |
i = (a |= 1); |
| 546 |
assert(i == 41); |
| 547 |
|
| 548 |
i = (a ^= 1); |
| 549 |
assert(i == 42); |
| 550 |
|
| 551 |
i = (a <<= 1); |
| 552 |
assert(i == 43); |
| 553 |
|
| 554 |
i = (a >>= 1); |
| 555 |
assert(i == 44); |
| 556 |
|
| 557 |
i = (a >>>= 1); |
| 558 |
assert(i == 45); |
| 559 |
|
| 560 |
i = (a ~= 1); |
| 561 |
assert(i == 46); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
/**************************************/ |
| 566 |
|
| 567 |
struct Foo7 |
| 568 |
{ |
| 569 |
int opSlice() { return 7; } |
| 570 |
int opSlice(int i, int j) { return i * (j + 1); } |
| 571 |
} |
| 572 |
|
| 573 |
void test7() |
| 574 |
{ |
| 575 |
Foo7 f; |
| 576 |
int i; |
| 577 |
|
| 578 |
i = f[]; |
| 579 |
assert(i == 7); |
| 580 |
i = f[3..4]; |
| 581 |
assert(i == 15); |
| 582 |
} |
| 583 |
|
| 584 |
/**************************************/ |
| 585 |
|
| 586 |
interface IWriter |
| 587 |
{ |
| 588 |
int opBinary(string op)(string i) if (op == "<<"); |
| 589 |
int opBinary(string op)(int i) if (op == "<<"); |
| 590 |
} |
| 591 |
|
| 592 |
class Writer : IWriter |
| 593 |
{ |
| 594 |
int opBinary(string op)(string i) if (op == "<<") |
| 595 |
{ |
| 596 |
printf("Writer.opShl(char[])\n"); |
| 597 |
return 1; |
| 598 |
} |
| 599 |
|
| 600 |
int opBinary(string op)(int i) if (op == "<<") |
| 601 |
{ |
| 602 |
printf("Writer.opShl(int)\n"); |
| 603 |
return 2; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
class BinaryWriter : Writer |
| 608 |
{ |
| 609 |
int opBinary(string op)(string i) if (op == "<<") |
| 610 |
{ |
| 611 |
printf("Writer.opShl(char[])\n"); |
| 612 |
return 1; |
| 613 |
} |
| 614 |
|
| 615 |
int opBinary(string op)(int i) if (op == "<<") |
| 616 |
{ |
| 617 |
printf("BinaryWriter.opShl(int)\n"); |
| 618 |
return 3; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
void test8() |
| 623 |
{ |
| 624 |
BinaryWriter bw = new BinaryWriter(); |
| 625 |
int i; |
| 626 |
|
| 627 |
i = bw << "test"; |
| 628 |
assert(i == 1); |
| 629 |
i = bw << 1; |
| 630 |
assert(i == 3); |
| 631 |
} |
| 632 |
|
| 633 |
/**************************************/ |
| 634 |
|
| 635 |
struct A9 |
| 636 |
{ |
| 637 |
int opCast() { return 28; } |
| 638 |
} |
| 639 |
|
| 640 |
void test9() |
| 641 |
{ |
| 642 |
A9 a; |
| 643 |
|
| 644 |
long i = cast(long)a; |
| 645 |
assert(i == 28); |
| 646 |
} |
| 647 |
|
| 648 |
/**************************************/ |
| 649 |
|
| 650 |
class A10 |
| 651 |
{ |
| 652 |
int opBinary(string op)(int i) if (op == "+") { return i + 1; } |
| 653 |
alias opBinaryRight = opBinary; |
| 654 |
} |
| 655 |
|
| 656 |
class B10 |
| 657 |
{ |
| 658 |
int opBinaryRight(string op)(A10 a) if (op == "+") { return 3; } |
| 659 |
alias opBinary = opBinaryRight; |
| 660 |
} |
| 661 |
|
| 662 |
void test10() |
| 663 |
{ |
| 664 |
int i; |
| 665 |
A10 a = new A10(); |
| 666 |
|
| 667 |
i = a + 1; |
| 668 |
printf("a + 1 = %d\n", i); |
| 669 |
assert(i == 2); |
| 670 |
|
| 671 |
i = 1 + a; |
| 672 |
printf("1 + a = %d\n", i); |
| 673 |
assert(i == 2); |
| 674 |
|
| 675 |
B10 b = new B10(); |
| 676 |
i = a + b; |
| 677 |
printf("a + b = %d\n", i); |
| 678 |
assert(i == 3); |
| 679 |
|
| 680 |
i = b + a; |
| 681 |
printf("b + a = %d\n", i); |
| 682 |
assert(i == 3); |
| 683 |
|
| 684 |
// i = b + 3; |
| 685 |
} |
| 686 |
|
| 687 |
/**************************************/ |
| 688 |
|
| 689 |
class A11 |
| 690 |
{ |
| 691 |
int opIndex(int i) { return i; } |
| 692 |
int opIndexAssign(int value, int i) { return value * 10 + i; } |
| 693 |
} |
| 694 |
|
| 695 |
void test11() |
| 696 |
{ |
| 697 |
int i; |
| 698 |
A11 a = new A11(); |
| 699 |
|
| 700 |
i = a[5]; |
| 701 |
assert(i == 5); |
| 702 |
i = (a[4] = 6); |
| 703 |
printf("i = %d\n", i); |
| 704 |
assert(i == 64); |
| 705 |
} |
| 706 |
|
| 707 |
/**************************************/ |
| 708 |
|
| 709 |
class A12 |
| 710 |
{ |
| 711 |
int opIndex(int i1, int i2) { return i1 * 10 + i2; } |
| 712 |
int opIndexAssign(int value, int i1, int i2) { return value * 100 + i1 * 10 + i2; } |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
void test12() |
| 717 |
{ |
| 718 |
A12 a = new A12(); |
| 719 |
int i; |
| 720 |
|
| 721 |
printf("%d\n", a[1, 2]); |
| 722 |
assert(a[1, 2] == 12); |
| 723 |
|
| 724 |
i = (a[3, 4] = 5); |
| 725 |
printf("%d\n", i); |
| 726 |
assert(i == 534); |
| 727 |
} |
| 728 |
|
| 729 |
/**************************************/ |
| 730 |
|
| 731 |
class A13 |
| 732 |
{ |
| 733 |
A13 opBinary(string op)(int x) if (op == "<<") |
| 734 |
{ |
| 735 |
printf("A::opShl(int %d)\n", x); |
| 736 |
printf("%d",x); |
| 737 |
return this; |
| 738 |
} |
| 739 |
A13 opBinary(string op)(string x) if (op == "<<") |
| 740 |
{ |
| 741 |
printf("A::opShl(char[])\n"); |
| 742 |
printf("%.*s", cast(int)x.length, x.ptr); |
| 743 |
return this; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
class B13 |
| 748 |
{ |
| 749 |
A13 opBinaryRight(string op)(A13 a) if (op == "<<") |
| 750 |
{ |
| 751 |
printf("B::opShl_r(A)\n"); |
| 752 |
return a; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
void test13() |
| 757 |
{ |
| 758 |
A13 a = new A13(); |
| 759 |
a << 4 << " " << 12 << "\n"; |
| 760 |
|
| 761 |
B13 b = new B13(); |
| 762 |
a << b; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
/**************************************/ |
| 767 |
|
| 768 |
class Foo14 |
| 769 |
{ int a; |
| 770 |
|
| 771 |
int opBinary(string op)(int x) if (op == "in") |
| 772 |
{ |
| 773 |
return a + x; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
void test14() |
| 778 |
{ |
| 779 |
auto f = new Foo14; |
| 780 |
f.a = 3; |
| 781 |
auto i = f in 7; |
| 782 |
assert(i == 10); |
| 783 |
} |
| 784 |
|
| 785 |
/**************************************/ |
| 786 |
|
| 787 |
// https://issues.dlang.org/show_bug.cgi?id=3983 |
| 788 |
|
| 789 |
struct Fug |
| 790 |
{ |
| 791 |
bool opEquals(ref const Fug y) const { |
| 792 |
return false; |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
struct Fig |
| 797 |
{ |
| 798 |
Fug f; |
| 799 |
bool opEquals(Tdummy=void)(ref const Fig y) const { |
| 800 |
return false; |
| 801 |
} |
| 802 |
|
| 803 |
bool opEquals(T: int)(T y) const { |
| 804 |
return false; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
void test15() |
| 809 |
{ |
| 810 |
Fig fx, fy; |
| 811 |
if (fx==2) {} |
| 812 |
} |
| 813 |
|
| 814 |
/**************************************/ |
| 815 |
// https://issues.dlang.org/show_bug.cgi?id=4953 |
| 816 |
|
| 817 |
struct S4953a |
| 818 |
{ |
| 819 |
short _x; |
| 820 |
bool opBinaryRight(string op)(short x) if (op == "in") |
| 821 |
{ |
| 822 |
return x == _x; |
| 823 |
} |
| 824 |
} |
| 825 |
void test4953a() |
| 826 |
{ |
| 827 |
S4953a s; |
| 828 |
5 in s; |
| 829 |
} |
| 830 |
|
| 831 |
struct S4953b |
| 832 |
{ |
| 833 |
void opBinary(string op)(short x) |
| 834 |
{} |
| 835 |
} |
| 836 |
void test4953b() |
| 837 |
{ |
| 838 |
S4953b s; |
| 839 |
s + 5; |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
struct S4953c |
| 844 |
{ |
| 845 |
void funOpAssign(float[1u] data) { } |
| 846 |
void opOpAssign(string op)(float[1u] data) if(op=="<<") { } |
| 847 |
} |
| 848 |
void test4953c() |
| 849 |
{ |
| 850 |
// dmd v2.054, v2.055 |
| 851 |
S4953c s; |
| 852 |
float[1u] a = [1.0f]; // OK: Implicit cast from float[] compiles |
| 853 |
s.funOpAssign([1.0f]); // OK: Implicit cast from float[] compiles |
| 854 |
s <<= [1.0f]; // Issue: Implicit cast from float[] does not compile |
| 855 |
s <<= cast(float[1u])[1.0f]; // OK: Explicit cast from float[] compiles |
| 856 |
} |
| 857 |
|
| 858 |
void f4953d1 (dstring d) { dstring e = d; } |
| 859 |
void f4953d2()(dstring d) { dstring e = d; } |
| 860 |
void f4953d3 ( byte[] b) { byte[] c = b; } |
| 861 |
void f4953d4()( byte[] b) { byte[] c = b; } |
| 862 |
void test4953d() |
| 863 |
{ |
| 864 |
f4953d1("abc"); // OK |
| 865 |
f4953d2("abc"); // OK |
| 866 |
f4953d3([1,2,3]); // OK |
| 867 |
f4953d4([1,2,3]); |
| 868 |
// Error: template test2.f() does not match any function template declaration |
| 869 |
// Error: template test2.f() cannot deduce template function from argument types !()(int[]) |
| 870 |
} |
| 871 |
|
| 872 |
/**************************************/ |
| 873 |
// https://issues.dlang.org/show_bug.cgi?id=4993 |
| 874 |
|
| 875 |
// reduced from the bug report |
| 876 |
struct Bar4993 |
| 877 |
{ |
| 878 |
void opIndexAssign(int value, size_t index) {} |
| 879 |
} |
| 880 |
@property auto bar4993() |
| 881 |
{ |
| 882 |
return Bar4993(); |
| 883 |
} |
| 884 |
void test4993() |
| 885 |
{ |
| 886 |
bar4993[3] = 42; |
| 887 |
} |
| 888 |
|
| 889 |
/**************************************/ |
| 890 |
// https://issues.dlang.org/show_bug.cgi?id=8133 |
| 891 |
|
| 892 |
void test8133() |
| 893 |
{ |
| 894 |
struct S |
| 895 |
{ |
| 896 |
int opCall() { return 1; } |
| 897 |
} |
| 898 |
struct A |
| 899 |
{ |
| 900 |
S s; |
| 901 |
alias s this; |
| 902 |
} |
| 903 |
A f = A(); |
| 904 |
assert(f() == 1); |
| 905 |
} |
| 906 |
|
| 907 |
/**************************************/ |
| 908 |
// https://issues.dlang.org/show_bug.cgi?id=8522 |
| 909 |
|
| 910 |
struct Point8522 |
| 911 |
{ |
| 912 |
bool opEquals(R)(R rhs) { return true; } |
| 913 |
bool opEquals(R)(R rhs) const { return true; } |
| 914 |
} |
| 915 |
|
| 916 |
void test8522() |
| 917 |
{ |
| 918 |
Point8522 mp; |
| 919 |
const Point8522 cp; |
| 920 |
assert(mp == mp); |
| 921 |
assert(mp == cp); |
| 922 |
assert(cp == mp); // doesn't work |
| 923 |
assert(cp == cp); // doesn't work |
| 924 |
} |
| 925 |
|
| 926 |
/**************************************/ |
| 927 |
// https://issues.dlang.org/show_bug.cgi?id=12778 |
| 928 |
|
| 929 |
struct Vec12778X |
| 930 |
{ |
| 931 |
Vec12778X opBinary(string op)(Vec12778X b) const |
| 932 |
if (op == "+") |
| 933 |
{ |
| 934 |
mixin("return Vec12778X(this.x " ~ op ~ " b.x, this.y " ~ op ~ " b.y);"); |
| 935 |
} |
| 936 |
alias opBinaryRight = opBinary; |
| 937 |
|
| 938 |
float x = 0, y = 0; |
| 939 |
} |
| 940 |
|
| 941 |
void test12778() |
| 942 |
{ |
| 943 |
struct S |
| 944 |
{ |
| 945 |
void test1() |
| 946 |
{ |
| 947 |
Vec12778X vx = vx1 + vx2; // ok |
| 948 |
} |
| 949 |
|
| 950 |
void test2() const |
| 951 |
{ |
| 952 |
Vec12778X vx = vx1 + vx2; // ok <- error |
| 953 |
} |
| 954 |
|
| 955 |
Vec12778X vx1, vx2; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
/**************************************/ |
| 960 |
// https://issues.dlang.org/show_bug.cgi?id=14343 |
| 961 |
|
| 962 |
struct S14343a |
| 963 |
{ |
| 964 |
int i; |
| 965 |
immutable(Object) o; |
| 966 |
|
| 967 |
S14343a opUnary(string op)() { return this; } |
| 968 |
void opAssign(S14343a other) {} |
| 969 |
} |
| 970 |
|
| 971 |
struct S14343b |
| 972 |
{ |
| 973 |
int i; |
| 974 |
immutable(Object) o; |
| 975 |
|
| 976 |
void opOpAssign(string op)(int j) if (op == "+") { i += j; } |
| 977 |
S14343b opUnary(string op)() if (op == "++") { ++i; return this; } |
| 978 |
void opAssign(S14343b other) {} |
| 979 |
} |
| 980 |
|
| 981 |
void test14343() |
| 982 |
{ |
| 983 |
S14343a s, t; |
| 984 |
|
| 985 |
t = s; // OK |
| 986 |
++s; // OK |
| 987 |
s++; // OK <- Error: cannot modify struct s S with immutable members |
| 988 |
} |
| 989 |
|
| 990 |
/**************************************/ |
| 991 |
// https://issues.dlang.org/show_bug.cgi?id=14344 |
| 992 |
|
| 993 |
struct S14344 |
| 994 |
{ |
| 995 |
S14344 opBinary(string op)(S14344 v) |
| 996 |
{ |
| 997 |
static assert(0); |
| 998 |
} |
| 999 |
S14344 opAssign()(S14344 v) |
| 1000 |
{ |
| 1001 |
static assert(0); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
struct S14344Mix |
| 1006 |
{ |
| 1007 |
S14344 s; |
| 1008 |
alias s this; |
| 1009 |
} |
| 1010 |
|
| 1011 |
class C14344 |
| 1012 |
{ |
| 1013 |
S14344Mix height() { return S14344Mix(); } |
| 1014 |
|
| 1015 |
void update() |
| 1016 |
{ |
| 1017 |
S14344 height = this.height; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
/**************************************/ |
| 1022 |
// https://issues.dlang.org/show_bug.cgi?id=1547 |
| 1023 |
struct A |
| 1024 |
{ |
| 1025 |
int b; |
| 1026 |
static A opCall(int k) |
| 1027 |
{ |
| 1028 |
assert(0); |
| 1029 |
} |
| 1030 |
this(int) {} |
| 1031 |
} |
| 1032 |
|
| 1033 |
void fun(A k = 2) {} |
| 1034 |
|
| 1035 |
void test1547() |
| 1036 |
{ |
| 1037 |
fun(); |
| 1038 |
} |
| 1039 |
|
| 1040 |
/**************************************/ |
| 1041 |
// https://issues.dlang.org/show_bug.cgi?id=20475 |
| 1042 |
struct S20475 |
| 1043 |
{ |
| 1044 |
string[2] x; |
| 1045 |
} |
| 1046 |
|
| 1047 |
void test20475() |
| 1048 |
{ |
| 1049 |
auto s = S20475(["abc", "bcd"]); |
| 1050 |
auto t = S20475(["abc", ""]); |
| 1051 |
string u = "abcd"; |
| 1052 |
t.x[1] = u[1..$]; |
| 1053 |
assert(s == t); |
| 1054 |
} |
| 1055 |
|
| 1056 |
/**************************************/ |
| 1057 |
int main() |
| 1058 |
{ |
| 1059 |
test4(); |
| 1060 |
test5(); |
| 1061 |
test6(); |
| 1062 |
test7(); |
| 1063 |
test9(); |
| 1064 |
test11(); |
| 1065 |
test12(); |
| 1066 |
test15(); |
| 1067 |
test1547(); |
| 1068 |
test4953a(); |
| 1069 |
test4953b(); |
| 1070 |
test4953c(); |
| 1071 |
test4953d(); |
| 1072 |
test4993(); |
| 1073 |
test8133(); |
| 1074 |
test8522(); |
| 1075 |
test20475(); |
| 1076 |
|
| 1077 |
printf("Success\n"); |
| 1078 |
return 0; |
| 1079 |
} |