PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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