PageRenderTime 68ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenImplicitlyTypeArraysTests.cs

https://github.com/EkardNT/Roslyn
C# | 1042 lines | 889 code | 82 blank | 71 comment | 0 complexity | cad50d6e10fdab93d06216a7d8d7c3d4 MD5 | raw file
  1. // Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System.IO;
  3. using Microsoft.CodeAnalysis.CSharp.Symbols;
  4. using Microsoft.CodeAnalysis.CSharp.Syntax;
  5. using ProprietaryTestResources = Microsoft.CodeAnalysis.Test.Resources.Proprietary;
  6. using Microsoft.CodeAnalysis.Test.Utilities;
  7. using Microsoft.CodeAnalysis.Text;
  8. using Roslyn.Test.Utilities;
  9. using Xunit;
  10. namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
  11. {
  12. public class CodeGenImplicitlyTypeArraysTests : CompilingTestBase
  13. {
  14. #region "Functionality tests"
  15. [Fact]
  16. public void Test_001_Simple()
  17. {
  18. var source = @"
  19. using System.Linq;
  20. namespace Test
  21. {
  22. public class Program
  23. {
  24. public static void Main()
  25. {
  26. var a = new [] {1, 2, 3};
  27. System.Console.Write(a.SequenceEqual(new int[]{1, 2, 3}));
  28. }
  29. }
  30. }
  31. ";
  32. CompileAndVerify(
  33. source,
  34. emitOptions: EmitOptions.CCI,
  35. additionalRefs: new[] { LinqAssemblyRef },
  36. expectedOutput: "True");
  37. }
  38. [Fact]
  39. public void Test_002_IntTypeBest()
  40. {
  41. // Best type: int
  42. var source = @"
  43. using System.Linq;
  44. namespace Test
  45. {
  46. public class Program
  47. {
  48. public static void Main()
  49. {
  50. var a = new [] {1, (byte)2, (short)3};
  51. System.Console.Write(a.SequenceEqual(new int[]{1, (byte)2, (short)3}));
  52. }
  53. }
  54. }
  55. ";
  56. CompileAndVerify(
  57. source,
  58. emitOptions: EmitOptions.CCI,
  59. additionalRefs: new[] { LinqAssemblyRef },
  60. expectedOutput: "True");
  61. }
  62. [Fact]
  63. public void Test_003_DoubleTypeBest()
  64. {
  65. // Best type: double
  66. var source = @"
  67. using System.Linq;
  68. namespace Test
  69. {
  70. public class Program
  71. {
  72. public static void Main()
  73. {
  74. var a = new [] {(sbyte)1, (byte)2, (short)3, (ushort)4, 5, 6u, 7l, 8ul, (char)9, 10.0f, 11.0d};
  75. System.Console.Write(a.SequenceEqual(new double[]{(sbyte)1, (byte)2, (short)3, (ushort)4, 5, 6u, 7l, 8ul, (char)9, 10.0f, 11.0d}));
  76. }
  77. }
  78. }
  79. ";
  80. CompileAndVerify(
  81. source,
  82. emitOptions: EmitOptions.CCI,
  83. additionalRefs: new[] { LinqAssemblyRef },
  84. expectedOutput: "True");
  85. }
  86. [Fact, WorkItem(895655, "DevDiv")]
  87. public void Test_004_Enum()
  88. {
  89. // Enums conversions
  90. var source = @"
  91. using System.Linq;
  92. namespace Test
  93. {
  94. public class Program
  95. {
  96. enum E
  97. {
  98. START
  99. };
  100. public static void Main()
  101. {
  102. var a = new [] {E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu};
  103. System.Console.Write(a.SequenceEqual(new E[]{E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu}));
  104. }
  105. }
  106. }
  107. ";
  108. var comp = CreateCompilationWithMscorlib(source, references: new[] { LinqAssemblyRef });
  109. comp.VerifyDiagnostics(
  110. // (15,54): warning CS0078: The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity
  111. // var a = new [] {E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu};
  112. Diagnostic(ErrorCode.WRN_LowercaseEllSuffix, "l").WithLocation(15, 54),
  113. // (15,88): warning CS0078: The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity
  114. // var a = new [] {E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu};
  115. Diagnostic(ErrorCode.WRN_LowercaseEllSuffix, "l").WithLocation(15, 88),
  116. // (15,93): warning CS0078: The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity
  117. // var a = new [] {E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu};
  118. Diagnostic(ErrorCode.WRN_LowercaseEllSuffix, "l").WithLocation(15, 93),
  119. // (17,84): warning CS0078: The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity
  120. // System.Console.Write(a.SequenceEqual(new E[]{E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu}));
  121. Diagnostic(ErrorCode.WRN_LowercaseEllSuffix, "l").WithLocation(17, 84),
  122. // (17,118): warning CS0078: The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity
  123. // System.Console.Write(a.SequenceEqual(new E[]{E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu}));
  124. Diagnostic(ErrorCode.WRN_LowercaseEllSuffix, "l").WithLocation(17, 118),
  125. // (17,123): warning CS0078: The 'l' suffix is easily confused with the digit '1' -- use 'L' for clarity
  126. // System.Console.Write(a.SequenceEqual(new E[]{E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu}));
  127. Diagnostic(ErrorCode.WRN_LowercaseEllSuffix, "l").WithLocation(17, 123),
  128. // (15,21): error CS0826: No best type found for implicitly-typed array
  129. // var a = new [] {E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu};
  130. Diagnostic(ErrorCode.ERR_ImplicitlyTypedArrayNoBestType, "new [] {E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu}").WithLocation(15, 21),
  131. // (17,35): error CS1929: '?[]' does not contain a definition for 'SequenceEqual' and the best extension method overload 'System.Linq.Queryable.SequenceEqual<Test.Program.E>(System.Linq.IQueryable<Test.Program.E>, System.Collections.Generic.IEnumerable<Test.Program.E>)' requires a receiver of type 'System.Linq.IQueryable<Test.Program.E>'
  132. // System.Console.Write(a.SequenceEqual(new E[]{E.START, 0, 0U, 0u, 0L, 0l, 0UL, 0Ul, 0uL, 0ul, 0LU, 0Lu, 0lU, 0lu}));
  133. Diagnostic(ErrorCode.ERR_BadInstanceArgType, "a").WithArguments("?[]", "SequenceEqual", "System.Linq.Queryable.SequenceEqual<Test.Program.E>(System.Linq.IQueryable<Test.Program.E>, System.Collections.Generic.IEnumerable<Test.Program.E>)", "System.Linq.IQueryable<Test.Program.E>").WithLocation(17, 35)
  134. );
  135. }
  136. [Fact]
  137. public void Test_005_ObjectTypeBest()
  138. {
  139. // Implicit reference conversions -- From any reference-type to object.
  140. var source = @"
  141. using System.Linq;
  142. namespace Test
  143. {
  144. public class C { };
  145. public interface I { };
  146. public class C2 : I { };
  147. public class Program
  148. {
  149. delegate void D();
  150. public static void M() { }
  151. public static void Main()
  152. {
  153. object o = new object();
  154. C c = new C();
  155. I i = new C2();
  156. D d = new D(M);
  157. int[] aa = new int[] {1};
  158. var a = new [] {o, """", c, i, d, aa};
  159. System.Console.Write(a.SequenceEqual(new object[]{o, """", c, i, d, aa}));
  160. }
  161. }
  162. }
  163. ";
  164. CompileAndVerify(
  165. source,
  166. emitOptions: EmitOptions.CCI,
  167. additionalRefs: new[] { LinqAssemblyRef },
  168. expectedOutput: "True");
  169. }
  170. [Fact]
  171. public void Test_006_ArrayTypeBest()
  172. {
  173. // Implicit reference conversions -- From an array-type S with an element type SE to an array-type T with an element type TE,
  174. var source = @"
  175. using System.Linq;
  176. namespace Test
  177. {
  178. public class Program
  179. {
  180. public static void Main()
  181. {
  182. object[] oa = new object[] {null};
  183. string[] sa = new string[] {null};
  184. var a = new [] {oa, sa};
  185. System.Console.Write(a.SequenceEqual(new object[][]{oa, sa}));
  186. }
  187. }
  188. }
  189. ";
  190. CompileAndVerify(
  191. source,
  192. emitOptions: EmitOptions.CCI,
  193. additionalRefs: new[] { LinqAssemblyRef },
  194. expectedOutput: "True");
  195. }
  196. [Fact]
  197. public void Test_007A()
  198. {
  199. // Implicit reference conversions -- From a one-dimensional array-type S[] to System.Collections.Generic.IList<S>.
  200. var testSrc = @"
  201. using System.Linq;
  202. using System.Collections.Generic;
  203. namespace Test
  204. {
  205. public class Program
  206. {
  207. public static void Main()
  208. {
  209. int[] ia = new int[] {1, 2, 3};
  210. IList<int> la = new List<int> {1, 2, 3};
  211. var a = new [] {ia, la};
  212. System.Console.Write(a.SequenceEqual(new IList<int>[]{ia, la}));
  213. }
  214. }
  215. }
  216. ";
  217. var compilation = CompileAndVerify(
  218. testSrc,
  219. emitOptions: EmitOptions.CCI,
  220. additionalRefs: new[] { LinqAssemblyRef },
  221. expectedOutput: "True");
  222. }
  223. [Fact]
  224. public void Test_007B()
  225. {
  226. // Implicit reference conversions -- From a one-dimensional array-type S[] to System.Collections.Generic.IReadOnlyList<S>.
  227. var testSrc = @"
  228. using System.Collections.Generic;
  229. namespace Test
  230. {
  231. public class Program
  232. {
  233. public static void Main()
  234. {
  235. int[] array = new int[] {1, 2, 3};
  236. object obj = array;
  237. IEnumerable<int> ro1 = array;
  238. IReadOnlyList<int> ro2 = (IReadOnlyList<int>)obj;
  239. IReadOnlyList<int> ro3 = (IReadOnlyList<int>)array;
  240. IReadOnlyList<int> ro4 = obj as IReadOnlyList<int>;
  241. System.Console.WriteLine(ro4 != null ? 1 : 2);
  242. }
  243. }
  244. }
  245. ";
  246. // The version of mscorlib checked in to the test resources in v4_0_30316 does not have
  247. // the IReadOnlyList<T> and IReadOnlyCollection<T> interfaces. Use the one in v4_0_30316_17626.
  248. var mscorlib17626 = new MetadataImageReference(ProprietaryTestResources.NetFX.v4_0_30316_17626.mscorlib.AsImmutableOrNull(), display: "mscorlib");
  249. CompileAndVerify(testSrc, new MetadataReference[] { mscorlib17626 }, expectedOutput: "1");
  250. }
  251. [Fact]
  252. public void Test_008_DelegateType()
  253. {
  254. // Implicit reference conversions -- From any delegate-type to System.Delegate.
  255. var source = @"
  256. using System;
  257. using System.Linq;
  258. namespace Test
  259. {
  260. public class Program
  261. {
  262. delegate void D1();
  263. public static void M1() {}
  264. delegate int D2();
  265. public static int M2() { return 0;}
  266. delegate void D3(int i);
  267. public static void M3(int i) {}
  268. delegate void D4(params object[] o);
  269. public static void M4(params object[] o) { }
  270. public static void Main()
  271. {
  272. D1 d1 = new D1(M1);
  273. D2 d2 = new D2(M2);
  274. D3 d3 = new D3(M3);
  275. D4 d4 = new D4(M4);
  276. Delegate d = d1;
  277. var a = new [] {d, d1, d2, d3, d4};
  278. System.Console.Write(a.SequenceEqual(new Delegate[]{d, d1, d2, d3, d4}));
  279. }
  280. }
  281. }
  282. ";
  283. CompileAndVerify(
  284. source,
  285. emitOptions: EmitOptions.CCI,
  286. additionalRefs: new[] { LinqAssemblyRef },
  287. expectedOutput: "True");
  288. }
  289. [Fact]
  290. public void Test_009_Null()
  291. {
  292. // Implicit reference conversions -- From the null type to any reference-type.
  293. var source = @"
  294. using System.Linq;
  295. namespace Test
  296. {
  297. public class Program
  298. {
  299. public static void Main()
  300. {
  301. var a = new [] {""aa"", ""bb"", null};
  302. System.Console.Write(a.SequenceEqual(new string[]{""aa"", ""bb"", null}));
  303. }
  304. }
  305. }
  306. ";
  307. CompileAndVerify(
  308. source,
  309. emitOptions: EmitOptions.CCI,
  310. additionalRefs: new[] { LinqAssemblyRef },
  311. expectedOutput: "True");
  312. }
  313. [Fact]
  314. public void Test_010_TypeParameter()
  315. {
  316. // Implicit reference conversions --
  317. // For a type-parameter T that is known to be a reference type , the following
  318. // implicit reference conversions exist:
  319. // From T to its effective base class C, from T to any base class of C,
  320. // and from T to any interface implemented by C.
  321. var source = @"
  322. using System.Linq;
  323. namespace Test
  324. {
  325. public interface I {}
  326. public class B {}
  327. public class C : B, I {}
  328. public class D : C {}
  329. public class Program
  330. {
  331. public static void M<T>() where T : C, new()
  332. {
  333. T t = new T();
  334. I i = t;
  335. var a = new [] {i, t};
  336. System.Console.Write(a.SequenceEqual(new I[]{i, t}));
  337. }
  338. public static void Main()
  339. {
  340. M<D>();
  341. }
  342. }
  343. }
  344. ";
  345. CompileAndVerify(
  346. source,
  347. emitOptions: EmitOptions.CCI,
  348. additionalRefs: new[] { LinqAssemblyRef },
  349. expectedOutput: "True");
  350. }
  351. [Fact]
  352. public void Test_011_BoxingConversion()
  353. {
  354. // Implicit reference conversions -- Boxing conversions
  355. var testSrc = @"
  356. using System;
  357. using System.Linq;
  358. namespace Test
  359. {
  360. public struct S
  361. {
  362. }
  363. public class Program
  364. {
  365. enum E { START };
  366. public static void Main()
  367. {
  368. IComparable v = 1;
  369. int? i = 1;
  370. var a = new [] {v, i, E.START, true, (byte)2, (sbyte)3, (short)4, (ushort)5, 6, 7U, 8L, 9UL, 10.0F, 11.0D, 12M, (char)13};
  371. System.Console.Write(a.SequenceEqual(new IComparable[]{v, i, E.START, true, (byte)2, (sbyte)3, (short)4, (ushort)5, 6, 7U, 8L, 9UL, 10.0F, 11.0D, 12M, (char)13}));
  372. }
  373. }
  374. }
  375. ";
  376. var compilation = CompileAndVerify(
  377. testSrc,
  378. emitOptions: EmitOptions.CCI,
  379. additionalRefs: new[] { LinqAssemblyRef },
  380. expectedOutput: "True");
  381. }
  382. [Fact]
  383. public void Test_012_UserDefinedImplicitConversion()
  384. {
  385. // User-defined implicit conversions.
  386. var testSrc = @"
  387. using System.Linq;
  388. namespace Test
  389. {
  390. public class B {}
  391. public class C
  392. {
  393. public static B b = new B();
  394. public static implicit operator B(C c)
  395. {
  396. return b;
  397. }
  398. }
  399. public class Program
  400. {
  401. public static void Main()
  402. {
  403. B b = new B();
  404. C c = new C();
  405. var a = new [] {b, c};
  406. System.Console.Write(a.SequenceEqual(new B[]{b, c}));
  407. }
  408. }
  409. }
  410. ";
  411. // NYI: When user-defined conversion lowering is implemented, replace the
  412. // NYI: error checking below with:
  413. // var compilation = CompileAndVerify(testSrc, emitOptions: EmitOptions.CCI,
  414. // additionalRefs: GetReferences(), expectedOutput: "");
  415. var compilation = CreateCompilationWithMscorlibAndSystemCore(testSrc);
  416. compilation.VerifyDiagnostics();
  417. }
  418. [Fact]
  419. public void Test_013_A_UserDefinedNullableConversions()
  420. {
  421. // Lifted user-defined conversions
  422. var testSrc = @"
  423. using System.Linq;
  424. namespace Test
  425. {
  426. public struct B { }
  427. public struct C
  428. {
  429. public static B b = new B();
  430. public static implicit operator B(C c)
  431. {
  432. return b;
  433. }
  434. }
  435. public class Program
  436. {
  437. public static void Main()
  438. {
  439. C? c = new C();
  440. B? b = new B();
  441. if (!(new [] {b, c}.SequenceEqual(new B?[] {b, c})))
  442. {
  443. System.Console.WriteLine(""Test fail at struct C? implicitly convert to struct B?"");
  444. }
  445. }
  446. }
  447. }
  448. ";
  449. // NYI: When lifted user-defined conversion lowering is implemented, replace the
  450. // NYI: error checking below with:
  451. // var compilation = CompileAndVerify(testSrc, emitOptions: EmitOptions.CCI,
  452. // additionalRefs: GetReferences(), expectedOutput: "");
  453. var compilation = CreateCompilationWithMscorlibAndSystemCore(testSrc);
  454. compilation.VerifyDiagnostics();
  455. }
  456. // Bug 10700: We should be able to infer the array type from elements of types int? and short?
  457. [Fact]
  458. public void Test_013_B_NullableConversions()
  459. {
  460. // Lifted implicit numeric conversions
  461. var testSrc = @"
  462. using System.Linq;
  463. namespace Test
  464. {
  465. public class Program
  466. {
  467. public static void Main()
  468. {
  469. int? i = 1;
  470. short? s = 2;
  471. if (!new [] {i, s}.SequenceEqual(new int?[] {i, s}))
  472. {
  473. System.Console.WriteLine(""Test fail at short? implicitly convert to int?"");
  474. }
  475. }
  476. }
  477. }
  478. ";
  479. // NYI: When lifted conversions are implemented, remove the diagnostics check
  480. // NYI: and replace it with:
  481. // var compilation = CompileAndVerify(testSrc, emitOptions: EmitOptions.CCI,
  482. // additionalRefs: GetReferences(), expectedOutput: "");
  483. var compilation = CreateCompilationWithMscorlibAndSystemCore(testSrc);
  484. compilation.VerifyDiagnostics();
  485. }
  486. [Fact]
  487. public void Test_014_LambdaExpression()
  488. {
  489. // Implicitly conversion from lambda expression to compatible delegate type
  490. var source = @"
  491. using System;
  492. namespace Test
  493. {
  494. public class Program
  495. {
  496. delegate int D(int i);
  497. public static int M(int i) {return i;}
  498. public static void Main()
  499. {
  500. var a = new [] {new D(M), (int i)=>{return i;}, x => x+1, (int i)=>{short s = 2; return s;}};
  501. Console.Write(((a is D[]) && (a.Length==4)));
  502. }
  503. }
  504. }
  505. ";
  506. CompileAndVerify(
  507. source,
  508. emitOptions: EmitOptions.CCI,
  509. additionalRefs: new[] { LinqAssemblyRef },
  510. expectedOutput: "True");
  511. }
  512. [Fact]
  513. public void Test_015_ImplicitlyTypedLocalExpr()
  514. {
  515. // local variable declared as "var" type is used inside an implicitly typed array.
  516. var source = @"
  517. using System.Linq;
  518. namespace Test
  519. {
  520. public class B { };
  521. public class C : B { };
  522. public class Program
  523. {
  524. public static void Main()
  525. {
  526. var b = new B();
  527. var c = new C();
  528. var a = new [] {b, c};
  529. System.Console.Write(a.SequenceEqual(new B[]{b, c}));
  530. }
  531. }
  532. }
  533. ";
  534. CompileAndVerify(
  535. source,
  536. emitOptions: EmitOptions.CCI,
  537. additionalRefs: new[] { LinqAssemblyRef },
  538. expectedOutput: "True");
  539. }
  540. [Fact]
  541. public void Test_016_ArrayCreationExpression()
  542. {
  543. // Array creation expression as element in implicitly typed arrays
  544. var source = @"
  545. using System;
  546. namespace Test
  547. {
  548. public class Program
  549. {
  550. public static void Main()
  551. {
  552. var a = new [] {new int[1] , new int[3] {11, 12, 13}, new int[] {21, 22, 23}};
  553. Console.Write(((a is int[][]) && (a.Length==3)));
  554. }
  555. }
  556. }
  557. ";
  558. CompileAndVerify(
  559. source,
  560. emitOptions: EmitOptions.CCI,
  561. additionalRefs: new[] { LinqAssemblyRef },
  562. expectedOutput: "True");
  563. }
  564. [Fact]
  565. public void Test_017_AnonymousObjectCreationExpression()
  566. {
  567. // Anonymous object creation expression as element in implicitly typed arrays
  568. var source = @"
  569. using System;
  570. namespace Test
  571. {
  572. public class Program
  573. {
  574. public static void Main()
  575. {
  576. var a = new [] {new {i = 2, s = ""bb""}, new {i = 3, s = ""cc""}};
  577. Console.Write(((a is Array) && (a.Length==2)));
  578. }
  579. }
  580. }
  581. ";
  582. CompileAndVerify(
  583. source,
  584. emitOptions: EmitOptions.CCI,
  585. additionalRefs: new[] { LinqAssemblyRef },
  586. expectedOutput: "True");
  587. }
  588. [Fact]
  589. public void Test_018_MemberAccessExpression()
  590. {
  591. // Member access expression as element in implicitly typed arrays
  592. var source = @"
  593. using System.Linq;
  594. using NT = Test;
  595. namespace Test
  596. {
  597. public class Program
  598. {
  599. public delegate int D(string s);
  600. public static D d1 = new D(M2);
  601. public static int M<T>(string s) {return 1;}
  602. public static int M2(string s) {return 2;}
  603. public D d2 = new D(M2);
  604. public static Program p = new Program();
  605. public static Program GetP() {return p;}
  606. public static void Main()
  607. {
  608. System.Console.Write(new [] {Program.d1, Program.M<int>, GetP().d2, int.Parse, NT::Program.M2}.SequenceEqual(
  609. new D[] {Program.d1, Program.M<int>, GetP().d2, int.Parse, NT::Program.M2}));
  610. }
  611. }
  612. }
  613. ";
  614. CompileAndVerify(
  615. source,
  616. emitOptions: EmitOptions.CCI,
  617. additionalRefs: new[] { LinqAssemblyRef },
  618. expectedOutput: "True");
  619. }
  620. [Fact]
  621. public void Test_019_JaggedArray()
  622. {
  623. // JaggedArray in implicitly typed arrays
  624. var source = @"
  625. using System;
  626. namespace Test
  627. {
  628. public class Program
  629. {
  630. public static void Main()
  631. {
  632. var a3 = new [] { new [] {new [] {1}},
  633. new [] {new int[] {2}},
  634. new int[][] {new int[] {3}},
  635. new int[][] {new [] {4}} };
  636. if ( !((a3 is int[][][]) && (a3.Length == 4)) )
  637. {
  638. Console.Write(""Test fail"");
  639. }
  640. else
  641. {
  642. Console.Write(""True"");
  643. }
  644. }
  645. }
  646. }
  647. ";
  648. CompileAndVerify(
  649. source,
  650. emitOptions: EmitOptions.CCI,
  651. additionalRefs: new[] { LinqAssemblyRef },
  652. expectedOutput: "True");
  653. }
  654. [Fact]
  655. public void Test_020_MultiDimensionalArray()
  656. {
  657. // MultiDimensionalArray in implicitly typed arrays
  658. var testSrc = @"
  659. using System;
  660. namespace Test
  661. {
  662. public class Program
  663. {
  664. public static void Main()
  665. {
  666. var a3 = new [] { new int[,,] {{{3, 4}}},
  667. new int[,,] {{{3, 4}}} };
  668. if ( !((a3 is int[][,,]) && (a3.Rank == 1) && (a3.Length == 2)) )
  669. {
  670. Console.WriteLine(0);
  671. }
  672. else
  673. {
  674. Console.WriteLine(1);
  675. }
  676. }
  677. }
  678. }";
  679. CompileAndVerify(testSrc, expectedOutput: "1");
  680. }
  681. [Fact]
  682. public void Test_021_MultiDimensionalArray_02()
  683. {
  684. // Implicitly typed arrays should can be used in creating MultiDimensionalArray
  685. var testSrc = @"
  686. using System;
  687. namespace Test
  688. {
  689. public class Program
  690. {
  691. public static void Main()
  692. {
  693. var a3 = new [,,] { {{2, 3, 4}}, {{2, 3, 4}} };
  694. if ( !((a3 is int[,,]) && (a3.Rank == 3) && (a3.Length == 6)) )
  695. {
  696. Console.WriteLine(0);
  697. }
  698. else
  699. {
  700. Console.WriteLine(1);
  701. }
  702. }
  703. }
  704. }
  705. ";
  706. CompileAndVerify(testSrc, expectedOutput: "1");
  707. }
  708. [Fact]
  709. public void Test_022_GenericMethod()
  710. {
  711. // Implicitly typed arrays used in generic method
  712. var source = @"
  713. using System.Linq;
  714. namespace Test
  715. {
  716. public class Program
  717. {
  718. public static void Main()
  719. {
  720. System.Console.Write(GM(new [] {1, 2, 3}).SequenceEqual(new int[]{1, 2, 3}));
  721. }
  722. public static T GM<T>(T t)
  723. {
  724. return t;
  725. }
  726. }
  727. }
  728. ";
  729. CompileAndVerify(
  730. source,
  731. emitOptions: EmitOptions.CCI,
  732. additionalRefs: new[] { LinqAssemblyRef },
  733. expectedOutput: "True");
  734. }
  735. [Fact]
  736. public void Test_023_Query()
  737. {
  738. // Query expression as element in implicitly typed arrays
  739. var source = @"
  740. using System;
  741. using System.Collections;
  742. using System.Collections.Generic;
  743. using System.Linq;
  744. public class Program
  745. {
  746. public static void Main()
  747. {
  748. int[] ta = new int[] {1, 2, 3, 4, 5};
  749. IEnumerable i = ta;
  750. IEnumerable[] a = new [] {i, from c in ta select c, from c in ta group c by c,
  751. from c in ta select c into g select g, from c in ta where c==3 select c,
  752. from c in ta orderby c select c, from c in ta orderby c ascending select c,
  753. from c in ta orderby c descending select c, from c in ta where c==3 orderby c select c};
  754. Console.Write((a is IEnumerable[]) && (a.Length==9));
  755. }
  756. }
  757. ";
  758. CompileAndVerify(
  759. source,
  760. emitOptions: EmitOptions.CCI,
  761. additionalRefs: new[] { LinqAssemblyRef },
  762. expectedOutput: "True");
  763. }
  764. [Fact]
  765. public void Test_023_Literal()
  766. {
  767. // Query expression as element in implicitly typed arrays
  768. var source = @"
  769. using System;
  770. using System.Linq;
  771. public class Program
  772. {
  773. public static void Main()
  774. {
  775. Console.Write(new [] {true, false}.SequenceEqual(new bool[] {true, false}));
  776. Console.Write(new [] {0123456789U, 1234567890U, 2345678901u, 3456789012UL, 4567890123Ul, 5678901234UL, 6789012345Ul, 7890123456uL, 8901234567ul, 9012345678LU, 9123456780Lu, 1234567809LU, 2345678091LU}.SequenceEqual(
  777. new ulong[] {0123456789U, 1234567890U, 2345678901u, 3456789012UL, 4567890123Ul, 5678901234UL, 6789012345Ul, 7890123456uL, 8901234567ul, 9012345678LU, 9123456780Lu, 1234567809LU, 2345678091LU}));
  778. Console.Write(new [] {0123456789, 1234567890, 2345678901, 3456789012L, 4567890123L, 5678901234L, 6789012345L, 7890123456L, 8901234567L, 9012345678L, 9123456780L, 1234567809L, 2345678091L}.SequenceEqual(
  779. new long[] {0123456789, 1234567890, 2345678901, 3456789012L, 4567890123L, 5678901234L, 6789012345L, 7890123456L, 8901234567L, 9012345678L, 9123456780L, 1234567809L, 2345678091L}));
  780. Console.Write(new [] {0x012345U, 0x6789ABU, 0xCDEFabU, 0xcdef01U, 0X123456U, 0x12579BU, 0x13579Bu, 0x14579BLU, 0x15579BLU, 0x16579BUL, 0x17579BUl, 0x18579BuL, 0x19579Bul, 0x1A579BLU, 0x1B579BLu, 0x1C579BLU, 0x1C579BLu}.SequenceEqual(
  781. new ulong[] {0x012345U, 0x6789ABU, 0xCDEFabU, 0xcdef01U, 0X123456U, 0x12579BU, 0x13579Bu, 0x14579BLU, 0x15579BLU, 0x16579BUL, 0x17579BUl, 0x18579BuL, 0x19579Bul, 0x1A579BLU, 0x1B579BLu, 0x1C579BLU, 0x1C579BLu}));
  782. Console.Write(new [] {0x012345, 0x6789AB, 0xCDEFab, 0xcdef01, 0X123456, 0x12579B, 0x13579B, 0x14579BL, 0x15579BL, 0x16579BL, 0x17579BL, 0x18579BL, 0x19579BL, 0x1A579BL, 0x1B579BL, 0x1C579BL, 0x1C579BL}.SequenceEqual(
  783. new long[] {0x012345, 0x6789AB, 0xCDEFab, 0xcdef01, 0X123456, 0x12579B, 0x13579B, 0x14579BL, 0x15579BL, 0x16579BL, 0x17579BL, 0x18579BL, 0x19579BL, 0x1A579BL, 0x1B579BL, 0x1C579BL, 0x1C579BL}));
  784. Console.Write(new [] {0123456789F, 1234567890f, 2345678901D, 3456789012d, 3.2, 3.3F, 3.4e5, 3.5E5, 3.6E+5, 3.7E-5, 3.8e+5, 3.9e-5, 3.22E5D, .234, .23456D, .245e3, .2334e7D, 3E5, 3E-5, 3E+6, 4E4D}.SequenceEqual(
  785. new double[] {0123456789F, 1234567890f, 2345678901D, 3456789012d, 3.2, 3.3F, 3.4e5, 3.5E5, 3.6E+5, 3.7E-5, 3.8e+5, 3.9e-5, 3.22E5D, .234, .23456D, .245e3, .2334e7D, 3E5, 3E-5, 3E+6, 4E4D})) ;
  786. Console.Write(new [] {0123456789M, 1234567890m, 3.3M, 3.22E5M, 3.2E+4M, 3.3E-4M, .234M, .245e3M, .2334e+7M, .24e-3M, 3E5M, 3E-5M, 3E+6M}.SequenceEqual(
  787. new decimal[] {0123456789M, 1234567890m, 3.3M, 3.22E5M, 3.2E+4M, 3.3E-4M, .234M, .245e3M, .2334e+7M, .24e-3M, 3E5M, 3E-5M, 3E+6M}));
  788. Console.Write(new [] {0123456789, 5678901234UL, 2345678901D, 3.2, 3.4E5, 3.6E+5, 3.7E-5, 3.22E5D, .234, .23456D, .245E3, 3E5, 4E4D}.SequenceEqual(
  789. new double[] {0123456789, 5678901234UL, 2345678901D, 3.2, 3.4E5, 3.6E+5, 3.7E-5, 3.22E5D, .234, .23456D, .245E3, 3E5, 4E4D}));
  790. Console.Write(new [] {0123456789, 5678901234UL, 2345678901M, 3.2M, 3.4E5M, 3.6E+5M, 3.7E-5M, .234M, .245E3M, 3E5M}.SequenceEqual(
  791. new decimal[] {0123456789, 5678901234UL, 2345678901M, 3.2M, 3.4E5M, 3.6E+5M, 3.7E-5M, .234M, .245E3M, 3E5M}));
  792. }
  793. }
  794. ";
  795. CompileAndVerify(
  796. source,
  797. emitOptions: EmitOptions.CCI,
  798. additionalRefs: new[] { LinqAssemblyRef },
  799. expectedOutput: "TrueTrueTrueTrueTrueTrueTrueTrueTrue");
  800. }
  801. #endregion
  802. #region "Error tests"
  803. [Fact]
  804. public void Error_NonArrayInitExpr()
  805. {
  806. var testSrc = @"
  807. namespace Test
  808. {
  809. public class Program
  810. {
  811. public void Foo()
  812. {
  813. var a3 = new[,,] { { { 3, 4 } }, 3, 4 };
  814. }
  815. }
  816. }
  817. ";
  818. var comp = CreateCompilationWithMscorlib(testSrc);
  819. comp.VerifyDiagnostics(
  820. // (8,46): error CS0846: A nested array initializer is expected
  821. // var a3 = new[,,] { { { 3, 4 } }, 3, 4 };
  822. Diagnostic(ErrorCode.ERR_ArrayInitializerExpected, "3").WithLocation(8, 46),
  823. // (8,49): error CS0846: A nested array initializer is expected
  824. // var a3 = new[,,] { { { 3, 4 } }, 3, 4 };
  825. Diagnostic(ErrorCode.ERR_ArrayInitializerExpected, "4").WithLocation(8, 49));
  826. }
  827. [Fact]
  828. public void Error_NonArrayInitExpr_02()
  829. {
  830. var testSrc = @"
  831. namespace Test
  832. {
  833. public class Program
  834. {
  835. public void Foo()
  836. {
  837. var a3 = new[,,] { { { 3, 4 } }, x, 4 };
  838. }
  839. }
  840. }
  841. ";
  842. var comp = CreateCompilationWithMscorlib(testSrc);
  843. comp.VerifyDiagnostics(
  844. // (8,46): error CS0103: The name 'x' does not exist in the current context
  845. // var a3 = new[,,] { { { 3, 4 } }, x, 4 };
  846. Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(8, 46),
  847. // (8,49): error CS0846: A nested array initializer is expected
  848. // var a3 = new[,,] { { { 3, 4 } }, x, 4 };
  849. Diagnostic(ErrorCode.ERR_ArrayInitializerExpected, "4").WithLocation(8, 49));
  850. }
  851. [WorkItem(543571, "DevDiv")]
  852. [Fact]
  853. public void CS0826ERR_ImplicitlyTypedArrayNoBestType()
  854. {
  855. var text = @"
  856. using System;
  857. namespace Test
  858. {
  859. public class Program
  860. {
  861. enum E
  862. {
  863. Zero,
  864. FortyTwo = 42
  865. };
  866. public static void Main()
  867. {
  868. E[] a = new[] { E.FortyTwo, 0 }; // Dev10 error CS0826
  869. Console.WriteLine(String.Format(""Type={0}, a[0]={1}, a[1]={2}"", a.GetType(), a[0], a[1]));
  870. }
  871. }
  872. }
  873. ";
  874. CreateCompilationWithMscorlib(text).VerifyDiagnostics(
  875. // (16,21): error CS0826: No best type found for implicitly-typed array
  876. // E[] a = new[] { E.FortyTwo, 0 }; // Dev10 error CS0826
  877. Diagnostic(ErrorCode.ERR_ImplicitlyTypedArrayNoBestType, "new[] { E.FortyTwo, 0 }").WithLocation(16, 21));
  878. }
  879. #endregion
  880. }
  881. }