-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds support and tests for initializing arrays with collection expres…
…sions (#256)
- Loading branch information
Showing
5 changed files
with
90 additions
and
16 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Cecilifier.Core.Tests/Tests/OutputBased/CollectionExpressionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Cecilifier.Core.Tests.Framework; | ||
using NUnit.Framework; | ||
|
||
namespace Cecilifier.Core.Tests.OutputBased; | ||
|
||
public class CollectionExpressionTests : OutputBasedTestBase | ||
{ | ||
[Test] | ||
public void ArrayWith3OrMoreElements() | ||
{ | ||
AssertOutput("int[] mediumArray = [1, 2, 3]; System.Console.WriteLine(mediumArray[0] + mediumArray[2]);", "4"); | ||
} | ||
|
||
[Test] | ||
public void ArrayWith2OrLessElements() | ||
{ | ||
AssertOutput("int[] mediumArray = [1, 2]; System.Console.WriteLine(mediumArray[0] + mediumArray[1]);", "3"); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Cecilifier.Core.Tests/Tests/Unit/CollectionExpressionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using NUnit.Framework; | ||
|
||
namespace Cecilifier.Core.Tests.Tests.Unit; | ||
|
||
[TestFixture] | ||
public class CollectionExpressionTests : CecilifierUnitTestBase | ||
{ | ||
[Test] | ||
public void ArrayWith3OrMoreElements_UsesOptimizedInitialization() | ||
{ | ||
var result = RunCecilifier("int[] mediumArray = [1, 2, 3];"); | ||
var cecilified = result.GeneratedCode.ReadToEnd(); | ||
Assert.That(cecilified, Does.Match("//__StaticArrayInitTypeSize=12 struct.")); | ||
Assert.That(cecilified, Does.Match(@"var fld_arrayInitializerData_\d+ = new FieldDefinition\(""[A-F0-9]+"",.+, st_rawDataTypeVar_\d+\);")); | ||
Assert.That(cecilified, Does.Match(@"il_topLevelMain_3.Emit\(OpCodes.Ldtoken, fld_arrayInitializerData_\d+\);")); | ||
} | ||
|
||
[Test] | ||
public void ArrayWith2OrLessElements_DoesNotUsesOptimizedInitialization() | ||
{ | ||
var result = RunCecilifier("int[] mediumArray = [1, 2];"); | ||
var cecilified = result.GeneratedCode.ReadToEnd(); | ||
Assert.That(cecilified, Does.Not.Match(@"//__StaticArrayInitTypeSize=\d+ struct.")); | ||
Assert.That(cecilified, Does.Match(""" | ||
//int\[\] mediumArray = \[1, 2\]; | ||
\s+var (?<array>l_mediumArray_\d+) = new VariableDefinition\(assembly.MainModule.TypeSystem.Int32.MakeArrayType\(\)\); | ||
\s+m_topLevelStatements_1.Body.Variables.Add\(\k<array>\); | ||
\s+(?<il>il_topLevelMain_\d+.Emit\(OpCodes\.)Ldc_I4, 2\); | ||
\s+\k<il>Newarr, assembly.MainModule.TypeSystem.Int32\); | ||
\s+\k<il>Dup\); | ||
\s+\k<il>Ldc_I4, 0\); | ||
\s+\k<il>Ldc_I4, 1\); | ||
\s+\k<il>Stelem_I4\); | ||
\s+\k<il>Dup\); | ||
\s+\k<il>Ldc_I4, 1\); | ||
\s+\k<il>Ldc_I4, 2\); | ||
\s+\k<il>Stelem_I4\); | ||
""")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters