PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/Diagnostics/Roslyn/Test/Performance/SpecializedEnumerableCreationAnalyzerTests.cs

https://github.com/EkardNT/Roslyn
C# | 308 lines | 288 code | 19 blank | 1 comment | 0 complexity | 6864a128c65e9043b1d997b562021788 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;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.CodeAnalysis.Diagnostics;
  8. using Roslyn.Diagnostics.Analyzers;
  9. using Roslyn.Diagnostics.Analyzers.CSharp;
  10. using Roslyn.Diagnostics.Analyzers.VisualBasic;
  11. using Xunit;
  12. namespace Microsoft.CodeAnalysis.UnitTests.Performance
  13. {
  14. public class SpecializedEnumerableCreationAnalyzerTests : DiagnosticAnalyzerTestBase
  15. {
  16. protected override IDiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
  17. {
  18. return new CSharpSpecializedEnumerableCreationAnalyzer();
  19. }
  20. protected override IDiagnosticAnalyzer GetBasicDiagnosticAnalyzer()
  21. {
  22. return new BasicSpecializedEnumerableCreationAnalyzer();
  23. }
  24. [Fact]
  25. public void ReturnEmptyArrayCSharp()
  26. {
  27. VerifyCSharp(@"
  28. using System.Collections.Generic;
  29. class C
  30. {
  31. IEnumerable<int> M1() { return new int[0]; }
  32. IEnumerable<int> M2() { return new int[0] { }; }
  33. int[] M3() { return new int[0]; }
  34. }
  35. ",
  36. GetCSharpResultAt(6, 36, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule),
  37. GetCSharpResultAt(7, 36, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule));
  38. }
  39. [Fact]
  40. public void ReturnSingletonArrayCSharp()
  41. {
  42. VerifyCSharp(@"
  43. using System.Collections.Generic;
  44. class C
  45. {
  46. IEnumerable<int> M1() { return new int[1]; }
  47. IEnumerable<int> M2() { return new int[1] { 1 }; }
  48. IEnumerable<int> M3() { return new[] { 1 }; }
  49. int[] M4() { return new[] { 1 }; }
  50. }
  51. ",
  52. GetCSharpResultAt(6, 36, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  53. GetCSharpResultAt(7, 36, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  54. GetCSharpResultAt(8, 36, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule));
  55. }
  56. [Fact]
  57. public void ReturnLinqEmptyEnumerableCSharp()
  58. {
  59. VerifyCSharp(@"
  60. using System.Collections.Generic;
  61. using System.Linq;
  62. class C
  63. {
  64. IEnumerable<int> M1() { return Enumerable.Empty<int>(); }
  65. }
  66. ",
  67. GetCSharpResultAt(7, 36, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule));
  68. }
  69. [Fact(Skip = "855425")]
  70. public void ReturnArrayWithinExpressionCSharp()
  71. {
  72. VerifyCSharp(@"
  73. using System.Collections.Generic;
  74. class C
  75. {
  76. IEnumerable<int> M1() { return 0 == 1 ? new[] { 1 } : new[] { 2 }; }
  77. IEnumerable<int> M2() { return null ?? new int[0]; }
  78. }
  79. ",
  80. GetCSharpResultAt(6, 45, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  81. GetCSharpResultAt(6, 59, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  82. GetCSharpResultAt(7, 44, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule));
  83. }
  84. [Fact(Skip = "855425")]
  85. public void ReturnLinqEmptyEnumerableWithinExpressionCSharp()
  86. {
  87. VerifyCSharp(@"
  88. using System.Collections.Generic;
  89. using System.Linq;
  90. class C
  91. {
  92. IEnumerable<int> M1() { return 0 == 1 ? Enumerable.Empty<int>() : null; }
  93. IEnumerable<int> M2() { return null ?? Enumerable.Empty<int>(); }
  94. }
  95. ",
  96. GetCSharpResultAt(7, 45, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule),
  97. GetCSharpResultAt(8, 44, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule));
  98. }
  99. [Fact]
  100. public void ReturnMultiElementArrayCSharp()
  101. {
  102. VerifyCSharp(@"
  103. using System.Collections.Generic;
  104. class C
  105. {
  106. IEnumerable<int> M1() { return new int[2]; }
  107. IEnumerable<int> M2() { return new int[2] { 1, 2 }; }
  108. IEnumerable<int> M3() { return new[] { 1, 2 }; }
  109. int[] M4() { return new[] { 1, 2 }; }
  110. }
  111. ");
  112. }
  113. [Fact]
  114. public void ReturnJaggedArrayCSharp()
  115. {
  116. VerifyCSharp(@"
  117. using System.Collections.Generic;
  118. class C
  119. {
  120. IEnumerable<int[]> M1() { return new int[2][] { new int[0], new int[0] }; }
  121. IEnumerable<int[]> M2() { return new[] { new[] { 1 } }; }
  122. IEnumerable<int[]> M3() { return new[] { new[] { 1, 2, 3 }, new[] { 1 } }; }
  123. }
  124. ",
  125. GetCSharpResultAt(7, 38, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule));
  126. }
  127. [Fact(Skip = "855425")]
  128. public void ImplicitConversionToNestedEnumerableCSharp()
  129. {
  130. VerifyCSharp(@"
  131. using System.Collections.Generic;
  132. class C
  133. {
  134. IEnumerable<IEnumerable<int>> M1() { return new[] { new[] { 1 } }; }
  135. }
  136. ",
  137. GetCSharpResultAt(5, 49, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  138. GetCSharpResultAt(5, 57, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule));
  139. }
  140. [Fact]
  141. public void ReturnEmptyArrayBasic()
  142. {
  143. VerifyBasic(@"
  144. Imports System.Collections.Generic
  145. Class C
  146. Function M1() As IEnumerable(Of Integer)
  147. Return New Integer(0) {}
  148. End Function
  149. Function M2() As IEnumerable(Of Integer)
  150. Return {}
  151. End Function
  152. End Class
  153. ",
  154. GetBasicResultAt(6, 16, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule),
  155. GetBasicResultAt(9, 16, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule));
  156. }
  157. [Fact]
  158. public void ReturnLinqEmptyEnumerableBasic()
  159. {
  160. VerifyBasic(@"
  161. Imports System.Collections.Generic
  162. Imports System.Linq
  163. Class C
  164. Function M1() As IEnumerable(Of Integer)
  165. Return Enumerable.Empty(Of Integer)()
  166. End Function
  167. End Class
  168. ",
  169. GetBasicResultAt(7, 16, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule));
  170. }
  171. [Fact]
  172. public void ReturnSingletonArrayBasic()
  173. {
  174. VerifyBasic(@"
  175. Imports System.Collections.Generic
  176. Class C
  177. Function M1() As IEnumerable(Of Integer)
  178. Return New Integer(1) {1}
  179. End Function
  180. Function M2() As IEnumerable(Of Integer)
  181. Return {1}
  182. End Function
  183. End Class
  184. ",
  185. GetBasicResultAt(6, 16, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  186. GetBasicResultAt(9, 16, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule));
  187. }
  188. [Fact(Skip = "855425")]
  189. public void ReturnArrayWithinExpressionBasic()
  190. {
  191. VerifyBasic(@"
  192. Imports System.Collections.Generic
  193. Class C
  194. Function M1() As IEnumerable(Of Integer)
  195. Return If(True, {1}, {2})
  196. End Function
  197. Function M2() As IEnumerable(Of Integer)
  198. Return If(True, {1})
  199. End Function
  200. End Class
  201. ",
  202. GetBasicResultAt(6, 25, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  203. GetBasicResultAt(6, 30, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  204. GetBasicResultAt(9, 25, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule));
  205. }
  206. [Fact(Skip = "855425")]
  207. public void ReturnLinqEmptyEnumerableWithinExpressionBasic()
  208. {
  209. VerifyBasic(@"
  210. Imports System.Collections.Generic
  211. Imports System.Linq
  212. Class C
  213. Function M1() As IEnumerable(Of Integer)
  214. Return If(True, Enumerable.Empty(Of Integer)(), Nothing)
  215. End Function
  216. Function M2() As IEnumerable(Of Integer)
  217. Return If(True, Enumerable.Empty(Of Integer)())
  218. End Function
  219. End Class
  220. ",
  221. GetBasicResultAt(7, 25, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule),
  222. GetBasicResultAt(10, 25, SpecializedEnumerableCreationAnalyzer.UseEmptyEnumerableRule));
  223. }
  224. [Fact]
  225. public void ReturnMultiElementArrayBasic()
  226. {
  227. VerifyBasic(@"
  228. Imports System.Collections.Generic
  229. Class C
  230. Function M1() As IEnumerable(Of Integer)
  231. Return New Integer(2) {1, 2}
  232. End Function
  233. Function M2() As IEnumerable(Of Integer)
  234. Return {1, 2}
  235. End Function
  236. End Class
  237. ");
  238. }
  239. [Fact]
  240. public void ReturnJaggedArrayBasic()
  241. {
  242. VerifyBasic(@"
  243. Imports System.Collections.Generic
  244. Class C
  245. Function M1() As IEnumerable(Of Integer())
  246. Return New Integer(1)() {New Integer() {}, New Integer() {}}
  247. End Function
  248. Function M2() As IEnumerable(Of Integer())
  249. Return {({1})}
  250. End Function
  251. Function M3() As IEnumerable(Of Integer())
  252. Return {({1, 2, 3}), ({1})}
  253. End Function
  254. End Class
  255. ",
  256. GetBasicResultAt(9, 16, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule));
  257. }
  258. [Fact(Skip = "855425")]
  259. public void ImplicitConversionToNestedEnumerableBasic()
  260. {
  261. VerifyBasic(@"
  262. Imports System.Collections.Generic
  263. Class C
  264. Function M1() As IEnumerable(Of IEnumerable(Of Integer))
  265. Return {({1})}
  266. End Function
  267. End Class
  268. ",
  269. GetBasicResultAt(6, 16, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule),
  270. GetBasicResultAt(6, 17, SpecializedEnumerableCreationAnalyzer.UseSingletonEnumerableRule));
  271. }
  272. }
  273. }