1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Collections; 6 using System.Collections.ObjectModel; 7 using System.Collections.Generic; 8 using Xunit; 9 10 namespace System.Linq.Expressions.Tests 11 { 12 public static class NewArrayListTests 13 { 14 #region Tests 15 16 [Theory, ClassData(typeof(CompilationTypes))] CheckBoolArrayListTest(bool useInterpreter)17 public static void CheckBoolArrayListTest(bool useInterpreter) 18 { 19 bool[][] array = new bool[][] 20 { 21 new bool[] { }, 22 new bool[] { true }, 23 new bool[] { true, false } 24 }; 25 Expression[][] exprs = new Expression[array.Length][]; 26 for (int i = 0; i < array.Length; i++) 27 { 28 exprs[i] = new Expression[array[i].Length]; 29 for (int j = 0; j < array[i].Length; j++) 30 { 31 bool val = array[i][j]; 32 exprs[i][j] = Expression.Constant(val, typeof(bool)); 33 } 34 } 35 36 for (int i = 0; i < array.Length; i++) 37 { 38 VerifyBoolArrayList(array[i], exprs[i], useInterpreter); 39 } 40 } 41 42 [Theory, ClassData(typeof(CompilationTypes))] CheckByteArrayListTest(bool useInterpreter)43 public static void CheckByteArrayListTest(bool useInterpreter) 44 { 45 byte[][] array = new byte[][] 46 { 47 new byte[] { }, 48 new byte[] { 0 }, 49 new byte[] { 0, 1, byte.MaxValue } 50 }; 51 Expression[][] exprs = new Expression[array.Length][]; 52 for (int i = 0; i < array.Length; i++) 53 { 54 exprs[i] = new Expression[array[i].Length]; 55 for (int j = 0; j < array[i].Length; j++) 56 { 57 byte val = array[i][j]; 58 exprs[i][j] = Expression.Constant(val, typeof(byte)); 59 } 60 } 61 62 for (int i = 0; i < array.Length; i++) 63 { 64 VerifyByteArrayList(array[i], exprs[i], useInterpreter); 65 } 66 } 67 68 [Theory, ClassData(typeof(CompilationTypes))] CheckCustomArrayListTest(bool useInterpreter)69 public static void CheckCustomArrayListTest(bool useInterpreter) 70 { 71 C[][] array = new C[][] 72 { 73 new C[] { }, 74 new C[] { null }, 75 new C[] { new C(), new D(), new D(0), new D(5) } 76 }; 77 Expression[][] exprs = new Expression[array.Length][]; 78 for (int i = 0; i < array.Length; i++) 79 { 80 exprs[i] = new Expression[array[i].Length]; 81 for (int j = 0; j < array[i].Length; j++) 82 { 83 C val = array[i][j]; 84 exprs[i][j] = Expression.Constant(val, typeof(C)); 85 } 86 } 87 88 for (int i = 0; i < array.Length; i++) 89 { 90 VerifyCustomArrayList(array[i], exprs[i], useInterpreter); 91 } 92 } 93 94 [Theory, ClassData(typeof(CompilationTypes))] CheckCharArrayListTest(bool useInterpreter)95 public static void CheckCharArrayListTest(bool useInterpreter) 96 { 97 char[][] array = new char[][] 98 { 99 new char[] { }, 100 new char[] { '\0' }, 101 new char[] { '\0', '\b', 'A', '\uffff' } 102 }; 103 Expression[][] exprs = new Expression[array.Length][]; 104 for (int i = 0; i < array.Length; i++) 105 { 106 exprs[i] = new Expression[array[i].Length]; 107 for (int j = 0; j < array[i].Length; j++) 108 { 109 char val = array[i][j]; 110 exprs[i][j] = Expression.Constant(val, typeof(char)); 111 } 112 } 113 114 for (int i = 0; i < array.Length; i++) 115 { 116 VerifyCharArrayList(array[i], exprs[i], useInterpreter); 117 } 118 } 119 120 [Theory, ClassData(typeof(CompilationTypes))] CheckCustom2ArrayListTest(bool useInterpreter)121 public static void CheckCustom2ArrayListTest(bool useInterpreter) 122 { 123 D[][] array = new D[][] 124 { 125 new D[] { }, 126 new D[] { null }, 127 new D[] { null, new D(), new D(0), new D(5) } 128 }; 129 Expression[][] exprs = new Expression[array.Length][]; 130 for (int i = 0; i < array.Length; i++) 131 { 132 exprs[i] = new Expression[array[i].Length]; 133 for (int j = 0; j < array[i].Length; j++) 134 { 135 D val = array[i][j]; 136 exprs[i][j] = Expression.Constant(val, typeof(D)); 137 } 138 } 139 140 for (int i = 0; i < array.Length; i++) 141 { 142 VerifyCustom2ArrayList(array[i], exprs[i], useInterpreter); 143 } 144 } 145 146 [Theory, ClassData(typeof(CompilationTypes))] CheckDecimalArrayListTest(bool useInterpreter)147 public static void CheckDecimalArrayListTest(bool useInterpreter) 148 { 149 decimal[][] array = new decimal[][] 150 { 151 new decimal[] { }, 152 new decimal[] { decimal.Zero }, 153 new decimal[] { decimal.Zero, decimal.One, decimal.MinusOne, decimal.MinValue, decimal.MaxValue } 154 }; 155 Expression[][] exprs = new Expression[array.Length][]; 156 for (int i = 0; i < array.Length; i++) 157 { 158 exprs[i] = new Expression[array[i].Length]; 159 for (int j = 0; j < array[i].Length; j++) 160 { 161 decimal val = array[i][j]; 162 exprs[i][j] = Expression.Constant(val, typeof(decimal)); 163 } 164 } 165 166 for (int i = 0; i < array.Length; i++) 167 { 168 VerifyDecimalArrayList(array[i], exprs[i], useInterpreter); 169 } 170 } 171 172 [Theory, ClassData(typeof(CompilationTypes))] CheckDelegateArrayListTest(bool useInterpreter)173 public static void CheckDelegateArrayListTest(bool useInterpreter) 174 { 175 Delegate[][] array = new Delegate[][] 176 { 177 new Delegate[] { }, 178 new Delegate[] { null }, 179 new Delegate[] { null, (Func<object>) delegate() { return null; }, (Func<int, int>) delegate(int i) { return i+1; }, (Action<object>) delegate { } } 180 }; 181 Expression[][] exprs = new Expression[array.Length][]; 182 for (int i = 0; i < array.Length; i++) 183 { 184 exprs[i] = new Expression[array[i].Length]; 185 for (int j = 0; j < array[i].Length; j++) 186 { 187 Delegate val = array[i][j]; 188 exprs[i][j] = Expression.Constant(val, typeof(Delegate)); 189 } 190 } 191 192 for (int i = 0; i < array.Length; i++) 193 { 194 VerifyDelegateArrayList(array[i], exprs[i], useInterpreter); 195 } 196 } 197 198 [Theory, ClassData(typeof(CompilationTypes))] CheckDoubleArrayListTest(bool useInterpreter)199 public static void CheckDoubleArrayListTest(bool useInterpreter) 200 { 201 double[][] array = new double[][] 202 { 203 new double[] { }, 204 new double[] { 0 }, 205 new double[] { 0, 1, -1, double.MinValue, double.MaxValue, double.Epsilon, double.NegativeInfinity, double.PositiveInfinity, double.NaN } 206 }; 207 Expression[][] exprs = new Expression[array.Length][]; 208 for (int i = 0; i < array.Length; i++) 209 { 210 exprs[i] = new Expression[array[i].Length]; 211 for (int j = 0; j < array[i].Length; j++) 212 { 213 double val = array[i][j]; 214 exprs[i][j] = Expression.Constant(val, typeof(double)); 215 } 216 } 217 218 for (int i = 0; i < array.Length; i++) 219 { 220 VerifyDoubleArrayList(array[i], exprs[i], useInterpreter); 221 } 222 } 223 224 [Theory, ClassData(typeof(CompilationTypes))] CheckEnumArrayListTest(bool useInterpreter)225 public static void CheckEnumArrayListTest(bool useInterpreter) 226 { 227 E[][] array = new E[][] 228 { 229 new E[] { }, 230 new E[] { (E) 0 }, 231 new E[] { (E) 0, E.A, E.B, (E) int.MaxValue, (E) int.MinValue } 232 }; 233 Expression[][] exprs = new Expression[array.Length][]; 234 for (int i = 0; i < array.Length; i++) 235 { 236 exprs[i] = new Expression[array[i].Length]; 237 for (int j = 0; j < array[i].Length; j++) 238 { 239 E val = array[i][j]; 240 exprs[i][j] = Expression.Constant(val, typeof(E)); 241 } 242 } 243 244 for (int i = 0; i < array.Length; i++) 245 { 246 VerifyEnumArrayList(array[i], exprs[i], useInterpreter); 247 } 248 } 249 250 [Theory, ClassData(typeof(CompilationTypes))] CheckEnumLongArrayListTest(bool useInterpreter)251 public static void CheckEnumLongArrayListTest(bool useInterpreter) 252 { 253 El[][] array = new El[][] 254 { 255 new El[] { }, 256 new El[] { (El) 0 }, 257 new El[] { (El) 0, El.A, El.B, (El) long.MaxValue, (El) long.MinValue } 258 }; 259 Expression[][] exprs = new Expression[array.Length][]; 260 for (int i = 0; i < array.Length; i++) 261 { 262 exprs[i] = new Expression[array[i].Length]; 263 for (int j = 0; j < array[i].Length; j++) 264 { 265 El val = array[i][j]; 266 exprs[i][j] = Expression.Constant(val, typeof(El)); 267 } 268 } 269 270 for (int i = 0; i < array.Length; i++) 271 { 272 VerifyEnumLongArrayList(array[i], exprs[i], useInterpreter); 273 } 274 } 275 276 [Theory, ClassData(typeof(CompilationTypes))] CheckFloatArrayListTest(bool useInterpreter)277 public static void CheckFloatArrayListTest(bool useInterpreter) 278 { 279 float[][] array = new float[][] 280 { 281 new float[] { }, 282 new float[] { 0 }, 283 new float[] { 0, 1, -1, float.MinValue, float.MaxValue, float.Epsilon, float.NegativeInfinity, float.PositiveInfinity, float.NaN } 284 }; 285 Expression[][] exprs = new Expression[array.Length][]; 286 for (int i = 0; i < array.Length; i++) 287 { 288 exprs[i] = new Expression[array[i].Length]; 289 for (int j = 0; j < array[i].Length; j++) 290 { 291 float val = array[i][j]; 292 exprs[i][j] = Expression.Constant(val, typeof(float)); 293 } 294 } 295 296 for (int i = 0; i < array.Length; i++) 297 { 298 VerifyFloatArrayList(array[i], exprs[i], useInterpreter); 299 } 300 } 301 302 [Theory, ClassData(typeof(CompilationTypes))] CheckFuncArrayListTest(bool useInterpreter)303 public static void CheckFuncArrayListTest(bool useInterpreter) 304 { 305 Func<object>[][] array = new Func<object>[][] 306 { 307 new Func<object>[] { }, 308 new Func<object>[] { null }, 309 new Func<object>[] { null, (Func<object>) delegate() { return null; } } 310 }; 311 Expression[][] exprs = new Expression[array.Length][]; 312 for (int i = 0; i < array.Length; i++) 313 { 314 exprs[i] = new Expression[array[i].Length]; 315 for (int j = 0; j < array[i].Length; j++) 316 { 317 Func<object> val = array[i][j]; 318 exprs[i][j] = Expression.Constant(val, typeof(Func<object>)); 319 } 320 } 321 322 for (int i = 0; i < array.Length; i++) 323 { 324 VerifyFuncArrayList(array[i], exprs[i], useInterpreter); 325 } 326 } 327 328 [Theory, ClassData(typeof(CompilationTypes))] CheckInterfaceArrayListTest(bool useInterpreter)329 public static void CheckInterfaceArrayListTest(bool useInterpreter) 330 { 331 I[][] array = new I[][] 332 { 333 new I[] { }, 334 new I[] { null }, 335 new I[] { null, new C(), new D(), new D(0), new D(5) } 336 }; 337 Expression[][] exprs = new Expression[array.Length][]; 338 for (int i = 0; i < array.Length; i++) 339 { 340 exprs[i] = new Expression[array[i].Length]; 341 for (int j = 0; j < array[i].Length; j++) 342 { 343 I val = array[i][j]; 344 exprs[i][j] = Expression.Constant(val, typeof(I)); 345 } 346 } 347 348 for (int i = 0; i < array.Length; i++) 349 { 350 VerifyInterfaceArrayList(array[i], exprs[i], useInterpreter); 351 } 352 } 353 354 [Theory, ClassData(typeof(CompilationTypes))] CheckIEquatableCustomArrayListTest(bool useInterpreter)355 public static void CheckIEquatableCustomArrayListTest(bool useInterpreter) 356 { 357 IEquatable<C>[][] array = new IEquatable<C>[][] 358 { 359 new IEquatable<C>[] { }, 360 new IEquatable<C>[] { null }, 361 new IEquatable<C>[] { null, new C(), new D(), new D(0), new D(5) } 362 }; 363 Expression[][] exprs = new Expression[array.Length][]; 364 for (int i = 0; i < array.Length; i++) 365 { 366 exprs[i] = new Expression[array[i].Length]; 367 for (int j = 0; j < array[i].Length; j++) 368 { 369 IEquatable<C> val = array[i][j]; 370 exprs[i][j] = Expression.Constant(val, typeof(IEquatable<C>)); 371 } 372 } 373 374 for (int i = 0; i < array.Length; i++) 375 { 376 VerifyIEquatableCustomArrayList(array[i], exprs[i], useInterpreter); 377 } 378 } 379 380 [Theory, ClassData(typeof(CompilationTypes))] CheckIEquatableCustom2ArrayListTest(bool useInterpreter)381 public static void CheckIEquatableCustom2ArrayListTest(bool useInterpreter) 382 { 383 IEquatable<D>[][] array = new IEquatable<D>[][] 384 { 385 new IEquatable<D>[] { }, 386 new IEquatable<D>[] { null }, 387 new IEquatable<D>[] { null, new D(), new D(0), new D(5) } 388 }; 389 Expression[][] exprs = new Expression[array.Length][]; 390 for (int i = 0; i < array.Length; i++) 391 { 392 exprs[i] = new Expression[array[i].Length]; 393 for (int j = 0; j < array[i].Length; j++) 394 { 395 IEquatable<D> val = array[i][j]; 396 exprs[i][j] = Expression.Constant(val, typeof(IEquatable<D>)); 397 } 398 } 399 400 for (int i = 0; i < array.Length; i++) 401 { 402 VerifyIEquatableCustom2ArrayList(array[i], exprs[i], useInterpreter); 403 } 404 } 405 406 [Theory, ClassData(typeof(CompilationTypes))] CheckIntArrayListTest(bool useInterpreter)407 public static void CheckIntArrayListTest(bool useInterpreter) 408 { 409 int[][] array = new int[][] 410 { 411 new int[] { }, 412 new int[] { 0 }, 413 new int[] { 0, 1, -1, int.MinValue, int.MaxValue } 414 }; 415 Expression[][] exprs = new Expression[array.Length][]; 416 for (int i = 0; i < array.Length; i++) 417 { 418 exprs[i] = new Expression[array[i].Length]; 419 for (int j = 0; j < array[i].Length; j++) 420 { 421 int val = array[i][j]; 422 exprs[i][j] = Expression.Constant(val, typeof(int)); 423 } 424 } 425 426 for (int i = 0; i < array.Length; i++) 427 { 428 VerifyIntArrayList(array[i], exprs[i], useInterpreter); 429 } 430 } 431 432 [Theory, ClassData(typeof(CompilationTypes))] CheckLongArrayListTest(bool useInterpreter)433 public static void CheckLongArrayListTest(bool useInterpreter) 434 { 435 long[][] array = new long[][] 436 { 437 new long[] { }, 438 new long[] { 0 }, 439 new long[] { 0, 1, -1, long.MinValue, long.MaxValue } 440 }; 441 Expression[][] exprs = new Expression[array.Length][]; 442 for (int i = 0; i < array.Length; i++) 443 { 444 exprs[i] = new Expression[array[i].Length]; 445 for (int j = 0; j < array[i].Length; j++) 446 { 447 long val = array[i][j]; 448 exprs[i][j] = Expression.Constant(val, typeof(long)); 449 } 450 } 451 452 for (int i = 0; i < array.Length; i++) 453 { 454 VerifyLongArrayList(array[i], exprs[i], useInterpreter); 455 } 456 } 457 458 [Theory, ClassData(typeof(CompilationTypes))] CheckObjectArrayListTest(bool useInterpreter)459 public static void CheckObjectArrayListTest(bool useInterpreter) 460 { 461 object[][] array = new object[][] 462 { 463 new object[] { }, 464 new object[] { null }, 465 new object[] { null, new object(), new C(), new D(3) } 466 }; 467 Expression[][] exprs = new Expression[array.Length][]; 468 for (int i = 0; i < array.Length; i++) 469 { 470 exprs[i] = new Expression[array[i].Length]; 471 for (int j = 0; j < array[i].Length; j++) 472 { 473 object val = array[i][j]; 474 exprs[i][j] = Expression.Constant(val, typeof(object)); 475 } 476 } 477 478 for (int i = 0; i < array.Length; i++) 479 { 480 VerifyObjectArrayList(array[i], exprs[i], useInterpreter); 481 } 482 } 483 484 [Theory, ClassData(typeof(CompilationTypes))] CheckStructArrayListTest(bool useInterpreter)485 public static void CheckStructArrayListTest(bool useInterpreter) 486 { 487 S[][] array = new S[][] 488 { 489 new S[] { }, 490 new S[] { default(S) }, 491 new S[] { default(S), new S() } 492 }; 493 Expression[][] exprs = new Expression[array.Length][]; 494 for (int i = 0; i < array.Length; i++) 495 { 496 exprs[i] = new Expression[array[i].Length]; 497 for (int j = 0; j < array[i].Length; j++) 498 { 499 S val = array[i][j]; 500 exprs[i][j] = Expression.Constant(val, typeof(S)); 501 } 502 } 503 504 for (int i = 0; i < array.Length; i++) 505 { 506 VerifyStructArrayList(array[i], exprs[i], useInterpreter); 507 } 508 } 509 510 [Theory, ClassData(typeof(CompilationTypes))] CheckSByteArrayListTest(bool useInterpreter)511 public static void CheckSByteArrayListTest(bool useInterpreter) 512 { 513 sbyte[][] array = new sbyte[][] 514 { 515 new sbyte[] { }, 516 new sbyte[] { 0 }, 517 new sbyte[] { 0, 1, -1, sbyte.MinValue, sbyte.MaxValue } 518 }; 519 Expression[][] exprs = new Expression[array.Length][]; 520 for (int i = 0; i < array.Length; i++) 521 { 522 exprs[i] = new Expression[array[i].Length]; 523 for (int j = 0; j < array[i].Length; j++) 524 { 525 sbyte val = array[i][j]; 526 exprs[i][j] = Expression.Constant(val, typeof(sbyte)); 527 } 528 } 529 530 for (int i = 0; i < array.Length; i++) 531 { 532 VerifySByteArrayList(array[i], exprs[i], useInterpreter); 533 } 534 } 535 536 [Theory, ClassData(typeof(CompilationTypes))] CheckStructWithStringArrayListTest(bool useInterpreter)537 public static void CheckStructWithStringArrayListTest(bool useInterpreter) 538 { 539 Sc[][] array = new Sc[][] 540 { 541 new Sc[] { }, 542 new Sc[] { default(Sc) }, 543 new Sc[] { default(Sc), new Sc(), new Sc(null) } 544 }; 545 Expression[][] exprs = new Expression[array.Length][]; 546 for (int i = 0; i < array.Length; i++) 547 { 548 exprs[i] = new Expression[array[i].Length]; 549 for (int j = 0; j < array[i].Length; j++) 550 { 551 Sc val = array[i][j]; 552 exprs[i][j] = Expression.Constant(val, typeof(Sc)); 553 } 554 } 555 556 for (int i = 0; i < array.Length; i++) 557 { 558 VerifyStructWithStringArrayList(array[i], exprs[i], useInterpreter); 559 } 560 } 561 562 [Theory, ClassData(typeof(CompilationTypes))] CheckStructWithStringAndFieldArrayListTest(bool useInterpreter)563 public static void CheckStructWithStringAndFieldArrayListTest(bool useInterpreter) 564 { 565 Scs[][] array = new Scs[][] 566 { 567 new Scs[] { }, 568 new Scs[] { default(Scs) }, 569 new Scs[] { default(Scs), new Scs(), new Scs(null,new S()) } 570 }; 571 Expression[][] exprs = new Expression[array.Length][]; 572 for (int i = 0; i < array.Length; i++) 573 { 574 exprs[i] = new Expression[array[i].Length]; 575 for (int j = 0; j < array[i].Length; j++) 576 { 577 Scs val = array[i][j]; 578 exprs[i][j] = Expression.Constant(val, typeof(Scs)); 579 } 580 } 581 582 for (int i = 0; i < array.Length; i++) 583 { 584 VerifyStructWithStringAndFieldArrayList(array[i], exprs[i], useInterpreter); 585 } 586 } 587 588 [Theory, ClassData(typeof(CompilationTypes))] CheckShortArrayListTest(bool useInterpreter)589 public static void CheckShortArrayListTest(bool useInterpreter) 590 { 591 short[][] array = new short[][] 592 { 593 new short[] { }, 594 new short[] { 0 }, 595 new short[] { 0, 1, -1, short.MinValue, short.MaxValue } 596 }; 597 Expression[][] exprs = new Expression[array.Length][]; 598 for (int i = 0; i < array.Length; i++) 599 { 600 exprs[i] = new Expression[array[i].Length]; 601 for (int j = 0; j < array[i].Length; j++) 602 { 603 short val = array[i][j]; 604 exprs[i][j] = Expression.Constant(val, typeof(short)); 605 } 606 } 607 608 for (int i = 0; i < array.Length; i++) 609 { 610 VerifyShortArrayList(array[i], exprs[i], useInterpreter); 611 } 612 } 613 614 [Theory, ClassData(typeof(CompilationTypes))] CheckStructWithTwoValuesArrayListTest(bool useInterpreter)615 public static void CheckStructWithTwoValuesArrayListTest(bool useInterpreter) 616 { 617 Sp[][] array = new Sp[][] 618 { 619 new Sp[] { }, 620 new Sp[] { default(Sp) }, 621 new Sp[] { default(Sp), new Sp(), new Sp(5,5.0) } 622 }; 623 Expression[][] exprs = new Expression[array.Length][]; 624 for (int i = 0; i < array.Length; i++) 625 { 626 exprs[i] = new Expression[array[i].Length]; 627 for (int j = 0; j < array[i].Length; j++) 628 { 629 Sp val = array[i][j]; 630 exprs[i][j] = Expression.Constant(val, typeof(Sp)); 631 } 632 } 633 634 for (int i = 0; i < array.Length; i++) 635 { 636 VerifyStructWithTwoValuesArrayList(array[i], exprs[i], useInterpreter); 637 } 638 } 639 640 [Theory, ClassData(typeof(CompilationTypes))] CheckStructWithValueArrayListTest(bool useInterpreter)641 public static void CheckStructWithValueArrayListTest(bool useInterpreter) 642 { 643 Ss[][] array = new Ss[][] 644 { 645 new Ss[] { }, 646 new Ss[] { default(Ss) }, 647 new Ss[] { default(Ss), new Ss(), new Ss(new S()) } 648 }; 649 Expression[][] exprs = new Expression[array.Length][]; 650 for (int i = 0; i < array.Length; i++) 651 { 652 exprs[i] = new Expression[array[i].Length]; 653 for (int j = 0; j < array[i].Length; j++) 654 { 655 Ss val = array[i][j]; 656 exprs[i][j] = Expression.Constant(val, typeof(Ss)); 657 } 658 } 659 660 for (int i = 0; i < array.Length; i++) 661 { 662 VerifyStructWithValueArrayList(array[i], exprs[i], useInterpreter); 663 } 664 } 665 666 [Theory, ClassData(typeof(CompilationTypes))] CheckStringArrayListTest(bool useInterpreter)667 public static void CheckStringArrayListTest(bool useInterpreter) 668 { 669 string[][] array = new string[][] 670 { 671 new string[] { }, 672 new string[] { null }, 673 new string[] { null, "", "a", "foo" } 674 }; 675 Expression[][] exprs = new Expression[array.Length][]; 676 for (int i = 0; i < array.Length; i++) 677 { 678 exprs[i] = new Expression[array[i].Length]; 679 for (int j = 0; j < array[i].Length; j++) 680 { 681 string val = array[i][j]; 682 exprs[i][j] = Expression.Constant(val, typeof(string)); 683 } 684 } 685 686 for (int i = 0; i < array.Length; i++) 687 { 688 VerifyStringArrayList(array[i], exprs[i], useInterpreter); 689 } 690 } 691 692 [Theory, ClassData(typeof(CompilationTypes))] CheckUIntArrayListTest(bool useInterpreter)693 public static void CheckUIntArrayListTest(bool useInterpreter) 694 { 695 uint[][] array = new uint[][] 696 { 697 new uint[] { }, 698 new uint[] { 0 }, 699 new uint[] { 0, 1, uint.MaxValue } 700 }; 701 Expression[][] exprs = new Expression[array.Length][]; 702 for (int i = 0; i < array.Length; i++) 703 { 704 exprs[i] = new Expression[array[i].Length]; 705 for (int j = 0; j < array[i].Length; j++) 706 { 707 uint val = array[i][j]; 708 exprs[i][j] = Expression.Constant(val, typeof(uint)); 709 } 710 } 711 712 for (int i = 0; i < array.Length; i++) 713 { 714 VerifyUIntArrayList(array[i], exprs[i], useInterpreter); 715 } 716 } 717 718 [Theory, ClassData(typeof(CompilationTypes))] CheckULongArrayListTest(bool useInterpreter)719 public static void CheckULongArrayListTest(bool useInterpreter) 720 { 721 ulong[][] array = new ulong[][] 722 { 723 new ulong[] { }, 724 new ulong[] { 0 }, 725 new ulong[] { 0, 1, ulong.MaxValue } 726 }; 727 Expression[][] exprs = new Expression[array.Length][]; 728 for (int i = 0; i < array.Length; i++) 729 { 730 exprs[i] = new Expression[array[i].Length]; 731 for (int j = 0; j < array[i].Length; j++) 732 { 733 ulong val = array[i][j]; 734 exprs[i][j] = Expression.Constant(val, typeof(ulong)); 735 } 736 } 737 738 for (int i = 0; i < array.Length; i++) 739 { 740 VerifyULongArrayList(array[i], exprs[i], useInterpreter); 741 } 742 } 743 744 [Theory, ClassData(typeof(CompilationTypes))] CheckUShortArrayListTest(bool useInterpreter)745 public static void CheckUShortArrayListTest(bool useInterpreter) 746 { 747 ushort[][] array = new ushort[][] 748 { 749 new ushort[] { }, 750 new ushort[] { 0 }, 751 new ushort[] { 0, 1, ushort.MaxValue } 752 }; 753 Expression[][] exprs = new Expression[array.Length][]; 754 for (int i = 0; i < array.Length; i++) 755 { 756 exprs[i] = new Expression[array[i].Length]; 757 for (int j = 0; j < array[i].Length; j++) 758 { 759 ushort val = array[i][j]; 760 exprs[i][j] = Expression.Constant(val, typeof(ushort)); 761 } 762 } 763 764 for (int i = 0; i < array.Length; i++) 765 { 766 VerifyUShortArrayList(array[i], exprs[i], useInterpreter); 767 } 768 } 769 770 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericCustomArrayListTest(bool useInterpreter)771 public static void CheckGenericCustomArrayListTest(bool useInterpreter) 772 { 773 CheckGenericArrayListHelper<C>(useInterpreter); 774 } 775 776 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericEnumArrayListTest(bool useInterpreter)777 public static void CheckGenericEnumArrayListTest(bool useInterpreter) 778 { 779 CheckGenericArrayListHelper<E>(useInterpreter); 780 } 781 782 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericObjectArrayListTest(bool useInterpreter)783 public static void CheckGenericObjectArrayListTest(bool useInterpreter) 784 { 785 CheckGenericArrayListHelper<object>(useInterpreter); 786 } 787 788 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericStructArrayListTest(bool useInterpreter)789 public static void CheckGenericStructArrayListTest(bool useInterpreter) 790 { 791 CheckGenericArrayListHelper<S>(useInterpreter); 792 } 793 794 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericStructWithStringAndFieldArrayListTest(bool useInterpreter)795 public static void CheckGenericStructWithStringAndFieldArrayListTest(bool useInterpreter) 796 { 797 CheckGenericArrayListHelper<Scs>(useInterpreter); 798 } 799 800 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericCustomWithClassRestrictionArrayListTest(bool useInterpreter)801 public static void CheckGenericCustomWithClassRestrictionArrayListTest(bool useInterpreter) 802 { 803 CheckGenericWithClassRestrictionArrayListHelper<C>(useInterpreter); 804 } 805 806 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericObjectWithClassRestrictionArrayListTest(bool useInterpreter)807 public static void CheckGenericObjectWithClassRestrictionArrayListTest(bool useInterpreter) 808 { 809 CheckGenericWithClassRestrictionArrayListHelper<object>(useInterpreter); 810 } 811 812 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericCustomWithSubClassRestrictionArrayListTest(bool useInterpreter)813 public static void CheckGenericCustomWithSubClassRestrictionArrayListTest(bool useInterpreter) 814 { 815 CheckGenericWithSubClassRestrictionArrayListHelper<C>(useInterpreter); 816 } 817 818 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericCustomWithClassAndNewRestrictionArrayListTest(bool useInterpreter)819 public static void CheckGenericCustomWithClassAndNewRestrictionArrayListTest(bool useInterpreter) 820 { 821 CheckGenericWithClassAndNewRestrictionArrayListHelper<C>(useInterpreter); 822 } 823 824 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericObjectWithClassAndNewRestrictionArrayListTest(bool useInterpreter)825 public static void CheckGenericObjectWithClassAndNewRestrictionArrayListTest(bool useInterpreter) 826 { 827 CheckGenericWithClassAndNewRestrictionArrayListHelper<object>(useInterpreter); 828 } 829 830 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericCustomWithSubClassAndNewRestrictionArrayListTest(bool useInterpreter)831 public static void CheckGenericCustomWithSubClassAndNewRestrictionArrayListTest(bool useInterpreter) 832 { 833 CheckGenericWithSubClassAndNewRestrictionArrayListHelper<C>(useInterpreter); 834 } 835 836 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericEnumWithStructRestrictionArrayListTest(bool useInterpreter)837 public static void CheckGenericEnumWithStructRestrictionArrayListTest(bool useInterpreter) 838 { 839 CheckGenericWithStructRestrictionArrayListHelper<E>(useInterpreter); 840 } 841 842 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericStructWithStructRestrictionArrayListTest(bool useInterpreter)843 public static void CheckGenericStructWithStructRestrictionArrayListTest(bool useInterpreter) 844 { 845 CheckGenericWithStructRestrictionArrayListHelper<S>(useInterpreter); 846 } 847 848 [Theory, ClassData(typeof(CompilationTypes))] CheckGenericStructWithStringAndFieldWithStructRestrictionArrayListTest(bool useInterpreter)849 public static void CheckGenericStructWithStringAndFieldWithStructRestrictionArrayListTest(bool useInterpreter) 850 { 851 CheckGenericWithStructRestrictionArrayListHelper<Scs>(useInterpreter); 852 } 853 854 [Fact] ThrowOnNegativeSizedCollection()855 public static void ThrowOnNegativeSizedCollection() 856 { 857 // This is an obscure case, and it doesn't much matter what is thrown, as long as is thrown before such 858 // an edge case could cause more obscure damage. A class derived from ReadOnlyCollection is used to catch 859 // assumptions that such a type is safe. 860 Assert.ThrowsAny<Exception>(() => Expression.NewArrayInit(typeof(int), new BogusReadOnlyCollection<Expression>())); 861 } 862 863 [Fact] ToStringTest()864 public static void ToStringTest() 865 { 866 NewArrayExpression e1 = Expression.NewArrayInit(typeof(int)); 867 Assert.Equal("new [] {}", e1.ToString()); 868 869 NewArrayExpression e2 = Expression.NewArrayInit(typeof(int), Expression.Parameter(typeof(int), "x")); 870 Assert.Equal("new [] {x}", e2.ToString()); 871 872 NewArrayExpression e3 = Expression.NewArrayInit(typeof(int), Expression.Parameter(typeof(int), "x"), Expression.Parameter(typeof(int), "y")); 873 Assert.Equal("new [] {x, y}", e3.ToString()); 874 } 875 876 #endregion 877 878 #region Helper methods 879 880 private class BogusCollection<T> : IList<T> 881 { 882 public T this[int index] 883 { 884 get { return default(T); } 885 886 set { throw new NotSupportedException(); } 887 } 888 889 public int Count 890 { 891 get { return -1; } 892 } 893 894 public bool IsReadOnly 895 { 896 get { return true; } 897 } 898 Add(T item)899 public void Add(T item) 900 { 901 throw new NotSupportedException(); 902 } 903 Clear()904 public void Clear() 905 { 906 throw new NotSupportedException(); 907 } 908 Contains(T item)909 public bool Contains(T item) 910 { 911 return false; 912 } 913 CopyTo(T[] array, int arrayIndex)914 public void CopyTo(T[] array, int arrayIndex) 915 { 916 } 917 GetEnumerator()918 public IEnumerator<T> GetEnumerator() 919 { 920 return Enumerable.Empty<T>().GetEnumerator(); 921 } 922 IndexOf(T item)923 public int IndexOf(T item) 924 { 925 return -1; 926 } 927 Insert(int index, T item)928 public void Insert(int index, T item) 929 { 930 throw new NotSupportedException(); 931 } 932 Remove(T item)933 public bool Remove(T item) 934 { 935 throw new NotSupportedException(); 936 } 937 RemoveAt(int index)938 public void RemoveAt(int index) 939 { 940 throw new NotSupportedException(); 941 } 942 IEnumerable.GetEnumerator()943 IEnumerator IEnumerable.GetEnumerator() 944 { 945 return GetEnumerator(); 946 } 947 } 948 949 private class BogusReadOnlyCollection<T> : ReadOnlyCollection<T> 950 { BogusReadOnlyCollection()951 public BogusReadOnlyCollection() 952 :base(new BogusCollection<T>()) 953 { 954 955 } 956 } 957 CheckGenericArrayListHelper(bool useInterpreter)958 private static void CheckGenericArrayListHelper<T>(bool useInterpreter) 959 { 960 T[][] array = new T[][] 961 { 962 new T[] { }, 963 new T[] { default(T) }, 964 new T[] { default(T) } 965 }; 966 Expression[][] exprs = new Expression[array.Length][]; 967 for (int i = 0; i < array.Length; i++) 968 { 969 exprs[i] = new Expression[array[i].Length]; 970 for (int j = 0; j < array[i].Length; j++) 971 { 972 T val = array[i][j]; 973 exprs[i][j] = Expression.Constant(val, typeof(T)); 974 } 975 } 976 977 for (int i = 0; i < array.Length; i++) 978 { 979 VerifyGenericArrayList<T>(array[i], exprs[i], useInterpreter); 980 } 981 } 982 983 private static void CheckGenericWithClassRestrictionArrayListHelper<Tc>(bool useInterpreter) where Tc : class 984 { 985 Tc[][] array = new Tc[][] 986 { 987 new Tc[] { }, 988 new Tc[] { default(Tc) }, 989 new Tc[] { default(Tc) } 990 }; 991 Expression[][] exprs = new Expression[array.Length][]; 992 for (int i = 0; i < array.Length; i++) 993 { 994 exprs[i] = new Expression[array[i].Length]; 995 for (int j = 0; j < array[i].Length; j++) 996 { 997 Tc val = array[i][j]; 998 exprs[i][j] = Expression.Constant(val, typeof(Tc)); 999 } 1000 } 1001 1002 for (int i = 0; i < array.Length; i++) 1003 { 1004 VerifyGenericWithClassRestrictionArrayList<Tc>(array[i], exprs[i], useInterpreter); 1005 } 1006 } 1007 1008 private static void CheckGenericWithSubClassRestrictionArrayListHelper<TC>(bool useInterpreter) where TC : C 1009 { 1010 TC[][] array = new TC[][] 1011 { 1012 new TC[] { }, 1013 new TC[] { default(TC) }, 1014 new TC[] { default(TC) } 1015 }; 1016 Expression[][] exprs = new Expression[array.Length][]; 1017 for (int i = 0; i < array.Length; i++) 1018 { 1019 exprs[i] = new Expression[array[i].Length]; 1020 for (int j = 0; j < array[i].Length; j++) 1021 { 1022 TC val = array[i][j]; 1023 exprs[i][j] = Expression.Constant(val, typeof(TC)); 1024 } 1025 } 1026 1027 for (int i = 0; i < array.Length; i++) 1028 { 1029 VerifyGenericWithSubClassRestrictionArrayList<TC>(array[i], exprs[i], useInterpreter); 1030 } 1031 } 1032 1033 private static void CheckGenericWithClassAndNewRestrictionArrayListHelper<Tcn>(bool useInterpreter) where Tcn : class, new() 1034 { 1035 Tcn[][] array = new Tcn[][] 1036 { 1037 new Tcn[] { }, 1038 new Tcn[] { default(Tcn) }, 1039 new Tcn[] { default(Tcn) } 1040 }; 1041 Expression[][] exprs = new Expression[array.Length][]; 1042 for (int i = 0; i < array.Length; i++) 1043 { 1044 exprs[i] = new Expression[array[i].Length]; 1045 for (int j = 0; j < array[i].Length; j++) 1046 { 1047 Tcn val = array[i][j]; 1048 exprs[i][j] = Expression.Constant(val, typeof(Tcn)); 1049 } 1050 } 1051 1052 for (int i = 0; i < array.Length; i++) 1053 { 1054 VerifyGenericWithClassAndNewRestrictionArrayList<Tcn>(array[i], exprs[i], useInterpreter); 1055 } 1056 } 1057 1058 private static void CheckGenericWithSubClassAndNewRestrictionArrayListHelper<TCn>(bool useInterpreter) where TCn : C, new() 1059 { 1060 TCn[][] array = new TCn[][] 1061 { 1062 new TCn[] { }, 1063 new TCn[] { default(TCn) }, 1064 new TCn[] { default(TCn) } 1065 }; 1066 Expression[][] exprs = new Expression[array.Length][]; 1067 for (int i = 0; i < array.Length; i++) 1068 { 1069 exprs[i] = new Expression[array[i].Length]; 1070 for (int j = 0; j < array[i].Length; j++) 1071 { 1072 TCn val = array[i][j]; 1073 exprs[i][j] = Expression.Constant(val, typeof(TCn)); 1074 } 1075 } 1076 1077 for (int i = 0; i < array.Length; i++) 1078 { 1079 VerifyGenericWithSubClassAndNewRestrictionArrayList<TCn>(array[i], exprs[i], useInterpreter); 1080 } 1081 } 1082 1083 private static void CheckGenericWithStructRestrictionArrayListHelper<Ts>(bool useInterpreter) where Ts : struct 1084 { 1085 Ts[][] array = new Ts[][] 1086 { 1087 new Ts[] { }, 1088 new Ts[] { default(Ts) }, 1089 new Ts[] { default(Ts) } 1090 }; 1091 Expression[][] exprs = new Expression[array.Length][]; 1092 for (int i = 0; i < array.Length; i++) 1093 { 1094 exprs[i] = new Expression[array[i].Length]; 1095 for (int j = 0; j < array[i].Length; j++) 1096 { 1097 Ts val = array[i][j]; 1098 exprs[i][j] = Expression.Constant(val, typeof(Ts)); 1099 } 1100 } 1101 1102 for (int i = 0; i < array.Length; i++) 1103 { 1104 VerifyGenericWithStructRestrictionArrayList<Ts>(array[i], exprs[i], useInterpreter); 1105 } 1106 } 1107 1108 #endregion 1109 1110 #region verifiers 1111 VerifyBoolArrayList(bool[] val, Expression[] exprs, bool useInterpreter)1112 private static void VerifyBoolArrayList(bool[] val, Expression[] exprs, bool useInterpreter) 1113 { 1114 Expression<Func<bool[]>> e = 1115 Expression.Lambda<Func<bool[]>>( 1116 Expression.NewArrayInit(typeof(bool), exprs), 1117 Enumerable.Empty<ParameterExpression>()); 1118 Func<bool[]> f = e.Compile(useInterpreter); 1119 bool[] result = f(); 1120 Assert.Equal(val.Length, result.Length); 1121 for (int i = 0; i < result.Length; i++) 1122 { 1123 Assert.Equal(val[i], result[i]); 1124 } 1125 } 1126 VerifyByteArrayList(byte[] val, Expression[] exprs, bool useInterpreter)1127 private static void VerifyByteArrayList(byte[] val, Expression[] exprs, bool useInterpreter) 1128 { 1129 Expression<Func<byte[]>> e = 1130 Expression.Lambda<Func<byte[]>>( 1131 Expression.NewArrayInit(typeof(byte), exprs), 1132 Enumerable.Empty<ParameterExpression>()); 1133 Func<byte[]> f = e.Compile(useInterpreter); 1134 byte[] result = f(); 1135 Assert.Equal(val.Length, result.Length); 1136 for (int i = 0; i < result.Length; i++) 1137 { 1138 Assert.Equal(val[i], result[i]); 1139 } 1140 } 1141 VerifyCustomArrayList(C[] val, Expression[] exprs, bool useInterpreter)1142 private static void VerifyCustomArrayList(C[] val, Expression[] exprs, bool useInterpreter) 1143 { 1144 Expression<Func<C[]>> e = 1145 Expression.Lambda<Func<C[]>>( 1146 Expression.NewArrayInit(typeof(C), exprs), 1147 Enumerable.Empty<ParameterExpression>()); 1148 Func<C[]> f = e.Compile(useInterpreter); 1149 C[] result = f(); 1150 Assert.Equal(val.Length, result.Length); 1151 for (int i = 0; i < result.Length; i++) 1152 { 1153 Assert.Equal(val[i], result[i]); 1154 } 1155 } 1156 VerifyCharArrayList(char[] val, Expression[] exprs, bool useInterpreter)1157 private static void VerifyCharArrayList(char[] val, Expression[] exprs, bool useInterpreter) 1158 { 1159 Expression<Func<char[]>> e = 1160 Expression.Lambda<Func<char[]>>( 1161 Expression.NewArrayInit(typeof(char), exprs), 1162 Enumerable.Empty<ParameterExpression>()); 1163 Func<char[]> f = e.Compile(useInterpreter); 1164 char[] result = f(); 1165 Assert.Equal(val.Length, result.Length); 1166 for (int i = 0; i < result.Length; i++) 1167 { 1168 Assert.Equal(val[i], result[i]); 1169 } 1170 } 1171 VerifyCustom2ArrayList(D[] val, Expression[] exprs, bool useInterpreter)1172 private static void VerifyCustom2ArrayList(D[] val, Expression[] exprs, bool useInterpreter) 1173 { 1174 Expression<Func<D[]>> e = 1175 Expression.Lambda<Func<D[]>>( 1176 Expression.NewArrayInit(typeof(D), exprs), 1177 Enumerable.Empty<ParameterExpression>()); 1178 Func<D[]> f = e.Compile(useInterpreter); 1179 D[] result = f(); 1180 Assert.Equal(val.Length, result.Length); 1181 for (int i = 0; i < result.Length; i++) 1182 { 1183 Assert.Equal(val[i], result[i]); 1184 } 1185 } 1186 VerifyDecimalArrayList(decimal[] val, Expression[] exprs, bool useInterpreter)1187 private static void VerifyDecimalArrayList(decimal[] val, Expression[] exprs, bool useInterpreter) 1188 { 1189 Expression<Func<decimal[]>> e = 1190 Expression.Lambda<Func<decimal[]>>( 1191 Expression.NewArrayInit(typeof(decimal), exprs), 1192 Enumerable.Empty<ParameterExpression>()); 1193 Func<decimal[]> f = e.Compile(useInterpreter); 1194 decimal[] result = f(); 1195 Assert.Equal(val.Length, result.Length); 1196 for (int i = 0; i < result.Length; i++) 1197 { 1198 Assert.Equal(val[i], result[i]); 1199 } 1200 } 1201 VerifyDelegateArrayList(Delegate[] val, Expression[] exprs, bool useInterpreter)1202 private static void VerifyDelegateArrayList(Delegate[] val, Expression[] exprs, bool useInterpreter) 1203 { 1204 Expression<Func<Delegate[]>> e = 1205 Expression.Lambda<Func<Delegate[]>>( 1206 Expression.NewArrayInit(typeof(Delegate), exprs), 1207 Enumerable.Empty<ParameterExpression>()); 1208 Func<Delegate[]> f = e.Compile(useInterpreter); 1209 Delegate[] result = f(); 1210 Assert.Equal(val.Length, result.Length); 1211 for (int i = 0; i < result.Length; i++) 1212 { 1213 Assert.Equal(val[i], result[i]); 1214 } 1215 } 1216 VerifyDoubleArrayList(double[] val, Expression[] exprs, bool useInterpreter)1217 private static void VerifyDoubleArrayList(double[] val, Expression[] exprs, bool useInterpreter) 1218 { 1219 Expression<Func<double[]>> e = 1220 Expression.Lambda<Func<double[]>>( 1221 Expression.NewArrayInit(typeof(double), exprs), 1222 Enumerable.Empty<ParameterExpression>()); 1223 Func<double[]> f = e.Compile(useInterpreter); 1224 double[] result = f(); 1225 Assert.Equal(val.Length, result.Length); 1226 for (int i = 0; i < result.Length; i++) 1227 { 1228 Assert.Equal(val[i], result[i]); 1229 } 1230 } 1231 VerifyEnumArrayList(E[] val, Expression[] exprs, bool useInterpreter)1232 private static void VerifyEnumArrayList(E[] val, Expression[] exprs, bool useInterpreter) 1233 { 1234 Expression<Func<E[]>> e = 1235 Expression.Lambda<Func<E[]>>( 1236 Expression.NewArrayInit(typeof(E), exprs), 1237 Enumerable.Empty<ParameterExpression>()); 1238 Func<E[]> f = e.Compile(useInterpreter); 1239 E[] result = f(); 1240 Assert.Equal(val.Length, result.Length); 1241 for (int i = 0; i < result.Length; i++) 1242 { 1243 Assert.Equal(val[i], result[i]); 1244 } 1245 } 1246 VerifyEnumLongArrayList(El[] val, Expression[] exprs, bool useInterpreter)1247 private static void VerifyEnumLongArrayList(El[] val, Expression[] exprs, bool useInterpreter) 1248 { 1249 Expression<Func<El[]>> e = 1250 Expression.Lambda<Func<El[]>>( 1251 Expression.NewArrayInit(typeof(El), exprs), 1252 Enumerable.Empty<ParameterExpression>()); 1253 Func<El[]> f = e.Compile(useInterpreter); 1254 El[] result = f(); 1255 Assert.Equal(val.Length, result.Length); 1256 for (int i = 0; i < result.Length; i++) 1257 { 1258 Assert.Equal(val[i], result[i]); 1259 } 1260 } 1261 VerifyFloatArrayList(float[] val, Expression[] exprs, bool useInterpreter)1262 private static void VerifyFloatArrayList(float[] val, Expression[] exprs, bool useInterpreter) 1263 { 1264 Expression<Func<float[]>> e = 1265 Expression.Lambda<Func<float[]>>( 1266 Expression.NewArrayInit(typeof(float), exprs), 1267 Enumerable.Empty<ParameterExpression>()); 1268 Func<float[]> f = e.Compile(useInterpreter); 1269 float[] result = f(); 1270 Assert.Equal(val.Length, result.Length); 1271 for (int i = 0; i < result.Length; i++) 1272 { 1273 Assert.Equal(val[i], result[i]); 1274 } 1275 } 1276 VerifyFuncArrayList(Func<object>[] val, Expression[] exprs, bool useInterpreter)1277 private static void VerifyFuncArrayList(Func<object>[] val, Expression[] exprs, bool useInterpreter) 1278 { 1279 Expression<Func<Func<object>[]>> e = 1280 Expression.Lambda<Func<Func<object>[]>>( 1281 Expression.NewArrayInit(typeof(Func<object>), exprs), 1282 Enumerable.Empty<ParameterExpression>()); 1283 Func<Func<object>[]> f = e.Compile(useInterpreter); 1284 Func<object>[] result = f(); 1285 Assert.Equal(val.Length, result.Length); 1286 for (int i = 0; i < result.Length; i++) 1287 { 1288 Assert.Equal(val[i], result[i]); 1289 } 1290 } 1291 VerifyInterfaceArrayList(I[] val, Expression[] exprs, bool useInterpreter)1292 private static void VerifyInterfaceArrayList(I[] val, Expression[] exprs, bool useInterpreter) 1293 { 1294 Expression<Func<I[]>> e = 1295 Expression.Lambda<Func<I[]>>( 1296 Expression.NewArrayInit(typeof(I), exprs), 1297 Enumerable.Empty<ParameterExpression>()); 1298 Func<I[]> f = e.Compile(useInterpreter); 1299 I[] result = f(); 1300 Assert.Equal(val.Length, result.Length); 1301 for (int i = 0; i < result.Length; i++) 1302 { 1303 Assert.Equal(val[i], result[i]); 1304 } 1305 } 1306 VerifyIEquatableCustomArrayList(IEquatable<C>[] val, Expression[] exprs, bool useInterpreter)1307 private static void VerifyIEquatableCustomArrayList(IEquatable<C>[] val, Expression[] exprs, bool useInterpreter) 1308 { 1309 Expression<Func<IEquatable<C>[]>> e = 1310 Expression.Lambda<Func<IEquatable<C>[]>>( 1311 Expression.NewArrayInit(typeof(IEquatable<C>), exprs), 1312 Enumerable.Empty<ParameterExpression>()); 1313 Func<IEquatable<C>[]> f = e.Compile(useInterpreter); 1314 IEquatable<C>[] result = f(); 1315 Assert.Equal(val.Length, result.Length); 1316 for (int i = 0; i < result.Length; i++) 1317 { 1318 Assert.Equal(val[i], result[i]); 1319 } 1320 } 1321 VerifyIEquatableCustom2ArrayList(IEquatable<D>[] val, Expression[] exprs, bool useInterpreter)1322 private static void VerifyIEquatableCustom2ArrayList(IEquatable<D>[] val, Expression[] exprs, bool useInterpreter) 1323 { 1324 Expression<Func<IEquatable<D>[]>> e = 1325 Expression.Lambda<Func<IEquatable<D>[]>>( 1326 Expression.NewArrayInit(typeof(IEquatable<D>), exprs), 1327 Enumerable.Empty<ParameterExpression>()); 1328 Func<IEquatable<D>[]> f = e.Compile(useInterpreter); 1329 IEquatable<D>[] result = f(); 1330 Assert.Equal(val.Length, result.Length); 1331 for (int i = 0; i < result.Length; i++) 1332 { 1333 Assert.Equal(val[i], result[i]); 1334 } 1335 } 1336 VerifyIntArrayList(int[] val, Expression[] exprs, bool useInterpreter)1337 private static void VerifyIntArrayList(int[] val, Expression[] exprs, bool useInterpreter) 1338 { 1339 Expression<Func<int[]>> e = 1340 Expression.Lambda<Func<int[]>>( 1341 Expression.NewArrayInit(typeof(int), exprs), 1342 Enumerable.Empty<ParameterExpression>()); 1343 Func<int[]> f = e.Compile(useInterpreter); 1344 int[] result = f(); 1345 Assert.Equal(val.Length, result.Length); 1346 for (int i = 0; i < result.Length; i++) 1347 { 1348 Assert.Equal(val[i], result[i]); 1349 } 1350 } 1351 VerifyLongArrayList(long[] val, Expression[] exprs, bool useInterpreter)1352 private static void VerifyLongArrayList(long[] val, Expression[] exprs, bool useInterpreter) 1353 { 1354 Expression<Func<long[]>> e = 1355 Expression.Lambda<Func<long[]>>( 1356 Expression.NewArrayInit(typeof(long), exprs), 1357 Enumerable.Empty<ParameterExpression>()); 1358 Func<long[]> f = e.Compile(useInterpreter); 1359 long[] result = f(); 1360 Assert.Equal(val.Length, result.Length); 1361 for (int i = 0; i < result.Length; i++) 1362 { 1363 Assert.Equal(val[i], result[i]); 1364 } 1365 } 1366 VerifyObjectArrayList(object[] val, Expression[] exprs, bool useInterpreter)1367 private static void VerifyObjectArrayList(object[] val, Expression[] exprs, bool useInterpreter) 1368 { 1369 Expression<Func<object[]>> e = 1370 Expression.Lambda<Func<object[]>>( 1371 Expression.NewArrayInit(typeof(object), exprs), 1372 Enumerable.Empty<ParameterExpression>()); 1373 Func<object[]> f = e.Compile(useInterpreter); 1374 object[] result = f(); 1375 Assert.Equal(val.Length, result.Length); 1376 for (int i = 0; i < result.Length; i++) 1377 { 1378 Assert.Equal(val[i], result[i]); 1379 } 1380 } 1381 VerifyStructArrayList(S[] val, Expression[] exprs, bool useInterpreter)1382 private static void VerifyStructArrayList(S[] val, Expression[] exprs, bool useInterpreter) 1383 { 1384 Expression<Func<S[]>> e = 1385 Expression.Lambda<Func<S[]>>( 1386 Expression.NewArrayInit(typeof(S), exprs), 1387 Enumerable.Empty<ParameterExpression>()); 1388 Func<S[]> f = e.Compile(useInterpreter); 1389 S[] result = f(); 1390 Assert.Equal(val.Length, result.Length); 1391 for (int i = 0; i < result.Length; i++) 1392 { 1393 Assert.Equal(val[i], result[i]); 1394 } 1395 } 1396 VerifySByteArrayList(sbyte[] val, Expression[] exprs, bool useInterpreter)1397 private static void VerifySByteArrayList(sbyte[] val, Expression[] exprs, bool useInterpreter) 1398 { 1399 Expression<Func<sbyte[]>> e = 1400 Expression.Lambda<Func<sbyte[]>>( 1401 Expression.NewArrayInit(typeof(sbyte), exprs), 1402 Enumerable.Empty<ParameterExpression>()); 1403 Func<sbyte[]> f = e.Compile(useInterpreter); 1404 sbyte[] result = f(); 1405 Assert.Equal(val.Length, result.Length); 1406 for (int i = 0; i < result.Length; i++) 1407 { 1408 Assert.Equal(val[i], result[i]); 1409 } 1410 } 1411 VerifyStructWithStringArrayList(Sc[] val, Expression[] exprs, bool useInterpreter)1412 private static void VerifyStructWithStringArrayList(Sc[] val, Expression[] exprs, bool useInterpreter) 1413 { 1414 Expression<Func<Sc[]>> e = 1415 Expression.Lambda<Func<Sc[]>>( 1416 Expression.NewArrayInit(typeof(Sc), exprs), 1417 Enumerable.Empty<ParameterExpression>()); 1418 Func<Sc[]> f = e.Compile(useInterpreter); 1419 Sc[] result = f(); 1420 Assert.Equal(val.Length, result.Length); 1421 for (int i = 0; i < result.Length; i++) 1422 { 1423 Assert.Equal(val[i], result[i]); 1424 } 1425 } 1426 VerifyStructWithStringAndFieldArrayList(Scs[] val, Expression[] exprs, bool useInterpreter)1427 private static void VerifyStructWithStringAndFieldArrayList(Scs[] val, Expression[] exprs, bool useInterpreter) 1428 { 1429 Expression<Func<Scs[]>> e = 1430 Expression.Lambda<Func<Scs[]>>( 1431 Expression.NewArrayInit(typeof(Scs), exprs), 1432 Enumerable.Empty<ParameterExpression>()); 1433 Func<Scs[]> f = e.Compile(useInterpreter); 1434 Scs[] result = f(); 1435 Assert.Equal(val.Length, result.Length); 1436 for (int i = 0; i < result.Length; i++) 1437 { 1438 Assert.Equal(val[i], result[i]); 1439 } 1440 } 1441 VerifyShortArrayList(short[] val, Expression[] exprs, bool useInterpreter)1442 private static void VerifyShortArrayList(short[] val, Expression[] exprs, bool useInterpreter) 1443 { 1444 Expression<Func<short[]>> e = 1445 Expression.Lambda<Func<short[]>>( 1446 Expression.NewArrayInit(typeof(short), exprs), 1447 Enumerable.Empty<ParameterExpression>()); 1448 Func<short[]> f = e.Compile(useInterpreter); 1449 short[] result = f(); 1450 Assert.Equal(val.Length, result.Length); 1451 for (int i = 0; i < result.Length; i++) 1452 { 1453 Assert.Equal(val[i], result[i]); 1454 } 1455 } 1456 VerifyStructWithTwoValuesArrayList(Sp[] val, Expression[] exprs, bool useInterpreter)1457 private static void VerifyStructWithTwoValuesArrayList(Sp[] val, Expression[] exprs, bool useInterpreter) 1458 { 1459 Expression<Func<Sp[]>> e = 1460 Expression.Lambda<Func<Sp[]>>( 1461 Expression.NewArrayInit(typeof(Sp), exprs), 1462 Enumerable.Empty<ParameterExpression>()); 1463 Func<Sp[]> f = e.Compile(useInterpreter); 1464 Sp[] result = f(); 1465 Assert.Equal(val.Length, result.Length); 1466 for (int i = 0; i < result.Length; i++) 1467 { 1468 Assert.Equal(val[i], result[i]); 1469 } 1470 } 1471 VerifyStructWithValueArrayList(Ss[] val, Expression[] exprs, bool useInterpreter)1472 private static void VerifyStructWithValueArrayList(Ss[] val, Expression[] exprs, bool useInterpreter) 1473 { 1474 Expression<Func<Ss[]>> e = 1475 Expression.Lambda<Func<Ss[]>>( 1476 Expression.NewArrayInit(typeof(Ss), exprs), 1477 Enumerable.Empty<ParameterExpression>()); 1478 Func<Ss[]> f = e.Compile(useInterpreter); 1479 Ss[] result = f(); 1480 Assert.Equal(val.Length, result.Length); 1481 for (int i = 0; i < result.Length; i++) 1482 { 1483 Assert.Equal(val[i], result[i]); 1484 } 1485 } 1486 VerifyStringArrayList(string[] val, Expression[] exprs, bool useInterpreter)1487 private static void VerifyStringArrayList(string[] val, Expression[] exprs, bool useInterpreter) 1488 { 1489 Expression<Func<string[]>> e = 1490 Expression.Lambda<Func<string[]>>( 1491 Expression.NewArrayInit(typeof(string), exprs), 1492 Enumerable.Empty<ParameterExpression>()); 1493 Func<string[]> f = e.Compile(useInterpreter); 1494 string[] result = f(); 1495 Assert.Equal(val.Length, result.Length); 1496 for (int i = 0; i < result.Length; i++) 1497 { 1498 Assert.Equal(val[i], result[i]); 1499 } 1500 } 1501 VerifyUIntArrayList(uint[] val, Expression[] exprs, bool useInterpreter)1502 private static void VerifyUIntArrayList(uint[] val, Expression[] exprs, bool useInterpreter) 1503 { 1504 Expression<Func<uint[]>> e = 1505 Expression.Lambda<Func<uint[]>>( 1506 Expression.NewArrayInit(typeof(uint), exprs), 1507 Enumerable.Empty<ParameterExpression>()); 1508 Func<uint[]> f = e.Compile(useInterpreter); 1509 uint[] result = f(); 1510 Assert.Equal(val.Length, result.Length); 1511 for (int i = 0; i < result.Length; i++) 1512 { 1513 Assert.Equal(val[i], result[i]); 1514 } 1515 } 1516 VerifyULongArrayList(ulong[] val, Expression[] exprs, bool useInterpreter)1517 private static void VerifyULongArrayList(ulong[] val, Expression[] exprs, bool useInterpreter) 1518 { 1519 Expression<Func<ulong[]>> e = 1520 Expression.Lambda<Func<ulong[]>>( 1521 Expression.NewArrayInit(typeof(ulong), exprs), 1522 Enumerable.Empty<ParameterExpression>()); 1523 Func<ulong[]> f = e.Compile(useInterpreter); 1524 ulong[] result = f(); 1525 Assert.Equal(val.Length, result.Length); 1526 for (int i = 0; i < result.Length; i++) 1527 { 1528 Assert.Equal(val[i], result[i]); 1529 } 1530 } 1531 VerifyUShortArrayList(ushort[] val, Expression[] exprs, bool useInterpreter)1532 private static void VerifyUShortArrayList(ushort[] val, Expression[] exprs, bool useInterpreter) 1533 { 1534 Expression<Func<ushort[]>> e = 1535 Expression.Lambda<Func<ushort[]>>( 1536 Expression.NewArrayInit(typeof(ushort), exprs), 1537 Enumerable.Empty<ParameterExpression>()); 1538 Func<ushort[]> f = e.Compile(useInterpreter); 1539 ushort[] result = f(); 1540 Assert.Equal(val.Length, result.Length); 1541 for (int i = 0; i < result.Length; i++) 1542 { 1543 Assert.Equal(val[i], result[i]); 1544 } 1545 } 1546 VerifyGenericArrayList(T[] val, Expression[] exprs, bool useInterpreter)1547 private static void VerifyGenericArrayList<T>(T[] val, Expression[] exprs, bool useInterpreter) 1548 { 1549 Expression<Func<T[]>> e = 1550 Expression.Lambda<Func<T[]>>( 1551 Expression.NewArrayInit(typeof(T), exprs), 1552 Enumerable.Empty<ParameterExpression>()); 1553 Func<T[]> f = e.Compile(useInterpreter); 1554 T[] result = f(); 1555 Assert.Equal(val.Length, result.Length); 1556 for (int i = 0; i < result.Length; i++) 1557 { 1558 Assert.Equal(val[i], result[i]); 1559 } 1560 } 1561 1562 private static void VerifyGenericWithClassRestrictionArrayList<Tc>(Tc[] val, Expression[] exprs, bool useInterpreter) where Tc : class 1563 { 1564 Expression<Func<Tc[]>> e = 1565 Expression.Lambda<Func<Tc[]>>( 1566 Expression.NewArrayInit(typeof(Tc), exprs), 1567 Enumerable.Empty<ParameterExpression>()); 1568 Func<Tc[]> f = e.Compile(useInterpreter); 1569 Tc[] result = f(); Assert.Equal(val.Length, result.Length)1570 Assert.Equal(val.Length, result.Length); 1571 for (int i = 0; i < result.Length; i++) 1572 { 1573 Assert.Equal(val[i], result[i]); 1574 } 1575 } 1576 1577 private static void VerifyGenericWithSubClassRestrictionArrayList<TC>(TC[] val, Expression[] exprs, bool useInterpreter) where TC : C 1578 { 1579 Expression<Func<TC[]>> e = 1580 Expression.Lambda<Func<TC[]>>( 1581 Expression.NewArrayInit(typeof(TC), exprs), 1582 Enumerable.Empty<ParameterExpression>()); 1583 Func<TC[]> f = e.Compile(useInterpreter); 1584 TC[] result = f(); 1585 Assert.Equal(val.Length, result.Length); 1586 for (int i = 0; i < result.Length; i++) 1587 { 1588 Assert.Equal(val[i], result[i]); 1589 } 1590 } 1591 1592 private static void VerifyGenericWithClassAndNewRestrictionArrayList<Tcn>(Tcn[] val, Expression[] exprs, bool useInterpreter) where Tcn : class, new() 1593 { 1594 Expression<Func<Tcn[]>> e = 1595 Expression.Lambda<Func<Tcn[]>>( 1596 Expression.NewArrayInit(typeof(Tcn), exprs), 1597 Enumerable.Empty<ParameterExpression>()); 1598 Func<Tcn[]> f = e.Compile(useInterpreter); 1599 Tcn[] result = f(); Assert.Equal(val.Length, result.Length)1600 Assert.Equal(val.Length, result.Length); 1601 for (int i = 0; i < result.Length; i++) 1602 { 1603 Assert.Equal(val[i], result[i]); 1604 } 1605 } 1606 1607 private static void VerifyGenericWithSubClassAndNewRestrictionArrayList<TCn>(TCn[] val, Expression[] exprs, bool useInterpreter) where TCn : C, new() 1608 { 1609 Expression<Func<TCn[]>> e = 1610 Expression.Lambda<Func<TCn[]>>( 1611 Expression.NewArrayInit(typeof(TCn), exprs), 1612 Enumerable.Empty<ParameterExpression>()); 1613 Func<TCn[]> f = e.Compile(useInterpreter); 1614 TCn[] result = f(); 1615 Assert.Equal(val.Length, result.Length); 1616 for (int i = 0; i < result.Length; i++) 1617 { 1618 Assert.Equal(val[i], result[i]); 1619 } 1620 } 1621 1622 private static void VerifyGenericWithStructRestrictionArrayList<Ts>(Ts[] val, Expression[] exprs, bool useInterpreter) where Ts : struct 1623 { 1624 Expression<Func<Ts[]>> e = 1625 Expression.Lambda<Func<Ts[]>>( 1626 Expression.NewArrayInit(typeof(Ts), exprs), 1627 Enumerable.Empty<ParameterExpression>()); 1628 Func<Ts[]> f = e.Compile(useInterpreter); 1629 Ts[] result = f(); Assert.EqualSystem.Linq.Expressions.Tests.NewArrayListTests.__anon41630 Assert.Equal(val.Length, result.Length); 1631 for (int i = 0; i < result.Length; i++) 1632 { 1633 Assert.Equal(val[i], result[i]); 1634 } 1635 } 1636 1637 #endregion 1638 1639 [Fact] NullType()1640 public static void NullType() 1641 { 1642 AssertExtensions.Throws<ArgumentNullException>("type", () => Expression.NewArrayInit(null)); 1643 } 1644 1645 [Fact] VoidType()1646 public static void VoidType() 1647 { 1648 AssertExtensions.Throws<ArgumentException>("type", () => Expression.NewArrayInit(typeof(void))); 1649 } 1650 1651 [Fact] NullInitializers()1652 public static void NullInitializers() 1653 { 1654 AssertExtensions.Throws<ArgumentNullException>("initializers", () => Expression.NewArrayInit(typeof(int), default(Expression[]))); 1655 AssertExtensions.Throws<ArgumentNullException>("initializers", () => Expression.NewArrayInit(typeof(int), default(IEnumerable<Expression>))); 1656 } 1657 1658 [Fact] NullInitializer()1659 public static void NullInitializer() 1660 { 1661 AssertExtensions.Throws<ArgumentNullException>("initializers[0]", () => Expression.NewArrayInit(typeof(int), new Expression[] { null, null })); 1662 AssertExtensions.Throws<ArgumentNullException>("initializers[0]", () => Expression.NewArrayInit(typeof(int), new List<Expression> { null, null })); 1663 } 1664 1665 [Fact] ByRefType()1666 public static void ByRefType() 1667 { 1668 AssertExtensions.Throws<ArgumentException>("type", () => Expression.NewArrayInit(typeof(int).MakeByRefType())); 1669 } 1670 1671 [Fact] PointerType()1672 public static void PointerType() 1673 { 1674 AssertExtensions.Throws<ArgumentException>("type", () => Expression.NewArrayInit(typeof(int).MakePointerType())); 1675 } 1676 1677 [Fact] GenericType()1678 public static void GenericType() 1679 { 1680 AssertExtensions.Throws<ArgumentException>("type", () => Expression.NewArrayInit(typeof(List<>))); 1681 } 1682 1683 [Fact] TypeContainsGenericParameters()1684 public static void TypeContainsGenericParameters() 1685 { 1686 AssertExtensions.Throws<ArgumentException>("type", () => Expression.NewArrayInit(typeof(List<>.Enumerator))); 1687 AssertExtensions.Throws<ArgumentException>("type", () => Expression.NewArrayInit(typeof(List<>).MakeGenericType(typeof(List<>)))); 1688 } 1689 1690 [Fact] NotAssignable()1691 public static void NotAssignable() 1692 { 1693 Assert.Throws<InvalidOperationException>(() => Expression.NewArrayInit(typeof(string), Expression.Constant(2))); 1694 } 1695 1696 [Theory, ClassData(typeof(CompilationTypes))] AutoQuote(bool useInterpreter)1697 public static void AutoQuote(bool useInterpreter) 1698 { 1699 Expression<Func<int, int>> doubleIt = x => x * 2; 1700 Expression<Func<Expression<Func<int, int>>[]>> quoted = Expression.Lambda<Func<Expression<Func<int, int>>[]>>( 1701 Expression.NewArrayInit( 1702 typeof(Expression<Func<int, int>>), 1703 doubleIt 1704 ) 1705 ); 1706 Func<Expression<Func<int, int>>[]> del = quoted.Compile(useInterpreter); 1707 Assert.Equal(new [] {doubleIt}, del()); 1708 1709 quoted = Expression.Lambda<Func<Expression<Func<int, int>>[]>>( 1710 Expression.NewArrayInit( 1711 typeof(Expression<Func<int, int>>), 1712 Expression.Constant(doubleIt), 1713 doubleIt, 1714 Expression.Constant(doubleIt) 1715 ) 1716 ); 1717 del = quoted.Compile(useInterpreter); 1718 Assert.Equal(new [] {doubleIt, doubleIt, doubleIt}, del()); 1719 } 1720 1721 [Theory, ClassData(typeof(CompilationTypes))] NestedCompile(bool useInterpreter)1722 public static void NestedCompile(bool useInterpreter) 1723 { 1724 Expression<Func<int, int>> doubleIt = x => x * 2; 1725 Expression<Func<Func<int, int>[]>> unquoted = Expression.Lambda<Func<Func<int, int>[]>>( 1726 Expression.NewArrayInit( 1727 typeof(Func<int, int>), 1728 doubleIt 1729 ) 1730 ); 1731 Func<Func<int, int>[]> del = unquoted.Compile(useInterpreter); 1732 Func<int, int>[] arr = del(); 1733 Assert.Equal(1, arr.Length); 1734 Assert.Equal(26, arr[0](13)); 1735 } 1736 1737 [Fact] UpdateSameReturnsSame()1738 public static void UpdateSameReturnsSame() 1739 { 1740 Expression element0 = Expression.Constant(2); 1741 Expression element1 = Expression.Constant(3); 1742 NewArrayExpression newArrayExpression = Expression.NewArrayInit(typeof(int), element0, element1); 1743 Assert.Same(newArrayExpression, newArrayExpression.Update(new[] { element0, element1 })); 1744 } 1745 1746 [Fact] UpdateDifferentReturnsDifferent()1747 public static void UpdateDifferentReturnsDifferent() 1748 { 1749 Expression element0 = Expression.Constant(2); 1750 Expression element1 = Expression.Constant(3); 1751 NewArrayExpression newArrayExpression = Expression.NewArrayInit(typeof(int), element0, element1); 1752 Assert.NotSame(newArrayExpression, newArrayExpression.Update(new[] { element0 })); 1753 Assert.NotSame(newArrayExpression, newArrayExpression.Update(newArrayExpression.Expressions.Reverse())); 1754 } 1755 1756 [Fact] UpdateDoesntRepeatEnumeration()1757 public static void UpdateDoesntRepeatEnumeration() 1758 { 1759 Expression element0 = Expression.Constant(2); 1760 Expression element1 = Expression.Constant(3); 1761 NewArrayExpression newArrayExpression = Expression.NewArrayInit(typeof(int), element0, element1); 1762 Assert.NotSame(newArrayExpression, newArrayExpression.Update(new RunOnceEnumerable<Expression>(new[] { element0 }))); 1763 } 1764 1765 [Fact] UpdateNullThrows()1766 public static void UpdateNullThrows() 1767 { 1768 Expression element0 = Expression.Constant(2); 1769 Expression element1 = Expression.Constant(3); 1770 NewArrayExpression newArrayExpression = Expression.NewArrayInit(typeof(int), element0, element1); 1771 AssertExtensions.Throws<ArgumentNullException>("expressions", () => newArrayExpression.Update(null)); 1772 } 1773 } 1774 } 1775